… | |
… | |
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 %PROP_TYPE; |
20 | our %PROP_TYPE; |
19 | our %PROP_IDX; |
21 | our %PROP_IDX; |
20 | our $LIBDIR = maps_directory "perl"; |
22 | our $LIBDIR = maps_directory "perl"; |
… | |
… | |
103 | } |
105 | } |
104 | |
106 | |
105 | ############################################################################# |
107 | ############################################################################# |
106 | # "new" plug-in system |
108 | # "new" plug-in system |
107 | |
109 | |
108 | =item $object->attach ($attachment, ...) |
110 | =item $object->attach ($attachment, key => $value...) |
109 | |
111 | |
110 | Attach a pre-registered attachment to an object. |
112 | Attach a pre-registered attachment to an object. |
111 | |
113 | |
112 | =item $player->attach ($attachment, ...) |
114 | =item $player->attach ($attachment, key => $value...) |
113 | |
115 | |
114 | Attach a pre-registered attachment to a player. |
116 | Attach a pre-registered attachment to a player. |
115 | |
117 | |
116 | =item $map->attach ($attachment, ...) # not yet persistent |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
117 | |
119 | |
118 | Attach a pre-registered attachment to a map. |
120 | Attach a pre-registered attachment to a map. |
119 | |
121 | |
120 | =item cf::attach_global ... |
122 | =item cf::attach_global ... |
121 | |
123 | |
… | |
… | |
150 | package and register them. Only handlers for eevents supported by the |
152 | package and register them. Only handlers for eevents supported by the |
151 | object/class are recognised. |
153 | object/class are recognised. |
152 | |
154 | |
153 | =back |
155 | =back |
154 | |
156 | |
155 | =item cf::attach_to_type $object_type, ... |
157 | =item cf::attach_to_type $object_type, $subtype, ... |
156 | |
158 | |
157 | Attach handlers for a specific object type (e.g. TRANSPORT). |
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. |
158 | |
162 | |
159 | =item cf::attach_to_objects ... |
163 | =item cf::attach_to_objects ... |
160 | |
164 | |
161 | Attach handlers to all objects. Do not use this except for debugging or |
165 | 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 |
166 | very rare events, as handlers are (obviously) called for I<all> objects in |
… | |
… | |
241 | |
245 | |
242 | \%undo |
246 | \%undo |
243 | } |
247 | } |
244 | |
248 | |
245 | sub _attach_attachment { |
249 | sub _attach_attachment { |
246 | my ($klass, $obj, $name, @args) = q_; |
250 | my ($obj, $name, %arg) = @_; |
247 | |
251 | |
248 | my $res; |
252 | my $res; |
249 | |
253 | |
250 | if (my $attach = $attachment{$name}) { |
254 | if (my $attach = $attachment{$name}) { |
251 | my $registry = $obj->registry; |
255 | my $registry = $obj->registry; |
252 | |
256 | |
|
|
257 | for (@$attach) { |
|
|
258 | my ($klass, @attach) = @$_; |
253 | $res = _attach @$registry, $klass, @$attach; |
259 | $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 | } |
260 | } |
|
|
261 | |
|
|
262 | $obj->{$name} = \%arg; |
264 | } else { |
263 | } else { |
265 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
264 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
266 | } |
265 | } |
267 | |
266 | |
268 | push @{$obj->{_attachment}}, $name; |
267 | push @{$obj->{_attachment}}, $name; |
… | |
… | |
270 | $res->{attachment} = $name; |
269 | $res->{attachment} = $name; |
271 | $res |
270 | $res |
272 | } |
271 | } |
273 | |
272 | |
274 | sub cf::object::attach { |
273 | sub cf::object::attach { |
275 | my ($obj, $name, @args) = @_; |
274 | my ($obj, $name, %arg) = @_; |
276 | |
275 | |
277 | _attach_attachment KLASS_OBJECT, $obj, $name, @args; |
276 | _attach_attachment $obj, $name, %arg; |
278 | } |
277 | } |
279 | |
278 | |
280 | sub cf::player::attach { |
279 | sub cf::player::attach { |
281 | my ($obj, $name, @args) = @_; |
280 | my ($obj, $name, %arg) = @_; |
282 | |
281 | |
283 | _attach_attachment KLASS_PLAYER, $obj, $name, @args; |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
284 | } |
283 | } |
285 | |
284 | |
286 | sub cf::map::attach { |
285 | sub cf::map::attach { |
287 | my ($obj, $name, @args) = @_; |
286 | my ($obj, $name, %arg) = @_; |
288 | |
287 | |
289 | _attach_attachment KLASS_MAP, $obj, $name, @args; |
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
290 | } |
289 | } |
291 | |
290 | |
292 | sub attach_global { |
291 | sub attach_global { |
293 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
294 | } |
293 | } |
295 | |
294 | |
296 | sub attach_to_type { |
295 | sub attach_to_type { |
297 | my $type = shift; |
296 | my $type = shift; |
|
|
297 | my $subtype = shift; |
298 | |
298 | |
299 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
299 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
300 | } |
300 | } |
301 | |
301 | |
302 | sub attach_to_objects { |
302 | sub attach_to_objects { |
303 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
303 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
304 | } |
304 | } |
… | |
… | |
312 | } |
312 | } |
313 | |
313 | |
314 | sub register_attachment { |
314 | sub register_attachment { |
315 | my $name = shift; |
315 | my $name = shift; |
316 | |
316 | |
317 | $attachment{$name} = [@_]; |
317 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
318 | } |
318 | } |
319 | |
319 | |
320 | our $override; |
320 | our $override; |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
322 | |
322 | |
… | |
… | |
356 | |
356 | |
357 | $data = from_json $data; |
357 | $data = from_json $data; |
358 | |
358 | |
359 | for (@$data) { |
359 | for (@$data) { |
360 | my ($name, $args) = @$_; |
360 | my ($name, $args) = @$_; |
361 | attach $obj, $name, @{$args || [] }; |
361 | |
|
|
362 | $obj->attach ($name, %{$args || {} }); |
362 | } |
363 | } |
363 | } |
364 | } |
364 | |
365 | |
365 | # basically do the same as instantiate, without calling instantiate |
366 | # basically do the same as instantiate, without calling instantiate |
366 | sub reattach { |
367 | sub reattach { |
367 | warn "reattach<@_>\n";#d# |
|
|
368 | my ($obj) = @_; |
368 | my ($obj) = @_; |
369 | my $registry = $obj->registry; |
369 | my $registry = $obj->registry; |
370 | |
370 | |
|
|
371 | @$registry = (); |
|
|
372 | |
|
|
373 | delete $obj->{_attachment} unless @{ $obj->{_attachment} || [] }; |
|
|
374 | |
371 | for my $name (@{ $obj->{_attachment} }) { |
375 | for my $name (@{ $obj->{_attachment} || [] }) { |
372 | if (my $attach = $attachment{$name}) { |
376 | if (my $attach = $attachment{$name}) { |
|
|
377 | for (@$attach) { |
|
|
378 | my ($klass, @attach) = @$_; |
373 | _attach @$registry, KLASS_OBJECT, @$attach; |
379 | _attach @$registry, $klass, @attach; |
|
|
380 | } |
374 | } else { |
381 | } else { |
375 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
382 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
376 | } |
383 | } |
377 | } |
384 | } |
378 | |
|
|
379 | warn "reattach<@_, $_>\n"; |
|
|
380 | } |
385 | } |
381 | |
386 | |
382 | sub object_freezer_save { |
387 | sub object_freezer_save { |
383 | my ($filename, $objs) = @_; |
388 | my ($filename, $objs) = @_; |
384 | warn "freeze $filename\n";#d# |
|
|
385 | use Data::Dumper; print Dumper $objs; |
|
|
386 | |
389 | |
387 | $filename .= ".pst"; |
390 | $filename .= ".pst"; |
388 | |
391 | |
389 | if (@$objs) { |
392 | if (@$objs) { |
390 | open my $fh, ">:raw", "$filename~"; |
393 | open my $fh, ">:raw", "$filename~"; |
… | |
… | |
398 | } |
401 | } |
399 | |
402 | |
400 | sub object_thawer_load { |
403 | sub object_thawer_load { |
401 | my ($filename) = @_; |
404 | my ($filename) = @_; |
402 | |
405 | |
403 | warn "thaw $filename\n";#d# |
|
|
404 | |
|
|
405 | open my $fh, "<:raw:perlio", "$filename.pst" |
406 | open my $fh, "<:raw:perlio", "$filename.pst" |
406 | or return; |
407 | or return; |
407 | |
408 | |
408 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
409 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
409 | } |
410 | } |
… | |
… | |
412 | prio => -1000000, |
413 | prio => -1000000, |
413 | on_clone => sub { |
414 | on_clone => sub { |
414 | my ($src, $dst) = @_; |
415 | my ($src, $dst) = @_; |
415 | |
416 | |
416 | @{$dst->registry} = @{$src->registry}; |
417 | @{$dst->registry} = @{$src->registry}; |
417 | warn "registry clone ", join ":", @{$src->registry};#d# |
|
|
418 | |
418 | |
419 | %$dst = %$src; |
419 | %$dst = %$src; |
420 | |
420 | |
421 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
421 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
422 | if exists $src->{_attachment}; |
422 | if exists $src->{_attachment}; |
423 | |
|
|
424 | warn "clone<@_>\n";#d# |
|
|
425 | }, |
423 | }, |
426 | ; |
424 | ; |
427 | |
425 | |
428 | ############################################################################# |
426 | ############################################################################# |
429 | # old plug-in events |
427 | # old plug-in events |
… | |
… | |
859 | $TICK_WATCHER->at ($NEXT_TICK); |
857 | $TICK_WATCHER->at ($NEXT_TICK); |
860 | $TICK_WATCHER->start; |
858 | $TICK_WATCHER->start; |
861 | }, |
859 | }, |
862 | ); |
860 | ); |
863 | |
861 | |
|
|
862 | _reload_2; |
|
|
863 | |
864 | 1 |
864 | 1 |
865 | |
865 | |