Debian Codegraph @

similar/main/segment.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 /*
9 *
10 * Segment Loading Stuff
11 *
12 */
13
14
15 #include "segment.h"
16 #include "physfsx.h"
17 #include "physfs-serial.h"
18 #include "d_underlying_value.h"
19
20 namespace dcx {
21
22 namespace {
23
24 struct composite_side
25 {
26 const shared_side &sside;
27 const unique_side &uside;
28 };
29
30 DEFINE_SERIAL_UDT_TO_MESSAGE(composite_side, s, (s.sside.wall_num, s.uside.tmap_num, s.uside.tmap_num2));
31 ASSERT_SERIAL_UDT_MESSAGE_SIZE(composite_side, 6);
32
33 static imsegidx_t build_segnum_from_optional_segnum(std::optional<segnum_t> value)
34 {
35 return value.value_or(segment_none);
36 }
37
38 static imsegidx_t build_segnum_from_untrusted_value(auto value)
39 {
40 return build_segnum_from_optional_segnum(vmsegidx_t::check_nothrow_index(value));
41 }
42
43 }
44
45 std::optional<sidenum_t> build_sidenum_from_untrusted(const uint8_t untrusted)
46 {
47 switch (untrusted)
48 {
49 case static_cast<uint8_t>(sidenum_t::WLEFT):
50 case static_cast<uint8_t>(sidenum_t::WTOP):
51 case static_cast<uint8_t>(sidenum_t::WRIGHT):
52 case static_cast<uint8_t>(sidenum_t::WBOTTOM):
53 case static_cast<uint8_t>(sidenum_t::WBACK):
54 case static_cast<uint8_t>(sidenum_t::WFRONT):
55 return sidenum_t{untrusted};
56 default:
57 return std::nullopt;
58 }
59 }
60
61 void segment_side_wall_tmap_write(PHYSFS_File *fp, const shared_side &sside, const unique_side &uside)
62 {
63 PHYSFSX_serialize_write(fp, composite_side{sside, uside});
64 }
65
66 imsegidx_t read_untrusted_segnum_le16(NamedPHYSFS_File fp)
67 {
68 return build_segnum_from_untrusted_value(PHYSFSX_readULE16(fp));
69 }
70
71 imsegidx_t read_untrusted_segnum_le32(NamedPHYSFS_File fp)
72 {
73 return build_segnum_from_untrusted_value(PHYSFSX_readULE32(fp));
74 }
75
76 imsegidx_t read_untrusted_segnum_xe16(NamedPHYSFS_File fp, const physfsx_endian swap)
77 {
78 return build_segnum_from_untrusted_value(PHYSFSX_readUXE16(fp, swap));
79 }
80
81 imsegidx_t read_untrusted_segnum_xe32(NamedPHYSFS_File fp, const physfsx_endian swap)
82 {
83 return build_segnum_from_untrusted_value(PHYSFSX_readUXE32(fp, swap));
84 }
85
86 }
87
88 #if DXX_BUILD_DESCENT == 2
89 namespace dsx {
90
91 static std::optional<delta_light_index> build_delta_light_index_from_untrusted(const uint16_t i)
92 {
93 if (i < MAX_DELTA_LIGHTS)
94 return delta_light_index{i};
95 else
96 return std::nullopt;
97 }
98
99 /*
100 * reads a delta_light structure from a PHYSFS_File
101 */
102 void delta_light_read(delta_light *dl, const NamedPHYSFS_File fp)
103 {
104 dl->segnum = read_untrusted_segnum_le16(fp);
105 dl->sidenum = build_sidenum_from_untrusted(PHYSFSX_readByte(fp)).value_or(sidenum_t::WLEFT);
106 PHYSFSX_skipBytes<1>(fp);
107 dl->vert_light[side_relative_vertnum::_0] = PHYSFSX_readByte(fp);
108 dl->vert_light[side_relative_vertnum::_1] = PHYSFSX_readByte(fp);
109 dl->vert_light[side_relative_vertnum::_2] = PHYSFSX_readByte(fp);
110 dl->vert_light[side_relative_vertnum::_3] = PHYSFSX_readByte(fp);
111 }
112
113
114 /*
115 * reads a dl_index structure from a PHYSFS_File
116 */
117 void dl_index_read(dl_index *di, const NamedPHYSFS_File fp)
118 {
119 di->segnum = read_untrusted_segnum_le16(fp);
120 di->sidenum = build_sidenum_from_untrusted(PHYSFSX_readByte(fp)).value_or(sidenum_t::WLEFT);
121 const auto count = PHYSFSX_readByte(fp);
122 if (const auto i{build_delta_light_index_from_untrusted(PHYSFSX_readSLE16(fp))}; i)
123 {
124 di->count = count;
125 di->index = *i;
126 }
127 else
128 {
129 di->count = 0;
130 di->index = {};
131 }
132 }
133
134 void segment2_write(const cscusegment s2, PHYSFS_File *fp)
135 {
136 PHYSFSX_writeU8(fp, underlying_value(s2.s.special));
137 PHYSFSX_writeU8(fp, underlying_value(s2.s.matcen_num));
138 PHYSFSX_writeU8(fp, underlying_value(s2.s.station_idx));
139 PHYSFSX_writeU8(fp, underlying_value(s2.s.s2_flags));
140 PHYSFSX_writeFix(fp, s2.u.static_light);
141 }
142
143 void delta_light_write(const delta_light *dl, PHYSFS_File *fp)
144 {
145 PHYSFS_writeSLE16(fp, dl->segnum);
146 PHYSFSX_writeU8(fp, underlying_value(dl->sidenum));
147 PHYSFSX_writeU8(fp, 0);
148 PHYSFSX_writeU8(fp, dl->vert_light[side_relative_vertnum::_0]);
149 PHYSFSX_writeU8(fp, dl->vert_light[side_relative_vertnum::_1]);
150 PHYSFSX_writeU8(fp, dl->vert_light[side_relative_vertnum::_2]);
151 PHYSFSX_writeU8(fp, dl->vert_light[side_relative_vertnum::_3]);
152 }
153
154 void dl_index_write(const dl_index *di, PHYSFS_File *fp)
155 {
156 PHYSFS_writeSLE16(fp, di->segnum);
157 PHYSFSX_writeU8(fp, underlying_value(di->sidenum));
158 PHYSFSX_writeU8(fp, di->count);
159 PHYSFS_writeSLE16(fp, underlying_value(di->index));
160 }
161
162 }
163 #endif