author | sheepluva |
Mon, 20 Jan 2014 10:16:29 +0100 | |
changeset 10024 | 7d21f52f0452 |
parent 10009 | 88929358d2e1 |
child 10015 | 4feced261c68 |
permissions | -rw-r--r-- |
184 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
9998 | 3 |
* Copyright (c) 2004-2014 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; |
184 | 26 |
|
2905 | 27 |
procedure LoadLocale(FileName: shortstring); |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
28 |
function Format(fmt: shortstring; var arg: shortstring): shortstring; |
10009 | 29 |
function FormatA(fmt: PChar; arg: ansistring): ansistring; |
30 |
function GetEventString(e: TEventId): PChar; |
|
31 |
procedure initModule; |
|
32 |
procedure freeModule; |
|
2140 | 33 |
|
7048
0a4c88935902
the ios port runs again, although with a few things to sort out
koda
parents:
6700
diff
changeset
|
34 |
{$IFDEF HWLIBRARY} |
0a4c88935902
the ios port runs again, although with a few things to sort out
koda
parents:
6700
diff
changeset
|
35 |
procedure LoadLocaleWrapper(str: pchar); cdecl; export; |
0a4c88935902
the ios port runs again, although with a few things to sort out
koda
parents:
6700
diff
changeset
|
36 |
{$ENDIF} |
0a4c88935902
the ios port runs again, although with a few things to sort out
koda
parents:
6700
diff
changeset
|
37 |
|
184 | 38 |
implementation |
10009 | 39 |
uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer, sysutils; |
2142 | 40 |
|
10009 | 41 |
var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of PChar; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
42 |
trevt_n: array[TEventId] of integer; |
184 | 43 |
|
2905 | 44 |
procedure LoadLocale(FileName: shortstring); |
10009 | 45 |
var s, sc: PChar; |
8028 | 46 |
f: pfsFile; |
371 | 47 |
a, b, c: LongInt; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
48 |
first: array[TEventId] of boolean; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
49 |
e: TEventId; |
184 | 50 |
begin |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
51 |
for e:= Low(TEventId) to High(TEventId) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
52 |
first[e]:= true; |
2140 | 53 |
|
8028 | 54 |
f:= pfsOpenRead(FileName); |
55 |
TryDo(f <> nil, 'Cannot load locale "' + FileName + '"', false); |
|
56 |
||
57 |
if f <> nil then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
58 |
begin |
8028 | 59 |
while not pfsEof(f) do |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
60 |
begin |
8107 | 61 |
pfsReadLnA(f, s); |
10009 | 62 |
if (Length(s) > 0) and (s[0] >= '0') and (s[0] <= '9') then |
63 |
begin |
|
64 |
TryDo(Length(s) > 6, 'Load locale: empty string', true); |
|
65 |
val(s[0]+s[1], a, c); |
|
66 |
TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true); |
|
67 |
TryDo(s[2] = ':', 'Load locale: ":" expected', true); |
|
68 |
val(s[3]+s[4], b, c); |
|
69 |
TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true); |
|
70 |
TryDo(s[5] = '=', 'Load locale: "=" expected', true); |
|
71 |
sc:= StrAlloc(length(s) - 5); |
|
72 |
StrCopy(sc, @s[6]); |
|
73 |
case a of |
|
74 |
0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then |
|
75 |
trammo[TAmmoStrId(b)]:= sc; |
|
76 |
1: if (b >=0) and (b <= ord(High(TMsgStrId))) then |
|
77 |
trmsg[TMsgStrId(b)]:= sc; |
|
78 |
2: if (b >=0) and (b <= ord(High(TEventId))) then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
79 |
begin |
10009 | 80 |
TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false); |
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)]]:= sc; |
|
87 |
inc(trevt_n[TEventId(b)]); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
88 |
end; |
10009 | 89 |
3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then |
90 |
trammoc[TAmmoStrId(b)]:= sc; |
|
91 |
4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then |
|
92 |
trammod[TAmmoStrId(b)]:= sc; |
|
93 |
5: if (b >=0) and (b <= ord(High(TGoalStrId))) then |
|
94 |
trgoal[TGoalStrId(b)]:= sc; |
|
95 |
end; |
|
96 |
end; |
|
97 |
StrDispose(s); |
|
98 |
end; |
|
8028 | 99 |
pfsClose(f); |
2722 | 100 |
end; |
184 | 101 |
end; |
102 |
||
10009 | 103 |
function GetEventString(e: TEventId): PChar; |
2140 | 104 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
105 |
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
|
106 |
GetEventString:= '*missing translation*' |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
107 |
else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2908
diff
changeset
|
108 |
GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it |
2140 | 109 |
end; |
110 |
||
184 | 111 |
function Format(fmt: shortstring; var arg: shortstring): shortstring; |
371 | 112 |
var i: LongInt; |
184 | 113 |
begin |
114 |
i:= Pos('%1', fmt); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
115 |
if i = 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
116 |
Format:= fmt |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
117 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
118 |
Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg) |
184 | 119 |
end; |
120 |
||
10009 | 121 |
function FormatA(fmt: PChar; arg: ansistring): ansistring; |
2905 | 122 |
var i: LongInt; |
10009 | 123 |
s: ansistring; |
2905 | 124 |
begin |
10009 | 125 |
s:= fmt; |
126 |
||
127 |
i:= Pos('%1', s); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
128 |
if i = 0 then |
10009 | 129 |
FormatA:= s |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6528
diff
changeset
|
130 |
else |
10009 | 131 |
FormatA:= copy(s, 1, i - 1) + arg + FormatA(PChar(copy(s, i + 2, Length(s) - i - 1)), arg) |
2905 | 132 |
end; |
133 |
||
7048
0a4c88935902
the ios port runs again, although with a few things to sort out
koda
parents:
6700
diff
changeset
|
134 |
{$IFDEF HWLIBRARY} |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
135 |
procedure LoadLocaleWrapper(str: pchar); cdecl; export; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
136 |
begin |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
137 |
LoadLocale(Strpas(str)); |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
138 |
end; |
7048
0a4c88935902
the ios port runs again, although with a few things to sort out
koda
parents:
6700
diff
changeset
|
139 |
{$ENDIF} |
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3774
diff
changeset
|
140 |
|
10009 | 141 |
procedure initModule; |
142 |
var e: TEventId; |
|
143 |
i: LongInt; |
|
144 |
begin |
|
145 |
for e:= Low(TEventId) to High(TEventId) do |
|
146 |
for i:= 0 to Pred(MAX_EVENT_STRINGS) do |
|
147 |
trevt[e][i]:= nil; |
|
148 |
end; |
|
149 |
||
150 |
procedure freeModule; |
|
151 |
var e: TEventId; |
|
152 |
i: LongInt; |
|
153 |
begin |
|
154 |
for e:= Low(TEventId) to High(TEventId) do |
|
155 |
for i:= 0 to Pred(trevt_n[e]) do |
|
156 |
StrDispose(trevt[e][i]); |
|
157 |
end; |
|
158 |
||
184 | 159 |
end. |