From 562a8a980c22eab85144558050e5fa5e425612c4 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Fri, 15 Jun 2012 12:53:17 +0300 Subject: [PATCH] SCI: Further cleanup of the script code Merge the init() and load() Script methods and reset the script when necessary --- engines/sci/engine/savegame.cpp | 7 ++---- engines/sci/engine/script.cpp | 34 ++++++++++++------------------ engines/sci/engine/script.h | 3 +-- engines/sci/engine/seg_manager.cpp | 3 +-- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/engines/sci/engine/savegame.cpp b/engines/sci/engine/savegame.cpp index 7c41e18becc..cabe5f468a5 100644 --- a/engines/sci/engine/savegame.cpp +++ b/engines/sci/engine/savegame.cpp @@ -189,7 +189,7 @@ void SegManager::saveLoadWithSerializer(Common::Serializer &s) { assert(mobj); - // Let the object sync custom data + // Let the object sync custom data. Scripts are loaded at this point. mobj->saveLoadWithSerializer(s); if (type == SEG_TYPE_SCRIPT) { @@ -200,9 +200,6 @@ void SegManager::saveLoadWithSerializer(Common::Serializer &s) { // Hook the script up in the script->segment map _scriptSegMap[scr->getScriptNumber()] = i; - // Now, load the script itself - scr->load(g_sci->getResMan()); - ObjMap objects = scr->getObjectMap(); for (ObjMap::iterator it = objects.begin(); it != objects.end(); ++it) it->_value.syncBaseObject(scr->getBuf(it->_value.getPos().offset)); @@ -486,7 +483,7 @@ void Script::saveLoadWithSerializer(Common::Serializer &s) { s.syncAsSint32LE(_nr); if (s.isLoading()) - init(_nr, g_sci->getResMan()); + load(_nr, g_sci->getResMan()); s.skip(4, VER(14), VER(22)); // OBSOLETE: Used to be _bufSize s.skip(4, VER(14), VER(22)); // OBSOLETE: Used to be _scriptSize s.skip(4, VER(14), VER(22)); // OBSOLETE: Used to be _heapSize diff --git a/engines/sci/engine/script.cpp b/engines/sci/engine/script.cpp index 18e23f36b5b..77149831204 100644 --- a/engines/sci/engine/script.cpp +++ b/engines/sci/engine/script.cpp @@ -32,12 +32,21 @@ namespace Sci { -Script::Script() : SegmentObj(SEG_TYPE_SCRIPT) { +Script::Script() : SegmentObj(SEG_TYPE_SCRIPT), _buf(NULL) { + freeScript(); +} + +Script::~Script() { + freeScript(); +} + +void Script::freeScript() { _nr = 0; + + free(_buf); _buf = NULL; _bufSize = 0; _scriptSize = 0; - _heapStart = NULL; _heapSize = 0; @@ -52,23 +61,13 @@ Script::Script() : SegmentObj(SEG_TYPE_SCRIPT) { _localsCount = 0; _lockers = 1; - _markedAsDeleted = false; -} - -Script::~Script() { - freeScript(); -} - -void Script::freeScript() { - free(_buf); - _buf = NULL; - _bufSize = 0; - _objects.clear(); } -void Script::init(int script_nr, ResourceManager *resMan) { +void Script::load(int script_nr, ResourceManager *resMan) { + freeScript(); + Resource *script = resMan->findResource(ResourceId(kResourceTypeScript, script_nr), 0); if (!script) error("Script %d not found", script_nr); @@ -118,11 +117,6 @@ void Script::init(int script_nr, ResourceManager *resMan) { error("TODO: SCI script %d is over 64KB - it's %d bytes long. This can't " "be handled at the moment, thus stopping", script_nr, script->size); } -} - -void Script::load(ResourceManager *resMan) { - Resource *script = resMan->findResource(ResourceId(kResourceTypeScript, _nr), 0); - assert(script != 0); uint extraLocalsWorkaround = 0; if (g_sci->getGameId() == GID_FANMADE && _nr == 1 && script->size == 11140) { diff --git a/engines/sci/engine/script.h b/engines/sci/engine/script.h index 06a7f089bab..4d23ffbba87 100644 --- a/engines/sci/engine/script.h +++ b/engines/sci/engine/script.h @@ -95,8 +95,7 @@ public: ~Script(); void freeScript(); - void init(int script_nr, ResourceManager *resMan); - void load(ResourceManager *resMan); + void load(int script_nr, ResourceManager *resMan); void matchSignatureAndPatch(uint16 scriptNr, byte *scriptData, const uint32 scriptSize); int32 findSignature(const SciScriptSignature *signature, const byte *scriptData, const uint32 scriptSize); diff --git a/engines/sci/engine/seg_manager.cpp b/engines/sci/engine/seg_manager.cpp index cfa83a7697d..8f85577fb89 100644 --- a/engines/sci/engine/seg_manager.cpp +++ b/engines/sci/engine/seg_manager.cpp @@ -977,8 +977,7 @@ int SegManager::instantiateScript(int scriptNum) { scr = allocateScript(scriptNum, &segmentId); } - scr->init(scriptNum, _resMan); - scr->load(_resMan); + scr->load(scriptNum, _resMan); scr->initializeLocals(this); scr->initializeClasses(this); scr->initializeObjects(this, segmentId);