Skip to content

Commit

Permalink
ADDED testcase for loading/saving (code still buggy)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksterker committed Sep 25, 2009
1 parent 0141a39 commit 0c56257
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
45 changes: 30 additions & 15 deletions src/base/savegame.cc
Expand Up @@ -38,7 +38,7 @@ using base::savegame;
using base::savegame_data;

/// list of saved games
std::vector<savegame_data> savegame::Games;
std::vector<savegame_data*> savegame::Games;

// ctor
savegame_data::savegame_data (const std::string & dir, const std::string & desc, const u_int32 & time)
Expand Down Expand Up @@ -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
{
Expand All @@ -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<base::serializer_base*>::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++;
Expand All @@ -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
Expand Down Expand Up @@ -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())
{
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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<base::serializer_base*>& savegame::Serializer ()
{
Expand Down
14 changes: 13 additions & 1 deletion src/base/savegame.h
Expand Up @@ -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.
Expand All @@ -247,7 +259,7 @@ namespace base

private:
/// list of available saved games
static std::vector<savegame_data> Games;
static std::vector<savegame_data*> Games;
/// classes that read write game data
static std::list<base::serializer_base*>& Serializer();
/// notify about load/save progress
Expand Down
1 change: 1 addition & 0 deletions src/py-wrappers/adonthell/py_base.i
Expand Up @@ -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"

Expand Down
23 changes: 19 additions & 4 deletions test/serializertest.py
Expand Up @@ -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

Expand Down

0 comments on commit 0c56257

Please sign in to comment.