This commit is contained in:
GabsPuNs
2026-05-14 21:52:21 -04:00
parent 1b2f5c6fe8
commit f8a2d644f5
291 changed files with 7363 additions and 18368 deletions

View File

@@ -14,168 +14,123 @@ StringTable::StringTable(PBYTE pbData, DWORD dwSize)
ProcessStringTableData();
}
StringTable::~StringTable(void)
{
Reset();
}
void StringTable::Reset(void)
{
m_stringsMap.clear();
m_stringsVec.clear();
m_langSizeMap.clear();
isStatic = false;
currentLanguage.clear();
}
void StringTable::ReloadStringTable()
{
Reset();
ProcessStringTableData();
m_stringsMap.clear();
m_stringsVec.clear();
ProcessStringTableData();
}
void StringTable::ProcessStringTableData(void)
{
if (src.length > 0 && src.data != nullptr)
ByteArrayInputStream bais(src);
DataInputStream dis(&bais);
int versionNumber = dis.readInt();
int languagesCount = dis.readInt();
vector< pair<wstring, int> > langSizeMap;
for(int i = 0; i < languagesCount; ++i)
{
wstring langId = dis.readUTF();
int langSize = dis.readInt();
langSizeMap.push_back( vector< pair<wstring, int> >::value_type(langId, langSize));
}
vector<wstring> locales;
app.getLocale(locales);
bool foundLang = false;
int64_t bytesToSkip = 0;
int dataSize = 0;
//
for (auto it_locales = locales.begin();
it_locales != locales.end() && (!foundLang);
++it_locales)
{
ByteArrayInputStream bais(src);
DataInputStream dis(&bais);
ReadAllFromStream(&dis);
dis.close();
bais.reset();
}
bytesToSkip = 0;
for(auto& it : langSizeMap)
{
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();
app.DebugPrintf("IsStatic=%d totalStrings = %d\n",isStatic?1:0,totalStrings);
if (!isStatic)
{
for(int i = 0; i < totalStrings; ++i)
{
wstring stringId = dis2.readUTF();
wstring stringValue = dis2.readUTF();
m_stringsMap.insert( unordered_map<wstring, wstring>::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");
isStatic = false;
}
// We can't delete this data in the dtor, so clear the reference
bais.reset();
}
void StringTable::ReadAllFromStream(DataInputStream* dis)
StringTable::~StringTable(void)
{
int versionNumber = dis->readInt();
int languagesCount = dis->readInt();
m_langSizeMap.clear();
for(int i = 0; i < languagesCount; ++i)
{
wstring langId = dis->readUTF();
int langSize = dis->readInt();
m_langSizeMap.push_back(make_pair(langId, langSize));
}
int64_t bytesToSkip = 0;
int dataSize = 0;
if(FindLanguage(currentLanguage, bytesToSkip, dataSize))
{
dis->skip(bytesToSkip);
ReadLanguageFromStream(dis, dataSize);
}
else
{
isStatic = false;
}
}
bool StringTable::FindLanguage(wstring& outLangId, int64_t& outBytesToSkip, int& outDataSize)
{
vector<wstring> locales;
app.getLocale(locales);
outBytesToSkip = 0;
for (auto it_locales = locales.begin(); it_locales != locales.end(); ++it_locales)
{
outBytesToSkip = 0;
for(auto& it : m_langSizeMap)
{
if(it.first.compare(*it_locales) == 0)
{
app.DebugPrintf("StringTable:: Found language '%ls'.\n", it_locales->c_str());
outDataSize = it.second;
outLangId = *it_locales;
return true;
}
outBytesToSkip += it.second;
}
app.DebugPrintf("StringTable:: Can't find language '%ls'.\n", it_locales->c_str());
}
app.DebugPrintf("Failed to get language\n");
return false;
}
void StringTable::ReadLanguageFromStream(DataInputStream* dis, int dataSize)
{
byteArray langData(dataSize);
dis->read(langData);
ByteArrayInputStream bais2(langData);
DataInputStream dis2(&bais2);
// Read the language file for the selected language
int langVersion = dis2.readInt();
isStatic = false;
if (langVersion > 0)
isStatic = dis2.readBoolean();
wstring langId = dis2.readUTF();
int totalStrings = dis2.readInt();
app.DebugPrintf("IsStatic=%s totalStrings = %d\n", isStatic ? "TRUE" : "FALSE", totalStrings);
if (!isStatic)
{
for(int i = 0; i < totalStrings; ++i)
{
wstring stringId = dis2.readUTF();
wstring stringValue = dis2.readUTF();
m_stringsMap.insert(unordered_map<wstring, wstring>::value_type(stringId, stringValue));
}
}
else
{
for(int i = 0; i < totalStrings; ++i)
{
m_stringsVec.push_back(dis2.readUTF());
}
}
dis2.close();
bais2.reset();
}
void StringTable::write(DataOutputStream* dos)
{
dos->writeInt(1);
dos->writeInt(1);
int langVersion = 1;
dos->writeInt(langVersion);
dos->writeBoolean(isStatic);
dos->writeUTF(currentLanguage.empty() ? L"en_US" : currentLanguage);
if (!isStatic)
{
dos->writeInt(m_stringsMap.size());
for (auto const& [key, val] : m_stringsMap)
{
dos->writeUTF(key);
dos->writeUTF(val);
}
}
else
{
dos->writeInt(m_stringsVec.size());
for (auto const& val : m_stringsVec)
{
dos->writeUTF(val);
}
}
// delete src.data; TODO 4J-JEV: ?
}
void StringTable::getData(PBYTE *ppData, UINT *pSize)
@@ -186,6 +141,14 @@ void StringTable::getData(PBYTE *ppData, UINT *pSize)
LPCWSTR StringTable::getString(const wstring &id)
{
#ifndef _CONTENT_PACKAGE
if (isStatic)
{
__debugbreak();
return L"";
}
#endif
auto it = m_stringsMap.find(id);
if(it != m_stringsMap.end())
@@ -200,11 +163,24 @@ LPCWSTR StringTable::getString(const wstring &id)
LPCWSTR StringTable::getString(int id)
{
if (id >= 0 && id < m_stringsVec.size())
#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"";
}
}