… | |
… | |
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 %PROP_TYPE; |
20 | our %PROP_TYPE; |
19 | our %PROP_IDX; |
21 | our %PROP_IDX; |
20 | our $LIBDIR = maps_directory "perl"; |
22 | our $LIBDIR = maps_directory "perl"; |
21 | |
23 | |
|
|
24 | our $TICK = MAX_TIME * 1e-6; |
|
|
25 | our $TICK_WATCHER; |
|
|
26 | our $NEXT_TICK; |
|
|
27 | |
22 | BEGIN { |
28 | BEGIN { |
23 | @EVENT = map lc, @EVENT; |
|
|
24 | |
|
|
25 | *CORE::GLOBAL::warn = sub { |
29 | *CORE::GLOBAL::warn = sub { |
26 | my $msg = join "", @_; |
30 | my $msg = join "", @_; |
27 | $msg .= "\n" |
31 | $msg .= "\n" |
28 | unless $msg =~ /\n$/; |
32 | unless $msg =~ /\n$/; |
29 | |
33 | |
… | |
… | |
64 | prop_gen PLAYER_PROP => "cf::object::player"; |
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
65 | |
69 | |
66 | prop_gen MAP_PROP => "cf::map"; |
70 | prop_gen MAP_PROP => "cf::map"; |
67 | prop_gen ARCH_PROP => "cf::arch"; |
71 | prop_gen ARCH_PROP => "cf::arch"; |
68 | |
72 | |
69 | # guessed hierarchies |
|
|
70 | |
|
|
71 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
72 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
73 | |
74 | |
74 | # 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 |
75 | # within the Safe compartment. |
76 | # within the Safe compartment. |
76 | 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)) { |
77 | no strict 'refs'; |
78 | no strict 'refs'; |
78 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | } |
80 | } |
80 | |
81 | |
81 | $Event::DIED = sub { |
82 | $Event::DIED = sub { |
82 | warn "error in event callback: @_"; |
83 | warn "error in event callback: @_"; |
83 | }; |
84 | }; |
… | |
… | |
86 | my @exts; |
87 | my @exts; |
87 | my @hook; |
88 | my @hook; |
88 | my %command; |
89 | my %command; |
89 | my %extcmd; |
90 | my %extcmd; |
90 | |
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, $rdata, $objs) = @_; |
|
|
482 | |
|
|
483 | if (length $$rdata) { |
|
|
484 | warn sprintf "saving %s (%d,%d)\n", |
|
|
485 | $filename, length $$rdata, scalar @$objs; |
|
|
486 | |
|
|
487 | if (open my $fh, ">:raw", "$filename~") { |
|
|
488 | chmod SAVE_MODE, $fh; |
|
|
489 | syswrite $fh, $$rdata; |
|
|
490 | close $fh; |
|
|
491 | |
|
|
492 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
493 | chmod SAVE_MODE, $fh; |
|
|
494 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
495 | close $fh; |
|
|
496 | rename "$filename.pst~", "$filename.pst"; |
|
|
497 | } else { |
|
|
498 | unlink "$filename.pst"; |
|
|
499 | } |
|
|
500 | |
|
|
501 | rename "$filename~", $filename; |
|
|
502 | } else { |
|
|
503 | warn "FATAL: $filename~: $!\n"; |
|
|
504 | } |
|
|
505 | } else { |
|
|
506 | unlink $filename; |
|
|
507 | unlink "$filename.pst"; |
|
|
508 | } |
|
|
509 | } |
|
|
510 | |
|
|
511 | sub object_thawer_load { |
|
|
512 | my ($filename) = @_; |
|
|
513 | |
|
|
514 | local $/; |
|
|
515 | |
|
|
516 | my $av; |
|
|
517 | |
|
|
518 | #TODO: use sysread etc. |
|
|
519 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
520 | $data = <$data>; |
|
|
521 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
|
|
522 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
523 | } |
|
|
524 | return ($data, $av); |
|
|
525 | } |
|
|
526 | |
|
|
527 | () |
|
|
528 | } |
|
|
529 | |
|
|
530 | attach_to_objects |
|
|
531 | prio => -1000000, |
|
|
532 | on_clone => sub { |
|
|
533 | my ($src, $dst) = @_; |
|
|
534 | |
|
|
535 | @{$dst->registry} = @{$src->registry}; |
|
|
536 | |
|
|
537 | %$dst = %$src; |
|
|
538 | |
|
|
539 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
540 | if exists $src->{_attachment}; |
|
|
541 | }, |
|
|
542 | ; |
|
|
543 | |
|
|
544 | ############################################################################# |
|
|
545 | # old plug-in events |
|
|
546 | |
91 | sub inject_event { |
547 | sub inject_event { |
92 | my $extension = shift; |
548 | my $extension = shift; |
93 | my $event_code = shift; |
549 | my $event_code = shift; |
94 | |
550 | |
95 | my $cb = $hook[$event_code]{$extension} |
551 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
137 | } |
593 | } |
138 | |
594 | |
139 | sub register { |
595 | sub register { |
140 | my ($base, $pkg) = @_; |
596 | my ($base, $pkg) = @_; |
141 | |
597 | |
142 | for my $idx (0 .. $#EVENT) { |
598 | #TODO |
143 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
|
|
144 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
|
|
145 | $hook[$idx]{$base} = $ref; |
|
|
146 | } |
|
|
147 | } |
|
|
148 | } |
599 | } |
149 | |
600 | |
150 | sub load_extension { |
601 | sub load_extension { |
151 | my ($path) = @_; |
602 | my ($path) = @_; |
152 | |
603 | |
153 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
604 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
154 | my $base = $1; |
605 | my $base = $1; |
155 | my $pkg = $1; |
606 | my $pkg = $1; |
156 | $pkg =~ s/[^[:word:]]/_/g; |
607 | $pkg =~ s/[^[:word:]]/_/g; |
157 | $pkg = "cf::ext::$pkg"; |
608 | $pkg = "ext::$pkg"; |
158 | |
609 | |
159 | warn "loading '$path' into '$pkg'\n"; |
610 | warn "loading '$path' into '$pkg'\n"; |
160 | |
611 | |
161 | open my $fh, "<:utf8", $path |
612 | open my $fh, "<:utf8", $path |
162 | or die "$path: $!"; |
613 | or die "$path: $!"; |
… | |
… | |
183 | my ($pkg) = @_; |
634 | my ($pkg) = @_; |
184 | |
635 | |
185 | warn "removing extension $pkg\n"; |
636 | warn "removing extension $pkg\n"; |
186 | |
637 | |
187 | # remove hooks |
638 | # remove hooks |
|
|
639 | #TODO |
188 | for my $idx (0 .. $#EVENT) { |
640 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
189 | delete $hook[$idx]{$pkg}; |
641 | # delete $hook[$idx]{$pkg}; |
190 | } |
642 | # } |
191 | |
643 | |
192 | # remove commands |
644 | # remove commands |
193 | for my $name (keys %command) { |
645 | for my $name (keys %command) { |
194 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
646 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
195 | |
647 | |
… | |
… | |
205 | # remove extcmds |
657 | # remove extcmds |
206 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
658 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
207 | delete $extcmd{$name}; |
659 | delete $extcmd{$name}; |
208 | } |
660 | } |
209 | |
661 | |
210 | if (my $cb = $pkg->can ("on_unload")) { |
662 | if (my $cb = $pkg->can ("unload")) { |
211 | eval { |
663 | eval { |
212 | $cb->($pkg); |
664 | $cb->($pkg); |
213 | 1 |
665 | 1 |
214 | } or warn "$pkg unloaded, but with errors: $@"; |
666 | } or warn "$pkg unloaded, but with errors: $@"; |
215 | } |
667 | } |
… | |
… | |
227 | 1 |
679 | 1 |
228 | } or warn "$ext not loaded: $@"; |
680 | } or warn "$ext not loaded: $@"; |
229 | } |
681 | } |
230 | } |
682 | } |
231 | |
683 | |
|
|
684 | sub _perl_reload(&) { |
|
|
685 | my ($msg) = @_; |
|
|
686 | |
|
|
687 | $msg->("reloading..."); |
|
|
688 | |
|
|
689 | eval { |
|
|
690 | # 1. cancel all watchers |
|
|
691 | $_->cancel for Event::all_watchers; |
|
|
692 | |
|
|
693 | # 2. unload all extensions |
|
|
694 | for (@exts) { |
|
|
695 | $msg->("unloading <$_>"); |
|
|
696 | unload_extension $_; |
|
|
697 | } |
|
|
698 | |
|
|
699 | # 3. unload all modules loaded from $LIBDIR |
|
|
700 | while (my ($k, $v) = each %INC) { |
|
|
701 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
702 | |
|
|
703 | $msg->("removing <$k>"); |
|
|
704 | delete $INC{$k}; |
|
|
705 | |
|
|
706 | $k =~ s/\.pm$//; |
|
|
707 | $k =~ s/\//::/g; |
|
|
708 | |
|
|
709 | if (my $cb = $k->can ("unload_module")) { |
|
|
710 | $cb->(); |
|
|
711 | } |
|
|
712 | |
|
|
713 | Symbol::delete_package $k; |
|
|
714 | } |
|
|
715 | |
|
|
716 | # 4. get rid of safe::, as good as possible |
|
|
717 | Symbol::delete_package "safe::$_" |
|
|
718 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
719 | |
|
|
720 | # 5. remove register_script_function callbacks |
|
|
721 | # TODO |
|
|
722 | |
|
|
723 | # 6. unload cf.pm "a bit" |
|
|
724 | delete $INC{"cf.pm"}; |
|
|
725 | |
|
|
726 | # don't, removes xs symbols, too, |
|
|
727 | # and global variables created in xs |
|
|
728 | #Symbol::delete_package __PACKAGE__; |
|
|
729 | |
|
|
730 | # 7. reload cf.pm |
|
|
731 | $msg->("reloading cf.pm"); |
|
|
732 | require cf; |
|
|
733 | |
|
|
734 | $msg->("load extensions"); |
|
|
735 | cf::load_extensions; |
|
|
736 | }; |
|
|
737 | $msg->($@) if $@; |
|
|
738 | |
|
|
739 | $msg->("reloaded"); |
|
|
740 | }; |
|
|
741 | |
|
|
742 | sub perl_reload() { |
|
|
743 | _perl_reload { |
|
|
744 | warn $_[0]; |
|
|
745 | print "$_[0]\n"; |
|
|
746 | }; |
|
|
747 | } |
|
|
748 | |
232 | register_command "perl-reload", 0, sub { |
749 | register_command "perl-reload", 0, sub { |
233 | my ($who, $arg) = @_; |
750 | my ($who, $arg) = @_; |
234 | |
751 | |
235 | if ($who->flag (FLAG_WIZ)) { |
752 | if ($who->flag (FLAG_WIZ)) { |
236 | $who->message ("reloading..."); |
753 | _perl_reload { |
237 | |
754 | warn $_[0]; |
238 | warn "reloading...\n"; |
755 | $who->message ($_[0]); |
239 | eval { |
|
|
240 | # 1. cancel all watchers |
|
|
241 | $_->cancel for Event::all_watchers; |
|
|
242 | |
|
|
243 | # 2. unload all extensions |
|
|
244 | for (@exts) { |
|
|
245 | $who->message ("unloading <$_>"); |
|
|
246 | unload_extension $_; |
|
|
247 | } |
|
|
248 | |
|
|
249 | # 3. unload all modules loaded from $LIBDIR |
|
|
250 | while (my ($k, $v) = each %INC) { |
|
|
251 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
252 | |
|
|
253 | $who->message ("removing <$k>"); |
|
|
254 | delete $INC{$k}; |
|
|
255 | |
|
|
256 | $k =~ s/\.pm$//; |
|
|
257 | $k =~ s/\//::/g; |
|
|
258 | |
|
|
259 | if (my $cb = $k->can ("unload_module")) { |
|
|
260 | $cb->(); |
|
|
261 | } |
|
|
262 | |
|
|
263 | Symbol::delete_package $k; |
|
|
264 | } |
|
|
265 | |
|
|
266 | # 4. get rid of ext::, as good as possible |
|
|
267 | Symbol::delete_package "ext::$_" |
|
|
268 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
269 | |
|
|
270 | # 5. remove register_script_function callbacks |
|
|
271 | # TODO |
|
|
272 | |
|
|
273 | # 6. unload cf.pm "a bit" |
|
|
274 | delete $INC{"cf.pm"}; |
|
|
275 | |
|
|
276 | # don't, removes xs symbols, too |
|
|
277 | #Symbol::delete_package __PACKAGE__; |
|
|
278 | |
|
|
279 | # 7. reload cf.pm |
|
|
280 | $who->message ("reloading cf.pm"); |
|
|
281 | require cf; |
|
|
282 | }; |
756 | }; |
283 | warn $@ if $@; |
|
|
284 | $who->message ($@) if $@; |
|
|
285 | warn "reloaded\n"; |
|
|
286 | |
|
|
287 | $who->message ("reloaded"); |
|
|
288 | } else { |
|
|
289 | $who->message ("Intruder Alert!"); |
|
|
290 | } |
757 | } |
291 | }; |
758 | }; |
292 | |
|
|
293 | ############################################################################# |
|
|
294 | # utility functions |
|
|
295 | |
|
|
296 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
297 | |
|
|
298 | sub from_json($) { |
|
|
299 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
300 | JSON::Syck::Load $_[0] |
|
|
301 | } |
|
|
302 | |
|
|
303 | sub to_json($) { |
|
|
304 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
305 | JSON::Syck::Dump $_[0] |
|
|
306 | } |
|
|
307 | |
759 | |
308 | ############################################################################# |
760 | ############################################################################# |
309 | # extcmd framework, basically convert ext <msg> |
761 | # extcmd framework, basically convert ext <msg> |
310 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
762 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
311 | |
763 | |
312 | sub on_extcmd { |
764 | attach_to_players |
|
|
765 | on_extcmd => sub { |
313 | my ($pl, $buf) = @_; |
766 | my ($pl, $buf) = @_; |
314 | |
767 | |
315 | my $msg = eval { from_json $buf }; |
768 | my $msg = eval { from_json $buf }; |
316 | |
769 | |
317 | if (ref $msg) { |
770 | if (ref $msg) { |
318 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
771 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
319 | if (my %reply = $cb->[0]->($pl, $msg)) { |
772 | if (my %reply = $cb->[0]->($pl, $msg)) { |
320 | $pl->ext_reply ($msg->{msgid}, %reply); |
773 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
774 | } |
321 | } |
775 | } |
322 | } |
|
|
323 | } else { |
776 | } else { |
324 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
777 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
778 | } |
|
|
779 | |
|
|
780 | cf::override; |
325 | } |
781 | }, |
326 | |
782 | ; |
327 | 1 |
|
|
328 | } |
|
|
329 | |
783 | |
330 | ############################################################################# |
784 | ############################################################################# |
331 | # load/save/clean perl data associated with a map |
785 | # load/save/clean perl data associated with a map |
332 | |
786 | |
333 | *on_mapclean = sub { |
787 | *cf::mapsupport::on_clean = sub { |
334 | my ($map) = @_; |
788 | my ($map) = @_; |
335 | |
789 | |
336 | my $path = $map->tmpname; |
790 | my $path = $map->tmpname; |
337 | defined $path or return; |
791 | defined $path or return; |
338 | |
792 | |
339 | unlink "$path.cfperl"; |
793 | unlink "$path.pst"; |
340 | }; |
794 | }; |
341 | |
795 | |
342 | *on_mapin = |
796 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
343 | *on_mapload = sub { |
|
|
344 | my ($map) = @_; |
|
|
345 | |
|
|
346 | my $path = $map->tmpname; |
|
|
347 | $path = $map->path unless defined $path; |
|
|
348 | |
|
|
349 | open my $fh, "<:raw", "$path.cfperl" |
|
|
350 | or return; # no perl data |
|
|
351 | |
|
|
352 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
353 | |
|
|
354 | $data->{version} <= 1 |
|
|
355 | or return; # too new |
|
|
356 | |
|
|
357 | $map->_set_obs ($data->{obs}); |
|
|
358 | }; |
|
|
359 | |
|
|
360 | *on_mapout = sub { |
|
|
361 | my ($map) = @_; |
|
|
362 | |
|
|
363 | my $path = $map->tmpname; |
|
|
364 | $path = $map->path unless defined $path; |
|
|
365 | |
|
|
366 | my $obs = $map->_get_obs; |
|
|
367 | |
|
|
368 | if (defined $obs) { |
|
|
369 | open my $fh, ">:raw", "$path.cfperl" |
|
|
370 | or die "$path.cfperl: $!"; |
|
|
371 | |
|
|
372 | stat $path; |
|
|
373 | |
|
|
374 | print $fh Storable::nfreeze { |
|
|
375 | size => (stat _)[7], |
|
|
376 | time => (stat _)[9], |
|
|
377 | version => 1, |
|
|
378 | obs => $obs, |
|
|
379 | }; |
|
|
380 | |
|
|
381 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
382 | } else { |
|
|
383 | unlink "$path.cfperl"; |
|
|
384 | } |
|
|
385 | }; |
|
|
386 | |
797 | |
387 | ############################################################################# |
798 | ############################################################################# |
388 | # load/save perl data associated with player->ob objects |
799 | # load/save perl data associated with player->ob objects |
389 | |
800 | |
390 | *on_player_load = sub { |
801 | sub all_objects(@) { |
|
|
802 | @_, map all_objects ($_->inv), @_ |
|
|
803 | } |
|
|
804 | |
|
|
805 | # TODO: compatibility cruft, remove when no longer needed |
|
|
806 | attach_to_players |
|
|
807 | on_load => sub { |
391 | my ($ob, $path) = @_; |
808 | my ($pl, $path) = @_; |
392 | |
809 | |
393 | for my $o ($ob, $ob->inv) { |
810 | for my $o (all_objects $pl->ob) { |
394 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
811 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
395 | $o->set_ob_key_value ("_perl_data"); |
812 | $o->set_ob_key_value ("_perl_data"); |
396 | |
813 | |
397 | %$o = %{ Storable::thaw pack "H*", $value }; |
814 | %$o = %{ Storable::thaw pack "H*", $value }; |
398 | } |
815 | } |
|
|
816 | } |
399 | } |
817 | }, |
400 | }; |
818 | ; |
401 | |
|
|
402 | *on_player_save = sub { |
|
|
403 | my ($ob, $path) = @_; |
|
|
404 | |
|
|
405 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
406 | for grep %$_, $ob, $ob->inv; |
|
|
407 | }; |
|
|
408 | |
819 | |
409 | ############################################################################# |
820 | ############################################################################# |
410 | # core extensions - in perl |
821 | # core extensions - in perl |
411 | |
|
|
412 | my $delta_timer = Event->timer ( |
|
|
413 | parked => 1, |
|
|
414 | prio => Event::PRIO_HIGH, |
|
|
415 | cb => sub { Event::unloop (undef) }, |
|
|
416 | ); |
|
|
417 | |
|
|
418 | sub sleep_delta($) { |
|
|
419 | $delta_timer->at (Event::time + $_[0]); |
|
|
420 | $delta_timer->start; |
|
|
421 | Event::loop; |
|
|
422 | } |
|
|
423 | |
822 | |
424 | =item cf::player::exists $login |
823 | =item cf::player::exists $login |
425 | |
824 | |
426 | Returns true when the given account exists. |
825 | Returns true when the given account exists. |
427 | |
826 | |
… | |
… | |
470 | } |
869 | } |
471 | |
870 | |
472 | ############################################################################# |
871 | ############################################################################# |
473 | # map scripting support |
872 | # map scripting support |
474 | |
873 | |
475 | our $safe = new Safe "ext"; |
874 | our $safe = new Safe "safe"; |
476 | our $safe_hole = new Safe::Hole; |
875 | our $safe_hole = new Safe::Hole; |
477 | |
876 | |
478 | $SIG{FPE} = 'IGNORE'; |
877 | $SIG{FPE} = 'IGNORE'; |
479 | |
878 | |
480 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
879 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
486 | ["cf::object::player" => qw(player)], |
885 | ["cf::object::player" => qw(player)], |
487 | ["cf::player" => qw(peaceful)], |
886 | ["cf::player" => qw(peaceful)], |
488 | ) { |
887 | ) { |
489 | no strict 'refs'; |
888 | no strict 'refs'; |
490 | my ($pkg, @funs) = @$_; |
889 | my ($pkg, @funs) = @$_; |
491 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
890 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
492 | for @funs; |
891 | for @funs; |
493 | } |
892 | } |
494 | |
893 | |
495 | sub safe_eval($;@) { |
894 | sub safe_eval($;@) { |
496 | my ($code, %vars) = @_; |
895 | my ($code, %vars) = @_; |
… | |
… | |
498 | my $qcode = $code; |
897 | my $qcode = $code; |
499 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
898 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
500 | $qcode =~ s/\n/\\n/g; |
899 | $qcode =~ s/\n/\\n/g; |
501 | |
900 | |
502 | local $_; |
901 | local $_; |
503 | local @ext::cf::_safe_eval_args = values %vars; |
902 | local @safe::cf::_safe_eval_args = values %vars; |
504 | |
903 | |
505 | $code = |
904 | my $eval = |
506 | "do {\n" |
905 | "do {\n" |
507 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
906 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
508 | . "#line 0 \"{$qcode}\"\n" |
907 | . "#line 0 \"{$qcode}\"\n" |
509 | . $code |
908 | . $code |
510 | . "\n}" |
909 | . "\n}" |
511 | ; |
910 | ; |
512 | |
911 | |
513 | sub_generation_inc; |
912 | sub_generation_inc; |
514 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
913 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
515 | sub_generation_inc; |
914 | sub_generation_inc; |
|
|
915 | |
|
|
916 | if ($@) { |
|
|
917 | warn "$@"; |
|
|
918 | warn "while executing safe code '$code'\n"; |
|
|
919 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
920 | } |
516 | |
921 | |
517 | wantarray ? @res : $res[0] |
922 | wantarray ? @res : $res[0] |
518 | } |
923 | } |
519 | |
924 | |
520 | sub register_script_function { |
925 | sub register_script_function { |
521 | my ($fun, $cb) = @_; |
926 | my ($fun, $cb) = @_; |
522 | |
927 | |
523 | no strict 'refs'; |
928 | no strict 'refs'; |
524 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
929 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
|
|
930 | } |
|
|
931 | |
|
|
932 | ############################################################################# |
|
|
933 | # the server's main() |
|
|
934 | |
|
|
935 | sub main { |
|
|
936 | load_extensions; |
|
|
937 | Event::loop; |
525 | } |
938 | } |
526 | |
939 | |
527 | ############################################################################# |
940 | ############################################################################# |
528 | # initialisation |
941 | # initialisation |
529 | |
942 | |
530 | register "<global>", __PACKAGE__; |
943 | register "<global>", __PACKAGE__; |
531 | |
944 | |
532 | unshift @INC, $LIBDIR; |
945 | unshift @INC, $LIBDIR; |
533 | |
946 | |
534 | load_extensions; |
947 | $TICK_WATCHER = Event->timer ( |
|
|
948 | prio => 1, |
|
|
949 | at => $NEXT_TICK || 1, |
|
|
950 | cb => sub { |
|
|
951 | cf::server_tick; # one server iteration |
|
|
952 | |
|
|
953 | my $NOW = Event::time; |
|
|
954 | $NEXT_TICK += $TICK; |
|
|
955 | |
|
|
956 | # if we are delayed by four ticks, skip them all |
|
|
957 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
958 | |
|
|
959 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
960 | $TICK_WATCHER->start; |
|
|
961 | }, |
|
|
962 | ); |
|
|
963 | |
|
|
964 | _reload_2; |
535 | |
965 | |
536 | 1 |
966 | 1 |
537 | |
967 | |