aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine/object.h
blob: a7be170f4fa07d9b2d704e6ad1a84c47584c5b95 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* 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.
 *
 */

#ifndef SCI_ENGINE_OBJECT_H
#define SCI_ENGINE_OBJECT_H

#include "common/array.h"
#include "common/serializer.h"
#include "common/textconsole.h"

#include "sci/sci.h"			// for the SCI versions
#include "sci/engine/vm_types.h"	// for reg_t
#include "sci/util.h"

namespace Sci {

class SegManager;

/** Clone has been marked as 'freed' */
enum {
	OBJECT_FLAG_FREED = (1 << 0)
};

enum infoSelectorFlags {
	kInfoFlagClone        = 0x0001,
#ifdef ENABLE_SCI32
	/**
	 * When set, indicates to game scripts that a screen
	 * item can be updated.
	 */
	kInfoFlagViewVisible  = 0x0008, // TODO: "dirty" ?

	/**
	 * When set, the object has an associated screen item in
	 * the rendering tree.
	 */
	kInfoFlagViewInserted = 0x0010,
#endif
	kInfoFlagClass        = 0x8000
};

enum ObjectOffsets {
	kOffsetLocalVariables = -6,
	kOffsetFunctionArea = -4,
	kOffsetSelectorCounter = -2,
	kOffsetSelectorSegment = 0,
	kOffsetInfoSelectorSci0 = 4,
	kOffsetNamePointerSci0 = 6,
	kOffsetInfoSelectorSci11 = 14,
	kOffsetNamePointerSci11 = 16
};

class Object {
public:
	Object() {
		_offset = getSciVersion() < SCI_VERSION_1_1 ? 0 : 5;
		_flags = 0;
		_baseObj = 0;
		_baseVars = 0;
		_methodCount = 0;
		_propertyOffsetsSci3 = 0;
	}

	~Object() {
		if (getSciVersion() == SCI_VERSION_3) {
			// FIXME: memory leak! Commented out because of reported heap
			// corruption by MSVC (e.g. in LSL7, when it starts)
			//free(_baseVars);
			//_baseVars = 0;
			//free(_propertyOffsetsSci3);
			//_propertyOffsetsSci3 = 0;
		}
	}

	reg_t getSpeciesSelector() const {
		if (getSciVersion() < SCI_VERSION_3)
			return _variables[_offset];
		else	// SCI3
			return _speciesSelectorSci3;
	}

	void setSpeciesSelector(reg_t value) {
		if (getSciVersion() < SCI_VERSION_3)
			_variables[_offset] = value;
		else	// SCI3
			_speciesSelectorSci3 = value;
	}

	reg_t getSuperClassSelector() const {
		if (getSciVersion() < SCI_VERSION_3)
			return _variables[_offset + 1];
		else	// SCI3
			return _superClassPosSci3;
	}

	void setSuperClassSelector(reg_t value) {
		if (getSciVersion() < SCI_VERSION_3)
			_variables[_offset + 1] = value;
		else	// SCI3
			_superClassPosSci3 = value;
	}

	reg_t getInfoSelector() const {
		if (getSciVersion() < SCI_VERSION_3)
			return _variables[_offset + 2];
		else	// SCI3
			return _infoSelectorSci3;
	}

	void setInfoSelector(reg_t info) {
		if (getSciVersion() < SCI_VERSION_3)
			_variables[_offset + 2] = info;
		else	// SCI3
			_infoSelectorSci3 = info;
	}

#ifdef ENABLE_SCI32
	void setInfoSelectorFlag(infoSelectorFlags flag) {
		if (getSciVersion() < SCI_VERSION_3) {
			_variables[_offset + 2] |= flag;
		} else {
			_infoSelectorSci3 |= flag;
		}
	}

	// NOTE: In real engine, -info- is treated as byte size
	void clearInfoSelectorFlag(infoSelectorFlags flag) {
		if (getSciVersion() < SCI_VERSION_3) {
			_variables[_offset + 2] &= ~flag;
		} else {
			_infoSelectorSci3 &= ~flag;
		}
	}
#endif

	reg_t getNameSelector() const {
		if (getSciVersion() < SCI_VERSION_3)
			return _offset + 3 < (uint16)_variables.size() ? _variables[_offset + 3] : NULL_REG;
		else	// SCI3
			return _variables.size() ? _variables[0] : NULL_REG;
	}

	// No setter for the name selector

	reg_t getPropDictSelector() const {
		if (getSciVersion() < SCI_VERSION_3)
			return _variables[2];
		else
			// This should never occur, this is called from a SCI1.1 - SCI2.1 only function
			error("getPropDictSelector called for SCI3");
	}

