|
@@ -29,7 +29,7 @@ int Script::load(const char *file) {
|
29
|
29
|
description = desc;
|
30
|
30
|
|
31
|
31
|
uint16_t gameflowSize = f.readU16();
|
32
|
|
- assert(gameflowSize == 128);
|
|
32
|
+ assertEqual(gameflowSize, 128);
|
33
|
33
|
|
34
|
34
|
firstOption = f.readU32();
|
35
|
35
|
titleReplace = f.read32();
|
|
@@ -55,7 +55,6 @@ int Script::load(const char *file) {
|
55
|
55
|
f.seek(f.tell() + 32);
|
56
|
56
|
|
57
|
57
|
flags = f.readU16();
|
58
|
|
- bool tag = (flags & S_UseSecurityTag) != 0;
|
59
|
58
|
|
60
|
59
|
// Filler 3 (6 bytes)
|
61
|
60
|
f.seek(f.tell() + 6);
|
|
@@ -68,50 +67,87 @@ int Script::load(const char *file) {
|
68
|
67
|
f.seek(f.tell() + 4);
|
69
|
68
|
|
70
|
69
|
// Strings
|
71
|
|
- readStringPackage(f, levelNames, numLevels, tag, 0);
|
72
|
|
- readStringPackage(f, pictureFilenames, numPictures, tag, 0);
|
73
|
|
- readStringPackage(f, titleFilenames, numTitles, tag, 0);
|
74
|
|
- readStringPackage(f, fmvFilenames, numFMVs, tag, 0);
|
75
|
|
- readStringPackage(f, levelFilenames, numLevels, tag, 0);
|
76
|
|
- readStringPackage(f, cutsceneFilenames, numCutscenes, tag, 0);
|
|
70
|
+ readStringPackage(f, levelNames, numLevels);
|
|
71
|
+ readStringPackage(f, pictureFilenames, numPictures);
|
|
72
|
+ readStringPackage(f, titleFilenames, numTitles);
|
|
73
|
+ readStringPackage(f, fmvFilenames, numFMVs);
|
|
74
|
+ readStringPackage(f, levelFilenames, numLevels);
|
|
75
|
+ readStringPackage(f, cutsceneFilenames, numCutscenes);
|
|
76
|
+
|
|
77
|
+ // Script Package
|
|
78
|
+ uint16_t *offset = new uint16_t[numLevels + 1];
|
|
79
|
+ for (unsigned int i = 0; i < (numLevels + 1); i++) {
|
|
80
|
+ offset[i] = f.readU16();
|
|
81
|
+ assertEqual(offset[i] % 2, 0);
|
|
82
|
+ }
|
77
|
83
|
|
78
|
|
- // Offset definitely TR2 specific?!
|
79
|
|
- readStringPackage(f, script, numLevels + 1, false, 6);
|
|
84
|
+ uint16_t numBytes = f.readU16() + 6; // Offset TR2 specific?!
|
|
85
|
+ assertEqual(numBytes % 2, 0); // 16 bit opcodes and operands
|
80
|
86
|
|
81
|
|
- numGameStrings = f.readU16();
|
82
|
|
- assert(numGameStrings == 89);
|
|
87
|
+ uint16_t *list = new uint16_t[numBytes / 2];
|
|
88
|
+ for (uint16_t i = 0; i < (numBytes / 2); i++) {
|
|
89
|
+ list[i] = f.readU16();
|
|
90
|
+ }
|
83
|
91
|
|
84
|
|
- readStringPackage(f, gameStrings, numGameStrings, tag, 0);
|
|
92
|
+ for (unsigned int i = 0; i < (numLevels + 1); i++) {
|
|
93
|
+ unsigned int end = offset[i] / 2;
|
|
94
|
+
|
|
95
|
+ bool readingOperand = false;
|
|
96
|
+ while (readingOperand || (list[end] != OP_END)) {
|
|
97
|
+ if (readingOperand) {
|
|
98
|
+ readingOperand = false;
|
|
99
|
+ end++;
|
|
100
|
+ } else {
|
|
101
|
+ if (opcodeHasOperand[list[end]]) {
|
|
102
|
+ readingOperand = true;
|
|
103
|
+ }
|
|
104
|
+ end++;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ end++;
|
85
|
108
|
|
86
|
|
- readStringPackage(f, pcStrings, 41, tag, 0);
|
|
109
|
+ std::vector<uint16_t> tmp;
|
|
110
|
+ for (unsigned int a = (offset[i] / 2); a < end; a++)
|
|
111
|
+ tmp.push_back(list[a]);
|
87
|
112
|
|
88
|
|
- readStringPackage(f, puzzles[0], numLevels, tag, 0);
|
89
|
|
- readStringPackage(f, puzzles[1], numLevels, tag, 0);
|
90
|
|
- readStringPackage(f, puzzles[2], numLevels, tag, 0);
|
91
|
|
- readStringPackage(f, puzzles[3], numLevels, tag, 0);
|
|
113
|
+ script.push_back(tmp);
|
|
114
|
+ }
|
92
|
115
|
|
93
|
|
- readStringPackage(f, pickups[0], numLevels, tag, 0);
|
94
|
|
- readStringPackage(f, pickups[1], numLevels, tag, 0);
|
|
116
|
+ delete [] list;
|
|
117
|
+ delete [] offset;
|
95
|
118
|
|
96
|
|
- readStringPackage(f, keys[0], numLevels, tag, 0);
|
97
|
|
- readStringPackage(f, keys[1], numLevels, tag, 0);
|
98
|
|
- readStringPackage(f, keys[2], numLevels, tag, 0);
|
99
|
|
- readStringPackage(f, keys[3], numLevels, tag, 0);
|
|
119
|
+ // Engine expects 89 game strings!
|
|
120
|
+ numGameStrings = f.readU16();
|
|
121
|
+ assertEqual(numGameStrings, 89);
|
|
122
|
+
|
|
123
|
+ // More strings...
|
|
124
|
+ readStringPackage(f, gameStrings, numGameStrings);
|
|
125
|
+ readStringPackage(f, pcStrings, 41);
|
|
126
|
+ readStringPackage(f, puzzles[0], numLevels);
|
|
127
|
+ readStringPackage(f, puzzles[1], numLevels);
|
|
128
|
+ readStringPackage(f, puzzles[2], numLevels);
|
|
129
|
+ readStringPackage(f, puzzles[3], numLevels);
|
|
130
|
+ readStringPackage(f, pickups[0], numLevels);
|
|
131
|
+ readStringPackage(f, pickups[1], numLevels);
|
|
132
|
+ readStringPackage(f, keys[0], numLevels);
|
|
133
|
+ readStringPackage(f, keys[1], numLevels);
|
|
134
|
+ readStringPackage(f, keys[2], numLevels);
|
|
135
|
+ readStringPackage(f, keys[3], numLevels);
|
100
|
136
|
|
101
|
137
|
return 0;
|
102
|
138
|
}
|
103
|
139
|
|
104
|
|
-void Script::readStringPackage(BinaryFile &f, std::vector<std::string> &v, unsigned int n, bool tag, uint16_t off) {
|
|
140
|
+void Script::readStringPackage(BinaryFile &f, std::vector<std::string> &v, unsigned int n) {
|
105
|
141
|
uint16_t *offset = new uint16_t[n];
|
106
|
142
|
for (unsigned int i = 0; i < n; i++)
|
107
|
143
|
offset[i] = f.readU16();
|
108
|
144
|
|
109
|
|
- uint16_t numBytes = f.readU16() + off;
|
|
145
|
+ uint16_t numBytes = f.readU16();
|
110
|
146
|
|
111
|
147
|
char *list = new char[numBytes];
|
112
|
148
|
for (uint16_t i = 0; i < numBytes; i++) {
|
113
|
149
|
list[i] = f.read8();
|
114
|
|
- if (tag) {
|
|
150
|
+ if (flags & S_UseSecurityTag) {
|
115
|
151
|
list[i] ^= cypherCode;
|
116
|
152
|
}
|
117
|
153
|
}
|
|
@@ -202,3 +238,38 @@ std::string Script::getKeyString(unsigned int i, unsigned int j) {
|
202
|
238
|
return keys.at(i).at(j);
|
203
|
239
|
}
|
204
|
240
|
|
|
241
|
+void Script::registerScriptHandler(ScriptOpCode op, std::function<int (bool, uint16_t)> func) {
|
|
242
|
+ assert(op < OP_UNKNOWN);
|
|
243
|
+ scriptHandlers[op] = func;
|
|
244
|
+}
|
|
245
|
+
|
|
246
|
+int Script::runScript(unsigned int level) {
|
|
247
|
+ assert(level < (numLevels + 1));
|
|
248
|
+ std::vector<uint16_t> s = script.at(level);
|
|
249
|
+
|
|
250
|
+ for (unsigned int i = 0; i < s.size(); i++) {
|
|
251
|
+ uint16_t opcode = s.at(i);
|
|
252
|
+ if (opcode >= OP_UNKNOWN) {
|
|
253
|
+ return 1;
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ uint16_t operand = 0;
|
|
257
|
+ if (opcodeHasOperand[opcode]) {
|
|
258
|
+ if ((i + 1) >= s.size())
|
|
259
|
+ return 2; // Can't read operand!
|
|
260
|
+
|
|
261
|
+ operand = s.at(++i);
|
|
262
|
+ }
|
|
263
|
+
|
|
264
|
+ if (scriptHandlers[opcode]) {
|
|
265
|
+ int error = scriptHandlers[opcode](opcodeHasOperand[opcode], operand);
|
|
266
|
+ if (error != 0)
|
|
267
|
+ return error;
|
|
268
|
+ } else {
|
|
269
|
+ return 3;
|
|
270
|
+ }
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ return 0;
|
|
274
|
+}
|
|
275
|
+
|