… | |
… | |
11 | use Event; |
11 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
|
|
16 | _reload_1; |
|
|
17 | |
16 | our %COMMAND = (); |
18 | our %COMMAND = (); |
17 | our @EVENT; |
19 | our @EVENT; |
18 | our @PLUGIN_EVENT; |
|
|
19 | our %PROP_TYPE; |
20 | our %PROP_TYPE; |
20 | our %PROP_IDX; |
21 | our %PROP_IDX; |
21 | our $LIBDIR = maps_directory "perl"; |
22 | our $LIBDIR = maps_directory "perl"; |
22 | |
23 | |
23 | our $TICK = MAX_TIME * 1e-6; |
24 | our $TICK = MAX_TIME * 1e-6; |
24 | our $TICK_WATCHER; |
25 | our $TICK_WATCHER; |
25 | our $NEXT_TICK; |
26 | our $NEXT_TICK; |
26 | |
27 | |
27 | BEGIN { |
28 | BEGIN { |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
|
|
29 | |
|
|
30 | *CORE::GLOBAL::warn = sub { |
29 | *CORE::GLOBAL::warn = sub { |
31 | my $msg = join "", @_; |
30 | my $msg = join "", @_; |
32 | $msg .= "\n" |
31 | $msg .= "\n" |
33 | unless $msg =~ /\n$/; |
32 | unless $msg =~ /\n$/; |
34 | |
33 | |
… | |
… | |
69 | prop_gen PLAYER_PROP => "cf::object::player"; |
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
70 | |
69 | |
71 | prop_gen MAP_PROP => "cf::map"; |
70 | prop_gen MAP_PROP => "cf::map"; |
72 | prop_gen ARCH_PROP => "cf::arch"; |
71 | prop_gen ARCH_PROP => "cf::arch"; |
73 | |
72 | |
74 | # guessed hierarchies |
|
|
75 | |
|
|
76 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
77 | @safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
74 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
76 | # 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)) { |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
82 | no strict 'refs'; |
78 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
80 | } |
85 | |
81 | |
86 | $Event::DIED = sub { |
82 | $Event::DIED = sub { |
… | |
… | |
92 | my @hook; |
88 | my @hook; |
93 | my %command; |
89 | my %command; |
94 | my %extcmd; |
90 | my %extcmd; |
95 | |
91 | |
96 | ############################################################################# |
92 | ############################################################################# |
|
|
93 | # utility functions |
|
|
94 | |
|
|
95 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
96 | |
|
|
97 | sub from_json($) { |
|
|
98 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
99 | JSON::Syck::Load $_[0] |
|
|
100 | } |
|
|
101 | |
|
|
102 | sub to_json($) { |
|
|
103 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
104 | JSON::Syck::Dump $_[0] |
|
|
105 | } |
|
|
106 | |
|
|
107 | ############################################################################# |
97 | # "new" plug-in system |
108 | # "new" plug-in system |
98 | |
109 | |
99 | =item cf::object::attach ... # NYI |
110 | =item $object->attach ($attachment, key => $value...) |
|
|
111 | |
|
|
112 | Attach a pre-registered attachment to an object. |
|
|
113 | |
|
|
114 | =item $player->attach ($attachment, key => $value...) |
|
|
115 | |
|
|
116 | Attach a pre-registered attachment to a player. |
|
|
117 | |
|
|
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
|
|
119 | |
|
|
120 | Attach a pre-registered attachment to a map. |
100 | |
121 | |
101 | =item cf::attach_global ... |
122 | =item cf::attach_global ... |
102 | |
123 | |
103 | =item cf::attach_to_type ... |
124 | Attach handlers for global events. |
|
|
125 | |
|
|
126 | This and all following C<attach_*>-functions expect any number of the |
|
|
127 | following handler/hook descriptions: |
|
|
128 | |
|
|
129 | =over 4 |
|
|
130 | |
|
|
131 | =item prio => $number |
|
|
132 | |
|
|
133 | Set the priority for all following handlers/hooks (unless overwritten |
|
|
134 | by another C<prio> setting). Lower priority handlers get executed |
|
|
135 | earlier. The default priority is C<0>, and many built-in handlers are |
|
|
136 | registered at priority C<-1000>, so lower priorities should not be used |
|
|
137 | unless you know what you are doing. |
|
|
138 | |
|
|
139 | =item on_I<event> => \&cb |
|
|
140 | |
|
|
141 | Call the given code reference whenever the named event happens (event is |
|
|
142 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
|
|
143 | handlers are recognised generally depends on the type of object these |
|
|
144 | handlers attach to). |
|
|
145 | |
|
|
146 | See F<include/eventinc.h> for the full list of events supported, and their |
|
|
147 | class. |
|
|
148 | |
|
|
149 | =item package => package:: |
|
|
150 | |
|
|
151 | Look for sub functions of the name C<< on_I<event> >> in the given |
|
|
152 | package and register them. Only handlers for eevents supported by the |
|
|
153 | object/class are recognised. |
|
|
154 | |
|
|
155 | =back |
|
|
156 | |
|
|
157 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
158 | |
|
|
159 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
160 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
161 | type. |
104 | |
162 | |
105 | =item cf::attach_to_objects ... |
163 | =item cf::attach_to_objects ... |
106 | |
164 | |
|
|
165 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
166 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
167 | the game. |
|
|
168 | |
107 | =item cf::attach_to_players ... |
169 | =item cf::attach_to_players ... |
108 | |
170 | |
|
|
171 | Attach handlers to all players. |
|
|
172 | |
109 | =item cf::attach_to_maps ... |
173 | =item cf::attach_to_maps ... |
110 | |
174 | |
111 | prio => $number, # lower is earlier |
175 | Attach handlers to all maps. |
112 | on_xxx => \&cb, |
176 | |
113 | package => package::, |
177 | =item cf:register_attachment $name, ... |
|
|
178 | |
|
|
179 | Register an attachment by name through which objects can refer to this |
|
|
180 | attachment. |
|
|
181 | |
|
|
182 | =item cf:register_map_attachment $name, ... |
|
|
183 | |
|
|
184 | Register an attachment by name through which maps can refer to this |
|
|
185 | attachment. |
114 | |
186 | |
115 | =cut |
187 | =cut |
116 | |
188 | |
117 | # the following variables are defined in .xs and must not be re-created |
189 | # the following variables are defined in .xs and must not be re-created |
118 | our @CB_GLOBAL = (); # registry for all global events |
190 | our @CB_GLOBAL = (); # registry for all global events |
119 | our @CB_OBJECT = (); |
191 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
120 | our @CB_PLAYER = (); |
192 | our @CB_PLAYER = (); |
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
193 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
122 | our @CB_MAP = (); |
194 | our @CB_MAP = (); |
123 | |
195 | |
|
|
196 | my %attachment; |
|
|
197 | |
124 | sub _attach_cb($\%$$$) { |
198 | sub _attach_cb($\%$$$) { |
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
199 | my ($registry, $undo, $event, $prio, $cb) = @_; |
126 | |
200 | |
127 | use sort 'stable'; |
201 | use sort 'stable'; |
128 | |
202 | |
… | |
… | |
136 | } |
210 | } |
137 | |
211 | |
138 | # attach handles attaching event callbacks |
212 | # attach handles attaching event callbacks |
139 | # the only thing the caller has to do is pass the correct |
213 | # the only thing the caller has to do is pass the correct |
140 | # registry (== where the callback attaches to). |
214 | # registry (== where the callback attaches to). |
141 | sub _attach(\@$\@) { |
215 | sub _attach(\@$@) { |
142 | my ($registry, $klass, $arg) = @_; |
216 | my ($registry, $klass, @arg) = @_; |
143 | |
217 | |
144 | my $prio = 0; |
218 | my $prio = 0; |
145 | |
219 | |
146 | my %undo = ( |
220 | my %undo = ( |
147 | registry => $registry, |
221 | registry => $registry, |
148 | cb => [], |
222 | cb => [], |
149 | ); |
223 | ); |
150 | |
224 | |
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
225 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
152 | |
226 | |
153 | while (@$arg) { |
227 | while (@arg) { |
154 | my $type = shift @$arg; |
228 | my $type = shift @arg; |
155 | |
229 | |
156 | if ($type eq "prio") { |
230 | if ($type eq "prio") { |
157 | $prio = shift @$arg; |
231 | $prio = shift @arg; |
158 | |
232 | |
159 | } elsif ($type eq "package") { |
233 | } elsif ($type eq "package") { |
160 | my $pkg = shift @$arg; |
234 | my $pkg = shift @arg; |
161 | |
235 | |
162 | while (my ($name, $id) = each %cb_id) { |
236 | while (my ($name, $id) = each %cb_id) { |
163 | if (my $cb = $pkg->can ($name)) { |
237 | if (my $cb = $pkg->can ($name)) { |
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
238 | _attach_cb $registry, %undo, $id, $prio, $cb; |
165 | } |
239 | } |
166 | } |
240 | } |
167 | |
241 | |
168 | } elsif (exists $cb_id{$type}) { |
242 | } elsif (exists $cb_id{$type}) { |
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
243 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
170 | |
244 | |
171 | } elsif (ref $type) { |
245 | } elsif (ref $type) { |
172 | warn "attaching objects not supported, ignoring.\n"; |
246 | warn "attaching objects not supported, ignoring.\n"; |
173 | |
247 | |
174 | } else { |
248 | } else { |
175 | shift @$arg; |
249 | shift @arg; |
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
250 | warn "attach argument '$type' not supported, ignoring.\n"; |
177 | } |
251 | } |
178 | } |
252 | } |
179 | |
253 | |
180 | \%undo |
254 | \%undo |
181 | } |
255 | } |
182 | |
256 | |
|
|
257 | sub _attach_attachment { |
|
|
258 | my ($obj, $name, %arg) = @_; |
|
|
259 | |
|
|
260 | my $res; |
|
|
261 | |
|
|
262 | if (my $attach = $attachment{$name}) { |
|
|
263 | my $registry = $obj->registry; |
|
|
264 | |
|
|
265 | for (@$attach) { |
|
|
266 | my ($klass, @attach) = @$_; |
|
|
267 | $res = _attach @$registry, $klass, @attach; |
|
|
268 | } |
|
|
269 | |
|
|
270 | $obj->{$name} = \%arg; |
|
|
271 | } else { |
|
|
272 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
273 | } |
|
|
274 | |
|
|
275 | $obj->{_attachment}{$name} = undef; |
|
|
276 | |
|
|
277 | $res->{attachment} = $name; |
|
|
278 | $res |
|
|
279 | } |
|
|
280 | |
183 | sub cf::object::attach { |
281 | sub cf::object::attach { |
184 | die; |
282 | my ($obj, $name, %arg) = @_; |
|
|
283 | |
|
|
284 | _attach_attachment $obj, $name, %arg; |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub cf::player::attach { |
|
|
288 | my ($obj, $name, %arg) = @_; |
|
|
289 | |
|
|
290 | _attach_attachment $obj, $name, %arg; |
|
|
291 | } |
|
|
292 | |
|
|
293 | sub cf::map::attach { |
|
|
294 | my ($obj, $name, %arg) = @_; |
|
|
295 | |
|
|
296 | _attach_attachment $obj, $name, %arg; |
185 | } |
297 | } |
186 | |
298 | |
187 | sub attach_global { |
299 | sub attach_global { |
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
189 | } |
301 | } |
190 | |
302 | |
191 | sub attach_to_type { |
303 | sub attach_to_type { |
192 | my $type = shift; |
304 | my $type = shift; |
|
|
305 | my $subtype = shift; |
|
|
306 | |
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
307 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
194 | } |
308 | } |
195 | |
309 | |
196 | sub attach_to_objects { |
310 | sub attach_to_objects { |
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
311 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
198 | } |
312 | } |
… | |
… | |
203 | |
317 | |
204 | sub attach_to_maps { |
318 | sub attach_to_maps { |
205 | _attach @CB_MAP, KLASS_MAP, @_ |
319 | _attach @CB_MAP, KLASS_MAP, @_ |
206 | } |
320 | } |
207 | |
321 | |
|
|
322 | sub register_attachment { |
|
|
323 | my $name = shift; |
|
|
324 | |
|
|
325 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
326 | } |
|
|
327 | |
|
|
328 | sub register_map_attachment { |
|
|
329 | my $name = shift; |
|
|
330 | |
|
|
331 | $attachment{$name} = [[KLASS_MAP, @_]]; |
|
|
332 | } |
|
|
333 | |
208 | our $override; |
334 | our $override; |
|
|
335 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
209 | |
336 | |
210 | sub override() { |
337 | sub override { |
211 | $override = 1 |
338 | $override = 1; |
|
|
339 | @invoke_results = (); |
212 | } |
340 | } |
213 | |
341 | |
214 | sub invoke { |
342 | sub do_invoke { |
215 | my $event = shift; |
343 | my $event = shift; |
216 | my $callbacks = shift; |
344 | my $callbacks = shift; |
|
|
345 | |
|
|
346 | @invoke_results = (); |
217 | |
347 | |
218 | local $override; |
348 | local $override; |
219 | |
349 | |
220 | for (@$callbacks) { |
350 | for (@$callbacks) { |
221 | eval { &{$_->[1]} }; |
351 | eval { &{$_->[1]} }; |
… | |
… | |
231 | |
361 | |
232 | 0 |
362 | 0 |
233 | } |
363 | } |
234 | |
364 | |
235 | ############################################################################# |
365 | ############################################################################# |
|
|
366 | # object support |
|
|
367 | |
|
|
368 | sub instantiate { |
|
|
369 | my ($obj, $data) = @_; |
|
|
370 | |
|
|
371 | $data = from_json $data; |
|
|
372 | |
|
|
373 | for (@$data) { |
|
|
374 | my ($name, $args) = @$_; |
|
|
375 | |
|
|
376 | $obj->attach ($name, %{$args || {} }); |
|
|
377 | } |
|
|
378 | } |
|
|
379 | |
|
|
380 | # basically do the same as instantiate, without calling instantiate |
|
|
381 | sub reattach { |
|
|
382 | my ($obj) = @_; |
|
|
383 | my $registry = $obj->registry; |
|
|
384 | |
|
|
385 | @$registry = (); |
|
|
386 | |
|
|
387 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
388 | |
|
|
389 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
|
|
390 | if (my $attach = $attachment{$name}) { |
|
|
391 | for (@$attach) { |
|
|
392 | my ($klass, @attach) = @$_; |
|
|
393 | _attach @$registry, $klass, @attach; |
|
|
394 | } |
|
|
395 | } else { |
|
|
396 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
397 | } |
|
|
398 | } |
|
|
399 | } |
|
|
400 | |
|
|
401 | sub object_freezer_save { |
|
|
402 | my ($filename, $objs) = @_; |
|
|
403 | |
|
|
404 | if (@$objs) { |
|
|
405 | open my $fh, ">:raw", "$filename.pst~"; |
|
|
406 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
407 | close $fh; |
|
|
408 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
409 | rename "$filename.pst~", "$filename.pst"; |
|
|
410 | } else { |
|
|
411 | unlink "$filename.pst"; |
|
|
412 | } |
|
|
413 | |
|
|
414 | chmod SAVE_MODE, "$filename~"; |
|
|
415 | rename "$filename~", $filename; |
|
|
416 | } |
|
|
417 | |
|
|
418 | sub object_thawer_load { |
|
|
419 | my ($filename) = @_; |
|
|
420 | |
|
|
421 | open my $fh, "<:raw:perlio", "$filename.pst" |
|
|
422 | or return; |
|
|
423 | |
|
|
424 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
|
|
425 | } |
|
|
426 | |
|
|
427 | attach_to_objects |
|
|
428 | prio => -1000000, |
|
|
429 | on_clone => sub { |
|
|
430 | my ($src, $dst) = @_; |
|
|
431 | |
|
|
432 | @{$dst->registry} = @{$src->registry}; |
|
|
433 | |
|
|
434 | %$dst = %$src; |
|
|
435 | |
|
|
436 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
437 | if exists $src->{_attachment}; |
|
|
438 | }, |
|
|
439 | ; |
|
|
440 | |
|
|
441 | ############################################################################# |
236 | # old plug-in events |
442 | # old plug-in events |
237 | |
443 | |
238 | sub inject_event { |
444 | sub inject_event { |
239 | my $extension = shift; |
445 | my $extension = shift; |
240 | my $event_code = shift; |
446 | my $event_code = shift; |
… | |
… | |
284 | } |
490 | } |
285 | |
491 | |
286 | sub register { |
492 | sub register { |
287 | my ($base, $pkg) = @_; |
493 | my ($base, $pkg) = @_; |
288 | |
494 | |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
495 | #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 | } |
496 | } |
296 | |
497 | |
297 | sub load_extension { |
498 | sub load_extension { |
298 | my ($path) = @_; |
499 | my ($path) = @_; |
299 | |
500 | |
… | |
… | |
330 | my ($pkg) = @_; |
531 | my ($pkg) = @_; |
331 | |
532 | |
332 | warn "removing extension $pkg\n"; |
533 | warn "removing extension $pkg\n"; |
333 | |
534 | |
334 | # remove hooks |
535 | # remove hooks |
|
|
536 | #TODO |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
537 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
336 | delete $hook[$idx]{$pkg}; |
538 | # delete $hook[$idx]{$pkg}; |
337 | } |
539 | # } |
338 | |
540 | |
339 | # remove commands |
541 | # remove commands |
340 | for my $name (keys %command) { |
542 | for my $name (keys %command) { |
341 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
543 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
342 | |
544 | |
… | |
… | |
352 | # remove extcmds |
554 | # remove extcmds |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
555 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
354 | delete $extcmd{$name}; |
556 | delete $extcmd{$name}; |
355 | } |
557 | } |
356 | |
558 | |
357 | if (my $cb = $pkg->can ("on_unload")) { |
559 | if (my $cb = $pkg->can ("unload")) { |
358 | eval { |
560 | eval { |
359 | $cb->($pkg); |
561 | $cb->($pkg); |
360 | 1 |
562 | 1 |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
563 | } or warn "$pkg unloaded, but with errors: $@"; |
362 | } |
564 | } |
… | |
… | |
408 | Symbol::delete_package $k; |
610 | Symbol::delete_package $k; |
409 | } |
611 | } |
410 | |
612 | |
411 | # 4. get rid of safe::, as good as possible |
613 | # 4. get rid of safe::, as good as possible |
412 | Symbol::delete_package "safe::$_" |
614 | Symbol::delete_package "safe::$_" |
413 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
615 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
414 | |
616 | |
415 | # 5. remove register_script_function callbacks |
617 | # 5. remove register_script_function callbacks |
416 | # TODO |
618 | # TODO |
417 | |
619 | |
418 | # 6. unload cf.pm "a bit" |
620 | # 6. unload cf.pm "a bit" |
… | |
… | |
448 | }; |
650 | }; |
449 | } |
651 | } |
450 | }; |
652 | }; |
451 | |
653 | |
452 | ############################################################################# |
654 | ############################################################################# |
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> |
655 | # extcmd framework, basically convert ext <msg> |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
656 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
470 | |
657 | |
471 | sub on_extcmd { |
658 | attach_to_players |
|
|
659 | on_extcmd => sub { |
472 | my ($pl, $buf) = @_; |
660 | my ($pl, $buf) = @_; |
473 | |
661 | |
474 | my $msg = eval { from_json $buf }; |
662 | my $msg = eval { from_json $buf }; |
475 | |
663 | |
476 | if (ref $msg) { |
664 | if (ref $msg) { |
477 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
665 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my %reply = $cb->[0]->($pl, $msg)) { |
666 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | $pl->ext_reply ($msg->{msgid}, %reply); |
667 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
668 | } |
480 | } |
669 | } |
|
|
670 | } else { |
|
|
671 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
481 | } |
672 | } |
482 | } else { |
673 | |
483 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
674 | cf::override; |
484 | } |
675 | }, |
485 | |
676 | ; |
486 | 1 |
|
|
487 | } |
|
|
488 | |
677 | |
489 | ############################################################################# |
678 | ############################################################################# |
490 | # load/save/clean perl data associated with a map |
679 | # load/save/clean perl data associated with a map |
491 | |
680 | |
492 | *cf::mapsupport::on_clean = sub { |
681 | *cf::mapsupport::on_clean = sub { |
493 | my ($map) = @_; |
682 | my ($map) = @_; |
494 | |
683 | |
495 | my $path = $map->tmpname; |
684 | my $path = $map->tmpname; |
496 | defined $path or return; |
685 | defined $path or return; |
497 | |
686 | |
498 | unlink "$path.cfperl"; |
687 | unlink "$path.pst"; |
499 | }; |
688 | }; |
500 | |
689 | |
|
|
690 | # old style persistent data, TODO: remove #d# |
501 | *cf::mapsupport::on_swapin = |
691 | *cf::mapsupport::on_swapin = |
502 | *cf::mapsupport::on_load = sub { |
692 | *cf::mapsupport::on_load = sub { |
503 | my ($map) = @_; |
693 | my ($map) = @_; |
504 | |
694 | |
505 | my $path = $map->tmpname; |
695 | my $path = $map->tmpname; |
… | |
… | |
512 | |
702 | |
513 | $data->{version} <= 1 |
703 | $data->{version} <= 1 |
514 | or return; # too new |
704 | or return; # too new |
515 | |
705 | |
516 | $map->_set_obs ($data->{obs}); |
706 | $map->_set_obs ($data->{obs}); |
517 | }; |
707 | $map->invoke (EVENT_MAP_UPGRADE); |
518 | |
|
|
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 | }; |
708 | }; |
545 | |
709 | |
546 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
710 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
547 | |
711 | |
548 | ############################################################################# |
712 | ############################################################################# |
… | |
… | |
561 | $o->set_ob_key_value ("_perl_data"); |
725 | $o->set_ob_key_value ("_perl_data"); |
562 | |
726 | |
563 | %$o = %{ Storable::thaw pack "H*", $value }; |
727 | %$o = %{ Storable::thaw pack "H*", $value }; |
564 | } |
728 | } |
565 | } |
729 | } |
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 | }, |
730 | }, |
573 | ; |
731 | ; |
574 | |
732 | |
575 | ############################################################################# |
733 | ############################################################################# |
576 | # core extensions - in perl |
734 | # core extensions - in perl |
… | |
… | |
624 | } |
782 | } |
625 | |
783 | |
626 | ############################################################################# |
784 | ############################################################################# |
627 | # map scripting support |
785 | # map scripting support |
628 | |
786 | |
629 | our $safe = new Safe "ext"; |
787 | our $safe = new Safe "safe"; |
630 | our $safe_hole = new Safe::Hole; |
788 | our $safe_hole = new Safe::Hole; |
631 | |
789 | |
632 | $SIG{FPE} = 'IGNORE'; |
790 | $SIG{FPE} = 'IGNORE'; |
633 | |
791 | |
634 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
792 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
654 | $qcode =~ s/\n/\\n/g; |
812 | $qcode =~ s/\n/\\n/g; |
655 | |
813 | |
656 | local $_; |
814 | local $_; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
815 | local @safe::cf::_safe_eval_args = values %vars; |
658 | |
816 | |
659 | $code = |
817 | my $eval = |
660 | "do {\n" |
818 | "do {\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
819 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
820 | . "#line 0 \"{$qcode}\"\n" |
663 | . $code |
821 | . $code |
664 | . "\n}" |
822 | . "\n}" |
665 | ; |
823 | ; |
666 | |
824 | |
667 | sub_generation_inc; |
825 | sub_generation_inc; |
668 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
826 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
669 | sub_generation_inc; |
827 | sub_generation_inc; |
|
|
828 | |
|
|
829 | if ($@) { |
|
|
830 | warn "$@"; |
|
|
831 | warn "while executing safe code '$code'\n"; |
|
|
832 | warn "with arguments " . (join " ", %vars) . "\n"; |
|
|
833 | } |
670 | |
834 | |
671 | wantarray ? @res : $res[0] |
835 | wantarray ? @res : $res[0] |
672 | } |
836 | } |
673 | |
837 | |
674 | sub register_script_function { |
838 | sub register_script_function { |
… | |
… | |
709 | $TICK_WATCHER->at ($NEXT_TICK); |
873 | $TICK_WATCHER->at ($NEXT_TICK); |
710 | $TICK_WATCHER->start; |
874 | $TICK_WATCHER->start; |
711 | }, |
875 | }, |
712 | ); |
876 | ); |
713 | |
877 | |
|
|
878 | _reload_2; |
|
|
879 | |
714 | 1 |
880 | 1 |
715 | |
881 | |