aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/StringTable.cpp
blob: bdee2ca918a104920ab1a808f4a6409909b4e762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#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);

	ProcessStringTableData();
}


void StringTable::ReloadStringTable()
{
	m_stringsMap.clear();
	m_stringsVec.clear();

	ProcessStringTableData();
}

void StringTable::ProcessStringTableData(void)
{
	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)
    {
		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();
}


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 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"";
}