OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
archive.h
1/*
2===========================================================================
3Copyright (C) 2025 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// archive.h: OpenMoHAA Archiver
24
25#pragma once
26
27#include "g_local.h"
28#include "class.h"
29#include "str.h"
30#include "vector.h"
31
32#define ARCHIVE_NULL_POINTER (-654321)
33#define ARCHIVE_POINTER_VALID (0)
34#define ARCHIVE_POINTER_NULL (ARCHIVE_NULL_POINTER)
35#define ARCHIVE_POINTER_SELF_REFERENTIAL (-123456)
36#define ARCHIVE_USE_TYPES
37
38typedef enum {
39 ARCHIVE_NONE,
40 ARCHIVE_WRITE,
41 ARCHIVE_READ
42} archivemode_e;
43
44enum {
45 pointer_fixup_ptr,
46 pointer_fixup_normal,
47 pointer_fixup_safe
48};
49
50typedef struct {
51 void **ptr;
52 int index;
53 int type;
55
56using fileSize_t = uint32_t;
57
58class ArchiveFile
59{
60protected:
61 str filename;
62 size_t length;
63 byte *buffer;
64 byte *pos;
65 size_t bufferlength;
66 bool writing;
67 bool opened;
68
69public:
70 ArchiveFile();
71 ~ArchiveFile();
72 void Close();
73 const char *Filename(void);
74 qboolean Compress();
75 size_t Length(void);
76 size_t Pos(void);
77 size_t Tell(void);
78 qboolean Seek(size_t newpos);
79 qboolean OpenRead(const char *name);
80 qboolean OpenWrite(const char *name);
81 qboolean Read(void *dest, size_t size);
82 qboolean Write(const void *source, size_t size);
83};
84
85class Archiver
86{
87private:
88 Container<LightClass *> classpointerList;
90
91protected:
92 str filename;
93 qboolean fileerror;
94 ArchiveFile archivefile;
95 int archivemode;
96 int numclassespos;
97 qboolean harderror;
98 size_t m_iNumBytesIO;
99 qboolean silent;
100
101 void CheckRead(void);
102 void CheckType(int type);
103 int ReadType(void);
104 fileSize_t ReadSize(void);
105 void CheckSize(int type, fileSize_t size);
106 void ArchiveData(int type, void *data, size_t size);
107
108 void CheckWrite(void);
109 void WriteType(int type);
110 void WriteSize(fileSize_t size);
111
112public:
113 Archiver();
114 ~Archiver();
115 void FileError(const char *fmt, ...);
116 void Close(void);
117
118 qboolean Read(str& name, qboolean harderror = qtrue);
119 qboolean Read(const char *name, qboolean harderror = qtrue);
120 Class *ReadObject(void);
121
122 qboolean Create(str& name, qboolean harderror = qtrue);
123 qboolean Create(const char *name, qboolean harderror = qtrue);
124
125 qboolean Loading(void);
126 qboolean Saving(void);
127 qboolean NoErrors(void);
128
129 void ArchiveVector(Vector *vec);
130 void ArchiveQuat(Quat *quat);
131 void ArchiveInteger(int *num);
132 void ArchiveUnsigned(unsigned *unum);
133 void ArchiveSize(long *unum);
134 void ArchiveByte(byte *num);
135 void ArchiveChar(char *ch);
136 void ArchiveShort(short *num);
137 void ArchiveUnsignedShort(unsigned short *num);
138 void ArchiveFloat(float *num);
139 void ArchiveDouble(double *num);
140 void ArchiveBoolean(qboolean *boolean);
141 void ArchiveString(str *string);
142 void ArchiveObjectPointer(LightClass **ptr);
143 void ArchiveObjectPointer(Class **ptr);
144 void ArchiveObjectPosition(LightClass *obj);
145 void ArchiveSafePointer(SafePtrBase *ptr);
146 void ArchiveEventPointer(Event **ev);
147 void ArchiveBool(bool *boolean);
148 void ArchivePosition(int *pos);
149 void ArchiveSvsTime(int *time);
150 void ArchiveVec2(vec2_t vec);
151 void ArchiveVec3(vec3_t vec);
152 void ArchiveVec4(vec4_t vec);
153
154 void ArchiveRaw(void *data, size_t size);
155 void ArchiveObject(Class *obj);
156 void ArchiveObject(SafePtrBase *obj); // Added in OPM
157
158 qboolean ObjectPositionExists(void *obj);
159
160 void Reset();
161 size_t Counter() const;
162
163 void ArchiveConfigString(int cs);
164 void SetSilent(bool bSilent);
165};
166
167template<class Type>
168inline void Container<Type>::Archive(Archiver& arc, void (*ArchiveFunc)(Archiver& arc, Type *obj))
169{
170 Type *obj;
171 int num;
172 int i;
173
174 if (arc.Loading()) {
175 arc.ArchiveInteger(&num);
176 Resize(num);
177
178 if (num > numobjects) {
179 numobjects = num;
180 }
181
182 for (i = 0; i < num; i++) {
183 obj = new (objlist + i) Type();
184 ArchiveFunc(arc, obj);
185 }
186 } else {
187 num = numobjects;
188 arc.ArchiveInteger(&num);
189
190 for (i = 0; i < num; i++) {
191 ArchiveFunc(arc, &objlist[i]);
192 }
193 }
194}
195
196template<>
197inline void Container<str>::Archive(Archiver& arc)
198{
199 int i, num;
200
201 if (arc.Loading()) {
202 ClearObjectList();
203 arc.ArchiveInteger(&num);
204 Resize(num);
205 } else {
206 num = numobjects;
207 arc.ArchiveInteger(&num);
208 }
209 for (i = 1; i <= num; i++) {
210 arc.ArchiveString(AddressOfObjectAt(i));
211 }
212}
213
214template<>
215inline void Container<Vector>::Archive(Archiver& arc)
216{
217 int i, num;
218
219 if (arc.Loading()) {
220 ClearObjectList();
221 arc.ArchiveInteger(&num);
222 Resize(num);
223 } else {
224 num = numobjects;
225 arc.ArchiveInteger(&num);
226 }
227 for (i = 1; i <= num; i++) {
228 arc.ArchiveVector(AddressOfObjectAt(i));
229 }
230}
231
232template<>
233inline void Container<int>::Archive(Archiver& arc)
234{
235 int i, num;
236
237 if (arc.Loading()) {
238 ClearObjectList();
239 arc.ArchiveInteger(&num);
240 Resize(num);
241 } else {
242 num = numobjects;
243 arc.ArchiveInteger(&num);
244 }
245 for (i = 1; i <= num; i++) {
246 arc.ArchiveInteger(AddressOfObjectAt(i));
247 }
248}
249
250template<>
251inline void Container<float>::Archive(Archiver& arc)
252{
253 int i, num;
254
255 if (arc.Loading()) {
256 ClearObjectList();
257 arc.ArchiveInteger(&num);
258 Resize(num);
259 } else {
260 num = numobjects;
261 arc.ArchiveInteger(&num);
262 }
263 for (i = 1; i <= num; i++) {
264 arc.ArchiveFloat(AddressOfObjectAt(i));
265 }
266}
267
268template<typename c>
269inline void ArchiveClass(Archiver& arc, c *obj)
270{
271 arc.ArchiveObject(obj);
272}
273
274template<typename Type>
275void Container<Type>::Archive(Archiver& arc)
276{
277 Archive(arc, ArchiveClass<Type>);
278}
279
280#ifndef NO_ARCHIVE
281
282template<typename key, typename value>
284{
285 Entry *e = NULL;
286 int hash;
287 int i;
288
289 arc.ArchiveUnsigned(&tableLength);
290 arc.ArchiveUnsigned(&threshold);
291 arc.ArchiveUnsigned(&count);
292 arc.ArchiveUnsignedShort(&tableLengthIndex);
293
294 if (arc.Loading()) {
295 if (tableLength != 1) {
296 table = new (NewTable(tableLength)) Entry *[tableLength]();
297 memset(table, 0, tableLength * sizeof(Entry *));
298 }
299
300 for (i = 0; i < count; i++) {
301 e = new Entry;
302 e->Archive(arc);
303
304 hash = HashCode<key>(e->GetKey()) % tableLength;
305
306 e->next = table[hash];
307 table[hash] = e;
308 }
309
310 defaultEntry = e;
311 } else {
312# ifndef NDEBUG
313 int total;
314
315 total = 0;
316
317 for (i = 0; i < tableLength; i++) {
318 for (e = table[i]; e != NULL; e = e->next) {
319 e->Archive(arc);
320 total++;
321 }
322 }
323 // it must match the number of elements
324 assert(total == count);
325# else
326 for (i = 0; i < tableLength; i++) {
327 for (e = table[i]; e != NULL; e = e->next) {
328 e->Archive(arc);
329 }
330 }
331# endif
332 }
333}
334
335template<typename key, typename value>
337{
338 m_con_set.Archive(arc);
339}
340
341#endif
342
343#define ArchiveEnum(thing, type) \
344 { \
345 int tempInt; \
346 \
347 tempInt = (int)(thing); \
348 arc.ArchiveInteger(&tempInt); \
349 (thing) = (type)tempInt; \
350 }
Definition archive.h:59
Definition archive.h:86
Definition class.h:276
Definition container.h:85
Definition listener.h:246
Definition lightclass.h:30
Definition vector.h:1020
Definition safeptr.h:32
Definition vector.h:61
Definition con_set.h:547
Definition con_set.h:119
Definition str.h:77
Definition archive.h:50