… | |
… | |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | our %COMMAND = (); |
16 | our %COMMAND = (); |
17 | our @EVENT; |
17 | our @EVENT; |
18 | our @PLUGIN_EVENT; |
|
|
19 | our %PROP_TYPE; |
18 | our %PROP_TYPE; |
20 | our %PROP_IDX; |
19 | our %PROP_IDX; |
21 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
22 | |
21 | |
23 | our $TICK = MAX_TIME * 1e-6; |
22 | our $TICK = MAX_TIME * 1e-6; |
24 | our $TICK_WATCHER; |
23 | our $TICK_WATCHER; |
25 | our $NEXT_TICK; |
24 | our $NEXT_TICK; |
26 | |
25 | |
27 | BEGIN { |
26 | BEGIN { |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
|
|
29 | |
|
|
30 | *CORE::GLOBAL::warn = sub { |
27 | *CORE::GLOBAL::warn = sub { |
31 | my $msg = join "", @_; |
28 | my $msg = join "", @_; |
32 | $msg .= "\n" |
29 | $msg .= "\n" |
33 | unless $msg =~ /\n$/; |
30 | unless $msg =~ /\n$/; |
34 | |
31 | |
… | |
… | |
69 | prop_gen PLAYER_PROP => "cf::object::player"; |
66 | prop_gen PLAYER_PROP => "cf::object::player"; |
70 | |
67 | |
71 | prop_gen MAP_PROP => "cf::map"; |
68 | prop_gen MAP_PROP => "cf::map"; |
72 | prop_gen ARCH_PROP => "cf::arch"; |
69 | prop_gen ARCH_PROP => "cf::arch"; |
73 | |
70 | |
74 | # guessed hierarchies |
|
|
75 | |
|
|
76 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
77 | @safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
72 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
73 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
74 | # within the Safe compartment. |
81 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
75 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
82 | no strict 'refs'; |
76 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
77 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
78 | } |
85 | |
79 | |
86 | $Event::DIED = sub { |
80 | $Event::DIED = sub { |
… | |
… | |
92 | my @hook; |
86 | my @hook; |
93 | my %command; |
87 | my %command; |
94 | my %extcmd; |
88 | my %extcmd; |
95 | |
89 | |
96 | ############################################################################# |
90 | ############################################################################# |
|
|
91 | # utility functions |
|
|
92 | |
|
|
93 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
94 | |
|
|
95 | sub from_json($) { |
|
|
96 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
97 | JSON::Syck::Load $_[0] |
|
|
98 | } |
|
|
99 | |
|
|
100 | sub to_json($) { |
|
|
101 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
102 | JSON::Syck::Dump $_[0] |
|
|
103 | } |
|
|
104 | |
|
|
105 | ############################################################################# |
97 | # "new" plug-in system |
106 | # "new" plug-in system |
98 | |
107 | |
99 | =item cf::object::attach ... # NYI |
108 | =item $object->attach ($attachment, ...) |
|
|
109 | |
|
|
110 | Attach a pre-registered attachment to an object. |
|
|
111 | |
|
|
112 | =item $player->attach ($attachment, ...) |
|
|
113 | |
|
|
114 | Attach a pre-registered attachment to a player. |
|
|
115 | |
|
|
116 | =item $map->attach ($attachment, ...) # not yet persistent |
|
|
117 | |
|
|
118 | Attach a pre-registered attachment to a map. |
100 | |
119 | |
101 | =item cf::attach_global ... |
120 | =item cf::attach_global ... |
102 | |
121 | |
|
|
122 | Attach handlers for global events. |
|
|
123 | |
|
|
124 | This and all following C<attach_*>-functions expect any number of the |
|
|
125 | following handler/hook descriptions: |
|
|
126 | |
|
|
127 | =over 4 |
|
|
128 | |
|
|
129 | =item prio => $number |
|
|
130 | |
|
|
131 | Set the priority for all following handlers/hooks (unless overwritten |
|
|
132 | by another C<prio> setting). Lower priority handlers get executed |
|
|
133 | earlier. The default priority is C<0>, and many built-in handlers are |
|
|
134 | registered at priority C<-1000>, so lower priorities should not be used |
|
|
135 | unless you know what you are doing. |
|
|
136 | |
|
|
137 | =item on_I<event> => \&cb |
|
|
138 | |
|
|
139 | Call the given code reference whenever the named event happens (event is |
|
|
140 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
|
|
141 | handlers are recognised generally depends on the type of object these |
|
|
142 | handlers attach to). |
|
|
143 | |
|
|
144 | See F<include/eventinc.h> for the full list of events supported, and their |
|
|
145 | class. |
|
|
146 | |
|
|
147 | =item package => package:: |
|
|
148 | |
|
|
149 | Look for sub functions of the name C<< on_I<event> >> in the given |
|
|
150 | package and register them. Only handlers for eevents supported by the |
|
|
151 | object/class are recognised. |
|
|
152 | |
|
|
153 | =back |
|
|
154 | |
103 | =item cf::attach_to_type ... |
155 | =item cf::attach_to_type $object_type, ... |
|
|
156 | |
|
|
157 | Attach handlers for a specific object type (e.g. TRANSPORT). |
104 | |
158 | |
105 | =item cf::attach_to_objects ... |
159 | =item cf::attach_to_objects ... |
106 | |
160 | |
|
|
161 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
162 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
163 | the game. |
|
|
164 | |
107 | =item cf::attach_to_players ... |
165 | =item cf::attach_to_players ... |
108 | |
166 | |
|
|
167 | Attach handlers to all players. |
|
|
168 | |
109 | =item cf::attach_to_maps ... |
169 | =item cf::attach_to_maps ... |
110 | |
170 | |
111 | prio => $number, # lower is earlier |
171 | Attach handlers to all maps. |
112 | on_xxx => \&cb, |
172 | |
113 | package => package::, |
173 | =item cf:register_attachment $name, ... |
114 | |
174 | |
115 | =cut |
175 | =cut |
116 | |
176 | |
117 | # the following variables are defined in .xs and must not be re-created |
177 | # the following variables are defined in .xs and must not be re-created |
118 | our @CB_GLOBAL = (); # registry for all global events |
178 | our @CB_GLOBAL = (); # registry for all global events |
119 | our @CB_OBJECT = (); |
179 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
120 | our @CB_PLAYER = (); |
180 | our @CB_PLAYER = (); |
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
181 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
122 | our @CB_MAP = (); |
182 | our @CB_MAP = (); |
123 | |
183 | |
|
|
184 | my %attachment; |
|
|
185 | |
124 | sub _attach_cb($\%$$$) { |
186 | sub _attach_cb($\%$$$) { |
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
187 | my ($registry, $undo, $event, $prio, $cb) = @_; |
126 | |
188 | |
127 | use sort 'stable'; |
189 | use sort 'stable'; |
128 | |
190 | |
… | |
… | |
136 | } |
198 | } |
137 | |
199 | |
138 | # attach handles attaching event callbacks |
200 | # attach handles attaching event callbacks |
139 | # the only thing the caller has to do is pass the correct |
201 | # the only thing the caller has to do is pass the correct |
140 | # registry (== where the callback attaches to). |
202 | # registry (== where the callback attaches to). |
141 | sub _attach(\@$\@) { |
203 | sub _attach(\@$@) { |
142 | my ($registry, $klass, $arg) = @_; |
204 | my ($registry, $klass, @arg) = @_; |
143 | |
205 | |
144 | my $prio = 0; |
206 | my $prio = 0; |
145 | |
207 | |
146 | my %undo = ( |
208 | my %undo = ( |
147 | registry => $registry, |
209 | registry => $registry, |
148 | cb => [], |
210 | cb => [], |
149 | ); |
211 | ); |
150 | |
212 | |
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
213 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
152 | |
214 | |
153 | while (@$arg) { |
215 | while (@arg) { |
154 | my $type = shift @$arg; |
216 | my $type = shift @arg; |
155 | |
217 | |
156 | if ($type eq "prio") { |
218 | if ($type eq "prio") { |
157 | $prio = shift @$arg; |
219 | $prio = shift @arg; |
158 | |
220 | |
159 | } elsif ($type eq "package") { |
221 | } elsif ($type eq "package") { |
160 | my $pkg = shift @$arg; |
222 | my $pkg = shift @arg; |
161 | |
223 | |
162 | while (my ($name, $id) = each %cb_id) { |
224 | while (my ($name, $id) = each %cb_id) { |
163 | if (my $cb = $pkg->can ($name)) { |
225 | if (my $cb = $pkg->can ($name)) { |
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
226 | _attach_cb $registry, %undo, $id, $prio, $cb; |
165 | } |
227 | } |
166 | } |
228 | } |
167 | |
229 | |
168 | } elsif (exists $cb_id{$type}) { |
230 | } elsif (exists $cb_id{$type}) { |
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
231 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
170 | |
232 | |
171 | } elsif (ref $type) { |
233 | } elsif (ref $type) { |
172 | warn "attaching objects not supported, ignoring.\n"; |
234 | warn "attaching objects not supported, ignoring.\n"; |
173 | |
235 | |
174 | } else { |
236 | } else { |
175 | shift @$arg; |
237 | shift @arg; |
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
238 | warn "attach argument '$type' not supported, ignoring.\n"; |
177 | } |
239 | } |
178 | } |
240 | } |
179 | |
241 | |
180 | \%undo |
242 | \%undo |
181 | } |
243 | } |
182 | |
244 | |
|
|
245 | sub _attach_attachment { |
|
|
246 | my ($klass, $obj, $name, @args) = q_; |
|
|
247 | |
|
|
248 | my $res; |
|
|
249 | |
|
|
250 | if (my $attach = $attachment{$name}) { |
|
|
251 | my $registry = $obj->registry; |
|
|
252 | |
|
|
253 | $res = _attach @$registry, $klass, @$attach; |
|
|
254 | |
|
|
255 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
|
|
256 | for (@$cb) { |
|
|
257 | eval { $_->[1]->($obj, @args); }; |
|
|
258 | if ($@) { |
|
|
259 | warn "$@"; |
|
|
260 | warn "... while processing '$name' instantiate with args <@args>.\n"; |
|
|
261 | } |
|
|
262 | } |
|
|
263 | } |
|
|
264 | } else { |
|
|
265 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
266 | } |
|
|
267 | |
|
|
268 | push @{$obj->{_attachment}}, $name; |
|
|
269 | |
|
|
270 | $res->{attachment} = $name; |
|
|
271 | $res |
|
|
272 | } |
|
|
273 | |
183 | sub cf::object::attach { |
274 | sub cf::object::attach { |
184 | die; |
275 | my ($obj, $name, @args) = @_; |
|
|
276 | |
|
|
277 | _attach_attachment KLASS_OBJECT, $obj, $name, @args; |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub cf::player::attach { |
|
|
281 | my ($obj, $name, @args) = @_; |
|
|
282 | |
|
|
283 | _attach_attachment KLASS_PLAYER, $obj, $name, @args; |
|
|
284 | } |
|
|
285 | |
|
|
286 | sub cf::map::attach { |
|
|
287 | my ($obj, $name, @args) = @_; |
|
|
288 | |
|
|
289 | _attach_attachment KLASS_MAP, $obj, $name, @args; |
185 | } |
290 | } |
186 | |
291 | |
187 | sub attach_global { |
292 | sub attach_global { |
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
293 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
189 | } |
294 | } |
190 | |
295 | |
191 | sub attach_to_type { |
296 | sub attach_to_type { |
192 | my $type = shift; |
297 | my $type = shift; |
|
|
298 | |
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
299 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
194 | } |
300 | } |
195 | |
301 | |
196 | sub attach_to_objects { |
302 | sub attach_to_objects { |
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
303 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
… | |
… | |
203 | |
309 | |
204 | sub attach_to_maps { |
310 | sub attach_to_maps { |
205 | _attach @CB_MAP, KLASS_MAP, @_ |
311 | _attach @CB_MAP, KLASS_MAP, @_ |
206 | } |
312 | } |
207 | |
313 | |
|
|
314 | sub register_attachment { |
|
|
315 | my $name = shift; |
|
|
316 | |
|
|
317 | $attachment{$name} = [@_]; |
|
|
318 | } |
|
|
319 | |
208 | our $override; |
320 | our $override; |
|
|
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
209 | |
322 | |
210 | sub override() { |
323 | sub override { |
211 | $override = 1 |
324 | $override = 1; |
|
|
325 | @invoke_results = (); |
212 | } |
326 | } |
213 | |
327 | |
214 | sub invoke { |
328 | sub do_invoke { |
215 | my $event = shift; |
329 | my $event = shift; |
216 | my $callbacks = shift; |
330 | my $callbacks = shift; |
|
|
331 | |
|
|
332 | @invoke_results = (); |
217 | |
333 | |
218 | local $override; |
334 | local $override; |
219 | |
335 | |
220 | for (@$callbacks) { |
336 | for (@$callbacks) { |
221 | eval { &{$_->[1]} }; |
337 | eval { &{$_->[1]} }; |
… | |
… | |
231 | |
347 | |
232 | 0 |
348 | 0 |
233 | } |
349 | } |
234 | |
350 | |
235 | ############################################################################# |
351 | ############################################################################# |
|
|
352 | # object support |
|
|
353 | |
|
|
354 | sub instantiate { |
|
|
355 | my ($obj, $data) = @_; |
|
|
356 | |
|
|
357 | $data = from_json $data; |
|
|
358 | |
|
|
359 | for (@$data) { |
|
|
360 | my ($name, $args) = @$_; |
|
|
361 | attach $obj, $name, @{$args || [] }; |
|
|
362 | } |
|
|
363 | } |
|
|
364 | |
|
|
365 | # basically do the same as instantiate, without calling instantiate |
|
|
366 | sub reattach { |
|
|
367 | warn "reattach<@_>\n";#d# |
|
|
368 | my ($obj) = @_; |
|
|
369 | my $registry = $obj->registry; |
|
|
370 | |
|
|
371 | for my $name (@{ $obj->{_attachment} }) { |
|
|
372 | if (my $attach = $attachment{$name}) { |
|
|
373 | _attach @$registry, KLASS_OBJECT, @$attach; |
|
|
374 | } else { |
|
|
375 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
376 | } |
|
|
377 | } |
|
|
378 | |
|
|
379 | warn "reattach<@_, $_>\n"; |
|
|
380 | } |
|
|
381 | |
|
|
382 | sub object_freezer_save { |
|
|
383 | my ($filename, $objs) = @_; |
|
|
384 | warn "freeze $filename\n";#d# |
|
|
385 | use Data::Dumper; print Dumper $objs; |
|
|
386 | |
|
|
387 | $filename .= ".pst"; |
|
|
388 | |
|
|
389 | if (@$objs) { |
|
|
390 | open my $fh, ">:raw", "$filename~"; |
|
|
391 | chmod $fh, SAVE_MODE; |
|
|
392 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
393 | close $fh; |
|
|
394 | rename "$filename~", $filename; |
|
|
395 | } else { |
|
|
396 | unlink $filename; |
|
|
397 | } |
|
|
398 | } |
|
|
399 | |
|
|
400 | sub object_thawer_load { |
|
|
401 | my ($filename) = @_; |
|
|
402 | |
|
|
403 | warn "thaw $filename\n";#d# |
|
|
404 | |
|
|
405 | open my $fh, "<:raw:perlio", "$filename.pst" |
|
|
406 | or return; |
|
|
407 | |
|
|
408 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
|
|
409 | } |
|
|
410 | |
|
|
411 | attach_to_objects |
|
|
412 | prio => -1000000, |
|
|
413 | on_clone => sub { |
|
|
414 | my ($src, $dst) = @_; |
|
|
415 | |
|
|
416 | @{$dst->registry} = @{$src->registry}; |
|
|
417 | warn "registry clone ", join ":", @{$src->registry};#d# |
|
|
418 | |
|
|
419 | %$dst = %$src; |
|
|
420 | |
|
|
421 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
|
|
422 | if exists $src->{_attachment}; |
|
|
423 | |
|
|
424 | warn "clone<@_>\n";#d# |
|
|
425 | }, |
|
|
426 | ; |
|
|
427 | |
|
|
428 | ############################################################################# |
236 | # old plug-in events |
429 | # old plug-in events |
237 | |
430 | |
238 | sub inject_event { |
431 | sub inject_event { |
239 | my $extension = shift; |
432 | my $extension = shift; |
240 | my $event_code = shift; |
433 | my $event_code = shift; |
… | |
… | |
284 | } |
477 | } |
285 | |
478 | |
286 | sub register { |
479 | sub register { |
287 | my ($base, $pkg) = @_; |
480 | my ($base, $pkg) = @_; |
288 | |
481 | |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
482 | #TODO |
290 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
|
|
291 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
|
|
292 | $hook[$idx]{$base} = $ref; |
|
|
293 | } |
|
|
294 | } |
|
|
295 | } |
483 | } |
296 | |
484 | |
297 | sub load_extension { |
485 | sub load_extension { |
298 | my ($path) = @_; |
486 | my ($path) = @_; |
299 | |
487 | |
… | |
… | |
330 | my ($pkg) = @_; |
518 | my ($pkg) = @_; |
331 | |
519 | |
332 | warn "removing extension $pkg\n"; |
520 | warn "removing extension $pkg\n"; |
333 | |
521 | |
334 | # remove hooks |
522 | # remove hooks |
|
|
523 | #TODO |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
524 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
336 | delete $hook[$idx]{$pkg}; |
525 | # delete $hook[$idx]{$pkg}; |
337 | } |
526 | # } |
338 | |
527 | |
339 | # remove commands |
528 | # remove commands |
340 | for my $name (keys %command) { |
529 | for my $name (keys %command) { |
341 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
530 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
342 | |
531 | |
… | |
… | |
352 | # remove extcmds |
541 | # remove extcmds |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
542 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
354 | delete $extcmd{$name}; |
543 | delete $extcmd{$name}; |
355 | } |
544 | } |
356 | |
545 | |
357 | if (my $cb = $pkg->can ("on_unload")) { |
546 | if (my $cb = $pkg->can ("unload")) { |
358 | eval { |
547 | eval { |
359 | $cb->($pkg); |
548 | $cb->($pkg); |
360 | 1 |
549 | 1 |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
550 | } or warn "$pkg unloaded, but with errors: $@"; |
362 | } |
551 | } |
… | |
… | |
408 | Symbol::delete_package $k; |
597 | Symbol::delete_package $k; |
409 | } |
598 | } |
410 | |
599 | |
411 | # 4. get rid of safe::, as good as possible |
600 | # 4. get rid of safe::, as good as possible |
412 | Symbol::delete_package "safe::$_" |
601 | Symbol::delete_package "safe::$_" |
413 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
602 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
414 | |
603 | |
415 | # 5. remove register_script_function callbacks |
604 | # 5. remove register_script_function callbacks |
416 | # TODO |
605 | # TODO |
417 | |
606 | |
418 | # 6. unload cf.pm "a bit" |
607 | # 6. unload cf.pm "a bit" |
… | |
… | |
448 | }; |
637 | }; |
449 | } |
638 | } |
450 | }; |
639 | }; |
451 | |
640 | |
452 | ############################################################################# |
641 | ############################################################################# |
453 | # utility functions |
|
|
454 | |
|
|
455 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
456 | |
|
|
457 | sub from_json($) { |
|
|
458 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
459 | JSON::Syck::Load $_[0] |
|
|
460 | } |
|
|
461 | |
|
|
462 | sub to_json($) { |
|
|
463 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
464 | JSON::Syck::Dump $_[0] |
|
|
465 | } |
|
|
466 | |
|
|
467 | ############################################################################# |
|
|
468 | # extcmd framework, basically convert ext <msg> |
642 | # extcmd framework, basically convert ext <msg> |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
643 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
470 | |
644 | |
471 | sub on_extcmd { |
645 | attach_to_players |
|
|
646 | on_extcmd => sub { |
472 | my ($pl, $buf) = @_; |
647 | my ($pl, $buf) = @_; |
473 | |
648 | |
474 | my $msg = eval { from_json $buf }; |
649 | my $msg = eval { from_json $buf }; |
475 | |
650 | |
476 | if (ref $msg) { |
651 | if (ref $msg) { |
477 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
652 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my %reply = $cb->[0]->($pl, $msg)) { |
653 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | $pl->ext_reply ($msg->{msgid}, %reply); |
654 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
655 | } |
480 | } |
656 | } |
|
|
657 | } else { |
|
|
658 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
481 | } |
659 | } |
482 | } else { |
660 | |
483 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
661 | cf::override; |
484 | } |
662 | }, |
485 | |
663 | ; |
486 | 1 |
|
|
487 | } |
|
|
488 | |
664 | |
489 | ############################################################################# |
665 | ############################################################################# |
490 | # load/save/clean perl data associated with a map |
666 | # load/save/clean perl data associated with a map |
491 | |
667 | |
492 | *cf::mapsupport::on_clean = sub { |
668 | *cf::mapsupport::on_clean = sub { |
… | |
… | |
494 | |
670 | |
495 | my $path = $map->tmpname; |
671 | my $path = $map->tmpname; |
496 | defined $path or return; |
672 | defined $path or return; |
497 | |
673 | |
498 | unlink "$path.cfperl"; |
674 | unlink "$path.cfperl"; |
|
|
675 | unlink "$path.pst"; |
499 | }; |
676 | }; |
500 | |
677 | |
501 | *cf::mapsupport::on_swapin = |
678 | *cf::mapsupport::on_swapin = |
502 | *cf::mapsupport::on_load = sub { |
679 | *cf::mapsupport::on_load = sub { |
503 | my ($map) = @_; |
680 | my ($map) = @_; |
… | |
… | |
514 | or return; # too new |
691 | or return; # too new |
515 | |
692 | |
516 | $map->_set_obs ($data->{obs}); |
693 | $map->_set_obs ($data->{obs}); |
517 | }; |
694 | }; |
518 | |
695 | |
519 | *cf::mapsupport::on_swapout = sub { |
|
|
520 | my ($map) = @_; |
|
|
521 | |
|
|
522 | my $path = $map->tmpname; |
|
|
523 | $path = $map->path unless defined $path; |
|
|
524 | |
|
|
525 | my $obs = $map->_get_obs; |
|
|
526 | |
|
|
527 | if (defined $obs) { |
|
|
528 | open my $fh, ">:raw", "$path.cfperl" |
|
|
529 | or die "$path.cfperl: $!"; |
|
|
530 | |
|
|
531 | stat $path; |
|
|
532 | |
|
|
533 | print $fh Storable::nfreeze { |
|
|
534 | size => (stat _)[7], |
|
|
535 | time => (stat _)[9], |
|
|
536 | version => 1, |
|
|
537 | obs => $obs, |
|
|
538 | }; |
|
|
539 | |
|
|
540 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
541 | } else { |
|
|
542 | unlink "$path.cfperl"; |
|
|
543 | } |
|
|
544 | }; |
|
|
545 | |
|
|
546 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
696 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
547 | |
697 | |
548 | ############################################################################# |
698 | ############################################################################# |
549 | # load/save perl data associated with player->ob objects |
699 | # load/save perl data associated with player->ob objects |
550 | |
700 | |
… | |
… | |
561 | $o->set_ob_key_value ("_perl_data"); |
711 | $o->set_ob_key_value ("_perl_data"); |
562 | |
712 | |
563 | %$o = %{ Storable::thaw pack "H*", $value }; |
713 | %$o = %{ Storable::thaw pack "H*", $value }; |
564 | } |
714 | } |
565 | } |
715 | } |
566 | }, |
|
|
567 | on_save => sub { |
|
|
568 | my ($pl, $path) = @_; |
|
|
569 | |
|
|
570 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
571 | for grep %$_, all_objects $pl->ob; |
|
|
572 | }, |
716 | }, |
573 | ; |
717 | ; |
574 | |
718 | |
575 | ############################################################################# |
719 | ############################################################################# |
576 | # core extensions - in perl |
720 | # core extensions - in perl |
… | |
… | |
624 | } |
768 | } |
625 | |
769 | |
626 | ############################################################################# |
770 | ############################################################################# |
627 | # map scripting support |
771 | # map scripting support |
628 | |
772 | |
629 | our $safe = new Safe "ext"; |
773 | our $safe = new Safe "safe"; |
630 | our $safe_hole = new Safe::Hole; |
774 | our $safe_hole = new Safe::Hole; |
631 | |
775 | |
632 | $SIG{FPE} = 'IGNORE'; |
776 | $SIG{FPE} = 'IGNORE'; |
633 | |
777 | |
634 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
778 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
654 | $qcode =~ s/\n/\\n/g; |
798 | $qcode =~ s/\n/\\n/g; |
655 | |
799 | |
656 | local $_; |
800 | local $_; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
801 | local @safe::cf::_safe_eval_args = values %vars; |
658 | |
802 | |
659 | $code = |
803 | my $eval = |
660 | "do {\n" |
804 | "do {\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
805 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
806 | . "#line 0 \"{$qcode}\"\n" |
663 | . $code |
807 | . $code |
664 | . "\n}" |
808 | . "\n}" |
665 | ; |
809 | ; |
666 | |
810 | |
667 | sub_generation_inc; |
811 | sub_generation_inc; |
668 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
812 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
669 | sub_generation_inc; |
813 | sub_generation_inc; |
|
|
814 | |
|
|
815 | if ($@) { |
|
|
816 | warn "$@"; |
|
|
817 | warn "while executing safe code '$code'\n"; |
|
|
818 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
819 | } |
670 | |
820 | |
671 | wantarray ? @res : $res[0] |
821 | wantarray ? @res : $res[0] |
672 | } |
822 | } |
673 | |
823 | |
674 | sub register_script_function { |
824 | sub register_script_function { |