Initial commit
This commit is contained in:
215
engines/wintermute/system/sys_class.cpp
Normal file
215
engines/wintermute/system/sys_class.cpp
Normal file
@@ -0,0 +1,215 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is based on WME Lite.
|
||||
* http://dead-code.org/redir.php?target=wmelite
|
||||
* Copyright (c) 2011 Jan Nedoma
|
||||
*/
|
||||
|
||||
#include "engines/wintermute/persistent.h"
|
||||
#include "engines/wintermute/system/sys_instance.h"
|
||||
#include "engines/wintermute/system/sys_class.h"
|
||||
#include "engines/wintermute/system/sys_class_registry.h"
|
||||
#include "engines/wintermute/base/base_game.h"
|
||||
#include "engines/wintermute/base/base_persistence_manager.h"
|
||||
|
||||
namespace Wintermute {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemClass::SystemClass(const AnsiString &name, PERSISTBUILD build, PERSISTLOAD load, bool persistentClass) {
|
||||
_name = name;
|
||||
|
||||
_build = build;
|
||||
_load = load;
|
||||
_next = nullptr;
|
||||
_savedId = -1;
|
||||
_persistent = persistentClass;
|
||||
_numInst = 0;
|
||||
|
||||
SystemClassRegistry::getInstance()->registerClass(this);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemClass::~SystemClass() {
|
||||
SystemClassRegistry::getInstance()->unregisterClass(this);
|
||||
removeAllInstances();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClass::removeAllInstances() {
|
||||
Instances::iterator it;
|
||||
for (it = _instances.begin(); it != _instances.end(); ++it) {
|
||||
delete(it->_value);
|
||||
}
|
||||
_instances.clear();
|
||||
_instanceMap.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemInstance *SystemClass::addInstance(void *instance, int id, int savedId) {
|
||||
SystemInstance *inst = new SystemInstance(instance, id, this);
|
||||
inst->setSavedId(savedId);
|
||||
_instances[inst] = (inst);
|
||||
|
||||
_instanceMap[instance] = inst;
|
||||
|
||||
SystemClassRegistry::getInstance()->addInstanceToTable(inst, instance);
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClass::removeInstance(void *instance) {
|
||||
InstanceMap::iterator mapIt = _instanceMap.find(instance);
|
||||
if (mapIt == _instanceMap.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Instances::iterator it = _instances.find((mapIt->_value));
|
||||
if (it != _instances.end()) {
|
||||
delete(it->_value);
|
||||
_instances.erase(it);
|
||||
}
|
||||
|
||||
_instanceMap.erase(mapIt);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
int SystemClass::getInstanceId(void *pointer) {
|
||||
InstanceMap::iterator mapIt = _instanceMap.find(pointer);
|
||||
if (mapIt == _instanceMap.end()) {
|
||||
return -1;
|
||||
} else {
|
||||
return (mapIt->_value)->getId();
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void *SystemClass::idToPointer(int savedID) {
|
||||
//slow
|
||||
Instances::iterator it;
|
||||
for (it = _instances.begin(); it != _instances.end(); ++it) {
|
||||
if ((it->_value)->getSavedId() == savedID) {
|
||||
return (it->_value)->getInstance();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
int SystemClass::getNumInstances() {
|
||||
return _instances.size();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::dump(Common::WriteStream *stream) {
|
||||
Common::String str;
|
||||
str = Common::String::format("%03d %c %-20s instances: %d\n", _id, _persistent ? 'p' : ' ', _name.c_str(), getNumInstances());
|
||||
stream->write(str.c_str(), str.size());
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::saveTable(BaseGame *game, BasePersistenceManager *persistMgr) {
|
||||
persistMgr->putString(_name.c_str());
|
||||
persistMgr->putDWORD(_id);
|
||||
persistMgr->putDWORD(_instances.size());
|
||||
|
||||
Instances::iterator it;
|
||||
for (it = _instances.begin(); it != _instances.end(); ++it) {
|
||||
persistMgr->putDWORD((it->_value)->getId());
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::loadTable(BaseGame *game, BasePersistenceManager *persistMgr) {
|
||||
_savedId = persistMgr->getDWORD();
|
||||
int numInstances = persistMgr->getDWORD();
|
||||
|
||||
for (int i = 0; i < numInstances; i++) {
|
||||
int instId = persistMgr->getDWORD();
|
||||
if (_persistent) {
|
||||
|
||||
if (i > 0) {
|
||||
game->LOG(0, "Warning: attempting to load multiple instances of persistent class %s (%d)", _name.c_str(), numInstances);
|
||||
continue;
|
||||
}
|
||||
|
||||
Instances::iterator it = _instances.begin();
|
||||
if (it != _instances.end()) {
|
||||
(it->_value)->setSavedId(instId);
|
||||
SystemClassRegistry::getInstance()->addInstanceToTable((it->_value), (it->_value)->getInstance());
|
||||
} else {
|
||||
game->LOG(0, "Warning: instance %d of persistent class %s not found", i, _name.c_str());
|
||||
}
|
||||
}
|
||||
// normal instances, create empty objects
|
||||
else {
|
||||
void *emptyObject = _build();
|
||||
addInstance(emptyObject, SystemClassRegistry::getInstance()->getNextId(), instId);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::saveInstances(BaseGame *game, BasePersistenceManager *persistMgr) {
|
||||
Instances::iterator it;
|
||||
for (it = _instances.begin(); it != _instances.end(); ++it) {
|
||||
// write instace header
|
||||
persistMgr->putString("<INSTANCE_HEAD>");
|
||||
persistMgr->putDWORD(_id);
|
||||
persistMgr->putDWORD((it->_value)->getId());
|
||||
persistMgr->putString("</INSTANCE_HEAD>");
|
||||
_load((it->_value)->getInstance(), persistMgr);
|
||||
persistMgr->putString("</INSTANCE>");
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::loadInstance(void *instance, BasePersistenceManager *persistMgr) {
|
||||
_load(instance, persistMgr);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::resetSavedIDs() {
|
||||
Instances::iterator it;
|
||||
for (it = _instances.begin(); it != _instances.end(); ++it) {
|
||||
(it->_value)->setSavedId(-1);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClass::instanceCallback(SYS_INSTANCE_CALLBACK lpCallback, void *lpData) {
|
||||
Instances::iterator it;
|
||||
for (it = _instances.begin(); it != _instances.end(); ++it) {
|
||||
lpCallback((it->_value)->getInstance(), lpData);
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace Wintermute
|
||||
129
engines/wintermute/system/sys_class.h
Normal file
129
engines/wintermute/system/sys_class.h
Normal file
@@ -0,0 +1,129 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is based on WME Lite.
|
||||
* http://dead-code.org/redir.php?target=wmelite
|
||||
* Copyright (c) 2011 Jan Nedoma
|
||||
*/
|
||||
|
||||
#ifndef WINTERMUTE_SYSCLASS_H
|
||||
#define WINTERMUTE_SYSCLASS_H
|
||||
|
||||
#include "engines/wintermute/persistent.h"
|
||||
#include "engines/wintermute/dctypes.h"
|
||||
#include "common/hashmap.h"
|
||||
#include "common/func.h"
|
||||
#include "common/stream.h"
|
||||
|
||||
namespace Wintermute {
|
||||
class SystemInstance;
|
||||
class BaseGame;
|
||||
class BasePersistenceManager;
|
||||
class SystemClass;
|
||||
|
||||
}
|
||||
|
||||
namespace Common {
|
||||
template<typename T> struct Hash;
|
||||
|
||||
template<> struct Hash<void *> : public UnaryFunction<void *, uint> {
|
||||
uint operator()(void *val) const {
|
||||
return (uint)((size_t)val);
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct Hash<Wintermute::SystemInstance *> : public UnaryFunction<Wintermute::SystemInstance *, uint> {
|
||||
uint operator()(Wintermute::SystemInstance *val) const {
|
||||
return (uint)((size_t)val);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
namespace Wintermute {
|
||||
|
||||
class SystemClass {
|
||||
public:
|
||||
SystemClass(const AnsiString &name, PERSISTBUILD build, PERSISTLOAD load, bool persistentClass);
|
||||
~SystemClass();
|
||||
|
||||
int getNumInstances();
|
||||
bool removeInstance(void *instance);
|
||||
SystemInstance *addInstance(void *instance, int id, int savedId = -1);
|
||||
bool removeAllInstances();
|
||||
|
||||
int getInstanceId(void *pointer);
|
||||
void *idToPointer(int savedId);
|
||||
|
||||
void setId(int id) {
|
||||
_id = id;
|
||||
}
|
||||
int getId() const {
|
||||
return _id;
|
||||
}
|
||||
|
||||
int getSavedId() const {
|
||||
return _savedId;
|
||||
}
|
||||
|
||||
bool isPersistent() const {
|
||||
return _persistent;
|
||||
}
|
||||
|
||||
AnsiString getName() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
void saveTable(BaseGame *game, BasePersistenceManager *persistMgr);
|
||||
void loadTable(BaseGame *game, BasePersistenceManager *persistMgr);
|
||||
|
||||
void saveInstances(BaseGame *game, BasePersistenceManager *persistMgr);
|
||||
void loadInstance(void *instance, BasePersistenceManager *persistMgr);
|
||||
|
||||
void instanceCallback(SYS_INSTANCE_CALLBACK lpCallback, void *lpData);
|
||||
|
||||
void resetSavedIDs();
|
||||
|
||||
void dump(Common::WriteStream *stream);
|
||||
|
||||
private:
|
||||
int _numInst;
|
||||
bool _persistent;
|
||||
SystemClass *_next;
|
||||
int _id{};
|
||||
int _savedId;
|
||||
AnsiString _name;
|
||||
PERSISTBUILD _build;
|
||||
PERSISTLOAD _load;
|
||||
|
||||
//typedef std::set<SystemInstance *> Instances;
|
||||
typedef Common::HashMap<SystemInstance *, SystemInstance *> Instances;
|
||||
Instances _instances;
|
||||
|
||||
typedef Common::HashMap<void *, SystemInstance *> InstanceMap;
|
||||
InstanceMap _instanceMap;
|
||||
};
|
||||
|
||||
} // End of namespace Wintermute
|
||||
|
||||
#endif
|
||||
344
engines/wintermute/system/sys_class_registry.cpp
Normal file
344
engines/wintermute/system/sys_class_registry.cpp
Normal file
@@ -0,0 +1,344 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is based on WME Lite.
|
||||
* http://dead-code.org/redir.php?target=wmelite
|
||||
* Copyright (c) 2011 Jan Nedoma
|
||||
*/
|
||||
|
||||
#include "engines/wintermute/base/base_game.h"
|
||||
#include "engines/wintermute/platform_osystem.h"
|
||||
#include "engines/wintermute/base/base_engine.h"
|
||||
#include "engines/wintermute/base/gfx/base_renderer.h"
|
||||
#include "engines/wintermute/system/sys_instance.h"
|
||||
#include "engines/wintermute/system/sys_class_registry.h"
|
||||
#include "engines/wintermute/system/sys_class.h"
|
||||
#include "engines/wintermute/wintermute.h"
|
||||
#include "common/stream.h"
|
||||
|
||||
namespace Wintermute {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemClassRegistry::SystemClassRegistry() {
|
||||
_count = 0;
|
||||
_disabled = false;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemClassRegistry::~SystemClassRegistry() {
|
||||
unregisterClasses();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemClassRegistry *SystemClassRegistry::getInstance() {
|
||||
return BaseEngine::instance().getClassRegistry();
|
||||
}
|
||||
|
||||
void SystemClassRegistry::unregisterClasses() {
|
||||
// SystemClass calls UnregisterClass upon destruction.
|
||||
while (_classes.size() > 0) {
|
||||
delete _classes.begin()->_value;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::registerClass(SystemClass *classObj) {
|
||||
classObj->setId(_count++);
|
||||
//_classes.insert(classObj);
|
||||
_classes[classObj] = classObj;
|
||||
|
||||
_nameMap[classObj->getName()] = classObj;
|
||||
_idMap[classObj->getId()] = classObj;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::unregisterClass(SystemClass *classObj) {
|
||||
|
||||
Classes::iterator it = _classes.find(classObj);
|
||||
if (it == _classes.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (classObj->getNumInstances() != 0) {
|
||||
debugC(Wintermute::kWintermuteDebugSaveGame, "Memory leak@class %-20s: %d instance(s) left\n", classObj->getName().c_str(), classObj->getNumInstances());
|
||||
}
|
||||
_classes.erase(it);
|
||||
|
||||
NameMap::iterator mapIt = _nameMap.find(classObj->getName());
|
||||
if (mapIt != _nameMap.end()) {
|
||||
_nameMap.erase(mapIt);
|
||||
}
|
||||
|
||||
IdMap::iterator idIt = _idMap.find(classObj->getId());
|
||||
if (idIt != _idMap.end()) {
|
||||
_idMap.erase(idIt);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::registerInstance(const char *className, void *instance) {
|
||||
if (_disabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
NameMap::iterator mapIt = _nameMap.find(className);
|
||||
if (mapIt == _nameMap.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SystemInstance *inst = (*mapIt)._value->addInstance(instance, _count++);
|
||||
return (inst != nullptr);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClassRegistry::addInstanceToTable(SystemInstance *instance, void *pointer) {
|
||||
_instanceMap[pointer] = instance;
|
||||
|
||||
if (instance->getSavedId() >= 0) {
|
||||
_savedInstanceMap[instance->getSavedId()] = instance;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
int SystemClassRegistry::getNextId() {
|
||||
return _count++;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::unregisterInstance(const char *className, void *instance) {
|
||||
NameMap::iterator mapIt = _nameMap.find(className);
|
||||
if (mapIt == _nameMap.end()) {
|
||||
return false;
|
||||
}
|
||||
(*mapIt)._value->removeInstance(instance);
|
||||
|
||||
InstanceMap::iterator instIt = _instanceMap.find(instance);
|
||||
if (instIt != _instanceMap.end()) {
|
||||
_instanceMap.erase(instIt);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::getPointerID(void *pointer, int *classID, int *instanceID) {
|
||||
if (pointer == nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
InstanceMap::iterator it = _instanceMap.find(pointer);
|
||||
if (it == _instanceMap.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
SystemInstance *inst = (*it)._value;
|
||||
*instanceID = inst->getId();
|
||||
*classID = inst->getClass()->getId();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void *SystemClassRegistry::idToPointer(int classID, int instanceID) {
|
||||
SavedInstanceMap::iterator it = _savedInstanceMap.find(instanceID);
|
||||
if (it == _savedInstanceMap.end()) {
|
||||
return nullptr;
|
||||
} else {
|
||||
return (*it)._value->getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
bool checkHeader(const char *tag, BasePersistenceManager *pm) {
|
||||
char *test = pm->getString();
|
||||
Common::String verify = test;
|
||||
delete[] test;
|
||||
bool retVal = (verify == tag);
|
||||
if (!retVal) {
|
||||
error("Expected %s in Save-file not found", tag);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::saveTable(BaseGame *game, BasePersistenceManager *persistMgr, bool quickSave) {
|
||||
persistMgr->putString("<CLASS_REGISTRY_TABLE>");
|
||||
persistMgr->putDWORD(_classes.size());
|
||||
|
||||
int counter = 0;
|
||||
|
||||
Classes::iterator it;
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
counter++;
|
||||
|
||||
if (!quickSave) {
|
||||
game->setIndicatorVal((int)(50.0f / (float)((float)_classes.size() / (float)counter)));
|
||||
}
|
||||
|
||||
(it->_value)->saveTable(game, persistMgr);
|
||||
}
|
||||
persistMgr->putString("</CLASS_REGISTRY_TABLE>");
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::loadTable(BaseGame *game, BasePersistenceManager *persistMgr) {
|
||||
checkHeader("<CLASS_REGISTRY_TABLE>", persistMgr);
|
||||
|
||||
// reset SavedID of current instances
|
||||
Classes::iterator it;
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
(it->_value)->resetSavedIDs();
|
||||
}
|
||||
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
if ((it->_value)->isPersistent()) {
|
||||
continue;
|
||||
}
|
||||
(it->_value)->removeAllInstances();
|
||||
}
|
||||
|
||||
_instanceMap.clear();
|
||||
|
||||
uint32 numClasses = persistMgr->getDWORD();
|
||||
|
||||
for (uint32 i = 0; i < numClasses; i++) {
|
||||
game->setIndicatorVal((int)(50.0f / (float)((float)numClasses / (float)(i + 1))));
|
||||
|
||||
Common::String className = persistMgr->getStringObj();
|
||||
|
||||
// WA to be removed later
|
||||
// This allow load save games where SXVlink class reference is stored
|
||||
if (className == "SXVlink" && !game->_targetName.contains("sunrise")) {
|
||||
persistMgr->getDWORD(); // saveId
|
||||
persistMgr->getDWORD(); // numInstances
|
||||
continue;
|
||||
}
|
||||
|
||||
NameMap::iterator mapIt = _nameMap.find(className);
|
||||
if (mapIt != _nameMap.end()) {
|
||||
(*mapIt)._value->loadTable(game, persistMgr);
|
||||
}
|
||||
}
|
||||
|
||||
checkHeader("</CLASS_REGISTRY_TABLE>", persistMgr);
|
||||
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::saveInstances(BaseGame *game, BasePersistenceManager *persistMgr, bool quickSave) {
|
||||
|
||||
Classes::iterator it;
|
||||
|
||||
// count total instances
|
||||
int numInstances = 0;
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
numInstances += (it->_value)->getNumInstances();
|
||||
}
|
||||
|
||||
persistMgr->putDWORD(numInstances);
|
||||
|
||||
int counter = 0;
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
counter++;
|
||||
|
||||
if (!quickSave) {
|
||||
if (counter % 20 == 0) {
|
||||
game->setIndicatorVal((int)(50.0f + 50.0f / (float)((float)_classes.size() / (float)counter)));
|
||||
}
|
||||
}
|
||||
game->miniUpdate();
|
||||
|
||||
(it->_value)->saveInstances(game, persistMgr);
|
||||
}
|
||||
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::loadInstances(BaseGame *game, BasePersistenceManager *persistMgr) {
|
||||
// get total instances
|
||||
int numInstances = persistMgr->getDWORD();
|
||||
|
||||
for (int i = 0; i < numInstances; i++) {
|
||||
if (i % 20 == 0) {
|
||||
game->setIndicatorVal((int)(50.0f + 50.0f / (float)((float)numInstances / (float)(i + 1))));
|
||||
}
|
||||
|
||||
checkHeader("<INSTANCE_HEAD>", persistMgr);
|
||||
|
||||
int classID = persistMgr->getDWORD();
|
||||
int instanceID = persistMgr->getDWORD();
|
||||
void *instance = idToPointer(classID, instanceID);
|
||||
|
||||
checkHeader("</INSTANCE_HEAD>", persistMgr);
|
||||
|
||||
Classes::iterator it;
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
if ((it->_value)->getSavedId() == classID) {
|
||||
(it->_value)->loadInstance(instance, persistMgr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
checkHeader("</INSTANCE>", persistMgr);
|
||||
}
|
||||
|
||||
_savedInstanceMap.clear();
|
||||
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool SystemClassRegistry::enumInstances(SYS_INSTANCE_CALLBACK lpCallback, const char *className, void *lpData) {
|
||||
NameMap::iterator mapIt = _nameMap.find(className);
|
||||
if (mapIt == _nameMap.end()) {
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
|
||||
(*mapIt)._value->instanceCallback(lpCallback, lpData);
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void SystemClassRegistry::dumpClasses(Common::WriteStream *stream) {
|
||||
Classes::iterator it;
|
||||
for (it = _classes.begin(); it != _classes.end(); ++it) {
|
||||
(it->_value)->dump(stream);
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace Wintermute
|
||||
108
engines/wintermute/system/sys_class_registry.h
Normal file
108
engines/wintermute/system/sys_class_registry.h
Normal file
@@ -0,0 +1,108 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is based on WME Lite.
|
||||
* http://dead-code.org/redir.php?target=wmelite
|
||||
* Copyright (c) 2011 Jan Nedoma
|
||||
*/
|
||||
|
||||
#ifndef WINTERMUTE_SYSCLASSREGISTRY_H
|
||||
#define WINTERMUTE_SYSCLASSREGISTRY_H
|
||||
|
||||
#include "engines/wintermute/wintypes.h"
|
||||
#include "engines/wintermute/dctypes.h"
|
||||
#include "engines/wintermute/system/sys_class.h"
|
||||
#include "common/hashmap.h"
|
||||
#include "common/hash-str.h"
|
||||
#include "common/func.h"
|
||||
#include "common/stream.h"
|
||||
|
||||
namespace Wintermute {
|
||||
class SystemClass;
|
||||
}
|
||||
|
||||
namespace Common {
|
||||
template<typename T> struct Hash;
|
||||
template<> struct Hash<Wintermute::SystemClass *> : public UnaryFunction<Wintermute::SystemClass *, uint> {
|
||||
uint operator()(Wintermute::SystemClass *val) const {
|
||||
return (uint)((size_t)val);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace Wintermute {
|
||||
|
||||
class BaseGame;
|
||||
class BasePersistenceManager;
|
||||
class SystemInstance;
|
||||
|
||||
class SystemClassRegistry {
|
||||
void unregisterClasses();
|
||||
public:
|
||||
void registerClasses(); // persistent.cpp
|
||||
#ifdef ENABLE_WME3D
|
||||
void register3DClasses(); // persistent.cpp
|
||||
#endif
|
||||
static SystemClassRegistry *getInstance();
|
||||
|
||||
bool enumInstances(SYS_INSTANCE_CALLBACK lpCallback, const char *className, void *lpData);
|
||||
bool loadTable(BaseGame *game, BasePersistenceManager *persistMgr);
|
||||
bool saveTable(BaseGame *game, BasePersistenceManager *persistMgr, bool quickSave);
|
||||
bool loadInstances(BaseGame *game, BasePersistenceManager *persistMgr);
|
||||
bool saveInstances(BaseGame *game, BasePersistenceManager *persistMgr, bool quickSave);
|
||||
void *idToPointer(int classID, int instanceId);
|
||||
bool getPointerID(void *pointer, int *classId, int *instanceId);
|
||||
bool registerClass(SystemClass *classObj);
|
||||
bool unregisterClass(SystemClass *classObj);
|
||||
bool registerInstance(const char *className, void *instance);
|
||||
bool unregisterInstance(const char *className, void *instance);
|
||||
void dumpClasses(Common::WriteStream *stream);
|
||||
int getNextId();
|
||||
void addInstanceToTable(SystemInstance *instance, void *pointer);
|
||||
|
||||
SystemClassRegistry();
|
||||
virtual ~SystemClassRegistry();
|
||||
|
||||
bool _disabled;
|
||||
int _count;
|
||||
|
||||
typedef Common::HashMap<SystemClass *, SystemClass *> Classes;
|
||||
Classes _classes;
|
||||
|
||||
typedef Common::HashMap<AnsiString, SystemClass *> NameMap;
|
||||
NameMap _nameMap;
|
||||
|
||||
typedef Common::HashMap<int, SystemClass *> IdMap;
|
||||
IdMap _idMap;
|
||||
|
||||
typedef Common::HashMap<void *, SystemInstance *> InstanceMap;
|
||||
InstanceMap _instanceMap;
|
||||
|
||||
typedef Common::HashMap<int, SystemInstance *> SavedInstanceMap;
|
||||
SavedInstanceMap _savedInstanceMap;
|
||||
|
||||
};
|
||||
|
||||
} // End of namespace Wintermute
|
||||
|
||||
#endif
|
||||
48
engines/wintermute/system/sys_instance.cpp
Normal file
48
engines/wintermute/system/sys_instance.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is based on WME Lite.
|
||||
* http://dead-code.org/redir.php?target=wmelite
|
||||
* Copyright (c) 2011 Jan Nedoma
|
||||
*/
|
||||
|
||||
#include "engines/wintermute/system/sys_instance.h"
|
||||
#include "engines/wintermute/system/sys_class_registry.h"
|
||||
#include "engines/wintermute/system/sys_class.h"
|
||||
|
||||
namespace Wintermute {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemInstance::SystemInstance(void *instance, int id, SystemClass *sysClass) {
|
||||
_instance = instance;
|
||||
_id = id;
|
||||
_savedId = -1;
|
||||
_class = sysClass;
|
||||
|
||||
_used = false;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
SystemInstance::~SystemInstance() {
|
||||
}
|
||||
|
||||
} // End of namespace Wintermute
|
||||
67
engines/wintermute/system/sys_instance.h
Normal file
67
engines/wintermute/system/sys_instance.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is based on WME Lite.
|
||||
* http://dead-code.org/redir.php?target=wmelite
|
||||
* Copyright (c) 2011 Jan Nedoma
|
||||
*/
|
||||
|
||||
#ifndef WINTERMUTE_SYSINSTANCE_H
|
||||
#define WINTERMUTE_SYSINSTANCE_H
|
||||
|
||||
namespace Wintermute {
|
||||
|
||||
class SystemClass;
|
||||
|
||||
class SystemInstance {
|
||||
public:
|
||||
SystemInstance(void *instance, int id, SystemClass *sysClass);
|
||||
virtual ~SystemInstance();
|
||||
|
||||
int getId() const {
|
||||
return _id;
|
||||
}
|
||||
int getSavedId() const {
|
||||
return _savedId;
|
||||
}
|
||||
void *getInstance() const {
|
||||
return _instance;
|
||||
}
|
||||
SystemClass *getClass() const {
|
||||
return _class;
|
||||
}
|
||||
|
||||
void setSavedId(int id) {
|
||||
_savedId = id;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _used;
|
||||
int _id;
|
||||
int _savedId;
|
||||
void *_instance;
|
||||
SystemClass *_class;
|
||||
};
|
||||
|
||||
} // End of namespace Wintermute
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user