author | unC0Rr |
Thu, 18 Nov 2010 17:01:35 +0300 | |
changeset 4382 | 935de6cd5ea3 |
parent 4377 | 43945842da0c |
child 4390 | f219628ed666 |
permissions | -rw-r--r-- |
184 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
925 | 3 |
* Copyright (c) 2006-2008 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
184 | 21 |
unit uLocale; |
22 |
interface |
|
4361 | 23 |
uses uTypes; |
2863 | 24 |
|
2142 | 25 |
const MAX_EVENT_STRINGS = 100; |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
26 |
var trammo: array[TAmmoStrId] of ansistring; // name of the weapon |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
27 |
trammoc: array[TAmmoStrId] of ansistring; // caption of the weapon |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
28 |
trammod: array[TAmmoStrId] of ansistring; // description of the weapon |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
29 |
trmsg: array[TMsgStrId] of ansistring; // message of the event |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
30 |
trgoal: array[TGoalStrId] of ansistring; // message of the goal |
184 | 31 |
|
2905 | 32 |
procedure LoadLocale(FileName: shortstring); |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
33 |
function Format(fmt: shortstring; var arg: shortstring): shortstring; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
34 |
function Format(fmt: ansistring; var arg: ansistring): ansistring; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
35 |
function GetEventString(e: TEventId): ansistring; |
2140 | 36 |
|
184 | 37 |
implementation |
4377 | 38 |
uses uRandom, uUtils, uIO; |
2142 | 39 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
40 |
var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of ansistring; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
41 |
trevt_n: array[TEventId] of integer; |
184 | 42 |
|
2905 | 43 |
procedure LoadLocale(FileName: shortstring); |
2999 | 44 |
var s: ansistring; |
184 | 45 |
f: textfile; |
371 | 46 |
a, b, c: LongInt; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
47 |
first: array[TEventId] of boolean; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
48 |
e: TEventId; |
2722 | 49 |
loaded: boolean; |
184 | 50 |
begin |
2722 | 51 |
loaded:= false; |
2357
babe1a55e284
Add an empty weapon to avoid selection of weapons which aren't yet ready. Might all be useful to switch to amNothing in certain situations, like after using up all ropes, instead of bazooka.
nemo
parents:
2185
diff
changeset
|
52 |
trammo[sidNothing]:= ' '; |
2144 | 53 |
for e:= Low(TEventId) to High(TEventId) do first[e]:= true; |
2140 | 54 |
|
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2693
diff
changeset
|
55 |
{$I-} // iochecks off |
2747 | 56 |
Assign(f, FileName); |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2693
diff
changeset
|
57 |
filemode:= 0; // readonly |
2747 | 58 |
Reset(f); |
2722 | 59 |
if IOResult = 0 then loaded:= true; |
60 |
TryDo(loaded, 'Cannot load locale "' + FileName + '"', false); |
|
61 |
if loaded then |
|
62 |
begin |
|
63 |
while not eof(f) do |
|
64 |
begin |
|
65 |
readln(f, s); |
|
66 |
if Length(s) = 0 then continue; |
|
67 |
if not (s[1] in ['0'..'9']) then continue; |
|
68 |
TryDo(Length(s) > 6, 'Load locale: empty string', true); |
|
69 |
val(s[1]+s[2], a, c); |
|
70 |
TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true); |
|
71 |
TryDo(s[3] = ':', 'Load locale: ":" expected', true); |
|
72 |
val(s[4]+s[5], b, c); |
|
73 |
TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true); |
|
74 |
TryDo(s[6] = '=', 'Load locale: "=" expected', true); |
|
75 |
Delete(s, 1, 6); |
|
76 |
case a of |
|
77 |
0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then trammo[TAmmoStrId(b+1)]:= s; |
|
78 |
1: if (b >=0) and (b <= ord(High(TMsgStrId))) then trmsg[TMsgStrId(b)]:= s; |
|
79 |
2: if (b >=0) and (b <= ord(High(TEventId))) then begin |
|
4374 | 80 |
TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false); |
2722 | 81 |
if first[TEventId(b)] then |
82 |
begin |
|
83 |
trevt_n[TEventId(b)]:= 0; |
|
84 |
first[TEventId(b)]:= false; |
|
85 |
end; |
|
86 |
trevt[TEventId(b)][trevt_n[TEventId(b)]]:= s; |
|
87 |
inc(trevt_n[TEventId(b)]); |
|
88 |
end; |
|
2747 | 89 |
3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then trammoc[TAmmoStrId(b+1)]:= s; |
90 |
4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then trammod[TAmmoStrId(b+1)]:= s; |
|
2863 | 91 |
5: if (b >=0) and (b <= ord(High(TGoalStrId))) then trgoal[TGoalStrId(b)]:= s; |
2722 | 92 |
end; |
93 |
end; |
|
94 |
Close(f) |
|
95 |
end; |
|
184 | 96 |
{$I+} |
97 |
end; |
|
98 |
||
2905 | 99 |
function GetEventString(e: TEventId): ansistring; |
2140 | 100 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
101 |
if trevt_n[e] = 0 then // no messages for this event type? |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
102 |
GetEventString:= '*missing translation*' |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
103 |
else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
104 |
GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it |
2140 | 105 |
end; |
106 |
||
184 | 107 |
function Format(fmt: shortstring; var arg: shortstring): shortstring; |
371 | 108 |
var i: LongInt; |
184 | 109 |
begin |
110 |
i:= Pos('%1', fmt); |
|
351 | 111 |
if i = 0 then Format:= fmt |
112 |
else Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg) |
|
184 | 113 |
end; |
114 |
||
2905 | 115 |
function Format(fmt: ansistring; var arg: ansistring): ansistring; |
116 |
var i: LongInt; |
|
117 |
begin |
|
118 |
i:= Pos('%1', fmt); |
|
119 |
if i = 0 then Format:= fmt |
|
120 |
else Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg) |
|
121 |
end; |
|
122 |
||
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
123 |
procedure LoadLocaleWrapper(str: pchar); cdecl; export; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
124 |
begin |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
125 |
LoadLocale(Strpas(str)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
126 |
end; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
127 |
|
184 | 128 |
end. |