persistentgameobjobserver.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. ** Command & Conquer Renegade(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/persistentgameobjobserver.cpp $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 11/29/01 3:32p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "persistentgameobjobserver.h"
  36. #include "chunkio.h"
  37. #include "persistfactory.h"
  38. #include "debug.h"
  39. /*
  40. **
  41. */
  42. PersistentGameObjObserverClass::PersistentGameObjObserverClass( void )
  43. {
  44. Set_ID( GameObjObserverManager::Get_Next_Observer_ID() );
  45. PersistentGameObjObserverManager::Add( this );
  46. }
  47. PersistentGameObjObserverClass::~PersistentGameObjObserverClass( void )
  48. {
  49. PersistentGameObjObserverManager::Remove( this );
  50. }
  51. enum {
  52. CHUNKID_PARENT = 411001149,
  53. CHUNKID_VARIABLES,
  54. MICROCHUNKID_OBSERVER_PTR = 1,
  55. MICROCHUNKID_OBSERVER_ID,
  56. };
  57. bool PersistentGameObjObserverClass::Save( ChunkSaveClass & csave )
  58. {
  59. csave.Begin_Chunk( CHUNKID_PARENT );
  60. PersistClass::Save( csave );
  61. csave.End_Chunk();
  62. csave.Begin_Chunk( CHUNKID_VARIABLES );
  63. void * observer_ptr = (GameObjObserverClass*)this;
  64. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_OBSERVER_PTR, observer_ptr );
  65. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_OBSERVER_ID, ID );
  66. csave.End_Chunk();
  67. return true;
  68. }
  69. bool PersistentGameObjObserverClass::Load( ChunkLoadClass &cload )
  70. {
  71. void * old_observer_ptr = NULL;
  72. while (cload.Open_Chunk()) {
  73. switch(cload.Cur_Chunk_ID()) {
  74. case CHUNKID_PARENT:
  75. PersistClass::Load( cload );
  76. break;
  77. case CHUNKID_VARIABLES:
  78. while (cload.Open_Micro_Chunk()) {
  79. switch(cload.Cur_Micro_Chunk_ID()) {
  80. READ_MICRO_CHUNK( cload, MICROCHUNKID_OBSERVER_PTR, old_observer_ptr );
  81. READ_MICRO_CHUNK( cload, MICROCHUNKID_OBSERVER_ID, ID );
  82. default:
  83. Debug_Say(( "Unrecognized PersistentGameObjObserverClass Variable chunkID\n" ));
  84. break;
  85. }
  86. cload.Close_Micro_Chunk();
  87. }
  88. break;
  89. default:
  90. Debug_Say(( "Unrecognized PersistentGameObjObserverClass chunkID\n" ));
  91. break;
  92. }
  93. cload.Close_Chunk();
  94. }
  95. WWASSERT( old_observer_ptr != NULL );
  96. if ( old_observer_ptr != NULL ) {
  97. SaveLoadSystemClass::Register_Pointer( old_observer_ptr, (GameObjObserverClass*)this );
  98. }
  99. return true;
  100. }
  101. /*
  102. **
  103. */
  104. DynamicVectorClass<PersistentGameObjObserverClass *> PersistentGameObjObserverManager::ObserverList;
  105. void PersistentGameObjObserverManager::Add( PersistentGameObjObserverClass * observer )
  106. {
  107. ObserverList.Add( observer );
  108. }
  109. void PersistentGameObjObserverManager::Remove( PersistentGameObjObserverClass * observer )
  110. {
  111. ObserverList.Delete( observer );
  112. }
  113. /*
  114. **
  115. */
  116. typedef enum {
  117. CHUNKID_OBSERVERS = 1,
  118. };
  119. bool PersistentGameObjObserverManager::Save( ChunkSaveClass & csave )
  120. {
  121. // Make sure we flush out all the old observers
  122. GameObjObserverManager::Delete_Pending();
  123. csave.Begin_Chunk( CHUNKID_OBSERVERS );
  124. // Allow each object in the master list to save
  125. for ( int i = 0; i < ObserverList.Count(); i++ ) {
  126. csave.Begin_Chunk( ObserverList[i]->Get_Factory().Chunk_ID() );
  127. ObserverList[i]->Get_Factory().Save( csave, ObserverList[i] );
  128. csave.End_Chunk();
  129. }
  130. csave.End_Chunk();
  131. return true;
  132. }
  133. bool PersistentGameObjObserverManager::Load( ChunkLoadClass & cload )
  134. {
  135. cload.Open_Chunk();
  136. WWASSERT( cload.Cur_Chunk_ID() == CHUNKID_OBSERVERS );
  137. while (cload.Open_Chunk()) {
  138. PersistFactoryClass * factory = SaveLoadSystemClass::Find_Persist_Factory( cload.Cur_Chunk_ID() );
  139. if ( factory ) {
  140. factory->Load( cload );
  141. }
  142. cload.Close_Chunk();
  143. }
  144. cload.Close_Chunk();
  145. return true;
  146. }
  147. void PersistentGameObjObserverManager::Reset( void )
  148. {
  149. GameObjObserverManager::Delete_Pending();
  150. // Delete each in the list
  151. while ( ObserverList.Count() ) {
  152. delete ObserverList[0];
  153. }
  154. GameObjObserverManager::Delete_Pending();
  155. }