Skip to content

Commit

Permalink
FIXED savegame handling and its Python wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Sterker authored and Kai Sterker committed Sep 25, 2009
1 parent e211b99 commit 8e52521
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/base/savegame.cc
Expand Up @@ -139,16 +139,6 @@ bool savegame::save (const s_int32 & slot, const std::string & desc, const u_int
return false;
}

#ifdef WIN32
if (mkdir (filepath.c_str()))
#else
if (mkdir (filepath.c_str(), 0700))
#endif
{
fprintf (stderr, "*** savegame::save: failed to create directory\n %s\n", filepath.c_str ());
return false;
}

// we'll need a new gamedata record
data = new savegame_data (filepath, desc, gametime);
Games.push_back (data);
Expand All @@ -157,21 +147,22 @@ bool savegame::save (const s_int32 & slot, const std::string & desc, const u_int
{
// we have to update the gamedata record
data->update (desc, gametime);
// and clean the directory before saving
cleanup (data->directory());
}

// prepare the directory before saving
cleanup (data->directory());

// save game data
u_int32 current = 1;
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)
{
Expand Down Expand Up @@ -257,6 +248,17 @@ void savegame::cleanup (const std::string & name)

closedir (dir);
}
else
{
#ifdef WIN32
if (mkdir (name.c_str()))
#else
if (mkdir (name.c_str(), 0700))
#endif
{
fprintf (stderr, "*** savegame::save: failed to create directory\n %s\n", name.c_str ());
}
}
}

// get game at given slot
Expand All @@ -266,10 +268,10 @@ savegame_data *savegame::get (const s_int32 & slot)
if (slot == NEW_SAVE) return NULL;

s_int32 real_slot = slot + SPECIAL_SLOT_COUNT;
if (slot < 0 || slot >= count())
if (real_slot < 0 || real_slot >= Games.size())
{
fprintf (stderr, "*** savegame::get: slot %i out of range [%i, %i[\n",
slot, -SPECIAL_SLOT_COUNT, count()-SPECIAL_SLOT_COUNT);
slot, -SPECIAL_SLOT_COUNT, count());
return NULL;
}

Expand Down Expand Up @@ -335,3 +337,4 @@ std::list<base::serializer_base*>& savegame::Serializer ()
static std::list<base::serializer_base*> *Serializer = new std::list<base::serializer_base*>();
return *Serializer;
}

3 changes: 3 additions & 0 deletions src/py-wrappers/adonthell/py_base.i
Expand Up @@ -42,6 +42,7 @@ namespace base
}
else
{
Load = NULL;
fprintf (stderr, "*** serializer: '%s' has no method 'load'!\n", instance->ob_type->tp_name);
}
Py_XDECREF (load);
Expand Down Expand Up @@ -128,6 +129,8 @@ namespace base {

// typemap to let C++ have ownership of cfg_option* given to configuration::add_option ()
%typemap(in) cfg_option *value "if ((SWIG_ConvertPtr ($input, (void **) &$1, $1_descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) SWIG_fail;"

%typemap(in) serializer_base *serializer "if ((SWIG_ConvertPtr ($input, (void **) &$1, $1_descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) SWIG_fail;"
}

// typemap for passing a FILE* from Python
Expand Down

0 comments on commit 8e52521

Please sign in to comment.