OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
serialize.h
Go to the documentation of this file.
1#pragma once
2
3#include <Tempest/IDevice>
4#include <Tempest/ODevice>
5#include <Tempest/Pixmap>
6
7#include <Tempest/Matrix4x4>
8
9#include <vector>
10#include <unordered_set>
11#include <cstdint>
12#include <type_traits>
13#include <ctime>
14
15#include <miniz.h>
16
17#include <zenkit/DaedalusVm.hh>
18#include <zenkit/addon/daedalus.hh>
19#include <zenkit/ModelAnimation.hh>
20
21#include "gametime.h"
22#include "constants.h"
23#include "utils/string_frm.h"
24
25class WayPoint;
26class Npc;
27class Interactive;
28class World;
29class FpLock;
30class ScriptFn;
31class SaveGameHeader;
32
33class Serialize {
34 public:
35 enum Version : uint16_t {
36 Current = 53,
38
44 First = 36, // first zip-based version
45 };
46 Serialize(Tempest::ODevice& fout);
47 Serialize(Tempest::IDevice& fin);
48 Serialize(Serialize&&)=default;
49 ~Serialize();
50
51 uint16_t version() const { return wldVer; }
52 void setVersion(uint16_t v) { wldVer = v; }
53 uint16_t globalVersion() const { return curVer; }
54 void setGlobalVersion(uint16_t v) { curVer = v; }
55
56 template<class ... Args>
57 bool setEntry(const Args& ... args) {
58 string_frm s(args...);
59 return implSetEntry(s);
60 }
61
62 template<class ... Args>
63 uint32_t directorySize(const Args& ... args) {
64 string_frm s(args...);
65 return implDirectorySize(s);
66 }
67
68 void setContext(World* ctx) { this->ctx=ctx; }
69 std::string_view worldName() const;
70
71 // raw
72 void writeBytes(const void* v,size_t sz);
73 void readBytes (void* v,size_t sz);
74
75 template<class ... Arg>
76 void write(const Arg& ... a){
77 (implWrite(a),... );
78 }
79
80 template<class ... Arg>
81 void read(Arg& ... a){
82 (implRead(a),... );
83 }
84
85 void readNpc(zenkit::DaedalusVm& vm, std::shared_ptr<zenkit::INpc>& npc);
86
87 private:
88 Serialize();
89
90 // trivial types
91 void implWrite(bool i) { implWrite(uint8_t(i ? 1 : 0)); }
92 void implRead (bool& i) { uint8_t x=0; read(x); i=(x!=0); }
93
94 void implWrite(char i) { writeBytes(&i,sizeof(i)); }
95 void implRead (char& i) { readBytes (&i,sizeof(i)); }
96
97 void implWrite(uint8_t i) { writeBytes(&i,sizeof(i)); }
98 void implRead (uint8_t& i) { readBytes (&i,sizeof(i)); }
99
100 void implWrite(uint16_t i) { writeBytes(&i,sizeof(i)); }
101 void implRead (uint16_t& i) { readBytes (&i,sizeof(i)); }
102
103 void implWrite(int32_t i) { writeBytes(&i,sizeof(i)); }
104 void implRead (int32_t& i) { readBytes (&i,sizeof(i)); }
105
106 void implWrite(uint32_t i) { writeBytes(&i,sizeof(i)); }
107 void implRead (uint32_t& i) { readBytes (&i,sizeof(i)); }
108
109 void implWrite(uint64_t i) { writeBytes(&i,sizeof(i)); }
110 void implRead (uint64_t& i) { readBytes (&i,sizeof(i)); }
111
112 void implWrite(float i) { writeBytes(&i,sizeof(i)); }
113 void implRead (float& i) { readBytes (&i,sizeof(i)); }
114
115 void implWrite(zenkit::DaedalusDataType i) { write((uint32_t) i); }
116 void implRead (zenkit::DaedalusDataType& i) { read((uint32_t&) i); }
117
118 template<class T, std::enable_if_t<!std::is_same<T,uint32_t>::value && !std::is_same<T,uint64_t>::value && std::is_same<T,size_t>::value,bool> = true>
119 void implWrite(T ) = delete;
120 template<class T, std::enable_if_t<!std::is_same<T,uint32_t>::value && !std::is_same<T,uint64_t>::value && std::is_same<T,size_t>::value,bool> = true>
121 void implRead(T&) = delete;
122
123 // typedef
124 void implWrite(gtime i) { writeBytes(&i,sizeof(i)); }
125 void implRead (gtime& i) { readBytes (&i,sizeof(i)); }
126
127 void implWrite(WalkBit i) { writeBytes(&i,sizeof(i)); }
128 void implRead (WalkBit& i) { readBytes (&i,sizeof(i)); }
129
130 void implWrite(BodyState i) { writeBytes(&i,sizeof(i)); }
131 void implRead (BodyState&i) { readBytes (&i,sizeof(i)); }
132
133 void implWrite(Attitude i) { writeBytes(&i,sizeof(i));}
134 void implRead (Attitude& i) { readBytes (&i,sizeof(i)); }
135
136 void implWrite(WeaponState w);
137 void implRead (WeaponState &w);
138
139 // composite types
140 void implWrite(const Tempest::Vec3& s) { writeBytes(&s,sizeof(s)); }
141 void implRead (Tempest::Vec3& s) { readBytes (&s,sizeof(s)); }
142
143 void implWrite(const Tempest::Matrix4x4& i) { writeBytes(&i,sizeof(i)); }
144 void implRead (Tempest::Matrix4x4& i) { readBytes (&i,sizeof(i)); }
145
146 void implWrite(const zenkit::AnimationSample& i);
147 void implRead (zenkit::AnimationSample& i);
148
149 // strings
150 void implWrite(const std::string& s);
151 void implRead (std::string& s);
152
153 void implWrite(std::string_view s);
154
155 // vectors
156 template<class T>
157 void implWrite(const std::vector<T>& s) {
158 implWriteVec(s,std::is_trivial<T>());
159 }
160
161 template<class T>
162 void implRead (std::vector<T>& s){
163 implReadVec(s,std::is_trivial<T>());
164 }
165
166 void implWrite(const std::vector<bool>& s) {
167 uint32_t sz=uint32_t(s.size());
168 write(sz);
169 for(size_t i=0; i<s.size(); ++i)
170 write(s[i]);
171 }
172
173 void implRead (std::vector<bool>& s) {
174 uint32_t sz=0;
175 read(sz);
176 s.resize(sz);
177 for(size_t i=0; i<s.size(); ++i) {
178 bool b=false;
179 read(b);
180 s[i] = b;
181 }
182 }
183
184 template<class T>
185 void implWriteVec(const std::vector<T>& s,std::false_type) {
186 uint32_t sz=uint32_t(s.size());
187 write(sz);
188 for(auto& i:s)
189 write(i);
190 }
191
192 template<class T>
193 void implWriteVec(const std::vector<T>& s,std::true_type) {
194 uint32_t sz=uint32_t(s.size());
195 write(sz);
196 writeBytes(s.data(),sz*sizeof(T));
197 }
198
199 template<class T>
200 void implReadVec(std::vector<T>& s,std::false_type) {
201 uint32_t sz=0;
202 read(sz);
203 s.resize(sz);
204 for(auto& i:s)
205 read(i);
206 }
207
208 template<class T>
209 void implReadVec(std::vector<T>& s,std::true_type) {
210 uint32_t sz=0;
211 read(sz);
212 s.resize(sz);
213 readBytes(s.data(),sz*sizeof(T));
214 }
215
216 // c-arrays
217 template<class T, size_t sz>
218 void implWrite(const T (&s)[sz]) {
219 implWriteArr(s,std::is_trivial<T>());
220 }
221
222 template<class T, size_t sz>
223 void implWriteArr(const T (&s)[sz],std::false_type) {
224 for(size_t i=0; i<sz; ++i)
225 write(s[i]);
226 }
227
228 template<class T, size_t sz>
229 void implWriteArr(const T (&s)[sz],std::true_type) {
230 writeBytes(s,sz*sizeof(T));
231 }
232
233 template<class T, size_t sz>
234 void implRead(T (&s)[sz]) {
235 implReadArr(s,std::is_trivial<T>());
236 }
237
238 template<class T, size_t sz>
239 void implReadArr(T (&s)[sz],std::false_type) {
240 for(size_t i=0; i<sz; ++i)
241 read(s[i]);
242 }
243
244 template<class T, size_t sz>
245 void implReadArr(T (&s)[sz],std::true_type) {
246 readBytes(s,sz*sizeof(T));
247 }
248
249 void implWrite(const SaveGameHeader& p);
250 void implRead (SaveGameHeader& p);
251
252 void implWrite(const Tempest::Pixmap& p);
253 void implRead (Tempest::Pixmap& p);
254
255 void implWrite(const zenkit::INpc& h);
256
257 void implWrite(const FpLock& fp);
258 void implRead (FpLock& fp);
259
260 // pointers
261 void implWrite(const WayPoint* wptr);
262 void implRead (const WayPoint*& wptr);
263
264 void implWrite(const ScriptFn& fn);
265 void implRead (ScriptFn& fn);
266
267 void implWrite(const Npc* npc);
268 void implRead (const Npc*& npc);
269
270 void implWrite(Npc* npc);
271 void implRead (Npc*& npc);
272
273 void implWrite(Interactive* mobsi);
274 void implRead (Interactive*& mobsi);
275
276 static size_t writeFunc(void *pOpaque, uint64_t file_ofs, const void *pBuf, size_t n);
277 static size_t readFunc (void *pOpaque, uint64_t file_ofs, void *pBuf, size_t n);
278
279 void closeEntry();
280 bool implSetEntry(std::string_view e);
281 uint32_t implDirectorySize(std::string_view e);
282
283 uint16_t curVer = Version::Current;
284 uint16_t wldVer = Version::Current;
285
286 std::string tmpStr;
287 std::unordered_set<std::string> outFileList;
288 World* ctx = nullptr;
289
290 mz_zip_archive impl = {};
291 std::string entryName;
292 std::vector<uint8_t> entryBuf;
293 uint64_t curOffset = 0;
294 uint64_t readOffset = 0;
295 Tempest::ODevice* fout = nullptr;
296 Tempest::IDevice* fin = nullptr;
297 };
298
Definition fplock.h:6
Definition npc.h:25
Serialize(Serialize &&)=default
void readNpc(zenkit::DaedalusVm &vm, std::shared_ptr< zenkit::INpc > &npc)
void setContext(World *ctx)
Definition serialize.h:68
uint16_t globalVersion() const
Definition serialize.h:53
void write(const Arg &... a)
Definition serialize.h:76
bool setEntry(const Args &... args)
Definition serialize.h:57
void writeBytes(const void *v, size_t sz)
void readBytes(void *v, size_t sz)
uint32_t directorySize(const Args &... args)
Definition serialize.h:63
uint16_t version() const
Definition serialize.h:51
void read(Arg &... a)
Definition serialize.h:81
void setVersion(uint16_t v)
Definition serialize.h:52
std::string_view worldName() const
Definition serialize.cpp:87
void setGlobalVersion(uint16_t v)
Definition serialize.h:54
Definition world.h:31
WalkBit
Definition constants.h:209
Attitude
Definition constants.h:234
BodyState
Definition constants.h:140
WeaponState
Definition constants.h:191