	void setPropDictSelector(reg_t value) {
		if (getSciVersion() < SCI_VERSION_3)
			_variables[2] = value;
		else
			// This should never occur, this is called from a SCI1.1 - SCI2.1 only function
			error("setPropDictSelector called for SCI3");
	}

	reg_t getClassScriptSelector() const {
		if (getSciVersion() < SCI_VERSION_3)
			return _variables[4];
		else	// SCI3
			return make_reg(0, READ_SCI11ENDIAN_UINT16(_baseObj + 6));
	}

	void setClassScriptSelector(reg_t value) {
		if (getSciVersion() < SCI_VERSION_3)
			_variables[4] = value;
		else	// SCI3
			// This should never occur, this is called from a SCI1.1 - SCI2.1 only function
			error("setClassScriptSelector called for SCI3");
	}

	Selector getVarSelector(uint16 i) const { return READ_SCI11ENDIAN_UINT16(_baseVars + i); }

	reg_t getFunction(uint16 i) const {
		uint16 offset = (getSciVersion() < SCI_VERSION_1_1) ? _methodCount + 1 + i : i * 2 + 2;
		if (getSciVersion() == SCI_VERSION_3)
			offset--;
		return make_reg(_pos.getSegment(), _baseMethod[offset]);
	}

	Selector getFuncSelector(uint16 i) const {
		uint16 offset = (getSciVersion() < SCI_VERSION_1_1) ? i : i * 2 + 1;
		if (getSciVersion() == SCI_VERSION_3)
			offset--;
		return _baseMethod[offset];
	}

	/**
	 * Determines if this object is a class and explicitly defines the
	 * selector as a funcselector. Does NOT say anything about the object's
	 * superclasses, i.e. failure may be returned even if one of the
	 * superclasses defines the funcselector
	 */
	int funcSelectorPosition(Selector sel) const {
		for (uint i = 0; i < _methodCount; i++)
			if (getFuncSelector(i) == sel)
				return i;

		return -1;
	}

	/**
	 * Determines if the object explicitly defines slc as a varselector.
	 * Returns -1 if not found.
	 */
	int locateVarSelector(SegManager *segMan, Selector slc) const;

	bool isClass() const { return (getInfoSelector().getOffset() & kInfoFlagClass); }
	const Object *getClass(SegManager *segMan) const;

	void markAsFreed() { _flags |= OBJECT_FLAG_FREED; }
	bool isFreed() const { return _flags & OBJECT_FLAG_FREED; }

	uint getVarCount() const { return _variables.size(); }

	void init(byte *buf, reg_t obj_pos, bool initVariables = true);

	reg_t getVariable(uint var) const { return _variables[var]; }
	reg_t &getVariableRef(uint var) { return _variables[var]; }

	uint16 getMethodCount() const { return _methodCount; }
	reg_t getPos() const { return _pos; }

	void saveLoadWithSerializer(Common::Serializer &ser);

	void cloneFromObject(const Object *obj) {
		_baseObj = obj ? obj->_baseObj : NULL;
		_baseMethod = obj ? obj->_baseMethod : Common::Array<uint16>();
		_baseVars = obj ? obj->_baseVars : NULL;
	}

	bool relocateSci0Sci21(SegmentId segment, int location, size_t scriptSize);
	bool relocateSci3(SegmentId segment, uint32 location, int offset, size_t scriptSize);

	int propertyOffsetToId(SegManager *segMan, int propertyOffset) const;

	void initSpecies(SegManager *segMan, reg_t addr);
	void initSuperClass(SegManager *segMan, reg_t addr);
	bool initBaseObject(SegManager *segMan, reg_t addr, bool doInitSuperClass = true);
	void syncBaseObject(const byte *ptr) { _baseObj = ptr; }

	bool mustSetViewVisibleSci3(int selector) const { return _mustSetViewVisible[selector/32]; }

private:
	void initSelectorsSci3(const byte *buf);

	const byte *_baseObj; /**< base + object offset within base */
	const uint16 *_baseVars; /**< Pointer to the varselector area for this object */
	Common::Array<uint16> _baseMethod; /**< Pointer to the method selector area for this object */
	uint32 *_propertyOffsetsSci3; /**< This is used to enable relocation of property valuesa in SCI3 */

	Common::Array<reg_t> _variables;
	uint16 _methodCount;
	int _flags;
	uint16 _offset;
	reg_t _pos; /**< Object offset within its script; for clones, this is their base */
	reg_t _superClassPosSci3; /**< reg_t pointing to superclass for SCI3 */
	reg_t _speciesSelectorSci3;	/**< reg_t containing species "selector" for SCI3 */
	reg_t _infoSelectorSci3; /**< reg_t containing info "selector" for SCI3 */
	Common::Array<bool> _mustSetViewVisible; /** cached bit of info to make lookup fast, SCI3 only */
};


} // End of namespace Sci

#endif // SCI_ENGINE_OBJECT_H