[Scummvm-git-logs] scummvm-tools master -> 7c830d80f89a6b42216d52b439a13f830406afe7

sev- sev at scummvm.org
Fri May 4 06:51:08 CEST 2018


This automated email contains information about 18 new commits which have been
pushed to the 'scummvm-tools' repo located at https://github.com/scummvm/scummvm-tools .

Summary:
734418f09a TOOLS: PRINCE: Simplified code and more references in decompilation
1afddf846e TOOLS: PRINCE: There are 63+1 rooms in skrypt.dat
8a98789345 TOOLS: PRINCE: More offsets to the decompilation
f161577144 TOOLS: PRINCE: More structures to decompilation
605929f218 TOOLS: PRINCE: Added two more scripts to decompilation
2e84451e25 TOOLS: PRINCE: Avoid duplicate decompilations
5c96bb8a4d TOOLS: PRINCE: Make difference between scripts and jump locations
ec925cc6a2 TOOLS: PRINCE: Added Databank class to utils
760dec652a TOOLS: PRINCE: Use Databank class in extractor
80debe2716 TOOLS: PRINCE: Added support of databank.ptc for decompiler
1a072a23f0 TOOLS: PRINCE: Added mode for dumping skrypt.dat file
75ee9c2a16 TOOLS: PRINCE: Implemented renum mode to decompiler
89766c952d TOOLS: PRINCE: Implement heuristics to decompile unused code
e5b7f0cd37 TOOLS: PRINCE: Fix bug in string output
6647353f67 TOOLS: PRINCE: Fix regression with missing jump labels
b9f14bd8c3 TOOLS: PRINCE: Print out label names where applicable in decompilation
9277637dc1 TOOLS: PRINCE: Fix duplicate labels
7c830d80f8 TOOLS: PRINCE: Correct calculated number of scripts


Commit: 734418f09a35513e80e2718cb5be52ec0392caac
    https://github.com/scummvm/scummvm-tools/commit/734418f09a35513e80e2718cb5be52ec0392caac
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T08:13:52+02:00

Commit Message:
TOOLS: PRINCE: Simplified code and more references in decompilation

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 81fb0a7..9b4aac9 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -378,12 +378,13 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 				break;
 			case 'o':
 				v = READ_LE_UINT32(&data[pos]); ADVANCES4();
+				v = pos + v - 4;
 
 				if (printOut) {
-					printf("%s[%d]", labels[pos + v - 4].c_str(), v);
+					printf("%s<%d>", labels[v].c_str(), v - pos + 4);
 				} else {
-					sprintf(buf, "script%06d", pos + v - 4);
-					decompile(buf, pos + v - 4);
+					sprintf(buf, "script%06d", v);
+					decompile(buf, v);
 				}
 
 				break;
@@ -409,11 +410,11 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 				break;
 			case 's':
 				v = READ_LE_UINT32(&data[pos]); ADVANCES4();
+				v = pos + v - 4;
 
 				if (printOut)
-					printf("\"%s\"[string%d]", &data[pos + v - 4], pos + v - 4);
+					printf("\"%s\"[string%d]", &data[v], v);
 
-				v = pos + v - 4;
 				sprintf(buf, "string%d", v);
 				labels[v] = buf;
 
