1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* 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.
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#ifndef SCI_ENGINE_SCRIPT_H
#define SCI_ENGINE_SCRIPT_H
#include "common/str.h"
#include "sci/engine/segment.h"
namespace Sci {
struct EngineState;
class ResourceManager;
struct SciScriptSignature;
enum ScriptObjectTypes {
SCI_OBJ_TERMINATOR,
SCI_OBJ_OBJECT,
SCI_OBJ_CODE,
SCI_OBJ_SYNONYMS,
SCI_OBJ_SAID,
SCI_OBJ_STRINGS,
SCI_OBJ_CLASS,
SCI_OBJ_EXPORTS,
SCI_OBJ_POINTERS,
SCI_OBJ_PRELOAD_TEXT, /* This is really just a flag. */
SCI_OBJ_LOCALVARS
};
typedef Common::HashMap<uint16, Object> ObjMap;
class Script : public SegmentObj {
private:
int _nr; /**< Script number */
byte *_buf; /**< Static data buffer, or NULL if not used */
byte *_heapStart; /**< Start of heap if SCI1.1, NULL otherwise */
int _lockers; /**< Number of classes and objects that require this script */
size_t _scriptSize;
size_t _heapSize;
uint16 _bufSize;
const uint16 *_exportTable; /**< Abs. offset of the export table or 0 if not present */
uint16 _numExports; /**< Number of entries in the exports table */
const byte *_synonyms; /**< Synonyms block or 0 if not present*/
uint16 _numSynonyms; /**< Number of entries in the synonyms block */
int _localsOffset;
uint16 _localsCount;
bool _markedAsDeleted;
public:
/**
* Table for objects, contains property variables.
* Indexed by the TODO offset.
*/
ObjMap _objects;
SegmentId _localsSegment; /**< The local variable segment */
LocalVariables *_localsBlock;
public:
int getLocalsOffset() const { return _localsOffset; }
uint16 getLocalsCount() const { return _localsCount; }
uint32 getScriptSize() const { return _scriptSize; }
uint32 getHeapSize() const { return _heapSize; }
uint32 getBufSize() const { return _bufSize; }
const byte *getBuf(uint offset = 0) const { return _buf + offset; }
int getScriptNumber() const { return _nr; }
public:
Script();
~Script();
void freeScript();
void init(int script_nr, ResourceManager *resMan);
void load(ResourceManager *resMan);
void matchSignatureAndPatch(uint16 scriptNr, byte *scriptData, const uint32 scriptSize);
int32 findSignature(const SciScriptSignature *signature, const byte *scriptData, const uint32 scriptSize);
void applyPatch(const uint16 *patch, byte *scriptData, const uint32 scriptSize, int32 signatureOffset);
virtual bool isValidOffset(uint16 offset) const;
virtual SegmentRef dereference(reg_t pointer);
virtual reg_t findCanonicAddress(SegManager *segMan, reg_t sub_addr) const;
virtual void freeAtAddress(SegManager *segMan, reg_t sub_addr);
virtual Common::Array<reg_t> listAllDeallocatable(SegmentId segId) const;
virtual Common::Array<reg_t> listAllOutgoingReferences(reg_t object) const;
/**
* Return a list of all references to objects in this script
* (and also to the locals segment, if any).
* Used by the garbage collector.
* @return a list of outgoing references within the object
*/
Common::Array<reg_t> listObjectReferences() const;
virtual void saveLoadWithSerializer(Common::Serializer &ser);
Object *getObject(uint16 offset);
const Object *getObject(uint16 offset) const;
/**
* Initializes an object within the segment manager
* @param obj_pos Location (segment, offset) of the object. It must
* point to the beginning of the script/class block
* (as opposed to what the VM considers to be the
* object location)
* @returns A newly created Object describing the object,
* stored within the relevant script
*/
Object *scriptObjInit(reg_t obj_pos, bool fullObjectInit = true);
/**
* Removes a script object
* @param obj_pos Location (segment, offset) of the object.
*/
void scriptObjRemove(reg_t obj_pos);
/**
* Initializes the script's local variables
* @param segMan A reference to the segment manager
*/
void initialiseLocals(SegManager *segMan);
/**
* Adds the script's classes to the segment manager's class table
* @param segMan A reference to the segment manager
*/
void initialiseClasses(SegManager *segMan);
/**
* Initializes the script's objects (SCI0)
* @param segMan A reference to the segment manager
* @param segmentId The script's segment id
*/
void initialiseObjectsSci0(SegManager *segMan, SegmentId segmentId);
/**
* Initializes the script's objects (SCI1.1+)
* @param segMan A reference to the segment manager
* @param segmentId The script's segment id
*/
void initialiseObjectsSci11(SegManager *segMan, SegmentId segmentId);
// script lock operations
/** Increments the number of lockers of this script by one. */
void incrementLockers();
/** Decrements the number of lockers of this script by one. */
void decrementLockers();
/**
* Retrieves the number of locks held on this script.
* @return the number of locks held on the previously identified script
*/
int getLockers() const;
/** Sets the number of locks held on this script. */
void setLockers(int lockers);
/**
* Retrieves a pointer to the exports of this script
* @return pointer to the exports.
*/
const uint16 *getExportTable() const { return _exportTable; }
/**
* Retrieves the number of exports of script.
* @return the number of exports of this script
*/
uint16 getExportsNr() const { return _numExports; }
/**
* Retrieves a pointer to the synonyms associated with this script
* @return pointer to the synonyms, in non-parsed format.
*/
const byte *getSynonyms() const { return _synonyms; }
/**
* Retrieves the number of synonyms associated with this script.
* @return the number of synonyms associated with this script
*/
uint16 getSynonymsNr() const { return _numSynonyms; }
/**
* Validate whether the specified public function is exported by
* the script in the specified segment.
* @param pubfunct Index of the function to validate
* @return NULL if the public function is invalid, its
* offset into the script's segment otherwise
*/
uint16 validateExportFunc(int pubfunct);
/**
* Marks the script as deleted.
* This will not actually delete the script. If references remain present on the
* heap or the stack, the script will stay in memory in a quasi-deleted state until
* either unreachable (resulting in its eventual deletion) or reloaded (resulting
* in its data being updated).
*/
void markDeleted() {
_markedAsDeleted = true;
}
/**
* Determines whether the script is marked as being deleted.
*/
bool isMarkedAsDeleted() const {
return _markedAsDeleted;
}
/**
* Copies a byte string into a script's heap representation.
* @param dst script-relative offset of the destination area
* @param src pointer to the data source location
* @param n number of bytes to copy
*/
void mcpyInOut(int dst, const void *src, size_t n);
/**
* Finds the pointer where a block of a specific type starts from
*/
byte *findBlock(int type, int skipBlockIndex = -1);
private:
/**
* Processes a relocation block witin a script
* This function is idempotent, but it must only be called after all
* objects have been instantiated, or a run-time error will occur.
* @param obj_pos Location (segment, offset) of the block
* @return Location of the relocation block
*/
void relocate(reg_t block);
bool relocateLocal(SegmentId segment, int location);
};
} // End of namespace Sci
#endif // SCI_ENGINE_SCRIPT_H
|