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
|
/* ScummVM Tools
*
* ScummVM Tools 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/* Scumm Script Disassembler (common code) */
#include <string.h>
#include <stdio.h>
#include "descumm.h"
#include "common/endian.h"
BlockStack g_blockStack;
bool pendingElse, haveElse;
int pendingElseTo;
int pendingElseOffs;
int pendingElseOpcode;
int pendingElseIndent;
int g_jump_opcode;
Options g_options;
byte *g_scriptCurPos, *g_scriptStart;
int currentOpcodeBlockStart;
uint g_scriptSize;
///////////////////////////////////////////////////////////////////////////
char *strecpy(char *buf, const char *src) {
strcpy(buf, src);
return strchr(buf, 0);
}
int get_curoffs() {
return g_scriptCurPos - g_scriptStart;
}
int get_byte() {
return (byte)(*g_scriptCurPos++);
}
int get_word() {
int i;
if (g_options.scriptVersion == 8) {
i = (int32)READ_LE_UINT32(g_scriptCurPos);
g_scriptCurPos += 4;
} else {
i = (int16)READ_LE_UINT16(g_scriptCurPos);
g_scriptCurPos += 2;
}
return i;
}
int get_dword() {
int i;
i = (int32)READ_LE_UINT32(g_scriptCurPos);
g_scriptCurPos += 4;
return i;
}
///////////////////////////////////////////////////////////////////////////
void outputLine(const char *buf, int curoffs, int opcode, int indent) {
if (buf[0]) {
assert(curoffs >= 0);
assert(indent >= 0);
// Show the offset
if (!g_options.dontShowOffsets) {
printf("[%.4X] ", curoffs);
}
// Show the opcode value
if (!g_options.dontShowOpcode) {
if (opcode != -1)
printf("(%.2X) ", opcode);
else
printf("(**) ");
}
// Indent the line as requested ...
for (int i = 0; i < indent; ++i)
printf(" ");
// ... and finally print the actual code
puts(buf);
}
}
///////////////////////////////////////////////////////////////////////////
// Returns 0 or 1 depending if it's ok to add a block
bool maybeAddIf(uint cur, uint to) {
Block p;
int i;
if (((to | cur) >> 24) || (to <= cur))
return false; // Invalid jump
for (i = 0; i < g_blockStack.size(); ++i) {
if (to > g_blockStack[i].to)
return false;
}
// Try to determine if this is a while loop. For this, first check if we
// jump right behind a regular jump, then whether that jump is targeting us.
if (g_options.scriptVersion == 8) {
p.isWhile = (*(byte*)(g_scriptStart+to-5) == g_jump_opcode);
i = (int32)READ_LE_UINT32(g_scriptStart+to-4);
} else {
p.isWhile = (*(byte*)(g_scriptStart+to-3) == g_jump_opcode);
i = (int16)READ_LE_UINT16(g_scriptStart+to-2);
}
p.isWhile = p.isWhile && (currentOpcodeBlockStart == (int)to + i);
p.from = cur;
p.to = to;
g_blockStack.push(p);
return true;
}
// Returns 0 or 1 depending if it's ok to add an else
bool maybeAddElse(uint cur, uint to) {
int i;
if (((to | cur) >> 16) || (to <= cur))
return false; /* Invalid jump */
if (g_blockStack.empty())
return false; /* There are no previous blocks, so an else is not ok */
if (cur != g_blockStack.top().to)
return false; /* We have no prevoius if that is exiting right at the end of this goto */
// Don't jump out of previous blocks. In addition, don't jump "onto"
// the end of a while loop, as that would lead to incorrect output.
// This test is stronger than the one in maybeAddIf.
for (i = 0; i < g_blockStack.size() - 1; ++i) {
if (to > g_blockStack[i].to || (to == g_blockStack[i].to && g_blockStack[i].isWhile))
return false;
}
Block tmp = g_blockStack.pop();
if (maybeAddIf(cur, to))
return true; /* We can add an else */
g_blockStack.push(tmp);
return false; /* An else is not OK here :( */
}
bool maybeAddElseIf(uint cur, uint elseto, uint to) {
uint k;
if (((to | cur | elseto) >> 16) || (elseto < to) || (to <= cur))
return false; /* Invalid jump */
if (g_blockStack.empty())
return false; /* There are no previous blocks, so an ifelse is not ok */
if (g_blockStack.top().isWhile)
return false;
if (g_options.scriptVersion == 8)
k = to - 5;
else
k = to - 3;
if (k >= g_scriptSize)
return false; /* Invalid jump */
if (elseto != to) {
if (g_scriptStart[k] != g_jump_opcode)
return false; /* Invalid jump */
if (g_options.scriptVersion == 8)
k = to + READ_LE_UINT32(g_scriptStart + k + 1);
else
k = to + READ_LE_UINT16(g_scriptStart + k + 1);
if (k != elseto)
return false; /* Not an ifelse */
}
g_blockStack.top().from = cur;
g_blockStack.top().to = to;
return true;
}
bool maybeAddBreak(uint cur, uint to) {
if (((to | cur) >> 16) || (to <= cur))
return false; /* Invalid jump */
if (g_blockStack.empty())
return false; /* There are no previous blocks, so a break is not ok */
/* Find the first parent block that is a while and if we're jumping to the end of that, we use a break */
for (int i = g_blockStack.size() - 1; i >= 0; --i) {
if (g_blockStack[i].isWhile) {
if (to == g_blockStack[i].to)
return true;
else
return false;
}
}
return false;
}
void writePendingElse() {
if (pendingElse) {
char buf[32];
sprintf(buf, g_options.alwaysShowOffs ? "} else /*%.4X*/ {" : "} else {", pendingElseTo);
outputLine(buf, currentOpcodeBlockStart, pendingElseOpcode, pendingElseIndent - 1);
currentOpcodeBlockStart = pendingElseOffs;
pendingElse = false;
}
}
char *put_ascii(char *buf, int i) {
if (i > 31 && i < 128) {
// non-printable chars are escaped by backslashes as so: "\x00"
// backslashes and quote marks are escaped like so: "\\" "\""
if (i == '\\' || i == '"') {
buf[0] = '\\';
buf++;
}
buf[0] = i;
buf[1] = 0;
return buf + 1;
}
return buf + sprintf(buf, "\\x%.2X", i);
}
extern char *get_var(char *buf);
extern char *get_var6(char *buf);
char *get_string(char *buf) {
byte cmd;
char *e = buf;
bool first = true;
bool in = false;
int i;
while ((cmd = get_byte()) != 0) {
if (cmd == 0xFF || cmd == 0xFE) {
if (in) {
e += sprintf(e, "\"");
in = false;
}
if (first) {
first = false;
} else {
e += sprintf(e, " + ");
}
i = get_byte();
switch (i) {
case 1: // newline
e += sprintf(e, "newline()");
break;
case 2:
e += sprintf(e, "keepText()");
break;
case 3:
e += sprintf(e, "wait()");
break;
case 4: // addIntToStack
e += sprintf(e, "getInt(");
goto addVarToStack;
case 5: // addVerbToStack
e += sprintf(e, "getVerb(");
goto addVarToStack;
case 6: // addNameToStack
e += sprintf(e, "getName(");
goto addVarToStack;
case 7: // addStringToStack
e += sprintf(e, "getString(");
addVarToStack:
if (g_options.scriptVersion >= 6) {
e = get_var6(e);
} else {
e = get_var(e);
}
e += sprintf(e, ")");
break;
case 9:
e += sprintf(e, "startAnim(%d)", get_word());
break;
case 10:
e += sprintf(e, "sound(");
// positions 2, 3, 6, 7 are the offset in MONSTER.SOU (LE).
// positions 10, 11, 14, 15 are the VCTL block size (LE).
{
// show the voice's position in the MONSTER.SOU
unsigned int p = 0;
p += get_word() & 0xFFFF;
g_scriptCurPos += 2; // skip the next "0xFF 0x0A"
p += (get_word() & 0xFFFF) << 16;
e += sprintf(e, "0x%X, ", p);
g_scriptCurPos += 2; // skip the next "0xFF 0x0A"
// show the size of the VCTL chunk/lip-synch tags
p = 0;
p += get_word() & 0xFFFF;
g_scriptCurPos += 2; // skip the next "0xFF 0x0A"
p += (get_word() & 0xFFFF) << 16;
e += sprintf(e, "0x%X)", p);
}
break;
case 12:
e += sprintf(e, "setColor(%d)", get_word());
break;
case 13: // was unk2
e += sprintf(e, "unknown13(%d)", get_word());
break;
case 14:
e += sprintf(e, "setFont(%d)", get_word());
break;
case 32: // Workaround for a script bug in Indy3
case 46: // Workaround for a script bug in Indy3
if (g_options.scriptVersion == 3 && g_options.IndyFlag) {
buf += sprintf(buf, "\\x%.2X", 0xE1); // should output German "sz" in-game.
continue;
}
// fall-through
default:
e += sprintf(e, "unknown%d(%d)", i, get_word());
}
} else {
if (!in) {
if (first) {
first = false;
} else {
e += sprintf(e, " + ");
}
*e++ = '"';
in = true;
}
e = put_ascii(e, cmd);
}
}
if (in)
*e++ = '"';
*e = 0;
return e;
}
|