gcsx_save.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* GCSx
  2. ** SAVE.H
  3. **
  4. ** File saving and loading
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_SAVE_H_
  24. #define __GCSx_SAVE_H_
  25. // Interface for our files that we pass to objects- no seeking!
  26. class FileWrite {
  27. private:
  28. File* filePtr;
  29. Uint32 compression;
  30. int bytesWritten;
  31. // Used for compression to go back and store original size
  32. int seekBegin;
  33. // ZLIB compression method
  34. z_stream* zlibStream;
  35. int zlibStreamInit;
  36. Byte* zlibBufferOut;
  37. enum { ZLIB_BUFFER_SIZE = 8192 };
  38. public:
  39. // Assumes filePtr is seeked properly, all writes must be sequential
  40. // Allowed to choose compression method
  41. FileWrite(File* myFilePtr, int noCompression = 0) throw_File;
  42. ~FileWrite();
  43. void write(const void* ptr, int size) throw_File;
  44. void writeInt(Uint32 value) throw_File;
  45. void writeIntBulk(const Uint32* ptr, int count) throw_File;
  46. void writeInt16(Uint16 value) throw_File;
  47. void writeInt8(Uint8 value) throw_File;
  48. void writeStr(const std::string& str) throw_File;
  49. int getSize() const { return bytesWritten; }
  50. Uint32 getCompression() const { return compression; }
  51. // Called when all writing is complete
  52. void flush() throw_File;
  53. };
  54. // Interface for objects that can save/load
  55. class SaveLoad : public virtual LoadOnly {
  56. public:
  57. // All objects are split into Header and Content
  58. // which are loaded and saved separately; If content
  59. // is modified, it should NOT be cached! New objects
  60. // MUST be marked as modified on both even if empty!
  61. virtual int isHeaderModified() const = 0;
  62. virtual int isContentModified() const = 0;
  63. // Save header, return version of data being saved
  64. // Can be called if header isn't modified, but may not
  65. // Discard FileWrite object when done
  66. virtual Uint32 saveHeader(FileWrite* file) = 0;
  67. // Must save; won't be called if content is cached
  68. // Can be called if content isn't modified, but may not
  69. // Discard FileWrite object when done
  70. virtual void saveContent(FileWrite* file) = 0;
  71. // Tells object save was successful, so modified flags
  72. // may be cleared
  73. virtual void saveSuccess() = 0;
  74. // After a successful save, all objects are called with
  75. // cachedContent() so it knows it can revert to being
  76. // cached if it desires; if oldData is true, the pointer
  77. // only points to the OLD cached content (a moved file, etc)
  78. // and the object is just being told to replace it's pointer
  79. // if it was already cached. If oldData is false,
  80. // object can choose to deallocate any data and go
  81. // back to being cached with this pointer
  82. // Object MUST DEALLOCATE this pointer when done or if not used
  83. // (as well as any previous cached content pointer it had)
  84. virtual void cachedContent(FileRead* file, int oldData) = 0;
  85. };
  86. class WorldFile : public WorldFileLoad {
  87. private:
  88. std::vector<SaveLoad*> blockSaves; // If block is saveable also, here's the ptr
  89. int inSaving; // If we're saving right now; so we don't try to resave on a crash
  90. int isModifiedAddDel; // If we're modified due to a new or deleted block
  91. File* emergencySave; // Currently unused
  92. enum {
  93. // Number of extra, empty blocks we add as padding to allow for easier quicksaves
  94. NUM_EMPTY_BLOCK = 10,
  95. };
  96. public:
  97. WorldFile() throw_File; // Create new
  98. // Pointer must remain valid
  99. WorldFile(const std::string* openFile) throw_File; // Open existing
  100. ~WorldFile(); // Closes the file without saving remaining changes
  101. // Is file new? (no filename, never saved)
  102. int isNew();
  103. // A saveable object uses this to "claim" a block by it's ID; it will
  104. // then have both load functions called
  105. void claimBlock(Uint32 id, SaveLoad* claimingObject);
  106. // An object uses this to create a new block; nothing will
  107. // be immediately called
  108. void newBlock(Uint32 blockType, SaveLoad* claimingObject);
  109. // An object can tell us to discard their block entirely
  110. void discardBlock(SaveLoad* claimingObject);
  111. // Saves all objects; this is a "quick save" that, although it
  112. // produces a usable file, doesn't optimize the file size
  113. // Can't call if isNew()
  114. void save() throw_File;
  115. // Saves everything to a new file and that becomes our
  116. // filename now; overwrites if needed; produces a file
  117. // of fullSave() quality
  118. // Pointer must remain valid
  119. void saveAs(const std::string* newFilename) throw_File;
  120. // Does a full save that produces an "optimized" file
  121. // Can't call if isNew()
  122. void fullSave() throw_File;
  123. // Is any object modified?
  124. int isModified();
  125. };
  126. #endif