1 |
#!/opt/bin/perl |
2 |
|
3 |
# this script checks, fixes and simplifies @match expressions in a map |
4 |
|
5 |
use Crossfire::Map; |
6 |
|
7 |
sub fix($) { |
8 |
my ($msg) = @_; |
9 |
|
10 |
local $_ = $msg; |
11 |
|
12 |
# fx pretty common error of having "* First line of response" |
13 |
my $response = s/^\* (.*)/*/ ? "\n$1" : ""; |
14 |
|
15 |
warn "$path ($_) unexpected characters in match \n" if !/^[a-zA-Z\[\]\|\*\!\' 0-9\-\?]+$/; |
16 |
|
17 |
s/\[(.)(.)\]/(lc $1) eq (lc $2) ? lc $1 : "[$1$2]"/ge; |
18 |
|
19 |
my %alt; |
20 |
|
21 |
for my $kw (split /\|/) { |
22 |
$kw =~ s/^\s+//; |
23 |
$kw =~ s/\s+$//; |
24 |
|
25 |
$alt{lc $kw} = $kw; |
26 |
} |
27 |
|
28 |
$_ = join "|", sort keys %alt; |
29 |
|
30 |
$_ .= $response; |
31 |
|
32 |
warn "$path <$msg><$_>\n" if $_ ne $msg; |
33 |
warn "$path ($_) unexpected characters in match\n" if /[\[\]]/; |
34 |
|
35 |
$_ |
36 |
} |
37 |
|
38 |
for $path (@ARGV) { |
39 |
eval { |
40 |
open my $fh, "<:raw:perlio:utf8", $path |
41 |
or die "$path: $!\n"; |
42 |
|
43 |
<$fh> =~ /^arch map$/ |
44 |
or die "$path: not a crossfire map file\n"; |
45 |
|
46 |
my $map = new_from_file Crossfire::Map $path |
47 |
or die "$path: file load error\n"; |
48 |
|
49 |
my $dirty; |
50 |
|
51 |
for my $a (map @$_, grep $_, map @$_, grep $_, @{ $map->{map} }) { |
52 |
next unless $a->{msg} =~ /^\@match /; |
53 |
|
54 |
my $old = $a->{msg}; |
55 |
|
56 |
$a->{msg} =~ s/^(\@match\s+)(.*)$/$1 . fix $2/gme; |
57 |
|
58 |
$dirty ||= $old ne $a->{msg}; |
59 |
} |
60 |
|
61 |
$map->write_file ($path) |
62 |
if $dirty; |
63 |
|
64 |
1 |
65 |
} or $@ =~ /not a crossfire map/ or warn $@; |
66 |
} |
67 |
|