2011-03-21 05:16:27 +08:00
|
|
|
|
|
|
|
#include "object.h"
|
|
|
|
#include "engines/grim/savegame.h"
|
|
|
|
|
|
|
|
#include "engines/grim/lua/lobject.h"
|
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
#include "engines/grim/font.h"
|
|
|
|
|
2011-04-12 20:41:22 +08:00
|
|
|
DECLARE_SINGLETON(Grim::ObjectManager)
|
2011-03-21 05:16:27 +08:00
|
|
|
|
2011-04-12 20:41:22 +08:00
|
|
|
namespace Grim {
|
2011-03-21 05:16:27 +08:00
|
|
|
|
|
|
|
Object::Object() : _refCount(0) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Object::~Object() {
|
2011-03-23 23:13:53 +01:00
|
|
|
if (lua_isopen()) {
|
|
|
|
luaO_resetObject(this); //after climbing the ties rope an ObjectState gets deleted but not removed
|
|
|
|
} //from the lua's userdata list, resulting in a dangling pointer
|
|
|
|
//that breaks the saving. We need to reset to NULL the pointer manually.
|
2011-03-21 05:16:27 +08:00
|
|
|
for (Common::List<Pointer *>::iterator i = _pointers.begin(); i != _pointers.end(); ++i) {
|
|
|
|
(*i)->resetPointer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-10 11:24:03 +02:00
|
|
|
void Object::saveState(SaveGame *) const {
|
2011-03-21 05:16:27 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-10 11:24:03 +02:00
|
|
|
bool Object::restoreState(SaveGame *) {
|
2011-03-21 05:16:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
void Object::reference() {
|
2011-03-21 05:16:27 +08:00
|
|
|
++_refCount;
|
|
|
|
}
|
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
void Object::dereference() {
|
2011-03-21 05:16:27 +08:00
|
|
|
if (_refCount > 0) {
|
|
|
|
--_refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_refCount == 0) {
|
|
|
|
_refCount = -1;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ObjectManager::saveObject(SaveGame *state, Object *object) {
|
|
|
|
const char *str = object->typeName();
|
|
|
|
int32 len = strlen(str);
|
|
|
|
|
|
|
|
state->writeLEUint32(len);
|
|
|
|
state->write(str, len);
|
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
object->saveState(state);
|
2011-03-21 05:16:27 +08:00
|
|
|
}
|
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
ObjectPtr<Object> ObjectManager::restoreObject(SaveGame *state) {
|
|
|
|
const char *str = state->readCharString();
|
2011-03-21 05:16:27 +08:00
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
ObjectPtr<Object> ptr;
|
|
|
|
Common::String type = str;
|
2011-03-21 05:16:27 +08:00
|
|
|
delete[] str;
|
|
|
|
if (_creators.contains(type)) {
|
|
|
|
CreatorFunc func = _creators.getVal(type);
|
2011-03-21 06:58:36 +08:00
|
|
|
ptr = (func)(state);
|
2011-03-21 05:16:27 +08:00
|
|
|
} else {
|
2011-03-21 06:58:36 +08:00
|
|
|
error("Type name \"%s\" not registered", type.c_str());
|
2011-03-21 05:16:27 +08:00
|
|
|
}
|
|
|
|
|
2011-03-21 06:58:36 +08:00
|
|
|
return ptr;
|
2011-03-21 05:16:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|