diff --git a/src/base/savegame.cc b/src/base/savegame.cc index dc5fdb9..1ad1496 100644 --- a/src/base/savegame.cc +++ b/src/base/savegame.cc @@ -38,7 +38,7 @@ using base::savegame; using base::savegame_data; /// list of saved games -std::vector savegame::Games; +std::vector savegame::Games; // ctor savegame_data::savegame_data (const std::string & dir, const std::string & desc, const u_int32 & time) @@ -150,8 +150,8 @@ bool savegame::save (const s_int32 & slot, const std::string & desc, const u_int } // we'll need a new gamedata record - Games.push_back (savegame_data (filepath, desc, gametime)); - data = &Games.back(); + data = new savegame_data (filepath, desc, gametime); + Games.push_back (data); } else { @@ -163,19 +163,19 @@ bool savegame::save (const s_int32 & slot, const std::string & desc, const u_int // save game data u_int32 current = 1; - u_int32 count = Serializer().size(); + u_int32 size = Serializer().size(); std::list::iterator i; for (i = Serializer().begin(); i != Serializer().end(); i++) { - if (!(*i)->save (data->directory())) - { + // if (!(*i)->save (data->directory())) + // { // TODO: cleanup - return false; - } + // return false; + // } if (ProgressCallback != NULL) { - (*ProgressCallback)((s_int32)((current * 100.0) / count)); + (*ProgressCallback)((s_int32)((current * 100.0) / size)); } current++; @@ -200,19 +200,19 @@ void savegame::init (const std::string & name) DIR *dir; // create initial saved game - Games.push_back (savegame_data ("", "Start New Game", 0)); + Games.push_back (new savegame_data ("", "Start New Game", 0)); // create auto save slot std::string save_dir = base::Paths.cfg_data_dir() + "/" + name; if (!load_meta_data (save_dir + "-auto-save")) { - Games.push_back (savegame_data (save_dir + "-auto-save", "Autosave", 0)); + Games.push_back (new savegame_data (save_dir + "-auto-save", "Autosave", 0)); } // create quick save slot if (!load_meta_data (save_dir + "-quick-save")) { - Games.push_back (savegame_data (save_dir + "-quick-save", "Quicksave", 0)); + Games.push_back (new savegame_data (save_dir + "-quick-save", "Quicksave", 0)); } // Read the user's saved games (if any) - they'll be located in @@ -262,6 +262,9 @@ void savegame::cleanup (const std::string & name) // get game at given slot savegame_data *savegame::get (const s_int32 & slot) { + // avoid error message + if (slot == NEW_SAVE) return NULL; + s_int32 real_slot = slot + SPECIAL_SLOT_COUNT; if (slot < 0 || slot >= count()) { @@ -270,7 +273,7 @@ savegame_data *savegame::get (const s_int32 & slot) return NULL; } - return &Games[real_slot]; + return Games[real_slot]; } // save meta data @@ -300,11 +303,11 @@ bool savegame::load_meta_data (const std::string & filepath) std::string desc = file.get_string("desc"); u_int32 time = file.get_uint32("time"); - savegame_data data (filepath, desc, time); + savegame_data *data = new savegame_data (filepath, desc, time); // get modification time of this game time_t mod_time = statbuf.st_mtimespec.tv_sec; - data.set_last_modified(mod_time); + data->set_last_modified(mod_time); Games.push_back (data); return true; @@ -314,6 +317,18 @@ bool savegame::load_meta_data (const std::string & filepath) return false; } +// register serializer +void savegame::add (base::serializer_base* serializer) +{ + Serializer ().push_back (serializer); +} + +// remove serializer +void savegame::remove (base::serializer_base* serializer) +{ + Serializer ().remove (serializer); +} + // factories for loading/saving game data std::list& savegame::Serializer () { diff --git a/src/base/savegame.h b/src/base/savegame.h index 5057681..80b4d0e 100644 --- a/src/base/savegame.h +++ b/src/base/savegame.h @@ -221,6 +221,18 @@ namespace base */ static void init (const std::string & name); + /** + * Register a serializer for saving/loading game data. + * @param serializer the serializer to add. + */ + static void add (base::serializer_base* serializer); + + /** + * Remove an existing serializer. + * @param serializer the serializer to remove. + */ + static void remove (base::serializer_base* serializer); + protected: /** * Delete all regular files in the given directory. @@ -247,7 +259,7 @@ namespace base private: /// list of available saved games - static std::vector Games; + static std::vector Games; /// classes that read write game data static std::list& Serializer(); /// notify about load/save progress diff --git a/src/py-wrappers/adonthell/py_base.i b/src/py-wrappers/adonthell/py_base.i index a8600ba..be6ff9b 100644 --- a/src/py-wrappers/adonthell/py_base.i +++ b/src/py-wrappers/adonthell/py_base.i @@ -156,6 +156,7 @@ namespace base { %include "base/diskio.h" %include "base/configuration.h" %include "base/paths.h" +typedef long time_t; %include "base/savegame.h" %include "base/serializer.h" diff --git a/test/serializertest.py b/test/serializertest.py index b6148da..5cd51dd 100644 --- a/test/serializertest.py +++ b/test/serializertest.py @@ -4,12 +4,27 @@ class testserializer(main.AdonthellApp): def save (self, name): print "saving state to", name, "..." - return 1 + return 1 + def progress (self, current): + print ".", + def main (self): - s = base.py_serializer(self) - s.save ("test") - s.load () + # -- register serializer + base.savegame.add (base.py_serializer(self)) + + # -- create savegame instance + sg = base.savegame (self.progress) + print sg + + # -- save + sg.load (-3) + sg.save (-2, "Test", 0) + + # -- list existing save games + for index in range (0, sg.count()): + data = sg.get (index) + print "*", data.directory (), data.last_modified() return 0