… | |
… | |
11 | use Event; |
11 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
|
|
16 | _reload_1; |
|
|
17 | |
16 | our %COMMAND = (); |
18 | our %COMMAND = (); |
17 | our @EVENT; |
19 | our @EVENT; |
18 | our @PLUGIN_EVENT; |
|
|
19 | our %PROP_TYPE; |
20 | our %PROP_TYPE; |
20 | our %PROP_IDX; |
21 | our %PROP_IDX; |
21 | our $LIBDIR = maps_directory "perl"; |
22 | our $LIBDIR = maps_directory "perl"; |
22 | |
23 | |
23 | our $TICK = MAX_TIME * 1e-6; |
24 | our $TICK = MAX_TIME * 1e-6; |
24 | our $TICK_WATCHER; |
25 | our $TICK_WATCHER; |
25 | our $NEXT_TICK; |
26 | our $NEXT_TICK; |
26 | |
27 | |
27 | BEGIN { |
28 | BEGIN { |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
|
|
29 | |
|
|
30 | *CORE::GLOBAL::warn = sub { |
29 | *CORE::GLOBAL::warn = sub { |
31 | my $msg = join "", @_; |
30 | my $msg = join "", @_; |
32 | $msg .= "\n" |
31 | $msg .= "\n" |
33 | unless $msg =~ /\n$/; |
32 | unless $msg =~ /\n$/; |
34 | |
33 | |
… | |
… | |
69 | prop_gen PLAYER_PROP => "cf::object::player"; |
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
70 | |
69 | |
71 | prop_gen MAP_PROP => "cf::map"; |
70 | prop_gen MAP_PROP => "cf::map"; |
72 | prop_gen ARCH_PROP => "cf::arch"; |
71 | prop_gen ARCH_PROP => "cf::arch"; |
73 | |
72 | |
74 | # guessed hierarchies |
|
|
75 | |
|
|
76 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
77 | @safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
74 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
76 | # within the Safe compartment. |
81 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
82 | no strict 'refs'; |
78 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
80 | } |
85 | |
81 | |
86 | $Event::DIED = sub { |
82 | $Event::DIED = sub { |
… | |
… | |
92 | my @hook; |
88 | my @hook; |
93 | my %command; |
89 | my %command; |
94 | my %extcmd; |
90 | my %extcmd; |
95 | |
91 | |
96 | ############################################################################# |
92 | ############################################################################# |
|
|
93 | # utility functions |
|
|
94 | |
|
|
95 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
96 | |
|
|
97 | sub from_json($) { |
|
|
98 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
99 | JSON::Syck::Load $_[0] |
|
|
100 | } |
|
|
101 | |
|
|
102 | sub to_json($) { |
|
|
103 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
104 | JSON::Syck::Dump $_[0] |
|
|
105 | } |
|
|
106 | |
|
|
107 | ############################################################################# |
97 | # "new" plug-in system |
108 | # "new" plug-in system |
98 | |
109 | |
99 | =item cf::object::attach ... # NYI |
110 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
111 | |
|
|
112 | =over 4 |
|
|
113 | |
|
|
114 | =item $object->attach ($attachment, key => $value...) |
|
|
115 | |
|
|
116 | =item $object->detach ($attachment) |
|
|
117 | |
|
|
118 | Attach/detach a pre-registered attachment to an object. |
|
|
119 | |
|
|
120 | =item $player->attach ($attachment, key => $value...) |
|
|
121 | |
|
|
122 | =item $player->detach ($attachment) |
|
|
123 | |
|
|
124 | Attach/detach a pre-registered attachment to a player. |
|
|
125 | |
|
|
126 | =item $map->attach ($attachment, key => $value...) |
|
|
127 | |
|
|
128 | =item $map->detach ($attachment) |
|
|
129 | |
|
|
130 | Attach/detach a pre-registered attachment to a map. |
|
|
131 | |
|
|
132 | =item $bool = $object->attached ($name) |
|
|
133 | |
|
|
134 | =item $bool = $player->attached ($name) |
|
|
135 | |
|
|
136 | =item $bool = $map->attached ($name) |
|
|
137 | |
|
|
138 | Checks wether the named attachment is currently attached to the object. |
100 | |
139 | |
101 | =item cf::attach_global ... |
140 | =item cf::attach_global ... |
102 | |
141 | |
103 | =item cf::attach_to_type ... |
142 | Attach handlers for global events. |
|
|
143 | |
|
|
144 | This and all following C<attach_*>-functions expect any number of the |
|
|
145 | following handler/hook descriptions: |
|
|
146 | |
|
|
147 | =over 4 |
|
|
148 | |
|
|
149 | =item prio => $number |
|
|
150 | |
|
|
151 | Set the priority for all following handlers/hooks (unless overwritten |
|
|
152 | by another C<prio> setting). Lower priority handlers get executed |
|
|
153 | earlier. The default priority is C<0>, and many built-in handlers are |
|
|
154 | registered at priority C<-1000>, so lower priorities should not be used |
|
|
155 | unless you know what you are doing. |
|
|
156 | |
|
|
157 | =item on_I<event> => \&cb |
|
|
158 | |
|
|
159 | Call the given code reference whenever the named event happens (event is |
|
|
160 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
|
|
161 | handlers are recognised generally depends on the type of object these |
|
|
162 | handlers attach to). |
|
|
163 | |
|
|
164 | See F<include/eventinc.h> for the full list of events supported, and their |
|
|
165 | class. |
|
|
166 | |
|
|
167 | =item package => package:: |
|
|
168 | |
|
|
169 | Look for sub functions of the name C<< on_I<event> >> in the given |
|
|
170 | package and register them. Only handlers for eevents supported by the |
|
|
171 | object/class are recognised. |
|
|
172 | |
|
|
173 | =back |
|
|
174 | |
|
|
175 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
176 | |
|
|
177 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
178 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
179 | type. |
104 | |
180 | |
105 | =item cf::attach_to_objects ... |
181 | =item cf::attach_to_objects ... |
106 | |
182 | |
|
|
183 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
184 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
185 | the game. |
|
|
186 | |
107 | =item cf::attach_to_players ... |
187 | =item cf::attach_to_players ... |
108 | |
188 | |
|
|
189 | Attach handlers to all players. |
|
|
190 | |
109 | =item cf::attach_to_maps ... |
191 | =item cf::attach_to_maps ... |
110 | |
192 | |
111 | prio => $number, # lower is earlier |
193 | Attach handlers to all maps. |
112 | on_xxx => \&cb, |
194 | |
113 | package => package::, |
195 | =item cf:register_attachment $name, ... |
|
|
196 | |
|
|
197 | Register an attachment by name through which objects can refer to this |
|
|
198 | attachment. |
|
|
199 | |
|
|
200 | =item cf:register_player_attachment $name, ... |
|
|
201 | |
|
|
202 | Register an attachment by name through which players can refer to this |
|
|
203 | attachment. |
|
|
204 | |
|
|
205 | =item cf:register_map_attachment $name, ... |
|
|
206 | |
|
|
207 | Register an attachment by name through which maps can refer to this |
|
|
208 | attachment. |
114 | |
209 | |
115 | =cut |
210 | =cut |
116 | |
211 | |
117 | # the following variables are defined in .xs and must not be re-created |
212 | # the following variables are defined in .xs and must not be re-created |
118 | our @CB_GLOBAL = (); # registry for all global events |
213 | our @CB_GLOBAL = (); # registry for all global events |
119 | our @CB_OBJECT = (); |
214 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
120 | our @CB_PLAYER = (); |
215 | our @CB_PLAYER = (); |
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
216 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
122 | our @CB_MAP = (); |
217 | our @CB_MAP = (); |
123 | |
218 | |
|
|
219 | my %attachment; |
|
|
220 | |
124 | sub _attach_cb($\%$$$) { |
221 | sub _attach_cb($\%$$$) { |
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
222 | my ($registry, $undo, $event, $prio, $cb) = @_; |
126 | |
223 | |
127 | use sort 'stable'; |
224 | use sort 'stable'; |
128 | |
225 | |
… | |
… | |
136 | } |
233 | } |
137 | |
234 | |
138 | # attach handles attaching event callbacks |
235 | # attach handles attaching event callbacks |
139 | # the only thing the caller has to do is pass the correct |
236 | # the only thing the caller has to do is pass the correct |
140 | # registry (== where the callback attaches to). |
237 | # registry (== where the callback attaches to). |
141 | sub _attach(\@$\@) { |
238 | sub _attach(\@$@) { |
142 | my ($registry, $klass, $arg) = @_; |
239 | my ($registry, $klass, @arg) = @_; |
143 | |
240 | |
144 | my $prio = 0; |
241 | my $prio = 0; |
145 | |
242 | |
146 | my %undo = ( |
243 | my %undo = ( |
147 | registry => $registry, |
244 | registry => $registry, |
148 | cb => [], |
245 | cb => [], |
149 | ); |
246 | ); |
150 | |
247 | |
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
248 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
152 | |
249 | |
153 | while (@$arg) { |
250 | while (@arg) { |
154 | my $type = shift @$arg; |
251 | my $type = shift @arg; |
155 | |
252 | |
156 | if ($type eq "prio") { |
253 | if ($type eq "prio") { |
157 | $prio = shift @$arg; |
254 | $prio = shift @arg; |
158 | |
255 | |
159 | } elsif ($type eq "package") { |
256 | } elsif ($type eq "package") { |
160 | my $pkg = shift @$arg; |
257 | my $pkg = shift @arg; |
161 | |
258 | |
162 | while (my ($name, $id) = each %cb_id) { |
259 | while (my ($name, $id) = each %cb_id) { |
163 | if (my $cb = $pkg->can ($name)) { |
260 | if (my $cb = $pkg->can ($name)) { |
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
261 | _attach_cb $registry, %undo, $id, $prio, $cb; |
165 | } |
262 | } |
166 | } |
263 | } |
167 | |
264 | |
168 | } elsif (exists $cb_id{$type}) { |
265 | } elsif (exists $cb_id{$type}) { |
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
266 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
170 | |
267 | |
171 | } elsif (ref $type) { |
268 | } elsif (ref $type) { |
172 | warn "attaching objects not supported, ignoring.\n"; |
269 | warn "attaching objects not supported, ignoring.\n"; |
173 | |
270 | |
174 | } else { |
271 | } else { |
175 | shift @$arg; |
272 | shift @arg; |
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
273 | warn "attach argument '$type' not supported, ignoring.\n"; |
177 | } |
274 | } |
178 | } |
275 | } |
179 | |
276 | |
180 | \%undo |
277 | \%undo |
181 | } |
278 | } |
182 | |
279 | |
|
|
280 | sub _attach_attachment { |
|
|
281 | my ($obj, $name, %arg) = @_; |
|
|
282 | |
|
|
283 | return if exists $obj->{_attachment}{$name}; |
|
|
284 | |
|
|
285 | my $res; |
|
|
286 | |
|
|
287 | if (my $attach = $attachment{$name}) { |
|
|
288 | my $registry = $obj->registry; |
|
|
289 | |
|
|
290 | for (@$attach) { |
|
|
291 | my ($klass, @attach) = @$_; |
|
|
292 | $res = _attach @$registry, $klass, @attach; |
|
|
293 | } |
|
|
294 | |
|
|
295 | $obj->{$name} = \%arg; |
|
|
296 | } else { |
|
|
297 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
298 | } |
|
|
299 | |
|
|
300 | $obj->{_attachment}{$name} = undef; |
|
|
301 | |
|
|
302 | $res->{attachment} = $name; |
|
|
303 | $res |
|
|
304 | } |
|
|
305 | |
183 | sub cf::object::attach { |
306 | *cf::object::attach = |
184 | die; |
307 | *cf::player::attach = |
185 | } |
308 | *cf::map::attach = sub { |
|
|
309 | my ($obj, $name, %arg) = @_; |
|
|
310 | |
|
|
311 | _attach_attachment $obj, $name, %arg; |
|
|
312 | }; |
|
|
313 | |
|
|
314 | # all those should be optimised |
|
|
315 | *cf::object::detach = |
|
|
316 | *cf::player::detach = |
|
|
317 | *cf::map::detach = sub { |
|
|
318 | my ($obj, $name) = @_; |
|
|
319 | |
|
|
320 | delete $obj->{_attachment}{$name}; |
|
|
321 | reattach ($obj); |
|
|
322 | }; |
|
|
323 | |
|
|
324 | *cf::object::attached = |
|
|
325 | *cf::player::attached = |
|
|
326 | *cf::map::attached = sub { |
|
|
327 | my ($obj, $name) = @_; |
|
|
328 | |
|
|
329 | exists $obj->{_attachment}{$name} |
|
|
330 | }; |
186 | |
331 | |
187 | sub attach_global { |
332 | sub attach_global { |
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
333 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
189 | } |
334 | } |
190 | |
335 | |
191 | sub attach_to_type { |
336 | sub attach_to_type { |
192 | my $type = shift; |
337 | my $type = shift; |
|
|
338 | my $subtype = shift; |
|
|
339 | |
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
340 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
194 | } |
341 | } |
195 | |
342 | |
196 | sub attach_to_objects { |
343 | sub attach_to_objects { |
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
344 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
198 | } |
345 | } |
… | |
… | |
203 | |
350 | |
204 | sub attach_to_maps { |
351 | sub attach_to_maps { |
205 | _attach @CB_MAP, KLASS_MAP, @_ |
352 | _attach @CB_MAP, KLASS_MAP, @_ |
206 | } |
353 | } |
207 | |
354 | |
|
|
355 | sub register_attachment { |
|
|
356 | my $name = shift; |
|
|
357 | |
|
|
358 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
359 | } |
|
|
360 | |
|
|
361 | sub register_player_attachment { |
|
|
362 | my $name = shift; |
|
|
363 | |
|
|
364 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
365 | } |
|
|
366 | |
|
|
367 | sub register_map_attachment { |
|
|
368 | my $name = shift; |
|
|
369 | |
|
|
370 | $attachment{$name} = [[KLASS_MAP, @_]]; |
|
|
371 | } |
|
|
372 | |
208 | our $override; |
373 | our $override; |
|
|
374 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
209 | |
375 | |
210 | sub override() { |
376 | sub override { |
211 | $override = 1 |
377 | $override = 1; |
|
|
378 | @invoke_results = (); |
212 | } |
379 | } |
213 | |
380 | |
214 | sub invoke { |
381 | sub do_invoke { |
215 | my $event = shift; |
382 | my $event = shift; |
216 | my $callbacks = shift; |
383 | my $callbacks = shift; |
|
|
384 | |
|
|
385 | @invoke_results = (); |
217 | |
386 | |
218 | local $override; |
387 | local $override; |
219 | |
388 | |
220 | for (@$callbacks) { |
389 | for (@$callbacks) { |
221 | eval { &{$_->[1]} }; |
390 | eval { &{$_->[1]} }; |
… | |
… | |
230 | } |
399 | } |
231 | |
400 | |
232 | 0 |
401 | 0 |
233 | } |
402 | } |
234 | |
403 | |
|
|
404 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
|
|
405 | |
|
|
406 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
407 | |
|
|
408 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
409 | |
|
|
410 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
411 | |
|
|
412 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
413 | |
|
|
414 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
415 | removed in future versions), and there is no public API to access override |
|
|
416 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
417 | |
|
|
418 | =back |
|
|
419 | |
|
|
420 | =head2 methods valid for all pointers |
|
|
421 | |
|
|
422 | =over 4 |
|
|
423 | |
|
|
424 | =item $object->valid |
|
|
425 | |
|
|
426 | =item $player->valid |
|
|
427 | |
|
|
428 | =item $map->valid |
|
|
429 | |
|
|
430 | Just because you have a perl object does not mean that the corresponding |
|
|
431 | C-level object still exists. If you try to access an object that has no |
|
|
432 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
433 | can be used to test for existence of the C object part without causing an |
|
|
434 | exception. |
|
|
435 | |
|
|
436 | =back |
|
|
437 | |
|
|
438 | =cut |
|
|
439 | |
|
|
440 | *cf::object::valid = |
|
|
441 | *cf::player::valid = |
|
|
442 | *cf::map::valid = \&cf::_valid; |
|
|
443 | |
|
|
444 | ############################################################################# |
|
|
445 | # object support |
|
|
446 | |
|
|
447 | sub instantiate { |
|
|
448 | my ($obj, $data) = @_; |
|
|
449 | |
|
|
450 | $data = from_json $data; |
|
|
451 | |
|
|
452 | for (@$data) { |
|
|
453 | my ($name, $args) = @$_; |
|
|
454 | |
|
|
455 | $obj->attach ($name, %{$args || {} }); |
|
|
456 | } |
|
|
457 | } |
|
|
458 | |
|
|
459 | # basically do the same as instantiate, without calling instantiate |
|
|
460 | sub reattach { |
|
|
461 | my ($obj) = @_; |
|
|
462 | my $registry = $obj->registry; |
|
|
463 | |
|
|
464 | @$registry = (); |
|
|
465 | |
|
|
466 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
467 | |
|
|
468 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
|
|
469 | if (my $attach = $attachment{$name}) { |
|
|
470 | for (@$attach) { |
|
|
471 | my ($klass, @attach) = @$_; |
|
|
472 | _attach @$registry, $klass, @attach; |
|
|
473 | } |
|
|
474 | } else { |
|
|
475 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | } |
|
|
479 | |
|
|
480 | sub object_freezer_save { |
|
|
481 | my ($filename, $rdata, $objs) = @_; |
|
|
482 | |
|
|
483 | if (open my $fh, ">:raw", "$filename~") { |
|
|
484 | chmod SAVE_MODE, $fh; |
|
|
485 | syswrite $fh, $$rdata; |
|
|
486 | close $fh; |
|
|
487 | |
|
|
488 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
489 | chmod SAVE_MODE, $fh; |
|
|
490 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
491 | close $fh; |
|
|
492 | rename "$filename.pst~", "$filename.pst"; |
|
|
493 | } else { |
|
|
494 | unlink "$filename.pst"; |
|
|
495 | } |
|
|
496 | |
|
|
497 | rename "$filename~", $filename; |
|
|
498 | } else { |
|
|
499 | warn "FATAL: $filename~: $!\n"; |
|
|
500 | } |
|
|
501 | } |
|
|
502 | |
|
|
503 | sub object_thawer_load { |
|
|
504 | my ($filename) = @_; |
|
|
505 | |
|
|
506 | open my $fh, "<:raw:perlio", "$filename.pst" |
|
|
507 | or return; |
|
|
508 | |
|
|
509 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
|
|
510 | } |
|
|
511 | |
|
|
512 | attach_to_objects |
|
|
513 | prio => -1000000, |
|
|
514 | on_clone => sub { |
|
|
515 | my ($src, $dst) = @_; |
|
|
516 | |
|
|
517 | @{$dst->registry} = @{$src->registry}; |
|
|
518 | |
|
|
519 | %$dst = %$src; |
|
|
520 | |
|
|
521 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
522 | if exists $src->{_attachment}; |
|
|
523 | }, |
|
|
524 | ; |
|
|
525 | |
235 | ############################################################################# |
526 | ############################################################################# |
236 | # old plug-in events |
527 | # old plug-in events |
237 | |
528 | |
238 | sub inject_event { |
529 | sub inject_event { |
239 | my $extension = shift; |
530 | my $extension = shift; |
… | |
… | |
284 | } |
575 | } |
285 | |
576 | |
286 | sub register { |
577 | sub register { |
287 | my ($base, $pkg) = @_; |
578 | my ($base, $pkg) = @_; |
288 | |
579 | |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
580 | #TODO |
290 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
|
|
291 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
|
|
292 | $hook[$idx]{$base} = $ref; |
|
|
293 | } |
|
|
294 | } |
|
|
295 | } |
581 | } |
296 | |
582 | |
297 | sub load_extension { |
583 | sub load_extension { |
298 | my ($path) = @_; |
584 | my ($path) = @_; |
299 | |
585 | |
… | |
… | |
330 | my ($pkg) = @_; |
616 | my ($pkg) = @_; |
331 | |
617 | |
332 | warn "removing extension $pkg\n"; |
618 | warn "removing extension $pkg\n"; |
333 | |
619 | |
334 | # remove hooks |
620 | # remove hooks |
|
|
621 | #TODO |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
622 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
336 | delete $hook[$idx]{$pkg}; |
623 | # delete $hook[$idx]{$pkg}; |
337 | } |
624 | # } |
338 | |
625 | |
339 | # remove commands |
626 | # remove commands |
340 | for my $name (keys %command) { |
627 | for my $name (keys %command) { |
341 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
628 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
342 | |
629 | |
… | |
… | |
352 | # remove extcmds |
639 | # remove extcmds |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
640 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
354 | delete $extcmd{$name}; |
641 | delete $extcmd{$name}; |
355 | } |
642 | } |
356 | |
643 | |
357 | if (my $cb = $pkg->can ("on_unload")) { |
644 | if (my $cb = $pkg->can ("unload")) { |
358 | eval { |
645 | eval { |
359 | $cb->($pkg); |
646 | $cb->($pkg); |
360 | 1 |
647 | 1 |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
648 | } or warn "$pkg unloaded, but with errors: $@"; |
362 | } |
649 | } |
… | |
… | |
408 | Symbol::delete_package $k; |
695 | Symbol::delete_package $k; |
409 | } |
696 | } |
410 | |
697 | |
411 | # 4. get rid of safe::, as good as possible |
698 | # 4. get rid of safe::, as good as possible |
412 | Symbol::delete_package "safe::$_" |
699 | Symbol::delete_package "safe::$_" |
413 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
700 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
414 | |
701 | |
415 | # 5. remove register_script_function callbacks |
702 | # 5. remove register_script_function callbacks |
416 | # TODO |
703 | # TODO |
417 | |
704 | |
418 | # 6. unload cf.pm "a bit" |
705 | # 6. unload cf.pm "a bit" |
… | |
… | |
448 | }; |
735 | }; |
449 | } |
736 | } |
450 | }; |
737 | }; |
451 | |
738 | |
452 | ############################################################################# |
739 | ############################################################################# |
453 | # utility functions |
|
|
454 | |
|
|
455 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
456 | |
|
|
457 | sub from_json($) { |
|
|
458 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
459 | JSON::Syck::Load $_[0] |
|
|
460 | } |
|
|
461 | |
|
|
462 | sub to_json($) { |
|
|
463 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
464 | JSON::Syck::Dump $_[0] |
|
|
465 | } |
|
|
466 | |
|
|
467 | ############################################################################# |
|
|
468 | # extcmd framework, basically convert ext <msg> |
740 | # extcmd framework, basically convert ext <msg> |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
741 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
470 | |
742 | |
471 | sub on_extcmd { |
743 | attach_to_players |
|
|
744 | on_extcmd => sub { |
472 | my ($pl, $buf) = @_; |
745 | my ($pl, $buf) = @_; |
473 | |
746 | |
474 | my $msg = eval { from_json $buf }; |
747 | my $msg = eval { from_json $buf }; |
475 | |
748 | |
476 | if (ref $msg) { |
749 | if (ref $msg) { |
477 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
750 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my %reply = $cb->[0]->($pl, $msg)) { |
751 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | $pl->ext_reply ($msg->{msgid}, %reply); |
752 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
753 | } |
480 | } |
754 | } |
|
|
755 | } else { |
|
|
756 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
481 | } |
757 | } |
482 | } else { |
758 | |
483 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
759 | cf::override; |
484 | } |
760 | }, |
485 | |
761 | ; |
486 | 1 |
|
|
487 | } |
|
|
488 | |
762 | |
489 | ############################################################################# |
763 | ############################################################################# |
490 | # load/save/clean perl data associated with a map |
764 | # load/save/clean perl data associated with a map |
491 | |
765 | |
492 | *cf::mapsupport::on_clean = sub { |
766 | *cf::mapsupport::on_clean = sub { |
493 | my ($map) = @_; |
767 | my ($map) = @_; |
494 | |
768 | |
495 | my $path = $map->tmpname; |
769 | my $path = $map->tmpname; |
496 | defined $path or return; |
770 | defined $path or return; |
497 | |
771 | |
498 | unlink "$path.cfperl"; |
772 | unlink "$path.pst"; |
499 | }; |
|
|
500 | |
|
|
501 | *cf::mapsupport::on_swapin = |
|
|
502 | *cf::mapsupport::on_load = sub { |
|
|
503 | my ($map) = @_; |
|
|
504 | |
|
|
505 | my $path = $map->tmpname; |
|
|
506 | $path = $map->path unless defined $path; |
|
|
507 | |
|
|
508 | open my $fh, "<:raw", "$path.cfperl" |
|
|
509 | or return; # no perl data |
|
|
510 | |
|
|
511 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
512 | |
|
|
513 | $data->{version} <= 1 |
|
|
514 | or return; # too new |
|
|
515 | |
|
|
516 | $map->_set_obs ($data->{obs}); |
|
|
517 | }; |
|
|
518 | |
|
|
519 | *cf::mapsupport::on_swapout = sub { |
|
|
520 | my ($map) = @_; |
|
|
521 | |
|
|
522 | my $path = $map->tmpname; |
|
|
523 | $path = $map->path unless defined $path; |
|
|
524 | |
|
|
525 | my $obs = $map->_get_obs; |
|
|
526 | |
|
|
527 | if (defined $obs) { |
|
|
528 | open my $fh, ">:raw", "$path.cfperl" |
|
|
529 | or die "$path.cfperl: $!"; |
|
|
530 | |
|
|
531 | stat $path; |
|
|
532 | |
|
|
533 | print $fh Storable::nfreeze { |
|
|
534 | size => (stat _)[7], |
|
|
535 | time => (stat _)[9], |
|
|
536 | version => 1, |
|
|
537 | obs => $obs, |
|
|
538 | }; |
|
|
539 | |
|
|
540 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
541 | } else { |
|
|
542 | unlink "$path.cfperl"; |
|
|
543 | } |
|
|
544 | }; |
773 | }; |
545 | |
774 | |
546 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
775 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
547 | |
776 | |
548 | ############################################################################# |
777 | ############################################################################# |
… | |
… | |
561 | $o->set_ob_key_value ("_perl_data"); |
790 | $o->set_ob_key_value ("_perl_data"); |
562 | |
791 | |
563 | %$o = %{ Storable::thaw pack "H*", $value }; |
792 | %$o = %{ Storable::thaw pack "H*", $value }; |
564 | } |
793 | } |
565 | } |
794 | } |
566 | }, |
|
|
567 | on_save => sub { |
|
|
568 | my ($pl, $path) = @_; |
|
|
569 | |
|
|
570 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
571 | for grep %$_, all_objects $pl->ob; |
|
|
572 | }, |
795 | }, |
573 | ; |
796 | ; |
574 | |
797 | |
575 | ############################################################################# |
798 | ############################################################################# |
576 | # core extensions - in perl |
799 | # core extensions - in perl |
… | |
… | |
624 | } |
847 | } |
625 | |
848 | |
626 | ############################################################################# |
849 | ############################################################################# |
627 | # map scripting support |
850 | # map scripting support |
628 | |
851 | |
629 | our $safe = new Safe "ext"; |
852 | our $safe = new Safe "safe"; |
630 | our $safe_hole = new Safe::Hole; |
853 | our $safe_hole = new Safe::Hole; |
631 | |
854 | |
632 | $SIG{FPE} = 'IGNORE'; |
855 | $SIG{FPE} = 'IGNORE'; |
633 | |
856 | |
634 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
857 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
654 | $qcode =~ s/\n/\\n/g; |
877 | $qcode =~ s/\n/\\n/g; |
655 | |
878 | |
656 | local $_; |
879 | local $_; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
880 | local @safe::cf::_safe_eval_args = values %vars; |
658 | |
881 | |
659 | $code = |
882 | my $eval = |
660 | "do {\n" |
883 | "do {\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
884 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
885 | . "#line 0 \"{$qcode}\"\n" |
663 | . $code |
886 | . $code |
664 | . "\n}" |
887 | . "\n}" |
665 | ; |
888 | ; |
666 | |
889 | |
667 | sub_generation_inc; |
890 | sub_generation_inc; |
668 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
891 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
669 | sub_generation_inc; |
892 | sub_generation_inc; |
|
|
893 | |
|
|
894 | if ($@) { |
|
|
895 | warn "$@"; |
|
|
896 | warn "while executing safe code '$code'\n"; |
|
|
897 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
898 | } |
670 | |
899 | |
671 | wantarray ? @res : $res[0] |
900 | wantarray ? @res : $res[0] |
672 | } |
901 | } |
673 | |
902 | |
674 | sub register_script_function { |
903 | sub register_script_function { |
… | |
… | |
709 | $TICK_WATCHER->at ($NEXT_TICK); |
938 | $TICK_WATCHER->at ($NEXT_TICK); |
710 | $TICK_WATCHER->start; |
939 | $TICK_WATCHER->start; |
711 | }, |
940 | }, |
712 | ); |
941 | ); |
713 | |
942 | |
|
|
943 | _reload_2; |
|
|
944 | |
714 | 1 |
945 | 1 |
715 | |
946 | |