forked from LemonHaze420/DCPopulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalization.cpp
227 lines (195 loc) · 5.23 KB
/
localization.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "Localization.h"
#include "Object.h"
#include "SafeProgramming.h"
#include "Misc.h"
#include "String.h"
#include "Platform.h"
#include "Preferences.h"
// ----
CLocalization::CLocalization( CPlatform *_PlatformRef,
CPreferences *_PreferencesRef)
{
PlatformRef = _PlatformRef;
PreferencesRef = _PreferencesRef;
// Clear all pointers to NULL.
for (int i = MAX_LOCALIZED_STRINGS; i--;)
{
StringLocalizationArray[i] = NULL;
}
}
CLocalization::~CLocalization()
{
FreeLocalizationStrings();
}
// ----
// Load localization strings - the language will be
// read from the preferences object (defaulting to
// English if this fails).
bool CLocalization::LoadLocalizationStrings()
{
FILE *FH;
CString Path;
char Buffer[256], Token[256];
char *Index;
char *EndToken;
int SupportedLangArray[32];
int i, NumOfTokens;
int LanguageToLoad;
// Free any existing strings.
FreeLocalizationStrings();
// Generate file path
if (!PlatformRef)
return false;
Path = PlatformRef->GetPath(EP_Text);
Path += "lanarch.ctd"; // LANgauge ARCHive . Converted Text Data.
FH = FOPEN(Path.GetString(), "rb");
if (FH)
{
// Now, see what langauges the archive supports.
fread(Buffer, 256, 1, FH);
ChangeEncryptionState(Buffer, 256);
Index = strchr(Buffer, '\n'); // Try to find the end of the info.
if (Index)
{
*(Index - 1) = 0;
i = Index - Buffer; Index++; // Calculate the length of the header
fseek(FH, i + 1, SEEK_SET); // Seek to the end of the header + new line char
NumOfTokens = 0;
Index = Buffer;
while (i > 0)
{
EndToken = strchr(Index, ','); // Comma seperated list.
if (!EndToken)
EndToken = strchr(Index, '\0');// End of line.
strncpy(Token, Index, (EndToken - Index));
*(Token + (EndToken - Index)) = 0;
SupportedLangArray[NumOfTokens] = atoi(Token);
ODS("Langauge ");
ODI(SupportedLangArray[NumOfTokens]);
ODSN(" supported");
// Prepare for next loop
i -= ((EndToken - Index) + 1);
NumOfTokens++;
Index = EndToken + 1;
}
ODI(NumOfTokens);
ODSN(" languages supported");
}
else
return false; // The language list heading is too long.
// If Preferences object exists.
// 1. and the request language exists -> Load it.
// 2. The langauge does exist -> Does English exist?
// a. Yes -> Load it.
// b. No -> Load first langauge we come across.
// No Preferences object.
// 1. Is English available.
// a. Yes -> Load it.
// b. No -> Load first langauge.
LanguageToLoad = 0; // First one.
if (PreferencesRef)
{
for (i = NumOfTokens; i--;)
{
if (SupportedLangArray[i] == (int)PreferencesRef->Language)
{
LanguageToLoad = i;
break;
}
}
}
else
{
// Hunt for English
for (i = NumOfTokens; i--;)
{
if (SupportedLangArray[i] == (int)EL_English)
{
LanguageToLoad = i;
break;
}
}
}
// Got our index - now load the data.
// LanguageToLoad <- index of language we wish to load.
// Need to locate the correct place in the file.
while (LanguageToLoad)
{
*Buffer = 0;
while (*Buffer != '~')
{
if (fread(Buffer, 1, 1, FH) != 1)
return false; // Should never run out of file.
// Unencrypt the char.
ChangeEncryptionState(Buffer, 1);
}
fread(Buffer, 2, 1, FH); // Read new-line chars
LanguageToLoad--;
}
int StringIndex = 0;
bool Continue = true;
bool NotEndOfToken;
int TokenLength;
int StringLength;
while (Continue)
{
// Hunt for (next) token
NotEndOfToken = true;
TokenLength = 0;
while ((Continue) && (NotEndOfToken))
{
Continue &= (fread((Buffer + TokenLength), 1, 1, FH) == 1);
if (Continue)
{
ChangeEncryptionState((Buffer + TokenLength), 1); // Unencrypt the particular character
if (*(Buffer + TokenLength) == '~')
Continue = false; // Found the end of this language section.
// See if it is the end of line delimiter.
if (*(Buffer + TokenLength) == '\n')
NotEndOfToken = false;
else
TokenLength++;
}
}
*(Buffer + TokenLength) = 0;
// Safeguard last loop.
if (Continue)
{
// Should now have valid tokens.
Index = strchr(Buffer, ','); // Find the comma between the two tokens.
*Index = 0; Index++;
StringIndex = atoi(Buffer);
StringLength = atoi(Index);
SAFELY_DELETE_C2248(StringLocalizationArray[StringIndex]);
NEW_C2248(StringLocalizationArray[StringIndex], CString());
StringLocalizationArray[StringIndex]->LoadEncodedString(FH, StringLength);
}
fread(Buffer, 2, 1, FH); // Read the line feed.
}
fclose(FH);
return true;
}
return true; // Can't load archive - not flagging this as an error as user may not want to use localization. (If they do, then specify that the file is checked somewhere).
}
// ----
// Free memory
void CLocalization::FreeLocalizationStrings()
{
for (int i = MAX_LOCALIZED_STRINGS; i--;)
{
SAFELY_DELETE(StringLocalizationArray[i]);
}
}
// ----
// Obtain a pointer to a string resource.
// The return value will be NULL if the requested
// resource does not exists or is out of bounds.
CString* CLocalization::GetString(int _Index)
{
if ( (_Index >= 0)
&& (_Index < MAX_LOCALIZED_STRINGS))
{
return StringLocalizationArray[_Index];
}
return NULL;
}