From b691c43c44ff180d10e7d4a9afc83b98551ff586 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sun, 1 Mar 2026 12:16:08 +0800 Subject: Initial commit --- Minecraft.Client/StringTable.cpp | 172 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Minecraft.Client/StringTable.cpp (limited to 'Minecraft.Client/StringTable.cpp') diff --git a/Minecraft.Client/StringTable.cpp b/Minecraft.Client/StringTable.cpp new file mode 100644 index 00000000..b0c46a7b --- /dev/null +++ b/Minecraft.Client/StringTable.cpp @@ -0,0 +1,172 @@ +#include "StdAfx.h" +#include "StringTable.h" + +StringTable::StringTable(void) +{ + +} + +// Load string table from a binary blob, filling out with the current localisation data only +StringTable::StringTable(PBYTE pbData, DWORD dwSize) +{ + src = byteArray(pbData, dwSize); + + ByteArrayInputStream bais(src); + DataInputStream dis(&bais); + + int versionNumber = dis.readInt(); + int languagesCount = dis.readInt(); + + vector< pair > langSizeMap; + for(int i = 0; i < languagesCount; ++i) + { + wstring langId = dis.readUTF(); + int langSize = dis.readInt(); + + langSizeMap.push_back( vector< pair >::value_type(langId, langSize)); + } + + vector locales; + app.getLocale(locales); + + bool foundLang = false; + __int64 bytesToSkip = 0; + int dataSize = 0; + + // + for( AUTO_VAR(it_locales, locales.begin()); + it_locales!=locales.end() && (!foundLang); + it_locales++ + ) + { + bytesToSkip = 0; + + for(AUTO_VAR(it, langSizeMap.begin()); it != langSizeMap.end(); ++it) + { + if(it->first.compare(*it_locales) == 0) + { + app.DebugPrintf("StringTable:: Found language '%ls'.\n", it_locales->c_str()); + dataSize = it->second; + foundLang = true; + break; + } + + bytesToSkip += it->second; + } + + if (!foundLang) + app.DebugPrintf("StringTable:: Can't find language '%ls'.\n", it_locales->c_str()); + } + + if(foundLang) + { + dis.skip(bytesToSkip); + + byteArray langData(dataSize); + dis.read(langData); + + dis.close(); + + ByteArrayInputStream bais2(langData); + DataInputStream dis2(&bais2); + + // Read the language file for the selected language + int langVersion = dis2.readInt(); + + isStatic = false; // 4J-JEV: Versions 1 and up could use + if (langVersion > 0) // integers rather than wstrings as keys. + isStatic = dis2.readBoolean(); + + wstring langId = dis2.readUTF(); + int totalStrings = dis2.readInt(); + + if (!isStatic) + { + for(int i = 0; i < totalStrings; ++i) + { + wstring stringId = dis2.readUTF(); + wstring stringValue = dis2.readUTF(); + + m_stringsMap.insert( unordered_map::value_type(stringId, stringValue) ); + } + } + else + { + for(int i = 0; i < totalStrings; ++i) + m_stringsVec.push_back( dis2.readUTF() ); + } + dis2.close(); + + // We can't delete this data in the dtor, so clear the reference + bais2.reset(); + } + else + { + app.DebugPrintf("Failed to get language\n"); +#ifdef _DEBUG + __debugbreak(); +#endif + + isStatic = false; + } + + // We can't delete this data in the dtor, so clear the reference + bais.reset(); +} + +StringTable::~StringTable(void) +{ + // delete src.data; TODO 4J-JEV: ? +} + +void StringTable::getData(PBYTE *ppData, UINT *pSize) +{ + *ppData = src.data; + *pSize = src.length; +} + +LPCWSTR StringTable::getString(const wstring &id) +{ +#ifndef _CONTENT_PACKAGE + if (isStatic) + { + __debugbreak(); + return L""; + } +#endif + + AUTO_VAR(it, m_stringsMap.find(id) ); + + if(it != m_stringsMap.end()) + { + return it->second.c_str(); + } + else + { + return L""; + } +} + +LPCWSTR StringTable::getString(int id) +{ +#ifndef _CONTENT_PACKAGE + if (!isStatic) + { + __debugbreak(); + return L""; + } +#endif + + if (id < m_stringsVec.size()) + { + LPCWSTR pwchString=m_stringsVec.at(id).c_str(); + return pwchString; + } + else + return L""; +} + + + + + -- cgit v1.2.3