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 |
sub patch_perl { |
39 |
my ($arch) = @_; |
40 |
my $patched = 0; |
41 |
|
42 |
my @appendinv; |
43 |
|
44 |
for (@{$arch->{inventory} || []}) { |
45 |
if ($_->{slaying} eq '/python/IPO/send.py') { |
46 |
$_->{title} = 'perl'; |
47 |
$_->{slaying} = 'ipo'; |
48 |
$patched++ |
49 |
} elsif ($_->{slaying} eq '/python/IPO/receive.py') { |
50 |
$_->{title} = 'perl'; |
51 |
$_->{slaying} = 'ipo'; |
52 |
$patched++ |
53 |
} elsif ($_->{slaying} eq '/python/IPO/board.py') { |
54 |
push @appendinv, { _name => 'event_apply', title => 'perl', slaying => 'board' }; |
55 |
$arch->{msg} = '@match *'."\n".'@eval board::command $who, $msg, $npc'."\n"; |
56 |
$patched++ |
57 |
} elsif ($_->{slaying} eq '/python/IPO/say.py') { |
58 |
$arch->{msg} = '@match *'."\n".'@eval ipo::command $who, $msg, $npc'."\n"; |
59 |
$patched++ |
60 |
} elsif ($_->{slaying} eq '/python/IPO/banksay.py') { |
61 |
$arch->{msg} = '@match *'."\n".'@eval bank::command $who, $msg, $npc'."\n"; |
62 |
$patched++ |
63 |
} |
64 |
} |
65 |
|
66 |
if ($arch->{inventory}) { |
67 |
my @oinv = |
68 |
grep { |
69 |
not grep { $_->{slaying} ne $_ } |
70 |
('/python/IPO/board.py', '/python/IPO/say.py', '/python/IPO/banksay.py') |
71 |
} @{$arch->{inventory}}; |
72 |
$patched++ if @oinv < @{$arch->{inventory}}; |
73 |
@{$arch->{inventory}} = @oinv; |
74 |
} |
75 |
|
76 |
if (@appendinv) { |
77 |
$arch->{inventory} = [@{$arch->{inventory} || []}, @appendinv]; |
78 |
$patched++ |
79 |
} |
80 |
|
81 |
$patched |
82 |
} |
83 |
|
84 |
for $path (@ARGV) { |
85 |
eval { |
86 |
open my $fh, "<:raw:perlio:utf8", $path |
87 |
or die "$path: $!\n"; |
88 |
|
89 |
<$fh> =~ /^arch map$/ |
90 |
or die "$path: not a crossfire map file\n"; |
91 |
|
92 |
my $map = new_from_file Crossfire::Map $path |
93 |
or die "$path: file load error\n"; |
94 |
|
95 |
my $dirty; |
96 |
|
97 |
for my $a (map @$_, grep $_, map @$_, grep $_, @{ $map->{map} }) { |
98 |
if ($a->{inventory} and patch_perl ($a)) { |
99 |
$dirty = 1; |
100 |
next; |
101 |
} |
102 |
|
103 |
next unless $a->{msg} =~ /^\@match /; |
104 |
|
105 |
my $old = $a->{msg}; |
106 |
|
107 |
$a->{msg} =~ s/^(\@match\s+)(.*)$/$1 . fix $2/gme; |
108 |
|
109 |
$dirty ||= $old ne $a->{msg}; |
110 |
} |
111 |
|
112 |
$map->write_file ($path) |
113 |
if $dirty; |
114 |
|
115 |
1 |
116 |
} or $@ =~ /not a crossfire map/ or warn $@; |
117 |
} |
118 |
|