| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use warnings; |
| 4 |
|
| 5 |
my $lang; |
| 6 |
my @files; |
| 7 |
|
| 8 |
for (@ARGV) |
| 9 |
{ |
| 10 |
if (/^-v$/) |
| 11 |
{ |
| 12 |
$verbose = 1; |
| 13 |
} |
| 14 |
# Hack: no two-letter names are allowed, as these are assumed to be lang. |
| 15 |
elsif (/^([a-z][a-z])$/) |
| 16 |
{ |
| 17 |
die "One language at a time, please.\n" if $lang; |
| 18 |
$lang = $1; |
| 19 |
} |
| 20 |
else |
| 21 |
{ |
| 22 |
s/\.txt$//; |
| 23 |
die "Arg not a lang nor a filename: '$_'.\n" unless m{^([/a-z0-9_-]+)$}i; #/ |
| 24 |
push @files, $1; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
sub good_file() |
| 29 |
{ |
| 30 |
return grep { $file =~ m{\b$_\b} } @files; |
| 31 |
} |
| 32 |
|
| 33 |
sub err($) |
| 34 |
{ |
| 35 |
print $_[0]; |
| 36 |
} |
| 37 |
|
| 38 |
sub __________________________________________________________________________ |
| 39 |
{ |
| 40 |
%entries = (); # per-db |
| 41 |
%source = (); # file:line |
| 42 |
%keys = (); # per-file |
| 43 |
} |
| 44 |
|
| 45 |
sub read_file($) |
| 46 |
{ |
| 47 |
my $skip = !!@files; |
| 48 |
($file) = @_; |
| 49 |
$skip = 0 if good_file(); |
| 50 |
$file =~ s|/|/$lang/| if $lang; |
| 51 |
$skip = 0 if good_file(); |
| 52 |
return if $skip; |
| 53 |
$file = "dat/$file.txt"; |
| 54 |
|
| 55 |
%keys = (); |
| 56 |
$line = 0; |
| 57 |
return $lang ? 0 : die "Can't read $file\n" unless open F, "<", $file; |
| 58 |
($key, $value) = (); |
| 59 |
local $_; |
| 60 |
|
| 61 |
print "Checking $file\n" if $verbose; |
| 62 |
|
| 63 |
sub add_key() |
| 64 |
{ |
| 65 |
err "Duplicate entry: '$key' at $source{$key} and $file:$line\n" |
| 66 |
if $source{$key}; |
| 67 |
$source{$key} = "$file:$line"; |
| 68 |
$entries{$key} = $value; |
| 69 |
$keys{$key} = 1; |
| 70 |
|
| 71 |
undef $key; |
| 72 |
undef $value; |
| 73 |
} |
| 74 |
|
| 75 |
while (<F>) |
| 76 |
{ |
| 77 |
next if /^#/; |
| 78 |
s/\s+$//; |
| 79 |
|
| 80 |
if (/^%%%%/) |
| 81 |
{ |
| 82 |
if (defined $key) |
| 83 |
{ |
| 84 |
defined($value) |
| 85 |
? add_key() |
| 86 |
: err("Incomplete entry '$key' at $file:$line\n"); |
| 87 |
} |
| 88 |
next; |
| 89 |
} |
| 90 |
|
| 91 |
unless (defined $key) |
| 92 |
{ |
| 93 |
next if /^$/; |
| 94 |
$key = $_; |
| 95 |
$line = $.; # report the start not end of a desc |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
next if /^$/ && !defined $value; |
| 100 |
$value .= $_; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (defined $key) |
| 105 |
{ |
| 106 |
defined($value) |
| 107 |
? add_key() |
| 108 |
: err("Incomplete entry '$key' at the end of '$file'\n"); |
| 109 |
} |
| 110 |
|
| 111 |
close F; |
| 112 |
return 1; |
| 113 |
} |
| 114 |
|
| 115 |
sub check_desc_needed($@) |
| 116 |
{ |
| 117 |
my ($kind, @needed) = @_; |
| 118 |
for (@needed) |
| 119 |
{ |
| 120 |
chomp; |
| 121 |
err "$file: No description for $kind '$_'\n" unless $entries{$_}; |
| 122 |
delete $keys{$_}; |
| 123 |
} |
| 124 |
for (sort keys %keys) |
| 125 |
{ |
| 126 |
err "$file: Unused description for $kind '$_'\n" unless /^_/; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
sub check_desc_links() |
| 131 |
{ |
| 132 |
for (keys %entries) |
| 133 |
{ |
| 134 |
next unless $entries{$_} =~ /^<([^>\n]*)>$/; |
| 135 |
err "$source{$_}: Dangling link '$_' -> '$1'\n" unless $entries{$1}; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
sub check_db_locals() |
| 140 |
{ |
| 141 |
my (%locals, %keys); |
| 142 |
$keys{$_}=1 for keys %entries; |
| 143 |
$locals{$_}=1 for map /@(_[^@\n]+_)@/g, values %entries; |
| 144 |
for (sort keys %locals) |
| 145 |
{ |
| 146 |
err("Undefined local entry '$_'\n") unless $entries{$_}; |
| 147 |
delete $keys{$_}; |
| 148 |
} |
| 149 |
err("Unused local entry '$_' at $source{$_}\n") for grep /^_.*_$/, keys %keys; |
| 150 |
} |
| 151 |
|
| 152 |
sub check_db_externs(@) |
| 153 |
{ |
| 154 |
my (%externs, %used, %keys); |
| 155 |
$externs{$_}=1 for @_; |
| 156 |
$keys{$_}=1 for keys %entries; |
| 157 |
$used{$_}=1 for grep !/^_/, map /@([^@\n]+)@/g, values %entries; |
| 158 |
for (sort keys %used) |
| 159 |
{ |
| 160 |
err("Undefined entry '$_'\n") unless $entries{$_} || $externs{$_}; |
| 161 |
delete $keys{$_}; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
sub check_db_needed($@) |
| 166 |
{ |
| 167 |
my ($kind, @needed) = @_; |
| 168 |
for (@needed) |
| 169 |
{ |
| 170 |
chomp; |
| 171 |
err "$file: No $kind entry for '$_'\n" unless $entries{$_}; |
| 172 |
delete $keys{$_}; |
| 173 |
} |
| 174 |
err "$file: Unused entry for '$_'\n" for grep !/^_.*_$/, sort keys %keys; |
| 175 |
} |
| 176 |
|
| 177 |
sub at_at_replaces($$) |
| 178 |
{ |
| 179 |
my ($file, $func) = @_; |
| 180 |
open F, '<', $file or die; |
| 181 |
undef local $/; |
| 182 |
local $_ = <F>; |
| 183 |
close F; |
| 184 |
/^[^ ].* $func\((.*?)^}/sm or die "No $func() in $file.\n"; |
| 185 |
$_ = $1; |
| 186 |
return /@([A-Za-z0-9_,-]+)@/g; |
| 187 |
} |
| 188 |
|
| 189 |
my @monspeak_at_at = at_at_replaces("mon-util.cc", "do_mon_str_replacements"); |
| 190 |
|
| 191 |
my @gods = ("the Shining One", "Sif Muna","Nemelex Xobeh", "Wu Jian", qw( |
| 192 |
Ashenzari Beogh Cheibriados Dithmenos Elyvilon Fedhas Gozag |
| 193 |
Hepliaklqana Ignis Jiyva Kikubaaqudgha Lugonu Makhleb Okawaru |
| 194 |
Pakellas Qazlal Ru Trog Uskayaw Vehumet Xom Yredelemnul Zin)); |
| 195 |
|
| 196 |
sub godspeak_keys() |
| 197 |
{ |
| 198 |
undef local $/; |
| 199 |
open F, "<xom.cc" or die "Can't read xom.cc\n"; |
| 200 |
$_ = <F>; |
| 201 |
close F; |
| 202 |
my %keys; |
| 203 |
$keys{"Xom $_"}=1 for m{_get_xom_speech\("([^"]+)"\)}g; |
| 204 |
$keys{"Xom $_"}=1 for m{XOM_SPEECH\("([^"]+)"\)}g; |
| 205 |
$keys{"Xom bored"}=1; |
| 206 |
for (@gods) |
| 207 |
{ |
| 208 |
$keys{"$_ newgame"}=1; |
| 209 |
$keys{"$_ welcome"}=1; |
| 210 |
$keys{"$_ penance"}=1; |
| 211 |
$keys{"$_ prayer"}=1; |
| 212 |
} |
| 213 |
$keys{$_}=1 for ("Pakellas death", "recite_closure", "Ignis elemental wrath"); |
| 214 |
return keys %keys; |
| 215 |
} |
| 216 |
__________________________________________________________________________(); |
| 217 |
read_file("descript/unident"); |
| 218 |
read_file("descript/skills"); |
| 219 |
read_file("descript/commands"); |
| 220 |
|
| 221 |
if (read_file("descript/gods")) |
| 222 |
{ |
| 223 |
check_desc_needed("god", map {($_, "$_ powers", "$_ wrath")} @gods); |
| 224 |
} |
| 225 |
|
| 226 |
if (read_file("descript/items")) |
| 227 |
{ |
| 228 |
check_desc_needed("item", `util/gather_items -d`); |
| 229 |
} |
| 230 |
|
| 231 |
if (read_file("descript/branches")) |
| 232 |
{ |
| 233 |
check_desc_needed("branch", `util/gather_branches`); |
| 234 |
} |
| 235 |
|
| 236 |
if (read_file("descript/features")) |
| 237 |
{ |
| 238 |
$entries{"A gate leading to a distant place"} = 1; |
| 239 |
check_desc_needed("feature", grep {!/explore horizon$/} `util/gather_features -a`); |
| 240 |
} |
| 241 |
|
| 242 |
if (read_file("descript/clouds")) |
| 243 |
{ |
| 244 |
check_desc_needed("cloud", `util/gather_clouds`); |
| 245 |
} |
| 246 |
|
| 247 |
if (read_file("descript/unrand")) |
| 248 |
{ |
| 249 |
delete $keys{'Screaming Sword'}; |
| 250 |
delete $keys{'Sulking Sword'}; |
| 251 |
check_desc_needed("unrand", grep {!/^DUMMY/} map {s/^NAME:\s+// && $_} |
| 252 |
`grep ^NAME: art-data.txt`) |
| 253 |
} |
| 254 |
|
| 255 |
if (read_file("descript/cards")) |
| 256 |
{ |
| 257 |
delete $keys{'a buggy card'}; |
| 258 |
delete $keys{'a very buggy card'}; |
| 259 |
check_desc_needed("card", map {s/$/ card/ && $_} `util/gather_cards`); |
| 260 |
} |
| 261 |
|
| 262 |
if (read_file("descript/ability")) |
| 263 |
{ |
| 264 |
check_desc_needed("ability", sort map {chomp;"$_ ability"} |
| 265 |
`util/gather_abilities`); |
| 266 |
} |
| 267 |
|
| 268 |
if (read_file("descript/spells")) |
| 269 |
{ |
| 270 |
check_desc_needed("spell", sort map {chomp;"$_ spell"} `util/gather_spells`); |
| 271 |
} |
| 272 |
|
| 273 |
if (read_file("descript/monsters")) |
| 274 |
{ |
| 275 |
$entries{$_} = 1 for |
| 276 |
( |
| 277 |
# described with a suffix |
| 278 |
'Serpent of Hell', |
| 279 |
# overridden descriptions |
| 280 |
'player illusion', |
| 281 |
# replacement for removed creatures |
| 282 |
'ghost', |
| 283 |
# species-only monsters |
| 284 |
'elephant slug', 'hell lord', |
| 285 |
); |
| 286 |
delete $keys{'__(_suffix_examine'}; |
| 287 |
delete $keys{"Serpent of Hell $_"} for qw(gehenna cocytus dis tartarus); |
| 288 |
delete $keys{"__cap-${_}_suffix"} for 'A'..'Z'; |
| 289 |
check_desc_needed("monster", `util/gather_mons -d`); |
| 290 |
} |
| 291 |
|
| 292 |
check_desc_links(); |
| 293 |
$described{$_} = 1 for keys %entries; |
| 294 |
__________________________________________________________________________(); |
| 295 |
if ($lang) |
| 296 |
{ |
| 297 |
# Quotes may be already translated even if the desc they're appended to |
| 298 |
# is not yet. |
| 299 |
my $real_lang = $lang; |
| 300 |
undef $lang; |
| 301 |
read_file("descript/$_") for qw(features items unident unrand monsters spells |
| 302 |
gods branches skills ability cards commands clouds); |
| 303 |
$described{$_} = 1 for keys %entries; |
| 304 |
$lang = $real_lang; |
| 305 |
} |
| 306 |
__________________________________________________________________________(); |
| 307 |
# These go to GameStartDB rather than DescriptionDB. |
| 308 |
if (read_file("descript/species")) |
| 309 |
{ |
| 310 |
check_desc_needed("species", `util/gather_species -d`); |
| 311 |
} |
| 312 |
|
| 313 |
if (read_file("descript/backgrounds")) |
| 314 |
{ |
| 315 |
check_desc_needed("background", `util/gather_backgrounds`); |
| 316 |
} |
| 317 |
__________________________________________________________________________(); |
| 318 |
read_file("database/$_") for qw(randname rand_wpn rand_arm rand_all randbook monname); |
| 319 |
check_db_locals(); |
| 320 |
__________________________________________________________________________(); |
| 321 |
read_file("database/$_") for qw(monspeak monspell monflee wpnnoise insult); |
| 322 |
check_db_locals(); |
| 323 |
#check_db_externs(@monspeak_at_at); |
| 324 |
|
| 325 |
if (read_file("database/godspeak")) |
| 326 |
{ |
| 327 |
$entries{"$_ newgame"}=1 for @gods; # not mandatory |
| 328 |
$entries{"$_ penance"}=1 for @gods; # not mandatory |
| 329 |
$entries{"$_ prayer"}=1 for @gods; # not mandatory |
| 330 |
check_db_needed("godspeak", godspeak_keys()); |
| 331 |
} |
| 332 |
__________________________________________________________________________(); |
| 333 |
read_file("database/$_") for qw(shout insult); |
| 334 |
check_db_locals(); |
| 335 |
check_db_externs(@monspeak_at_at); |
| 336 |
__________________________________________________________________________(); |
| 337 |
read_file("descript/quotes"); |
| 338 |
check_desc_links(); |
| 339 |
if (%described) # can't check unless descs are loaded |
| 340 |
{ |
| 341 |
for (keys %entries) |
| 342 |
{ |
| 343 |
next if /^__(?:cap-[A-Z]|[a-z])_suffix$/; |
| 344 |
err "$file: quote for undescribed '$_'\n" unless $described{$_}; |
| 345 |
} |
| 346 |
} |
| 347 |
__________________________________________________________________________(); |
| 348 |
read_file("database/help"); |
| 349 |
__________________________________________________________________________(); |
| 350 |
read_file("database/FAQ"); |
| 351 |
__________________________________________________________________________(); |
| 352 |
read_file("descript/hints"); |
| 353 |
read_file("descript/tutorial"); |
| 354 |
check_desc_links(); |