diff options
Diffstat (limited to 'engines')
-rw-r--r-- | engines/saga/actor.cpp | 1 | ||||
-rw-r--r-- | engines/saga/resource_res.cpp | 4 | ||||
-rw-r--r-- | engines/saga/saga.cpp | 60 | ||||
-rw-r--r-- | engines/saga/saga.h | 25 | ||||
-rw-r--r-- | engines/saga/scene.cpp | 2 | ||||
-rw-r--r-- | engines/saga/script.cpp | 1 | ||||
-rw-r--r-- | engines/saga/script.h | 2 |
7 files changed, 46 insertions, 49 deletions
diff --git a/engines/saga/actor.cpp b/engines/saga/actor.cpp index 0dfe2294b5..be011d0119 100644 --- a/engines/saga/actor.cpp +++ b/engines/saga/actor.cpp @@ -296,7 +296,6 @@ Actor::~Actor() { debug(9, "Actor::~Actor()"); free(_pathCell); - _actorsStrings.freeMem(); //release resources freeProtagStates(); freeActorList(); diff --git a/engines/saga/resource_res.cpp b/engines/saga/resource_res.cpp index 848d14436a..ab983ba0d4 100644 --- a/engines/saga/resource_res.cpp +++ b/engines/saga/resource_res.cpp @@ -108,7 +108,7 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) { _vm->_actor->_protagonist->_sceneNumber = _metaResource.sceneIndex; - _vm->_actor->_objectsStrings.freeMem(); + _vm->_actor->_objectsStrings.clear(); _vm->_resource->loadResource(resourceContext, _metaResource.objectsStringsResourceID, resourcePointer, resourceLength); _vm->loadStrings(_vm->_actor->_objectsStrings, resourcePointer, resourceLength); @@ -142,7 +142,7 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) { _vm->_interface->_defPortraits.clear(); _vm->_sprite->loadList(_metaResource.protagFaceSpritesID, _vm->_interface->_defPortraits); - _vm->_actor->_actorsStrings.freeMem(); + _vm->_actor->_actorsStrings.clear(); _vm->_resource->loadResource(resourceContext, _metaResource.actorsStringsResourceID, resourcePointer, resourceLength); _vm->loadStrings(_vm->_actor->_actorsStrings, resourcePointer, resourceLength); diff --git a/engines/saga/saga.cpp b/engines/saga/saga.cpp index 1643ecfa17..745414f0df 100644 --- a/engines/saga/saga.cpp +++ b/engines/saga/saga.cpp @@ -397,24 +397,22 @@ void SagaEngine::loadStrings(StringsTable &stringsTable, const byte *stringsPoin uint16 stringsCount; size_t offset; size_t prevOffset = 0; - int i; + Common::Array<size_t> tempOffsets; + uint ui; if (stringsLength == 0) { error("SagaEngine::loadStrings() Error loading strings list resource"); } - stringsTable.stringsPointer = (byte*)malloc(stringsLength); - memcpy(stringsTable.stringsPointer, stringsPointer, stringsLength); - - MemoryReadStreamEndian scriptS(stringsTable.stringsPointer, stringsLength, isBigEndian()); //TODO: get endianess from context + MemoryReadStreamEndian scriptS(stringsPointer, stringsLength, isBigEndian()); //TODO: get endianess from context offset = scriptS.readUint16(); stringsCount = offset / 2; - stringsTable.strings = (const char **)malloc(stringsCount * sizeof(*stringsTable.strings)); - i = 0; + ui = 0; scriptS.seek(0); - while (i < stringsCount) { + tempOffsets.resize(stringsCount); + while (ui < stringsCount) { offset = scriptS.readUint16(); // In some rooms in IHNM, string offsets can be greater than the maximum value than a 16-bit integer can hold // We detect this by checking the previous offset, and if it was bigger than the current one, an overflow @@ -424,33 +422,43 @@ void SagaEngine::loadStrings(StringsTable &stringsTable, const byte *stringsPoin offset += 65536; prevOffset = offset; if (offset == stringsLength) { - stringsCount = i; - const char **tmp = (const char **)realloc(stringsTable.strings, stringsCount * sizeof(*stringsTable.strings)); - if ((tmp != NULL) || (stringsCount == 0)) { - stringsTable.strings = tmp; - } else { - error("SagaEngine::loadStrings() Error while reallocating memory"); - } + stringsCount = ui; + tempOffsets.resize(stringsCount); break; } if (offset > stringsLength) { // This case should never occur, but apparently it does in the Italian fan // translation of IHNM warning("SagaEngine::loadStrings wrong strings table"); - stringsCount = i; - const char **tmp = (const char **)realloc(stringsTable.strings, stringsCount * sizeof(*stringsTable.strings)); - if ((tmp != NULL) || (stringsCount == 0)) { - stringsTable.strings = tmp; - } else { - error("SagaEngine::loadStrings() Error while reallocating memory"); - } + stringsCount = ui; + tempOffsets.resize(stringsCount); break; } - stringsTable.strings[i] = (const char *)stringsTable.stringsPointer + offset; - debug(9, "string[%i]=%s", i, stringsTable.strings[i]); - i++; + tempOffsets[ui] = offset; + ui++; + } + + prevOffset = scriptS.pos(); + int32 left = scriptS.size() - prevOffset; + if (left < 0) { + error("SagaEngine::loadStrings() Error loading strings buffer"); + } + + stringsTable.buffer.resize(left); + if (left > 0) { + scriptS.read(&stringsTable.buffer.front(), left); + } + + stringsTable.strings.resize(tempOffsets.size()); + for (ui = 0; ui < tempOffsets.size(); ui++) { + offset = tempOffsets[ui] - prevOffset; + if (offset >= stringsTable.buffer.size()) { + error("SagaEngine::loadStrings() Wrong offset"); + } + stringsTable.strings[ui] = &stringsTable.buffer[offset]; + + debug(9, "string[%i]=%s", ui, stringsTable.strings[ui]); } - stringsTable.stringsCount = stringsCount; } const char *SagaEngine::getObjectName(uint16 objectId) { diff --git a/engines/saga/saga.h b/engines/saga/saga.h index 34937c5131..589be1becc 100644 --- a/engines/saga/saga.h +++ b/engines/saga/saga.h @@ -381,30 +381,21 @@ struct ImageHeader { }; struct StringsTable { - byte *stringsPointer; - int stringsCount; - const char **strings; + Common::Array<char> buffer; + Common::Array<char *> strings; - const char *getString(int index) const { - if ((stringsCount <= index) || (index < 0)) { + const char *getString(uint index) const { + if ((strings.size() <= index) || (index < 0)) { // This occurs at the end of Ted's chapter, right after the ending cutscene - warning("StringsTable::getString wrong index 0x%X (%d)", index, stringsCount); + warning("StringsTable::getString wrong index 0x%X (%d)", index, strings.size()); return ""; } return strings[index]; } - void freeMem() { - free(strings); - free(stringsPointer); - memset(this, 0, sizeof(*this)); - } - - StringsTable() { - memset(this, 0, sizeof(*this)); - } - ~StringsTable() { - freeMem(); + void clear() { + strings.clear(); + buffer.clear(); } }; diff --git a/engines/saga/scene.cpp b/engines/saga/scene.cpp index 2887d79693..5b42932ec8 100644 --- a/engines/saga/scene.cpp +++ b/engines/saga/scene.cpp @@ -1249,7 +1249,7 @@ void Scene::endScene() { _objectMap->freeMem(); _actionMap->freeMem(); _entryList.freeMem(); - _sceneStrings.freeMem(); + _sceneStrings.clear(); if (_vm->getGameId() == GID_ITE) _vm->_isoMap->freeMem(); diff --git a/engines/saga/script.cpp b/engines/saga/script.cpp index b0e20da48c..f40d15f30c 100644 --- a/engines/saga/script.cpp +++ b/engines/saga/script.cpp @@ -158,7 +158,6 @@ SAGA1Script::SAGA1Script(SagaEngine *vm) : Script(vm) { SAGA1Script::~SAGA1Script() { debug(8, "Shutting down scripting subsystem."); - _mainStrings.freeMem(); _globalVoiceLUT.freeMem(); freeModules(); diff --git a/engines/saga/script.h b/engines/saga/script.h index 21afeb5c44..56261a9365 100644 --- a/engines/saga/script.h +++ b/engines/saga/script.h @@ -159,7 +159,7 @@ struct ModuleData { StringsTable strings; VoiceLUT voiceLUT; void freeMem() { - strings.freeMem(); + strings.clear(); voiceLUT.freeMem(); free(moduleBase); free(entryPoints); |