Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
saveload.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWSaveLoad *
24 * *
25 * $Archive:: /Commando/Code/wwsaveload/saveload.h $*
26 * *
27 * Author:: Greg Hjelstrom *
28 * *
29 * $Modtime:: 9/19/01 4:13p $*
30 * *
31 * $Revision:: 12 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#if defined(_MSC_VER)
39#pragma once
40#endif
41
42
43#ifndef SAVELOAD_H
44#define SAVELOAD_H
45
46#include "always.h"
47#include "pointerremap.h"
48#include "bittype.h"
49#include "slist.h"
50
51class RefCountClass;
54class PersistClass;
56class ChunkSaveClass;
57class ChunkLoadClass;
58
59
61//
62// WWSaveLoad
63//
64// The WWSaveLoad library is a framework for saving and loading. The main
65// goals that we attempted to achieve in designing this system are:
66//
67// - Save things in a form that could adapt as our code evolves. We want
68// to be able to load files which were created with a previous version of the
69// application into the current version.
70// - Use the same framework throughout all of our libraries with as small an
71// impact on them as possible
72// - Automate as much of the implementation of save-load as possible.
73// - Make this a generic hunk of code which contains no commando-specific parts
74// so that it can be used by other applications.
75// - Make this system capable of generating the file formats for our level editor,
76// the game's level definition file, and player save files.
77//
78// To achive this, we developed several core concepts:
79//
80// - Persistant Objects: Most of the state of the game is contained in the objects
81// active at any given time. PersistClass is an abstract interface which will allow
82// objects to be used with the save-load system. It was also important to keep the
83// overhead caused by inheriting this class to an absolute minimum.
84//
85// - PersistFactories: We need an automatic "virtual-constructor" or "abstract-factory"
86// system for all objects that get saved. This is the PersistFactoryClass and the
87// automated SimplePersistFactory template. All objects that "persist" are derived from
88// PersistClass and all concrete derived PersistClasses have an associated static
89// instance of a PersistFactory which handles their saving and construction upon encountering
90// them while loading. In certain cases these PersistFactories can also serve as
91// a "shortcut" where we cheat by not actually telling the object to save but simply save
92// a small piece of data that allows us to recreate an identical object when we load. This
93// is used in WW3D sometimes; we just save a render object name and then ask the WW3D asset
94// manager to recreate that model for us.
95//
96// - SaveLoadSubSystems: The overall file structure will be governed by many sub-systems
97// (derived from SaveLoadSubSystemClass). The application in-effect creates file formats
98// by simply having the sub-systems that it wants write into a file. In this way you can
99// achieve things like saving only static data into one file and dynamic into another, etc.
100// All persistant objects that get saved will be told to save by some sub-system. For
101// example: in Commando, I have a PhysicsDynamicDataSubSystem which saves all of the
102// dynamic physics objects. In saving those objects I use the built-in PersistFactories
103// and am therefore completely safe from new object types being added to the system, it will just
104// automatically work
105//
106// - Pointer re-mapping: A pointer remaping system is built into the save-load system. There
107// are several things that happen in this system. Each object, as it is saved and loaded,
108// registers with the system its old address and its new address. (the old address is saved
109// and the new address is available once the object is created). This is automated by the
110// SimplePersistFactory for all but classes that use multiple inheritance. During the load
111// process a table is built which contains all of these pointer pairs (old address, new address).
112// Whenever an object loads a pointer, it gives a "pointer to that pointer" to the save load system.
113// Then, after all of the objects have been loaded, the system goes through that list of pointers
114// and finds them in the pointer pair table. NOTE: use the macros for re-mapping your
115// pointers to enable automatic debugging information when you build with WWDEBUG defined.
116//
117// - Chunks: The file format will be chunk based since that gives us the flexibility to
118// add new data and remove obsolete data without necessarily losing the ability
119// to read old files. We will use a "high-granularity" of chunks. In many cases, each
120// member variable will be in its own chunk for maximum flexibility. To help soften
121// the memory usage for this approach, we developed the concept of "micro-chunks".
122// Micro-chunks are just like chunks in that they have an id and a size but
123// each of these fields are only a single byte and they are never hierarchical.
124//
125// - ChunkID's: The chunk ID's used by Subsystems and PersistFactories must be unique
126// but all others can be considered local to the object that is saving itself. Unique
127// ids for the subsystems and factories are achieved by saveloadids.h defining ranges
128// of ids for various libraries and then those libraries maintaining a single header
129// file internally which gives unique id's within that range to all of their sub-systems
130// and persist factories. Never re-use an id or you will break compatibility with older
131// versions of your files...
132//
134
135
137//
138// SaveLoadSystemClass
139//
142{
143public:
144
145 /*
146 ** Save-Load interface. To create a file, ask each sub-system to save itself.
147 ** To load a file just open it and pass it to the load method.
148 */
149 static bool Save (ChunkSaveClass &csave, SaveLoadSubSystemClass & subsystem);
150 static bool Load (ChunkLoadClass &cload,bool auto_post_load = true);
151 static bool Post_Load_Processing (void(*network_callback)(void));
152 /*
153 ** Look up the persist factory for a given chunk id
154 */
156
157 /*
158 ** Post-Load interface. An object being loaded can ask for a callback after
159 ** all objects have been loaded and pointers re-mapped.
160 */
162
163 /*
164 ** Pointer Remapping interface. NOTE: use the macros defined below to
165 ** get debug info with your pointers when doing a debug build.
166 */
167 static void Register_Pointer (void *old_pointer, void *new_pointer);
168
169#ifdef WWDEBUG
170 static void Request_Pointer_Remap (void **pointer_to_convert,const char * file = NULL,int line = 0);
171 static void Request_Ref_Counted_Pointer_Remap (RefCountClass **pointer_to_convert,const char * file = NULL,int line = 0);
172#else
173 static void Request_Pointer_Remap (void **pointer_to_convert);
174 static void Request_Ref_Counted_Pointer_Remap (RefCountClass **pointer_to_convert);
175#endif
176
177protected:
178
179 /*
180 ** Internal SaveLoadSystem functions
181 */
182 static void Register_Sub_System (SaveLoadSubSystemClass * subsys);
183 static void Unregister_Sub_System (SaveLoadSubSystemClass * subsys);
185
186 static void Register_Persist_Factory(PersistFactoryClass * factory);
187 static void Unregister_Persist_Factory(PersistFactoryClass * factory);
188
189 static void Link_Sub_System(SaveLoadSubSystemClass * subsys);
190 static void Unlink_Sub_System(SaveLoadSubSystemClass * subsys);
191 static void Link_Factory(PersistFactoryClass * factory);
192 static void Unlink_Factory(PersistFactoryClass * factory);
193
195
200
201 /*
202 ** these are friends so that they can register themselves at construction time.
203 */
206};
207
208
209/*
210** Use the following macros to automatically enable pointer-remap DEBUG code. Remember that
211** in all cases you submit a pointer to the pointer you want re-mapped.
212*/
213#ifdef WWDEBUG
214#define REQUEST_POINTER_REMAP(pp) SaveLoadSystemClass::Request_Pointer_Remap(pp,__FILE__,__LINE__)
215#define REQUEST_REF_COUNTED_POINTER_REMAP(pp) SaveLoadSystemClass::Request_Ref_Counted_Pointer_Remap(pp,__FILE__,__LINE__)
216#else
217#define REQUEST_POINTER_REMAP(pp) SaveLoadSystemClass::Request_Pointer_Remap(pp)
218#define REQUEST_REF_COUNTED_POINTER_REMAP(pp) SaveLoadSystemClass::Request_Ref_Counted_Pointer_Remap(pp)
219#endif
220
221
222#endif //SAVELOAD_H
223
#define NULL
Definition BaseType.h:92
unsigned long uint32
Definition bittype.h:46
Definition SLIST.H:67
static void Link_Sub_System(SaveLoadSubSystemClass *subsys)
Definition saveload.cpp:241
static PointerRemapClass PointerRemapper
Definition saveload.h:198
static void Unlink_Factory(PersistFactoryClass *factory)
Definition saveload.cpp:282
friend class SaveLoadSubSystemClass
Definition saveload.h:204
static void Unlink_Sub_System(SaveLoadSubSystemClass *subsys)
Definition saveload.cpp:251
static void Register_Pointer(void *old_pointer, void *new_pointer)
Definition saveload.cpp:210
static void Register_Persist_Factory(PersistFactoryClass *factory)
Definition saveload.cpp:161
static SaveLoadSubSystemClass * Find_Sub_System(uint32 chunk_id)
Definition saveload.cpp:149
friend class PersistFactoryClass
Definition saveload.h:205
static PersistFactoryClass * FactoryListHead
Definition saveload.h:197
static bool Save(ChunkSaveClass &csave, SaveLoadSubSystemClass &subsystem)
Definition saveload.cpp:60
static bool Is_Post_Load_Callback_Registered(PostLoadableClass *obj)
Definition saveload.cpp:185
static void Unregister_Sub_System(SaveLoadSubSystemClass *subsys)
Definition saveload.cpp:142
static void Link_Factory(PersistFactoryClass *factory)
Definition saveload.cpp:272
static void Register_Sub_System(SaveLoadSubSystemClass *subsys)
Definition saveload.cpp:135
static PersistFactoryClass * Find_Persist_Factory(uint32 chunk_id)
Definition saveload.cpp:173
static SaveLoadSubSystemClass * SubSystemListHead
Definition saveload.h:196
static bool Load(ChunkLoadClass &cload, bool auto_post_load=true)
Definition saveload.cpp:73
static bool Post_Load_Processing(void(*network_callback)(void))
Definition saveload.cpp:119
static void Register_Post_Load_Callback(PostLoadableClass *obj)
Definition saveload.cpp:201
static SList< PostLoadableClass > PostLoadList
Definition saveload.h:199
static void Request_Pointer_Remap(void **pointer_to_convert)
Definition saveload.cpp:229
static void Unregister_Persist_Factory(PersistFactoryClass *factory)
Definition saveload.cpp:167
static void Request_Ref_Counted_Pointer_Remap(RefCountClass **pointer_to_convert)
Definition saveload.cpp:234