2009-02-17 15:09:09 +00:00
|
|
|
/* 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 02:34:24 +01:00
|
|
|
*
|
2009-02-17 15:09:09 +00:00
|
|
|
* 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.
|
2014-02-18 02:34:24 +01:00
|
|
|
*
|
2009-02-17 15:09:09 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2015-05-04 21:19:05 +02:00
|
|
|
#include "sci/console.h"
|
2009-05-13 16:52:41 +00:00
|
|
|
#include "sci/sci.h"
|
2009-05-15 14:07:45 +00:00
|
|
|
#include "sci/resource.h"
|
2010-05-18 04:17:58 +00:00
|
|
|
#include "sci/util.h"
|
2010-02-13 17:44:58 +00:00
|
|
|
#include "sci/engine/features.h"
|
2009-02-27 02:23:40 +00:00
|
|
|
#include "sci/engine/state.h"
|
2010-01-29 11:05:06 +00:00
|
|
|
#include "sci/engine/kernel.h"
|
|
|
|
#include "sci/engine/script.h"
|
|
|
|
|
2009-02-16 00:34:40 +00:00
|
|
|
#include "common/util.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
const char *sciObjectTypeNames[] = {
|
|
|
|
"terminator", "object", "code", "synonyms", "said", "strings", "class",
|
|
|
|
"exports", "pointers", "preload text", "local vars"
|
|
|
|
};
|
|
|
|
|
2013-12-04 20:42:16 +01:00
|
|
|
Script::Script()
|
2016-12-31 20:39:57 -06:00
|
|
|
: SegmentObj(SEG_TYPE_SCRIPT), _buf() {
|
2012-06-15 12:53:17 +03:00
|
|
|
freeScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
Script::~Script() {
|
|
|
|
freeScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::freeScript() {
|
2010-06-27 20:38:43 +00:00
|
|
|
_nr = 0;
|
2012-06-15 12:53:17 +03:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
_buf.clear();
|
|
|
|
_script.clear();
|
|
|
|
_heap.clear();
|
|
|
|
_exports.clear();
|
2012-06-15 11:48:56 +03:00
|
|
|
_numExports = 0;
|
2016-12-31 20:39:57 -06:00
|
|
|
_synonyms.clear();
|
2012-06-15 11:48:56 +03:00
|
|
|
_numSynonyms = 0;
|
2010-06-27 20:38:43 +00:00
|
|
|
|
|
|
|
_localsOffset = 0;
|
|
|
|
_localsSegment = 0;
|
|
|
|
_localsBlock = NULL;
|
|
|
|
_localsCount = 0;
|
|
|
|
|
2012-06-15 11:48:56 +03:00
|
|
|
_lockers = 1;
|
2010-06-27 20:38:43 +00:00
|
|
|
_markedAsDeleted = false;
|
2012-06-15 12:53:17 +03:00
|
|
|
_objects.clear();
|
2015-05-17 18:51:22 +02:00
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
_offsetLookupArray.clear();
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupObjectCount = 0;
|
|
|
|
_offsetLookupStringCount = 0;
|
|
|
|
_offsetLookupSaidCount = 0;
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 10:11:24 -05:00
|
|
|
enum {
|
|
|
|
kSci11NumExportsOffset = 6,
|
|
|
|
kSci11ExportTableOffset = 8
|
|
|
|
};
|
|
|
|
|
2013-12-04 20:42:16 +01:00
|
|
|
void Script::load(int script_nr, ResourceManager *resMan, ScriptPatcher *scriptPatcher) {
|
2010-06-27 20:38:43 +00:00
|
|
|
freeScript();
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
Resource *script = resMan->findResource(ResourceId(kResourceTypeScript, script_nr), false);
|
2010-09-04 15:38:24 +00:00
|
|
|
if (!script)
|
2011-03-14 20:00:26 +02:00
|
|
|
error("Script %d not found", script_nr);
|
2010-09-04 15:38:24 +00:00
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
_nr = script_nr;
|
2016-12-31 20:39:57 -06:00
|
|
|
uint32 scriptSize = script->size();
|
|
|
|
uint32 bufSize = scriptSize;
|
2010-06-27 20:38:43 +00:00
|
|
|
|
|
|
|
if (getSciVersion() == SCI_VERSION_0_EARLY) {
|
2016-12-31 20:39:57 -06:00
|
|
|
bufSize += script->getUint16LEAt(0) * 2;
|
2015-12-29 01:44:11 +01:00
|
|
|
} else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2010-11-17 08:30:08 +00:00
|
|
|
// In SCI1.1 - SCI2.1, the heap was in a separate space from the script. We append
|
2010-06-28 11:22:20 +00:00
|
|
|
// it to the end of the script, and adjust addressing accordingly.
|
|
|
|
// However, since we address the heap with a 16-bit pointer, the
|
|
|
|
// combined size of the stack and the heap must be 64KB. So far this has
|
|
|
|
// worked for SCI11, SCI2 and SCI21 games. SCI3 games use a different
|
|
|
|
// script format, and theoretically they can exceed the 64KB boundary
|
|
|
|
// using relocation.
|
2016-12-31 20:39:57 -06:00
|
|
|
Resource *heap = resMan->findResource(ResourceId(kResourceTypeHeap, script_nr), false);
|
|
|
|
bufSize += heap->size();
|
2010-06-27 20:38:43 +00:00
|
|
|
|
|
|
|
// Ensure that the start of the heap resource can be word-aligned.
|
2016-12-31 20:39:57 -06:00
|
|
|
if (script->size() & 2) {
|
|
|
|
++bufSize;
|
|
|
|
++scriptSize;
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// As mentioned above, the script and the heap together should not exceed 64KB
|
2016-12-31 20:39:57 -06:00
|
|
|
if (script->size() + heap->size() > 65535)
|
2010-06-27 20:38:43 +00:00
|
|
|
error("Script and heap sizes combined exceed 64K. This means a fundamental "
|
2010-06-28 11:22:20 +00:00
|
|
|
"design bug was made regarding SCI1.1 and newer games.\n"
|
|
|
|
"Please report this error to the ScummVM team");
|
2010-11-17 20:46:59 +00:00
|
|
|
} else if (getSciVersion() == SCI_VERSION_3) {
|
|
|
|
// Check for scripts over 64KB. These won't work with the current 16-bit address
|
|
|
|
// scheme. We need an overlaying mechanism, or a mechanism to split script parts
|
|
|
|
// in different segments to handle these. For now, simply stop when such a script
|
|
|
|
// is found.
|
2012-06-14 12:14:04 +03:00
|
|
|
//
|
|
|
|
// Known large SCI 3 scripts are:
|
|
|
|
// Lighthouse: 9, 220, 270, 351, 360, 490, 760, 765, 800
|
|
|
|
// LSL7: 240, 511, 550
|
|
|
|
// Phantasmagoria 2: none (hooray!)
|
|
|
|
// RAMA: 70
|
|
|
|
//
|
2010-11-17 20:46:59 +00:00
|
|
|
// TODO: Remove this once such a mechanism is in place
|
2016-12-31 20:39:57 -06:00
|
|
|
if (script->size() > 65535)
|
2017-03-30 22:00:27 +02:00
|
|
|
warning("TODO: SCI script %d is over 64KB - it's %u bytes long. This can't "
|
2016-12-31 20:39:57 -06:00
|
|
|
"be fully handled at the moment", script_nr, script->size());
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 08:25:35 +02:00
|
|
|
uint extraLocalsWorkaround = 0;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (g_sci->getGameId() == GID_FANMADE && _nr == 1 && script->size() == 11140) {
|
2011-03-02 08:25:35 +02:00
|
|
|
// WORKAROUND: Script 1 in Ocean Battle doesn't have enough locals to
|
|
|
|
// fit the string showing how many shots are left (a nasty script bug,
|
|
|
|
// corrupting heap memory). We add 10 more locals so that it has enough
|
|
|
|
// space to use as the target for its kFormat operation. Fixes bug
|
|
|
|
// #3059871.
|
|
|
|
extraLocalsWorkaround = 10;
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
bufSize += extraLocalsWorkaround * 2;
|
2011-03-02 08:25:35 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<byte> outBuffer = _buf->allocate(bufSize, script->name() + " buffer");
|
|
|
|
script->copyDataTo(outBuffer);
|
|
|
|
// The word-aligned script size is used here because other parts of the code
|
|
|
|
// currently rely on finding the start of the heap by reading the script
|
|
|
|
// size
|
|
|
|
_script = _buf->subspan(0, scriptSize, script->name());
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2015-12-29 01:44:11 +01:00
|
|
|
if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2016-12-31 20:39:57 -06:00
|
|
|
Resource *heap = resMan->findResource(ResourceId(kResourceTypeHeap, _nr), false);
|
|
|
|
assert(heap);
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<byte> outHeap = outBuffer.subspan(scriptSize, heap->size(), heap->name(), 0);
|
|
|
|
heap->copyDataTo(outHeap);
|
|
|
|
_heap = outHeap;
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:26:23 +02:00
|
|
|
// Check scripts (+ possibly SCI 1.1 heap) for matching signatures and patch those, if found
|
2016-12-31 20:39:57 -06:00
|
|
|
scriptPatcher->processScript(_nr, outBuffer);
|
2015-04-16 01:26:23 +02:00
|
|
|
|
2010-11-17 08:53:02 +00:00
|
|
|
if (getSciVersion() <= SCI_VERSION_1_LATE) {
|
2017-04-18 11:16:34 -05:00
|
|
|
// Some buggy game scripts contain two export tables (e.g. script 912
|
|
|
|
// in Camelot and script 306 in KQ4); in these scripts, the first table
|
|
|
|
// is broken, so we ignore it and use the last one instead
|
|
|
|
// Fixes bugs #3039785 and #3037595.
|
|
|
|
SciSpan<const uint16> exportTable = findBlockSCI0(SCI_OBJ_EXPORTS, true).subspan<const uint16>(0);
|
2016-12-31 20:39:57 -06:00
|
|
|
if (exportTable) {
|
|
|
|
// The export table is after the block header (4 bytes / 2 uint16s)
|
|
|
|
// and the number of exports (2 bytes / 1 uint16).
|
|
|
|
// The exports span does not need to be explicitly sized since the
|
|
|
|
// maximum size was already determined by findBlockSCI0
|
|
|
|
_exports = exportTable.subspan(3);
|
|
|
|
_numExports = exportTable.getUint16SEAt(2);
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
|
|
|
|
SciSpan<const byte> synonymTable = findBlockSCI0(SCI_OBJ_SYNONYMS);
|
|
|
|
if (synonymTable) {
|
|
|
|
// the synonyms table is after the block header (4 bytes),
|
|
|
|
// and each synonym entry is 4 bytes
|
|
|
|
_synonyms = synonymTable.subspan(4);
|
|
|
|
_numSynonyms = _synonyms.size() / 4;
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
|
|
|
|
SciSpan<const byte> localsTable = findBlockSCI0(SCI_OBJ_LOCALVARS);
|
|
|
|
if (localsTable) {
|
|
|
|
// skip header (4 bytes)
|
|
|
|
_localsOffset = localsTable - *_buf + 4;
|
2017-02-23 11:25:59 -06:00
|
|
|
_localsCount = localsTable.size() / 2 - 2;
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
2015-12-29 01:44:11 +01:00
|
|
|
} else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2016-12-31 20:39:57 -06:00
|
|
|
_numExports = _buf->getUint16SEAt(kSci11NumExportsOffset);
|
2016-10-13 10:11:24 -05:00
|
|
|
if (_numExports) {
|
2016-12-31 20:39:57 -06:00
|
|
|
_exports = _buf->subspan<const uint16>(kSci11ExportTableOffset, _numExports * sizeof(uint16));
|
2010-11-17 08:53:02 +00:00
|
|
|
}
|
2016-10-13 10:11:24 -05:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
_localsOffset = _script.size() + 4;
|
|
|
|
_localsCount = _buf->getUint16SEAt(_localsOffset - 2);
|
2010-11-17 08:53:02 +00:00
|
|
|
} else if (getSciVersion() == SCI_VERSION_3) {
|
2016-12-31 20:39:57 -06:00
|
|
|
_localsCount = _buf->getUint16LEAt(12);
|
|
|
|
_numExports = _buf->getUint16LEAt(20);
|
|
|
|
if (_numExports) {
|
|
|
|
_exports = _buf->subspan<const uint16>(22, _numExports * sizeof(uint16));
|
|
|
|
}
|
2017-02-24 19:47:30 -06:00
|
|
|
|
|
|
|
// SCI3 local variables always start dword-aligned
|
|
|
|
if (_numExports % 2)
|
|
|
|
_localsOffset = 22 + _numExports * sizeof(uint16);
|
|
|
|
else
|
|
|
|
_localsOffset = 24 + _numExports * sizeof(uint16);
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 08:25:35 +02:00
|
|
|
// WORKAROUND: Increase locals, if needed (check above)
|
|
|
|
_localsCount += extraLocalsWorkaround;
|
|
|
|
|
2010-11-17 08:53:02 +00:00
|
|
|
if (getSciVersion() == SCI_VERSION_0_EARLY) {
|
|
|
|
// SCI0 early
|
|
|
|
// Old script block. There won't be a localvar block in this case.
|
|
|
|
// Instead, the script starts with a 16 bit int specifying the
|
|
|
|
// number of locals we need; these are then allocated and zeroed.
|
2016-12-31 20:39:57 -06:00
|
|
|
_localsCount = _buf->getUint16LEAt(0);
|
2010-11-17 08:53:02 +00:00
|
|
|
_localsOffset = -_localsCount * 2; // Make sure it's invalid
|
|
|
|
} else {
|
|
|
|
// SCI0 late and newer
|
2010-06-27 20:38:43 +00:00
|
|
|
// Does the script actually have locals? If not, set the locals offset to 0
|
|
|
|
if (!_localsCount)
|
|
|
|
_localsOffset = 0;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (_localsOffset + _localsCount * 2 + 1 >= (int)_buf->size()) {
|
|
|
|
error("Locals extend beyond end of script: offset %04x, count %d vs size %d", _localsOffset, _localsCount, (int)_buf->size());
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
|
|
|
|
// find all strings of this script
|
2015-05-14 20:33:21 +02:00
|
|
|
identifyOffsets();
|
2015-05-04 21:19:05 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
void Script::identifyOffsets() {
|
|
|
|
offsetLookupArrayEntry arrayEntry;
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> scriptDataPtr;
|
|
|
|
SciSpan<const byte> stringStartPtr;
|
|
|
|
SciSpan<const byte> stringDataPtr;
|
2015-05-04 21:19:05 +02:00
|
|
|
byte stringDataByte = 0;
|
2015-05-14 20:33:21 +02:00
|
|
|
uint16 typeObject_id = 0;
|
|
|
|
uint16 typeString_id = 0;
|
|
|
|
uint16 typeSaid_id = 0;
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-05-14 20:58:14 +02:00
|
|
|
uint16 blockType = 0;
|
|
|
|
uint16 blockSize = 0;
|
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
_offsetLookupArray.clear();
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupObjectCount = 0;
|
|
|
|
_offsetLookupStringCount = 0;
|
|
|
|
_offsetLookupSaidCount = 0;
|
2016-10-13 10:11:24 -05:00
|
|
|
_codeOffset = 0;
|
2015-05-17 18:51:22 +02:00
|
|
|
|
2015-05-04 21:19:05 +02:00
|
|
|
if (getSciVersion() < SCI_VERSION_1_1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// SCI0 + SCI1
|
2016-12-31 20:39:57 -06:00
|
|
|
scriptDataPtr = *_buf;
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
// Go through all blocks
|
2015-05-04 21:19:05 +02:00
|
|
|
if (getSciVersion() == SCI_VERSION_0_EARLY) {
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script %d", _nr);
|
2016-12-31 20:39:57 -06:00
|
|
|
scriptDataPtr += 2;
|
2015-05-04 21:19:05 +02:00
|
|
|
}
|
2015-05-14 20:33:21 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
blockType = scriptDataPtr.getUint16LEAt(0);
|
|
|
|
scriptDataPtr += 2;
|
2015-05-04 21:19:05 +02:00
|
|
|
if (blockType == 0) // end of blocks detected
|
|
|
|
break;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
blockSize = scriptDataPtr.getUint16LEAt(0);
|
2015-05-04 21:19:05 +02:00
|
|
|
if (blockSize < 4)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): invalid block size in script %d", _nr);
|
2016-12-31 20:39:57 -06:00
|
|
|
blockSize -= 4; // block size includes block-type UINT16 and block-size UINT16
|
|
|
|
scriptDataPtr += 2;
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < blockSize)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): invalid block size in script %d", _nr);
|
|
|
|
|
|
|
|
switch (blockType) {
|
|
|
|
case SCI_OBJ_OBJECT:
|
|
|
|
case SCI_OBJ_CLASS:
|
|
|
|
typeObject_id++;
|
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_OBJECT;
|
|
|
|
arrayEntry.id = typeObject_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = scriptDataPtr - *_buf + 8; // Calculate offset inside script data (VM uses +8)
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.stringSize = 0;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupObjectCount++;
|
2015-05-14 20:33:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCI_OBJ_STRINGS:
|
2015-05-04 21:19:05 +02:00
|
|
|
// string block detected, we now grab all NUL terminated strings out of this block
|
2016-12-31 20:39:57 -06:00
|
|
|
stringDataPtr = scriptDataPtr.subspan(0, blockSize);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_STRING;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (stringDataPtr.size() < 1) // no more bytes left
|
2015-05-04 21:19:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
stringStartPtr = stringDataPtr;
|
2015-05-14 20:33:21 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() == 1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// only 1 byte left and that byte is a [00], in that case we also exit
|
|
|
|
stringDataByte = *stringStartPtr;
|
|
|
|
if (stringDataByte == 0x00)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:19:05 +02:00
|
|
|
// now look for terminating [NUL]
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
2015-05-04 21:50:42 +02:00
|
|
|
stringDataByte = *stringDataPtr;
|
2015-05-04 21:19:05 +02:00
|
|
|
stringDataPtr++;
|
|
|
|
if (!stringDataByte) // NUL found, exit this loop
|
|
|
|
break;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() < 1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// no more bytes left
|
|
|
|
warning("Script::identifyOffsets(): string without terminating NUL in script %d", _nr);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-14 20:33:21 +02:00
|
|
|
|
|
|
|
if (stringDataByte)
|
|
|
|
break;
|
|
|
|
|
|
|
|
typeString_id++;
|
|
|
|
arrayEntry.id = typeString_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.stringSize = stringDataPtr - stringStartPtr;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupStringCount++;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-14 20:33:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCI_OBJ_SAID:
|
|
|
|
// said block detected, we now try to find every single said "string" inside this block
|
|
|
|
// said strings are terminated with a 0xFF, the string itself may contain words (2 bytes), where
|
|
|
|
// the second byte of a word may also be a 0xFF.
|
2016-12-31 20:39:57 -06:00
|
|
|
stringDataPtr = scriptDataPtr.subspan(0, blockSize);
|
2015-05-14 20:33:21 +02:00
|
|
|
|
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_SAID;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (stringDataPtr.size() < 1) // no more bytes left
|
2015-05-14 20:33:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
stringStartPtr = stringDataPtr;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() == 1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// only 1 byte left and that byte is a [00], in that case we also exit
|
|
|
|
// happens in some scripts, for example Conquests of Camelot, script 997
|
|
|
|
// may have been a bug in the compiler or just an intentional filler byte
|
|
|
|
stringDataByte = *stringStartPtr;
|
|
|
|
if (stringDataByte == 0x00)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now look for terminating 0xFF
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
2015-05-14 20:33:21 +02:00
|
|
|
stringDataByte = *stringDataPtr;
|
|
|
|
stringDataPtr++;
|
|
|
|
if (stringDataByte == 0xFF) // Terminator found, exit this loop
|
|
|
|
break;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() < 1) // no more bytes left
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): said-string without terminator in script %d", _nr);
|
|
|
|
if (stringDataByte < 0xF0) {
|
|
|
|
// Part of a word, skip second byte
|
|
|
|
stringDataPtr++;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() < 1) // no more bytes left
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): said-string without terminator in script %d", _nr);
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
typeSaid_id++;
|
|
|
|
arrayEntry.id = typeSaid_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.stringSize = 0;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupSaidCount++;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-14 20:33:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-05-04 21:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scriptDataPtr += blockSize;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-12-29 01:44:11 +01:00
|
|
|
} else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// Strings in SCI1.1 up to SCI2 come after the object instances
|
2016-12-31 20:39:57 -06:00
|
|
|
scriptDataPtr = _heap;
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-10-13 10:11:24 -05:00
|
|
|
enum {
|
2016-12-31 20:39:57 -06:00
|
|
|
kExportSize = sizeof(uint16),
|
|
|
|
kPropertySize = sizeof(uint16),
|
|
|
|
kNumMethodsSize = sizeof(uint16),
|
2016-10-13 10:11:24 -05:00
|
|
|
kPropDictEntrySize = 2,
|
|
|
|
kMethDictEntrySize = 4
|
|
|
|
};
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> hunkPtr = _buf->subspan(kSci11ExportTableOffset + _numExports * kExportSize);
|
2016-10-13 10:11:24 -05:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < 4)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script in script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
uint16 endOfStringOffset = scriptDataPtr.getUint16SEAt(0);
|
|
|
|
uint16 objectStartOffset = scriptDataPtr.getUint16SEAt(2) * 2 + 4;
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < objectStartOffset)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): object start is beyond heap size in script %d", _nr);
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < endOfStringOffset)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): end of string is beyond heap size in script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> endOfStringPtr = scriptDataPtr.subspan(endOfStringOffset);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
|
|
|
scriptDataPtr += objectStartOffset;
|
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
// go through all objects
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
blockType = scriptDataPtr.getUint16SEAt(0);
|
2015-05-04 21:19:05 +02:00
|
|
|
scriptDataPtr += 2;
|
|
|
|
if (blockType != SCRIPT_OBJECT_MAGIC_NUMBER)
|
|
|
|
break;
|
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
// Object found, add offset of object
|
|
|
|
typeObject_id++;
|
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_OBJECT;
|
|
|
|
arrayEntry.id = typeObject_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = scriptDataPtr - *_buf - 2; // the VM uses a pointer to the Magic-Number
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.stringSize = 0;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupObjectCount++;
|
2015-05-14 20:33:21 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script in script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
const uint16 numProperties = scriptDataPtr.getUint16SEAt(0);
|
2016-10-13 10:11:24 -05:00
|
|
|
blockSize = numProperties * kPropertySize;
|
2015-05-14 20:58:14 +02:00
|
|
|
if (blockSize < 4)
|
|
|
|
error("Script::identifyOffsets(): invalid block size in script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
scriptDataPtr += 2;
|
2016-10-13 10:11:24 -05:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
const uint16 scriptNum = scriptDataPtr.getUint16SEAt(6);
|
2016-10-13 10:11:24 -05:00
|
|
|
|
|
|
|
if (scriptNum != 0xFFFF) {
|
|
|
|
hunkPtr += numProperties * kPropDictEntrySize;
|
|
|
|
}
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
const uint16 numMethods = hunkPtr.getUint16SEAt(0);
|
2016-10-13 10:11:24 -05:00
|
|
|
hunkPtr += kNumMethodsSize + numMethods * kMethDictEntrySize;
|
|
|
|
|
2015-05-04 21:19:05 +02:00
|
|
|
blockSize -= 4; // blocksize contains UINT16 type and UINT16 size
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < blockSize)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): invalid block size in script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
|
|
|
scriptDataPtr += blockSize;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
_codeOffset = hunkPtr - *_buf;
|
2016-10-13 10:11:24 -05:00
|
|
|
|
2015-05-04 21:19:05 +02:00
|
|
|
// now scriptDataPtr points to right at the start of the strings
|
|
|
|
if (scriptDataPtr > endOfStringPtr)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): string block / end-of-string block mismatch in script %d", _nr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
stringDataPtr = scriptDataPtr.subspan(0, endOfStringPtr - scriptDataPtr);
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_STRING;
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (stringDataPtr.size() < 1) // no more bytes left
|
2015-05-04 21:19:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
stringStartPtr = stringDataPtr;
|
|
|
|
// now look for terminating [NUL]
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
2015-05-04 21:50:42 +02:00
|
|
|
stringDataByte = *stringDataPtr;
|
2015-05-04 21:19:05 +02:00
|
|
|
stringDataPtr++;
|
|
|
|
if (!stringDataByte) // NUL found, exit this loop
|
|
|
|
break;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() < 1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// no more bytes left
|
|
|
|
warning("Script::identifyOffsets(): string without terminating NUL in script %d", _nr);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
|
2015-05-14 20:33:21 +02:00
|
|
|
if (stringDataByte)
|
|
|
|
break;
|
|
|
|
|
|
|
|
typeString_id++;
|
|
|
|
arrayEntry.id = typeString_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.stringSize = stringDataPtr - stringStartPtr;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupStringCount++;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
|
|
|
|
} else if (getSciVersion() == SCI_VERSION_3) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// SCI3
|
2015-05-15 23:46:25 +02:00
|
|
|
uint32 sci3StringOffset = 0;
|
|
|
|
uint32 sci3RelocationOffset = 0;
|
|
|
|
uint32 sci3BoundaryOffset = 0;
|
2015-05-14 20:33:21 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (_buf->size() < 22)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): script %d smaller than expected SCI3-header", _nr);
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
sci3StringOffset = _buf->getUint32LEAt(4);
|
|
|
|
sci3RelocationOffset = _buf->getUint32LEAt(8);
|
2015-05-14 20:33:21 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (sci3RelocationOffset > _buf->size())
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): relocation offset is beyond end of script %d", _nr);
|
|
|
|
|
2015-05-14 20:58:14 +02:00
|
|
|
// First we get all the objects
|
|
|
|
scriptDataPtr = getSci3ObjectsPointer();
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:58:14 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script %d", _nr);
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
blockType = scriptDataPtr.getUint16SEAt(0);
|
|
|
|
scriptDataPtr += 2;
|
2015-05-14 20:58:14 +02:00
|
|
|
if (blockType != SCRIPT_OBJECT_MAGIC_NUMBER)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Object found, add offset of object
|
|
|
|
typeObject_id++;
|
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_OBJECT;
|
|
|
|
arrayEntry.id = typeObject_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = scriptDataPtr - *_buf - 2; // the VM uses a pointer to the Magic-Number
|
2015-05-14 20:58:14 +02:00
|
|
|
arrayEntry.stringSize = 0;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupObjectCount++;
|
2015-05-14 20:58:14 +02:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < 2)
|
2015-05-14 20:58:14 +02:00
|
|
|
error("Script::identifyOffsets(): unexpected end of script in script %d", _nr);
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
blockSize = scriptDataPtr.getUint16SEAt(0);
|
2015-05-14 20:58:14 +02:00
|
|
|
if (blockSize < 4)
|
|
|
|
error("Script::identifyOffsets(): invalid block size in script %d", _nr);
|
|
|
|
scriptDataPtr += 2;
|
|
|
|
blockSize -= 4; // blocksize contains UINT16 type and UINT16 size
|
2016-12-31 20:39:57 -06:00
|
|
|
if (scriptDataPtr.size() < blockSize)
|
2015-05-14 20:58:14 +02:00
|
|
|
error("Script::identifyOffsets(): invalid block size in script %d", _nr);
|
|
|
|
|
|
|
|
scriptDataPtr += blockSize;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-14 20:58:14 +02:00
|
|
|
|
|
|
|
// And now we get all the strings
|
2015-05-15 23:46:25 +02:00
|
|
|
if (sci3StringOffset > 0) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// string offset set, we expect strings
|
2016-12-31 20:39:57 -06:00
|
|
|
if (sci3StringOffset > _buf->size())
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): string offset is beyond end of script %d", _nr);
|
|
|
|
|
2015-05-15 23:46:25 +02:00
|
|
|
if (sci3RelocationOffset < sci3StringOffset)
|
2015-05-14 20:33:21 +02:00
|
|
|
error("Script::identifyOffsets(): string offset points beyond relocation offset in script %d", _nr);
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
stringDataPtr = _buf->subspan(sci3StringOffset, sci3RelocationOffset - sci3StringOffset);
|
2015-05-14 20:33:21 +02:00
|
|
|
|
|
|
|
arrayEntry.type = SCI_SCR_OFFSET_TYPE_STRING;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
if (stringDataPtr.size() < 1) // no more bytes left
|
2015-05-14 20:33:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
stringStartPtr = stringDataPtr;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() == 1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// only 1 byte left and that byte is a [00], in that case we also exit
|
|
|
|
stringDataByte = *stringStartPtr;
|
|
|
|
if (stringDataByte == 0x00)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now look for terminating [NUL]
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
2015-05-14 20:33:21 +02:00
|
|
|
stringDataByte = *stringDataPtr;
|
|
|
|
stringDataPtr++;
|
|
|
|
if (!stringDataByte) // NUL found, exit this loop
|
|
|
|
break;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() < 1) {
|
2015-05-14 20:33:21 +02:00
|
|
|
// no more bytes left
|
|
|
|
warning("Script::identifyOffsets(): string without terminating NUL in script %d", _nr);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-14 20:33:21 +02:00
|
|
|
|
|
|
|
if (stringDataByte)
|
|
|
|
break;
|
|
|
|
|
|
|
|
typeString_id++;
|
|
|
|
arrayEntry.id = typeString_id;
|
2016-12-31 20:39:57 -06:00
|
|
|
arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data
|
2015-05-14 20:33:21 +02:00
|
|
|
arrayEntry.stringSize = stringDataPtr - stringStartPtr;
|
|
|
|
_offsetLookupArray.push_back(arrayEntry);
|
2015-05-17 18:51:22 +02:00
|
|
|
_offsetLookupStringCount++;
|
2015-05-15 23:46:25 +02:00
|
|
|
|
|
|
|
// SCI3 seems to have aligned all string on DWORD boundaries
|
2016-12-31 20:39:57 -06:00
|
|
|
sci3BoundaryOffset = stringDataPtr - *_buf; // Calculate current offset inside script data
|
2015-05-15 23:46:25 +02:00
|
|
|
sci3BoundaryOffset = sci3BoundaryOffset & 3; // Check boundary offset
|
|
|
|
if (sci3BoundaryOffset) {
|
|
|
|
// lower 2 bits are set? Then we have to adjust the offset
|
|
|
|
sci3BoundaryOffset = 4 - sci3BoundaryOffset;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (stringDataPtr.size() < sci3BoundaryOffset)
|
2015-05-15 23:46:25 +02:00
|
|
|
error("Script::identifyOffsets(): SCI3 string boundary adjustment goes beyond end of string block in script %d", _nr);
|
|
|
|
stringDataPtr += sci3BoundaryOffset;
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2015-05-14 20:33:21 +02:00
|
|
|
}
|
2015-05-04 21:19:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> Script::getSci3ObjectsPointer() {
|
|
|
|
SciSpan<const byte> ptr;
|
2010-11-17 09:28:04 +00:00
|
|
|
|
|
|
|
// SCI3 local variables always start dword-aligned
|
|
|
|
if (_numExports % 2)
|
2016-12-31 20:39:57 -06:00
|
|
|
ptr = _buf->subspan(22 + _numExports * sizeof(uint16));
|
2010-11-17 09:28:04 +00:00
|
|
|
else
|
2016-12-31 20:39:57 -06:00
|
|
|
ptr = _buf->subspan(24 + _numExports * sizeof(uint16));
|
2010-11-17 09:28:04 +00:00
|
|
|
|
|
|
|
// SCI3 object structures always start dword-aligned
|
|
|
|
if (_localsCount % 2)
|
2016-12-31 20:39:57 -06:00
|
|
|
ptr += 2 + _localsCount * sizeof(uint16);
|
2010-11-17 09:28:04 +00:00
|
|
|
else
|
2016-12-31 20:39:57 -06:00
|
|
|
ptr += _localsCount * sizeof(uint16);
|
2010-11-17 09:28:04 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:47:22 +03:00
|
|
|
Object *Script::getObject(uint16 offset) {
|
|
|
|
if (_objects.contains(offset))
|
|
|
|
return &_objects[offset];
|
2010-06-27 20:38:43 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:47:22 +03:00
|
|
|
const Object *Script::getObject(uint16 offset) const {
|
|
|
|
if (_objects.contains(offset))
|
|
|
|
return &_objects[offset];
|
2010-06-27 20:38:43 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object *Script::scriptObjInit(reg_t obj_pos, bool fullObjectInit) {
|
|
|
|
if (getSciVersion() < SCI_VERSION_1_1 && fullObjectInit)
|
2012-06-18 05:21:59 +03:00
|
|
|
obj_pos.incOffset(8); // magic offset (SCRIPT_OBJECT_MAGIC_OFFSET)
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (obj_pos.getOffset() >= _buf->size())
|
2011-03-14 20:00:26 +02:00
|
|
|
error("Attempt to initialize object beyond end of script");
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2010-06-28 12:27:48 +00:00
|
|
|
// Get the object at the specified position and init it. This will
|
|
|
|
// automatically "allocate" space for it in the _objects map if necessary.
|
2012-06-18 05:21:59 +03:00
|
|
|
Object *obj = &_objects[obj_pos.getOffset()];
|
2016-12-31 20:39:57 -06:00
|
|
|
obj->init(*_buf, obj_pos, fullObjectInit);
|
2010-06-27 20:38:43 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function is used by Script::relocateLocal and Object::relocate
|
|
|
|
// Duplicate in segment.cpp and script.cpp
|
|
|
|
static bool relocateBlock(Common::Array<reg_t> &block, int block_location, SegmentId segment, int location, size_t scriptSize) {
|
|
|
|
int rel = location - block_location;
|
|
|
|
|
|
|
|
if (rel < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint idx = rel >> 1;
|
|
|
|
|
|
|
|
if (idx >= block.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (rel & 1) {
|
|
|
|
error("Attempt to relocate odd variable #%d.5e (relative to %04x)\n", idx, block_location);
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-18 05:21:59 +03:00
|
|
|
block[idx].setSegment(segment); // Perform relocation
|
2015-12-29 01:44:11 +01:00
|
|
|
if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE)
|
2012-06-18 05:21:59 +03:00
|
|
|
block[idx].incOffset(scriptSize);
|
2010-06-27 20:38:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:57:34 -05:00
|
|
|
int Script::relocateOffsetSci3(uint32 offset) const {
|
2016-12-31 20:39:57 -06:00
|
|
|
int relocStart = _buf->getUint32LEAt(8);
|
|
|
|
int relocCount = _buf->getUint16LEAt(18);
|
2017-02-17 00:17:11 -06:00
|
|
|
SciSpan<const byte> seeker = _buf->subspan(relocStart);
|
2010-11-17 11:15:52 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < relocCount; ++i) {
|
2016-12-31 20:39:57 -06:00
|
|
|
if (seeker.getUint32SEAt(0) == offset) {
|
|
|
|
return _buf->getUint16SEAt(offset) + seeker.getUint32SEAt(4);
|
2010-11-17 11:15:52 +00:00
|
|
|
}
|
|
|
|
seeker += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
bool Script::relocateLocal(SegmentId segment, int location) {
|
|
|
|
if (_localsBlock)
|
2016-12-31 20:39:57 -06:00
|
|
|
return relocateBlock(_localsBlock->_locals, _localsOffset, segment, location, _script.size());
|
2010-06-27 20:38:43 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-17 11:15:52 +00:00
|
|
|
void Script::relocateSci0Sci21(reg_t block) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> heap = *_buf;
|
2010-06-27 20:38:43 +00:00
|
|
|
uint16 heapOffset = 0;
|
|
|
|
|
2015-12-29 01:44:11 +01:00
|
|
|
if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2016-12-31 20:39:57 -06:00
|
|
|
heap = _heap;
|
|
|
|
heapOffset = _script.size();
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-08-30 14:53:58 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (block.getOffset() >= (uint16)heap.size() ||
|
|
|
|
heap.getUint16SEAt(block.getOffset()) * 2 + block.getOffset() >= (uint16)heap.size())
|
2011-03-14 20:00:26 +02:00
|
|
|
error("Relocation block outside of script");
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
int count = heap.getUint16SEAt(block.getOffset());
|
2010-06-27 20:38:43 +00:00
|
|
|
int exportIndex = 0;
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
2016-12-31 20:39:57 -06:00
|
|
|
pos = heap.getUint16SEAt(block.getOffset() + 2 + (exportIndex * 2)) + heapOffset;
|
2010-06-28 11:21:39 +00:00
|
|
|
// This occurs in SCI01/SCI1 games where usually one export value is
|
|
|
|
// zero. It seems that in this situation, we should skip the export and
|
|
|
|
// move to the next one, though the total count of valid exports remains
|
|
|
|
// the same
|
2010-06-27 20:38:43 +00:00
|
|
|
if (!pos) {
|
|
|
|
exportIndex++;
|
2016-12-31 20:39:57 -06:00
|
|
|
pos = heap.getUint16SEAt(block.getOffset() + 2 + (exportIndex * 2)) + heapOffset;
|
2010-06-27 20:38:43 +00:00
|
|
|
if (!pos)
|
|
|
|
error("Script::relocate(): Consecutive zero exports found");
|
|
|
|
}
|
|
|
|
|
2010-06-28 11:21:39 +00:00
|
|
|
// In SCI0-SCI1, script local variables, objects and code are relocated.
|
|
|
|
// We only relocate locals and objects here, and ignore relocation of
|
|
|
|
// code blocks. In SCI1.1 and newer versions, only locals and objects
|
|
|
|
// are relocated.
|
2012-06-18 05:21:59 +03:00
|
|
|
if (!relocateLocal(block.getSegment(), pos)) {
|
2010-09-04 17:16:41 +00:00
|
|
|
// Not a local? It's probably an object or code block. If it's an
|
|
|
|
// object, relocate it.
|
2010-06-27 20:38:43 +00:00
|
|
|
const ObjMap::iterator end = _objects.end();
|
|
|
|
for (ObjMap::iterator it = _objects.begin(); it != end; ++it)
|
2016-12-31 20:39:57 -06:00
|
|
|
if (it->_value.relocateSci0Sci21(block.getSegment(), pos, _script.size()))
|
2010-06-27 20:38:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
exportIndex++;
|
2009-08-26 22:11:31 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2010-11-17 11:15:52 +00:00
|
|
|
void Script::relocateSci3(reg_t block) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> relocStart = _buf->subspan(_buf->getUint32SEAt(8));
|
2017-02-13 14:54:08 -06:00
|
|
|
const uint relocCount = _buf->getUint16SEAt(18);
|
2010-11-17 11:15:52 +00:00
|
|
|
|
|
|
|
ObjMap::iterator it;
|
|
|
|
for (it = _objects.begin(); it != _objects.end(); ++it) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> seeker = relocStart;
|
2017-02-13 14:54:08 -06:00
|
|
|
for (uint i = 0; i < relocCount; ++i) {
|
2012-06-18 05:21:59 +03:00
|
|
|
it->_value.relocateSci3(block.getSegment(),
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker.getUint32SEAt(0),
|
|
|
|
seeker.getUint32SEAt(4),
|
|
|
|
_script.size());
|
2010-11-17 11:15:52 +00:00
|
|
|
seeker += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
void Script::incrementLockers() {
|
2011-05-13 23:01:02 +02:00
|
|
|
assert(!_markedAsDeleted);
|
2010-06-27 20:38:43 +00:00
|
|
|
_lockers++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::decrementLockers() {
|
|
|
|
if (_lockers > 0)
|
|
|
|
_lockers--;
|
|
|
|
}
|
2010-02-03 01:34:15 +00:00
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
int Script::getLockers() const {
|
|
|
|
return _lockers;
|
|
|
|
}
|
2010-02-03 01:34:15 +00:00
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
void Script::setLockers(int lockers) {
|
2011-05-13 23:01:02 +02:00
|
|
|
assert(lockers == 0 || !_markedAsDeleted);
|
2010-06-27 20:38:43 +00:00
|
|
|
_lockers = lockers;
|
|
|
|
}
|
2010-02-03 01:34:15 +00:00
|
|
|
|
2012-06-26 11:12:37 +03:00
|
|
|
uint32 Script::validateExportFunc(int pubfunct, bool relocSci3) {
|
2010-06-27 20:38:43 +00:00
|
|
|
bool exportsAreWide = (g_sci->_features->detectLofsType() == SCI_VERSION_1_MIDDLE);
|
2010-02-03 01:34:15 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (_numExports <= (uint)pubfunct) {
|
2010-06-27 20:38:43 +00:00
|
|
|
error("validateExportFunc(): pubfunct is invalid");
|
|
|
|
return 0;
|
2010-02-03 01:34:15 +00:00
|
|
|
}
|
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
if (exportsAreWide)
|
|
|
|
pubfunct *= 2;
|
2010-11-18 17:04:00 +00:00
|
|
|
|
2012-06-23 21:38:46 +03:00
|
|
|
uint32 offset;
|
2010-11-18 17:04:00 +00:00
|
|
|
|
2012-06-23 21:20:43 +03:00
|
|
|
if (getSciVersion() != SCI_VERSION_3) {
|
2016-12-31 20:39:57 -06:00
|
|
|
offset = _exports.getUint16SEAt(pubfunct);
|
2010-11-18 17:04:00 +00:00
|
|
|
} else {
|
2012-06-26 11:12:37 +03:00
|
|
|
if (!relocSci3)
|
2016-12-31 20:39:57 -06:00
|
|
|
offset = _exports.getUint16SEAt(pubfunct) + getCodeBlockOffsetSci3();
|
2012-06-23 21:20:43 +03:00
|
|
|
else
|
|
|
|
offset = relocateOffsetSci3(pubfunct * 2 + 22);
|
2010-11-18 17:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 10:11:24 -05:00
|
|
|
// TODO: Check if this should be done for SCI1.1 games as well
|
|
|
|
if (getSciVersion() >= SCI_VERSION_2 && offset == 0) {
|
|
|
|
offset = _codeOffset;
|
|
|
|
}
|
2012-06-23 21:20:43 +03:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
if (offset >= _buf->size())
|
2012-06-23 21:20:43 +03:00
|
|
|
error("Invalid export function pointer");
|
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
return offset;
|
2010-02-03 01:34:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 11:16:34 -05:00
|
|
|
SciSpan<const byte> Script::findBlockSCI0(ScriptObjectTypes type, bool findLastBlock) {
|
|
|
|
SciSpan<const byte> foundBlock;
|
|
|
|
|
2010-06-27 20:38:43 +00:00
|
|
|
bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY);
|
2010-02-03 01:34:15 +00:00
|
|
|
|
2017-04-18 11:16:34 -05:00
|
|
|
SciSpan<const byte> buf = *_buf;
|
2010-06-27 20:38:43 +00:00
|
|
|
if (oldScriptHeader)
|
|
|
|
buf += 2;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
|
|
|
const int blockType = buf.getUint16LEAt(0);
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2010-11-04 23:19:23 +00:00
|
|
|
if (blockType == 0)
|
2010-06-27 20:38:43 +00:00
|
|
|
break;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
// the size in the block header includes the size of the header itself
|
|
|
|
const int blockSize = buf.getUint16LEAt(2);
|
2010-11-04 23:19:23 +00:00
|
|
|
assert(blockSize > 0);
|
2016-12-31 20:39:57 -06:00
|
|
|
|
2017-04-18 11:16:34 -05:00
|
|
|
if (blockType == type) {
|
|
|
|
foundBlock = buf.subspan(0, blockSize, Common::String::format("%s, %s block", _buf->name().c_str(), sciObjectTypeNames[type]));;
|
|
|
|
|
|
|
|
if (!findLastBlock) {
|
|
|
|
break;
|
|
|
|
}
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
|
|
|
|
2010-11-04 23:19:23 +00:00
|
|
|
buf += blockSize;
|
2016-12-31 20:39:57 -06:00
|
|
|
}
|
2010-06-27 20:38:43 +00:00
|
|
|
|
2017-04-18 11:16:34 -05:00
|
|
|
return foundBlock;
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// memory operations
|
|
|
|
|
|
|
|
bool Script::isValidOffset(uint16 offset) const {
|
2016-12-31 20:39:57 -06:00
|
|
|
return offset < _buf->size();
|
2010-06-27 20:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SegmentRef Script::dereference(reg_t pointer) {
|
2016-12-31 20:39:57 -06:00
|
|
|
if (pointer.getOffset() > _buf->size()) {
|
2017-03-30 14:02:27 -05:00
|
|
|
error("Script::dereference(): Attempt to dereference invalid pointer %04x:%04x into script segment (script size=%u)",
|
2016-12-31 20:39:57 -06:00
|
|
|
PRINT_REG(pointer), _buf->size());
|
2010-06-27 20:38:43 +00:00
|
|
|
return SegmentRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
SegmentRef ret;
|
|
|
|
ret.isRaw = true;
|
2016-12-31 20:39:57 -06:00
|
|
|
ret.maxSize = _buf->size() - pointer.getOffset();
|
|
|
|
ret.raw = const_cast<byte *>(_buf->getUnsafeDataAt(pointer.getOffset(), ret.maxSize));
|
2010-06-27 20:38:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-05 02:53:08 +02:00
|
|
|
LocalVariables *Script::allocLocalsSegment(SegManager *segMan) {
|
|
|
|
if (!getLocalsCount()) { // No locals
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
LocalVariables *locals;
|
|
|
|
|
|
|
|
if (_localsSegment) {
|
|
|
|
locals = (LocalVariables *)segMan->getSegment(_localsSegment, SEG_TYPE_LOCALS);
|
|
|
|
if (!locals || locals->getType() != SEG_TYPE_LOCALS || locals->script_id != getScriptNumber())
|
|
|
|
error("Invalid script locals segment while allocating locals");
|
|
|
|
} else
|
|
|
|
locals = (LocalVariables *)segMan->allocSegment(new LocalVariables(), &_localsSegment);
|
|
|
|
|
|
|
|
_localsBlock = locals;
|
|
|
|
locals->script_id = getScriptNumber();
|
|
|
|
locals->_locals.resize(getLocalsCount());
|
|
|
|
|
|
|
|
return locals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
void Script::initializeLocals(SegManager *segMan) {
|
2011-11-05 02:53:08 +02:00
|
|
|
LocalVariables *locals = allocLocalsSegment(segMan);
|
2010-02-03 01:34:15 +00:00
|
|
|
if (locals) {
|
2010-06-14 12:44:57 +00:00
|
|
|
if (getSciVersion() > SCI_VERSION_0_EARLY) {
|
2016-12-31 20:39:57 -06:00
|
|
|
const SciSpan<const byte> base = _buf->subspan(getLocalsOffset());
|
2010-06-14 12:44:57 +00:00
|
|
|
|
2010-06-27 21:00:34 +00:00
|
|
|
for (uint16 i = 0; i < getLocalsCount(); i++)
|
2016-12-31 20:39:57 -06:00
|
|
|
locals->_locals[i] = make_reg(0, base.getUint16SEAt(i * 2));
|
2010-06-14 12:44:57 +00:00
|
|
|
} else {
|
|
|
|
// In SCI0 early, locals are set at run time, thus zero them all here
|
2010-06-27 21:00:34 +00:00
|
|
|
for (uint16 i = 0; i < getLocalsCount(); i++)
|
2010-06-14 12:44:57 +00:00
|
|
|
locals->_locals[i] = NULL_REG;
|
|
|
|
}
|
2010-02-03 01:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-05 02:53:08 +02:00
|
|
|
void Script::syncLocalsBlock(SegManager *segMan) {
|
|
|
|
_localsBlock = (_localsSegment == 0) ? NULL : (LocalVariables *)(segMan->getSegment(_localsSegment, SEG_TYPE_LOCALS));
|
|
|
|
}
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
void Script::initializeClasses(SegManager *segMan) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> seeker;
|
2010-06-27 20:09:51 +00:00
|
|
|
uint16 mult = 0;
|
2010-11-17 08:53:02 +00:00
|
|
|
|
|
|
|
if (getSciVersion() <= SCI_VERSION_1_LATE) {
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker = _script;
|
2010-06-27 20:09:51 +00:00
|
|
|
mult = 1;
|
2016-12-31 20:39:57 -06:00
|
|
|
|
|
|
|
// SCI0 early has an extra two bytes of header
|
|
|
|
if (getSciVersion() == SCI_VERSION_0_EARLY) {
|
|
|
|
seeker += 2;
|
|
|
|
}
|
2015-12-29 01:44:11 +01:00
|
|
|
} else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker = _heap.subspan(4 + _heap.getUint16SEAt(2) * 2);
|
2010-11-17 08:53:02 +00:00
|
|
|
mult = 2;
|
|
|
|
} else if (getSciVersion() == SCI_VERSION_3) {
|
2010-11-17 09:28:04 +00:00
|
|
|
seeker = getSci3ObjectsPointer();
|
|
|
|
mult = 1;
|
2010-06-27 20:09:51 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2010-06-27 20:09:51 +00:00
|
|
|
if (!seeker)
|
|
|
|
return;
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2010-06-28 14:46:28 +00:00
|
|
|
uint16 marker;
|
2010-11-17 11:15:52 +00:00
|
|
|
bool isClass = false;
|
2012-06-15 11:53:19 +03:00
|
|
|
uint32 classpos;
|
2010-06-28 14:46:28 +00:00
|
|
|
int16 species = 0;
|
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
for (;;) {
|
2010-06-27 20:09:51 +00:00
|
|
|
// In SCI0-SCI1, this is the segment type. In SCI11, it's a marker (0x1234)
|
2016-12-31 20:39:57 -06:00
|
|
|
marker = seeker.getUint16SEAt(0);
|
|
|
|
classpos = seeker - *_buf;
|
2010-06-26 23:13:05 +00:00
|
|
|
|
2010-11-17 11:15:52 +00:00
|
|
|
if (getSciVersion() <= SCI_VERSION_1_LATE && !marker)
|
|
|
|
break;
|
|
|
|
|
2012-06-23 21:20:43 +03:00
|
|
|
if (getSciVersion() >= SCI_VERSION_1_1 && marker != SCRIPT_OBJECT_MAGIC_NUMBER)
|
2010-06-27 20:09:51 +00:00
|
|
|
break;
|
2010-06-14 19:37:19 +00:00
|
|
|
|
2010-11-17 08:53:02 +00:00
|
|
|
if (getSciVersion() <= SCI_VERSION_1_LATE) {
|
2010-06-27 20:09:51 +00:00
|
|
|
isClass = (marker == SCI_OBJ_CLASS);
|
2010-06-28 14:46:28 +00:00
|
|
|
if (isClass)
|
2016-12-31 20:39:57 -06:00
|
|
|
species = seeker.getUint16SEAt(12);
|
2010-06-27 20:09:51 +00:00
|
|
|
classpos += 12;
|
2015-12-29 01:44:11 +01:00
|
|
|
} else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) {
|
2016-12-31 20:39:57 -06:00
|
|
|
isClass = (seeker.getUint16SEAt(14) & kInfoFlagClass); // -info- selector
|
|
|
|
species = seeker.getUint16SEAt(10);
|
2010-11-17 08:53:02 +00:00
|
|
|
} else if (getSciVersion() == SCI_VERSION_3) {
|
2016-12-31 20:39:57 -06:00
|
|
|
isClass = (seeker.getUint16SEAt(10) & kInfoFlagClass);
|
|
|
|
species = seeker.getUint16SEAt(4);
|
2010-06-27 20:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isClass) {
|
2010-07-21 17:42:51 +00:00
|
|
|
// WORKAROUNDs for off-by-one script errors
|
2010-11-17 13:01:58 +00:00
|
|
|
if (species == (int)segMan->classTableSize()) {
|
|
|
|
if (g_sci->getGameId() == GID_LSL2 && g_sci->isDemo())
|
|
|
|
segMan->resizeClassTable(species + 1);
|
|
|
|
else if (g_sci->getGameId() == GID_LSL3 && !g_sci->isDemo() && _nr == 500)
|
|
|
|
segMan->resizeClassTable(species + 1);
|
|
|
|
else if (g_sci->getGameId() == GID_SQ3 && !g_sci->isDemo() && _nr == 93)
|
|
|
|
segMan->resizeClassTable(species + 1);
|
|
|
|
else if (g_sci->getGameId() == GID_SQ3 && !g_sci->isDemo() && _nr == 99)
|
|
|
|
segMan->resizeClassTable(species + 1);
|
|
|
|
}
|
2010-06-27 20:09:51 +00:00
|
|
|
|
2010-06-27 21:00:34 +00:00
|
|
|
if (species < 0 || species >= (int)segMan->classTableSize())
|
2010-07-21 17:42:51 +00:00
|
|
|
error("Invalid species %d(0x%x) unknown max %d(0x%x) while instantiating script %d\n",
|
|
|
|
species, species, segMan->classTableSize(), segMan->classTableSize(), _nr);
|
2010-06-26 23:13:05 +00:00
|
|
|
|
2010-06-27 21:00:34 +00:00
|
|
|
SegmentId segmentId = segMan->getScriptSegment(_nr);
|
|
|
|
segMan->setClassOffset(species, make_reg(segmentId, classpos));
|
2010-06-26 23:13:05 +00:00
|
|
|
}
|
2010-06-27 20:09:51 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker += seeker.getUint16SEAt(2) * mult;
|
2010-06-26 23:13:05 +00:00
|
|
|
}
|
2010-06-27 20:09:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
void Script::initializeObjectsSci0(SegManager *segMan, SegmentId segmentId) {
|
2010-06-27 20:09:51 +00:00
|
|
|
bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY);
|
|
|
|
|
2010-11-27 18:08:47 +00:00
|
|
|
// We need to make two passes, as the objects in the script might be in the
|
|
|
|
// wrong order (e.g. in the demo of Iceman) - refer to bug #3034713
|
|
|
|
for (int pass = 1; pass <= 2; pass++) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> seeker = _buf->subspan(oldScriptHeader ? 2 : 0);
|
2010-11-27 18:08:47 +00:00
|
|
|
|
|
|
|
do {
|
2016-12-31 20:39:57 -06:00
|
|
|
uint16 objType = seeker.getUint16SEAt(0);
|
2010-11-27 18:08:47 +00:00
|
|
|
if (!objType)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (objType) {
|
|
|
|
case SCI_OBJ_OBJECT:
|
|
|
|
case SCI_OBJ_CLASS:
|
|
|
|
{
|
2016-12-31 20:39:57 -06:00
|
|
|
reg_t addr = make_reg(segmentId, seeker - *_buf + 4);
|
2010-11-27 18:08:47 +00:00
|
|
|
Object *obj = scriptObjInit(addr);
|
|
|
|
obj->initSpecies(segMan, addr);
|
|
|
|
|
|
|
|
if (pass == 2) {
|
|
|
|
if (!obj->initBaseObject(segMan, addr)) {
|
2011-01-04 23:11:19 +00:00
|
|
|
if ((_nr == 202 || _nr == 764) && g_sci->getGameId() == GID_KQ5) {
|
|
|
|
// WORKAROUND: Script 202 of KQ5 French and German
|
|
|
|
// (perhaps Spanish too?) has an invalid object.
|
|
|
|
// This is non-fatal. Refer to bugs #3035396 and
|
|
|
|
// #3150767.
|
|
|
|
// Same happens with script 764, it seems to
|
|
|
|
// contain junk towards its end.
|
2011-04-20 23:37:53 +02:00
|
|
|
_objects.erase(addr.toUint16() - SCRIPT_OBJECT_MAGIC_OFFSET);
|
2011-01-04 23:11:19 +00:00
|
|
|
} else {
|
|
|
|
error("Failed to locate base object for object at %04X:%04X", PRINT_REG(addr));
|
|
|
|
}
|
2010-11-27 18:08:47 +00:00
|
|
|
}
|
2010-06-28 11:21:55 +00:00
|
|
|
}
|
2010-06-27 20:09:51 +00:00
|
|
|
}
|
2010-11-27 18:08:47 +00:00
|
|
|
break;
|
2010-06-27 20:09:51 +00:00
|
|
|
|
2010-11-27 18:08:47 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-06-27 20:09:51 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker += seeker.getUint16SEAt(2);
|
|
|
|
} while ((uint32)(seeker - *_buf) < getScriptSize() - 2);
|
2010-11-27 18:08:47 +00:00
|
|
|
}
|
2010-06-28 11:22:20 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
const SciSpan<const byte> relocationBlock = findBlockSCI0(SCI_OBJ_POINTERS);
|
2010-06-28 11:22:20 +00:00
|
|
|
if (relocationBlock)
|
2016-12-31 20:39:57 -06:00
|
|
|
relocateSci0Sci21(make_reg(segmentId, relocationBlock - *_buf + 4));
|
2010-06-27 20:09:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
void Script::initializeObjectsSci11(SegManager *segMan, SegmentId segmentId) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> seeker = _heap.subspan(4 + _heap.getUint16SEAt(2) * 2);
|
2010-06-26 23:13:05 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
while (seeker.getUint16SEAt(0) == SCRIPT_OBJECT_MAGIC_NUMBER) {
|
|
|
|
reg_t reg = make_reg(segmentId, seeker - *_buf);
|
2010-06-27 21:00:34 +00:00
|
|
|
Object *obj = scriptObjInit(reg);
|
2010-06-26 23:13:05 +00:00
|
|
|
|
|
|
|
// Copy base from species class, as we need its selector IDs
|
|
|
|
obj->setSuperClassSelector(
|
2012-06-18 05:21:59 +03:00
|
|
|
segMan->getClassAddress(obj->getSuperClassSelector().getOffset(), SCRIPT_GET_LOCK, 0));
|
2010-06-26 23:13:05 +00:00
|
|
|
|
2016-10-12 01:03:35 +02:00
|
|
|
// -propDict- is used by Obj::isMemberOf to determine if an object
|
|
|
|
// is an instance of a class. For classes, we therefore relocate
|
|
|
|
// -propDict- to the script's segment. For instances, we copy
|
|
|
|
// -propDict- from its class.
|
2010-09-04 17:16:41 +00:00
|
|
|
// Example test case - room 381 of sq4cd - if isMemberOf() doesn't work,
|
2016-10-12 01:03:35 +02:00
|
|
|
// talk-clicks on the robot will act like clicking on ego.
|
|
|
|
if (obj->isClass()) {
|
|
|
|
reg_t propDict = obj->getPropDictSelector();
|
|
|
|
propDict.setSegment(segmentId);
|
|
|
|
obj->setPropDictSelector(propDict);
|
|
|
|
} else {
|
2010-06-26 23:13:05 +00:00
|
|
|
reg_t classObject = obj->getSuperClassSelector();
|
2010-06-28 12:27:48 +00:00
|
|
|
const Object *classObj = segMan->getObject(classObject);
|
2010-06-26 23:13:05 +00:00
|
|
|
obj->setPropDictSelector(classObj->getPropDictSelector());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the -classScript- selector to the script number.
|
|
|
|
// FIXME: As this selector is filled in at run-time, it is likely
|
|
|
|
// that it is supposed to hold a pointer. The Obj::isKindOf method
|
|
|
|
// uses this selector together with -propDict- to compare classes.
|
|
|
|
// For the purpose of Obj::isKindOf, using the script number appears
|
|
|
|
// to be sufficient.
|
2010-06-27 21:00:34 +00:00
|
|
|
obj->setClassScriptSelector(make_reg(0, _nr));
|
2010-06-26 23:13:05 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker += seeker.getUint16SEAt(2) * 2;
|
2010-06-26 23:13:05 +00:00
|
|
|
}
|
2010-06-28 11:22:20 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
relocateSci0Sci21(make_reg(segmentId, _heap.getUint16SEAt(0)));
|
2010-02-02 22:50:32 +00:00
|
|
|
}
|
2009-02-15 22:28:50 +00:00
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
void Script::initializeObjectsSci3(SegManager *segMan, SegmentId segmentId) {
|
2016-12-31 20:39:57 -06:00
|
|
|
SciSpan<const byte> seeker = getSci3ObjectsPointer();
|
2010-11-17 09:10:43 +00:00
|
|
|
|
2016-12-31 20:39:57 -06:00
|
|
|
while (seeker.getUint16SEAt(0) == SCRIPT_OBJECT_MAGIC_NUMBER) {
|
2015-07-13 01:33:03 +03:00
|
|
|
// We call setSegment and setOffset directly here, instead of using
|
|
|
|
// make_reg, as in large scripts, seeker - _buf can be larger than
|
|
|
|
// a 16-bit integer
|
|
|
|
reg_t reg;
|
|
|
|
reg.setSegment(segmentId);
|
2016-12-31 20:39:57 -06:00
|
|
|
reg.setOffset(seeker - *_buf);
|
2010-11-17 09:10:43 +00:00
|
|
|
|
2015-07-13 01:33:03 +03:00
|
|
|
Object *obj = scriptObjInit(reg);
|
2012-06-18 05:21:59 +03:00
|
|
|
obj->setSuperClassSelector(segMan->getClassAddress(obj->getSuperClassSelector().getOffset(), SCRIPT_GET_LOCK, 0));
|
2016-12-31 20:39:57 -06:00
|
|
|
seeker += seeker.getUint16SEAt(2);
|
2010-11-17 09:10:43 +00:00
|
|
|
}
|
|
|
|
|
2010-11-17 11:15:52 +00:00
|
|
|
relocateSci3(make_reg(segmentId, 0));
|
2010-11-17 09:10:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
void Script::initializeObjects(SegManager *segMan, SegmentId segmentId) {
|
2010-11-17 08:53:02 +00:00
|
|
|
if (getSciVersion() <= SCI_VERSION_1_LATE)
|
2011-05-25 11:17:11 -04:00
|
|
|
initializeObjectsSci0(segMan, segmentId);
|
2015-12-29 01:44:11 +01:00
|
|
|
else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE)
|
2011-05-25 11:17:11 -04:00
|
|
|
initializeObjectsSci11(segMan, segmentId);
|
2010-11-17 08:53:02 +00:00
|
|
|
else if (getSciVersion() == SCI_VERSION_3)
|
2011-05-25 11:17:11 -04:00
|
|
|
initializeObjectsSci3(segMan, segmentId);
|
2010-09-13 13:17:55 +00:00
|
|
|
}
|
|
|
|
|
2010-06-28 12:28:46 +00:00
|
|
|
reg_t Script::findCanonicAddress(SegManager *segMan, reg_t addr) const {
|
2012-06-18 05:21:59 +03:00
|
|
|
addr.setOffset(0);
|
2010-06-28 12:28:46 +00:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::freeAtAddress(SegManager *segMan, reg_t addr) {
|
|
|
|
/*
|
2011-01-01 12:48:12 +00:00
|
|
|
debugC(kDebugLevelGC, "[GC] Freeing script %04x:%04x", PRINT_REG(addr));
|
2010-06-28 12:28:46 +00:00
|
|
|
if (_localsSegment)
|
2011-01-01 12:48:12 +00:00
|
|
|
debugC(kDebugLevelGC, "[GC] Freeing locals %04x:0000", _localsSegment);
|
2010-06-28 12:28:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (_markedAsDeleted)
|
|
|
|
segMan->deallocateScript(_nr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Array<reg_t> Script::listAllDeallocatable(SegmentId segId) const {
|
|
|
|
const reg_t r = make_reg(segId, 0);
|
|
|
|
return Common::Array<reg_t>(&r, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Array<reg_t> Script::listAllOutgoingReferences(reg_t addr) const {
|
|
|
|
Common::Array<reg_t> tmp;
|
2016-12-31 20:39:57 -06:00
|
|
|
if (addr.getOffset() <= _buf->size() && addr.getOffset() >= (uint)-SCRIPT_OBJECT_MAGIC_OFFSET && offsetIsObject(addr.getOffset())) {
|
2012-06-18 05:21:59 +03:00
|
|
|
const Object *obj = getObject(addr.getOffset());
|
2010-06-28 12:28:46 +00:00
|
|
|
if (obj) {
|
|
|
|
// Note all local variables, if we have a local variable environment
|
|
|
|
if (_localsSegment)
|
|
|
|
tmp.push_back(make_reg(_localsSegment, 0));
|
|
|
|
|
|
|
|
for (uint i = 0; i < obj->getVarCount(); i++)
|
|
|
|
tmp.push_back(obj->getVariable(i));
|
|
|
|
} else {
|
|
|
|
error("Request for outgoing script-object reference at %04x:%04x failed", PRINT_REG(addr));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* warning("Unexpected request for outgoing script-object references at %04x:%04x", PRINT_REG(addr));*/
|
|
|
|
/* Happens e.g. when we're looking into strings */
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Array<reg_t> Script::listObjectReferences() const {
|
|
|
|
Common::Array<reg_t> tmp;
|
|
|
|
|
|
|
|
// Locals, if present
|
|
|
|
if (_localsSegment)
|
|
|
|
tmp.push_back(make_reg(_localsSegment, 0));
|
|
|
|
|
|
|
|
// All objects (may be classes, may be indirectly reachable)
|
|
|
|
ObjMap::iterator it;
|
|
|
|
const ObjMap::iterator end = _objects.end();
|
|
|
|
for (it = _objects.begin(); it != end; ++it) {
|
|
|
|
tmp.push_back(it->_value.getPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2012-06-15 22:02:13 +03:00
|
|
|
bool Script::offsetIsObject(uint16 offset) const {
|
2016-12-31 20:39:57 -06:00
|
|
|
return _buf->getUint16SEAt(offset + SCRIPT_OBJECT_MAGIC_OFFSET) == SCRIPT_OBJECT_MAGIC_NUMBER;
|
2012-06-15 22:02:13 +03:00
|
|
|
}
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|