Skip to content

Commit

Permalink
ADDED serializer that can be used to register objects with the save g…
Browse files Browse the repository at this point in the history
…ame manager for loading and saving
  • Loading branch information
ksterker committed Sep 18, 2009
1 parent 7092f0c commit 79549d2
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/base/CMakeLists.txt
Expand Up @@ -32,6 +32,7 @@ set(adonthell_base_HEADERS
configuration.h
diskwriter_xml.h
gettext.h
serializer.h
timer.h
)

Expand Down
1 change: 1 addition & 0 deletions src/base/Makefile.am
Expand Up @@ -20,6 +20,7 @@ pkgincludebase_HEADERS = \
hash_map.h \
nls.h \
paths.h \
serializer.h \
timer.h \
types.h

Expand Down
140 changes: 140 additions & 0 deletions src/base/serializer.h
@@ -0,0 +1,140 @@
/*
Copyright (C) 2009 Kai Sterker <kaisterker@linuxgames.com>
Part of the Adonthell Project http://adonthell.linuxgames.com
Adonthell is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Adonthell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Adonthell; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/**
* @file base/serializer.h
* @author Kai Sterker <kaisterker@linuxgames.com>
*
* @brief Gamedata serialization suppert.
*
*
*/

#ifndef BASE_SAVEGAME_H
#define BASE_SAVEGAME_H

#include "python/callback_support.h"

namespace base
{
/**
* Wrapper for anything that can be serialized to a file on disk.
* It supports static classes, instanciated objects and Python
* classes, as long as those provide the two methods
*
* \li bool load()
* \li bool save(const std::string & path)
*
* Using this wrapper, objects can be registered at the gamedata
* class, which will load or save their contents as appropriate.
*/
class serializer_base
{
public:
/**
* Cleanup.
*/
~serializer_base ()
{
delete Save;
delete Load;
}

/**
* Serialize object into the given directory. Which filename
* to use is up to the object that is being serialized.
* @param path directory to write into.
* @return true on success, false otherwise.
*/
bool save (const std::string & path)
{
if (!Save) return false;
return (*Save)(path);
}

/**
* Load object from a file. Which file to load is up to the
* object itself. It must search for that file in Adonthell's
* search path.
* @return true on success, false otherwise.
*/
bool load ()
{
if (!Load) return false;
return (*Load)();
}

#ifndef SWIG
/// python support
GET_TYPE_NAME(base::serializer_base);
#endif

protected:
/**
* Prevent instantiation of this abstract base class through
* the user.
*/
serializer_base()
{
Save = NULL;
Load = NULL;
}

/// callback for saving the wrapped object
base::functor_1ret<const std::string &, bool> *Save;
/// callback for loading the wrapped object
base::functor_0ret<bool> *Load;
};

/**
* Wrapper around static or instanciated class. Called
* py_serializer on Python side (see base.i for specialization).
* This class can be instanciated to register at the savegame
* class.
*/
template <class T>
class serializer : public serializer_base
{
public:
/**
* Create a serializer for a static class.
*/
serializer()
{
Save = base::make_functor_ret(&T::save);
Load = base::make_functor_ret(&T::load);
}

/**
* Create a serializer for the given instance. Note
* that the serializer knows nothing about the instance's
* livetime. So make sure to deregister the serializer
* once the instance gets deleted.
* @param instance object to serialize.
*/
serializer(T *instance)
{
Save = base::make_functor_ret(*instance, &T::save);
Load = base::make_functor_ret(*instance, &T::load);
}
};

}

