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 "app/common/DLC/DLCFile.h"
#include "app/linux/LinuxGame.h"
#include "minecraft/locale/StringTable.h"
DLCLocalisationFile::DLCLocalisationFile(const std::string& path)
@ -11,5 +12,7 @@ DLCLocalisationFile::DLCLocalisationFile(const std::string& path)
void DLCLocalisationFile::addData(std::uint8_t* pbData,
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(
DLCManager::e_DLCType_LocalisationData, "languages.loc");
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();
std::vector<uint8_t> bStringTable(bStringTableSize);
dis2.read(bStringTable);
std::vector<std::string> locales;
app.getLocale(locales);
StringTable* strings =
new StringTable(bStringTable.data(), bStringTable.size());
new StringTable(bStringTable.data(), bStringTable.size(), locales);
// Read RuleFile.
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";
if (mediaArchive->hasFile(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 {
m_stringTable = nullptr;
assert(false);

View file

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

View file

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

View file

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

View file

@ -54,9 +54,10 @@ public:
// };
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);
void ReloadStringTable();
void ReloadStringTable(const std::vector<std::string>& locales);
void getData(std::uint8_t** ppData, unsigned int* pSize);
@ -67,5 +68,5 @@ public:
private:
// 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
locale/I18n.cpp
locale/Language.cpp
locale/StringTable.cpp
network/Connection.cpp
network/packet/AddEntityPacket.cpp
network/packet/AddExperienceOrbPacket.cpp