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