#endif
3 changes: 1 addition & 2 deletions src/main/adonthell.h
Expand Up @@ -44,8 +44,7 @@ using std::string;
namespace adonthell {
/**
* Subclass adonthell::app to create the entry point for any application
* using the Adonthell framework. The class you create must be named theApp
* in order to be found by the linker. Override the main() method. It will
* using the Adonthell framework. Override the main() method. It will
* be called after all operating system dependend intialization has been
* completed. You will find the command line args passed to your application
* in the member variables argc and argv.
Expand Down
41 changes: 41 additions & 0 deletions src/py-wrappers/adonthell/py_base.i
Expand Up @@ -9,6 +9,44 @@
#include "base/types.h"
#include "base/diskio.h"
#include "base/configuration.h"
#include "base/serializer.h"

#include "python/callback.h"

// partial specialization of base::serializer so it can be used from Python
namespace base
{
template<>
serializer<PyObject>::serializer() : serializer_base()
{
}

template<>
serializer<PyObject>::serializer(PyObject *instance) : serializer_base()
{
PyObject *save = PyObject_GetAttrString (instance, "save");
if (PyCallable_Check (save))
{
Save = new python::functor_1ret<const std::string &, bool>(save);
}
else
{
fprintf (stderr, "*** serializer: '%s' has no method 'save'!\n", instance->ob_type->tp_name);
}
Py_XDECREF (save);

PyObject *load = PyObject_GetAttrString (instance, "load");
if (PyCallable_Check (load))
{
Load = new python::functor_0ret<bool>(load);
}
else
{
fprintf (stderr, "*** serializer: '%s' has no method 'load'!\n", instance->ob_type->tp_name);
}
Py_XDECREF (load);
}
}
%}

%include "stdint.i"
Expand Down Expand Up @@ -118,6 +156,9 @@ namespace base {
%include "base/diskio.h"
%include "base/configuration.h"
%include "base/paths.h"
%include "base/serializer.h"

%template(py_serializer) base::serializer<PyObject>;

/* implement friend operators of igzstream */
%extend base::igzstream {
Expand Down
12 changes: 6 additions & 6 deletions src/python/callback.h
Expand Up @@ -158,18 +158,18 @@ namespace python
void run(P1 arg1, P2 arg2)
{
PyObject * pyarg1;
PyObject * pyarg2;
PyObject * pyarg2;
PyObject * pyres;

PyObject * pyargs = PyTuple_New(2);
pyarg1 = pass_instance(arg1);
pyarg2 = pass_instance(arg2);
pyarg1 = pass_instance<P1>(arg1);
pyarg2 = pass_instance<P2>(arg2);
if (!pyarg1) std::cerr << "Warning! Argument not valid!\n" << std::endl;
if (!pyarg2) std::cerr << "Warning! Argument not valid!\n" << std::endl;
if (!pyarg2) std::cerr << "Warning! Argument not valid!\n" << std::endl;

// The SetItem steals our reference to pyarg1
PyTuple_SetItem(pyargs, 0, pyarg1);
PyTuple_SetItem(pyargs, 1, pyarg2);
PyTuple_SetItem(pyargs, 1, pyarg2);

// We can finally call our function
pyres = PyObject_CallObject(callable, pyargs);
Expand Down Expand Up @@ -205,7 +205,7 @@ namespace python
PyObject * pyres;

PyObject * pyargs = PyTuple_New(1);
pyarg1 = pass_instance(arg1);
pyarg1 = pass_instance<P1>(arg1);
if (!pyarg1) std::cerr << "Warning! Argument not valid!\n" << std::endl;

// The SetItem steals our reference to pyarg1
Expand Down
2 changes: 1 addition & 1 deletion src/python/python.h
Expand Up @@ -195,7 +195,7 @@ namespace python
* @return a Python object representing \e arg.
*/
template <> inline
PyObject * pass_instance<std::string &>(std::string & arg, const ownership own)
PyObject * pass_instance<const std::string &>(const std::string & arg, const ownership own)
{
return PyString_FromString(arg.c_str());
show_traceback();
Expand Down
4 changes: 2 additions & 2 deletions test/Makefile.am
Expand Up @@ -4,8 +4,8 @@ SUBDIRS = data

EXTRA_DIST = README audiotest.py dialogtest.py gfxsave.py itemtest.py \
worldtest.py configtest.py equipmenttest.py gfxtest.py logtest.py \
convert_data.py eventtest.py guitest.py questtest.py \
convert_quests.py filereport.py inputtest.py searchtest.py \
convert_data.py eventtest.py guitest.py questtest.py filereport.py \
convert_quests.py inputtest.py searchtest.py serializertest.py \
convert_graphics.py CMakeLists.txt README.worldtest smallworld.cc

noinst_PROGRAMS = callbacktest diskiotest guitest inputtest gfxtest worldtest \
Expand Down
10 changes: 7 additions & 3 deletions test/README
Expand Up @@ -19,8 +19,8 @@ Utility Scripts:

* convert_graphics.py

Extracts images from v0.3 media files (.anim, .img, .mobj) and
converts them into PNG format.
Extracts images from v0.3 media files (.anim, .img, .mobj, .mchar)
and converts them into PNG format.
Run with 'python convert_graphics.py <filename>'

* filereport.py
Expand Down Expand Up @@ -101,8 +101,12 @@ Test Scripts:
enter a list of search terms and prints which texts in the library
contain these terms. Run with 'python -i searchtest.py'

* serializertest.py

Tests using the game data serialization for a Python class. Run
with 'python serializertest.py'.

* worldtest.py

Tests the world module. Requires the gfx module checked out from
CVS. Run with 'python gfxtest.py -g <path to gfx module>'

0 comments on commit 79549d2

Please sign in to comment.