… | |
… | |
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; |
16 | _init_vars; |
17 | |
17 | |
18 | our %COMMAND = (); |
18 | our %COMMAND = (); |
19 | our @EVENT; |
19 | our @EVENT; |
20 | our %PROP_TYPE; |
|
|
21 | our %PROP_IDX; |
|
|
22 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
23 | |
21 | |
24 | our $TICK = MAX_TIME * 1e-6; |
22 | our $TICK = MAX_TIME * 1e-6; |
25 | our $TICK_WATCHER; |
23 | our $TICK_WATCHER; |
26 | our $NEXT_TICK; |
24 | our $NEXT_TICK; |
|
|
25 | |
|
|
26 | our %CFG; |
|
|
27 | |
|
|
28 | ############################################################################# |
|
|
29 | |
|
|
30 | =head2 GLOBAL VARIABLES |
|
|
31 | |
|
|
32 | =over 4 |
|
|
33 | |
|
|
34 | =item $cf::LIBDIR |
|
|
35 | |
|
|
36 | The perl library directory, where extensions and cf-specific modules can |
|
|
37 | be found. It will be added to C<@INC> automatically. |
|
|
38 | |
|
|
39 | =item $cf::TICK |
|
|
40 | |
|
|
41 | The interval between server ticks, in seconds. |
|
|
42 | |
|
|
43 | =item %cf::CFG |
|
|
44 | |
|
|
45 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
|
|
46 | from wherever your confdir points to. |
|
|
47 | |
|
|
48 | =back |
|
|
49 | |
|
|
50 | =cut |
27 | |
51 | |
28 | BEGIN { |
52 | BEGIN { |
29 | *CORE::GLOBAL::warn = sub { |
53 | *CORE::GLOBAL::warn = sub { |
30 | my $msg = join "", @_; |
54 | my $msg = join "", @_; |
31 | $msg .= "\n" |
55 | $msg .= "\n" |
… | |
… | |
34 | print STDERR "cfperl: $msg"; |
58 | print STDERR "cfperl: $msg"; |
35 | LOG llevError, "cfperl: $msg"; |
59 | LOG llevError, "cfperl: $msg"; |
36 | }; |
60 | }; |
37 | } |
61 | } |
38 | |
62 | |
39 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
|
|
40 | |
|
|
41 | # generate property mutators |
|
|
42 | sub prop_gen { |
|
|
43 | my ($prefix, $class) = @_; |
|
|
44 | |
|
|
45 | no strict 'refs'; |
|
|
46 | |
|
|
47 | for my $prop (keys %PROP_TYPE) { |
|
|
48 | $prop =~ /^\Q$prefix\E_(.*$)/ or next; |
|
|
49 | my $sub = lc $1; |
|
|
50 | |
|
|
51 | my $type = $PROP_TYPE{$prop}; |
|
|
52 | my $idx = $PROP_IDX {$prop}; |
|
|
53 | |
|
|
54 | *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { |
|
|
55 | $_[0]->get_property ($type, $idx) |
|
|
56 | }; |
|
|
57 | |
|
|
58 | *{"$class\::set_$sub"} = sub { |
|
|
59 | $_[0]->set_property ($type, $idx, $_[1]); |
|
|
60 | } unless $ignore_set{$prop}; |
|
|
61 | } |
|
|
62 | } |
|
|
63 | |
|
|
64 | # auto-generate most of the API |
|
|
65 | |
|
|
66 | prop_gen OBJECT_PROP => "cf::object"; |
|
|
67 | # CFAPI_OBJECT_ANIMATION? |
|
|
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
|
|
69 | |
|
|
70 | prop_gen MAP_PROP => "cf::map"; |
|
|
71 | prop_gen ARCH_PROP => "cf::arch"; |
|
|
72 | |
|
|
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
63 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
64 | |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
65 | # we bless all objects into (empty) derived classes to force a method lookup |
76 | # within the Safe compartment. |
66 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
67 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
… | |
… | |
87 | my @exts; |
77 | my @exts; |
88 | my @hook; |
78 | my @hook; |
89 | my %command; |
79 | my %command; |
90 | my %extcmd; |
80 | my %extcmd; |
91 | |
81 | |
92 | ############################################################################# |
82 | =head2 UTILITY FUNCTIONS |
93 | # utility functions |
83 | |
|
|
84 | =over 4 |
|
|
85 | |
|
|
86 | =cut |
94 | |
87 | |
95 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
88 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
89 | |
|
|
90 | =item $ref = cf::from_json $json |
|
|
91 | |
|
|
92 | Converts a JSON string into the corresponding perl data structure. |
|
|
93 | |
|
|
94 | =cut |
96 | |
95 | |
97 | sub from_json($) { |
96 | sub from_json($) { |
98 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
97 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
99 | JSON::Syck::Load $_[0] |
98 | JSON::Syck::Load $_[0] |
100 | } |
99 | } |
101 | |
100 | |
|
|
101 | =item $json = cf::to_json $ref |
|
|
102 | |
|
|
103 | Converts a perl data structure into its JSON representation. |
|
|
104 | |
|
|
105 | =cut |
|
|
106 | |
102 | sub to_json($) { |
107 | sub to_json($) { |
103 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
108 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
104 | JSON::Syck::Dump $_[0] |
109 | JSON::Syck::Dump $_[0] |
105 | } |
110 | } |
106 | |
111 | |
|
|
112 | =back |
|
|
113 | |
107 | ############################################################################# |
114 | ############################################################################# |
108 | # "new" plug-in system |
115 | |
|
|
116 | =head2 EVENTS AND OBJECT ATTACHMENTS |
|
|
117 | |
|
|
118 | =over 4 |
109 | |
119 | |
110 | =item $object->attach ($attachment, key => $value...) |
120 | =item $object->attach ($attachment, key => $value...) |
111 | |
121 | |
|
|
122 | =item $object->detach ($attachment) |
|
|
123 | |
112 | Attach a pre-registered attachment to an object. |
124 | Attach/detach a pre-registered attachment to an object. |
113 | |
125 | |
114 | =item $player->attach ($attachment, key => $value...) |
126 | =item $player->attach ($attachment, key => $value...) |
115 | |
127 | |
|
|
128 | =item $player->detach ($attachment) |
|
|
129 | |
116 | Attach a pre-registered attachment to a player. |
130 | Attach/detach a pre-registered attachment to a player. |
117 | |
131 | |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
132 | =item $map->attach ($attachment, key => $value...) |
119 | |
133 | |
|
|
134 | =item $map->detach ($attachment) |
|
|
135 | |
120 | Attach a pre-registered attachment to a map. |
136 | Attach/detach a pre-registered attachment to a map. |
|
|
137 | |
|
|
138 | =item $bool = $object->attached ($name) |
|
|
139 | |
|
|
140 | =item $bool = $player->attached ($name) |
|
|
141 | |
|
|
142 | =item $bool = $map->attached ($name) |
|
|
143 | |
|
|
144 | Checks wether the named attachment is currently attached to the object. |
121 | |
145 | |
122 | =item cf::attach_global ... |
146 | =item cf::attach_global ... |
123 | |
147 | |
124 | Attach handlers for global events. |
148 | Attach handlers for global events. |
125 | |
149 | |
… | |
… | |
173 | =item cf::attach_to_maps ... |
197 | =item cf::attach_to_maps ... |
174 | |
198 | |
175 | Attach handlers to all maps. |
199 | Attach handlers to all maps. |
176 | |
200 | |
177 | =item cf:register_attachment $name, ... |
201 | =item cf:register_attachment $name, ... |
|
|
202 | |
|
|
203 | Register an attachment by name through which objects can refer to this |
|
|
204 | attachment. |
|
|
205 | |
|
|
206 | =item cf:register_player_attachment $name, ... |
|
|
207 | |
|
|
208 | Register an attachment by name through which players can refer to this |
|
|
209 | attachment. |
|
|
210 | |
|
|
211 | =item cf:register_map_attachment $name, ... |
|
|
212 | |
|
|
213 | Register an attachment by name through which maps can refer to this |
|
|
214 | attachment. |
178 | |
215 | |
179 | =cut |
216 | =cut |
180 | |
217 | |
181 | # the following variables are defined in .xs and must not be re-created |
218 | # the following variables are defined in .xs and must not be re-created |
182 | our @CB_GLOBAL = (); # registry for all global events |
219 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
247 | } |
284 | } |
248 | |
285 | |
249 | sub _attach_attachment { |
286 | sub _attach_attachment { |
250 | my ($obj, $name, %arg) = @_; |
287 | my ($obj, $name, %arg) = @_; |
251 | |
288 | |
|
|
289 | return if exists $obj->{_attachment}{$name}; |
|
|
290 | |
252 | my $res; |
291 | my $res; |
253 | |
292 | |
254 | if (my $attach = $attachment{$name}) { |
293 | if (my $attach = $attachment{$name}) { |
255 | my $registry = $obj->registry; |
294 | my $registry = $obj->registry; |
256 | |
295 | |
… | |
… | |
268 | |
307 | |
269 | $res->{attachment} = $name; |
308 | $res->{attachment} = $name; |
270 | $res |
309 | $res |
271 | } |
310 | } |
272 | |
311 | |
273 | sub cf::object::attach { |
312 | *cf::object::attach = |
|
|
313 | *cf::player::attach = |
|
|
314 | *cf::map::attach = sub { |
274 | my ($obj, $name, %arg) = @_; |
315 | my ($obj, $name, %arg) = @_; |
275 | |
316 | |
276 | _attach_attachment $obj, $name, %arg; |
317 | _attach_attachment $obj, $name, %arg; |
277 | } |
318 | }; |
278 | |
319 | |
|
|
320 | # all those should be optimised |
|
|
321 | *cf::object::detach = |
279 | sub cf::player::attach { |
322 | *cf::player::detach = |
|
|
323 | *cf::map::detach = sub { |
280 | my ($obj, $name, %arg) = @_; |
324 | my ($obj, $name) = @_; |
281 | |
325 | |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
326 | delete $obj->{_attachment}{$name}; |
283 | } |
327 | reattach ($obj); |
|
|
328 | }; |
284 | |
329 | |
285 | sub cf::map::attach { |
330 | *cf::object::attached = |
|
|
331 | *cf::player::attached = |
|
|
332 | *cf::map::attached = sub { |
286 | my ($obj, $name, %arg) = @_; |
333 | my ($obj, $name) = @_; |
287 | |
334 | |
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
335 | exists $obj->{_attachment}{$name} |
289 | } |
336 | }; |
290 | |
337 | |
291 | sub attach_global { |
338 | sub attach_global { |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
339 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
293 | } |
340 | } |
294 | |
341 | |
… | |
… | |
315 | my $name = shift; |
362 | my $name = shift; |
316 | |
363 | |
317 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
364 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
318 | } |
365 | } |
319 | |
366 | |
|
|
367 | sub register_player_attachment { |
|
|
368 | my $name = shift; |
|
|
369 | |
|
|
370 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
371 | } |
|
|
372 | |
|
|
373 | sub register_map_attachment { |
|
|
374 | my $name = shift; |
|
|
375 | |
|
|
376 | $attachment{$name} = [[KLASS_MAP, @_]]; |
|
|
377 | } |
|
|
378 | |
320 | our $override; |
379 | our $override; |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
380 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
322 | |
381 | |
323 | sub override { |
382 | sub override { |
324 | $override = 1; |
383 | $override = 1; |
… | |
… | |
336 | for (@$callbacks) { |
395 | for (@$callbacks) { |
337 | eval { &{$_->[1]} }; |
396 | eval { &{$_->[1]} }; |
338 | |
397 | |
339 | if ($@) { |
398 | if ($@) { |
340 | warn "$@"; |
399 | warn "$@"; |
341 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
400 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
342 | override; |
401 | override; |
343 | } |
402 | } |
344 | |
403 | |
345 | return 1 if $override; |
404 | return 1 if $override; |
346 | } |
405 | } |
347 | |
406 | |
348 | 0 |
407 | 0 |
349 | } |
408 | } |
|
|
409 | |
|
|
410 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
|
|
411 | |
|
|
412 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
413 | |
|
|
414 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
415 | |
|
|
416 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
417 | |
|
|
418 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
419 | |
|
|
420 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
421 | removed in future versions), and there is no public API to access override |
|
|
422 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
423 | |
|
|
424 | =back |
|
|
425 | |
|
|
426 | ############################################################################# |
|
|
427 | |
|
|
428 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
|
|
429 | |
|
|
430 | =over 4 |
|
|
431 | |
|
|
432 | =item $object->valid, $player->valid, $map->valid |
|
|
433 | |
|
|
434 | Just because you have a perl object does not mean that the corresponding |
|
|
435 | C-level object still exists. If you try to access an object that has no |
|
|
436 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
437 | can be used to test for existence of the C object part without causing an |
|
|
438 | exception. |
|
|
439 | |
|
|
440 | =back |
|
|
441 | |
|
|
442 | =cut |
|
|
443 | |
|
|
444 | *cf::object::valid = |
|
|
445 | *cf::player::valid = |
|
|
446 | *cf::map::valid = \&cf::_valid; |
350 | |
447 | |
351 | ############################################################################# |
448 | ############################################################################# |
352 | # object support |
449 | # object support |
353 | |
450 | |
354 | sub instantiate { |
451 | sub instantiate { |
… | |
… | |
383 | } |
480 | } |
384 | } |
481 | } |
385 | } |
482 | } |
386 | |
483 | |
387 | sub object_freezer_save { |
484 | sub object_freezer_save { |
388 | my ($filename, $objs) = @_; |
485 | my ($filename, $rdata, $objs) = @_; |
389 | |
486 | |
390 | if (@$objs) { |
487 | if (length $$rdata) { |
|
|
488 | warn sprintf "saving %s (%d,%d)\n", |
|
|
489 | $filename, length $$rdata, scalar @$objs; |
|
|
490 | |
391 | open my $fh, ">:raw", "$filename.pst~"; |
491 | if (open my $fh, ">:raw", "$filename~") { |
|
|
492 | chmod SAVE_MODE, $fh; |
|
|
493 | syswrite $fh, $$rdata; |
|
|
494 | close $fh; |
|
|
495 | |
|
|
496 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
497 | chmod SAVE_MODE, $fh; |
392 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
498 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
393 | close $fh; |
499 | close $fh; |
394 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
395 | rename "$filename.pst~", "$filename.pst"; |
500 | rename "$filename.pst~", "$filename.pst"; |
|
|
501 | } else { |
|
|
502 | unlink "$filename.pst"; |
|
|
503 | } |
|
|
504 | |
|
|
505 | rename "$filename~", $filename; |
|
|
506 | } else { |
|
|
507 | warn "FATAL: $filename~: $!\n"; |
|
|
508 | } |
396 | } else { |
509 | } else { |
|
|
510 | unlink $filename; |
397 | unlink "$filename.pst"; |
511 | unlink "$filename.pst"; |
398 | } |
512 | } |
399 | |
|
|
400 | chmod SAVE_MODE, "$filename~"; |
|
|
401 | rename "$filename~", $filename; |
|
|
402 | } |
513 | } |
403 | |
514 | |
404 | sub object_thawer_load { |
515 | sub object_thawer_load { |
405 | my ($filename) = @_; |
516 | my ($filename) = @_; |
406 | |
517 | |
|
|
518 | local $/; |
|
|
519 | |
|
|
520 | my $av; |
|
|
521 | |
|
|
522 | #TODO: use sysread etc. |
|
|
523 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
524 | $data = <$data>; |
407 | open my $fh, "<:raw:perlio", "$filename.pst" |
525 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
408 | or return; |
526 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
527 | } |
|
|
528 | return ($data, $av); |
|
|
529 | } |
409 | |
530 | |
410 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
531 | () |
411 | } |
532 | } |
412 | |
533 | |
413 | attach_to_objects |
534 | attach_to_objects |
414 | prio => -1000000, |
535 | prio => -1000000, |
415 | on_clone => sub { |
536 | on_clone => sub { |
… | |
… | |
561 | load_extension $ext; |
682 | load_extension $ext; |
562 | 1 |
683 | 1 |
563 | } or warn "$ext not loaded: $@"; |
684 | } or warn "$ext not loaded: $@"; |
564 | } |
685 | } |
565 | } |
686 | } |
566 | |
|
|
567 | sub _perl_reload(&) { |
|
|
568 | my ($msg) = @_; |
|
|
569 | |
|
|
570 | $msg->("reloading..."); |
|
|
571 | |
|
|
572 | eval { |
|
|
573 | # 1. cancel all watchers |
|
|
574 | $_->cancel for Event::all_watchers; |
|
|
575 | |
|
|
576 | # 2. unload all extensions |
|
|
577 | for (@exts) { |
|
|
578 | $msg->("unloading <$_>"); |
|
|
579 | unload_extension $_; |
|
|
580 | } |
|
|
581 | |
|
|
582 | # 3. unload all modules loaded from $LIBDIR |
|
|
583 | while (my ($k, $v) = each %INC) { |
|
|
584 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
585 | |
|
|
586 | $msg->("removing <$k>"); |
|
|
587 | delete $INC{$k}; |
|
|
588 | |
|
|
589 | $k =~ s/\.pm$//; |
|
|
590 | $k =~ s/\//::/g; |
|
|
591 | |
|
|
592 | if (my $cb = $k->can ("unload_module")) { |
|
|
593 | $cb->(); |
|
|
594 | } |
|
|
595 | |
|
|
596 | Symbol::delete_package $k; |
|
|
597 | } |
|
|
598 | |
|
|
599 | # 4. get rid of safe::, as good as possible |
|
|
600 | Symbol::delete_package "safe::$_" |
|
|
601 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
602 | |
|
|
603 | # 5. remove register_script_function callbacks |
|
|
604 | # TODO |
|
|
605 | |
|
|
606 | # 6. unload cf.pm "a bit" |
|
|
607 | delete $INC{"cf.pm"}; |
|
|
608 | |
|
|
609 | # don't, removes xs symbols, too, |
|
|
610 | # and global variables created in xs |
|
|
611 | #Symbol::delete_package __PACKAGE__; |
|
|
612 | |
|
|
613 | # 7. reload cf.pm |
|
|
614 | $msg->("reloading cf.pm"); |
|
|
615 | require cf; |
|
|
616 | }; |
|
|
617 | $msg->($@) if $@; |
|
|
618 | |
|
|
619 | $msg->("reloaded"); |
|
|
620 | }; |
|
|
621 | |
|
|
622 | sub perl_reload() { |
|
|
623 | _perl_reload { |
|
|
624 | warn $_[0]; |
|
|
625 | print "$_[0]\n"; |
|
|
626 | }; |
|
|
627 | } |
|
|
628 | |
|
|
629 | register_command "perl-reload", 0, sub { |
|
|
630 | my ($who, $arg) = @_; |
|
|
631 | |
|
|
632 | if ($who->flag (FLAG_WIZ)) { |
|
|
633 | _perl_reload { |
|
|
634 | warn $_[0]; |
|
|
635 | $who->message ($_[0]); |
|
|
636 | }; |
|
|
637 | } |
|
|
638 | }; |
|
|
639 | |
687 | |
640 | ############################################################################# |
688 | ############################################################################# |
641 | # extcmd framework, basically convert ext <msg> |
689 | # extcmd framework, basically convert ext <msg> |
642 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
690 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
643 | |
691 | |
… | |
… | |
671 | defined $path or return; |
719 | defined $path or return; |
672 | |
720 | |
673 | unlink "$path.pst"; |
721 | unlink "$path.pst"; |
674 | }; |
722 | }; |
675 | |
723 | |
676 | # old style persistent data, TODO: remove #d# |
|
|
677 | *cf::mapsupport::on_swapin = |
|
|
678 | *cf::mapsupport::on_load = sub { |
|
|
679 | my ($map) = @_; |
|
|
680 | |
|
|
681 | my $path = $map->tmpname; |
|
|
682 | $path = $map->path unless defined $path; |
|
|
683 | |
|
|
684 | open my $fh, "<:raw", "$path.cfperl" |
|
|
685 | or return; # no perl data |
|
|
686 | |
|
|
687 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
688 | |
|
|
689 | $data->{version} <= 1 |
|
|
690 | or return; # too new |
|
|
691 | |
|
|
692 | $map->_set_obs ($data->{obs}); |
|
|
693 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
694 | }; |
|
|
695 | |
|
|
696 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
724 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
697 | |
725 | |
698 | ############################################################################# |
726 | ############################################################################# |
699 | # load/save perl data associated with player->ob objects |
727 | # load/save perl data associated with player->ob objects |
700 | |
728 | |
701 | sub all_objects(@) { |
729 | sub all_objects(@) { |
702 | @_, map all_objects ($_->inv), @_ |
730 | @_, map all_objects ($_->inv), @_ |
703 | } |
731 | } |
704 | |
732 | |
|
|
733 | # TODO: compatibility cruft, remove when no longer needed |
705 | attach_to_players |
734 | attach_to_players |
706 | on_load => sub { |
735 | on_load => sub { |
707 | my ($pl, $path) = @_; |
736 | my ($pl, $path) = @_; |
708 | |
737 | |
709 | for my $o (all_objects $pl->ob) { |
738 | for my $o (all_objects $pl->ob) { |
… | |
… | |
715 | } |
744 | } |
716 | }, |
745 | }, |
717 | ; |
746 | ; |
718 | |
747 | |
719 | ############################################################################# |
748 | ############################################################################# |
720 | # core extensions - in perl |
749 | |
|
|
750 | =head2 CORE EXTENSIONS |
|
|
751 | |
|
|
752 | Functions and methods that extend core crossfire objects. |
|
|
753 | |
|
|
754 | =over 4 |
721 | |
755 | |
722 | =item cf::player::exists $login |
756 | =item cf::player::exists $login |
723 | |
757 | |
724 | Returns true when the given account exists. |
758 | Returns true when the given account exists. |
725 | |
759 | |
… | |
… | |
765 | $msg{msgid} = $id; |
799 | $msg{msgid} = $id; |
766 | |
800 | |
767 | $self->send ("ext " . to_json \%msg); |
801 | $self->send ("ext " . to_json \%msg); |
768 | } |
802 | } |
769 | |
803 | |
|
|
804 | =back |
|
|
805 | |
|
|
806 | =cut |
|
|
807 | |
770 | ############################################################################# |
808 | ############################################################################# |
771 | # map scripting support |
809 | |
|
|
810 | =head2 SAFE SCRIPTING |
|
|
811 | |
|
|
812 | Functions that provide a safe environment to compile and execute |
|
|
813 | snippets of perl code without them endangering the safety of the server |
|
|
814 | itself. Looping constructs, I/O operators and other built-in functionality |
|
|
815 | is not available in the safe scripting environment, and the number of |
|
|
816 | functions and methods that cna be called is greatly reduced. |
|
|
817 | |
|
|
818 | =cut |
772 | |
819 | |
773 | our $safe = new Safe "safe"; |
820 | our $safe = new Safe "safe"; |
774 | our $safe_hole = new Safe::Hole; |
821 | our $safe_hole = new Safe::Hole; |
775 | |
822 | |
776 | $SIG{FPE} = 'IGNORE'; |
823 | $SIG{FPE} = 'IGNORE'; |
777 | |
824 | |
778 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
825 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
779 | |
826 | |
780 | # here we export the classes and methods available to script code |
827 | # here we export the classes and methods available to script code |
|
|
828 | |
|
|
829 | =pod |
|
|
830 | |
|
|
831 | The following fucntions and emthods are available within a safe environment: |
|
|
832 | |
|
|
833 | cf::object contr pay_amount pay_player |
|
|
834 | cf::object::player player |
|
|
835 | cf::player peaceful |
|
|
836 | |
|
|
837 | =cut |
781 | |
838 | |
782 | for ( |
839 | for ( |
783 | ["cf::object" => qw(contr pay_amount pay_player)], |
840 | ["cf::object" => qw(contr pay_amount pay_player)], |
784 | ["cf::object::player" => qw(player)], |
841 | ["cf::object::player" => qw(player)], |
785 | ["cf::player" => qw(peaceful)], |
842 | ["cf::player" => qw(peaceful)], |
… | |
… | |
788 | my ($pkg, @funs) = @$_; |
845 | my ($pkg, @funs) = @$_; |
789 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
846 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
790 | for @funs; |
847 | for @funs; |
791 | } |
848 | } |
792 | |
849 | |
|
|
850 | =over 4 |
|
|
851 | |
|
|
852 | =item @retval = safe_eval $code, [var => value, ...] |
|
|
853 | |
|
|
854 | Compiled and executes the given perl code snippet. additional var/value |
|
|
855 | pairs result in temporary local (my) scalar variables of the given name |
|
|
856 | that are available in the code snippet. Example: |
|
|
857 | |
|
|
858 | my $five = safe_eval '$first + $second', first => 1, second => 4; |
|
|
859 | |
|
|
860 | =cut |
|
|
861 | |
793 | sub safe_eval($;@) { |
862 | sub safe_eval($;@) { |
794 | my ($code, %vars) = @_; |
863 | my ($code, %vars) = @_; |
795 | |
864 | |
796 | my $qcode = $code; |
865 | my $qcode = $code; |
797 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
866 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
… | |
… | |
819 | } |
888 | } |
820 | |
889 | |
821 | wantarray ? @res : $res[0] |
890 | wantarray ? @res : $res[0] |
822 | } |
891 | } |
823 | |
892 | |
|
|
893 | =item cf::register_script_function $function => $cb |
|
|
894 | |
|
|
895 | Register a function that can be called from within map/npc scripts. The |
|
|
896 | function should be reasonably secure and should be put into a package name |
|
|
897 | like the extension. |
|
|
898 | |
|
|
899 | Example: register a function that gets called whenever a map script calls |
|
|
900 | C<rent::overview>, as used by the C<rent> extension. |
|
|
901 | |
|
|
902 | cf::register_script_function "rent::overview" => sub { |
|
|
903 | ... |
|
|
904 | }; |
|
|
905 | |
|
|
906 | =cut |
|
|
907 | |
824 | sub register_script_function { |
908 | sub register_script_function { |
825 | my ($fun, $cb) = @_; |
909 | my ($fun, $cb) = @_; |
826 | |
910 | |
827 | no strict 'refs'; |
911 | no strict 'refs'; |
828 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
912 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
829 | } |
913 | } |
830 | |
914 | |
|
|
915 | =back |
|
|
916 | |
|
|
917 | ############################################################################# |
|
|
918 | |
|
|
919 | =head2 EXTENSION DATABASE SUPPORT |
|
|
920 | |
|
|
921 | Crossfire maintains a very simple database for extension use. It can |
|
|
922 | currently store anything that can be serialised using Storable, which |
|
|
923 | excludes objects. |
|
|
924 | |
|
|
925 | The parameter C<$family> should best start with the name of the extension |
|
|
926 | using it, it should be unique. |
|
|
927 | |
|
|
928 | =over 4 |
|
|
929 | |
|
|
930 | =item $hashref = cf::db_get $family |
|
|
931 | |
|
|
932 | Return a hashref for use by the extension C<$family>, which can be |
|
|
933 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
934 | C<cf::db_sync>. |
|
|
935 | |
|
|
936 | =item $value = cf::db_get $family => $key |
|
|
937 | |
|
|
938 | Returns a single value from the database |
|
|
939 | |
|
|
940 | =item cf::db_put $family => $hashref |
|
|
941 | |
|
|
942 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
943 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
944 | |
|
|
945 | =item cf::db_put $family => $key => $value |
|
|
946 | |
|
|
947 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
|
|
948 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
949 | |
|
|
950 | =item cf::db_dirty |
|
|
951 | |
|
|
952 | Marks the database as dirty, to be updated at a later time. |
|
|
953 | |
|
|
954 | =item cf::db_sync |
|
|
955 | |
|
|
956 | Immediately write the database to disk I<if it is dirty>. |
|
|
957 | |
|
|
958 | =cut |
|
|
959 | |
|
|
960 | { |
|
|
961 | my $db; |
|
|
962 | my $path = cf::localdir . "/database.pst"; |
|
|
963 | |
|
|
964 | sub db_load() { |
|
|
965 | warn "loading database $path\n";#d# remove later |
|
|
966 | $db = stat $path ? Storable::retrieve $path : { }; |
|
|
967 | } |
|
|
968 | |
|
|
969 | my $pid; |
|
|
970 | |
|
|
971 | sub db_save() { |
|
|
972 | warn "saving database $path\n";#d# remove later |
|
|
973 | waitpid $pid, 0 if $pid; |
|
|
974 | if (0 == ($pid = fork)) { |
|
|
975 | $db->{_meta}{version} = 1; |
|
|
976 | Storable::nstore $db, "$path~"; |
|
|
977 | rename "$path~", $path; |
|
|
978 | cf::_exit 0 if defined $pid; |
|
|
979 | } |
|
|
980 | } |
|
|
981 | |
|
|
982 | my $dirty; |
|
|
983 | |
|
|
984 | sub db_sync() { |
|
|
985 | db_save if $dirty; |
|
|
986 | undef $dirty; |
|
|
987 | } |
|
|
988 | |
|
|
989 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
|
|
990 | db_sync; |
|
|
991 | }); |
|
|
992 | |
|
|
993 | sub db_dirty() { |
|
|
994 | $dirty = 1; |
|
|
995 | $idle->start; |
|
|
996 | } |
|
|
997 | |
|
|
998 | sub db_get($;$) { |
|
|
999 | @_ >= 2 |
|
|
1000 | ? $db->{$_[0]}{$_[1]} |
|
|
1001 | : ($db->{$_[0]} ||= { }) |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | sub db_put($$;$) { |
|
|
1005 | if (@_ >= 3) { |
|
|
1006 | $db->{$_[0]}{$_[1]} = $_[2]; |
|
|
1007 | } else { |
|
|
1008 | $db->{$_[0]} = $_[1]; |
|
|
1009 | } |
|
|
1010 | db_dirty; |
|
|
1011 | } |
|
|
1012 | |
|
|
1013 | attach_global |
|
|
1014 | prio => 10000, |
|
|
1015 | on_cleanup => sub { |
|
|
1016 | db_sync; |
|
|
1017 | }, |
|
|
1018 | ; |
|
|
1019 | } |
|
|
1020 | |
831 | ############################################################################# |
1021 | ############################################################################# |
832 | # the server's main() |
1022 | # the server's main() |
833 | |
1023 | |
834 | sub main { |
1024 | sub main { |
|
|
1025 | db_load; |
|
|
1026 | load_extensions; |
835 | Event::loop; |
1027 | Event::loop; |
836 | } |
1028 | } |
837 | |
1029 | |
838 | ############################################################################# |
1030 | ############################################################################# |
839 | # initialisation |
1031 | # initialisation |
840 | |
1032 | |
|
|
1033 | sub _perl_reload(&) { |
|
|
1034 | my ($msg) = @_; |
|
|
1035 | |
|
|
1036 | $msg->("reloading..."); |
|
|
1037 | |
|
|
1038 | eval { |
|
|
1039 | # cancel all watchers |
|
|
1040 | $_->cancel for Event::all_watchers; |
|
|
1041 | |
|
|
1042 | # unload all extensions |
|
|
1043 | for (@exts) { |
|
|
1044 | $msg->("unloading <$_>"); |
|
|
1045 | unload_extension $_; |
|
|
1046 | } |
|
|
1047 | |
|
|
1048 | # unload all modules loaded from $LIBDIR |
|
|
1049 | while (my ($k, $v) = each %INC) { |
|
|
1050 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
1051 | |
|
|
1052 | $msg->("removing <$k>"); |
|
|
1053 | delete $INC{$k}; |
|
|
1054 | |
|
|
1055 | $k =~ s/\.pm$//; |
|
|
1056 | $k =~ s/\//::/g; |
|
|
1057 | |
|
|
1058 | if (my $cb = $k->can ("unload_module")) { |
|
|
1059 | $cb->(); |
|
|
1060 | } |
|
|
1061 | |
|
|
1062 | Symbol::delete_package $k; |
|
|
1063 | } |
|
|
1064 | |
|
|
1065 | # sync database to disk |
|
|
1066 | cf::db_sync; |
|
|
1067 | |
|
|
1068 | # get rid of safe::, as good as possible |
|
|
1069 | Symbol::delete_package "safe::$_" |
|
|
1070 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
1071 | |
|
|
1072 | # remove register_script_function callbacks |
|
|
1073 | # TODO |
|
|
1074 | |
|
|
1075 | # unload cf.pm "a bit" |
|
|
1076 | delete $INC{"cf.pm"}; |
|
|
1077 | |
|
|
1078 | # don't, removes xs symbols, too, |
|
|
1079 | # and global variables created in xs |
|
|
1080 | #Symbol::delete_package __PACKAGE__; |
|
|
1081 | |
|
|
1082 | # reload cf.pm |
|
|
1083 | $msg->("reloading cf.pm"); |
|
|
1084 | require cf; |
|
|
1085 | |
|
|
1086 | # load database again |
|
|
1087 | cf::db_load; |
|
|
1088 | |
|
|
1089 | # load extensions |
|
|
1090 | $msg->("load extensions"); |
|
|
1091 | cf::load_extensions; |
|
|
1092 | |
|
|
1093 | # reattach attachments to objects |
|
|
1094 | $msg->("reattach"); |
|
|
1095 | _global_reattach; |
|
|
1096 | }; |
|
|
1097 | $msg->($@) if $@; |
|
|
1098 | |
|
|
1099 | $msg->("reloaded"); |
|
|
1100 | }; |
|
|
1101 | |
|
|
1102 | sub perl_reload() { |
|
|
1103 | _perl_reload { |
|
|
1104 | warn $_[0]; |
|
|
1105 | print "$_[0]\n"; |
|
|
1106 | }; |
|
|
1107 | } |
|
|
1108 | |
|
|
1109 | register_command "perl-reload", 0, sub { |
|
|
1110 | my ($who, $arg) = @_; |
|
|
1111 | |
|
|
1112 | if ($who->flag (FLAG_WIZ)) { |
|
|
1113 | _perl_reload { |
|
|
1114 | warn $_[0]; |
|
|
1115 | $who->message ($_[0]); |
|
|
1116 | }; |
|
|
1117 | } |
|
|
1118 | }; |
|
|
1119 | |
841 | register "<global>", __PACKAGE__; |
1120 | register "<global>", __PACKAGE__; |
842 | |
1121 | |
843 | unshift @INC, $LIBDIR; |
1122 | unshift @INC, $LIBDIR; |
844 | |
|
|
845 | load_extensions; |
|
|
846 | |
1123 | |
847 | $TICK_WATCHER = Event->timer ( |
1124 | $TICK_WATCHER = Event->timer ( |
848 | prio => 1, |
1125 | prio => 1, |
849 | at => $NEXT_TICK || 1, |
1126 | at => $NEXT_TICK || 1, |
850 | cb => sub { |
1127 | cb => sub { |
… | |
… | |
859 | $TICK_WATCHER->at ($NEXT_TICK); |
1136 | $TICK_WATCHER->at ($NEXT_TICK); |
860 | $TICK_WATCHER->start; |
1137 | $TICK_WATCHER->start; |
861 | }, |
1138 | }, |
862 | ); |
1139 | ); |
863 | |
1140 | |
864 | _reload_2; |
|
|
865 | |
|
|
866 | 1 |
1141 | 1 |
867 | |
1142 | |