[Scummvm-git-logs] scummvm master -> f48b2bb51f9ae423fc12228956f3afe98136147a

lephilousophe noreply at scummvm.org
Sun Nov 15 17:37:12 UTC 2026


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .

Summary:
f48b2bb51f SNATCHER: Split fan translations code for detection plugin


Commit: f48b2bb51f9ae423fc12228956f3afe98136147a
    https://github.com/scummvm/scummvm/commit/f48b2bb51f9ae423fc12228956f3afe98136147a
Author: Le Philousophe (lephilousophe at users.noreply.github.com)
Date: 2026-11-15T18:30:43+01:00

Commit Message:
SNATCHER: Split fan translations code for detection plugin

Changed paths:
  A engines/snatcher/fantranslations_detection.cpp
  A engines/snatcher/fantranslations_detection.h
    engines/snatcher/detection.cpp
    engines/snatcher/fantranslations.cpp
    engines/snatcher/fantranslations.h
    engines/snatcher/module.mk


diff --git a/engines/snatcher/detection.cpp b/engines/snatcher/detection.cpp
index e103f42884a..658650479ec 100644
--- a/engines/snatcher/detection.cpp
+++ b/engines/snatcher/detection.cpp
@@ -28,7 +28,7 @@
 
 #include "snatcher/detection.h"
 #include "snatcher/detection_tables.h"