@@ -612,7 +613,7 @@ void loadBackAnim(int anum, int offset, bool printOut) {
 	int data2 = READ_LE_UINT32(&data[pos]); ADVANCE4();
 
 	if (printOut)
-		printf("backanim%02d: type=%x data=%x anims=%x unk1=%x unk2=%x unk3=%x data2=%x\n", anum,
+		printf("backanim%02d[%d]: type=%x data=%x anims=%x unk1=%x unk2=%x unk3=%x data2=%x\n", anum, offset,
 			type, bdata, anims, unk1, unk2, unk3, data2);
 
 	if (anims == 0) {


Commit: 1afddf846e42d8c4abfa5a223b4a8e22f62b2e51
    https://github.com/scummvm/scummvm-tools/commit/1afddf846e42d8c4abfa5a223b4a8e22f62b2e51
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T08:30:25+02:00

Commit Message:
TOOLS: PRINCE: There are 63+1 rooms in skrypt.dat

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 9b4aac9..c345949 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -32,7 +32,7 @@
 
 #include <assert.h>
 
-static const int16 kMaxRooms = 60;
+static const int16 kMaxRooms = 63;
 static const int kMaxBackAnims = 64;
 static const int kMaxMobs = 64;
 static const int kMaxObjects = 64;
@@ -730,9 +730,9 @@ int main(int argc, char *argv[]) {
 	printf("stdGiveItem: %d\n", scriptInfo.stdGiveItem);
 	printf("goTester: %d\n", scriptInfo.goTester);
 
-	Room rooms[kMaxRooms];
+	Room rooms[kMaxRooms + 1];
 
-	for (int i = 0; i < kMaxRooms; i++) {
+	for (int i = 0; i < kMaxRooms + 1; i++) {
 		pos = scriptInfo.rooms + i * 64;
 
 		rooms[i].mobs = READ_LE_UINT32(&data[pos]); ADVANCE4();			// byte[kMaxMobs]


Commit: 8a98789345a441acda8878f838c0814d5d584c9f
    https://github.com/scummvm/scummvm-tools/commit/8a98789345a441acda8878f838c0814d5d584c9f
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T08:51:47+02:00

Commit Message:
TOOLS: PRINCE: More offsets to the decompilation

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index c345949..33bc1d1 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -517,14 +517,14 @@ void loadMask(int offset) {
 		tempMask._z = READ_LE_UINT16(&data[pos]); ADVANCE2();
 		tempMask._number = READ_LE_UINT16(&data[pos]); ADVANCE2();
 
+		printf("  mask%d[%d] state=%d fl=%d x1=%d y1=%d x2=%d y2=%d z=%d number=%d\n", n, pos-16, tempMask._state,
+					tempMask._flags, tempMask._x1, tempMask._y1, tempMask._x2, tempMask._y2,
+					tempMask._z, tempMask._number);
+
 		if (tempMask._state == 0xffff) {
 			break;
 		}
 
-		printf("  mask%d state=%d fl=%d x1=%d y1=%d x2=%d y2=%d z=%d number=%d\n", n, tempMask._state,
-					tempMask._flags, tempMask._x1, tempMask._y1, tempMask._x2, tempMask._y2,
-					tempMask._z, tempMask._number);
-
 		n++;
 	}
 }
@@ -544,11 +544,11 @@ void loadMobEvents(int offset, const char *name) {
 		mob = (int16)READ_LE_UINT16(&data[pos]); ADVANCE2();
 		code = READ_LE_UINT32(&data[pos]); ADVANCE4();
 
+		printf("  mob%02d[%d]: mob=%d code=%d\n", i, pos-6, mob, code);
+
 		if (mob == -1)
 			break;
 
-		printf("  mob%02d: mob=%d code=%d\n", i, mob, code);
-
 		sprintf(buf, "%s.mob%d", name, mob);
 		decompile(buf, code);
 
@@ -572,11 +572,12 @@ void loadMobEventsWithItem(int offset, const char *name) {
 		mob = (int16)READ_LE_UINT16(&data[pos]); ADVANCE2();
 		item = READ_LE_UINT16(&data[pos]); ADVANCE2();
 		code = READ_LE_UINT32(&data[pos]); ADVANCE4();
+
+		printf("  mobitem%02d[%d]: mob=%d item=%d code=%d\n", i, pos-8, mob, item, code);
+
 		if (mob == -1)
 			break;
 
-		printf("  mobitem%02d: mob=%d item=%d code=%d\n", i, mob, item, code);
-
 		sprintf(buf, "%s.mobitem%d", name, mob);
 		decompile(buf, code);
 
@@ -593,7 +594,7 @@ void loadLightSources(int offset) {
 		int scale = READ_LE_UINT16(&data[pos]); ADVANCE2();
 		int unk = READ_LE_UINT16(&data[pos]); ADVANCE2();
 
-		printf("  light%02d: x=%d y=%d scale=%d unk=%d\n", i, x, y, scale, unk);
+		printf("  light%02d[%d]: x=%d y=%d scale=%d unk=%d\n", i, pos-8, x, y, scale, unk);
 	}
 }
 
@@ -629,7 +630,7 @@ void loadBackAnim(int anum, int offset, bool printOut) {
 		int unk = READ_LE_UINT16(&data[pos]); ADVANCE2();
 
 		if (printOut)
-			printf("  backanim%02d.%d: num=%d start=%d end=%d unk=%d\n", anum, i, num, start, end, unk);
+			printf("  backanim%02d.%d[%d]: num=%d start=%d end=%d unk=%d\n", anum, pos-8, i, num, start, end, unk);
 	}
 }
 
@@ -727,13 +728,14 @@ int main(int argc, char *argv[]) {
 	printf("end lightSources\n");
 	printf("specRout: %d\n", scriptInfo.specRout);
 	printf("invObjGive: %d\n", scriptInfo.invObjGive);
-	printf("stdGiveItem: %d\n", scriptInfo.stdGiveItem);
+	printf("stdGiveItem: [%d]\n", scriptInfo.stdGiveItem);
 	printf("goTester: %d\n", scriptInfo.goTester);
 
 	Room rooms[kMaxRooms + 1];
 
 	for (int i = 0; i < kMaxRooms + 1; i++) {
 		pos = scriptInfo.rooms + i * 64;
+		printf("room%02d: [%d]", i, pos);
 
 		rooms[i].mobs = READ_LE_UINT32(&data[pos]); ADVANCE4();			// byte[kMaxMobs]
 		rooms[i].backAnim = READ_LE_UINT32(&data[pos]); ADVANCE4();		// int32[kMaxBackAnims]


Commit: f16157714477707ed88df870d8e98b862c3e69ef
    https://github.com/scummvm/scummvm-tools/commit/f16157714477707ed88df870d8e98b862c3e69ef
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T09:06:42+02:00

Commit Message:
TOOLS: PRINCE: More structures to decompilation

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 33bc1d1..be1e35f 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -630,7 +630,7 @@ void loadBackAnim(int anum, int offset, bool printOut) {
 		int unk = READ_LE_UINT16(&data[pos]); ADVANCE2();
 
 		if (printOut)
-			printf("  backanim%02d.%d[%d]: num=%d start=%d end=%d unk=%d\n", anum, pos-8, i, num, start, end, unk);
+			printf("  backanim%02d.%d[%d]: num=%d start=%d end=%d unk=%d\n", anum, i, pos-8, num, start, end, unk);
 	}
 }
 
@@ -727,7 +727,9 @@ int main(int argc, char *argv[]) {
 	loadLightSources(scriptInfo.lightSources);
 	printf("end lightSources\n");
 	printf("specRout: %d\n", scriptInfo.specRout);
-	printf("invObjGive: %d\n", scriptInfo.invObjGive);
+	printf("invObjGive: [%d]\n", scriptInfo.invObjGive);
+	loadMobEvents(scriptInfo.invObjGive, "invObjGive");
+	printf("end invObjGive\n");
 	printf("stdGiveItem: [%d]\n", scriptInfo.stdGiveItem);
 	printf("goTester: %d\n", scriptInfo.goTester);
 
@@ -735,7 +737,7 @@ int main(int argc, char *argv[]) {
 
 	for (int i = 0; i < kMaxRooms + 1; i++) {
 		pos = scriptInfo.rooms + i * 64;
-		printf("room%02d: [%d]", i, pos);
+		printf("room%02d: [%d]\n", i, pos);
 
 		rooms[i].mobs = READ_LE_UINT32(&data[pos]); ADVANCE4();			// byte[kMaxMobs]
 		rooms[i].backAnim = READ_LE_UINT32(&data[pos]); ADVANCE4();		// int32[kMaxBackAnims]


Commit: 605929f218315e1cdc4560143ca825301418b806
    https://github.com/scummvm/scummvm-tools/commit/605929f218315e1cdc4560143ca825301418b806
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T09:29:37+02:00

Commit Message:
TOOLS: PRINCE: Added two more scripts to decompilation

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index be1e35f..4583b87 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -726,12 +726,12 @@ int main(int argc, char *argv[]) {
 	printf("lightSources: [%d]\n", scriptInfo.lightSources);
 	loadLightSources(scriptInfo.lightSources);
 	printf("end lightSources\n");
-	printf("specRout: %d\n", scriptInfo.specRout);
+	printf("specRout: [%d]\n", scriptInfo.specRout);
 	printf("invObjGive: [%d]\n", scriptInfo.invObjGive);
 	loadMobEvents(scriptInfo.invObjGive, "invObjGive");
 	printf("end invObjGive\n");
 	printf("stdGiveItem: [%d]\n", scriptInfo.stdGiveItem);
-	printf("goTester: %d\n", scriptInfo.goTester);
+	printf("goTester: [%d]\n", scriptInfo.goTester);
 
 	Room rooms[kMaxRooms + 1];
 
@@ -828,6 +828,8 @@ int main(int argc, char *argv[]) {
 	decompile("usdCode", scriptInfo.usdCode);
 	decompile("stdUseItem", scriptInfo.stdUseItem);
 	decompile("stdGiveItem", scriptInfo.stdGiveItem);
+	decompile("specRout", scriptInfo.specRout);
+	decompile("goTester", scriptInfo.goTester);
 
 #if 1
 	int n = 0;


Commit: 2e84451e25d903b162457130dbd617c8b20f6d0f
    https://github.com/scummvm/scummvm-tools/commit/2e84451e25d903b162457130dbd617c8b20f6d0f
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T09:30:13+02:00

Commit Message:
TOOLS: PRINCE: Avoid duplicate decompilations

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 4583b87..1aea268 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -313,9 +313,6 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 	if (!printOut)
 		numscripts++;
 
-	if (printOut)
-		printf("%s: ; %d 0x%x\n", sname, pos, pos);
-
 	bool nf = false;
 	int tableOffset = -1;
 
@@ -327,6 +324,13 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 		if (!printOut && dataDecompile[pos])
 			break;
 
+		if (printOut) {
+			if (!labels[pos].empty()) {
+				printf("\n%s: ; %d 0x%x\n", labels[pos].c_str(), pos, pos);
+				labels[pos].clear();
+			}
+		}
+
 		uint16 op = READ_LE_UINT16(&data[pos]); ADVANCES2();
 
 		if (op >= ARRAYSIZE(opcodes))
@@ -469,7 +473,7 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 	}
 
 	if (tableOffset != -1 && printOut) {
-		printf("tableOffset: %d\n", tableOffset);
+		printf("\ntableOffset: %d\n", tableOffset);
 
 		printArray(tableOffset, 4, kMaxRooms, true, true);
 	}
@@ -482,9 +486,6 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 		}
 	}
 
-	if (printOut)
-		printf("\n");
-
 	if (tableOffset != -1) {
 		pos = tableOffset;
 


Commit: 5c96bb8a4d3641cf76387f07f7d737c28f84863f
    https://github.com/scummvm/scummvm-tools/commit/5c96bb8a4d3641cf76387f07f7d737c28f84863f
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T09:47:02+02:00

Commit Message:
TOOLS: PRINCE: Make difference between scripts and jump locations

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 1aea268..fb530b0 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -70,15 +70,15 @@ struct OpCodes {
 	{ "O_CLS", "r", false },
 	{ "O__CALL", "o", false },
 	{ "O_RETURN", "", true },			// 25
-	{ "O_GO", "o", false },
+	{ "O_GO", "O", false },
 	{ "O_BACKANIMUPDATEOFF", "f", false },
 	{ "O_BACKANIMUPDATEON", "f", false },
 	{ "O_CHANGECURSOR", "f", false },
 	{ "O_CHANGEANIMTYPE", "r", false },	// 30
 	{ "O__SETFLAG", "df", false },
 	{ "O_COMPARE", "df", false },
-	{ "O_JUMPZ", "o", false },
-	{ "O_JUMPNZ", "o", false },
+	{ "O_JUMPZ", "O", false },
+	{ "O_JUMPNZ", "O", false },
 	{ "O_EXIT", "", true },				// 35
 	{ "O_ADDFLAG", "df", false },
 	{ "O_TALKANIM", "ff", false },
@@ -306,8 +306,12 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 	if (pos == 0)
 		return;
 
-	if (labels[pos].empty() || labels[pos].hasPrefix("script")) {
-		labels[pos] = sname;
+	if (labels[pos].empty() || labels[pos].hasPrefix("script") || labels[pos].hasPrefix("loc")) {
+		if (labels[pos].hasPrefix("script") && !strncmp(sname, "loc", 3)) {
+			// do not override
+		} else {
+			labels[pos] = sname;
+		}
 	}
 
 	if (!printOut)
@@ -381,13 +385,14 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 					printf("%s", Flags::getFlagName(v));
 				break;
 			case 'o':
+			case 'O':
 				v = READ_LE_UINT32(&data[pos]); ADVANCES4();
 				v = pos + v - 4;
 
 				if (printOut) {
 					printf("%s<%d>", labels[v].c_str(), v - pos + 4);
 				} else {
-					sprintf(buf, "script%06d", v);
+					sprintf(buf, "%s%06d", (*param == 'o' ? "script" : "loc"), v);
 					decompile(buf, v);
 				}
 


Commit: ec925cc6a23fc1566d0e5aff673e228a433504db
    https://github.com/scummvm/scummvm-tools/commit/ec925cc6a23fc1566d0e5aff673e228a433504db
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T10:38:08+02:00

Commit Message:
TOOLS: PRINCE: Added Databank class to utils

Changed paths:
    engines/prince/utils.cpp
    engines/prince/utils.h


diff --git a/engines/prince/utils.cpp b/engines/prince/utils.cpp
index b46451d..6cee8ba 100644
--- a/engines/prince/utils.cpp
+++ b/engines/prince/utils.cpp
@@ -20,6 +20,7 @@
  */
 
 #include "common/scummsys.h"
+#include "common/endian.h"
 #include "utils.h"
 
 // John_Doe's implementation
@@ -164,3 +165,92 @@ int Decompressor::getBit() {
 	}
 	return bit;
 }
+
+Databank::Databank(Common::String name) {
+	_databank.open(name, "rb");
+
+	if (!_databank.isOpen())
+		return;
+
+	_databank.readUint32LE(); // magic
+	uint32 fileTableOffset = _databank.readUint32LE() ^ 0x4D4F4B2D; // MOK-
+	uint32 fileTableSize = _databank.readUint32LE() ^ 0x534F4654; // SOFT
+
+	_databank.seek(fileTableOffset, SEEK_SET);
+
+	byte *fileTable = (byte *)malloc(fileTableSize);
+	byte *fileTableEnd = fileTable + fileTableSize;
+	_databank.read_throwsOnError(fileTable, fileTableSize);
+
+	decrypt(fileTable, fileTableSize);
+
+	for (byte *fileItem = fileTable; fileItem < fileTableEnd; fileItem += 32) {
+		FileEntry item;
+		item._name = (const char *)fileItem;
+		item._offset = READ_LE_UINT32(fileItem + 24);
+		item._size = READ_LE_UINT32(fileItem + 28);
+		_items.push_back(item);
+	}
+
+	free(fileTable);
+}
+
+Databank::~Databank() {
+	_databank.close();
+}
+
+void Databank::decrypt(byte *buffer, uint32 size) {
+	uint32 key = 0xDEADF00D;
+	while (size--) {
+		*buffer++ += key & 0xFF;
+		key ^= 0x2E84299A;
+		key += 0x424C4148;
+		key = ((key & 1) << 31) | (key >> 1);
+	}
+}
+
+FileData Databank::loadFile(Common::String name) {
+	FileData fileData;
+	fileData._fileTable = 0;
+	fileData._size = 0;
+
+	int itemIndex = -1;
+
+	if (!_databank.isOpen())
+		return fileData;
+
+	for (size_t i = 0; i < _items.size(); i++) {
+		if (!scumm_stricmp(_items[i]._name.c_str(), name.c_str())) {
+			itemIndex = i;
+			break;
+		}
+	}
+
+	if (itemIndex == -1)
+		return fileData;
+
+	const FileEntry &entryHeader = _items[itemIndex];
+
+	if (entryHeader._size < 4) {
+		return fileData;
+	}
+
+	fileData._size = entryHeader._size;
+
+	_databank.seek(entryHeader._offset, SEEK_SET);
+
+	fileData._fileTable = (byte *)malloc(fileData._size);
+	_databank.read_throwsOnError(fileData._fileTable, fileData._size);
+
+	if (READ_BE_UINT32(fileData._fileTable) == 0x4D41534D) {
+		Decompressor dec;
+		uint32 decompLen = READ_BE_UINT32(fileData._fileTable + 14);
+		byte *decompData = (byte *)malloc(decompLen);
+		dec.decompress(fileData._fileTable + 18, decompData, decompLen);
+		free(fileData._fileTable);
+		fileData._size = decompLen;
+		fileData._fileTable = decompData;
+	}
+
+	return fileData;
+}
diff --git a/engines/prince/utils.h b/engines/prince/utils.h
index bf5d030..bbccddb 100644
--- a/engines/prince/utils.h
+++ b/engines/prince/utils.h
@@ -22,6 +22,14 @@
 #ifndef PRINCE_UTILS_H
 #define PRINCE_UTILS_H
 
+#include "common/file.h"
+#include "common/str.h"
+
+struct FileData {
+	byte *_fileTable;
+	uint32 _size;
+};
+
 class Decompressor {
 public:
 	void decompress(byte *source, byte *dest, uint32 destSize);
@@ -32,4 +40,25 @@ protected:
 	int getBit();
 };
 
+class Databank {
+	struct FileEntry {
+		Common::String _name;
+		uint32 _offset;
+		uint32 _size;
+	};
+
+public:
+	Databank(Common::String name);
+	~Databank();
+
+	byte *openDatabank();
+	static void decrypt(byte *buffer, uint32 size);
+
+	FileData loadFile(Common::String name);
+
+private:
+	Common::Array<FileEntry> _items;
+	Common::File _databank;
+};
+
 #endif


Commit: 760dec652a9379b6c14d854c6a97c0ddb945af7b
    https://github.com/scummvm/scummvm-tools/commit/760dec652a9379b6c14d854c6a97c0ddb945af7b
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T10:55:23+02:00

Commit Message:
TOOLS: PRINCE: Use Databank class in extractor

Changed paths:
    engines/prince/extract_prince.cpp
    engines/prince/extract_prince.h
    engines/prince/utils.cpp
    engines/prince/utils.h


diff --git a/engines/prince/extract_prince.cpp b/engines/prince/extract_prince.cpp
index 201550f..a8de572 100644
--- a/engines/prince/extract_prince.cpp
+++ b/engines/prince/extract_prince.cpp
@@ -60,49 +60,41 @@ void ExtractPrince::execute() {
 	// If the databank.ptc file is not found, we try to access the
 	// uncompressed files directly.
 	if (Common::Filename(databankFullName).exists()) {
-		_databank.open(filename, "rb");
+		_databank = new Databank(databankFullName);
 		print("DATABANK.PTC loaded, processing... ");
-		byte *fileTable = openDatabank();
 		bool mobsDE = false;
-		for (size_t i = 0; i < _items.size(); i++) {
-			if (!scumm_stricmp(_items[i]._name.c_str(), "variatxt.dat")) {
-				exportVariaTxt(loadFile(i));
-			}
-			if (!scumm_stricmp(_items[i]._name.c_str(), "invtxt.dat")) {
-				exportInvTxt(loadFile(i));
-			}
-			if (!scumm_stricmp(_items[i]._name.c_str(), "talktxt.dat")) {
-				exportTalkTxt(loadFile(i));
-			}
-			if (!scumm_stricmp(_items[i]._name.c_str(), "credits.dat")) {
-				exportCredits(loadFile(i));
+
+		exportVariaTxt(_databank->loadFile("variatxt.dat"));
+		exportInvTxt(_databank->loadFile("invtxt.dat"));
+		exportTalkTxt(_databank->loadFile("talktxt.dat"));
+		exportCredits(_databank->loadFile("credits.dat"));
+
+		int index = _databank->getFileIndex("mob01.lst");
+
+		if (index != -1) {
+			// For DE game data
+			mobsDE = true;
+			_outputPath.setFullName("mob.txt");
+			_fFiles.open(_outputPath, "w");
+			if (!_fFiles.isOpen()) {
+				error("Unable to create mob.txt");
 			}
-			if (!scumm_stricmp(_items[i]._name.c_str(), "mob01.lst")) {
-				// For DE game data
-				mobsDE = true;
-				_outputPath.setFullName("mob.txt");
-				_fFiles.open(_outputPath, "w");
-				if (!_fFiles.isOpen()) {
-					error("Unable to create mob.txt");
-				}
-				_fFiles.print("mob.lst\nmob_name - exam text\n");
-				int loc = 0;
-				for (int j = 1; j <= kNumberOfLocations; j++) {
-					_fFiles.print("%d.\n", j);
-					// no databanks for loc 44 and 45
-					if (j != 44 && j != 45) {
-						exportMobs(loadFile(i + loc));
-						loc++;
-					}
+			_fFiles.print("mob.lst\nmob_name - exam text\n");
+			int loc = 0;
+			for (int j = 1; j <= kNumberOfLocations; j++) {
+				_fFiles.print("%d.\n", j);
+				// no databanks for loc 44 and 45
+				if (j != 44 && j != 45) {
+					exportMobs(_databank->loadFile(index + loc));
+					loc++;
 				}
-				print("mob.txt - done");
-				print("All done!");
-				_fFiles.close();
 			}
+			print("mob.txt - done");
+			print("All done!");
+			_fFiles.close();
 		}
-		free(fileTable);
-		_databank.close();
-		_items.clear();
+
+		delete _databank;
 
 		// For PL game data
 		if (!mobsDE) {
@@ -120,20 +112,14 @@ void ExtractPrince::execute() {
 					sprintf(pathBuffer, "%02d/databank.ptc", loc);
 					databankFullName += pathBuffer;
 					filename = Common::Filename(databankFullName);
-					_databank.open(filename, "rb");
-					if (!_databank.isOpen()) {
+					_databank = new Databank(databankFullName);
+					if (!_databank->isOpen()) {
 						_fFiles.close();
 						error("Unable to open %02d/databank.ptc", loc);
 					}
-					fileTable = openDatabank();
-					for (size_t i = 0; i < _items.size(); i++) {
-						if (!scumm_stricmp(_items[i]._name.c_str(), "mob.lst")) {
-							exportMobs(loadFile(i));
-						}
-					}
-					free(fileTable);
-					_databank.close();
-					_items.clear();
+					exportMobs(_databank->loadFile("mob.lst"));
+
+					delete _databank;
 				}
 			}
 			print("mob.txt - done");
@@ -178,72 +164,8 @@ InspectionMatch ExtractPrince::inspectInput(const Common::Filename &filename) {
 	return IMATCH_PERFECT;
 }
 
-byte *ExtractPrince::openDatabank() {
-	_databank.readUint32LE(); // magic
-	uint32 fileTableOffset = _databank.readUint32LE() ^ 0x4D4F4B2D; // MOK-
-	uint32 fileTableSize = _databank.readUint32LE() ^ 0x534F4654; // SOFT
-
-	_databank.seek(fileTableOffset, SEEK_SET);
-
-	byte *fileTable = (byte *)malloc(fileTableSize);
-	byte *fileTableEnd = fileTable + fileTableSize;
-	_databank.read_throwsOnError(fileTable, fileTableSize);
-
-	decrypt(fileTable, fileTableSize);
-
-	for (byte *fileItem = fileTable; fileItem < fileTableEnd; fileItem += 32) {
-		FileEntry item;
-		item._name = (const char *)fileItem;
-		item._offset = READ_LE_UINT32(fileItem + 24);
-		item._size = READ_LE_UINT32(fileItem + 28);
-		_items.push_back(item);
-	}
-	return fileTable;
-}
-
-void ExtractPrince::decrypt(byte *buffer, uint32 size) {
-	uint32 key = 0xDEADF00D;
-	while (size--) {
-		*buffer++ += key & 0xFF;
-		key ^= 0x2E84299A;
-		key += 0x424C4148;
-		key = ((key & 1) << 31) | (key >> 1);
-	}
-}
-
-// DATABANK loader
-ExtractPrince::FileData ExtractPrince::loadFile(int itemIndex) {
-	FileData fileData;
-	fileData._fileTable = 0;
-	fileData._size = 0;
-
-	const FileEntry &entryHeader = _items[itemIndex];
-
-	if (entryHeader._size < 4) {
-		return fileData;
-	}
-
-	fileData._size = entryHeader._size;
-
-	_databank.seek(entryHeader._offset, SEEK_SET);
-
-	fileData._fileTable = (byte *)malloc(fileData._size);
-	_databank.read_throwsOnError(fileData._fileTable, fileData._size);
-
-	if (READ_BE_UINT32(fileData._fileTable) == 0x4D41534D) {
-		Decompressor dec;
-		uint32 decompLen = READ_BE_UINT32(fileData._fileTable + 14);
-		byte *decompData = (byte *)malloc(decompLen);
-		dec.decompress(fileData._fileTable + 18, decompData, decompLen);
-		free(fileData._fileTable);
-		fileData._size = decompLen;
-		fileData._fileTable = decompData;
-	}
-
-	return fileData;
-}
 // Uncompressed datafile loader
-ExtractPrince::FileData ExtractPrince::loadFile(const std::string &fileName) {
+FileData ExtractPrince::loadFile(const std::string &fileName) {
 	Common::File file;
 	file.open(fileName, "rb");
 	if (!file.isOpen()) {
diff --git a/engines/prince/extract_prince.h b/engines/prince/extract_prince.h
index 2dfa6e7..d632c17 100644
--- a/engines/prince/extract_prince.h
+++ b/engines/prince/extract_prince.h
@@ -23,8 +23,11 @@
 #define EXTRACT_PRINCE_H
 
 #include "tool.h"
+#include "utils.h"
 #include "common/array.h"
 
+class Databank;
+
 class ExtractPrince : public Tool {
 public:
 	ExtractPrince(const std::string &name = "extract_prince");
@@ -34,21 +37,6 @@ public:
 	virtual InspectionMatch inspectInput(const Common::Filename &filename);
 
 protected:
-	struct FileEntry {
-		std::string _name;
-		uint32 _offset;
-		uint32 _size;
-	};
-
-	struct FileData {
-		byte *_fileTable;
-		uint32 _size;
-	};
-
-	byte *openDatabank();
-	static void decrypt(byte *buffer, uint32 size);
-
-	FileData loadFile(int itemIndex);
 	FileData loadFile(const std::string &fileName);
 	char correctPolishLetter(char c);
 
@@ -61,8 +49,9 @@ protected:
 	byte *talkTxtNoDialog(byte *talkTxt);
 	bool printSpecialDialogData(byte c);
 
-	Common::Array<FileEntry> _items;
-	Common::File _databank, _fFiles;
+	Common::File _fFiles;
+
+	Databank *_databank;
 };
 
 #endif
diff --git a/engines/prince/utils.cpp b/engines/prince/utils.cpp
index 6cee8ba..a01cfb1 100644
--- a/engines/prince/utils.cpp
+++ b/engines/prince/utils.cpp
@@ -209,15 +209,11 @@ void Databank::decrypt(byte *buffer, uint32 size) {
 	}
 }
 
-FileData Databank::loadFile(Common::String name) {
-	FileData fileData;
-	fileData._fileTable = 0;
-	fileData._size = 0;
-
+int Databank::getFileIndex(Common::String name) {
 	int itemIndex = -1;
 
 	if (!_databank.isOpen())
-		return fileData;
+		return itemIndex;
 
 	for (size_t i = 0; i < _items.size(); i++) {
 		if (!scumm_stricmp(_items[i]._name.c_str(), name.c_str())) {
@@ -226,6 +222,27 @@ FileData Databank::loadFile(Common::String name) {
 		}
 	}
 
+	return itemIndex;
+}
+
+FileData Databank::loadFile(Common::String name) {
+	FileData fileData;
+	fileData._fileTable = 0;
+	fileData._size = 0;
+
+	int index = getFileIndex(name);
+
+	if (index == -1)
+		return fileData;
+
+	return loadFile(index);
+}
+
+FileData Databank::loadFile(int itemIndex) {
+	FileData fileData;
+	fileData._fileTable = 0;
+	fileData._size = 0;
+
 	if (itemIndex == -1)
 		return fileData;
 
diff --git a/engines/prince/utils.h b/engines/prince/utils.h
index bbccddb..de5fc4d 100644
--- a/engines/prince/utils.h
+++ b/engines/prince/utils.h
@@ -51,9 +51,13 @@ public:
 	Databank(Common::String name);
 	~Databank();
 
+	bool isOpen() { return _databank.isOpen(); }
+
 	byte *openDatabank();
 	static void decrypt(byte *buffer, uint32 size);
 
+	int getFileIndex(Common::String name);
+	FileData loadFile(int fileIndex);
 	FileData loadFile(Common::String name);
 
 private:


Commit: 80debe2716fecee102602edda617ed311b6bb14a
    https://github.com/scummvm/scummvm-tools/commit/80debe2716fecee102602edda617ed311b6bb14a
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T11:05:38+02:00

Commit Message:
TOOLS: PRINCE: Added support of databank.ptc for decompiler

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index fb530b0..a236ace 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -246,7 +246,7 @@ struct Mask {
 };
 
 void printUsage(const char *appName) {
-	printf("Usage: %s skrypt.dat\n", appName);
+	printf("Usage: %s skrypt.dat|databank.ptc\n", appName);
 }
 
 byte *data;
@@ -646,28 +646,45 @@ int main(int argc, char *argv[]) {
 		return 1;
 	}
 
-	Common::File scriptFile(argv[1], "rb");
-	if (!scriptFile.isOpen()) {
-		error("couldn't load file '%s'", argv[1]);
-		return 1;
-	}
+	Common::String fname = argv[1];
+	fname.toLowercase();
 
-	uint32 size = scriptFile.size();
-	uint8 *fdata = new uint8[size];
-	assert(fdata);
-	if (size != scriptFile.read_noThrow(fdata, size)) {
-		delete [] fdata;
-		error("couldn't read all bytes from file '%s'", argv[1]);
-		return 1;
-	}
+	if (fname.contains("databank.ptc")) {
+		Databank databank(argv[1]);
+		FileData fdata;
+
+		fdata = databank.loadFile("skrypt.dat");
 
-	scriptFile.close();
+		if (fdata._size == 0)
+			error("databank.ptc does not contain skrypt.dat");
 
-	Decompressor dec;
-	dataLen = READ_BE_UINT32(fdata + 14);
-	data = (byte *)malloc(dataLen);
-	dec.decompress(fdata + 18, data, dataLen);
-	delete [] fdata;
+		data = fdata._fileTable;
+		dataLen = fdata._size;
+	} else {
+		// Plain file
+		Common::File scriptFile(argv[1], "rb");
+		if (!scriptFile.isOpen()) {
+			error("couldn't load file '%s'", argv[1]);
+			return 1;
+		}
+
+		uint32 size = scriptFile.size();
+		uint8 *fdata = new uint8[size];
+		assert(fdata);
+		if (size != scriptFile.read_noThrow(fdata, size)) {
+			delete [] fdata;
+			error("couldn't read all bytes from file '%s'", argv[1]);
+			return 1;
+		}
+
+		scriptFile.close();
+
+		Decompressor dec;
+		dataLen = READ_BE_UINT32(fdata + 14);
+		data = (byte *)malloc(dataLen);
+		dec.decompress(fdata + 18, data, dataLen);
+		delete [] fdata;
+	}
 
 #if 0
 	Common::File dumpFile("skrypt.dump", "wb");


Commit: 1a072a23f04b0b75920312020f48728879f2842e
    https://github.com/scummvm/scummvm-tools/commit/1a072a23f04b0b75920312020f48728879f2842e
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T11:17:23+02:00

Commit Message:
TOOLS: PRINCE: Added mode for dumping skrypt.dat file

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index a236ace..d3b2295 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -246,7 +246,7 @@ struct Mask {
 };
 
 void printUsage(const char *appName) {
-	printf("Usage: %s skrypt.dat|databank.ptc\n", appName);
+	printf("Usage: %s skrypt.dat|databank.ptc [dump]\n", appName);
 }
 
 byte *data;
@@ -641,11 +641,18 @@ void loadBackAnim(int anum, int offset, bool printOut) {
 }
 
 int main(int argc, char *argv[]) {
-	if (argc != 2) {
+	if (argc < 2) {
 		printUsage(argv[0]);
 		return 1;
 	}
 
+	bool modeDump = false;
+
+	if (argc == 3) {
+		if (!scumm_stricmp(argv[2], "dump"))
+			modeDump = true;
+	}
+
 	Common::String fname = argv[1];
 	fname.toLowercase();
 
@@ -686,15 +693,15 @@ int main(int argc, char *argv[]) {
 		delete [] fdata;
 	}
 
-#if 0
-	Common::File dumpFile("skrypt.dump", "wb");
-	if (!dumpFile.isOpen()) {
-		error("couldn't load file '%s'", argv[1]);
-		return 1;
+	if (modeDump) {
+		Common::File dumpFile("skrypt.dump", "wb");
+		if (!dumpFile.isOpen()) {
+			error("couldn't load file '%s'", argv[1]);
+			return 1;
+		}
+		dumpFile.write(data, dataLen);
+		dumpFile.close();
 	}
-	dumpFile.write(data, dataLen);
-	dumpFile.close();
-#endif
 
 	dataMark = (bool *)calloc(dataLen, sizeof(bool));
 	dataDecompile = (bool *)calloc(dataLen, sizeof(bool));


Commit: 75ee9c2a16dd959efe3f1595391a04d58304445f
    https://github.com/scummvm/scummvm-tools/commit/75ee9c2a16dd959efe3f1595391a04d58304445f
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T11:56:32+02:00

Commit Message:
TOOLS: PRINCE: Implemented renum mode to decompiler

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index d3b2295..83ae1c1 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -246,7 +246,7 @@ struct Mask {
 };
 
 void printUsage(const char *appName) {
-	printf("Usage: %s skrypt.dat|databank.ptc [dump]\n", appName);
+	printf("Usage: %s skrypt.dat|databank.ptc [dump|renum]\n", appName);
 }
 
 byte *data;
@@ -255,6 +255,8 @@ bool *dataMark;
 bool *dataDecompile;
 int numscripts = 0;
 
+bool modeRenum = false;
+
 Common::String *labels;
 
 void loadBackAnim(int anum, int offset, bool printOut = true);
@@ -330,7 +332,10 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 
 		if (printOut) {
 			if (!labels[pos].empty()) {
-				printf("\n%s: ; %d 0x%x\n", labels[pos].c_str(), pos, pos);
+				if (modeRenum)
+					printf("\n%s:\n", labels[pos].c_str());
+				else
+					printf("\n%s: ; %d 0x%x\n", labels[pos].c_str(), pos, pos);
 				labels[pos].clear();
 			}
 		}
@@ -390,7 +395,10 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 				v = pos + v - 4;
 
 				if (printOut) {
-					printf("%s<%d>", labels[v].c_str(), v - pos + 4);
+					printf("%s", labels[v].c_str());
+
+					if (!modeRenum)
+						printf("<%d>", v);
 				} else {
 					sprintf(buf, "%s%06d", (*param == 'o' ? "script" : "loc"), v);
 					decompile(buf, v);
@@ -404,11 +412,13 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 					if (printOut)
 						printf("%d", v);
 				} else {
-					if (printOut)
-						printf("\"%s\"[string%d]", &data[v], v);
+					if (!printOut) {
+						sprintf(buf, "string%d", v);
+						labels[v] = buf;
+					}
 
-					sprintf(buf, "string%d", v);
-					labels[v] = buf;
+					if (printOut)
+						printf("\"%s\"[%s]", &data[v], labels[v].c_str());
 
 					while (data[v]) {
 						dataMark[v] = dataDecompile[v] = true;
@@ -421,11 +431,13 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 				v = READ_LE_UINT32(&data[pos]); ADVANCES4();
 				v = pos + v - 4;
 
-				if (printOut)
-					printf("\"%s\"[string%d]", &data[v], v);
+				if (!modeRenum) {
+					sprintf(buf, "string%d", v);
+					labels[v] = buf;
+				}
 
-				sprintf(buf, "string%d", v);
-				labels[v] = buf;
+				if (printOut)
+					printf("\"%s\"[%s]", &data[v], labels[v].c_str());
 
 				while (data[v]) {
 					dataMark[v] = dataDecompile[v] = true;
@@ -651,6 +663,8 @@ int main(int argc, char *argv[]) {
 	if (argc == 3) {
 		if (!scumm_stricmp(argv[2], "dump"))
 			modeDump = true;
+		if (!scumm_stricmp(argv[2], "renum"))
+			modeRenum = true;
 	}
 
 	Common::String fname = argv[1];
@@ -880,8 +894,26 @@ int main(int argc, char *argv[]) {
 	printf("\n");
 #endif
 
+	if (modeRenum) {
+		const char *pref[] = { "loc", "script", "string", 0 };
+
+		for (const char **p = pref; *p; p++) {
+			int nn = 1;
+			char buf[50];
+
+			for (int i = 0; i < dataLen; i++) {
+				if (!labels[i].empty() && labels[i].hasPrefix(*p)) {
+					sprintf(buf, "%s%d", *p, nn);
+					labels[i] = buf;
+					nn++;
+				}
+			}
+		}
+	}
+
 	bool inDB = false;
 	int nunmapped = 0;
+	int nlabel = 1;
 
 	for (int i = 0; i < dataLen; i++) {
 		if (!labels[i].empty() && !labels[i].hasPrefix("backanim")) {
@@ -899,7 +931,12 @@ int main(int argc, char *argv[]) {
 			nunmapped++;
 
 			if (!inDB) {
-				printf("label%d: ; 0x%x\n  db %d", i, i, data[i]);
+				if (modeRenum) {
+					printf("label%d:\n  db %d", nlabel, data[i]);
+					nlabel++;
+				} else {
+					printf("label%d: ; 0x%x\n  db %d", i, i, data[i]);
+				}
 				inDB = true;
 			} else {
 				printf(", %d", data[i]);


Commit: 89766c952ddfcf02453e916d6811e2530f0bdde2
    https://github.com/scummvm/scummvm-tools/commit/89766c952ddfcf02453e916d6811e2530f0bdde2
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T12:24:14+02:00

Commit Message:
TOOLS: PRINCE: Implement heuristics to decompile unused code

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 83ae1c1..37180b4 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -875,31 +875,56 @@ int main(int argc, char *argv[]) {
 	decompile("specRout", scriptInfo.specRout);
 	decompile("goTester", scriptInfo.goTester);
 
-#if 1
-	int n = 0;
-	const char *shades[] = {" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
-	for (uint i = 0; i < dataLen; i++) {
-		if (i % 8 == 0 && i) {
-			printf("%s", shades[n]);
-			n = 0;
-		}
+	char buf[100];
 
-		if (i % 800 == 0 && i)
-			printf("\n");
+	int nlabel = 1;
+
+	// Heuristics to decompile the rest
+	for (int i = 0; i < dataLen; i++) {
+		if (!dataMark[i]) {
+			if (i > 53000 && i < 124348 && READ_LE_UINT16(&data[i]) < 244) {
+				sprintf(buf, "unused%d", (modeRenum ? nlabel : i));
+				nlabel++;
+				decompile(buf, i);
+			} else if (i > 124348) {
+				if (data[i] && data[i] < 127) {
+					sprintf(buf, "unusedstring%d", i);
+					labels[i] = buf;
 
-		if (dataMark[i])
-			n++;
+					while (data[i] != 0) {
+						dataMark[i] = true;
+						i++;
+					}
+					dataMark[i] = true;
+				}
+			}
+		}
 	}
 
-	printf("\n");
-#endif
+	#if 1
+		int n = 0;
+		const char *shades[] = {" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
+		for (uint i = 0; i < dataLen; i++) {
+			if (i % 8 == 0 && i) {
+				printf("%s", shades[n]);
+				n = 0;
+			}
+
+			if (i % 800 == 0 && i)
+				printf("\n");
+
+			if (dataMark[i])
+				n++;
+		}
+
+		printf("\n");
+	#endif
 
 	if (modeRenum) {
-		const char *pref[] = { "loc", "script", "string", 0 };
+		const char *pref[] = { "loc", "script", "string", "unusedstring", 0 };
 
 		for (const char **p = pref; *p; p++) {
 			int nn = 1;
-			char buf[50];
 
 			for (int i = 0; i < dataLen; i++) {
 				if (!labels[i].empty() && labels[i].hasPrefix(*p)) {
@@ -911,9 +936,10 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
-	bool inDB = false;
 	int nunmapped = 0;
-	int nlabel = 1;
+	bool inDB = false;
+
+	nlabel = 1;
 
 	for (int i = 0; i < dataLen; i++) {
 		if (!labels[i].empty() && !labels[i].hasPrefix("backanim")) {
@@ -922,7 +948,7 @@ int main(int argc, char *argv[]) {
 				inDB = false;
 			}
 
-			if (labels[i].hasPrefix("string")) {
+			if (labels[i].hasPrefix("string") || labels[i].hasPrefix("unusedstring") ) {
 				printf("%s:\n  db \"%s\", 0\n", labels[i].c_str(), &data[i]);
 			} else {
 				decompile(labels[i].c_str(), i, true);


Commit: e5b7f0cd3704f7885dd850a6ca3dce014684cc16
    https://github.com/scummvm/scummvm-tools/commit/e5b7f0cd3704f7885dd850a6ca3dce014684cc16
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T12:27:54+02:00

Commit Message:
TOOLS: PRINCE: Fix bug in string output

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 37180b4..b157cfa 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -431,7 +431,7 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 				v = READ_LE_UINT32(&data[pos]); ADVANCES4();
 				v = pos + v - 4;
 
-				if (!modeRenum) {
+				if (!printOut) {
 					sprintf(buf, "string%d", v);
 					labels[v] = buf;
 				}


Commit: 6647353f67c269bff7aa5290eb3cf133f0e379fa
    https://github.com/scummvm/scummvm-tools/commit/6647353f67c269bff7aa5290eb3cf133f0e379fa
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T21:50:16+02:00

Commit Message:
TOOLS: PRINCE: Fix regression with missing jump labels

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index b157cfa..3036a90 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -304,16 +304,14 @@ void printArray(int offset, int type, int size, bool split = true, bool offsets
 	printf("]\n");
 }
 
-void decompile(const char *sname, int pos, bool printOut = false) {
+int decompile(const char *sname, int pos, bool printOut = false) {
 	if (pos == 0)
-		return;
+		return 0;
 
-	if (labels[pos].empty() || labels[pos].hasPrefix("script") || labels[pos].hasPrefix("loc")) {
-		if (labels[pos].hasPrefix("script") && !strncmp(sname, "loc", 3)) {
-			// do not override
-		} else {
-			labels[pos] = sname;
-		}
+	if (labels[pos].empty()) {
+		labels[pos] = sname;
+	} else if ((labels[pos].hasPrefix("script") || labels[pos].hasPrefix("loc")) && strncmp(sname, "loc", 3)) {
+		labels[pos] = sname;
 	}
 
 	if (!printOut)
@@ -336,7 +334,6 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 					printf("\n%s:\n", labels[pos].c_str());
 				else
 					printf("\n%s: ; %d 0x%x\n", labels[pos].c_str(), pos, pos);
-				labels[pos].clear();
 			}
 		}
 
@@ -473,10 +470,10 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 				break;
 			case 'r':
 				error("Unsupported op %s at %d (%x)", opcodes[op].name, pos - 2, pos - 2);
-				return;
+				return pos;
 			default:
 				error("Unhandled param '%c' for %s", *param, opcodes[op].name);
-				return;
+				return pos;
 			}
 
 			param++;
@@ -489,6 +486,8 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 			printf("\n");
 	}
 
+	int retpos = pos;
+
 	if (tableOffset != -1 && printOut) {
 		printf("\ntableOffset: %d\n", tableOffset);
 
@@ -514,6 +513,8 @@ void decompile(const char *sname, int pos, bool printOut = false) {
 			decompile(buf, off);
 		}
 	}
+
+	return retpos;
 }
 
 void loadMask(int offset) {
@@ -951,7 +952,7 @@ int main(int argc, char *argv[]) {
 			if (labels[i].hasPrefix("string") || labels[i].hasPrefix("unusedstring") ) {
 				printf("%s:\n  db \"%s\", 0\n", labels[i].c_str(), &data[i]);
 			} else {
-				decompile(labels[i].c_str(), i, true);
+				i = decompile(labels[i].c_str(), i, true) - 1; // -1 to compensate the for() loop increment
 			}
 		} else if (!dataMark[i]) {
 			nunmapped++;


Commit: b9f14bd8c368a10aafc20356d672794d187b184f
    https://github.com/scummvm/scummvm-tools/commit/b9f14bd8c368a10aafc20356d672794d187b184f
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T22:26:52+02:00

Commit Message:
TOOLS: PRINCE: Print out label names where applicable in decompilation

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index 3036a90..dbd52b1 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -548,7 +548,7 @@ void loadMask(int offset) {
 	}
 }
 
-void loadMobEvents(int offset, const char *name) {
+void loadMobEvents(int offset, const char *name, bool printOut) {
 	if (!offset)
 		return;
 
@@ -563,19 +563,22 @@ void loadMobEvents(int offset, const char *name) {
 		mob = (int16)READ_LE_UINT16(&data[pos]); ADVANCE2();
 		code = READ_LE_UINT32(&data[pos]); ADVANCE4();
 
-		printf("  mob%02d[%d]: mob=%d code=%d\n", i, pos-6, mob, code);
+		if (printOut)
+			printf("  mob%02d[%d]: mob=%d code=%s\n", i, pos-6, mob, (mob == -1 ? "0" : labels[code].c_str()));
 
 		if (mob == -1)
 			break;
 
-		sprintf(buf, "%s.mob%d", name, mob);
-		decompile(buf, code);
+		if (!printOut) {
+			sprintf(buf, "%s.mob%d", name, mob);
+			decompile(buf, code, printOut);
+		}
 
 		i++;
 	}
 }
 
-void loadMobEventsWithItem(int offset, const char *name) {
+void loadMobEventsWithItem(int offset, const char *name, bool printOut) {
 	if (!offset)
 		return;
 
@@ -592,13 +595,16 @@ void loadMobEventsWithItem(int offset, const char *name) {
 		item = READ_LE_UINT16(&data[pos]); ADVANCE2();
 		code = READ_LE_UINT32(&data[pos]); ADVANCE4();
 
-		printf("  mobitem%02d[%d]: mob=%d item=%d code=%d\n", i, pos-8, mob, item, code);
+		if (printOut)
+			printf("  mobitem%02d[%d]: mob=%d item=%d code=%s\n", i, pos-8, mob, item, (mob == -1 ? "0" : labels[code].c_str()));
 
 		if (mob == -1)
 			break;
 
-		sprintf(buf, "%s.mobitem%d", name, mob);
-		decompile(buf, code);
+		if (!printOut) {
+			sprintf(buf, "%s.mobitem%d", name, mob);
+			decompile(buf, code, printOut);
+		}
 
 		i++;
 	}
@@ -747,42 +753,19 @@ int main(int argc, char *argv[]) {
 	scriptInfo.stdGiveItem = READ_LE_UINT32(&data[pos]); ADVANCE4();
 	scriptInfo.goTester = READ_LE_UINT32(&data[pos]); ADVANCE4();
 
-	printf("rooms: [%d]\n", scriptInfo.rooms);
-	printf("startGame: [%d]\n", scriptInfo.startGame);
-	printf("restoreGame: [%d]\n", scriptInfo.restoreGame);
-	printf("stdExamine: [%d]\n", scriptInfo.stdExamine);
-	printf("stdPickup: [%d]\n", scriptInfo.stdPickup);
-	printf("stdUse: [%d]\n", scriptInfo.stdUse);
-	printf("stdOpen: [%d]\n", scriptInfo.stdOpen);
-	printf("stdClose: [%d]\n", scriptInfo.stdClose);
-	printf("stdTalk: [%d]\n", scriptInfo.stdTalk);
-	printf("stdGive: [%d]\n", scriptInfo.stdGive);
-	printf("usdCode: [%d]\n", scriptInfo.usdCode);
-	printf("invObjExam: [%d]\n", scriptInfo.invObjExam);
-	loadMobEvents(scriptInfo.invObjExam, "invObjExam");
-	printf("end invObjExam\n");
-	printf("invObjUse: [%d]\n", scriptInfo.invObjUse);
-	loadMobEvents(scriptInfo.invObjUse, "invObjUse");
-	printf("end invObjUse\n");
-	printf("invObjUU: [%d]\n", scriptInfo.invObjUU);
-	loadMobEventsWithItem(scriptInfo.invObjUU, "invObjUU");
-	printf("end invObjUU\n");
-	printf("stdUseItem: [%d]\n", scriptInfo.stdUseItem);
-	printf("lightSources: [%d]\n", scriptInfo.lightSources);
-	loadLightSources(scriptInfo.lightSources);
-	printf("end lightSources\n");
-	printf("specRout: [%d]\n", scriptInfo.specRout);
-	printf("invObjGive: [%d]\n", scriptInfo.invObjGive);
-	loadMobEvents(scriptInfo.invObjGive, "invObjGive");
-	printf("end invObjGive\n");
-	printf("stdGiveItem: [%d]\n", scriptInfo.stdGiveItem);
-	printf("goTester: [%d]\n", scriptInfo.goTester);
+	// Decompile offsets
+	loadMobEvents(scriptInfo.invObjExam, "invObjExam", false);
+	loadMobEvents(scriptInfo.invObjUse, "invObjUse", false);
+	loadMobEventsWithItem(scriptInfo.invObjUU, "invObjUU", false);
+	loadMobEvents(scriptInfo.invObjGive, "invObjGive", false);
+
+	char buf[100];
 
+	// Load rooms
 	Room rooms[kMaxRooms + 1];
 
 	for (int i = 0; i < kMaxRooms + 1; i++) {
 		pos = scriptInfo.rooms + i * 64;
-		printf("room%02d: [%d]\n", i, pos);
 
 		rooms[i].mobs = READ_LE_UINT32(&data[pos]); ADVANCE4();			// byte[kMaxMobs]
 		rooms[i].backAnim = READ_LE_UINT32(&data[pos]); ADVANCE4();		// int32[kMaxBackAnims]
@@ -801,64 +784,26 @@ int main(int argc, char *argv[]) {
 		rooms[i].unk1 = READ_LE_UINT32(&data[pos]); ADVANCE4();
 		rooms[i].unk2 = READ_LE_UINT32(&data[pos]); ADVANCE4();
 
-		char buf[100];
-
-		printf("r%02d mobs: [%d]: ", i, rooms[i].mobs);
-		printArray(rooms[i].mobs, 1, kMaxMobs, false);
-		printf("r%02d backAnim: [%d]: ", i, rooms[i].backAnim);
-		printArray(rooms[i].backAnim, 4, kMaxBackAnims, false);
-		printf("r%02d obj: [%d]: ", i, rooms[i].obj);
-		printArray(rooms[i].obj, 1, kMaxObjects, false);
-		printf("r%02d masks [%d]\n", i, rooms[i].nak);
-		loadMask(rooms[i].nak);
-		printf("end masks\n");
-		printf("r%02d itemUse: [%d]\n", i, rooms[i].itemUse);
 		sprintf(buf, "rooms%02d.itemUse", i);
-		loadMobEventsWithItem(rooms[i].itemUse, buf);
-		printf("end itemUse\n");
-		printf("r%02d itemGive: [%d]\n", i, rooms[i].itemGive);
+		loadMobEventsWithItem(rooms[i].itemUse, buf, false);
 		sprintf(buf, "rooms%02d.itemGive", i);
-		loadMobEventsWithItem(rooms[i].itemGive, buf);
-		printf("end itemGive\n");
-		printf("r%02d walkTo: [%d]\n", i, rooms[i].walkTo);
+		loadMobEventsWithItem(rooms[i].itemGive, buf, false);
 		sprintf(buf, "rooms%02d.walkTo", i);
-		loadMobEvents(rooms[i].walkTo, buf);
-		printf("end walkTo\n");
-		printf("r%02d examine: [%d]\n", i, rooms[i].examine);
+		loadMobEvents(rooms[i].walkTo, buf, false);
 		sprintf(buf, "rooms%02d.examine", i);
-		loadMobEvents(rooms[i].examine, buf);
-		printf("end examine\n");
-		printf("r%02d pickup: [%d]\n", i, rooms[i].pickup);
+		loadMobEvents(rooms[i].examine, buf, false);
 		sprintf(buf, "rooms%02d.pickup", i);
-		loadMobEvents(rooms[i].pickup, buf);
-		printf("end pickup\n");
-		printf("r%02d use: [%d]\n", i, rooms[i].use);
+		loadMobEvents(rooms[i].pickup, buf, false);
 		sprintf(buf, "rooms%02d.use", i);
-		loadMobEvents(rooms[i].use, buf);
-		printf("end use\n");
-		printf("r%02d pushOpen: [%d]\n", i, rooms[i].pushOpen);
+		loadMobEvents(rooms[i].use, buf, false);
 		sprintf(buf, "rooms%02d.pushOpen", i);
-		loadMobEvents(rooms[i].pushOpen, buf);
-		printf("end pushOpen\n");
-		printf("r%02d pullClose: [%d]\n", i, rooms[i].pullClose);
+		loadMobEvents(rooms[i].pushOpen, buf, false);
 		sprintf(buf, "rooms%02d.pullClose", i);
-		loadMobEvents(rooms[i].pullClose, buf);
-		printf("end pullClose\n");
-		printf("r%02d talk: [%d]\n", i, rooms[i].talk);
+		loadMobEvents(rooms[i].pullClose, buf, false);
 		sprintf(buf, "rooms%02d.talk", i);
-		loadMobEvents(rooms[i].talk, buf);
-		printf("end talk\n");
-		printf("r%02d give: [%d]\n", i, rooms[i].give);
+		loadMobEvents(rooms[i].talk, buf, false);
 		sprintf(buf, "rooms%02d.give", i);
-		loadMobEvents(rooms[i].give, buf);
-		printf("end give\n");
-		printf("r%02d unk1: %d\n", i, rooms[i].unk1);
-		printf("r%02d unk2: %d\n", i, rooms[i].unk2);
-
-		if (rooms[i].backAnim)
-			for (int b = 0; b < kMaxBackAnims; b++) {
-				loadBackAnim(b, READ_LE_UINT32(&data[rooms[i].backAnim + b * 4]));
-			}
+		loadMobEvents(rooms[i].give, buf, false);
 	}
 
 	decompile("startGame", scriptInfo.startGame);
@@ -876,8 +821,6 @@ int main(int argc, char *argv[]) {
 	decompile("specRout", scriptInfo.specRout);
 	decompile("goTester", scriptInfo.goTester);
 
-	char buf[100];
-
 	int nlabel = 1;
 
 	// Heuristics to decompile the rest
@@ -902,6 +845,101 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
+	// Print out header
+	printf("rooms: [%d]\n", scriptInfo.rooms);
+	printf("startGame: [%d]\n", scriptInfo.startGame);
+	printf("restoreGame: [%d]\n", scriptInfo.restoreGame);
+	printf("stdExamine: %s\n", labels[scriptInfo.stdExamine].c_str());
+	printf("stdPickup: %s\n", labels[scriptInfo.stdPickup].c_str());
+	printf("stdUse: %s\n", labels[scriptInfo.stdUse].c_str());
+	printf("stdOpen: %s\n", labels[scriptInfo.stdOpen].c_str());
+	printf("stdClose: %s\n", labels[scriptInfo.stdClose].c_str());
+	printf("stdTalk: %s\n", labels[scriptInfo.stdTalk].c_str());
+	printf("stdGive: %s\n", labels[scriptInfo.stdGive].c_str());
+	printf("usdCode: %s\n", labels[scriptInfo.usdCode].c_str());
+	printf("invObjExam: [%d]\n", scriptInfo.invObjExam);
+	loadMobEvents(scriptInfo.invObjExam, "invObjExam", true);
+	printf("end invObjExam\n");
+	printf("invObjUse: [%d]\n", scriptInfo.invObjUse);
+	loadMobEvents(scriptInfo.invObjUse, "invObjUse", true);
+	printf("end invObjUse\n");
+	printf("invObjUU: [%d]\n", scriptInfo.invObjUU);
+	loadMobEventsWithItem(scriptInfo.invObjUU, "invObjUU", true);
+	printf("end invObjUU\n");
+	printf("stdUseItem: %s\n", labels[scriptInfo.stdUseItem].c_str());
+	printf("lightSources: [%d]\n", scriptInfo.lightSources);
+	loadLightSources(scriptInfo.lightSources);
+	printf("end lightSources\n");
+	printf("specRout: %s\n", labels[scriptInfo.specRout].c_str());
+	printf("invObjGive: [%d]\n", scriptInfo.invObjGive);
+	loadMobEvents(scriptInfo.invObjGive, "invObjGive", true);
+	printf("end invObjGive\n");
+	printf("stdGiveItem: %s\n", labels[scriptInfo.stdGiveItem].c_str());
+	printf("goTester: %s\n", labels[scriptInfo.goTester].c_str());
+
+	// Print out rooms
+	for (int i = 0; i < kMaxRooms + 1; i++) {
+		pos = scriptInfo.rooms + i * 64;
+		printf("room%02d: [%d]\n", i, pos);
+
+		printf("r%02d mobs: [%d]: ", i, rooms[i].mobs);
+		printArray(rooms[i].mobs, 1, kMaxMobs, false);
+		printf("r%02d backAnim: [%d]: ", i, rooms[i].backAnim);
+		printArray(rooms[i].backAnim, 4, kMaxBackAnims, false);
+		printf("r%02d obj: [%d]: ", i, rooms[i].obj);
+		printArray(rooms[i].obj, 1, kMaxObjects, false);
+		printf("r%02d masks [%d]\n", i, rooms[i].nak);
+		loadMask(rooms[i].nak);
+		printf("end masks\n");
+		printf("r%02d itemUse: [%d]\n", i, rooms[i].itemUse);
+		sprintf(buf, "rooms%02d.itemUse", i);
+		loadMobEventsWithItem(rooms[i].itemUse, buf, true);
+		printf("end itemUse\n");
+		printf("r%02d itemGive: [%d]\n", i, rooms[i].itemGive);
+		sprintf(buf, "rooms%02d.itemGive", i);
+		loadMobEventsWithItem(rooms[i].itemGive, buf, true);
+		printf("end itemGive\n");
+		printf("r%02d walkTo: [%d]\n", i, rooms[i].walkTo);
+		sprintf(buf, "rooms%02d.walkTo", i);
+		loadMobEvents(rooms[i].walkTo, buf, true);
+		printf("end walkTo\n");
+		printf("r%02d examine: [%d]\n", i, rooms[i].examine);
+		sprintf(buf, "rooms%02d.examine", i);
+		loadMobEvents(rooms[i].examine, buf, true);
+		printf("end examine\n");
+		printf("r%02d pickup: [%d]\n", i, rooms[i].pickup);
+		sprintf(buf, "rooms%02d.pickup", i);
+		loadMobEvents(rooms[i].pickup, buf, true);
+		printf("end pickup\n");
+		printf("r%02d use: [%d]\n", i, rooms[i].use);
+		sprintf(buf, "rooms%02d.use", i);
+		loadMobEvents(rooms[i].use, buf, true);
+		printf("end use\n");
+		printf("r%02d pushOpen: [%d]\n", i, rooms[i].pushOpen);
+		sprintf(buf, "rooms%02d.pushOpen", i);
+		loadMobEvents(rooms[i].pushOpen, buf, true);
+		printf("end pushOpen\n");
+		printf("r%02d pullClose: [%d]\n", i, rooms[i].pullClose);
+		sprintf(buf, "rooms%02d.pullClose", i);
+		loadMobEvents(rooms[i].pullClose, buf, true);
+		printf("end pullClose\n");
+		printf("r%02d talk: [%d]\n", i, rooms[i].talk);
+		sprintf(buf, "rooms%02d.talk", i);
+		loadMobEvents(rooms[i].talk, buf, true);
+		printf("end talk\n");
+		printf("r%02d give: [%d]\n", i, rooms[i].give);
+		sprintf(buf, "rooms%02d.give", i);
+		loadMobEvents(rooms[i].give, buf, true);
+		printf("end give\n");
+		printf("r%02d unk1: %d\n", i, rooms[i].unk1);
+		printf("r%02d unk2: %d\n", i, rooms[i].unk2);
+
+		if (rooms[i].backAnim)
+			for (int b = 0; b < kMaxBackAnims; b++) {
+				loadBackAnim(b, READ_LE_UINT32(&data[rooms[i].backAnim + b * 4]));
+			}
+	}
+
 	#if 1
 		int n = 0;
 		const char *shades[] = {" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};


Commit: 9277637dc14a6749c8f3331e699becf9c83ddcc4
    https://github.com/scummvm/scummvm-tools/commit/9277637dc14a6749c8f3331e699becf9c83ddcc4
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T22:42:22+02:00

Commit Message:
TOOLS: PRINCE: Fix duplicate labels

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index dbd52b1..bf7c4c7 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -570,7 +570,7 @@ void loadMobEvents(int offset, const char *name, bool printOut) {
 			break;
 
 		if (!printOut) {
-			sprintf(buf, "%s.mob%d", name, mob);
+			sprintf(buf, "%s.mob%d", name, i);
 			decompile(buf, code, printOut);
 		}
 
@@ -602,7 +602,7 @@ void loadMobEventsWithItem(int offset, const char *name, bool printOut) {
 			break;
 
 		if (!printOut) {
-			sprintf(buf, "%s.mobitem%d", name, mob);
+			sprintf(buf, "%s.mobitem%d", name, i);
 			decompile(buf, code, printOut);
 		}
 


Commit: 7c830d80f89a6b42216d52b439a13f830406afe7
    https://github.com/scummvm/scummvm-tools/commit/7c830d80f89a6b42216d52b439a13f830406afe7
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2018-05-03T22:47:50+02:00

Commit Message:
TOOLS: PRINCE: Correct calculated number of scripts

Changed paths:
    engines/prince/deprince.cpp


diff --git a/engines/prince/deprince.cpp b/engines/prince/deprince.cpp
index bf7c4c7..ecac57b 100644
--- a/engines/prince/deprince.cpp
+++ b/engines/prince/deprince.cpp
@@ -314,9 +314,6 @@ int decompile(const char *sname, int pos, bool printOut = false) {
 		labels[pos] = sname;
 	}
 
-	if (!printOut)
-		numscripts++;
-
 	bool nf = false;
 	int tableOffset = -1;
 
@@ -991,6 +988,7 @@ int main(int argc, char *argv[]) {
 				printf("%s:\n  db \"%s\", 0\n", labels[i].c_str(), &data[i]);
 			} else {
 				i = decompile(labels[i].c_str(), i, true) - 1; // -1 to compensate the for() loop increment
+				numscripts++;
 			}
 		} else if (!dataMark[i]) {
 			nunmapped++;





More information about the Scummvm-git-logs mailing list