1 | package cf; |
1 | package cf; |
2 | |
2 | |
3 | use Symbol; |
3 | use Symbol; |
4 | use List::Util; |
4 | use List::Util; |
5 | use Storable; |
5 | use Storable; |
|
|
6 | |
|
|
7 | use Event; |
|
|
8 | $Event::Eval = 1; # no idea why this is required, but it is |
6 | |
9 | |
7 | use strict; |
10 | use strict; |
8 | |
11 | |
9 | our %COMMAND; |
12 | our %COMMAND; |
10 | our @EVENT; |
13 | our @EVENT; |
… | |
… | |
61 | # guessed hierarchies |
64 | # guessed hierarchies |
62 | |
65 | |
63 | @cf::object::player::ISA = 'cf::object'; |
66 | @cf::object::player::ISA = 'cf::object'; |
64 | @cf::object::map::ISA = 'cf::object'; |
67 | @cf::object::map::ISA = 'cf::object'; |
65 | |
68 | |
|
|
69 | $Event::DIED = sub { |
|
|
70 | warn "error in event callback: @_"; |
|
|
71 | }; |
|
|
72 | |
66 | my %ext_pkg; |
73 | my %ext_pkg; |
67 | my @exts; |
74 | my @exts; |
68 | my @hook; |
75 | my @hook; |
69 | my %command; |
76 | my %command; |
|
|
77 | my %extcmd; |
70 | |
78 | |
71 | sub inject_event { |
79 | sub inject_event { |
72 | my ($data) = @_; |
80 | my $extension = shift; |
|
|
81 | my $event_code = shift; |
73 | |
82 | |
74 | my $cb = $hook[$data->{event_code}]{$data->{extension}} |
83 | my $cb = $hook[$event_code]{$extension} |
75 | or return; |
84 | or return; |
76 | |
85 | |
77 | $cb->($data) |
86 | &$cb |
78 | } |
87 | } |
79 | |
88 | |
80 | sub inject_global_event { |
89 | sub inject_global_event { |
81 | my $event = shift; |
90 | my $event = shift; |
82 | |
91 | |
… | |
… | |
98 | |
107 | |
99 | sub register_command { |
108 | sub register_command { |
100 | my ($name, $time, $cb) = @_; |
109 | my ($name, $time, $cb) = @_; |
101 | |
110 | |
102 | my $caller = caller; |
111 | my $caller = caller; |
103 | |
|
|
104 | warn "registering command '$name/$time' to '$caller'"; |
112 | #warn "registering command '$name/$time' to '$caller'"; |
105 | |
113 | |
106 | push @{ $command{$name} }, [$time, $cb, $caller]; |
114 | push @{ $command{$name} }, [$time, $cb, $caller]; |
107 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
115 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
108 | } |
116 | } |
109 | |
117 | |
|
|
118 | sub register_extcmd { |
|
|
119 | my ($name, $cb) = @_; |
|
|
120 | |
|
|
121 | my $caller = caller; |
|
|
122 | #warn "registering extcmd '$name' to '$caller'"; |
|
|
123 | |
|
|
124 | $extcmd{$name} = [$cb, $caller]; |
|
|
125 | } |
|
|
126 | |
110 | sub register { |
127 | sub register { |
111 | my ($base, $pkg) = @_; |
128 | my ($base, $pkg) = @_; |
112 | |
129 | |
113 | for my $idx (0 .. $#EVENT) { |
130 | for my $idx (0 .. $#EVENT) { |
114 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
131 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
115 | warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
132 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
116 | $hook[$idx]{$base} = $ref; |
133 | $hook[$idx]{$base} = $ref; |
117 | } |
134 | } |
118 | } |
135 | } |
119 | } |
136 | } |
120 | |
137 | |
… | |
… | |
171 | delete $command{$name}; |
188 | delete $command{$name}; |
172 | delete $COMMAND{"$name\000"}; |
189 | delete $COMMAND{"$name\000"}; |
173 | } |
190 | } |
174 | } |
191 | } |
175 | |
192 | |
|
|
193 | # remove extcmds |
|
|
194 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
|
|
195 | delete $extcmd{$name}; |
|
|
196 | } |
|
|
197 | |
176 | Symbol::delete_package $pkg; |
198 | Symbol::delete_package $pkg; |
177 | } |
199 | } |
178 | |
200 | |
179 | sub load_extensions { |
201 | sub load_extensions { |
180 | my $LIBDIR = maps_directory "perl"; |
202 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
213 | $who->message ("Intruder Alert!"); |
235 | $who->message ("Intruder Alert!"); |
214 | } |
236 | } |
215 | }; |
237 | }; |
216 | |
238 | |
217 | ############################################################################# |
239 | ############################################################################# |
|
|
240 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
|
|
241 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
242 | |
|
|
243 | sub on_extcmd { |
|
|
244 | my ($pl, $buf) = @_; |
|
|
245 | |
|
|
246 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
|
|
247 | |
|
|
248 | $extcmd{$type}[0]->($pl, $buf) |
|
|
249 | if $extcmd{$type}; |
|
|
250 | } |
|
|
251 | |
|
|
252 | ############################################################################# |
218 | # load/save/clean perl data associated with a map |
253 | # load/save/clean perl data associated with a map |
219 | |
254 | |
220 | *on_mapclean = sub { |
255 | *on_mapclean = sub { |
221 | my $map = shift->{map}; |
256 | my ($map) = @_; |
222 | |
257 | |
223 | my $path = $map->tmpname; |
258 | my $path = $map->tmpname; |
224 | defined $path or return; |
259 | defined $path or return; |
225 | |
260 | |
226 | unlink "$path.cfperl"; |
261 | unlink "$path.cfperl"; |
227 | }; |
262 | }; |
228 | |
263 | |
229 | *on_mapin = |
264 | *on_mapin = |
230 | *on_mapload = sub { |
265 | *on_mapload = sub { |
231 | my $map = shift->{map}; |
266 | my ($map) = @_; |
232 | |
267 | |
233 | my $path = $map->tmpname; |
268 | my $path = $map->tmpname; |
234 | $path = $map->path unless defined $path; |
269 | $path = $map->path unless defined $path; |
235 | |
270 | |
236 | open my $fh, "<:raw", "$path.cfperl" |
271 | open my $fh, "<:raw", "$path.cfperl" |
… | |
… | |
243 | |
278 | |
244 | $map->_set_obs ($data->{obs}); |
279 | $map->_set_obs ($data->{obs}); |
245 | }; |
280 | }; |
246 | |
281 | |
247 | *on_mapout = sub { |
282 | *on_mapout = sub { |
248 | my $map = shift->{map}; |
283 | my ($map) = @_; |
249 | |
284 | |
250 | my $path = $map->tmpname; |
285 | my $path = $map->tmpname; |
251 | $path = $map->path unless defined $path; |
286 | $path = $map->path unless defined $path; |
252 | |
287 | |
253 | my $obs = $map->_get_obs; |
288 | my $obs = $map->_get_obs; |
… | |
… | |
273 | |
308 | |
274 | ############################################################################# |
309 | ############################################################################# |
275 | # load/save perl data associated with player->ob objects |
310 | # load/save perl data associated with player->ob objects |
276 | |
311 | |
277 | *on_player_load = sub { |
312 | *on_player_load = sub { |
278 | my ($event) = @_; |
313 | my ($ob, $path) = @_; |
279 | my $path = $event->{message}; |
|
|
280 | my $ob = $event->{who}; |
|
|
281 | |
314 | |
282 | if (open my $fh, "<:raw", "$path.cfperl") { |
315 | if (open my $fh, "<:raw", "$path.cfperl") { |
283 | |
316 | |
284 | #d##TODO#remove |
317 | #d##TODO#remove |
285 | |
318 | |
… | |
… | |
300 | } |
333 | } |
301 | } |
334 | } |
302 | }; |
335 | }; |
303 | |
336 | |
304 | *on_player_save = sub { |
337 | *on_player_save = sub { |
305 | my ($event) = @_; |
338 | my ($ob, $path) = @_; |
306 | my $path = $event->{message}; |
|
|
307 | my $ob = $event->{who}; |
|
|
308 | |
339 | |
309 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
340 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
310 | for grep %$_, $ob, $ob->inv; |
341 | for grep %$_, $ob, $ob->inv; |
311 | |
342 | |
312 | unlink "$path.cfperl";#d##TODO#remove |
343 | unlink "$path.cfperl";#d##TODO#remove |
313 | }; |
344 | }; |
314 | |
345 | |
315 | register "<global>", __PACKAGE__; |
346 | register "<global>", __PACKAGE__; |
316 | |
347 | |
|
|
348 | unshift @INC, maps_directory "perl"; |
|
|
349 | |
317 | load_extensions; |
350 | load_extensions; |
318 | |
351 | |
319 | 1 |
352 | 1 |
320 | |
353 | |