Debian Codegraph @

tools/genpot

raw
1 #!/usr/bin/perl
2 #
3 # Seven Kingdoms: Ancient Adversaries
4 #
5 # Copyright 1997,1998 Enlight Software Ltd.
6 # Copyright 2017 Jesse Allen
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21 #
22
23 use warnings;
24 use strict;
25
26 use FindBin;
27 use lib $FindBin::Bin;
28
29 use File::Spec;
30
31 use dbf;
32
33 if (!@ARGV) {
34 print "Usage: $0 datadir\n";
35 exit 0;
36 }
37 my ($datadir) = @ARGV;
38
39 if (!defined($ENV{DERESX})) {
40 $ENV{DERESX} = File::Spec->catfile($FindBin::Bin, "deresx");
41 }
42
43 system($ENV{DERESX}, respath("STD.SET"), stdpath(), ".dbf");
44 if (! -d stdpath()) {
45 warn "Error: Expecting STD.SET unpacked in ", stdpath(), "\n";
46 }
47
48 system($ENV{DERESX}, respath("TUT_INTR.RES"), tut_intr_path(), ".TXT");
49 if (! -d tut_intr_path()) {
50 warn "Error: Expecting TUT_INTR.RES unpacked in ", tut_intr_path(), "\n";
51 }
52
53 system($ENV{DERESX}, respath("TUT_TEXT.RES"), tut_text_path(), ".TXT");
54 if (! -d tut_text_path()) {
55 warn "Error: Expecting TUT_TEXT.RES unpacked in ", tut_text_path(), "\n";
56 }
57
58 my @dbf_list = (
59 [
60 respath('TUT_LIST.RES'),
61 ['DES', ''], # Tutorial|Title
62 ],[
63 stdpath('00003-RAW.dbf'),
64 ['NAME', ''], # Raw|Name
65 ],[
66 stdpath('00005-UNIT.dbf'),
67 ['NAME', ''], # Unit|Name
68 ],[
69 stdpath('00006-RACE.dbf'),
70 ['NAME', ''], # Race|Name
71 ],[
72 stdpath('00007-FIRM.dbf'),
73 ['NAME', ''], # Firm|Name
74 ['SHORTNAME', ''], # Firm|Shortname
75 ['OVERTITLE', ''], # Unit|Occupation
76 ['WORKTITLE', ''], # Unit|Occupation
77 #],[
78 # This is not used in the game anywhere. Probaby an early unit file.
79 # stdpath('00019-UNIT2.dbf'),
80 # ['NAME', 'Unit|Name'],
81 #],[
82 # This just duplicates monster names, so skip this.
83 # stdpath('00027-MONSTER.dbf'),
84 # ['NAME', 'Unit|Name'],
85 ]);
86
87 print "# POT extraction from 7KAA data files\n";
88 foreach (@dbf_list) {
89 pot_extract_dbf(@$_);
90 }
91
92 foreach (glob(tut_intr_path("*.TXT"))) {
93 tut_text_extract($_, 0);
94 }
95
96 foreach (glob(tut_text_path("*.TXT"))) {
97 tut_text_extract($_, 1);
98 }
99
100 foreach (glob(sct1_text_path("*.SCT"))) {
101 sct_text_extract($_);
102 }
103
104 foreach (glob(sct2_text_path("*.SCT"))) {
105 sct_text_extract($_);
106 }
107
108 help_res_extract(help_res_path());
109
110 exit 0;
111
112 sub respath {
113 return File::Spec->catfile($datadir, 'RESOURCE', @_);
114 }
115
116 sub stdpath {
117 return File::Spec->catfile($datadir, 'RESOURCE', 'STD', @_);
118 }
119
120 sub tut_intr_path {
121 return File::Spec->catfile($datadir, 'RESOURCE', 'TUT_INTR', @_);
122 }
123
124 sub tut_text_path {
125 return File::Spec->catfile($datadir, 'RESOURCE', 'TUT_TEXT', @_);
126 }
127
128 sub sct1_text_path {
129 return File::Spec->catfile($datadir, 'SCENARIO', @_);
130 }
131
132 sub sct2_text_path {
133 return File::Spec->catfile($datadir, 'SCENARI2', @_);
134 }
135
136 sub help_res_path {
137 return File::Spec->catfile($datadir, 'RESOURCE', 'HELP.RES');
138 }
139
140 # implements FileTxt::read_line()
141 sub read_line {
142 my $fh;
143
144 ($fh) = @_;
145
146 # force scalar context to read one line
147 return dbf::trim(scalar(<$fh>));
148 }
149
150 sub read_paragraph {
151 }
152
153 sub get_msgid {
154 my $file;
155 my $msgctxt;
156 my $msgid;
157 my $loc;
158
159 ($file, $msgctxt, $msgid, $loc) = @_;
160
161 if (defined($msgctxt) && $msgctxt ne "") {
162 $msgctxt = "msgctxt \"$msgctxt\"\n";
163 } else {
164 $msgctxt = "";
165 }
166 if (defined($loc)) {
167 $loc = ":$loc";
168 } else {
169 $loc = "";
170 }
171 $msgid =~ s/\r\n/ /g; # get_paragraph converts to space
172 #$msgid =~ s/\n/'\\n'/g;
173 $msgid =~ s/"/\\"/g;
174
175 if ($msgid =~ /[\x00-\x1F\x7F-\x9F]/) {
176 print "\n# Next entry has characters that doesn't conform to ISO 8859-1\n";
177 }
178
179 return <<EOF;
180
181 # $file$loc
182 ${msgctxt}msgid "$msgid"
183 msgstr ""
184 EOF
185 }
186
187 sub pot_extract_dbf {
188 my $dbf_file;
189 my $dbf;
190 my @fields;
191 my $records;
192 my %msgids;
193 my @ids;
194
195 $dbf_file = shift(@_);
196 $dbf = dbf->read_file($dbf_file);
197 if (!defined($dbf)) {
198 warn "Error: Can't read $dbf_file\n";
199 return;
200 }
201 $records = $dbf->get_records();
202 for (my $i = 0; $i < @_; $i++) {
203 my $field;
204 $field = $dbf->get_field($_[$i]->[0]);
205 if (!defined($field)) {
206 warn "Error: Can't read $dbf_file for $_[$i]->[0]\n";
207 return;
208 }
209 for (my $j = 0; $j < $records; $j++) {
210 my $value;
211 $value = $dbf->get_value($j, $field);
212 if (!defined($value)) {
213 next;
214 }
215 $value = dbf::rtrim($value);
216 if ($value eq '') {
217 next;
218 }
219 $msgids{$value} = get_msgid($dbf_file, $_[$i]->[1], $value);
220 }
221 }
222 @ids = sort(keys(%msgids));
223 for (my $i = 0; $i < @ids; $i++) {
224 print $msgids{$ids[$i]};
225 }
226 }
227
228 # Extract tut_text and tut_intr files.
229 # tut_text files have the first two lines as a file description.
230 # After, it follows the method used in FileTxt::read_paragraph()
231 sub tut_text_extract {
232 my $file;
233 my $is_tut_text;
234 my $fh;
235 my $msgid;
236 my $line_no;
237 my $loc;
238
239 ($file, $is_tut_text) = @_;
240
241 if (!open($fh, $file)) {
242 warn "Error: Can't read $file\n";
243 return;
244 }
245 $line_no = 0;
246 if ($is_tut_text) {
247 # skip first two lines
248 my $line = <$fh>;
249 $line = <$fh>;
250 $line_no += 2;
251 }
252 $msgid = "";
253 $loc = $line_no+1;
254 while (my $line = <$fh>) {
255 $line_no++;
256 if ($line =~ /^Button:/) {
257 next;
258 }
259 if ($line =~ /^~/) { # End of paragraph
260 print get_msgid($file, "", $msgid, $loc);
261 $msgid = "";
262 $loc = $line_no+1;
263 next;
264 }
265 if ($line =~ s/([^\x1A]*)\x1A//) { # End of file
266 $msgid .= $1;
267 last;
268 }
269 $msgid .= $line;
270 }
271 close($fh);
272
273 if ($msgid !~ /^\s*$/) {
274 print get_msgid($file, "", $msgid, $loc);
275 }
276 }
277
278 # First line is a title
279 # Next three lines skip
280 # Follows the method used in FileTxt::read_paragraph()
281 sub sct_text_extract {
282 my $file;
283 my $fh;
284 my $msgid;
285 my $line_no;
286 my $loc;
287
288 ($file) = @_;
289
290 if (!open($fh, $file)) {
291 warn "Error: Can't read $file\n";
292 return;
293 }
294
295 # Get title
296 $line_no = 0;
297 $msgid = read_line($fh);
298 $line_no++;
299 print get_msgid($file, "", $msgid, $line_no);
300
301 # Skip formatting three lines
302 for (my $i = 0; $i < 3; $i++) {
303 my $line = <$fh>;
304 $line_no++;
305 }
306
307 $msgid = "";
308 $loc = $line_no+1;
309 while (my $line = <$fh>) {
310 $line_no++;
311 if ($line =~ /^Button:/) {
312 next;
313 }
314 if ($line =~ /^~/) { # End of paragraph
315 print get_msgid($file, "", $msgid, $loc);
316 $msgid = "";
317 $loc = $line_no+1;
318 next;
319 }
320 if ($line =~ s/([^\x1A]*)\x1A//) { # End of file
321 $msgid .= $1;
322 last;
323 }
324 $msgid .= $line;
325 }
326 close($fh);
327
328 if ($msgid !~ /^\s*$/) {
329 print get_msgid($file, "", $msgid, $loc);
330 }
331 }
332
333 # First line is a help code or position
334 # Second line is a short help title
335 # Follows the method used in FileTxt::read_paragraph()
336 sub help_res_extract {
337 my $file;
338 my $fh;
339 my $msgid;
340 my $line_no;
341 my $loc;
342 my $at_eof;
343
344 ($file) = @_;
345
346 if (!open($fh, $file)) {
347 warn "Error: Can't read $file\n";
348 return;
349 }
350
351 # Get code (skip)
352 $line_no = 0;
353 $at_eof = 0;
354
355 while (!$at_eof) {
356 $msgid = <$fh>;
357 if (!defined($msgid)) {
358 last;
359 }
360 $line_no++;
361
362 # Get help title
363 $msgid = read_line($fh);
364 $line_no++;
365 print get_msgid($file, "", $msgid, $line_no);
366
367 $msgid = "";
368 $loc = $line_no+1;
369 while (my $line = <$fh>) {
370 $line_no++;
371 if ($line =~ /^Button:/) {
372 next;
373 }
374 if ($line =~ /^~/) { # End of paragraph
375 last;
376 }
377 if ($line =~ s/([^\x1A]*)\x1A//) { # End of file
378 $msgid .= $1;
379 $at_eof = 1;
380 last;
381 }
382 $msgid .= $line;
383 }
384 if ($msgid eq "") {
385 next;
386 }
387 print get_msgid($file, "", $msgid, $loc);
388 $loc = $line_no+1;
389 }
390 close($fh);
391 }