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