-
Notifications
You must be signed in to change notification settings - Fork 0
/
LangPlus.inc
566 lines (448 loc) · 15.3 KB
/
LangPlus.inc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#if defined _inc_LangPlus
#undef _inc_LangPlus
#endif
#if defined _LangPlus_included
#endinput
#endif
#define _LangPlus_included
#if !defined PP_SYNTAX_FOR_POOL
#define PP_SYNTAX_FOR_POOL
#endif
#if !defined PP_SYNTAX_FOR_MAP
#define PP_SYNTAX_FOR_MAP
#endif
#include <open.mp>
#include <sscanf2>
#include <PawnPlus>
#include <strlib>
#tryinclude <YSI_Coding\y_va>
#tryinclude <YSI_Data\y_iterate>
#tryinclude <logger>
#if !defined MAX_LANGUAGES
#define MAX_LANGUAGES 4
#endif
#if !defined DELIMITER_CHAR
#define DELIMITER_CHAR "="
#endif
// the directory language data is stored in
#if !defined DIRECTORY_LANGUAGES
#define DIRECTORY_LANGUAGES "languages/"
#endif
// the maximum length of a language key
#if !defined MAX_LANGUAGE_KEY_LEN
#define MAX_LANGUAGE_KEY_LEN (32)
#endif
// the maximum length of a language text entry
#if !defined MAX_LANGUAGE_ENTRY_LENGTH
#define MAX_LANGUAGE_ENTRY_LENGTH (768)
#endif
// name limit for a language
#if !defined MAX_LANGUAGE_NAME
#define MAX_LANGUAGE_NAME (32)
#endif
// Max file name length for a language file
#if !defined MAX_FILE_NAME
#define MAX_FILE_NAME (64)
#endif
#if !defined MAX_REPLACEMENT_KEY_LEN
#define MAX_REPLACEMENT_KEY_LEN (16)
#endif
#if !defined MAX_REPLACEMENT_VALUE_LEN
#define MAX_REPLACEMENT_VALUE_LEN (16)
#endif
#define INVALID_LANGUAGE_ID (Language:-1)
#if defined SS_LANG_COMPATIBILITY
#define DefineLanguageReplacement LangPlus_SetStringReplacement
#define InitLanguage LangPlus_LoadLanguage
#define AddLanguageEntry LangPlus_AddLanguageEntry
#define GetLanguageString LangPlus_GetLanguageString
#define GetLanguageList LangPlus_GetLanguageList
#define GetLanguageID LangPlus_GetLanguageId
#define GetLanguageName LangPlus_GetLanguageName
#define GetPlayerLanguage LangPlus_GetPlayerLanguage
#define SetPlayerLanguage LangPlus_SetPlayerLanguage
#define @L(%0,%1) LangPlus_ReturnLanguageString(LangPlus_GetPlayerLanguage(%0), %1)
#endif
static Map:s_LanguageMap[Language:MAX_LANGUAGES],
Pool:s_LanguagePool,
Map:s_StringReplacements;
static Language:s_PlayerLanguage[MAX_PLAYERS];
public OnGameModeInit() {
s_LanguagePool = pool_new(MAX_LANGUAGES);
#if defined LangPlus_OnGameModeInit
return LangPlus_OnGameModeInit();
#else
return 1;
#endif
}
public OnPlayerConnect(playerid) {
s_PlayerLanguage[playerid] = Language:0;
#if defined LangPlus_OnPlayerConnect
return LangPlus_OnPlayerConnect(playerid);
#else
return 1;
#endif
}
static Language:_InitLanguage(const string:langName[]) {
if (pool_size(s_LanguagePool) == MAX_LANGUAGES) {
#if(defined _logger_included)
Logger_Dbg("langplus", "too many languages",
Logger_S("language", langName));
#endif
return INVALID_LANGUAGE_ID;
}
// pool_find_str returns the index, it's not a bool!
if (pool_find_str(s_LanguagePool, langName) != -1) {
#if(defined _logger_included)
Logger_Dbg("langplus", "language already exists",
Logger_S("language", langName));
#endif
return INVALID_LANGUAGE_ID;
}
return Language:pool_add_str(s_LanguagePool, langName);
}
static bool:LangPlus_IsValidLanguage(Language:id) {
if (!pool_valid(s_LanguagePool)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "no languages have been initialized");
#endif
return false;
}
if (_:id < 0 || _:id >= pool_size(s_LanguagePool)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid language id",
Logger_I("languageid", _:id));
#endif
return false;
}
if (!map_valid(s_LanguageMap[id])) {
#if(defined _logger_included)
Logger_Dbg("langplus", "language hasn't been initialized ",
Logger_I("languageid", _:id));
#endif
return false;
}
return pool_has(s_LanguagePool, _:id);
}
LangPlus_AddLanguageEntry(const Language:languageid, const string:key[], const string:val[]) {
if (!LangPlus_IsValidLanguage(languageid) || key[0] == EOS || val[0] == EOS) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid language id or key/value",
Logger_I("languageid", _:languageid),
Logger_S("key", key),
Logger_S("val", val));
#endif
return false;
}
if (!pool_has(s_LanguagePool, _:languageid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid language id",
Logger_I("languageid", _:languageid));
#endif
return false;
}
if (map_has_str_key(s_LanguageMap[languageid], key)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "language key already exists",
Logger_S("key", key));
#endif
return false;
}
map_str_add_str(s_LanguageMap[languageid], key, val);
return true;
}
// This one shouldn't be a stock.
// Importing the library and not using it is pointless
Language:LangPlus_LoadLanguage(const string:langName[]) {
new Language:languageid = _InitLanguage(langName);
if (languageid == INVALID_LANGUAGE_ID) {
#if(defined _logger_included)
Logger_Dbg("langplus", "unable to initialize language",
Logger_S("language", langName));
#endif
return INVALID_LANGUAGE_ID;
}
s_LanguageMap[languageid] = map_new();
new fileName[MAX_FILE_NAME],
File:fHandler,
line[MAX_LANGUAGE_KEY_LEN + 1 + MAX_LANGUAGE_ENTRY_LENGTH],
lineNumber = 1,
lineLength;
format(fileName, sizeof(fileName), #DIRECTORY_LANGUAGES "%s.ini", langName);
fHandler = fopen(fileName, io_read);
if (!fHandler) {
#if(defined _logger_included)
Logger_Dbg("langplus", "unable to open file",
Logger_S("fileName", fileName));
#endif
return INVALID_LANGUAGE_ID;
}
new key[MAX_LANGUAGE_KEY_LEN],
val[MAX_LANGUAGE_ENTRY_LENGTH],
valFormatted[MAX_LANGUAGE_ENTRY_LENGTH];
while (fread(fHandler, line)) {
lineLength = strlen(line);
if (lineLength < 3) {
// Minimum length of a line is 3 characters: "a=b"
continue;
}
if (
(line[0] < 97 || line[0] > 122)
&& (line[0] < 65 || line[0] > 90)
) {
// Skip lines that don't start with a letter (most likely comments)
// We also skip sections. These are only for users and translation software
continue;
}
if (sscanf(line, "p<"#DELIMITER_CHAR">s["#MAX_LANGUAGE_KEY_LEN"]s["#MAX_LANGUAGE_ENTRY_LENGTH"]", key, val)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid line format",
Logger_I("line", lineNumber),
Logger_S("file", fileName),
Logger_S("line", line));
#endif
return INVALID_LANGUAGE_ID;
}
strfromliteral(valFormatted, val);
if (map_valid(s_StringReplacements)) {
for_map(it : s_StringReplacements) {
new replacementKey[MAX_REPLACEMENT_KEY_LEN + 1],
replacementValue[MAX_REPLACEMENT_VALUE_LEN + 1];
iter_get_key_str(it, replacementKey);
iter_get_value_str(it, replacementValue);
strreplace(valFormatted, replacementKey, replacementValue);
}
}
LangPlus_AddLanguageEntry(languageid, key, valFormatted);
lineNumber++;
}
fclose(fHandler);
return languageid;
}
stock bool:LangPlus_SetStringReplacement(const string:key[], const string:value[]) {
if (key[0] == EOS || value[0] == EOS) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid key/value",
Logger_S("key", key),
Logger_S("value", value));
#endif
return false;
}
if (strlen(key) > MAX_REPLACEMENT_KEY_LEN) {
#if(defined _logger_included)
Logger_Dbg("langplus", "key is too long",
Logger_S("key", key));
#endif
return false;
}
if (strlen(value) > MAX_REPLACEMENT_VALUE_LEN) {
#if(defined _logger_included)
Logger_Dbg("langplus", "value is too long",
Logger_S("value", value));
#endif
return false;
}
if (!map_valid(s_StringReplacements)) {
s_StringReplacements = map_new();
}
if (map_has_str_key(s_StringReplacements, key)) {
#if(defined _logger_included)
Logger_Log("replacement key already exists",
Logger_S("key", key));
#endif
return false;
}
map_str_add_str(s_StringReplacements, key, value);
return true;
}
stock Language:LangPlus_GetLanguageId(const string:language[]) {
if (pool_size(s_LanguagePool) == 0) {
#if(defined _logger_included)
Logger_Dbg("langplus", "no languages have been initialized");
#endif
return INVALID_LANGUAGE_ID;
}
// Index of not existing languages is -1, so it equals INVALID_LANGUAGE_ID
return Language:pool_find_str(s_LanguagePool, language);
}
stock LangPlus_GetLanguageList(string:languages[][]) {
if (pool_size(s_LanguagePool) == 0) {
#if(defined _logger_included)
Logger_Dbg("langplus", "no languages have been initialized");
#endif
return 0;
}
new langName[MAX_LANGUAGE_NAME + 1],
index;
for_pool(it : s_LanguagePool) {
iter_get_value_str(it, langName);
strcopy(languages[index], langName, MAX_LANGUAGE_NAME);
index++;
}
return pool_size(s_LanguagePool);
}
stock bool:LangPlus_GetLanguageName(Language:languageid, string:output[] = "", len = sizeof(output)) {
if (!LangPlus_IsValidLanguage(languageid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid language id",
Logger_I("languageid", _:languageid));
#endif
return false;
}
pool_get_value_str(s_LanguagePool, _:languageid, output, len);
return output[0] != '\0';
}
stock bool:LangPlus_GetLanguageString(Language:languageid, const string:key[], string:output[], len = sizeof(output)) {
if (!LangPlus_IsValidLanguage(languageid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid language id",
Logger_I("languageid", _:languageid));
#endif
return false;
}
if (!map_has_str_key(s_LanguageMap[languageid], key)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "language key does not exist",
Logger_I("languageid", _:languageid),
Logger_S("key", key));
#endif
if (languageid != Language:0) {
return LangPlus_GetLanguageString(Language:0, key, output, len);
}
#if(defined _logger_included)
Logger_Log("language key does not exist in the default language",
Logger_I("languageid", _:languageid),
Logger_S("key", key));
#endif
strcpy(output, key, MAX_LANGUAGE_ENTRY_LENGTH);
return true;
}
map_str_get_str(s_LanguageMap[languageid], key, output, len);
return true;
}
stock LangPlus_ReturnLanguageString(Language:languageid, const string:key[]) {
new string:result[MAX_LANGUAGE_ENTRY_LENGTH + 1];
LangPlus_GetLanguageString(languageid, key, result);
return result;
}
stock bool:LangPlus_SetPlayerLanguage(playerid, Language:languageid) {
if (!LangPlus_IsValidLanguage(languageid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid language id",
Logger_I("languageid", _:languageid));
#endif
return false;
}
if (!IsPlayerConnected(playerid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid player id",
Logger_I("playerid", playerid));
#endif
return false;
}
s_PlayerLanguage[playerid] = languageid;
return true;
}
stock Language:LangPlus_GetPlayerLanguage(playerid) {
if (!IsPlayerConnected(playerid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid player id",
Logger_I("playerid", playerid));
#endif
return INVALID_LANGUAGE_ID;
}
return s_PlayerLanguage[playerid];
}
stock bool:LangPlus_SetPlayerLanguageByName(playerid, const string:language[]) {
return LangPlus_SetPlayerLanguage(playerid, LangPlus_GetLanguageId(language));
}
stock Language:LangPlus_GetPlayerLanguageName(playerid, string:output[] = "", len = sizeof(output)) {
if (!IsPlayerConnected(playerid)) {
#if(defined _logger_included)
Logger_Dbg("langplus", "invalid player id",
Logger_I("playerid", playerid));
#endif
return INVALID_LANGUAGE_ID;
}
LangPlus_GetLanguageName(s_PlayerLanguage[playerid], output);
return s_PlayerLanguage[playerid];
}
#if defined _INC_y_va
#if defined LANGPLUS_REPLACE_NATIVES
LangPlus_SendClientMessage(playerid, colour, const format[], OPEN_MP_TAGS:...) {
new Language:playerLanguage;
playerLanguage = LangPlus_GetPlayerLanguage(playerid);
if (playerLanguage == INVALID_LANGUAGE_ID) {
return SendClientMessage(playerid, colour, format, ___(3));
}
new langMessage[144];
new bool:ret;
ret = LangPlus_GetLanguageString(playerLanguage, format, langMessage);
if (ret) {
SendClientMessage(playerid, colour, langMessage, ___(3));
return true;
}
SendClientMessage(playerid, colour, format, ___(3));
return true;
}
bool:LangPlus_SendClientMessageToAll(color, const string:msg[], OPEN_MP_TAGS:...) {
foreach (new i : Player) {
SendClientMessage(
i,
color,
msg,
___(2)
);
}
return true;
}
#else
stock bool:LangPlus_SendClientMessage(playerid, color, const string:msg[], OPEN_MP_TAGS:...) {
return SendClientMessage(
playerid,
color,
LangPlus_ReturnLanguageString(LangPlus_GetPlayerLanguage(playerid), msg),
___(3)
);
}
stock bool:LangPlus_SendClientMessageToAll(color, const string:msg[], OPEN_MP_TAGS:...) {
foreach (new i : Player) {
SendClientMessage(
i,
color,
LangPlus_ReturnLanguageString(LangPlus_GetPlayerLanguage(i), msg),
___(2)
);
}
return true;
}
#endif
#endif
// ALS
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit LangPlus_OnGameModeInit
#if defined LangPlus_OnGameModeInit
forward LangPlus_OnGameModeInit();
#endif
#if defined _ALS_OnPlayerConnect
#undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect LangPlus_OnPlayerConnect
#if defined LangPlus_OnPlayerConnect
forward LangPlus_OnPlayerConnect(playerid);
#endif
#if defined LANGPLUS_REPLACE_NATIVES
#if defined _ALS_SendClientMessage
#undef SendClientMessage
#else
#define _ALS_SendClientMessage
#endif
#define SendClientMessage LangPlus_SendClientMessage
#define SendClientMessageToAll LangPlus_SendClientMessageToAll
#endif