… | |
… | |
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; |
… | |
… | |
34 | print STDERR "cfperl: $msg"; |
32 | print STDERR "cfperl: $msg"; |
35 | LOG llevError, "cfperl: $msg"; |
33 | LOG llevError, "cfperl: $msg"; |
36 | }; |
34 | }; |
37 | } |
35 | } |
38 | |
36 | |
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'; |
37 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
38 | |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
39 | # we bless all objects into (empty) derived classes to force a method lookup |
76 | # within the Safe compartment. |
40 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
41 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
78 | no strict 'refs'; |
42 | no strict 'refs'; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
43 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
80 | } |
44 | } |
81 | |
45 | |
82 | $Event::DIED = sub { |
46 | $Event::DIED = sub { |
… | |
… | |
105 | } |
69 | } |
106 | |
70 | |
107 | ############################################################################# |
71 | ############################################################################# |
108 | # "new" plug-in system |
72 | # "new" plug-in system |
109 | |
73 | |
|
|
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
75 | |
|
|
76 | =over 4 |
|
|
77 | |
|
|
78 | =item $object->attach ($attachment, key => $value...) |
|
|
79 | |
110 | =item $object->attach ($attachment, ...) |
80 | =item $object->detach ($attachment) |
111 | |
81 | |
112 | Attach a pre-registered attachment to an object. |
82 | Attach/detach a pre-registered attachment to an object. |
113 | |
83 | |
|
|
84 | =item $player->attach ($attachment, key => $value...) |
|
|
85 | |
114 | =item $player->attach ($attachment, ...) |
86 | =item $player->detach ($attachment) |
115 | |
87 | |
116 | Attach a pre-registered attachment to a player. |
88 | Attach/detach a pre-registered attachment to a player. |
117 | |
89 | |
118 | =item $map->attach ($attachment, ...) # not yet persistent |
90 | =item $map->attach ($attachment, key => $value...) |
119 | |
91 | |
|
|
92 | =item $map->detach ($attachment) |
|
|
93 | |
120 | Attach a pre-registered attachment to a map. |
94 | Attach/detach a pre-registered attachment to a map. |
|
|
95 | |
|
|
96 | =item $bool = $object->attached ($name) |
|
|
97 | |
|
|
98 | =item $bool = $player->attached ($name) |
|
|
99 | |
|
|
100 | =item $bool = $map->attached ($name) |
|
|
101 | |
|
|
102 | Checks wether the named attachment is currently attached to the object. |
121 | |
103 | |
122 | =item cf::attach_global ... |
104 | =item cf::attach_global ... |
123 | |
105 | |
124 | Attach handlers for global events. |
106 | Attach handlers for global events. |
125 | |
107 | |
… | |
… | |
173 | =item cf::attach_to_maps ... |
155 | =item cf::attach_to_maps ... |
174 | |
156 | |
175 | Attach handlers to all maps. |
157 | Attach handlers to all maps. |
176 | |
158 | |
177 | =item cf:register_attachment $name, ... |
159 | =item cf:register_attachment $name, ... |
|
|
160 | |
|
|
161 | Register an attachment by name through which objects can refer to this |
|
|
162 | attachment. |
|
|
163 | |
|
|
164 | =item cf:register_player_attachment $name, ... |
|
|
165 | |
|
|
166 | Register an attachment by name through which players can refer to this |
|
|
167 | attachment. |
|
|
168 | |
|
|
169 | =item cf:register_map_attachment $name, ... |
|
|
170 | |
|
|
171 | Register an attachment by name through which maps can refer to this |
|
|
172 | attachment. |
178 | |
173 | |
179 | =cut |
174 | =cut |
180 | |
175 | |
181 | # the following variables are defined in .xs and must not be re-created |
176 | # the following variables are defined in .xs and must not be re-created |
182 | our @CB_GLOBAL = (); # registry for all global events |
177 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
245 | |
240 | |
246 | \%undo |
241 | \%undo |
247 | } |
242 | } |
248 | |
243 | |
249 | sub _attach_attachment { |
244 | sub _attach_attachment { |
250 | my ($obj, $name, @args) = @_; |
245 | my ($obj, $name, %arg) = @_; |
|
|
246 | |
|
|
247 | return if exists $obj->{_attachment}{$name}; |
251 | |
248 | |
252 | my $res; |
249 | my $res; |
253 | |
250 | |
254 | if (my $attach = $attachment{$name}) { |
251 | if (my $attach = $attachment{$name}) { |
255 | my $registry = $obj->registry; |
252 | my $registry = $obj->registry; |
… | |
… | |
257 | for (@$attach) { |
254 | for (@$attach) { |
258 | my ($klass, @attach) = @$_; |
255 | my ($klass, @attach) = @$_; |
259 | $res = _attach @$registry, $klass, @attach; |
256 | $res = _attach @$registry, $klass, @attach; |
260 | } |
257 | } |
261 | |
258 | |
262 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
259 | $obj->{$name} = \%arg; |
263 | for (@$cb) { |
|
|
264 | eval { $_->[1]->($obj, @args); }; |
|
|
265 | if ($@) { |
|
|
266 | warn "$@"; |
|
|
267 | warn "... while processing '$name' instantiate with args <@args>.\n"; |
|
|
268 | } |
|
|
269 | } |
|
|
270 | } |
|
|
271 | } else { |
260 | } else { |
272 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
261 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
273 | } |
262 | } |
274 | |
263 | |
275 | push @{$obj->{_attachment}}, $name; |
264 | $obj->{_attachment}{$name} = undef; |
276 | |
265 | |
277 | $res->{attachment} = $name; |
266 | $res->{attachment} = $name; |
278 | $res |
267 | $res |
279 | } |
268 | } |
280 | |
269 | |
281 | sub cf::object::attach { |
270 | *cf::object::attach = |
|
|
271 | *cf::player::attach = |
|
|
272 | *cf::map::attach = sub { |
282 | my ($obj, $name, @args) = @_; |
273 | my ($obj, $name, %arg) = @_; |
283 | |
274 | |
284 | _attach_attachment $obj, $name, @args; |
275 | _attach_attachment $obj, $name, %arg; |
285 | } |
276 | }; |
286 | |
277 | |
|
|
278 | # all those should be optimised |
|
|
279 | *cf::object::detach = |
287 | sub cf::player::attach { |
280 | *cf::player::detach = |
|
|
281 | *cf::map::detach = sub { |
288 | my ($obj, $name, @args) = @_; |
282 | my ($obj, $name) = @_; |
289 | |
283 | |
290 | _attach_attachment KLASS_PLAYER, $obj, $name, @args; |
284 | delete $obj->{_attachment}{$name}; |
291 | } |
285 | reattach ($obj); |
|
|
286 | }; |
292 | |
287 | |
293 | sub cf::map::attach { |
288 | *cf::object::attached = |
|
|
289 | *cf::player::attached = |
|
|
290 | *cf::map::attached = sub { |
294 | my ($obj, $name, @args) = @_; |
291 | my ($obj, $name) = @_; |
295 | |
292 | |
296 | _attach_attachment KLASS_MAP, $obj, $name, @args; |
293 | exists $obj->{_attachment}{$name} |
297 | } |
294 | }; |
298 | |
295 | |
299 | sub attach_global { |
296 | sub attach_global { |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
297 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
301 | } |
298 | } |
302 | |
299 | |
… | |
… | |
323 | my $name = shift; |
320 | my $name = shift; |
324 | |
321 | |
325 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
322 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
326 | } |
323 | } |
327 | |
324 | |
|
|
325 | sub register_player_attachment { |
|
|
326 | my $name = shift; |
|
|
327 | |
|
|
328 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
329 | } |
|
|
330 | |
|
|
331 | sub register_map_attachment { |
|
|
332 | my $name = shift; |
|
|
333 | |
|
|
334 | $attachment{$name} = [[KLASS_MAP, @_]]; |
|
|
335 | } |
|
|
336 | |
328 | our $override; |
337 | our $override; |
329 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
338 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
330 | |
339 | |
331 | sub override { |
340 | sub override { |
332 | $override = 1; |
341 | $override = 1; |
… | |
… | |
344 | for (@$callbacks) { |
353 | for (@$callbacks) { |
345 | eval { &{$_->[1]} }; |
354 | eval { &{$_->[1]} }; |
346 | |
355 | |
347 | if ($@) { |
356 | if ($@) { |
348 | warn "$@"; |
357 | warn "$@"; |
349 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
358 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
350 | override; |
359 | override; |
351 | } |
360 | } |
352 | |
361 | |
353 | return 1 if $override; |
362 | return 1 if $override; |
354 | } |
363 | } |
355 | |
364 | |
356 | 0 |
365 | 0 |
357 | } |
366 | } |
|
|
367 | |
|
|
368 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
|
|
369 | |
|
|
370 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
371 | |
|
|
372 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
373 | |
|
|
374 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
375 | |
|
|
376 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
377 | |
|
|
378 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
379 | removed in future versions), and there is no public API to access override |
|
|
380 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
381 | |
|
|
382 | =back |
|
|
383 | |
|
|
384 | =head2 methods valid for all pointers |
|
|
385 | |
|
|
386 | =over 4 |
|
|
387 | |
|
|
388 | =item $object->valid |
|
|
389 | |
|
|
390 | =item $player->valid |
|
|
391 | |
|
|
392 | =item $map->valid |
|
|
393 | |
|
|
394 | Just because you have a perl object does not mean that the corresponding |
|
|
395 | C-level object still exists. If you try to access an object that has no |
|
|
396 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
397 | can be used to test for existence of the C object part without causing an |
|
|
398 | exception. |
|
|
399 | |
|
|
400 | =back |
|
|
401 | |
|
|
402 | =cut |
|
|
403 | |
|
|
404 | *cf::object::valid = |
|
|
405 | *cf::player::valid = |
|
|
406 | *cf::map::valid = \&cf::_valid; |
358 | |
407 | |
359 | ############################################################################# |
408 | ############################################################################# |
360 | # object support |
409 | # object support |
361 | |
410 | |
362 | sub instantiate { |
411 | sub instantiate { |
… | |
… | |
364 | |
413 | |
365 | $data = from_json $data; |
414 | $data = from_json $data; |
366 | |
415 | |
367 | for (@$data) { |
416 | for (@$data) { |
368 | my ($name, $args) = @$_; |
417 | my ($name, $args) = @$_; |
369 | attach $obj, $name, @{$args || [] }; |
418 | |
|
|
419 | $obj->attach ($name, %{$args || {} }); |
370 | } |
420 | } |
371 | } |
421 | } |
372 | |
422 | |
373 | # basically do the same as instantiate, without calling instantiate |
423 | # basically do the same as instantiate, without calling instantiate |
374 | sub reattach { |
424 | sub reattach { |
375 | my ($obj) = @_; |
425 | my ($obj) = @_; |
376 | my $registry = $obj->registry; |
426 | my $registry = $obj->registry; |
377 | |
427 | |
378 | @$registry = (); |
428 | @$registry = (); |
379 | |
429 | |
|
|
430 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
431 | |
380 | for my $name (@{ $obj->{_attachment} }) { |
432 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
381 | if (my $attach = $attachment{$name}) { |
433 | if (my $attach = $attachment{$name}) { |
382 | for (@$attach) { |
434 | for (@$attach) { |
383 | my ($klass, @attach) = @$_; |
435 | my ($klass, @attach) = @$_; |
384 | _attach @$registry, $klass, @attach; |
436 | _attach @$registry, $klass, @attach; |
385 | } |
437 | } |
… | |
… | |
388 | } |
440 | } |
389 | } |
441 | } |
390 | } |
442 | } |
391 | |
443 | |
392 | sub object_freezer_save { |
444 | sub object_freezer_save { |
393 | my ($filename, $objs) = @_; |
445 | my ($filename, $rdata, $objs) = @_; |
394 | |
446 | |
395 | $filename .= ".pst"; |
447 | if (length $$rdata) { |
|
|
448 | warn sprintf "saving %s (%d,%d)\n", |
|
|
449 | $filename, length $$rdata, scalar @$objs; |
396 | |
450 | |
397 | if (@$objs) { |
|
|
398 | open my $fh, ">:raw", "$filename~"; |
451 | if (open my $fh, ">:raw", "$filename~") { |
399 | chmod $fh, SAVE_MODE; |
452 | chmod SAVE_MODE, $fh; |
|
|
453 | syswrite $fh, $$rdata; |
|
|
454 | close $fh; |
|
|
455 | |
|
|
456 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
457 | chmod SAVE_MODE, $fh; |
400 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
458 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
401 | close $fh; |
459 | close $fh; |
|
|
460 | rename "$filename.pst~", "$filename.pst"; |
|
|
461 | } else { |
|
|
462 | unlink "$filename.pst"; |
|
|
463 | } |
|
|
464 | |
402 | rename "$filename~", $filename; |
465 | rename "$filename~", $filename; |
|
|
466 | } else { |
|
|
467 | warn "FATAL: $filename~: $!\n"; |
|
|
468 | } |
403 | } else { |
469 | } else { |
404 | unlink $filename; |
470 | unlink $filename; |
|
|
471 | unlink "$filename.pst"; |
405 | } |
472 | } |
406 | } |
473 | } |
407 | |
474 | |
408 | sub object_thawer_load { |
475 | sub object_thawer_load { |
409 | my ($filename) = @_; |
476 | my ($filename) = @_; |
410 | |
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
411 | open my $fh, "<:raw:perlio", "$filename.pst" |
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
412 | or return; |
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
413 | |
490 | |
414 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
491 | () |
415 | } |
492 | } |
416 | |
493 | |
417 | attach_to_objects |
494 | attach_to_objects |
418 | prio => -1000000, |
495 | prio => -1000000, |
419 | on_clone => sub { |
496 | on_clone => sub { |
… | |
… | |
421 | |
498 | |
422 | @{$dst->registry} = @{$src->registry}; |
499 | @{$dst->registry} = @{$src->registry}; |
423 | |
500 | |
424 | %$dst = %$src; |
501 | %$dst = %$src; |
425 | |
502 | |
426 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
503 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
427 | if exists $src->{_attachment}; |
504 | if exists $src->{_attachment}; |
428 | }, |
505 | }, |
429 | ; |
506 | ; |
430 | |
507 | |
431 | ############################################################################# |
508 | ############################################################################# |
… | |
… | |
565 | load_extension $ext; |
642 | load_extension $ext; |
566 | 1 |
643 | 1 |
567 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
568 | } |
645 | } |
569 | } |
646 | } |
570 | |
|
|
571 | sub _perl_reload(&) { |
|
|
572 | my ($msg) = @_; |
|
|
573 | |
|
|
574 | $msg->("reloading..."); |
|
|
575 | |
|
|
576 | eval { |
|
|
577 | # 1. cancel all watchers |
|
|
578 | $_->cancel for Event::all_watchers; |
|
|
579 | |
|
|
580 | # 2. unload all extensions |
|
|
581 | for (@exts) { |
|
|
582 | $msg->("unloading <$_>"); |
|
|
583 | unload_extension $_; |
|
|
584 | } |
|
|
585 | |
|
|
586 | # 3. unload all modules loaded from $LIBDIR |
|
|
587 | while (my ($k, $v) = each %INC) { |
|
|
588 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
589 | |
|
|
590 | $msg->("removing <$k>"); |
|
|
591 | delete $INC{$k}; |
|
|
592 | |
|
|
593 | $k =~ s/\.pm$//; |
|
|
594 | $k =~ s/\//::/g; |
|
|
595 | |
|
|
596 | if (my $cb = $k->can ("unload_module")) { |
|
|
597 | $cb->(); |
|
|
598 | } |
|
|
599 | |
|
|
600 | Symbol::delete_package $k; |
|
|
601 | } |
|
|
602 | |
|
|
603 | # 4. get rid of safe::, as good as possible |
|
|
604 | Symbol::delete_package "safe::$_" |
|
|
605 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
606 | |
|
|
607 | # 5. remove register_script_function callbacks |
|
|
608 | # TODO |
|
|
609 | |
|
|
610 | # 6. unload cf.pm "a bit" |
|
|
611 | delete $INC{"cf.pm"}; |
|
|
612 | |
|
|
613 | # don't, removes xs symbols, too, |
|
|
614 | # and global variables created in xs |
|
|
615 | #Symbol::delete_package __PACKAGE__; |
|
|
616 | |
|
|
617 | # 7. reload cf.pm |
|
|
618 | $msg->("reloading cf.pm"); |
|
|
619 | require cf; |
|
|
620 | }; |
|
|
621 | $msg->($@) if $@; |
|
|
622 | |
|
|
623 | $msg->("reloaded"); |
|
|
624 | }; |
|
|
625 | |
|
|
626 | sub perl_reload() { |
|
|
627 | _perl_reload { |
|
|
628 | warn $_[0]; |
|
|
629 | print "$_[0]\n"; |
|
|
630 | }; |
|
|
631 | } |
|
|
632 | |
|
|
633 | register_command "perl-reload", 0, sub { |
|
|
634 | my ($who, $arg) = @_; |
|
|
635 | |
|
|
636 | if ($who->flag (FLAG_WIZ)) { |
|
|
637 | _perl_reload { |
|
|
638 | warn $_[0]; |
|
|
639 | $who->message ($_[0]); |
|
|
640 | }; |
|
|
641 | } |
|
|
642 | }; |
|
|
643 | |
647 | |
644 | ############################################################################# |
648 | ############################################################################# |
645 | # extcmd framework, basically convert ext <msg> |
649 | # extcmd framework, basically convert ext <msg> |
646 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
647 | |
651 | |
… | |
… | |
672 | my ($map) = @_; |
676 | my ($map) = @_; |
673 | |
677 | |
674 | my $path = $map->tmpname; |
678 | my $path = $map->tmpname; |
675 | defined $path or return; |
679 | defined $path or return; |
676 | |
680 | |
677 | unlink "$path.cfperl"; |
|
|
678 | unlink "$path.pst"; |
681 | unlink "$path.pst"; |
679 | }; |
682 | }; |
680 | |
683 | |
681 | *cf::mapsupport::on_swapin = |
|
|
682 | *cf::mapsupport::on_load = sub { |
|
|
683 | my ($map) = @_; |
|
|
684 | |
|
|
685 | my $path = $map->tmpname; |
|
|
686 | $path = $map->path unless defined $path; |
|
|
687 | |
|
|
688 | open my $fh, "<:raw", "$path.cfperl" |
|
|
689 | or return; # no perl data |
|
|
690 | |
|
|
691 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
692 | |
|
|
693 | $data->{version} <= 1 |
|
|
694 | or return; # too new |
|
|
695 | |
|
|
696 | $map->_set_obs ($data->{obs}); |
|
|
697 | }; |
|
|
698 | |
|
|
699 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
684 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
700 | |
685 | |
701 | ############################################################################# |
686 | ############################################################################# |
702 | # load/save perl data associated with player->ob objects |
687 | # load/save perl data associated with player->ob objects |
703 | |
688 | |
704 | sub all_objects(@) { |
689 | sub all_objects(@) { |
705 | @_, map all_objects ($_->inv), @_ |
690 | @_, map all_objects ($_->inv), @_ |
706 | } |
691 | } |
707 | |
692 | |
|
|
693 | # TODO: compatibility cruft, remove when no longer needed |
708 | attach_to_players |
694 | attach_to_players |
709 | on_load => sub { |
695 | on_load => sub { |
710 | my ($pl, $path) = @_; |
696 | my ($pl, $path) = @_; |
711 | |
697 | |
712 | for my $o (all_objects $pl->ob) { |
698 | for my $o (all_objects $pl->ob) { |
… | |
… | |
830 | no strict 'refs'; |
816 | no strict 'refs'; |
831 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
832 | } |
818 | } |
833 | |
819 | |
834 | ############################################################################# |
820 | ############################################################################# |
|
|
821 | |
|
|
822 | =head2 EXTENSION DATABASE SUPPORT |
|
|
823 | |
|
|
824 | Crossfire maintains a very simple database for extension use. It can |
|
|
825 | currently store anything that can be serialised using Storable, which |
|
|
826 | excludes objects. |
|
|
827 | |
|
|
828 | The parameter C<$family> should best start with the name of the extension |
|
|
829 | using it, it should be unique. |
|
|
830 | |
|
|
831 | =over 4 |
|
|
832 | |
|
|
833 | =item $hashref = cf::db_get $family |
|
|
834 | |
|
|
835 | Return a hashref for use by the extension C<$family>, which can be |
|
|
836 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
837 | C<cf::db_sync>. |
|
|
838 | |
|
|
839 | =item $value = cf::db_get $family => $key |
|
|
840 | |
|
|
841 | Returns a single value from the database |
|
|
842 | |
|
|
843 | =item cf::db_put $family => $hashref |
|
|
844 | |
|
|
845 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
846 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
847 | |
|
|
848 | =item cf::db_put $family => $key => $value |
|
|
849 | |
|
|
850 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
|
|
851 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
852 | |
|
|
853 | =item cf::db_dirty |
|
|
854 | |
|
|
855 | Marks the database as dirty, to be updated at a later time. |
|
|
856 | |
|
|
857 | =item cf::db_sync |
|
|
858 | |
|
|
859 | Immediately write the database to disk I<if it is dirty>. |
|
|
860 | |
|
|
861 | =cut |
|
|
862 | |
|
|
863 | { |
|
|
864 | my $db; |
|
|
865 | my $path = cf::datadir . "/database.pst"; |
|
|
866 | |
|
|
867 | sub db_load() { |
|
|
868 | warn "loading database $path\n";#d# remove later |
|
|
869 | $db = stat $path ? Storable::retrieve $path : { }; |
|
|
870 | } |
|
|
871 | |
|
|
872 | my $pid; |
|
|
873 | |
|
|
874 | sub db_save() { |
|
|
875 | warn "saving database $path\n";#d# remove later |
|
|
876 | waitpid $pid, 0 if $pid; |
|
|
877 | unless ($pid = fork) { |
|
|
878 | $db->{_meta}{version} = 1; |
|
|
879 | Storable::nstore $db, "$path~"; |
|
|
880 | rename "$path~", $path; |
|
|
881 | cf::_exit 0 if defined $pid; |
|
|
882 | } |
|
|
883 | } |
|
|
884 | |
|
|
885 | my $dirty; |
|
|
886 | |
|
|
887 | sub db_sync() { |
|
|
888 | db_save if $dirty; |
|
|
889 | undef $dirty; |
|
|
890 | } |
|
|
891 | |
|
|
892 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
|
|
893 | db_sync; |
|
|
894 | }); |
|
|
895 | |
|
|
896 | sub db_dirty() { |
|
|
897 | $dirty = 1; |
|
|
898 | $idle->start; |
|
|
899 | } |
|
|
900 | |
|
|
901 | sub db_get($;$) { |
|
|
902 | @_ >= 2 |
|
|
903 | ? $db->{$_[0]}{$_[1]} |
|
|
904 | : ($db->{$_[0]} ||= { }) |
|
|
905 | } |
|
|
906 | |
|
|
907 | sub db_put($$;$) { |
|
|
908 | if (@_ >= 3) { |
|
|
909 | $db->{$_[0]}{$_[1]} = $_[2]; |
|
|
910 | } else { |
|
|
911 | $db->{$_[0]} = $_[1]; |
|
|
912 | } |
|
|
913 | db_dirty; |
|
|
914 | } |
|
|
915 | } |
|
|
916 | |
|
|
917 | ############################################################################# |
835 | # the server's main() |
918 | # the server's main() |
836 | |
919 | |
837 | sub main { |
920 | sub main { |
|
|
921 | db_load; |
|
|
922 | load_extensions; |
838 | Event::loop; |
923 | Event::loop; |
839 | } |
924 | } |
840 | |
925 | |
841 | ############################################################################# |
926 | ############################################################################# |
842 | # initialisation |
927 | # initialisation |
843 | |
928 | |
|
|
929 | sub _perl_reload(&) { |
|
|
930 | my ($msg) = @_; |
|
|
931 | |
|
|
932 | $msg->("reloading..."); |
|
|
933 | |
|
|
934 | eval { |
|
|
935 | # cancel all watchers |
|
|
936 | $_->cancel for Event::all_watchers; |
|
|
937 | |
|
|
938 | # unload all extensions |
|
|
939 | for (@exts) { |
|
|
940 | $msg->("unloading <$_>"); |
|
|
941 | unload_extension $_; |
|
|
942 | } |
|
|
943 | |
|
|
944 | # unload all modules loaded from $LIBDIR |
|
|
945 | while (my ($k, $v) = each %INC) { |
|
|
946 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
947 | |
|
|
948 | $msg->("removing <$k>"); |
|
|
949 | delete $INC{$k}; |
|
|
950 | |
|
|
951 | $k =~ s/\.pm$//; |
|
|
952 | $k =~ s/\//::/g; |
|
|
953 | |
|
|
954 | if (my $cb = $k->can ("unload_module")) { |
|
|
955 | $cb->(); |
|
|
956 | } |
|
|
957 | |
|
|
958 | Symbol::delete_package $k; |
|
|
959 | } |
|
|
960 | |
|
|
961 | # sync database to disk |
|
|
962 | cf::db_sync; |
|
|
963 | |
|
|
964 | # get rid of safe::, as good as possible |
|
|
965 | Symbol::delete_package "safe::$_" |
|
|
966 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
967 | |
|
|
968 | # remove register_script_function callbacks |
|
|
969 | # TODO |
|
|
970 | |
|
|
971 | # unload cf.pm "a bit" |
|
|
972 | delete $INC{"cf.pm"}; |
|
|
973 | |
|
|
974 | # don't, removes xs symbols, too, |
|
|
975 | # and global variables created in xs |
|
|
976 | #Symbol::delete_package __PACKAGE__; |
|
|
977 | |
|
|
978 | # reload cf.pm |
|
|
979 | $msg->("reloading cf.pm"); |
|
|
980 | require cf; |
|
|
981 | |
|
|
982 | # load database again |
|
|
983 | cf::db_load; |
|
|
984 | |
|
|
985 | # load extensions |
|
|
986 | $msg->("load extensions"); |
|
|
987 | cf::load_extensions; |
|
|
988 | |
|
|
989 | # reattach attachments to objects |
|
|
990 | $msg->("reattach"); |
|
|
991 | _global_reattach; |
|
|
992 | }; |
|
|
993 | $msg->($@) if $@; |
|
|
994 | |
|
|
995 | $msg->("reloaded"); |
|
|
996 | }; |
|
|
997 | |
|
|
998 | sub perl_reload() { |
|
|
999 | _perl_reload { |
|
|
1000 | warn $_[0]; |
|
|
1001 | print "$_[0]\n"; |
|
|
1002 | }; |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | register_command "perl-reload", 0, sub { |
|
|
1006 | my ($who, $arg) = @_; |
|
|
1007 | |
|
|
1008 | if ($who->flag (FLAG_WIZ)) { |
|
|
1009 | _perl_reload { |
|
|
1010 | warn $_[0]; |
|
|
1011 | $who->message ($_[0]); |
|
|
1012 | }; |
|
|
1013 | } |
|
|
1014 | }; |
|
|
1015 | |
844 | register "<global>", __PACKAGE__; |
1016 | register "<global>", __PACKAGE__; |
845 | |
1017 | |
846 | unshift @INC, $LIBDIR; |
1018 | unshift @INC, $LIBDIR; |
847 | |
|
|
848 | load_extensions; |
|
|
849 | |
1019 | |
850 | $TICK_WATCHER = Event->timer ( |
1020 | $TICK_WATCHER = Event->timer ( |
851 | prio => 1, |
1021 | prio => 1, |
852 | at => $NEXT_TICK || 1, |
1022 | at => $NEXT_TICK || 1, |
853 | cb => sub { |
1023 | cb => sub { |
… | |
… | |
862 | $TICK_WATCHER->at ($NEXT_TICK); |
1032 | $TICK_WATCHER->at ($NEXT_TICK); |
863 | $TICK_WATCHER->start; |
1033 | $TICK_WATCHER->start; |
864 | }, |
1034 | }, |
865 | ); |
1035 | ); |
866 | |
1036 | |
867 | _reload_2; |
|
|
868 | |
|
|
869 | 1 |
1037 | 1 |
870 | |
1038 | |