refactor: move StringTable impl into minecraft/locale and inject locales

This commit is contained in:
MatthewBeshay 2026-04-09 20:35:53 +10:00
parent aa250ff560
commit 84140ae6d4
9 changed files with 40 additions and 26 deletions

View file

@ -2,6 +2,7 @@
#include "DLCManager.h" #include "DLCManager.h"
#include "app/common/DLC/DLCFile.h" #include "app/common/DLC/DLCFile.h"
#include "app/linux/LinuxGame.h"
#include "minecraft/locale/StringTable.h" #include "minecraft/locale/StringTable.h"
DLCLocalisationFile::DLCLocalisationFile(const std::string& path) DLCLocalisationFile::DLCLocalisationFile(const std::string& path)
@ -11,5 +12,7 @@ DLCLocalisationFile::DLCLocalisationFile(const std::string& path)
void DLCLocalisationFile::addData(std::uint8_t* pbData, void DLCLocalisationFile::addData(std::uint8_t* pbData,
std::uint32_t dataBytes) { std::uint32_t dataBytes) {
m_strings = new StringTable(pbData, dataBytes); std::vector<std::string> locales;
app.getLocale(locales);
m_strings = new StringTable(pbData, dataBytes, locales);
} }

View file

@ -351,6 +351,8 @@ void DLCPack::UpdateLanguage() {
DLCLocalisationFile* localisationFile = (DLCLocalisationFile*)getFile( DLCLocalisationFile* localisationFile = (DLCLocalisationFile*)getFile(
DLCManager::e_DLCType_LocalisationData, "languages.loc"); DLCManager::e_DLCType_LocalisationData, "languages.loc");
StringTable* strTable = localisationFile->getStringTable(); StringTable* strTable = localisationFile->getStringTable();
strTable->ReloadStringTable(); std::vector<std::string> locales;
app.getLocale(locales);
strTable->ReloadStringTable(locales);
} }
} }

View file

