… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use Time::HiRes; |
10 | use Event; |
11 | use Event; |
11 | $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 |
12 | |
13 | |
13 | use strict; |
14 | use strict; |
14 | |
15 | |
|
|
16 | _reload_1; |
|
|
17 | |
15 | our %COMMAND; |
18 | our %COMMAND = (); |
16 | our @EVENT; |
19 | our @EVENT; |
17 | our %PROP_TYPE; |
20 | our %PROP_TYPE; |
18 | our %PROP_IDX; |
21 | our %PROP_IDX; |
19 | our $LIBDIR = maps_directory "perl"; |
22 | our $LIBDIR = maps_directory "perl"; |
20 | |
23 | |
|
|
24 | our $TICK = MAX_TIME * 1e-6; |
|
|
25 | our $TICK_WATCHER; |
|
|
26 | our $NEXT_TICK; |
|
|
27 | |
21 | BEGIN { |
28 | BEGIN { |
22 | @EVENT = map lc, @EVENT; |
|
|
23 | |
|
|
24 | *CORE::GLOBAL::warn = sub { |
29 | *CORE::GLOBAL::warn = sub { |
25 | my $msg = join "", @_; |
30 | my $msg = join "", @_; |
26 | $msg .= "\n" |
31 | $msg .= "\n" |
27 | unless $msg =~ /\n$/; |
32 | unless $msg =~ /\n$/; |
28 | |
33 | |
… | |
… | |
63 | prop_gen PLAYER_PROP => "cf::object::player"; |
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
64 | |
69 | |
65 | prop_gen MAP_PROP => "cf::map"; |
70 | prop_gen MAP_PROP => "cf::map"; |
66 | prop_gen ARCH_PROP => "cf::arch"; |
71 | prop_gen ARCH_PROP => "cf::arch"; |
67 | |
72 | |
68 | # guessed hierarchies |
|
|
69 | |
|
|
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
72 | |
74 | |
73 | # we bless all objects into derived classes to force a method lookup |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
74 | # within the Safe compartment. |
76 | # within the Safe compartment. |
75 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
76 | no strict 'refs'; |
78 | no strict 'refs'; |
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
78 | } |
80 | } |
79 | |
81 | |
80 | $Event::DIED = sub { |
82 | $Event::DIED = sub { |
81 | warn "error in event callback: @_"; |
83 | warn "error in event callback: @_"; |
82 | }; |
84 | }; |
… | |
… | |
85 | my @exts; |
87 | my @exts; |
86 | my @hook; |
88 | my @hook; |
87 | my %command; |
89 | my %command; |
88 | my %extcmd; |
90 | my %extcmd; |
89 | |
91 | |
|
|
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 | ############################################################################# |
|
|
108 | # "new" plug-in system |
|
|
109 | |
|
|
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. |
|
|
139 | |
|
|
140 | =item cf::attach_global ... |
|
|
141 | |
|
|
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. |
|
|
180 | |
|
|
181 | =item cf::attach_to_objects ... |
|
|
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 | |
|
|
187 | =item cf::attach_to_players ... |
|
|
188 | |
|
|
189 | Attach handlers to all players. |
|
|
190 | |
|
|
191 | =item cf::attach_to_maps ... |
|
|
192 | |
|
|
193 | Attach handlers to all maps. |
|
|
194 | |
|
|
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. |
|
|
209 | |
|
|
210 | =cut |
|
|
211 | |
|
|
212 | # the following variables are defined in .xs and must not be re-created |
|
|
213 | our @CB_GLOBAL = (); # registry for all global events |
|
|
214 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
|
|
215 | our @CB_PLAYER = (); |
|
|
216 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
217 | our @CB_MAP = (); |
|
|
218 | |
|
|
219 | my %attachment; |
|
|
220 | |
|
|
221 | sub _attach_cb($\%$$$) { |
|
|
222 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
223 | |
|
|
224 | use sort 'stable'; |
|
|
225 | |
|
|
226 | $cb = [$prio, $cb]; |
|
|
227 | |
|
|
228 | @{$registry->[$event]} = sort |
|
|
229 | { $a->[0] cmp $b->[0] } |
|
|
230 | @{$registry->[$event] || []}, $cb; |
|
|
231 | |
|
|
232 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
233 | } |
|
|
234 | |
|
|
235 | # attach handles attaching event callbacks |
|
|
236 | # the only thing the caller has to do is pass the correct |
|
|
237 | # registry (== where the callback attaches to). |
|
|
238 | sub _attach(\@$@) { |
|
|
239 | my ($registry, $klass, @arg) = @_; |
|
|
240 | |
|
|
241 | my $prio = 0; |
|
|
242 | |
|
|
243 | my %undo = ( |
|
|
244 | registry => $registry, |
|
|
245 | cb => [], |
|
|
246 | ); |
|
|
247 | |
|
|
248 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
249 | |
|
|
250 | while (@arg) { |
|
|
251 | my $type = shift @arg; |
|
|
252 | |
|
|
253 | if ($type eq "prio") { |
|
|
254 | $prio = shift @arg; |
|
|
255 | |
|
|
256 | } elsif ($type eq "package") { |
|
|
257 | my $pkg = shift @arg; |
|
|
258 | |
|
|
259 | while (my ($name, $id) = each %cb_id) { |
|
|
260 | if (my $cb = $pkg->can ($name)) { |
|
|
261 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
262 | } |
|
|
263 | } |
|
|
264 | |
|
|
265 | } elsif (exists $cb_id{$type}) { |
|
|
266 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
|
|
267 | |
|
|
268 | } elsif (ref $type) { |
|
|
269 | warn "attaching objects not supported, ignoring.\n"; |
|
|
270 | |
|
|
271 | } else { |
|
|
272 | shift @arg; |
|
|
273 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
274 | } |
|
|
275 | } |
|
|
276 | |
|
|
277 | \%undo |
|
|
278 | } |
|
|
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 | |
|
|
306 | *cf::object::attach = |
|
|
307 | *cf::player::attach = |
|
|
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 | }; |
|
|
331 | |
|
|
332 | sub attach_global { |
|
|
333 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
334 | } |
|
|
335 | |
|
|
336 | sub attach_to_type { |
|
|
337 | my $type = shift; |
|
|
338 | my $subtype = shift; |
|
|
339 | |
|
|
340 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
|
|
341 | } |
|
|
342 | |
|
|
343 | sub attach_to_objects { |
|
|
344 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
345 | } |
|
|
346 | |
|
|
347 | sub attach_to_players { |
|
|
348 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
349 | } |
|
|
350 | |
|
|
351 | sub attach_to_maps { |
|
|
352 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
353 | } |
|
|
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 | |
|
|
373 | our $override; |
|
|
374 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
|
|
375 | |
|
|
376 | sub override { |
|
|
377 | $override = 1; |
|
|
378 | @invoke_results = (); |
|
|
379 | } |
|
|
380 | |
|
|
381 | sub do_invoke { |
|
|
382 | my $event = shift; |
|
|
383 | my $callbacks = shift; |
|
|
384 | |
|
|
385 | @invoke_results = (); |
|
|
386 | |
|
|
387 | local $override; |
|
|
388 | |
|
|
389 | for (@$callbacks) { |
|
|
390 | eval { &{$_->[1]} }; |
|
|
391 | |
|
|
392 | if ($@) { |
|
|
393 | warn "$@"; |
|
|
394 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
|
|
395 | override; |
|
|
396 | } |
|
|
397 | |
|
|
398 | return 1 if $override; |
|
|
399 | } |
|
|
400 | |
|
|
401 | 0 |
|
|
402 | } |
|
|
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, $objs) = @_; |
|
|
482 | |
|
|
483 | if (@$objs) { |
|
|
484 | open my $fh, ">:raw", "$filename.pst~"; |
|
|
485 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
486 | close $fh; |
|
|
487 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
488 | rename "$filename.pst~", "$filename.pst"; |
|
|
489 | } else { |
|
|
490 | unlink "$filename.pst"; |
|
|
491 | } |
|
|
492 | |
|
|
493 | chmod SAVE_MODE, "$filename~"; |
|
|
494 | rename "$filename~", $filename; |
|
|
495 | } |
|
|
496 | |
|
|
497 | sub object_thawer_load { |
|
|
498 | my ($filename) = @_; |
|
|
499 | |
|
|
500 | open my $fh, "<:raw:perlio", "$filename.pst" |
|
|
501 | or return; |
|
|
502 | |
|
|
503 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
|
|
504 | } |
|
|
505 | |
|
|
506 | attach_to_objects |
|
|
507 | prio => -1000000, |
|
|
508 | on_clone => sub { |
|
|
509 | my ($src, $dst) = @_; |
|
|
510 | |
|
|
511 | @{$dst->registry} = @{$src->registry}; |
|
|
512 | |
|
|
513 | %$dst = %$src; |
|
|
514 | |
|
|
515 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
516 | if exists $src->{_attachment}; |
|
|
517 | }, |
|
|
518 | ; |
|
|
519 | |
|
|
520 | ############################################################################# |
|
|
521 | # old plug-in events |
|
|
522 | |
90 | sub inject_event { |
523 | sub inject_event { |
91 | my $extension = shift; |
524 | my $extension = shift; |
92 | my $event_code = shift; |
525 | my $event_code = shift; |
93 | |
526 | |
94 | my $cb = $hook[$event_code]{$extension} |
527 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
136 | } |
569 | } |
137 | |
570 | |
138 | sub register { |
571 | sub register { |
139 | my ($base, $pkg) = @_; |
572 | my ($base, $pkg) = @_; |
140 | |
573 | |
141 | for my $idx (0 .. $#EVENT) { |
574 | #TODO |
142 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
|
|
143 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
|
|
144 | $hook[$idx]{$base} = $ref; |
|
|
145 | } |
|
|
146 | } |
|
|
147 | } |
575 | } |
148 | |
576 | |
149 | sub load_extension { |
577 | sub load_extension { |
150 | my ($path) = @_; |
578 | my ($path) = @_; |
151 | |
579 | |
152 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
580 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
153 | my $base = $1; |
581 | my $base = $1; |
154 | my $pkg = $1; |
582 | my $pkg = $1; |
155 | $pkg =~ s/[^[:word:]]/_/g; |
583 | $pkg =~ s/[^[:word:]]/_/g; |
156 | $pkg = "cf::ext::$pkg"; |
584 | $pkg = "ext::$pkg"; |
157 | |
585 | |
158 | warn "loading '$path' into '$pkg'\n"; |
586 | warn "loading '$path' into '$pkg'\n"; |
159 | |
587 | |
160 | open my $fh, "<:utf8", $path |
588 | open my $fh, "<:utf8", $path |
161 | or die "$path: $!"; |
589 | or die "$path: $!"; |
… | |
… | |
181 | sub unload_extension { |
609 | sub unload_extension { |
182 | my ($pkg) = @_; |
610 | my ($pkg) = @_; |
183 | |
611 | |
184 | warn "removing extension $pkg\n"; |
612 | warn "removing extension $pkg\n"; |
185 | |
613 | |
186 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
187 | $cb->($pkg); |
|
|
188 | } |
|
|
189 | |
|
|
190 | # remove hooks |
614 | # remove hooks |
|
|
615 | #TODO |
191 | for my $idx (0 .. $#EVENT) { |
616 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
192 | delete $hook[$idx]{$pkg}; |
617 | # delete $hook[$idx]{$pkg}; |
193 | } |
618 | # } |
194 | |
619 | |
195 | # remove commands |
620 | # remove commands |
196 | for my $name (keys %command) { |
621 | for my $name (keys %command) { |
197 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
622 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
198 | |
623 | |
… | |
… | |
208 | # remove extcmds |
633 | # remove extcmds |
209 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
634 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
210 | delete $extcmd{$name}; |
635 | delete $extcmd{$name}; |
211 | } |
636 | } |
212 | |
637 | |
|
|
638 | if (my $cb = $pkg->can ("unload")) { |
|
|
639 | eval { |
|
|
640 | $cb->($pkg); |
|
|
641 | 1 |
|
|
642 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
643 | } |
|
|
644 | |
213 | Symbol::delete_package $pkg; |
645 | Symbol::delete_package $pkg; |
214 | } |
646 | } |
215 | |
647 | |
216 | sub load_extensions { |
648 | sub load_extensions { |
217 | my $LIBDIR = maps_directory "perl"; |
649 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
223 | 1 |
655 | 1 |
224 | } or warn "$ext not loaded: $@"; |
656 | } or warn "$ext not loaded: $@"; |
225 | } |
657 | } |
226 | } |
658 | } |
227 | |
659 | |
|
|
660 | sub _perl_reload(&) { |
|
|
661 | my ($msg) = @_; |
|
|
662 | |
|
|
663 | $msg->("reloading..."); |
|
|
664 | |
|
|
665 | eval { |
|
|
666 | # 1. cancel all watchers |
|
|
667 | $_->cancel for Event::all_watchers; |
|
|
668 | |
|
|
669 | # 2. unload all extensions |
|
|
670 | for (@exts) { |
|
|
671 | $msg->("unloading <$_>"); |
|
|
672 | unload_extension $_; |
|
|
673 | } |
|
|
674 | |
|
|
675 | # 3. unload all modules loaded from $LIBDIR |
|
|
676 | while (my ($k, $v) = each %INC) { |
|
|
677 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
678 | |
|
|
679 | $msg->("removing <$k>"); |
|
|
680 | delete $INC{$k}; |
|
|
681 | |
|
|
682 | $k =~ s/\.pm$//; |
|
|
683 | $k =~ s/\//::/g; |
|
|
684 | |
|
|
685 | if (my $cb = $k->can ("unload_module")) { |
|
|
686 | $cb->(); |
|
|
687 | } |
|
|
688 | |
|
|
689 | Symbol::delete_package $k; |
|
|
690 | } |
|
|
691 | |
|
|
692 | # 4. get rid of safe::, as good as possible |
|
|
693 | Symbol::delete_package "safe::$_" |
|
|
694 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
695 | |
|
|
696 | # 5. remove register_script_function callbacks |
|
|
697 | # TODO |
|
|
698 | |
|
|
699 | # 6. unload cf.pm "a bit" |
|
|
700 | delete $INC{"cf.pm"}; |
|
|
701 | |
|
|
702 | # don't, removes xs symbols, too, |
|
|
703 | # and global variables created in xs |
|
|
704 | #Symbol::delete_package __PACKAGE__; |
|
|
705 | |
|
|
706 | # 7. reload cf.pm |
|
|
707 | $msg->("reloading cf.pm"); |
|
|
708 | require cf; |
|
|
709 | }; |
|
|
710 | $msg->($@) if $@; |
|
|
711 | |
|
|
712 | $msg->("reloaded"); |
|
|
713 | }; |
|
|
714 | |
|
|
715 | sub perl_reload() { |
|
|
716 | _perl_reload { |
|
|
717 | warn $_[0]; |
|
|
718 | print "$_[0]\n"; |
|
|
719 | }; |
|
|
720 | } |
|
|
721 | |
228 | register_command "perl-reload", 0, sub { |
722 | register_command "perl-reload", 0, sub { |
229 | my ($who, $arg) = @_; |
723 | my ($who, $arg) = @_; |
230 | |
724 | |
231 | if ($who->flag (FLAG_WIZ)) { |
725 | if ($who->flag (FLAG_WIZ)) { |
232 | $who->message ("reloading..."); |
726 | _perl_reload { |
233 | |
727 | warn $_[0]; |
234 | warn "reloading...\n"; |
728 | $who->message ($_[0]); |
235 | eval { |
|
|
236 | # 1. cancel all watchers |
|
|
237 | $_->cancel for Event::all_watchers; |
|
|
238 | |
|
|
239 | # 2. unload all extensions |
|
|
240 | for (@exts) { |
|
|
241 | $who->message ("unloading <$_>"); |
|
|
242 | unload_extension $_; |
|
|
243 | } |
|
|
244 | |
|
|
245 | # 3. unload all modules loaded from $LIBDIR |
|
|
246 | while (my ($k, $v) = each %INC) { |
|
|
247 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
248 | |
|
|
249 | $who->message ("removing <$k>"); |
|
|
250 | delete $INC{$k}; |
|
|
251 | |
|
|
252 | $k =~ s/\.pm$//; |
|
|
253 | $k =~ s/\//::/g; |
|
|
254 | |
|
|
255 | if (my $cb = $k->can ("unload_module")) { |
|
|
256 | $cb->(); |
|
|
257 | } |
|
|
258 | |
|
|
259 | Symbol::delete_package $k; |
|
|
260 | } |
|
|
261 | |
|
|
262 | # 4. get rid of ext::, as good as possible |
|
|
263 | Symbol::delete_package "ext::$_" |
|
|
264 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
265 | |
|
|
266 | # 5. remove register_script_function callbacks |
|
|
267 | # TODO |
|
|
268 | |
|
|
269 | # 6. unload cf.pm "a bit" |
|
|
270 | delete $INC{"cf.pm"}; |
|
|
271 | |
|
|
272 | # don't, removes xs symbols, too |
|
|
273 | #Symbol::delete_package __PACKAGE__; |
|
|
274 | |
|
|
275 | # 7. reload cf.pm |
|
|
276 | $who->message ("reloading cf.pm"); |
|
|
277 | require cf; |
|
|
278 | }; |
729 | }; |
279 | warn $@ if $@; |
|
|
280 | $who->message ($@) if $@; |
|
|
281 | warn "reloaded\n"; |
|
|
282 | |
|
|
283 | $who->message ("reloaded"); |
|
|
284 | } else { |
|
|
285 | $who->message ("Intruder Alert!"); |
|
|
286 | } |
730 | } |
287 | }; |
731 | }; |
288 | |
|
|
289 | ############################################################################# |
|
|
290 | # utility functions |
|
|
291 | |
|
|
292 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
293 | |
|
|
294 | $JSON::Syck::ImplicitUnicode = 1; |
|
|
295 | |
|
|
296 | sub from_json($) { |
|
|
297 | JSON::Syck::Load $_[0] |
|
|
298 | } |
|
|
299 | |
|
|
300 | sub to_json($) { |
|
|
301 | JSON::Syck::Dump $_[0] |
|
|
302 | } |
|
|
303 | |
732 | |
304 | ############################################################################# |
733 | ############################################################################# |
305 | # extcmd framework, basically convert ext <msg> |
734 | # extcmd framework, basically convert ext <msg> |
306 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
735 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
307 | |
736 | |
308 | sub on_extcmd { |
737 | attach_to_players |
|
|
738 | on_extcmd => sub { |
309 | my ($pl, $buf) = @_; |
739 | my ($pl, $buf) = @_; |
310 | |
740 | |
311 | my $msg = eval { from_json $buf }; |
741 | my $msg = eval { from_json $buf }; |
312 | |
742 | |
313 | if (ref $msg) { |
743 | if (ref $msg) { |
314 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
744 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
315 | if (my %reply = $cb->[0]->($pl, $msg)) { |
745 | if (my %reply = $cb->[0]->($pl, $msg)) { |
316 | $pl->ext_reply ($msg->{msgid}, %reply); |
746 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
747 | } |
|
|
748 | } |
|
|
749 | } else { |
|
|
750 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
751 | } |
|
|
752 | |
|
|
753 | cf::override; |
|
|
754 | }, |
|
|
755 | ; |
|
|
756 | |
|
|
757 | ############################################################################# |
|
|
758 | # load/save/clean perl data associated with a map |
|
|
759 | |
|
|
760 | *cf::mapsupport::on_clean = sub { |
|
|
761 | my ($map) = @_; |
|
|
762 | |
|
|
763 | my $path = $map->tmpname; |
|
|
764 | defined $path or return; |
|
|
765 | |
|
|
766 | unlink "$path.pst"; |
|
|
767 | }; |
|
|
768 | |
|
|
769 | # old style persistent data, TODO: remove #d# |
|
|
770 | *cf::mapsupport::on_swapin = |
|
|
771 | *cf::mapsupport::on_load = sub { |
|
|
772 | my ($map) = @_; |
|
|
773 | |
|
|
774 | my $path = $map->tmpname; |
|
|
775 | $path = $map->path unless defined $path; |
|
|
776 | |
|
|
777 | open my $fh, "<:raw", "$path.cfperl" |
|
|
778 | or return; # no perl data |
|
|
779 | |
|
|
780 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
781 | |
|
|
782 | $data->{version} <= 1 |
|
|
783 | or return; # too new |
|
|
784 | |
|
|
785 | $map->_set_obs ($data->{obs}); |
|
|
786 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
787 | }; |
|
|
788 | |
|
|
789 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
790 | |
|
|
791 | ############################################################################# |
|
|
792 | # load/save perl data associated with player->ob objects |
|
|
793 | |
|
|
794 | sub all_objects(@) { |
|
|
795 | @_, map all_objects ($_->inv), @_ |
|
|
796 | } |
|
|
797 | |
|
|
798 | attach_to_players |
|
|
799 | on_load => sub { |
|
|
800 | my ($pl, $path) = @_; |
|
|
801 | |
|
|
802 | for my $o (all_objects $pl->ob) { |
|
|
803 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
804 | $o->set_ob_key_value ("_perl_data"); |
|
|
805 | |
|
|
806 | %$o = %{ Storable::thaw pack "H*", $value }; |
317 | } |
807 | } |
318 | } |
808 | } |
319 | } else { |
|
|
320 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
321 | } |
809 | }, |
322 | |
810 | ; |
323 | 1 |
|
|
324 | } |
|
|
325 | |
|
|
326 | ############################################################################# |
|
|
327 | # load/save/clean perl data associated with a map |
|
|
328 | |
|
|
329 | *on_mapclean = sub { |
|
|
330 | my ($map) = @_; |
|
|
331 | |
|
|
332 | my $path = $map->tmpname; |
|
|
333 | defined $path or return; |
|
|
334 | |
|
|
335 | unlink "$path.cfperl"; |
|
|
336 | }; |
|
|
337 | |
|
|
338 | *on_mapin = |
|
|
339 | *on_mapload = sub { |
|
|
340 | my ($map) = @_; |
|
|
341 | |
|
|
342 | my $path = $map->tmpname; |
|
|
343 | $path = $map->path unless defined $path; |
|
|
344 | |
|
|
345 | open my $fh, "<:raw", "$path.cfperl" |
|
|
346 | or return; # no perl data |
|
|
347 | |
|
|
348 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
349 | |
|
|
350 | $data->{version} <= 1 |
|
|
351 | or return; # too new |
|
|
352 | |
|
|
353 | $map->_set_obs ($data->{obs}); |
|
|
354 | }; |
|
|
355 | |
|
|
356 | *on_mapout = sub { |
|
|
357 | my ($map) = @_; |
|
|
358 | |
|
|
359 | my $path = $map->tmpname; |
|
|
360 | $path = $map->path unless defined $path; |
|
|
361 | |
|
|
362 | my $obs = $map->_get_obs; |
|
|
363 | |
|
|
364 | if (defined $obs) { |
|
|
365 | open my $fh, ">:raw", "$path.cfperl" |
|
|
366 | or die "$path.cfperl: $!"; |
|
|
367 | |
|
|
368 | stat $path; |
|
|
369 | |
|
|
370 | print $fh Storable::nfreeze { |
|
|
371 | size => (stat _)[7], |
|
|
372 | time => (stat _)[9], |
|
|
373 | version => 1, |
|
|
374 | obs => $obs, |
|
|
375 | }; |
|
|
376 | |
|
|
377 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
378 | } else { |
|
|
379 | unlink "$path.cfperl"; |
|
|
380 | } |
|
|
381 | }; |
|
|
382 | |
|
|
383 | ############################################################################# |
|
|
384 | # load/save perl data associated with player->ob objects |
|
|
385 | |
|
|
386 | *on_player_load = sub { |
|
|
387 | my ($ob, $path) = @_; |
|
|
388 | |
|
|
389 | for my $o ($ob, $ob->inv) { |
|
|
390 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
391 | $o->set_ob_key_value ("_perl_data"); |
|
|
392 | |
|
|
393 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
394 | } |
|
|
395 | } |
|
|
396 | }; |
|
|
397 | |
|
|
398 | *on_player_save = sub { |
|
|
399 | my ($ob, $path) = @_; |
|
|
400 | |
|
|
401 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
402 | for grep %$_, $ob, $ob->inv; |
|
|
403 | }; |
|
|
404 | |
811 | |
405 | ############################################################################# |
812 | ############################################################################# |
406 | # core extensions - in perl |
813 | # core extensions - in perl |
407 | |
814 | |
408 | =item cf::player::exists $login |
815 | =item cf::player::exists $login |
… | |
… | |
454 | } |
861 | } |
455 | |
862 | |
456 | ############################################################################# |
863 | ############################################################################# |
457 | # map scripting support |
864 | # map scripting support |
458 | |
865 | |
459 | our $safe = new Safe "ext"; |
866 | our $safe = new Safe "safe"; |
460 | our $safe_hole = new Safe::Hole; |
867 | our $safe_hole = new Safe::Hole; |
461 | |
868 | |
462 | $SIG{FPE} = 'IGNORE'; |
869 | $SIG{FPE} = 'IGNORE'; |
463 | |
870 | |
464 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
871 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
470 | ["cf::object::player" => qw(player)], |
877 | ["cf::object::player" => qw(player)], |
471 | ["cf::player" => qw(peaceful)], |
878 | ["cf::player" => qw(peaceful)], |
472 | ) { |
879 | ) { |
473 | no strict 'refs'; |
880 | no strict 'refs'; |
474 | my ($pkg, @funs) = @$_; |
881 | my ($pkg, @funs) = @$_; |
475 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
882 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
476 | for @funs; |
883 | for @funs; |
477 | } |
884 | } |
478 | |
885 | |
479 | sub safe_eval($;@) { |
886 | sub safe_eval($;@) { |
480 | my ($code, %vars) = @_; |
887 | my ($code, %vars) = @_; |
… | |
… | |
482 | my $qcode = $code; |
889 | my $qcode = $code; |
483 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
890 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
484 | $qcode =~ s/\n/\\n/g; |
891 | $qcode =~ s/\n/\\n/g; |
485 | |
892 | |
486 | local $_; |
893 | local $_; |
487 | local @ext::cf::_safe_eval_args = values %vars; |
894 | local @safe::cf::_safe_eval_args = values %vars; |
488 | |
895 | |
489 | $code = |
896 | my $eval = |
490 | "do {\n" |
897 | "do {\n" |
491 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
898 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
492 | . "#line 0 \"{$qcode}\"\n" |
899 | . "#line 0 \"{$qcode}\"\n" |
493 | . $code |
900 | . $code |
494 | . "\n}" |
901 | . "\n}" |
495 | ; |
902 | ; |
496 | |
903 | |
497 | sub_generation_inc; |
904 | sub_generation_inc; |
498 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
905 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
499 | sub_generation_inc; |
906 | sub_generation_inc; |
|
|
907 | |
|
|
908 | if ($@) { |
|
|
909 | warn "$@"; |
|
|
910 | warn "while executing safe code '$code'\n"; |
|
|
911 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
912 | } |
500 | |
913 | |
501 | wantarray ? @res : $res[0] |
914 | wantarray ? @res : $res[0] |
502 | } |
915 | } |
503 | |
916 | |
504 | sub register_script_function { |
917 | sub register_script_function { |
505 | my ($fun, $cb) = @_; |
918 | my ($fun, $cb) = @_; |
506 | |
919 | |
507 | no strict 'refs'; |
920 | no strict 'refs'; |
508 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
921 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
|
|
922 | } |
|
|
923 | |
|
|
924 | ############################################################################# |
|
|
925 | # the server's main() |
|
|
926 | |
|
|
927 | sub main { |
|
|
928 | Event::loop; |
509 | } |
929 | } |
510 | |
930 | |
511 | ############################################################################# |
931 | ############################################################################# |
512 | # initialisation |
932 | # initialisation |
513 | |
933 | |
… | |
… | |
515 | |
935 | |
516 | unshift @INC, $LIBDIR; |
936 | unshift @INC, $LIBDIR; |
517 | |
937 | |
518 | load_extensions; |
938 | load_extensions; |
519 | |
939 | |
|
|
940 | $TICK_WATCHER = Event->timer ( |
|
|
941 | prio => 1, |
|
|
942 | at => $NEXT_TICK || 1, |
|
|
943 | cb => sub { |
|
|
944 | cf::server_tick; # one server iteration |
|
|
945 | |
|
|
946 | my $NOW = Event::time; |
|
|
947 | $NEXT_TICK += $TICK; |
|
|
948 | |
|
|
949 | # if we are delayed by four ticks, skip them all |
|
|
950 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
951 | |
|
|
952 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
953 | $TICK_WATCHER->start; |
|
|
954 | }, |
|
|
955 | ); |
|
|
956 | |
|
|
957 | _reload_2; |
|
|
958 | |
520 | 1 |
959 | 1 |
521 | |
960 | |