1 | package cf; |
1 | package cf; |
2 | |
2 | |
3 | use Symbol; |
3 | use Symbol; |
4 | use List::Util; |
4 | use List::Util; |
5 | use Storable; |
5 | use Storable; |
|
|
6 | use Opcode; |
|
|
7 | use Safe; |
|
|
8 | use Safe::Hole; |
6 | |
9 | |
|
|
10 | use Time::HiRes; |
7 | use Event; |
11 | use Event; |
8 | $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 |
9 | |
13 | |
10 | use strict; |
14 | use strict; |
11 | |
15 | |
|
|
16 | _init_vars; |
|
|
17 | |
12 | our %COMMAND; |
18 | our %COMMAND = (); |
13 | our @EVENT; |
19 | our @EVENT; |
14 | our %PROP_TYPE; |
20 | our $LIBDIR = maps_directory "perl"; |
15 | our %PROP_IDX; |
21 | |
|
|
22 | our $TICK = MAX_TIME * 1e-6; |
|
|
23 | our $TICK_WATCHER; |
|
|
24 | our $NEXT_TICK; |
16 | |
25 | |
17 | BEGIN { |
26 | BEGIN { |
18 | @EVENT = map lc, @EVENT; |
|
|
19 | |
|
|
20 | *CORE::GLOBAL::warn = sub { |
27 | *CORE::GLOBAL::warn = sub { |
21 | my $msg = join "", @_; |
28 | my $msg = join "", @_; |
22 | $msg .= "\n" |
29 | $msg .= "\n" |
23 | unless $msg =~ /\n$/; |
30 | unless $msg =~ /\n$/; |
24 | |
31 | |
25 | print STDERR "cfperl: $msg"; |
32 | print STDERR "cfperl: $msg"; |
26 | LOG llevError, "cfperl: $msg"; |
33 | LOG llevError, "cfperl: $msg"; |
27 | }; |
34 | }; |
28 | } |
35 | } |
29 | |
36 | |
30 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
37 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
31 | |
38 | |
32 | # generate property mutators |
39 | # we bless all objects into (empty) derived classes to force a method lookup |
33 | sub prop_gen { |
40 | # within the Safe compartment. |
34 | my ($prefix, $class) = @_; |
41 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
35 | |
|
|
36 | no strict 'refs'; |
42 | no strict 'refs'; |
37 | |
43 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
38 | for my $prop (keys %PROP_TYPE) { |
|
|
39 | $prop =~ /^\Q$prefix\E_(.*$)/ or next; |
|
|
40 | my $sub = lc $1; |
|
|
41 | |
|
|
42 | my $type = $PROP_TYPE{$prop}; |
|
|
43 | my $idx = $PROP_IDX {$prop}; |
|
|
44 | |
|
|
45 | *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { |
|
|
46 | $_[0]->get_property ($type, $idx) |
|
|
47 | }; |
|
|
48 | |
|
|
49 | *{"$class\::set_$sub"} = sub { |
|
|
50 | $_[0]->set_property ($type, $idx, $_[1]); |
|
|
51 | } unless $ignore_set{$prop}; |
|
|
52 | } |
|
|
53 | } |
44 | } |
54 | |
|
|
55 | # auto-generate most of the API |
|
|
56 | |
|
|
57 | prop_gen OBJECT_PROP => "cf::object"; |
|
|
58 | # CFAPI_OBJECT_ANIMATION? |
|
|
59 | prop_gen PLAYER_PROP => "cf::object::player"; |
|
|
60 | |
|
|
61 | prop_gen MAP_PROP => "cf::map"; |
|
|
62 | prop_gen ARCH_PROP => "cf::arch"; |
|
|
63 | |
|
|
64 | # guessed hierarchies |
|
|
65 | |
|
|
66 | @cf::object::player::ISA = 'cf::object'; |
|
|
67 | @cf::object::map::ISA = 'cf::object'; |
|
|
68 | |
45 | |
69 | $Event::DIED = sub { |
46 | $Event::DIED = sub { |
70 | warn "error in event callback: @_"; |
47 | warn "error in event callback: @_"; |
71 | }; |
48 | }; |
72 | |
49 | |
… | |
… | |
74 | my @exts; |
51 | my @exts; |
75 | my @hook; |
52 | my @hook; |
76 | my %command; |
53 | my %command; |
77 | my %extcmd; |
54 | my %extcmd; |
78 | |
55 | |
|
|
56 | ############################################################################# |
|
|
57 | # utility functions |
|
|
58 | |
|
|
59 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
60 | |
|
|
61 | sub from_json($) { |
|
|
62 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
63 | JSON::Syck::Load $_[0] |
|
|
64 | } |
|
|
65 | |
|
|
66 | sub to_json($) { |
|
|
67 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
68 | JSON::Syck::Dump $_[0] |
|
|
69 | } |
|
|
70 | |
|
|
71 | ############################################################################# |
|
|
72 | # "new" plug-in system |
|
|
73 | |
|
|
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
75 | |
|
|
76 | =over 4 |
|
|
77 | |
|
|
78 | =item $object->attach ($attachment, key => $value...) |
|
|
79 | |
|
|
80 | =item $object->detach ($attachment) |
|
|
81 | |
|
|
82 | Attach/detach a pre-registered attachment to an object. |
|
|
83 | |
|
|
84 | =item $player->attach ($attachment, key => $value...) |
|
|
85 | |
|
|
86 | =item $player->detach ($attachment) |
|
|
87 | |
|
|
88 | Attach/detach a pre-registered attachment to a player. |
|
|
89 | |
|
|
90 | =item $map->attach ($attachment, key => $value...) |
|
|
91 | |
|
|
92 | =item $map->detach ($attachment) |
|
|
93 | |
|
|
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. |
|
|
103 | |
|
|
104 | =item cf::attach_global ... |
|
|
105 | |
|
|
106 | Attach handlers for global events. |
|
|
107 | |
|
|
108 | This and all following C<attach_*>-functions expect any number of the |
|
|
109 | following handler/hook descriptions: |
|
|
110 | |
|
|
111 | =over 4 |
|
|
112 | |
|
|
113 | =item prio => $number |
|
|
114 | |
|
|
115 | Set the priority for all following handlers/hooks (unless overwritten |
|
|
116 | by another C<prio> setting). Lower priority handlers get executed |
|
|
117 | earlier. The default priority is C<0>, and many built-in handlers are |
|
|
118 | registered at priority C<-1000>, so lower priorities should not be used |
|
|
119 | unless you know what you are doing. |
|
|
120 | |
|
|
121 | =item on_I<event> => \&cb |
|
|
122 | |
|
|
123 | Call the given code reference whenever the named event happens (event is |
|
|
124 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
|
|
125 | handlers are recognised generally depends on the type of object these |
|
|
126 | handlers attach to). |
|
|
127 | |
|
|
128 | See F<include/eventinc.h> for the full list of events supported, and their |
|
|
129 | class. |
|
|
130 | |
|
|
131 | =item package => package:: |
|
|
132 | |
|
|
133 | Look for sub functions of the name C<< on_I<event> >> in the given |
|
|
134 | package and register them. Only handlers for eevents supported by the |
|
|
135 | object/class are recognised. |
|
|
136 | |
|
|
137 | =back |
|
|
138 | |
|
|
139 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
140 | |
|
|
141 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
142 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
143 | type. |
|
|
144 | |
|
|
145 | =item cf::attach_to_objects ... |
|
|
146 | |
|
|
147 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
148 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
149 | the game. |
|
|
150 | |
|
|
151 | =item cf::attach_to_players ... |
|
|
152 | |
|
|
153 | Attach handlers to all players. |
|
|
154 | |
|
|
155 | =item cf::attach_to_maps ... |
|
|
156 | |
|
|
157 | Attach handlers to all maps. |
|
|
158 | |
|
|
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. |
|
|
173 | |
|
|
174 | =cut |
|
|
175 | |
|
|
176 | # the following variables are defined in .xs and must not be re-created |
|
|
177 | our @CB_GLOBAL = (); # registry for all global events |
|
|
178 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
|
|
179 | our @CB_PLAYER = (); |
|
|
180 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
181 | our @CB_MAP = (); |
|
|
182 | |
|
|
183 | my %attachment; |
|
|
184 | |
|
|
185 | sub _attach_cb($\%$$$) { |
|
|
186 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
187 | |
|
|
188 | use sort 'stable'; |
|
|
189 | |
|
|
190 | $cb = [$prio, $cb]; |
|
|
191 | |
|
|
192 | @{$registry->[$event]} = sort |
|
|
193 | { $a->[0] cmp $b->[0] } |
|
|
194 | @{$registry->[$event] || []}, $cb; |
|
|
195 | |
|
|
196 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
197 | } |
|
|
198 | |
|
|
199 | # attach handles attaching event callbacks |
|
|
200 | # the only thing the caller has to do is pass the correct |
|
|
201 | # registry (== where the callback attaches to). |
|
|
202 | sub _attach(\@$@) { |
|
|
203 | my ($registry, $klass, @arg) = @_; |
|
|
204 | |
|
|
205 | my $prio = 0; |
|
|
206 | |
|
|
207 | my %undo = ( |
|
|
208 | registry => $registry, |
|
|
209 | cb => [], |
|
|
210 | ); |
|
|
211 | |
|
|
212 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
213 | |
|
|
214 | while (@arg) { |
|
|
215 | my $type = shift @arg; |
|
|
216 | |
|
|
217 | if ($type eq "prio") { |
|
|
218 | $prio = shift @arg; |
|
|
219 | |
|
|
220 | } elsif ($type eq "package") { |
|
|
221 | my $pkg = shift @arg; |
|
|
222 | |
|
|
223 | while (my ($name, $id) = each %cb_id) { |
|
|
224 | if (my $cb = $pkg->can ($name)) { |
|
|
225 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
226 | } |
|
|
227 | } |
|
|
228 | |
|
|
229 | } elsif (exists $cb_id{$type}) { |
|
|
230 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
|
|
231 | |
|
|
232 | } elsif (ref $type) { |
|
|
233 | warn "attaching objects not supported, ignoring.\n"; |
|
|
234 | |
|
|
235 | } else { |
|
|
236 | shift @arg; |
|
|
237 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
238 | } |
|
|
239 | } |
|
|
240 | |
|
|
241 | \%undo |
|
|
242 | } |
|
|
243 | |
|
|
244 | sub _attach_attachment { |
|
|
245 | my ($obj, $name, %arg) = @_; |
|
|
246 | |
|
|
247 | return if exists $obj->{_attachment}{$name}; |
|
|
248 | |
|
|
249 | my $res; |
|
|
250 | |
|
|
251 | if (my $attach = $attachment{$name}) { |
|
|
252 | my $registry = $obj->registry; |
|
|
253 | |
|
|
254 | for (@$attach) { |
|
|
255 | my ($klass, @attach) = @$_; |
|
|
256 | $res = _attach @$registry, $klass, @attach; |
|
|
257 | } |
|
|
258 | |
|
|
259 | $obj->{$name} = \%arg; |
|
|
260 | } else { |
|
|
261 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
262 | } |
|
|
263 | |
|
|
264 | $obj->{_attachment}{$name} = undef; |
|
|
265 | |
|
|
266 | $res->{attachment} = $name; |
|
|
267 | $res |
|
|
268 | } |
|
|
269 | |
|
|
270 | *cf::object::attach = |
|
|
271 | *cf::player::attach = |
|
|
272 | *cf::map::attach = sub { |
|
|
273 | my ($obj, $name, %arg) = @_; |
|
|
274 | |
|
|
275 | _attach_attachment $obj, $name, %arg; |
|
|
276 | }; |
|
|
277 | |
|
|
278 | # all those should be optimised |
|
|
279 | *cf::object::detach = |
|
|
280 | *cf::player::detach = |
|
|
281 | *cf::map::detach = sub { |
|
|
282 | my ($obj, $name) = @_; |
|
|
283 | |
|
|
284 | delete $obj->{_attachment}{$name}; |
|
|
285 | reattach ($obj); |
|
|
286 | }; |
|
|
287 | |
|
|
288 | *cf::object::attached = |
|
|
289 | *cf::player::attached = |
|
|
290 | *cf::map::attached = sub { |
|
|
291 | my ($obj, $name) = @_; |
|
|
292 | |
|
|
293 | exists $obj->{_attachment}{$name} |
|
|
294 | }; |
|
|
295 | |
|
|
296 | sub attach_global { |
|
|
297 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
298 | } |
|
|
299 | |
|
|
300 | sub attach_to_type { |
|
|
301 | my $type = shift; |
|
|
302 | my $subtype = shift; |
|
|
303 | |
|
|
304 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub attach_to_objects { |
|
|
308 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
309 | } |
|
|
310 | |
|
|
311 | sub attach_to_players { |
|
|
312 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
313 | } |
|
|
314 | |
|
|
315 | sub attach_to_maps { |
|
|
316 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
317 | } |
|
|
318 | |
|
|
319 | sub register_attachment { |
|
|
320 | my $name = shift; |
|
|
321 | |
|
|
322 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
323 | } |
|
|
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 | |
|
|
337 | our $override; |
|
|
338 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
|
|
339 | |
|
|
340 | sub override { |
|
|
341 | $override = 1; |
|
|
342 | @invoke_results = (); |
|
|
343 | } |
|
|
344 | |
|
|
345 | sub do_invoke { |
|
|
346 | my $event = shift; |
|
|
347 | my $callbacks = shift; |
|
|
348 | |
|
|
349 | @invoke_results = (); |
|
|
350 | |
|
|
351 | local $override; |
|
|
352 | |
|
|
353 | for (@$callbacks) { |
|
|
354 | eval { &{$_->[1]} }; |
|
|
355 | |
|
|
356 | if ($@) { |
|
|
357 | warn "$@"; |
|
|
358 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
|
|
359 | override; |
|
|
360 | } |
|
|
361 | |
|
|
362 | return 1 if $override; |
|
|
363 | } |
|
|
364 | |
|
|
365 | 0 |
|
|
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; |
|
|
407 | |
|
|
408 | ############################################################################# |
|
|
409 | # object support |
|
|
410 | |
|
|
411 | sub instantiate { |
|
|
412 | my ($obj, $data) = @_; |
|
|
413 | |
|
|
414 | $data = from_json $data; |
|
|
415 | |
|
|
416 | for (@$data) { |
|
|
417 | my ($name, $args) = @$_; |
|
|
418 | |
|
|
419 | $obj->attach ($name, %{$args || {} }); |
|
|
420 | } |
|
|
421 | } |
|
|
422 | |
|
|
423 | # basically do the same as instantiate, without calling instantiate |
|
|
424 | sub reattach { |
|
|
425 | my ($obj) = @_; |
|
|
426 | my $registry = $obj->registry; |
|
|
427 | |
|
|
428 | @$registry = (); |
|
|
429 | |
|
|
430 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
431 | |
|
|
432 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
|
|
433 | if (my $attach = $attachment{$name}) { |
|
|
434 | for (@$attach) { |
|
|
435 | my ($klass, @attach) = @$_; |
|
|
436 | _attach @$registry, $klass, @attach; |
|
|
437 | } |
|
|
438 | } else { |
|
|
439 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
440 | } |
|
|
441 | } |
|
|
442 | } |
|
|
443 | |
|
|
444 | sub object_freezer_save { |
|
|
445 | my ($filename, $rdata, $objs) = @_; |
|
|
446 | |
|
|
447 | if (length $$rdata) { |
|
|
448 | warn sprintf "saving %s (%d,%d)\n", |
|
|
449 | $filename, length $$rdata, scalar @$objs; |
|
|
450 | |
|
|
451 | if (open my $fh, ">:raw", "$filename~") { |
|
|
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; |
|
|
458 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
459 | close $fh; |
|
|
460 | rename "$filename.pst~", "$filename.pst"; |
|
|
461 | } else { |
|
|
462 | unlink "$filename.pst"; |
|
|
463 | } |
|
|
464 | |
|
|
465 | rename "$filename~", $filename; |
|
|
466 | } else { |
|
|
467 | warn "FATAL: $filename~: $!\n"; |
|
|
468 | } |
|
|
469 | } else { |
|
|
470 | unlink $filename; |
|
|
471 | unlink "$filename.pst"; |
|
|
472 | } |
|
|
473 | } |
|
|
474 | |
|
|
475 | sub object_thawer_load { |
|
|
476 | my ($filename) = @_; |
|
|
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
|
|
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
|
|
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
|
|
490 | |
|
|
491 | () |
|
|
492 | } |
|
|
493 | |
|
|
494 | attach_to_objects |
|
|
495 | prio => -1000000, |
|
|
496 | on_clone => sub { |
|
|
497 | my ($src, $dst) = @_; |
|
|
498 | |
|
|
499 | @{$dst->registry} = @{$src->registry}; |
|
|
500 | |
|
|
501 | %$dst = %$src; |
|
|
502 | |
|
|
503 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
504 | if exists $src->{_attachment}; |
|
|
505 | }, |
|
|
506 | ; |
|
|
507 | |
|
|
508 | ############################################################################# |
|
|
509 | # old plug-in events |
|
|
510 | |
79 | sub inject_event { |
511 | sub inject_event { |
80 | my $extension = shift; |
512 | my $extension = shift; |
81 | my $event_code = shift; |
513 | my $event_code = shift; |
82 | |
514 | |
83 | my $cb = $hook[$event_code]{$extension} |
515 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
125 | } |
557 | } |
126 | |
558 | |
127 | sub register { |
559 | sub register { |
128 | my ($base, $pkg) = @_; |
560 | my ($base, $pkg) = @_; |
129 | |
561 | |
130 | for my $idx (0 .. $#EVENT) { |
562 | #TODO |
131 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
|
|
132 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
|
|
133 | $hook[$idx]{$base} = $ref; |
|
|
134 | } |
|
|
135 | } |
|
|
136 | } |
563 | } |
137 | |
564 | |
138 | sub load_extension { |
565 | sub load_extension { |
139 | my ($path) = @_; |
566 | my ($path) = @_; |
140 | |
567 | |
141 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
568 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
142 | my $base = $1; |
569 | my $base = $1; |
143 | my $pkg = $1; |
570 | my $pkg = $1; |
144 | $pkg =~ s/[^[:word:]]/_/g; |
571 | $pkg =~ s/[^[:word:]]/_/g; |
145 | $pkg = "cf::ext::$pkg"; |
572 | $pkg = "ext::$pkg"; |
146 | |
573 | |
147 | warn "loading '$path' into '$pkg'\n"; |
574 | warn "loading '$path' into '$pkg'\n"; |
148 | |
575 | |
149 | open my $fh, "<:utf8", $path |
576 | open my $fh, "<:utf8", $path |
150 | or die "$path: $!"; |
577 | or die "$path: $!"; |
… | |
… | |
160 | |
587 | |
161 | push @exts, $pkg; |
588 | push @exts, $pkg; |
162 | $ext_pkg{$base} = $pkg; |
589 | $ext_pkg{$base} = $pkg; |
163 | |
590 | |
164 | # no strict 'refs'; |
591 | # no strict 'refs'; |
165 | # @{"$pkg\::ISA"} = cf::ext::; |
592 | # @{"$pkg\::ISA"} = ext::; |
166 | |
593 | |
167 | register $base, $pkg; |
594 | register $base, $pkg; |
168 | } |
595 | } |
169 | |
596 | |
170 | sub unload_extension { |
597 | sub unload_extension { |
171 | my ($pkg) = @_; |
598 | my ($pkg) = @_; |
172 | |
599 | |
173 | warn "removing extension $pkg\n"; |
600 | warn "removing extension $pkg\n"; |
174 | |
601 | |
175 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
176 | $cb->($pkg); |
|
|
177 | } |
|
|
178 | |
|
|
179 | # remove hooks |
602 | # remove hooks |
|
|
603 | #TODO |
180 | for my $idx (0 .. $#EVENT) { |
604 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
181 | delete $hook[$idx]{$pkg}; |
605 | # delete $hook[$idx]{$pkg}; |
182 | } |
606 | # } |
183 | |
607 | |
184 | # remove commands |
608 | # remove commands |
185 | for my $name (keys %command) { |
609 | for my $name (keys %command) { |
186 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
610 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
187 | |
611 | |
… | |
… | |
197 | # remove extcmds |
621 | # remove extcmds |
198 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
622 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
199 | delete $extcmd{$name}; |
623 | delete $extcmd{$name}; |
200 | } |
624 | } |
201 | |
625 | |
|
|
626 | if (my $cb = $pkg->can ("unload")) { |
|
|
627 | eval { |
|
|
628 | $cb->($pkg); |
|
|
629 | 1 |
|
|
630 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
631 | } |
|
|
632 | |
202 | Symbol::delete_package $pkg; |
633 | Symbol::delete_package $pkg; |
203 | } |
634 | } |
204 | |
635 | |
205 | sub load_extensions { |
636 | sub load_extensions { |
206 | my $LIBDIR = maps_directory "perl"; |
637 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
212 | 1 |
643 | 1 |
213 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
214 | } |
645 | } |
215 | } |
646 | } |
216 | |
647 | |
|
|
648 | ############################################################################# |
|
|
649 | # extcmd framework, basically convert ext <msg> |
|
|
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
651 | |
|
|
652 | attach_to_players |
|
|
653 | on_extcmd => sub { |
|
|
654 | my ($pl, $buf) = @_; |
|
|
655 | |
|
|
656 | my $msg = eval { from_json $buf }; |
|
|
657 | |
|
|
658 | if (ref $msg) { |
|
|
659 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
660 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
661 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
662 | } |
|
|
663 | } |
|
|
664 | } else { |
|
|
665 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
666 | } |
|
|
667 | |
|
|
668 | cf::override; |
|
|
669 | }, |
|
|
670 | ; |
|
|
671 | |
|
|
672 | ############################################################################# |
|
|
673 | # load/save/clean perl data associated with a map |
|
|
674 | |
|
|
675 | *cf::mapsupport::on_clean = sub { |
|
|
676 | my ($map) = @_; |
|
|
677 | |
|
|
678 | my $path = $map->tmpname; |
|
|
679 | defined $path or return; |
|
|
680 | |
|
|
681 | unlink "$path.pst"; |
|
|
682 | }; |
|
|
683 | |
|
|
684 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
685 | |
|
|
686 | ############################################################################# |
|
|
687 | # load/save perl data associated with player->ob objects |
|
|
688 | |
|
|
689 | sub all_objects(@) { |
|
|
690 | @_, map all_objects ($_->inv), @_ |
|
|
691 | } |
|
|
692 | |
|
|
693 | # TODO: compatibility cruft, remove when no longer needed |
|
|
694 | attach_to_players |
|
|
695 | on_load => sub { |
|
|
696 | my ($pl, $path) = @_; |
|
|
697 | |
|
|
698 | for my $o (all_objects $pl->ob) { |
|
|
699 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
700 | $o->set_ob_key_value ("_perl_data"); |
|
|
701 | |
|
|
702 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
703 | } |
|
|
704 | } |
|
|
705 | }, |
|
|
706 | ; |
|
|
707 | |
|
|
708 | ############################################################################# |
|
|
709 | # core extensions - in perl |
|
|
710 | |
|
|
711 | =item cf::player::exists $login |
|
|
712 | |
|
|
713 | Returns true when the given account exists. |
|
|
714 | |
|
|
715 | =cut |
|
|
716 | |
|
|
717 | sub cf::player::exists($) { |
|
|
718 | cf::player::find $_[0] |
|
|
719 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
720 | } |
|
|
721 | |
|
|
722 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
723 | |
|
|
724 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
725 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
726 | dialogue with the given NPC character. |
|
|
727 | |
|
|
728 | =cut |
|
|
729 | |
|
|
730 | # rough implementation of a future "reply" method that works |
|
|
731 | # with dialog boxes. |
|
|
732 | sub cf::object::player::reply($$$;$) { |
|
|
733 | my ($self, $npc, $msg, $flags) = @_; |
|
|
734 | |
|
|
735 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
736 | |
|
|
737 | if ($self->{record_replies}) { |
|
|
738 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
739 | } else { |
|
|
740 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
741 | $self->message ($msg, $flags); |
|
|
742 | } |
|
|
743 | } |
|
|
744 | |
|
|
745 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
746 | |
|
|
747 | Sends an ext reply to the player. |
|
|
748 | |
|
|
749 | =cut |
|
|
750 | |
|
|
751 | sub cf::player::ext_reply($$$%) { |
|
|
752 | my ($self, $id, %msg) = @_; |
|
|
753 | |
|
|
754 | $msg{msgid} = $id; |
|
|
755 | |
|
|
756 | $self->send ("ext " . to_json \%msg); |
|
|
757 | } |
|
|
758 | |
|
|
759 | ############################################################################# |
|
|
760 | # map scripting support |
|
|
761 | |
|
|
762 | our $safe = new Safe "safe"; |
|
|
763 | our $safe_hole = new Safe::Hole; |
|
|
764 | |
|
|
765 | $SIG{FPE} = 'IGNORE'; |
|
|
766 | |
|
|
767 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
768 | |
|
|
769 | # here we export the classes and methods available to script code |
|
|
770 | |
|
|
771 | for ( |
|
|
772 | ["cf::object" => qw(contr pay_amount pay_player)], |
|
|
773 | ["cf::object::player" => qw(player)], |
|
|
774 | ["cf::player" => qw(peaceful)], |
|
|
775 | ) { |
|
|
776 | no strict 'refs'; |
|
|
777 | my ($pkg, @funs) = @$_; |
|
|
778 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
779 | for @funs; |
|
|
780 | } |
|
|
781 | |
|
|
782 | sub safe_eval($;@) { |
|
|
783 | my ($code, %vars) = @_; |
|
|
784 | |
|
|
785 | my $qcode = $code; |
|
|
786 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
787 | $qcode =~ s/\n/\\n/g; |
|
|
788 | |
|
|
789 | local $_; |
|
|
790 | local @safe::cf::_safe_eval_args = values %vars; |
|
|
791 | |
|
|
792 | my $eval = |
|
|
793 | "do {\n" |
|
|
794 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
795 | . "#line 0 \"{$qcode}\"\n" |
|
|
796 | . $code |
|
|
797 | . "\n}" |
|
|
798 | ; |
|
|
799 | |
|
|
800 | sub_generation_inc; |
|
|
801 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
|
|
802 | sub_generation_inc; |
|
|
803 | |
|
|
804 | if ($@) { |
|
|
805 | warn "$@"; |
|
|
806 | warn "while executing safe code '$code'\n"; |
|
|
807 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
808 | } |
|
|
809 | |
|
|
810 | wantarray ? @res : $res[0] |
|
|
811 | } |
|
|
812 | |
|
|
813 | sub register_script_function { |
|
|
814 | my ($fun, $cb) = @_; |
|
|
815 | |
|
|
816 | no strict 'refs'; |
|
|
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
|
|
818 | } |
|
|
819 | |
|
|
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::localdir . "/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 | if (0 == ($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 | attach_global |
|
|
917 | prio => 10000, |
|
|
918 | on_cleanup => sub { |
|
|
919 | db_sync; |
|
|
920 | }, |
|
|
921 | ; |
|
|
922 | } |
|
|
923 | |
|
|
924 | ############################################################################# |
|
|
925 | # the server's main() |
|
|
926 | |
|
|
927 | sub main { |
|
|
928 | db_load; |
|
|
929 | load_extensions; |
|
|
930 | Event::loop; |
|
|
931 | } |
|
|
932 | |
|
|
933 | ############################################################################# |
|
|
934 | # initialisation |
|
|
935 | |
|
|
936 | sub _perl_reload(&) { |
|
|
937 | my ($msg) = @_; |
|
|
938 | |
|
|
939 | $msg->("reloading..."); |
|
|
940 | |
|
|
941 | eval { |
|
|
942 | # cancel all watchers |
|
|
943 | $_->cancel for Event::all_watchers; |
|
|
944 | |
|
|
945 | # unload all extensions |
|
|
946 | for (@exts) { |
|
|
947 | $msg->("unloading <$_>"); |
|
|
948 | unload_extension $_; |
|
|
949 | } |
|
|
950 | |
|
|
951 | # unload all modules loaded from $LIBDIR |
|
|
952 | while (my ($k, $v) = each %INC) { |
|
|
953 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
954 | |
|
|
955 | $msg->("removing <$k>"); |
|
|
956 | delete $INC{$k}; |
|
|
957 | |
|
|
958 | $k =~ s/\.pm$//; |
|
|
959 | $k =~ s/\//::/g; |
|
|
960 | |
|
|
961 | if (my $cb = $k->can ("unload_module")) { |
|
|
962 | $cb->(); |
|
|
963 | } |
|
|
964 | |
|
|
965 | Symbol::delete_package $k; |
|
|
966 | } |
|
|
967 | |
|
|
968 | # sync database to disk |
|
|
969 | cf::db_sync; |
|
|
970 | |
|
|
971 | # get rid of safe::, as good as possible |
|
|
972 | Symbol::delete_package "safe::$_" |
|
|
973 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
974 | |
|
|
975 | # remove register_script_function callbacks |
|
|
976 | # TODO |
|
|
977 | |
|
|
978 | # unload cf.pm "a bit" |
|
|
979 | delete $INC{"cf.pm"}; |
|
|
980 | |
|
|
981 | # don't, removes xs symbols, too, |
|
|
982 | # and global variables created in xs |
|
|
983 | #Symbol::delete_package __PACKAGE__; |
|
|
984 | |
|
|
985 | # reload cf.pm |
|
|
986 | $msg->("reloading cf.pm"); |
|
|
987 | require cf; |
|
|
988 | |
|
|
989 | # load database again |
|
|
990 | cf::db_load; |
|
|
991 | |
|
|
992 | # load extensions |
|
|
993 | $msg->("load extensions"); |
|
|
994 | cf::load_extensions; |
|
|
995 | |
|
|
996 | # reattach attachments to objects |
|
|
997 | $msg->("reattach"); |
|
|
998 | _global_reattach; |
|
|
999 | }; |
|
|
1000 | $msg->($@) if $@; |
|
|
1001 | |
|
|
1002 | $msg->("reloaded"); |
|
|
1003 | }; |
|
|
1004 | |
|
|
1005 | sub perl_reload() { |
|
|
1006 | _perl_reload { |
|
|
1007 | warn $_[0]; |
|
|
1008 | print "$_[0]\n"; |
|
|
1009 | }; |
|
|
1010 | } |
|
|
1011 | |
217 | register_command "perl-reload", 0, sub { |
1012 | register_command "perl-reload", 0, sub { |
218 | my ($who, $arg) = @_; |
1013 | my ($who, $arg) = @_; |
219 | |
1014 | |
220 | if ($who->flag (FLAG_WIZ)) { |
1015 | if ($who->flag (FLAG_WIZ)) { |
221 | $who->message ("reloading..."); |
1016 | _perl_reload { |
222 | |
1017 | warn $_[0]; |
223 | warn "reloading...\n"; |
1018 | $who->message ($_[0]); |
224 | eval { |
|
|
225 | $_->cancel for Event::all_watchers; |
|
|
226 | |
|
|
227 | unload_extension $_ for @exts; |
|
|
228 | delete $INC{"cf.pm"}; |
|
|
229 | |
|
|
230 | # don't, removes xs symbols, too |
|
|
231 | #Symbol::delete_package __PACKAGE__; |
|
|
232 | |
|
|
233 | require cf; |
|
|
234 | }; |
1019 | }; |
235 | warn $@ if $@; |
|
|
236 | $who->message ($@) if $@; |
|
|
237 | warn "reloaded\n"; |
|
|
238 | |
|
|
239 | $who->message ("reloaded"); |
|
|
240 | } else { |
|
|
241 | $who->message ("Intruder Alert!"); |
|
|
242 | } |
1020 | } |
243 | }; |
1021 | }; |
244 | |
1022 | |
245 | ############################################################################# |
|
|
246 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
|
|
247 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
248 | |
|
|
249 | sub on_extcmd { |
|
|
250 | my ($pl, $buf) = @_; |
|
|
251 | |
|
|
252 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
|
|
253 | |
|
|
254 | $extcmd{$type}[0]->($pl, $buf) |
|
|
255 | if $extcmd{$type}; |
|
|
256 | } |
|
|
257 | |
|
|
258 | ############################################################################# |
|
|
259 | # load/save/clean perl data associated with a map |
|
|
260 | |
|
|
261 | *on_mapclean = sub { |
|
|
262 | my ($map) = @_; |
|
|
263 | |
|
|
264 | my $path = $map->tmpname; |
|
|
265 | defined $path or return; |
|
|
266 | |
|
|
267 | unlink "$path.cfperl"; |
|
|
268 | }; |
|
|
269 | |
|
|
270 | *on_mapin = |
|
|
271 | *on_mapload = sub { |
|
|
272 | my ($map) = @_; |
|
|
273 | |
|
|
274 | my $path = $map->tmpname; |
|
|
275 | $path = $map->path unless defined $path; |
|
|
276 | |
|
|
277 | open my $fh, "<:raw", "$path.cfperl" |
|
|
278 | or return; # no perl data |
|
|
279 | |
|
|
280 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
281 | |
|
|
282 | $data->{version} <= 1 |
|
|
283 | or return; # too new |
|
|
284 | |
|
|
285 | $map->_set_obs ($data->{obs}); |
|
|
286 | }; |
|
|
287 | |
|
|
288 | *on_mapout = sub { |
|
|
289 | my ($map) = @_; |
|
|
290 | |
|
|
291 | my $path = $map->tmpname; |
|
|
292 | $path = $map->path unless defined $path; |
|
|
293 | |
|
|
294 | my $obs = $map->_get_obs; |
|
|
295 | |
|
|
296 | if (defined $obs) { |
|
|
297 | open my $fh, ">:raw", "$path.cfperl" |
|
|
298 | or die "$path.cfperl: $!"; |
|
|
299 | |
|
|
300 | stat $path; |
|
|
301 | |
|
|
302 | print $fh Storable::nfreeze { |
|
|
303 | size => (stat _)[7], |
|
|
304 | time => (stat _)[9], |
|
|
305 | version => 1, |
|
|
306 | obs => $obs, |
|
|
307 | }; |
|
|
308 | |
|
|
309 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
310 | } else { |
|
|
311 | unlink "$path.cfperl"; |
|
|
312 | } |
|
|
313 | }; |
|
|
314 | |
|
|
315 | ############################################################################# |
|
|
316 | # load/save perl data associated with player->ob objects |
|
|
317 | |
|
|
318 | *on_player_load = sub { |
|
|
319 | my ($ob, $path) = @_; |
|
|
320 | |
|
|
321 | if (open my $fh, "<:raw", "$path.cfperl") { |
|
|
322 | |
|
|
323 | #d##TODO#remove |
|
|
324 | |
|
|
325 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
326 | |
|
|
327 | $data->{version} <= 1 |
|
|
328 | or return; # too new |
|
|
329 | |
|
|
330 | %$ob = %{$data->{ob}}; |
|
|
331 | return; |
|
|
332 | } |
|
|
333 | |
|
|
334 | for my $o ($ob, $ob->inv) { |
|
|
335 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
336 | $o->set_ob_key_value ("_perl_data"); |
|
|
337 | |
|
|
338 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
339 | } |
|
|
340 | } |
|
|
341 | }; |
|
|
342 | |
|
|
343 | *on_player_save = sub { |
|
|
344 | my ($ob, $path) = @_; |
|
|
345 | |
|
|
346 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
347 | for grep %$_, $ob, $ob->inv; |
|
|
348 | |
|
|
349 | unlink "$path.cfperl";#d##TODO#remove |
|
|
350 | }; |
|
|
351 | |
|
|
352 | register "<global>", __PACKAGE__; |
1023 | register "<global>", __PACKAGE__; |
353 | |
1024 | |
354 | unshift @INC, maps_directory "perl"; |
1025 | unshift @INC, $LIBDIR; |
355 | |
1026 | |
356 | load_extensions; |
1027 | $TICK_WATCHER = Event->timer ( |
|
|
1028 | prio => 1, |
|
|
1029 | at => $NEXT_TICK || 1, |
|
|
1030 | cb => sub { |
|
|
1031 | cf::server_tick; # one server iteration |
|
|
1032 | |
|
|
1033 | my $NOW = Event::time; |
|
|
1034 | $NEXT_TICK += $TICK; |
|
|
1035 | |
|
|
1036 | # if we are delayed by four ticks, skip them all |
|
|
1037 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
1038 | |
|
|
1039 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
1040 | $TICK_WATCHER->start; |
|
|
1041 | }, |
|
|
1042 | ); |
357 | |
1043 | |
358 | 1 |
1044 | 1 |
359 | |
1045 | |