@ -223,8 +223,10 @@ void GameRuleManager::loadGameRules(LevelGenerationOptions* lgo, uint8_t* dIn,
unsigned int bStringTableSize = dis2.readInt(); unsigned int bStringTableSize = dis2.readInt();
std::vector<uint8_t> bStringTable(bStringTableSize); std::vector<uint8_t> bStringTable(bStringTableSize);
dis2.read(bStringTable); dis2.read(bStringTable);
std::vector<std::string> locales;
app.getLocale(locales);
StringTable* strings = StringTable* strings =
new StringTable(bStringTable.data(), bStringTable.size()); new StringTable(bStringTable.data(), bStringTable.size(), locales);
// Read RuleFile. // Read RuleFile.
std::vector<uint8_t> bRuleFile(content.size() - bStringTable.size()); std::vector<uint8_t> bRuleFile(content.size() - bStringTable.size());

View file

@ -77,7 +77,10 @@ void LocalizationManager::loadStringTable(ArchiveFile* mediaArchive) {
std::string localisationFile = "languages.loc"; std::string localisationFile = "languages.loc";
if (mediaArchive->hasFile(localisationFile)) { if (mediaArchive->hasFile(localisationFile)) {
std::vector<uint8_t> locFile = mediaArchive->getFile(localisationFile); std::vector<uint8_t> locFile = mediaArchive->getFile(localisationFile);
m_stringTable = new StringTable(locFile.data(), locFile.size()); std::vector<std::string> locales;
getLocale(locales);
m_stringTable =
new StringTable(locFile.data(), locFile.size(), locales);
} else { } else {
m_stringTable = nullptr; m_stringTable = nullptr;
assert(false); assert(false);

View file

@ -145,8 +145,10 @@ void IUIScene_StartGame::UpdateTexturePackDescription(int index) {
app.GetFileFromTPD(eTPDFileType_Loc, pbData, dwBytes, &pbFileData, app.GetFileFromTPD(eTPDFileType_Loc, pbData, dwBytes, &pbFileData,
&dwFileBytes); &dwFileBytes);
if (dwFileBytes > 0 && pbFileData) { if (dwFileBytes > 0 && pbFileData) {
std::vector<std::string> locales;
app.getLocale(locales);
StringTable* pStringTable = StringTable* pStringTable =
new StringTable(pbFileData, dwFileBytes); new StringTable(pbFileData, dwFileBytes, locales);
m_texturePackTitle.SetText( m_texturePackTitle.SetText(
pStringTable->getString("IDS_DISPLAY_NAME")); pStringTable->getString("IDS_DISPLAY_NAME"));
m_texturePackDescription.SetText( m_texturePackDescription.SetText(

View file

@ -49,7 +49,6 @@ common/GameSettingsManager.cpp
common/Game_XuiActions.cpp common/Game_XuiActions.cpp
common/Leaderboards/LeaderboardInterface.cpp common/Leaderboards/LeaderboardInterface.cpp
common/Leaderboards/LeaderboardManager.cpp common/Leaderboards/LeaderboardManager.cpp
common/Localisation/StringTable.cpp
common/LocalizationManager.cpp common/LocalizationManager.cpp
common/MenuController.cpp common/MenuController.cpp
common/Network/GameNetworkManager.cpp common/Network/GameNetworkManager.cpp

View file

@ -3,28 +3,32 @@
#include <ranges> #include <ranges>
#include <utility> #include <utility>
#include "app/linux/LinuxGame.h"
#include "java/InputOutputStream/ByteArrayInputStream.h" #include "java/InputOutputStream/ByteArrayInputStream.h"
#include "java/InputOutputStream/DataInputStream.h" #include "java/InputOutputStream/DataInputStream.h"
#include "minecraft/util/Log.h"
StringTable::StringTable(void) {} StringTable::StringTable(void) {}
// Load string table from a binary blob, filling out with the current // Load string table from a binary blob, filling out with the current
// localisation data only // localisation data only. The caller passes the locale list it wants
StringTable::StringTable(std::uint8_t* pbData, unsigned int dataSize) { // matched (typically obtained from the LocalizationManager) so that
// StringTable does not have to know about app-side singletons.
StringTable::StringTable(std::uint8_t* pbData, unsigned int dataSize,
const std::vector<std::string>& locales) {
src = std::vector<uint8_t>(pbData, pbData + dataSize); src = std::vector<uint8_t>(pbData, pbData + dataSize);
ProcessStringTableData(); ProcessStringTableData(locales);
} }
void StringTable::ReloadStringTable() { void StringTable::ReloadStringTable(const std::vector<std::string>& locales) {
m_stringsMap.clear(); m_stringsMap.clear();
m_stringsVec.clear(); m_stringsVec.clear();
ProcessStringTableData(); ProcessStringTableData(locales);
} }
void StringTable::ProcessStringTableData(void) { void StringTable::ProcessStringTableData(
const std::vector<std::string>& locales) {
ByteArrayInputStream bais(src); ByteArrayInputStream bais(src);
DataInputStream dis(&bais); DataInputStream dis(&bais);
@ -41,9 +45,6 @@ void StringTable::ProcessStringTableData(void) {
langSize)); langSize));
} }
std::vector<std::string> locales;
app.getLocale(locales);
bool foundLang = false; bool foundLang = false;
int64_t bytesToSkip = 0; int64_t bytesToSkip = 0;
int dataSize = 0; int dataSize = 0;
@ -55,8 +56,8 @@ void StringTable::ProcessStringTableData(void) {
for (auto it = langSizeMap.begin(); it != langSizeMap.end(); ++it) { for (auto it = langSizeMap.begin(); it != langSizeMap.end(); ++it) {
if (it->first.compare(*it_locales) == 0) { if (it->first.compare(*it_locales) == 0) {
app.DebugPrintf("StringTable:: Found language '%s'.\n", Log::info("StringTable:: Found language '%s'.\n",
it_locales->c_str()); it_locales->c_str());
dataSize = it->second; dataSize = it->second;
foundLang = true; foundLang = true;
break; break;
@ -66,8 +67,8 @@ void StringTable::ProcessStringTableData(void) {
} }
if (!foundLang) if (!foundLang)
app.DebugPrintf("StringTable:: Can't find language '%s'.\n", Log::info("StringTable:: Can't find language '%s'.\n",
it_locales->c_str()); it_locales->c_str());
} }
if (foundLang) { if (foundLang) {
@ -91,8 +92,8 @@ void StringTable::ProcessStringTableData(void) {
std::string langId = dis2.readUTF(); std::string langId = dis2.readUTF();
int totalStrings = dis2.readInt(); int totalStrings = dis2.readInt();
app.DebugPrintf("IsStatic=%d totalStrings = %d\n", isStatic ? 1 : 0, Log::info("IsStatic=%d totalStrings = %d\n", isStatic ? 1 : 0,
totalStrings); totalStrings);
if (!isStatic) { if (!isStatic) {
for (int i = 0; i < totalStrings; ++i) { for (int i = 0; i < totalStrings; ++i) {
@ -112,7 +113,7 @@ void StringTable::ProcessStringTableData(void) {
// We can't delete this data in the dtor, so clear the reference // We can't delete this data in the dtor, so clear the reference
bais2.reset(); bais2.reset();
} else { } else {
app.DebugPrintf("Failed to get language\n"); Log::info("Failed to get language\n");
#ifdef _DEBUG #ifdef _DEBUG
assert(0); assert(0);
#endif #endif

View file

@ -54,9 +54,10 @@ public:
// }; // };
StringTable(void); StringTable(void);
StringTable(std::uint8_t* pbData, unsigned int dataSize); StringTable(std::uint8_t* pbData, unsigned int dataSize,
const std::vector<std::string>& locales);
~StringTable(void); ~StringTable(void);
void ReloadStringTable(); void ReloadStringTable(const std::vector<std::string>& locales);
void getData(std::uint8_t** ppData, unsigned int* pSize); void getData(std::uint8_t** ppData, unsigned int* pSize);
@ -67,5 +68,5 @@ public:
private: private:
// std::string getLangId(uint32_t dwLanguage=0); // std::string getLangId(uint32_t dwLanguage=0);
void ProcessStringTableData(void); void ProcessStringTableData(const std::vector<std::string>& locales);
}; };

View file

@ -284,6 +284,7 @@ core/FacingEnum.cpp
core/ItemDispenseBehaviors.cpp core/ItemDispenseBehaviors.cpp
locale/I18n.cpp locale/I18n.cpp
locale/Language.cpp locale/Language.cpp
locale/StringTable.cpp
network/Connection.cpp network/Connection.cpp
network/packet/AddEntityPacket.cpp network/packet/AddEntityPacket.cpp
network/packet/AddExperienceOrbPacket.cpp network/packet/AddExperienceOrbPacket.cpp