2021-05-30 21:43:56 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
2021-05-31 22:09:59 +02:00
|
|
|
// Heavily based on code by X
|
2021-05-30 21:43:56 +02:00
|
|
|
|
2021-04-20 18:53:43 -03:00
|
|
|
#include "private/decompiler.h"
|
2021-04-19 01:30:08 +02:00
|
|
|
|
|
|
|
namespace Private {
|
|
|
|
|
2021-06-20 10:24:25 +02:00
|
|
|
const char *kHeader = "Precompiled Game Matrix";
|
|
|
|
const uint kHeaderSize = 23;
|
|
|
|
|
2021-04-20 18:53:43 -03:00
|
|
|
Decompiler::Decompiler(char *buf, uint32 fileSize, bool mac) {
|
2021-04-19 01:30:08 +02:00
|
|
|
|
2021-06-20 09:47:48 +02:00
|
|
|
Common::Array<byte> array;
|
2021-04-20 18:53:43 -03:00
|
|
|
uint32 i = 0;
|
|
|
|
while (i < fileSize) {
|
|
|
|
array.push_back(buf[i]);
|
|
|
|
i++;
|
|
|
|
}
|
2021-04-21 21:05:51 -03:00
|
|
|
|
2021-06-20 10:24:25 +02:00
|
|
|
Common::String firstBytes((const char *)array.begin(), (const char *)array.begin() + kHeaderSize);
|
2021-06-20 09:01:33 +02:00
|
|
|
|
2021-04-21 21:05:51 -03:00
|
|
|
if (firstBytes != kHeader) {
|
|
|
|
debug("Not a precompiled game matrix");
|
|
|
|
_result = Common::String(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:53:43 -03:00
|
|
|
decompile(array, mac);
|
2021-04-19 01:30:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 09:47:48 +02:00
|
|
|
void Decompiler::decompile(Common::Array<byte> &buffer, bool mac) {
|
|
|
|
Common::Array<byte>::iterator it = buffer.begin();
|
2021-06-20 09:01:33 +02:00
|
|
|
|
2021-04-20 18:53:43 -03:00
|
|
|
Common::String ss;
|
2021-04-19 01:30:08 +02:00
|
|
|
bool inDefineRects = false;
|
2021-06-20 10:24:25 +02:00
|
|
|
for (it += kHeaderSize; it != buffer.end();) {
|
2021-06-20 09:47:48 +02:00
|
|
|
byte b = *it++;
|
|
|
|
if (b == kCodeString) {
|
|
|
|
byte len = *it++;
|
2021-06-20 09:01:33 +02:00
|
|
|
Common::String s((const char *)it, (const char *)it + len);
|
2021-04-19 01:30:08 +02:00
|
|
|
it += len;
|
2021-06-20 09:01:33 +02:00
|
|
|
ss += Common::String::format("\"%s\"", s.c_str());
|
2021-06-20 09:47:48 +02:00
|
|
|
} else if (b == kCodeShortLiteral || b == kCodeShortId) {
|
|
|
|
byte b1 = *it++;
|
|
|
|
byte b2 = *it++;
|
|
|
|
uint number = mac ? b2 + (b1 << 8) : b1 + (b2 << 8);
|
|
|
|
if (b == kCodeShortId)
|
2021-06-20 09:01:33 +02:00
|
|
|
ss += "k";
|
|
|
|
ss += Common::String::format("%d", number);
|
2021-06-20 09:47:48 +02:00
|
|
|
} else if (b == kCodeRect && inDefineRects) {
|
2021-04-20 18:53:43 -03:00
|
|
|
ss += "RECT"; // override CRect
|
2021-06-20 09:47:48 +02:00
|
|
|
} else if (b <= kCodeShortId && strlen(kCodeTable[b]) > 0) {
|
|
|
|
ss += kCodeTable[b];
|
2021-04-19 01:30:08 +02:00
|
|
|
} else {
|
2021-06-20 09:47:48 +02:00
|
|
|
error("decompile(): Unknown byte code (%d %c)", b, b);
|
2021-04-19 01:30:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 09:47:48 +02:00
|
|
|
if (b == kCodeRects) {
|
2021-04-19 01:30:08 +02:00
|
|
|
inDefineRects = true;
|
2021-06-20 09:47:48 +02:00
|
|
|
} else if (b == kCodeBraceClose && inDefineRects) {
|
2021-04-19 01:30:08 +02:00
|
|
|
inDefineRects = false;
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 18:53:43 -03:00
|
|
|
_result = ss;
|
2021-04-19 01:30:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:53:43 -03:00
|
|
|
Common::String Decompiler::getResult() const {
|
|
|
|
return _result;
|
2021-04-19 01:30:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 09:01:33 +02:00
|
|
|
} // namespace Private
|