1 |
elmex |
1.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 |
elmex |
1.3 |
sub patch_perl { |
39 |
elmex |
1.1 |
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 |
elmex |
1.3 |
} elsif ($_->{slaying} eq '/python/IPO/banksay.py') { |
61 |
|
|
$arch->{msg} = '@match *'."\n".'@eval bank::command $who, $msg, $npc'."\n"; |
62 |
|
|
$patched++ |
63 |
elmex |
1.1 |
} |
64 |
|
|
} |
65 |
elmex |
1.2 |
|
66 |
elmex |
1.1 |
if ($arch->{inventory}) { |
67 |
elmex |
1.2 |
my @oinv = |
68 |
elmex |
1.1 |
grep { |
69 |
elmex |
1.5 |
my $o = $_; |
70 |
|
|
not grep { $o->{slaying} eq $_ } |
71 |
elmex |
1.4 |
('/python/IPO/board.py', '/python/IPO/say.py', '/python/IPO/banksay.py') |
72 |
elmex |
1.1 |
} @{$arch->{inventory}}; |
73 |
elmex |
1.2 |
$patched++ if @oinv < @{$arch->{inventory}}; |
74 |
|
|
@{$arch->{inventory}} = @oinv; |
75 |
elmex |
1.1 |
} |
76 |
|
|
|
77 |
|
|
if (@appendinv) { |
78 |
|
|
$arch->{inventory} = [@{$arch->{inventory} || []}, @appendinv]; |
79 |
elmex |
1.2 |
$patched++ |
80 |
elmex |
1.1 |
} |
81 |
|
|
|
82 |
|
|
$patched |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
for $path (@ARGV) { |
86 |
|
|
eval { |
87 |
|
|
open my $fh, "<:raw:perlio:utf8", $path |
88 |
|
|
or die "$path: $!\n"; |
89 |
|
|
|
90 |
|
|
<$fh> =~ /^arch map$/ |
91 |
|
|
or die "$path: not a crossfire map file\n"; |
92 |
|
|
|
93 |
|
|
my $map = new_from_file Crossfire::Map $path |
94 |
|
|
or die "$path: file load error\n"; |
95 |
|
|
|
96 |
|
|
my $dirty; |
97 |
|
|
|
98 |
|
|
for my $a (map @$_, grep $_, map @$_, grep $_, @{ $map->{map} }) { |
99 |
elmex |
1.3 |
if ($a->{inventory} and patch_perl ($a)) { |
100 |
elmex |
1.1 |
$dirty = 1; |
101 |
|
|
next; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
next unless $a->{msg} =~ /^\@match /; |
105 |
|
|
|
106 |
|
|
my $old = $a->{msg}; |
107 |
|
|
|
108 |
|
|
$a->{msg} =~ s/^(\@match\s+)(.*)$/$1 . fix $2/gme; |
109 |
|
|
|
110 |
|
|
$dirty ||= $old ne $a->{msg}; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
$map->write_file ($path) |
114 |
|
|
if $dirty; |
115 |
|
|
|
116 |
|
|
1 |
117 |
|
|
} or $@ =~ /not a crossfire map/ or warn $@; |
118 |
|
|
} |
119 |
|
|
|