/ghost-legacy/savegame.cpp

http://ghostcb.googlecode.com/ · C++ · 118 lines · 68 code · 17 blank · 33 comment · 6 complexity · 2ab5ef931b18283263529fcb65e1eee5 MD5 · raw file

  1. /*
  2. Copyright [2008] [Trevor Hogan]
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
  13. */
  14. #include "ghost.h"
  15. #include "util.h"
  16. #include "packed.h"
  17. #include "savegame.h"
  18. //
  19. // CSaveGame
  20. //
  21. CSaveGame :: CSaveGame( ) : CPacked( )
  22. {
  23. m_NumSlots = 0;
  24. m_RandomSeed = 0;
  25. }
  26. CSaveGame :: ~CSaveGame( )
  27. {
  28. }
  29. #define READB( x, y, z ) (x).read( (char *)(y), (z) )
  30. #define READSTR( x, y ) getline( (x), (y), '\0' )
  31. void CSaveGame :: ParseSaveGame( )
  32. {
  33. m_MapPath.clear( );
  34. m_GameName.clear( );
  35. m_NumSlots = 0;
  36. m_Slots.clear( );
  37. m_RandomSeed = 0;
  38. m_MagicNumber.clear( );
  39. if( m_Flags != 0 )
  40. {
  41. CONSOLE_Print( "[SAVEGAME] invalid replay (flags mismatch)" );
  42. m_Valid = false;
  43. return;
  44. }
  45. istringstream ISS( m_Decompressed );
  46. // savegame format figured out by Varlock:
  47. // string -> map path
  48. // 0 (string?) -> ??? (no idea what this is)
  49. // string -> game name
  50. // 0 (string?) -> ??? (maybe original game password)
  51. // string -> stat string
  52. // 4 bytes -> ??? (seems to be # of slots)
  53. // 4 bytes -> ??? (seems to be 0x01 0x28 0x49 0x00 on both of the savegames examined)
  54. // 2 bytes -> ??? (no idea what this is)
  55. // slot structure
  56. // 4 bytes -> magic number
  57. unsigned char Garbage1;
  58. uint16_t Garbage2;
  59. uint32_t Garbage4;
  60. string GarbageString;
  61. uint32_t MagicNumber;
  62. READSTR( ISS, m_MapPath ); // map path
  63. READSTR( ISS, GarbageString ); // ???
  64. READSTR( ISS, m_GameName ); // game name
  65. READSTR( ISS, GarbageString ); // ???
  66. READSTR( ISS, GarbageString ); // stat string
  67. READB( ISS, &Garbage4, 4 ); // ???
  68. READB( ISS, &Garbage4, 4 ); // ???
  69. READB( ISS, &Garbage2, 2 ); // ???
  70. READB( ISS, &m_NumSlots, 1 ); // number of slots
  71. if( m_NumSlots > 12 )
  72. {
  73. CONSOLE_Print( "[SAVEGAME] invalid savegame (too many slots)" );
  74. m_Valid = false;
  75. return;
  76. }
  77. for( unsigned char i = 0; i < m_NumSlots; i++ )
  78. {
  79. unsigned char SlotData[9];
  80. READB( ISS, SlotData, 9 ); // slot data
  81. m_Slots.push_back( CGameSlot( SlotData[0], SlotData[1], SlotData[2], SlotData[3], SlotData[4], SlotData[5], SlotData[6], SlotData[7], SlotData[8] ) );
  82. }
  83. READB( ISS, &m_RandomSeed, 4 ); // random seed
  84. READB( ISS, &Garbage1, 1 ); // GameType
  85. READB( ISS, &Garbage1, 1 ); // number of player slots (non observer)
  86. READB( ISS, &MagicNumber, 4 ); // magic number
  87. if( ISS.eof( ) || ISS.fail( ) )
  88. {
  89. CONSOLE_Print( "[SAVEGAME] failed to parse savegame header" );
  90. m_Valid = false;
  91. return;
  92. }
  93. m_MagicNumber = UTIL_CreateByteArray( MagicNumber, false );
  94. m_Valid = true;
  95. }