saveload.h 11 KB

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