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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/* 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$
*
*/
#include "common/util.h"
#include "common/endian.h"
#include "gob/gob.h"
#include "gob/script.h"
#include "gob/dataio.h"
#include "gob/expression.h"
#include "gob/videoplayer.h"
namespace Gob {
Script::Script(GobEngine *vm) : _vm(vm) {
_expression = new Expression(vm);
_finished = true;
_totData = 0;
_totPtr = 0;
_totSize = 0;
_lomHandle = -1;
}
Script::~Script() {
unload();
delete _expression;
}
uint32 Script::read(byte *data, int32 size) {
int32 toRead = MIN<int32>(size, _totSize - (_totPtr - _totData));
if (toRead < 1)
return 0;
memcpy(data, _totPtr, toRead);
_totPtr += toRead;
return toRead;
}
uint32 Script::peek(byte *data, int32 size, int32 offset) const {
int32 totOffset = ((_totPtr + offset) - _totData);
if (totOffset < 1)
return 0;
if (((uint32) totOffset) >= _totSize)
return 0;
int32 toPeek = MIN<int32>(size, _totSize - totOffset);
if (toPeek < 1)
return 0;
memcpy(data, _totPtr + offset, toPeek);
return toPeek;
}
int32 Script::pos() const {
if (!_totData)
return -1;
return _totPtr - _totData;
}
int32 Script::getSize() const {
if (!_totData)
return -1;
return _totSize;
}
bool Script::seek(int32 offset, int whence) {
if (!_totData)
return false;
if (whence == SEEK_CUR)
offset += pos();
else if (whence == SEEK_END)
offset += _totSize;
if ((offset < 0) || (((uint32) offset) >= _totSize))
return false;
// Cannot seek into the header
if (offset < 128) {
_finished = true;
return false;
}
// A successful seek means the script file continues to be executed
_finished = false;
_totPtr = _totData + offset;
return true;
}
bool Script::skip(int32 offset) {
return seek(offset, SEEK_CUR);
}
int32 Script::getOffset(byte *ptr) const {
if (!_totData)
return -1;
if ((ptr < _totData) || (ptr >= (_totData + _totSize)))
return -1;
return ptr - _totData;
}
byte *Script::getData(int32 offset) const {
if (!_totData)
return 0;
if ((offset < 0) || (((uint32) offset) >= _totSize))
return 0;
return _totData + offset;
}
byte *Script::getData() {
return _totData;
}
byte Script::readByte() {
byte v;
read(&v, 1);
return v;
}
char Script::readChar() {
return (char) readByte();
}
uint8 Script::readUint8() {
return (uint8) readByte();
}
uint16 Script::readUint16() {
byte v[2];
read(v, 2);
return READ_LE_UINT16(v);
}
uint32 Script::readUint32() {
byte v[4];
read(v, 4);
return READ_LE_UINT32(v);
}
int8 Script::readInt8() {
return (int8) readByte();
}
int16 Script::readInt16() {
return (int16) readUint16();
}
int32 Script::readInt32() {
return (int32) readUint32();
}
char *Script::readString(int32 length) {
if (length < 0) {
length = 0;
while (_totPtr[length++] != '\0');
}
char *string = (char *) _totPtr;
_totPtr += length;
return string;
}
byte Script::peekByte(int32 offset) {
byte v;
peek(&v, 1, offset);
return v;
}
char Script::peekChar(int32 offset) {
return (char) peekByte(offset);
}
uint8 Script::peekUint8(int32 offset) {
return (uint8) peekByte(offset);
}
uint16 Script::peekUint16(int32 offset) {
byte v[2];
peek(v, 2, offset);
return READ_LE_UINT16(v);
}
uint32 Script::peekUint32(int32 offset) {
byte v[4];
peek(v, 4, offset);
return READ_LE_UINT32(v);
}
int8 Script::peekInt8(int32 offset) {
return (int8) peekByte(offset);
}
int16 Script::peekInt16(int32 offset) {
return (int16) peekUint16(offset);
}
int32 Script::peekInt32(int32 offset) {
return (int32) peekUint32(offset);
}
char *Script::peekString(int32 offset) {
return (char *) (_totPtr + offset);
}
int16 Script::readVarIndex(uint16 *size, uint16 *type) {
return _expression->parseVarIndex(size, type);
}
int16 Script::readValExpr(byte stopToken) {
return _expression->parseValExpr(stopToken);
}
int16 Script::readExpr(byte stopToken, byte *type) {
return _expression->parseExpr(stopToken, type);
}
void Script::skipExpr(char stopToken) {
_expression->skipExpr(stopToken);
}
char Script::evalExpr(int16 *pRes) {
byte type;
_expression->printExpr(99);
_expression->parseExpr(99, &type);
if (!pRes)
return type;
switch (type) {
case TYPE_IMM_INT16:
*pRes = _expression->getResultInt();
break;
case TYPE_IMM_STR:
case GOB_FALSE:
*pRes = 0;
break;
case GOB_TRUE:
*pRes = 1;
break;
}
return type;
}
bool Script::evalBoolResult() {
byte type;
_expression->printExpr(99);
_expression->parseExpr(99, &type);
if ( (type == GOB_TRUE) ||
((type == TYPE_IMM_INT16) && _expression->getResultInt()))
return true;
else
return false;
}
int32 Script::getResultInt() const {
return _expression->getResultInt();
}
char *Script::getResultStr() const {
return _expression->getResultStr();
}
bool Script::load(const char *fileName) {
unload();
_finished = false;
bool lom = false;
Common::String *fileBase;
const char *dot;
if ((dot = strrchr(fileName, '.'))) {
// fileName includes an extension
fileBase = new Common::String(fileName, dot);
// Is it a LOM file?
if (!scumm_stricmp(dot + 1, "LOM"))
lom = true;
} else
fileBase = new Common::String(fileName);
// If it's a LOM file, it includes the TOT file
_totFile = *fileBase + (lom ? ".lom" : ".tot");
delete fileBase;
if (lom) {
if (!loadLOM(_totFile)) {
unload();
return false;
}
} else {
if (!loadTOT(_totFile)) {
unload();
return false;
}
}
return true;
}
bool Script::loadTOT(const Common::String &fileName) {
if (_vm->_dataIO->existData(fileName.c_str())) {
// Direct data file
_totSize = _vm->_dataIO->getDataSize(_totFile.c_str());
_totData = _vm->_dataIO->getData(_totFile.c_str());
} else {
// Trying to read the TOT file out of the currently loaded video file
Common::MemoryReadStream *videoExtraData = _vm->_vidPlayer->getExtraData(fileName.c_str());
if (videoExtraData) {
warning("Loading TOT \"%s\" from video file", fileName.c_str());
_totSize = videoExtraData->size();
_totData = new byte[_totSize];
videoExtraData->read(_totData, _totSize);
delete videoExtraData;
}
}
if (_totData == 0)
return false;
return getTOTProperties();
}
bool Script::loadLOM(const Common::String &fileName) {
warning("Urban Stub: loadLOM %s", _totFile.c_str());
_lomHandle = _vm->_dataIO->openData(_totFile.c_str());
if (_lomHandle < 0)
return false;
DataStream *stream = _vm->_dataIO->openAsStream(_lomHandle);
stream->seek(48);
_totSize = stream->readUint32LE();
stream->seek(0);
_totData = new byte[_totSize];
stream->read(_totData, _totSize);
delete stream;
return true;
}
bool Script::getTOTProperties() {
// Offset 39-41: Version in "Major.Minor" string form
if (_totData[40] != '.')
return false;
_versionMajor = _totData[39] - '0';
_versionMinor = _totData[41] - '0';
_variablesCount = READ_LE_UINT32(_totData + 44);
_textsOffset = READ_LE_UINT32(_totData + 48);
_resourcesOffset = READ_LE_UINT32(_totData + 52);
_animDataSize = READ_LE_UINT16(_totData + 56);
_imFileNumber = _totData[59];
_exFileNumber = _totData[60];
_communHandling = _totData[61];
return true;
}
void Script::unload() {
unloadTOT();
}
void Script::unloadTOT() {
if (_lomHandle >= 0)
_vm->_dataIO->closeData(_lomHandle);
// Unwind the call stack
while (!_callStack.empty())
pop();
delete[] _totData;
_totData = 0;
_totSize = 0;
_totPtr = 0;
_lomHandle = -1;
_totFile.clear();
_finished = true;
}
bool Script::isLoaded() const {
return _totData != 0;
}
void Script::setFinished(bool finished) {
_finished = finished;
}
bool Script::isFinished() const {
return _finished;
}
void Script::push() {
if (!isLoaded())
// Nothing to do
return;
CallEntry currentCall;
currentCall.totPtr = _totPtr;
currentCall.finished = _finished;
_callStack.push(currentCall);
}
void Script::pop(bool ret) {
if (!isLoaded())
// Nothing to do
return;
// Unmatched pop?
assert(!_callStack.empty());
CallEntry lastCall = _callStack.pop();
if (ret) {
_totPtr = lastCall.totPtr;
_finished = lastCall.finished;
}
}
void Script::call(uint32 offset) {
if (!isLoaded())
// Nothing to do
return;
push();
seek(offset);
}
uint8 Script::getVersionMajor() const {
return _versionMajor;
}
uint8 Script::getVersionMinor() const {
return _versionMinor;
}
uint32 Script::getVariablesCount() const {
return _variablesCount;
}
uint32 Script::getTextsOffset() const {
return _textsOffset;
}
uint32 Script::getResourcesOffset() const {
return _resourcesOffset;
}
uint16 Script::getAnimDataSize() const {
return _animDataSize;
}
uint8 Script::getImFileNumber() const {
return _imFileNumber;
}
uint8 Script::getExFileNumber() const {
return _exFileNumber;
}
uint8 Script::getCommunHandling() const {
return _communHandling;
}
uint16 Script::getFunctionOffset(uint8 function) const {
if (!_totData)
return 0;
// Offsets 100-128, 2 bytes per function
assert(function <= 13);
return READ_LE_UINT16(_totData + 100 + function * 2);
}
uint32 Script::getVariablesCount(const char *fileName, GobEngine *vm) {
if (!vm->_dataIO->existData(fileName))
return 0;
DataStream *stream = vm->_dataIO->getDataStream(fileName);
stream->seek(0x2C);
uint32 variablesCount = stream->readUint32LE();
delete stream;
return variablesCount;
}
} // End of namespace Gob
|