-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMenuStrings.cpp
349 lines (282 loc) · 8.62 KB
/
MenuStrings.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
MenuStrings.cpp - custom menu strings
Copyright (C) 2011 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <ctype.h>
#include "extdll_menu.h"
#include "BaseMenu.h"
#include "Utils.h"
#include "MenuStrings.h"
#include "utl/unicode_strtools.h"
namespace ui {
#define EMPTY_STRINGS_1 ""
#define EMPTY_STRINGS_2 EMPTY_STRINGS_1, EMPTY_STRINGS_1
#define EMPTY_STRINGS_5 EMPTY_STRINGS_2, EMPTY_STRINGS_2, EMPTY_STRINGS_1
#define EMPTY_STRINGS_10 EMPTY_STRINGS_5, EMPTY_STRINGS_5
#define EMPTY_STRINGS_20 EMPTY_STRINGS_10, EMPTY_STRINGS_10
#define EMPTY_STRINGS_50 EMPTY_STRINGS_20, EMPTY_STRINGS_20, EMPTY_STRINGS_10
#define EMPTY_STRINGS_100 EMPTY_STRINGS_50, EMPTY_STRINGS_50
#define HASH_SIZE 256 // 256 * 4 * 4 == 4096 bytes
static struct dictionary_t
{
const char *name;
const char *value;
dictionary_t *next;
} *hashed_cmds[HASH_SIZE];
const char *MenuStrings[IDS_LAST] =
{
EMPTY_STRINGS_100, // 0..9
EMPTY_STRINGS_20, // 100..119
EMPTY_STRINGS_10, // 120..129
EMPTY_STRINGS_2, // 130..131
"Display mode", // 132
EMPTY_STRINGS_5, // 133..137
EMPTY_STRINGS_2, // 138..139
EMPTY_STRINGS_20, // 140..159
EMPTY_STRINGS_10, // 160..169
EMPTY_STRINGS_1, // 170
"Reverse mouse", // 171
EMPTY_STRINGS_10, // 172..181
EMPTY_STRINGS_2, // 182..183
"Mouse sensitivity", // 184
EMPTY_STRINGS_1, // 185
EMPTY_STRINGS_2, // 186..187
"返回游戏", // 188
"单机试玩", // 189
EMPTY_STRINGS_1, // 190
"Load a previously saved game.", // 191
"Load a saved game, save the current game.", // 192
"游戏设置", // 193
EMPTY_STRINGS_20, // 194..213
EMPTY_STRINGS_20, // 214..233
"Starting a Hazard Course will exit\nany current game, OK to exit?", // 234
EMPTY_STRINGS_1, // 235
"Are you sure you want to quit?", // 236
EMPTY_STRINGS_2, // 237..238
EMPTY_STRINGS_1, // 239
"Starting a new game will exit\nany current game, OK to exit?", // 240
EMPTY_STRINGS_5, // 241..245
EMPTY_STRINGS_2, // 246..247
EMPTY_STRINGS_2, // 248..249
EMPTY_STRINGS_100, // 250..349
EMPTY_STRINGS_50, // 350..399
"Find more about CSMoE", // 400
EMPTY_STRINGS_1, // 401
"https://github.com/MoeMod/CSMoE", // 402
EMPTY_STRINGS_5, // 403..407
EMPTY_STRINGS_2, // 408..409
EMPTY_STRINGS_100, // 410..509
EMPTY_STRINGS_20, // 510..529
"Select a custom game", // 530
EMPTY_STRINGS_5, // 531..535
EMPTY_STRINGS_2, // 536..537
EMPTY_STRINGS_2, // 538..539
EMPTY_STRINGS_50, // 540..589
EMPTY_STRINGS_10, // 590..599
};
/*
=================
Com_HashKey
returns hash key for string
=================
*/
static uint Com_HashKey( const char *string, uint hashSize )
{
uint i, hashKey = 0;
for( i = 0; string[i]; i++ )
{
hashKey = (hashKey + i) * 37 + tolower( string[i] );
}
return (hashKey % hashSize);
}
static inline dictionary_t *Dictionary_FindInBucket( dictionary_t *bucket, const char *name )
{
dictionary_t *i = bucket;
for( ; i && stricmp( name, i->name ); // filter out
i = i->next );
return i;
}
static void Dictionary_Insert( const char *name, const char *second )
{
uint hash = Com_HashKey( name, HASH_SIZE );
dictionary_t *elem;
elem = new dictionary_t;
elem->name = StringCopy(name);
elem->value = StringCopy(second);
elem->next = hashed_cmds[hash];
hashed_cmds[hash] = elem;
}
static inline dictionary_t *Dictionary_GetBucket( const char *name )
{
return hashed_cmds[ Com_HashKey( name, HASH_SIZE ) ];
}
static void UI_InitAliasStrings( void )
{
char token[1024];
// some strings needs to be initialized here
sprintf( token, "Quit %s without\nsaving current game?", gMenu.m_gameinfo.title );
MenuStrings[IDS_MAIN_QUITPROMPTINGAME] = StringCopy( token );
sprintf( token, "Learn how to play %s", gMenu.m_gameinfo.title );
MenuStrings[IDS_MAIN_TRAININGHELP] = StringCopy( token );
sprintf( token, "Play %s on the 'easy' skill setting", gMenu.m_gameinfo.title );
MenuStrings[IDS_NEWGAME_EASYHELP] = StringCopy( token );
sprintf( token, "Play %s on the 'medium' skill setting", gMenu.m_gameinfo.title );
MenuStrings[IDS_NEWGAME_MEDIUMHELP] = StringCopy( token );
sprintf( token, "Play %s on the 'difficult' skill setting", gMenu.m_gameinfo.title );
MenuStrings[IDS_NEWGAME_DIFFICULTHELP] = StringCopy( token );
sprintf( token, "退出 %s", gMenu.m_gameinfo.title );
MenuStrings[IDS_MAIN_QUITHELP] = StringCopy( token );
sprintf( token, "联机游戏" );
MenuStrings[IDS_MAIN_MULTIPLAYERHELP] = StringCopy( token );
}
static void Localize_AddToDictionary( const char *name, const char *lang )
{
char filename[64];
snprintf( filename, sizeof( filename ), "resource/%s_%s.txt", name, lang );
int unicodeLength;
uchar16 *unicodeBuf = (uchar16*)EngFuncs::COM_LoadFile( filename, &unicodeLength );
if( unicodeBuf ) // no problem, so read it.
{
int ansiLength = unicodeLength / 2;
char *afile = new char[ansiLength]; // save original pointer, so we can free it later
char *pfile = afile;
char token[4096];
int i = 0;
Q_UTF16ToUTF8( unicodeBuf + 1, afile, ansiLength, STRINGCONVERT_ASSERT_REPLACE );
pfile = EngFuncs::COM_ParseFile( pfile, token );
if( stricmp( token, "lang" ))
{
Con_Printf( "Localize_AddToDict( %s, %s ): invalid header, got %s", name, lang, token );
goto error;
}
pfile = EngFuncs::COM_ParseFile( pfile, token );
if( strcmp( token, "{" ))
{
Con_Printf( "Localize_AddToDict( %s, %s ): want {, got %s", name, lang, token );
goto error;
}
pfile = EngFuncs::COM_ParseFile( pfile, token );
if( stricmp( token, "Language" ))
{
Con_Printf( "Localize_AddToDict( %s, %s ): want Language, got %s", name, lang, token );
goto error;
}
// skip language actual name
pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token );
if( stricmp( token, "Tokens" ))
{
Con_Printf( "Localize_AddToDict( %s, %s ): want Tokens, got %s", name, lang, token );
goto error;
}
pfile = EngFuncs::COM_ParseFile( pfile, token );
if( strcmp( token, "{" ))
{
Con_Printf( "Localize_AddToDict( %s, %s ): want { after Tokens, got %s", name, lang, token );
goto error;
}
while( (pfile = EngFuncs::COM_ParseFile( pfile, token )))
{
if( !strcmp( token, "}" ))
break;
char szLocString[4096];
pfile = EngFuncs::COM_ParseFile( pfile, szLocString );
if( !strcmp( szLocString, "}" ))
break;
if( pfile )
{
Dictionary_Insert( token, szLocString );
i++;
}
}
Con_Printf( "Localize_AddToDict: loaded %i words from %s\n", i, filename );
error:
delete[] afile;
EngFuncs::COM_FreeFile( unicodeBuf );
}
else
{
Con_Printf( "Couldn't open file %s. Strings will not be localized!.\n", filename );
}
}
static void Localize_Init( void )
{
char gamedir[256];
EngFuncs::GetGameDir( gamedir );
memset( hashed_cmds, 0, sizeof( hashed_cmds ) );
// first is lowest in priority
if( strcmp( gamedir, "gameui" )) // just for case
Localize_AddToDictionary( "gameui", "english" );
Localize_AddToDictionary( "valve", "english" );
if( strcmp( gamedir, "valve" ))
Localize_AddToDictionary( gamedir, "english" );
}
static void Localize_Free( void )
{
for( int i = 0; i < HASH_SIZE; i++ )
{
dictionary_t *base = hashed_cmds[i];
while( base )
{
dictionary_t *next = base->next;
delete [] base->value;
delete [] base->name;
delete base;
base = next;
}
}
return;
}
void UI_LoadCustomStrings( void )
{
char *afile = (char *)EngFuncs::COM_LoadFile( "gfx/shell/strings.lst", NULL );
char *pfile = afile;
char token[1024];
int string_num;
UI_InitAliasStrings ();
Localize_Init();
if( !afile )
return;
while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
{
if( isdigit( token[0] ))
{
string_num = atoi( token );
// check for bad stiringnum
if( string_num < 0 ) continue;
if( string_num > ( IDS_LAST - 1 ))
continue;
}
else continue; // invalid declaration ?
// parse new string
pfile = EngFuncs::COM_ParseFile( pfile, token );
MenuStrings[string_num] = StringCopy( token ); // replace default string with custom
}
EngFuncs::COM_FreeFile( afile );
}
const char *Localize( const char *szStr ) // L means Localize!
{
if( szStr )
{
if( *szStr == '#' )
szStr++;
dictionary_t *base = Dictionary_GetBucket( szStr );
dictionary_t *found = Dictionary_FindInBucket( base, szStr );
if( found )
return found->value;
}
return szStr;
}
void UI_FreeCustomStrings( void )
{
Localize_Free();
}
}