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_msg($) { |
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 |
|
41 |
my $patched; |
42 |
|
43 |
my $inv = $arch->{inventory} || []; |
44 |
|
45 |
for (@$inv) { |
46 |
if ($_->{type} == 116 || $_->{_name} =~ /^event_/) { |
47 |
# crossfire to old crossfire+ |
48 |
if ($_->{slaying} eq '/python/IPO/send.py') { |
49 |
$_->{title} = 'perl'; |
50 |
$_->{slaying} = 'ipo'; |
51 |
$patched++ |
52 |
} elsif ($_->{slaying} eq '/python/IPO/receive.py') { |
53 |
$_->{title} = 'perl'; |
54 |
$_->{slaying} = 'ipo'; |
55 |
$patched++; |
56 |
} elsif ($_->{slaying} eq '/python/IPO/board.py') { |
57 |
$_ = { _name => 'event_apply', title => 'perl', slaying => 'board' }; |
58 |
$arch->{msg} = '@match *'."\n".'@eval board::command $who, $msg, $npc'."\n"; |
59 |
$patched++; |
60 |
} elsif ($_->{slaying} eq '/python/IPO/say.py') { |
61 |
$arch->{msg} = '@match *'."\n".'@eval ipo::command $who, $msg, $npc'."\n"; |
62 |
$_ = undef; |
63 |
$patched++; |
64 |
} elsif ($_->{slaying} eq '/python/IPO/banksay.py') { |
65 |
$arch->{msg} = '@match *'."\n".'@eval bank::command $who, $msg, $npc'."\n"; |
66 |
$_ = undef; |
67 |
$patched++; |
68 |
} |
69 |
|
70 |
# old crossfire+ to new plug-in system |
71 |
if ($_ && $_->{title} eq "perl") { |
72 |
if ($_->{slaying} eq "board") { |
73 |
push @{$arch->{attach}}, ["board"]; |
74 |
$_ = undef; |
75 |
$patched++; |
76 |
} elsif ($_->{slaying} eq "ipo") { |
77 |
if ($_->{_name} eq "event_close") { |
78 |
push @{$arch->{attach}}, ["ipo_mailbox"]; |
79 |
} elsif ($_->{_name} eq "event_apply") { |
80 |
# |
81 |
} |
82 |
$_ = undef; |
83 |
$patched++; |
84 |
} elsif ($_->{slaying} eq "nimbus") { |
85 |
push @{$arch->{attach}}, ["nimbus_exit", { restore => $_->{name} eq "restore"}]; |
86 |
$_ = undef; |
87 |
$patched++; |
88 |
} elsif ($_->{slaying} eq "minesweeper") { |
89 |
push @{$arch->{attach}}, ["minesweeper", { split /(?:\s+|=)/, $_->{name} }]; |
90 |
$_ = undef; |
91 |
$patched++; |
92 |
} elsif ($_->{slaying} eq "reseller") { |
93 |
if ($_->{_name} eq "event_drop_on") { |
94 |
push @{$arch->{attach}}, ["reseller_floor"]; |
95 |
} elsif ($_->{_name} eq "event_trigger") { |
96 |
my ($a, $b, $c) = split /,/, $_->{name}; |
97 |
push @{$arch->{attach}}, ["reseller_shopmat", {npc_name => $a, npc_x => $b, npc_y => $c}]; |
98 |
} |
99 |
$_ = undef; |
100 |
$patched++; |
101 |
} else { |
102 |
warn "WARNING: unsupported perl event<$_->{slaying}>\n";#d# |
103 |
} |
104 |
} |
105 |
} |
106 |
} |
107 |
|
108 |
$arch->{inventory} = [grep $_, @$inv]; |
109 |
|
110 |
$patched |
111 |
} |
112 |
|
113 |
for $path (@ARGV) { |
114 |
eval { |
115 |
open my $fh, "<:raw:perlio:utf8", $path |
116 |
or die "$path: $!\n"; |
117 |
|
118 |
<$fh> =~ /^arch \S+$/ |
119 |
or die "$path: not a crossfire map file\n"; |
120 |
|
121 |
my $map = new_from_file Crossfire::Map $path |
122 |
or die "$path: file load error\n"; |
123 |
|
124 |
my $dirty; |
125 |
|
126 |
for my $a (map @$_, grep $_, map @$_, grep $_, @{ $map->{map} }) { |
127 |
if ($a->{inventory} and patch_perl ($a)) { |
128 |
$dirty = 1; |
129 |
next; |
130 |
} |
131 |
|
132 |
next unless $a->{msg} =~ /^\@match /; |
133 |
|
134 |
my $old = $a->{msg}; |
135 |
|
136 |
$a->{msg} =~ s/^(\@match\s+)(.*)$/$1 . fix_msg $2/gme; |
137 |
|
138 |
$dirty ||= $old ne $a->{msg}; |
139 |
} |
140 |
|
141 |
$map->write_file ($path) |
142 |
if $dirty; |
143 |
|
144 |
1 |
145 |
} or $@ =~ /not a crossfire map/ or warn $@; |
146 |
} |
147 |
|