-#include "snatcher/fantranslations.h"
+#include "snatcher/fantranslations_detection.h"
 
 namespace Snatcher {
 
@@ -64,7 +64,7 @@ DetectedGames SnatcherMetaEngineDetection::detectGames(const Common::FSList &fsl
 		if (!game.gameId.equals("snatcher"))
 			continue;
 
-		Common::List<Common::Language> langList = FanTranslationHandler::listTranslations(game.platform);
+		Common::List<Common::Language> langList = FanTranslationHandlerDetection::listTranslations(game.platform);
 
 		if (!langList.empty()) {
 			langList.push_back(game.language);
@@ -85,7 +85,7 @@ Common::Error SnatcherMetaEngineDetection::identifyGame(DetectedGame &game, cons
 		Common::Platform platform = Common::parsePlatform(ConfMan.get("platform"));
 
 		if (selectedlang != Common::UNK_LANG && platform != Common::kPlatformUnknown) {
-			Common::List<Common::Language> langList = FanTranslationHandler::listTranslations(platform, &defaultLang);
+			Common::List<Common::Language> langList = FanTranslationHandlerDetection::listTranslations(platform, &defaultLang);
 			for (Common::List<Common::Language>::const_iterator i = langList.begin(); i != langList.end(); ++i) {
 				if (*i == selectedlang)
 					supportedFanLang = true;
diff --git a/engines/snatcher/fantranslations.cpp b/engines/snatcher/fantranslations.cpp
index b190d3a5ddb..5451edf9f31 100644
--- a/engines/snatcher/fantranslations.cpp
+++ b/engines/snatcher/fantranslations.cpp
@@ -63,9 +63,6 @@ FanTranslationHandler::~FanTranslationHandler() {
 }
 
 const uint8 *FanTranslationHandler::process(uint32 addr, const uint8 *data) {
-// The fantranslation.o module is included in the detection plugin, but that only needs the static functions (and not
-// this one here). We add an #ifdef guard here, since the MemMapping dependency isn't included in the detection plugin.
-#ifdef ENABLE_SNATCHER
 	if (_mode == kTranslate && _gameTranslateableData) {
 		for (int i = 0; i < 100; ++i) {
 			if (!_gameTranslateableData[i])
@@ -82,7 +79,6 @@ const uint8 *FanTranslationHandler::process(uint32 addr, const uint8 *data) {
 		int fileNo = (addr < MemMapping::MEM_SCRIPT_LOCATION) ? _scdFileNo : (addr >= MemMapping::MEM_MODULE_LOCATION ? _gfxFileNo : _scriptFileNo);
 		updateDataTable(addr, data, _gameTranslateableData, _mode, 256, fileNo);
 	}
-#endif
 	return data;
 }
 
@@ -110,393 +106,12 @@ void FanTranslationHandler::loadFile(int number, ResourceType rtype) {
 	parseTranslations(Common::kPlatformUnknown, Common::UNK_LANG, _mode, number, _gameTranslateableData, nullptr, nullptr, _fileOffsetList, nullptr);
 }
 
-Common::List<Common::Language> FanTranslationHandler::listTranslations(Common::Platform platform, Common::Language *defaultLang) {
-	Common::List<Common::Language> res;
-	parseTranslations(platform, Common::UNK_LANG, kDetect, -1, nullptr, &res, defaultLang, nullptr, nullptr);
-	return res;
-}
-
 const FanTranslationHandler::FontProperties &FanTranslationHandler::getFontProperties() const {
 	if (_fontProperties == nullptr)
 		error("%s(): Font properties not initialized", __FUNCTION__);
 	return *_fontProperties;
 }
 
-void FanTranslationHandler::updateDataTable(uint32 addr, const uint8 *data, PerFileTranslateableData **table, TranslatorMode mode, uint dataSize, int file) {
-	Common::String tmp(reinterpret_cast<const char *>(data), dataSize);
-	size_t tt = tmp.findFirstOf(-1);
-	tmp.chop(tmp.size() - tt - 1);
-
-	if (mode == kCollect) {
-		for (uint i = 0; i < tmp.size(); ++i) {
-			uint8 c = tmp[i];
-			if (c < 32 || c > 127) {
-				tmp.replace(i, 1, Common::String::format("\\x%02X", static_cast<uint8>(c)));
-				i += 3;
-			} else if (c == '\"') {
-				tmp.replace(i, 1, "\\\"");
-				i++;
-			}
-		}
-	}
-
-	assert(table);
-	if (!table[file])
-		table[file] = new PerFileTranslateableData();
-
-	PerFileTranslateableData &d = *table[file];
-
-	Common::List<Common::String>::iterator j = d.stringTable.begin();
-	for (Common::List<uint32>::iterator i = d.addressTable.begin(); i != d.addressTable.end(); ++i, ++j) {
-		if (addr == *i) {
-			addr = 0;
-			break;
-		}
-		if (addr < *i) {
-			d.addressTable.insert(i, addr);
-			d.stringTable.insert(j, tmp);
-			if (file == -1)
-				debug("0x%06x=\"%s\"", addr, tmp.c_str());
-			addr = 0;
-			break;
-		}
-	}
-
-	if (addr != 0) {
-		d.addressTable.push_back(addr);
-		d.stringTable.push_back(tmp);
-		if (file == -1)
-			debug("0x%06x=\"%s\"", addr, tmp.c_str());
-	}
-}
-
-const struct CPMapping {
-	Common::CodePage cp;
-	const char str[5];
-} cpMapping[] = {
-	{Common::kWindows1250, "1250"},
-	{Common::kWindows1251, "1251"},
-	{Common::kWindows1252, "1252"},
-	{Common::kWindows1253, "1253"},
-	{Common::kWindows1254, "1254"},
-	{Common::kWindows1255, "1255"},
-	{Common::kWindows1256, "1256"},
-	{Common::kWindows1257, "1257"},
-	{Common::kWindows932, "932"},
-	{Common::kWindows936, "936"},
-	{Common::kWindows949, "949"},
-	{Common::kWindows950, "950"},
-	{Common::kDos850, "850"},
-	{Common::kDos862, "862"},
-	{Common::kDos866, "866"},
-};
-
-void FanTranslationHandler::parseTranslations(Common::Platform platform, Common::Language lang, TranslatorMode mode, int fileNumber,
-	PerFileTranslateableData **table, Common::List<Common::Language> *langList, Common::Language *defaultLang, uint32 *fileOffsetList, FontProperties *fontProperties) {
-
-	Common::Path fileName = Common::Path("snatcher_translations.dat");
-	Common::File file;
-	if (!file.open(fileName)) {
-
-		// Some lower level magic for the detection/metaengine phase where the normal file search hasn't been initiated yet.
-		const char *keys[] = {"path", "extrapath"};
-		for (int i = 0; i < ARRAYSIZE(keys); ++i) {
-			if (ConfMan.hasKey(keys[i])) {
-				Common::Path filePath(ConfMan.getPath(keys[i]));
-				Common::FSList list;
-				Common::FSNode(filePath).getChildren(list, Common::FSNode::kListFilesOnly);
-				for (Common::FSList::const_iterator it = list.begin(); it != list.end(); ++it) {
-					if (it->getName().equalsIgnoreCase(fileName.toString())) {
-						file.open(*it);
-						break;
-					}
-				}
-				if (file.isOpen())
-					break;
-			}
-		}
-
-		if (!file.isOpen())
-			return;
-	}
-
-	if (platform == Common::Platform::kPlatformUnknown && !fileOffsetList)
-		error("%s(): Platform must be specified to load a translation", __FUNCTION__);
-
-	uint16 blockSize = 5000;
-	uint16 extraBytes = 5;
-	uint8 *buff = new uint8[blockSize + extraBytes]();
-
-	bool leftPos = true;
-	bool skipLine = false;
-	int parseTag = 0;
-	bool parseFile = false;
-	bool parseMapping = false;
-	bool parseText = false;
-	bool saveOffset = false;
-	bool isDefault = false;
-
-	Common::String tmp;
-	Common::String versionStr;
-	Common::String platformStr;
-	Common::String languageStr;
-	Common::String fileNoStr;
-	Common::String currentLine;
-	Common::String extraCp;
-	Common::String extraRTL;
-	Common::String extraFont;
-	Common::Language *defLang = nullptr;
-
-	int fileNo = -1;
-	uint32 addr = 0;
-	Common::String *dstr = &tmp;
-	uint16 bytesLeft = file.size();
-	uint16 readSize = 0;
-	int diff = 0;
-
-	if (fileNumber != -1 && fileOffsetList != nullptr && (uint)fileNumber < 100) {
-		uint32 offs = fileOffsetList[fileNumber];
-		if (offs == 0)	// Many module files do not actually contain any text.
-			return;
-		file.seek(offs);
-		bytesLeft -= offs;
-		fileNo = fileNumber;
-		parseMapping = true;
-		dstr = ¤tLine;
-	}
-
-	while (bytesLeft > 0) {
-		diff = MAX<int>(readSize + diff - blockSize, 0);
-		if (diff)
-			memmove(buff, buff + blockSize + extraBytes - diff, diff);
-		readSize = file.read(buff + diff, blockSize + extraBytes - diff);
-		bytesLeft -= readSize;
-		if (bytesLeft == 0)
-			extraBytes = 0;
-
-		const char *p = reinterpret_cast<const char *>(buff);
-		const char *last = nullptr;
-		const char *end = p + readSize - extraBytes + diff;
-
-		while (p < end) {
-			if (p == last) // avoid infinite loop on invalid data
-				error("%s(): Fan translation file '%s' is corrupt or invalid. Error at pos: 0x%04x", __FUNCTION__, fileName.toString().c_str(), static_cast<int>(file.pos()) - readSize - diff + static_cast<uint32>(reinterpret_cast<const uint8*>(p) - buff));
-			last = p;
-
-			if ((leftPos || parseTag || parseFile) && (*p == ' ' || *p == '*')) {
-				if (*p++ == '*') {
-					if (leftPos && !parseFile) {
-						fileNo = -1;
-						parseFile = true;
-					}
-					if (!fileNoStr.empty()) {
-						parseMapping = true;
-						fileNo = fileNoStr.asUint64();
-						fileNoStr.clear();
-						dstr = ¤tLine;
-						parseFile = false;
-						saveOffset = (platform != Common::kPlatformUnknown && lang != Common::UNK_LANG && mode == kTranslate && fileOffsetList != nullptr && fileNumber == -1);
-					}
-				}
-				continue;
-			}
-			if (*p == '\r' || *p == '\n') {
-				skipLine = false;
-				if (!fileNoStr.empty()) {
-					parseMapping = true;
-					fileNo = fileNoStr.asUint64();
-					fileNoStr.clear();
-					dstr = ¤tLine;
-					parseFile = false;
-					saveOffset = (platform != Common::kPlatformUnknown && lang != Common::UNK_LANG && mode == kTranslate && fileOffsetList != nullptr && fileNumber == -1);
-				}
-				if (parseText) {
-					if (currentLine.lastChar() != '\xFF')
-						currentLine += '\xFF';
-					if ((mode == kCollect || fileNumber != -1) && table != nullptr)
-						updateDataTable(addr, reinterpret_cast<const uint8*>(currentLine.c_str()), table, mode, currentLine.size(), fileNo);
-					addr = 0;
-					parseText = false;
-				}
-				currentLine.clear();
-				leftPos = true;
-				++p;
-				if (saveOffset)
-					fileOffsetList[fileNo] = file.pos() - readSize - diff + static_cast<uint32>(reinterpret_cast<const uint8*>(p) - buff);
-				saveOffset = false;
-				continue;
-			}
-			leftPos = false;
-			if (*p == ';') {
-				skipLine = true;
-				++p;
-				continue;
-			}
-			if (skipLine) {
-				++p;
-				continue;
-			}
-			if (*p == '[') {
-				if (++parseTag == 1) {
-					platformStr.clear();
-					languageStr.clear();
-					versionStr.clear();
-					extraCp.clear();
-					extraRTL.clear();
-					extraFont.clear();
-					tmp.clear();
-					fileNo = -1;
-					parseFile = false;
-					isDefault = false;
-					dstr = &tmp;
-					if (fileNumber != -1) {
-						bytesLeft = 0;
-						p = end;
-					}
-				}
-				if (parseTag == 3) {
-					tmp.clear();
-					dstr = &tmp;
-				}
-				++p;
-				continue;
-			}
-			if (*p == ']') {
-				if (--parseTag == 0) {
-					if (!versionStr.empty()) {
-						int version = versionStr.asUint64();
-						if (version < SNATCHER_FANTRANSLATIONS_CURRENT_VERSION)
-							warning("Translation file '%s' is outdated (version %d, expected %d)", fileName.toString().c_str(), version, SNATCHER_FANTRANSLATIONS_CURRENT_VERSION);
-						versionStr.clear();
-					}
-					if (Common::parsePlatform(platformStr) == platform) {
-						if ((mode == kCollect && lang == Common::UNK_LANG) || fileNumber != -1 || (lang != Common::UNK_LANG && (Common::parseLanguage(languageStr) == lang || Common::parseLanguage(languageStr.substr(0, 2)) == lang))) {
-							parseFile = true;
-						} else if (mode == kDetect && lang == Common::UNK_LANG) {
-							Common::Language l = Common::parseLanguage(languageStr);
-							if (l == Common::UNK_LANG)
-								l = Common::parseLanguage(languageStr.substr(0, 2));
-							if (l != Common::UNK_LANG) {
-								if (defLang != nullptr)
-									*defLang = l;
-								else if (langList != nullptr)
-									langList->push_back(l);
-							}
-						}
-						languageStr.clear();
-						defLang = nullptr;
-					}
-				}
-				if (parseTag == 2) {
-					if (isDefault)
-						defLang = defaultLang;
-					bool processResult = (defLang == nullptr && (Common::parseLanguage(languageStr) != Common::UNK_LANG || Common::parseLanguage(languageStr.substr(0, 2)) != Common::UNK_LANG) && fileOffsetList != nullptr);
-					if (!extraCp.empty()) {
-						if (processResult) {
-							for (uint i = 0; i < ARRAYSIZE(cpMapping); ++i) {
-								if (extraCp.equalsIgnoreCase(cpMapping[i].str)) {
-									fontProperties->codepage = cpMapping[i].cp;
-									break;
-								}
-							}
-						}
-						extraCp.clear();
-					}
-					if (!extraFont.empty()) {
-						if (processResult)
-							fontProperties->fontfile = extraFont;
-						extraFont.clear();
-					}
-					if (!extraRTL.empty()) {
-						if (processResult)
-							fontProperties->rtl = extraRTL.equalsIgnoreCase("TRUE") || extraRTL.equalsIgnoreCase("YES") || extraRTL.equalsIgnoreCase("1");
-						extraRTL.clear();
-					}
-				}
-				if (parseTag < 1)
-					parseTag = 0;
-				tmp.clear();
-				dstr = &tmp;
-				++p;
-				continue;
-			}
-			if (parseTag) {
-				*dstr += *p++;
-				if (dstr->equalsIgnoreCase("VERSION:"))
-					dstr = &versionStr;
-				else if (dstr->equalsIgnoreCase("PLATFORM:"))
-					dstr = &platformStr;
-				else if (dstr->equalsIgnoreCase("LANGUAGE:"))
-					dstr = &languageStr;
-				else if (dstr->equalsIgnoreCase("CODEPAGE:"))
-					dstr = &extraCp;
-				else if (dstr->equalsIgnoreCase("FONTFILE:"))
-					dstr = &extraFont;
-				else if (dstr->equalsIgnoreCase("RTL:"))
-					dstr = &extraRTL;
-				else if (dstr->equalsIgnoreCase("DEFAULT"))
-					isDefault = true;
-				continue;
-			}
-			if (parseFile) {
-				*dstr += *p++;
-				if (dstr->equalsIgnoreCase("FILE:"))
-					dstr = &fileNoStr;
-				continue;
-			}
-			if (parseMapping) {
-				if (!parseText && (*p == ' ' || *p == ':')) {
-					if (addr == 0) {
-						if (currentLine.substr(0, 2).equalsIgnoreCase("0x") == false)
-							currentLine = "0x" + currentLine;
-						addr = currentLine.asUint64Ext();
-					}
-					currentLine.clear();
-					++p;
-					continue;
-				}
-				if (parseText) {
-					if (*p == '\\') {
-						++p;
-						if (*p == 'x' || *p == 'X') {
-							*dstr += static_cast<char>(Common::String("0x" + Common::String(p + 1, 2)).asUint64Ext());
-							p += 3;
-						} else {
-							if (*p == '\"')
-								*dstr += '\"';
-							++p;
-						}
-						continue;
-					}
-				}
-				if (*p == '\"') {
-					if (parseText) {
-						if (currentLine.lastChar() != '\xFF')
-							currentLine += '\xFF';
-						if (((mode == kCollect && (lang == Common::UNK_LANG) == isDefault) || fileNumber != -1) && table != nullptr)
-							updateDataTable(addr, reinterpret_cast<const uint8*>(currentLine.c_str()), table, mode, currentLine.size(), fileNo);
-						currentLine.clear();
-						addr = 0;
-						parseText = false;
-					} else {
-						parseText = true;
-					}
-					++p;
-					continue;
-				}
-
-				*dstr += *p++;
-				continue;
-			}
-			++p;
-		}
-		diff -= (p - end);
-	}
-
-	delete[] buff;
-	file.close();
-}
-
 void FanTranslationHandler::saveFile() {
 	const char l1[] =	"; This file is used for Snatcher fan translations.\n\n"
 						"; It is grouped into platform and language tags [[ PLATFORM: <x> ][ LANGUAGE: <x> ]]. The first language\n"
diff --git a/engines/snatcher/fantranslations.h b/engines/snatcher/fantranslations.h
index 623ff262a13..5397ca5699b 100644
--- a/engines/snatcher/fantranslations.h
+++ b/engines/snatcher/fantranslations.h
@@ -24,22 +24,14 @@
 #define SNATCHER_FANTRANSLATIONS_H
 
 #include "common/language.h"
-#include "common/list.h"
 #include "common/platform.h"
-#include "common/scummsys.h"
-#include "common/str-enc.h"
-#include "common/str.h"
+
+#include "engines/snatcher/fantranslations_detection.h"
 
 namespace Snatcher {
 
-class FanTranslationHandler {
+class FanTranslationHandler : public FanTranslationHandlerDetection {
 public:
-	enum TranslatorMode {
-		kDetect = 0,
-		kTranslate = 1,
-		kCollect = 2
-	};
-
 	enum ResourceType {
 		kInvalid = -1,
 		kModule = 0,
@@ -53,32 +45,9 @@ public:
 	const uint8 *process(uint32 addr, const uint8 *data);
 	void loadFile(int number, ResourceType rtype);
 
-	struct FontProperties {
-		FontProperties() : codepage(Common::kCodePageInvalid), rtl(false) {}
-		Common::CodePage codepage;
-		Common::String fontfile;
-		bool rtl;
-	};
-
 	const FontProperties &getFontProperties() const;
 
-	static Common::List<Common::Language> listTranslations(Common::Platform platform, Common::Language *defaultLang = nullptr);
-
 private:
-	struct PerFileTranslateableData {
-		PerFileTranslateableData() {}
-		~PerFileTranslateableData() {
-			stringTable.clear();
-			addressTable.clear();
-		}
-
-		Common::List<Common::String> stringTable;
-		Common::List<uint32> addressTable;
-	};
-
-	static void updateDataTable(uint32 addr, const uint8 *data, PerFileTranslateableData **table, TranslatorMode mode, uint dataSize = 256, int file = -1);
-	static void parseTranslations(Common::Platform platform, Common::Language lang, TranslatorMode mode, int fileNumber = -1,
-		PerFileTranslateableData **table = nullptr, Common::List<Common::Language> *langList = nullptr, Common::Language *defaultLang = nullptr, uint32 *fileOffsetList = nullptr, FontProperties *fontProperties = nullptr);
 	void saveFile();
 
 	PerFileTranslateableData **_gameTranslateableData = nullptr;
diff --git a/engines/snatcher/fantranslations_detection.cpp b/engines/snatcher/fantranslations_detection.cpp
new file mode 100644
index 00000000000..01007d768ce
--- /dev/null
+++ b/engines/snatcher/fantranslations_detection.cpp
@@ -0,0 +1,417 @@
+/* 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 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/>.
+*
+*/
+
+
+#include "snatcher/fantranslations.h"
+#include "snatcher/mem_mapping.h"
+
+#include "common/algorithm.h"
+#include "common/config-manager.h"
+#include "common/debug.h"
+#include "common/file.h"
+
+namespace Snatcher {
+
+#define SNATCHER_FANTRANSLATIONS_CURRENT_VERSION		1
+
+
+Common::List<Common::Language> FanTranslationHandlerDetection::listTranslations(Common::Platform platform, Common::Language *defaultLang) {
+	Common::List<Common::Language> res;
+	parseTranslations(platform, Common::UNK_LANG, kDetect, -1, nullptr, &res, defaultLang, nullptr, nullptr);
+	return res;
+}
+
+void FanTranslationHandlerDetection::updateDataTable(uint32 addr, const uint8 *data, PerFileTranslateableData **table, TranslatorMode mode, uint dataSize, int file) {
+	Common::String tmp(reinterpret_cast<const char *>(data), dataSize);
+	size_t tt = tmp.findFirstOf(-1);
+	tmp.chop(tmp.size() - tt - 1);
+
+	if (mode == kCollect) {
+		for (uint i = 0; i < tmp.size(); ++i) {
+			uint8 c = tmp[i];
+			if (c < 32 || c > 127) {
+				tmp.replace(i, 1, Common::String::format("\\x%02X", static_cast<uint8>(c)));
+				i += 3;
+			} else if (c == '\"') {
+				tmp.replace(i, 1, "\\\"");
+				i++;
+			}
+		}
+	}
+
+	assert(table);
+	if (!table[file])
+		table[file] = new PerFileTranslateableData();
+
+	PerFileTranslateableData &d = *table[file];
+
+	Common::List<Common::String>::iterator j = d.stringTable.begin();
+	for (Common::List<uint32>::iterator i = d.addressTable.begin(); i != d.addressTable.end(); ++i, ++j) {
+		if (addr == *i) {
+			addr = 0;
+			break;
+		}
+		if (addr < *i) {
+			d.addressTable.insert(i, addr);
+			d.stringTable.insert(j, tmp);
+			if (file == -1)
+				debug("0x%06x=\"%s\"", addr, tmp.c_str());
+			addr = 0;
+			break;
+		}
+	}
+
+	if (addr != 0) {
+		d.addressTable.push_back(addr);
+		d.stringTable.push_back(tmp);
+		if (file == -1)
+			debug("0x%06x=\"%s\"", addr, tmp.c_str());
+	}
+}
+
+static const struct CPMapping {
+	Common::CodePage cp;
+	const char str[5];
+} cpMapping[] = {
+	{Common::kWindows1250, "1250"},
+	{Common::kWindows1251, "1251"},
+	{Common::kWindows1252, "1252"},
+	{Common::kWindows1253, "1253"},
+	{Common::kWindows1254, "1254"},
+	{Common::kWindows1255, "1255"},
+	{Common::kWindows1256, "1256"},
+	{Common::kWindows1257, "1257"},
+	{Common::kWindows932, "932"},
+	{Common::kWindows936, "936"},
+	{Common::kWindows949, "949"},
+	{Common::kWindows950, "950"},
+	{Common::kDos850, "850"},
+	{Common::kDos862, "862"},
+	{Common::kDos866, "866"},
+};
+
+void FanTranslationHandlerDetection::parseTranslations(Common::Platform platform, Common::Language lang, TranslatorMode mode, int fileNumber,
+	PerFileTranslateableData **table, Common::List<Common::Language> *langList, Common::Language *defaultLang, uint32 *fileOffsetList, FontProperties *fontProperties) {
+
+	Common::Path fileName = Common::Path("snatcher_translations.dat");
+	Common::File file;
+	if (!file.open(fileName)) {
+
+		// Some lower level magic for the detection/metaengine phase where the normal file search hasn't been initiated yet.
+		const char *keys[] = {"path", "extrapath"};
+		for (int i = 0; i < ARRAYSIZE(keys); ++i) {
+			if (ConfMan.hasKey(keys[i])) {
+				Common::Path filePath(ConfMan.getPath(keys[i]));
+				Common::FSList list;
+				Common::FSNode(filePath).getChildren(list, Common::FSNode::kListFilesOnly);
+				for (Common::FSList::const_iterator it = list.begin(); it != list.end(); ++it) {
+					if (it->getName().equalsIgnoreCase(fileName.toString())) {
+						file.open(*it);
+						break;
+					}
+				}
+				if (file.isOpen())
+					break;
+			}
+		}
+
+		if (!file.isOpen())
+			return;
+	}
+
+	if (platform == Common::Platform::kPlatformUnknown && !fileOffsetList)
+		error("%s(): Platform must be specified to load a translation", __FUNCTION__);
+
+	uint16 blockSize = 5000;
+	uint16 extraBytes = 5;
+	uint8 *buff = new uint8[blockSize + extraBytes]();
+
+	bool leftPos = true;
+	bool skipLine = false;
+	int parseTag = 0;
+	bool parseFile = false;
+	bool parseMapping = false;
+	bool parseText = false;
+	bool saveOffset = false;
+	bool isDefault = false;
+
+	Common::String tmp;
+	Common::String versionStr;
+	Common::String platformStr;
+	Common::String languageStr;
+	Common::String fileNoStr;
+	Common::String currentLine;
+	Common::String extraCp;
+	Common::String extraRTL;
+	Common::String extraFont;
+	Common::Language *defLang = nullptr;
+
+	int fileNo = -1;
+	uint32 addr = 0;
+	Common::String *dstr = &tmp;
+	uint16 bytesLeft = file.size();
+	uint16 readSize = 0;
+	int diff = 0;
+
+	if (fileNumber != -1 && fileOffsetList != nullptr && (uint)fileNumber < 100) {
+		uint32 offs = fileOffsetList[fileNumber];
+		if (offs == 0)	// Many module files do not actually contain any text.
+			return;
+		file.seek(offs);
+		bytesLeft -= offs;
+		fileNo = fileNumber;
+		parseMapping = true;
+		dstr = ¤tLine;
+	}
+
+	while (bytesLeft > 0) {
+		diff = MAX<int>(readSize + diff - blockSize, 0);
+		if (diff)
+			memmove(buff, buff + blockSize + extraBytes - diff, diff);
+		readSize = file.read(buff + diff, blockSize + extraBytes - diff);
+		bytesLeft -= readSize;
+		if (bytesLeft == 0)
+			extraBytes = 0;
+
+		const char *p = reinterpret_cast<const char *>(buff);
+		const char *last = nullptr;
+		const char *end = p + readSize - extraBytes + diff;
+
+		while (p < end) {
+			if (p == last) // avoid infinite loop on invalid data
+				error("%s(): Fan translation file '%s' is corrupt or invalid. Error at pos: 0x%04x", __FUNCTION__, fileName.toString().c_str(), static_cast<int>(file.pos()) - readSize - diff + static_cast<uint32>(reinterpret_cast<const uint8*>(p) - buff));
+			last = p;
+
+			if ((leftPos || parseTag || parseFile) && (*p == ' ' || *p == '*')) {
+				if (*p++ == '*') {
+					if (leftPos && !parseFile) {
+						fileNo = -1;
+						parseFile = true;
+					}
+					if (!fileNoStr.empty()) {
+						parseMapping = true;
+						fileNo = fileNoStr.asUint64();
+						fileNoStr.clear();
+						dstr = ¤tLine;
+						parseFile = false;
+						saveOffset = (platform != Common::kPlatformUnknown && lang != Common::UNK_LANG && mode == kTranslate && fileOffsetList != nullptr && fileNumber == -1);
+					}
+				}
+				continue;
+			}
+			if (*p == '\r' || *p == '\n') {
+				skipLine = false;
+				if (!fileNoStr.empty()) {
+					parseMapping = true;
+					fileNo = fileNoStr.asUint64();
+					fileNoStr.clear();
+					dstr = ¤tLine;
+					parseFile = false;
+					saveOffset = (platform != Common::kPlatformUnknown && lang != Common::UNK_LANG && mode == kTranslate && fileOffsetList != nullptr && fileNumber == -1);
+				}
+				if (parseText) {
+					if (currentLine.lastChar() != '\xFF')
+						currentLine += '\xFF';
+					if ((mode == kCollect || fileNumber != -1) && table != nullptr)
+						updateDataTable(addr, reinterpret_cast<const uint8*>(currentLine.c_str()), table, mode, currentLine.size(), fileNo);
+					addr = 0;
+					parseText = false;
+				}
+				currentLine.clear();
+				leftPos = true;
+				++p;
+				if (saveOffset)
+					fileOffsetList[fileNo] = file.pos() - readSize - diff + static_cast<uint32>(reinterpret_cast<const uint8*>(p) - buff);
+				saveOffset = false;
+				continue;
+			}
+			leftPos = false;
+			if (*p == ';') {
+				skipLine = true;
+				++p;
+				continue;
+			}
+			if (skipLine) {
+				++p;
+				continue;
+			}
+			if (*p == '[') {
+				if (++parseTag == 1) {
+					platformStr.clear();
+					languageStr.clear();
+					versionStr.clear();
+					extraCp.clear();
+					extraRTL.clear();
+					extraFont.clear();
+					tmp.clear();
+					fileNo = -1;
+					parseFile = false;
+					isDefault = false;
+					dstr = &tmp;
+					if (fileNumber != -1) {
+						bytesLeft = 0;
+						p = end;
+					}
+				}
+				if (parseTag == 3) {
+					tmp.clear();
+					dstr = &tmp;
+				}
+				++p;
+				continue;
+			}
+			if (*p == ']') {
+				if (--parseTag == 0) {
+					if (!versionStr.empty()) {
+						int version = versionStr.asUint64();
+						if (version < SNATCHER_FANTRANSLATIONS_CURRENT_VERSION)
+							warning("Translation file '%s' is outdated (version %d, expected %d)", fileName.toString().c_str(), version, SNATCHER_FANTRANSLATIONS_CURRENT_VERSION);
+						versionStr.clear();
+					}
+					if (Common::parsePlatform(platformStr) == platform) {
+						if ((mode == kCollect && lang == Common::UNK_LANG) || fileNumber != -1 || (lang != Common::UNK_LANG && (Common::parseLanguage(languageStr) == lang || Common::parseLanguage(languageStr.substr(0, 2)) == lang))) {
+							parseFile = true;
+						} else if (mode == kDetect && lang == Common::UNK_LANG) {
+							Common::Language l = Common::parseLanguage(languageStr);
+							if (l == Common::UNK_LANG)
+								l = Common::parseLanguage(languageStr.substr(0, 2));
+							if (l != Common::UNK_LANG) {
+								if (defLang != nullptr)
+									*defLang = l;
+								else if (langList != nullptr)
+									langList->push_back(l);
+							}
+						}
+						languageStr.clear();
+						defLang = nullptr;
+					}
+				}
+				if (parseTag == 2) {
+					if (isDefault)
+						defLang = defaultLang;
+					bool processResult = (defLang == nullptr && (Common::parseLanguage(languageStr) != Common::UNK_LANG || Common::parseLanguage(languageStr.substr(0, 2)) != Common::UNK_LANG) && fileOffsetList != nullptr);
+					if (!extraCp.empty()) {
+						if (processResult) {
+							for (uint i = 0; i < ARRAYSIZE(cpMapping); ++i) {
+								if (extraCp.equalsIgnoreCase(cpMapping[i].str)) {
+									fontProperties->codepage = cpMapping[i].cp;
+									break;
+								}
+							}
+						}
+						extraCp.clear();
+					}
+					if (!extraFont.empty()) {
+						if (processResult)
+							fontProperties->fontfile = extraFont;
+						extraFont.clear();
+					}
+					if (!extraRTL.empty()) {
+						if (processResult)
+							fontProperties->rtl = extraRTL.equalsIgnoreCase("TRUE") || extraRTL.equalsIgnoreCase("YES") || extraRTL.equalsIgnoreCase("1");
+						extraRTL.clear();
+					}
+				}
+				if (parseTag < 1)
+					parseTag = 0;
+				tmp.clear();
+				dstr = &tmp;
+				++p;
+				continue;
+			}
+			if (parseTag) {
+				*dstr += *p++;
+				if (dstr->equalsIgnoreCase("VERSION:"))
+					dstr = &versionStr;
+				else if (dstr->equalsIgnoreCase("PLATFORM:"))
+					dstr = &platformStr;
+				else if (dstr->equalsIgnoreCase("LANGUAGE:"))
+					dstr = &languageStr;
+				else if (dstr->equalsIgnoreCase("CODEPAGE:"))
+					dstr = &extraCp;
+				else if (dstr->equalsIgnoreCase("FONTFILE:"))
+					dstr = &extraFont;
+				else if (dstr->equalsIgnoreCase("RTL:"))
+					dstr = &extraRTL;
+				else if (dstr->equalsIgnoreCase("DEFAULT"))
+					isDefault = true;
+				continue;
+			}
+			if (parseFile) {
+				*dstr += *p++;
+				if (dstr->equalsIgnoreCase("FILE:"))
+					dstr = &fileNoStr;
+				continue;
+			}
+			if (parseMapping) {
+				if (!parseText && (*p == ' ' || *p == ':')) {
+					if (addr == 0) {
+						if (currentLine.substr(0, 2).equalsIgnoreCase("0x") == false)
+							currentLine = "0x" + currentLine;
+						addr = currentLine.asUint64Ext();
+					}
+					currentLine.clear();
+					++p;
+					continue;
+				}
+				if (parseText) {
+					if (*p == '\\') {
+						++p;
+						if (*p == 'x' || *p == 'X') {
+							*dstr += static_cast<char>(Common::String("0x" + Common::String(p + 1, 2)).asUint64Ext());
+							p += 3;
+						} else {
+							if (*p == '\"')
+								*dstr += '\"';
+							++p;
+						}
+						continue;
+					}
+				}
+				if (*p == '\"') {
+					if (parseText) {
+						if (currentLine.lastChar() != '\xFF')
+							currentLine += '\xFF';
+						if (((mode == kCollect && (lang == Common::UNK_LANG) == isDefault) || fileNumber != -1) && table != nullptr)
+							updateDataTable(addr, reinterpret_cast<const uint8*>(currentLine.c_str()), table, mode, currentLine.size(), fileNo);
+						currentLine.clear();
+						addr = 0;
+						parseText = false;
+					} else {
+						parseText = true;
+					}
+					++p;
+					continue;
+				}
+
+				*dstr += *p++;
+				continue;
+			}
+			++p;
+		}
+		diff -= (p - end);
+	}
+
+	delete[] buff;
+	file.close();
+}
+
+} // End of namespace Snatcher
diff --git a/engines/snatcher/fantranslations_detection.h b/engines/snatcher/fantranslations_detection.h
new file mode 100644
index 00000000000..e76d411aee7
--- /dev/null
+++ b/engines/snatcher/fantranslations_detection.h
@@ -0,0 +1,71 @@
+/* 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 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/>.
+*
+*/
+
+
+#ifndef SNATCHER_FANTRANSLATIONS_DETECTION_H
+#define SNATCHER_FANTRANSLATIONS_DETECTION_H
+
+#include "common/language.h"
+#include "common/list.h"
+#include "common/platform.h"
+#include "common/str-enc.h"
+#include "common/str.h"
+
+namespace Snatcher {
+
+class FanTranslationHandlerDetection {
+public:
+	enum TranslatorMode {
+		kDetect = 0,
+		kTranslate = 1,
+		kCollect = 2
+	};
+
+	struct FontProperties {
+		FontProperties() : codepage(Common::kCodePageInvalid), rtl(false) {}
+		Common::CodePage codepage;
+		Common::String fontfile;
+		bool rtl;
+	};
+
+	static Common::List<Common::Language> listTranslations(Common::Platform platform, Common::Language *defaultLang = nullptr);
+
+protected:
+	struct PerFileTranslateableData {
+		PerFileTranslateableData() {}
+		~PerFileTranslateableData() {
+			stringTable.clear();
+			addressTable.clear();
+		}
+
+		Common::List<Common::String> stringTable;
+		Common::List<uint32> addressTable;
+	};
+
+	static void updateDataTable(uint32 addr, const uint8 *data, PerFileTranslateableData **table, TranslatorMode mode, uint dataSize = 256, int file = -1);
+	static void parseTranslations(Common::Platform platform, Common::Language lang, TranslatorMode mode, int fileNumber = -1,
+		PerFileTranslateableData **table = nullptr, Common::List<Common::Language> *langList = nullptr, Common::Language *defaultLang = nullptr, uint32 *fileOffsetList = nullptr, FontProperties *fontProperties = nullptr);
+};
+
+} // End of namespace Snatcher
+
+#endif // SNATCHER_FANTRANSLATIONS_H
+
diff --git a/engines/snatcher/module.mk b/engines/snatcher/module.mk
index e3fb5eb5265..1ddf23ccca0 100644
--- a/engines/snatcher/module.mk
+++ b/engines/snatcher/module.mk
@@ -4,6 +4,7 @@ MODULE_OBJS := \
 	action.o \
 	animator_scd.o \
 	fantranslations.o \
+	fantranslations_detection.o \
 	graphics.o \
 	memory.o \
 	mem_mapping.o \
@@ -41,5 +42,5 @@ DETECT_OBJS += $(MODULE)/detection.o
 # module is enabled, because it already has the contents.
 ifneq ($(ENABLE_SNATCHER), STATIC_PLUGIN)
 # External dependencies for detection.
-DETECT_OBJS += $(MODULE)/fantranslations.o
+DETECT_OBJS += $(MODULE)/fantranslations_detection.o
 endif




More information about the Scummvm-git-logs mailing list