… | |
… | |
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 | _init_vars; |
|
|
17 | |
16 | our %COMMAND = (); |
18 | our %COMMAND = (); |
17 | our @EVENT; |
19 | our @EVENT; |
18 | our @PLUGIN_EVENT; |
|
|
19 | our %PROP_TYPE; |
|
|
20 | 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 | |
35 | print STDERR "cfperl: $msg"; |
32 | print STDERR "cfperl: $msg"; |
36 | LOG llevError, "cfperl: $msg"; |
33 | LOG llevError, "cfperl: $msg"; |
37 | }; |
34 | }; |
38 | } |
35 | } |
39 | |
36 | |
40 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
|
|
41 | |
|
|
42 | # generate property mutators |
|
|
43 | sub prop_gen { |
|
|
44 | my ($prefix, $class) = @_; |
|
|
45 | |
|
|
46 | no strict 'refs'; |
|
|
47 | |
|
|
48 | for my $prop (keys %PROP_TYPE) { |
|
|
49 | $prop =~ /^\Q$prefix\E_(.*$)/ or next; |
|
|
50 | my $sub = lc $1; |
|
|
51 | |
|
|
52 | my $type = $PROP_TYPE{$prop}; |
|
|
53 | my $idx = $PROP_IDX {$prop}; |
|
|
54 | |
|
|
55 | *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { |
|
|
56 | $_[0]->get_property ($type, $idx) |
|
|
57 | }; |
|
|
58 | |
|
|
59 | *{"$class\::set_$sub"} = sub { |
|
|
60 | $_[0]->set_property ($type, $idx, $_[1]); |
|
|
61 | } unless $ignore_set{$prop}; |
|
|
62 | } |
|
|
63 | } |
|
|
64 | |
|
|
65 | # auto-generate most of the API |
|
|
66 | |
|
|
67 | prop_gen OBJECT_PROP => "cf::object"; |
|
|
68 | # CFAPI_OBJECT_ANIMATION? |
|
|
69 | prop_gen PLAYER_PROP => "cf::object::player"; |
|
|
70 | |
|
|
71 | prop_gen MAP_PROP => "cf::map"; |
|
|
72 | prop_gen ARCH_PROP => "cf::arch"; |
|
|
73 | |
|
|
74 | # guessed hierarchies |
|
|
75 | |
|
|
76 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
37 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
77 | @safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
38 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
39 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
40 | # 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)) { |
41 | 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'; |
42 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
43 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
44 | } |
85 | |
45 | |
86 | $Event::DIED = sub { |
46 | $Event::DIED = sub { |
… | |
… | |
92 | my @hook; |
52 | my @hook; |
93 | my %command; |
53 | my %command; |
94 | my %extcmd; |
54 | my %extcmd; |
95 | |
55 | |
96 | ############################################################################# |
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 | ############################################################################# |
97 | # "new" plug-in system |
72 | # "new" plug-in system |
98 | |
73 | |
99 | =item cf::object::attach ... # NYI |
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. |
100 | |
103 | |
101 | =item cf::attach_global ... |
104 | =item cf::attach_global ... |
102 | |
105 | |
103 | =item cf::attach_to_type ... |
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. |
104 | |
144 | |
105 | =item cf::attach_to_objects ... |
145 | =item cf::attach_to_objects ... |
106 | |
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 | |
107 | =item cf::attach_to_players ... |
151 | =item cf::attach_to_players ... |
108 | |
152 | |
|
|
153 | Attach handlers to all players. |
|
|
154 | |
109 | =item cf::attach_to_maps ... |
155 | =item cf::attach_to_maps ... |
110 | |
156 | |
111 | prio => $number, # lower is earlier |
157 | Attach handlers to all maps. |
112 | on_xxx => \&cb, |
158 | |
113 | package => package::, |
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. |
114 | |
173 | |
115 | =cut |
174 | =cut |
116 | |
175 | |
117 | # the following variables are defined in .xs and must not be re-created |
176 | # the following variables are defined in .xs and must not be re-created |
118 | our @CB_GLOBAL = (); # registry for all global events |
177 | our @CB_GLOBAL = (); # registry for all global events |
119 | our @CB_OBJECT = (); |
178 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
120 | our @CB_PLAYER = (); |
179 | our @CB_PLAYER = (); |
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
180 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
122 | our @CB_MAP = (); |
181 | our @CB_MAP = (); |
123 | |
182 | |
|
|
183 | my %attachment; |
|
|
184 | |
124 | sub _attach_cb($\%$$$) { |
185 | sub _attach_cb($\%$$$) { |
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
186 | my ($registry, $undo, $event, $prio, $cb) = @_; |
126 | |
187 | |
127 | use sort 'stable'; |
188 | use sort 'stable'; |
128 | |
189 | |
… | |
… | |
136 | } |
197 | } |
137 | |
198 | |
138 | # attach handles attaching event callbacks |
199 | # attach handles attaching event callbacks |
139 | # the only thing the caller has to do is pass the correct |
200 | # the only thing the caller has to do is pass the correct |
140 | # registry (== where the callback attaches to). |
201 | # registry (== where the callback attaches to). |
141 | sub _attach(\@$\@) { |
202 | sub _attach(\@$@) { |
142 | my ($registry, $klass, $arg) = @_; |
203 | my ($registry, $klass, @arg) = @_; |
143 | |
204 | |
144 | my $prio = 0; |
205 | my $prio = 0; |
145 | |
206 | |
146 | my %undo = ( |
207 | my %undo = ( |
147 | registry => $registry, |
208 | registry => $registry, |
148 | cb => [], |
209 | cb => [], |
149 | ); |
210 | ); |
150 | |
211 | |
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
212 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
152 | |
213 | |
153 | while (@$arg) { |
214 | while (@arg) { |
154 | my $type = shift @$arg; |
215 | my $type = shift @arg; |
155 | |
216 | |
156 | if ($type eq "prio") { |
217 | if ($type eq "prio") { |
157 | $prio = shift @$arg; |
218 | $prio = shift @arg; |
158 | |
219 | |
159 | } elsif ($type eq "package") { |
220 | } elsif ($type eq "package") { |
160 | my $pkg = shift @$arg; |
221 | my $pkg = shift @arg; |
161 | |
222 | |
162 | while (my ($name, $id) = each %cb_id) { |
223 | while (my ($name, $id) = each %cb_id) { |
163 | if (my $cb = $pkg->can ($name)) { |
224 | if (my $cb = $pkg->can ($name)) { |
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
225 | _attach_cb $registry, %undo, $id, $prio, $cb; |
165 | } |
226 | } |
166 | } |
227 | } |
167 | |
228 | |
168 | } elsif (exists $cb_id{$type}) { |
229 | } elsif (exists $cb_id{$type}) { |
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
230 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
170 | |
231 | |
171 | } elsif (ref $type) { |
232 | } elsif (ref $type) { |
172 | warn "attaching objects not supported, ignoring.\n"; |
233 | warn "attaching objects not supported, ignoring.\n"; |
173 | |
234 | |
174 | } else { |
235 | } else { |
175 | shift @$arg; |
236 | shift @arg; |
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
237 | warn "attach argument '$type' not supported, ignoring.\n"; |
177 | } |
238 | } |
178 | } |
239 | } |
179 | |
240 | |
180 | \%undo |
241 | \%undo |
181 | } |
242 | } |
182 | |
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 | |
183 | sub cf::object::attach { |
270 | *cf::object::attach = |
184 | die; |
271 | *cf::player::attach = |
185 | } |
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 | }; |
186 | |
295 | |
187 | sub attach_global { |
296 | sub attach_global { |
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
297 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
189 | } |
298 | } |
190 | |
299 | |
191 | sub attach_to_type { |
300 | sub attach_to_type { |
192 | my $type = shift; |
301 | my $type = shift; |
|
|
302 | my $subtype = shift; |
|
|
303 | |
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
304 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
194 | } |
305 | } |
195 | |
306 | |
196 | sub attach_to_objects { |
307 | sub attach_to_objects { |
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
308 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
198 | } |
309 | } |
… | |
… | |
203 | |
314 | |
204 | sub attach_to_maps { |
315 | sub attach_to_maps { |
205 | _attach @CB_MAP, KLASS_MAP, @_ |
316 | _attach @CB_MAP, KLASS_MAP, @_ |
206 | } |
317 | } |
207 | |
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 | |
208 | our $override; |
337 | our $override; |
|
|
338 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
209 | |
339 | |
210 | sub override() { |
340 | sub override { |
211 | $override = 1 |
341 | $override = 1; |
|
|
342 | @invoke_results = (); |
212 | } |
343 | } |
213 | |
344 | |
214 | sub invoke { |
345 | sub do_invoke { |
215 | my $event = shift; |
346 | my $event = shift; |
216 | my $callbacks = shift; |
347 | my $callbacks = shift; |
217 | |
348 | |
|
|
349 | @invoke_results = (); |
|
|
350 | |
218 | local $override; |
351 | local $override; |
219 | |
352 | |
220 | for (@$callbacks) { |
353 | for (@$callbacks) { |
221 | eval { &{$_->[1]} }; |
354 | eval { &{$_->[1]} }; |
222 | |
355 | |
223 | if ($@) { |
356 | if ($@) { |
224 | warn "$@"; |
357 | warn "$@"; |
225 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
358 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
226 | override; |
359 | override; |
227 | } |
360 | } |
228 | |
361 | |
229 | return 1 if $override; |
362 | return 1 if $override; |
230 | } |
363 | } |
231 | |
364 | |
232 | 0 |
365 | 0 |
233 | } |
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 | ; |
234 | |
507 | |
235 | ############################################################################# |
508 | ############################################################################# |
236 | # old plug-in events |
509 | # old plug-in events |
237 | |
510 | |
238 | sub inject_event { |
511 | sub inject_event { |
… | |
… | |
284 | } |
557 | } |
285 | |
558 | |
286 | sub register { |
559 | sub register { |
287 | my ($base, $pkg) = @_; |
560 | my ($base, $pkg) = @_; |
288 | |
561 | |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
562 | #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 | } |
563 | } |
296 | |
564 | |
297 | sub load_extension { |
565 | sub load_extension { |
298 | my ($path) = @_; |
566 | my ($path) = @_; |
299 | |
567 | |
… | |
… | |
330 | my ($pkg) = @_; |
598 | my ($pkg) = @_; |
331 | |
599 | |
332 | warn "removing extension $pkg\n"; |
600 | warn "removing extension $pkg\n"; |
333 | |
601 | |
334 | # remove hooks |
602 | # remove hooks |
|
|
603 | #TODO |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
604 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
336 | delete $hook[$idx]{$pkg}; |
605 | # delete $hook[$idx]{$pkg}; |
337 | } |
606 | # } |
338 | |
607 | |
339 | # remove commands |
608 | # remove commands |
340 | for my $name (keys %command) { |
609 | for my $name (keys %command) { |
341 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
610 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
342 | |
611 | |
… | |
… | |
352 | # remove extcmds |
621 | # remove extcmds |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
622 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
354 | delete $extcmd{$name}; |
623 | delete $extcmd{$name}; |
355 | } |
624 | } |
356 | |
625 | |
357 | if (my $cb = $pkg->can ("on_unload")) { |
626 | if (my $cb = $pkg->can ("unload")) { |
358 | eval { |
627 | eval { |
359 | $cb->($pkg); |
628 | $cb->($pkg); |
360 | 1 |
629 | 1 |
361 | } or warn "$pkg unloaded, but with errors: $@"; |
630 | } or warn "$pkg unloaded, but with errors: $@"; |
362 | } |
631 | } |
… | |
… | |
374 | 1 |
643 | 1 |
375 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
376 | } |
645 | } |
377 | } |
646 | } |
378 | |
647 | |
379 | sub _perl_reload(&) { |
|
|
380 | my ($msg) = @_; |
|
|
381 | |
|
|
382 | $msg->("reloading..."); |
|
|
383 | |
|
|
384 | eval { |
|
|
385 | # 1. cancel all watchers |
|
|
386 | $_->cancel for Event::all_watchers; |
|
|
387 | |
|
|
388 | # 2. unload all extensions |
|
|
389 | for (@exts) { |
|
|
390 | $msg->("unloading <$_>"); |
|
|
391 | unload_extension $_; |
|
|
392 | } |
|
|
393 | |
|
|
394 | # 3. unload all modules loaded from $LIBDIR |
|
|
395 | while (my ($k, $v) = each %INC) { |
|
|
396 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
397 | |
|
|
398 | $msg->("removing <$k>"); |
|
|
399 | delete $INC{$k}; |
|
|
400 | |
|
|
401 | $k =~ s/\.pm$//; |
|
|
402 | $k =~ s/\//::/g; |
|
|
403 | |
|
|
404 | if (my $cb = $k->can ("unload_module")) { |
|
|
405 | $cb->(); |
|
|
406 | } |
|
|
407 | |
|
|
408 | Symbol::delete_package $k; |
|
|
409 | } |
|
|
410 | |
|
|
411 | # 4. get rid of safe::, as good as possible |
|
|
412 | Symbol::delete_package "safe::$_" |
|
|
413 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
414 | |
|
|
415 | # 5. remove register_script_function callbacks |
|
|
416 | # TODO |
|
|
417 | |
|
|
418 | # 6. unload cf.pm "a bit" |
|
|
419 | delete $INC{"cf.pm"}; |
|
|
420 | |
|
|
421 | # don't, removes xs symbols, too, |
|
|
422 | # and global variables created in xs |
|
|
423 | #Symbol::delete_package __PACKAGE__; |
|
|
424 | |
|
|
425 | # 7. reload cf.pm |
|
|
426 | $msg->("reloading cf.pm"); |
|
|
427 | require cf; |
|
|
428 | }; |
|
|
429 | $msg->($@) if $@; |
|
|
430 | |
|
|
431 | $msg->("reloaded"); |
|
|
432 | }; |
|
|
433 | |
|
|
434 | sub perl_reload() { |
|
|
435 | _perl_reload { |
|
|
436 | warn $_[0]; |
|
|
437 | print "$_[0]\n"; |
|
|
438 | }; |
|
|
439 | } |
|
|
440 | |
|
|
441 | register_command "perl-reload", 0, sub { |
|
|
442 | my ($who, $arg) = @_; |
|
|
443 | |
|
|
444 | if ($who->flag (FLAG_WIZ)) { |
|
|
445 | _perl_reload { |
|
|
446 | warn $_[0]; |
|
|
447 | $who->message ($_[0]); |
|
|
448 | }; |
|
|
449 | } |
|
|
450 | }; |
|
|
451 | |
|
|
452 | ############################################################################# |
|
|
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 | ############################################################################# |
648 | ############################################################################# |
468 | # extcmd framework, basically convert ext <msg> |
649 | # extcmd framework, basically convert ext <msg> |
469 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
470 | |
651 | |
471 | sub on_extcmd { |
652 | attach_to_players |
|
|
653 | on_extcmd => sub { |
472 | my ($pl, $buf) = @_; |
654 | my ($pl, $buf) = @_; |
473 | |
655 | |
474 | my $msg = eval { from_json $buf }; |
656 | my $msg = eval { from_json $buf }; |
475 | |
657 | |
476 | if (ref $msg) { |
658 | if (ref $msg) { |
477 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
659 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
478 | if (my %reply = $cb->[0]->($pl, $msg)) { |
660 | if (my %reply = $cb->[0]->($pl, $msg)) { |
479 | $pl->ext_reply ($msg->{msgid}, %reply); |
661 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
662 | } |
480 | } |
663 | } |
481 | } |
|
|
482 | } else { |
664 | } else { |
483 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
665 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
666 | } |
|
|
667 | |
|
|
668 | cf::override; |
484 | } |
669 | }, |
485 | |
670 | ; |
486 | 1 |
|
|
487 | } |
|
|
488 | |
671 | |
489 | ############################################################################# |
672 | ############################################################################# |
490 | # load/save/clean perl data associated with a map |
673 | # load/save/clean perl data associated with a map |
491 | |
674 | |
492 | *cf::mapsupport::on_clean = sub { |
675 | *cf::mapsupport::on_clean = sub { |
493 | my ($map) = @_; |
676 | my ($map) = @_; |
494 | |
677 | |
495 | my $path = $map->tmpname; |
678 | my $path = $map->tmpname; |
496 | defined $path or return; |
679 | defined $path or return; |
497 | |
680 | |
498 | unlink "$path.cfperl"; |
681 | unlink "$path.pst"; |
499 | }; |
|
|
500 | |
|
|
501 | *cf::mapsupport::on_swapin = |
|
|
502 | *cf::mapsupport::on_load = sub { |
|
|
503 | my ($map) = @_; |
|
|
504 | |
|
|
505 | my $path = $map->tmpname; |
|
|
506 | $path = $map->path unless defined $path; |
|
|
507 | |
|
|
508 | open my $fh, "<:raw", "$path.cfperl" |
|
|
509 | or return; # no perl data |
|
|
510 | |
|
|
511 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
512 | |
|
|
513 | $data->{version} <= 1 |
|
|
514 | or return; # too new |
|
|
515 | |
|
|
516 | $map->_set_obs ($data->{obs}); |
|
|
517 | }; |
|
|
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 | }; |
682 | }; |
545 | |
683 | |
546 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
684 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
547 | |
685 | |
548 | ############################################################################# |
686 | ############################################################################# |
… | |
… | |
550 | |
688 | |
551 | sub all_objects(@) { |
689 | sub all_objects(@) { |
552 | @_, map all_objects ($_->inv), @_ |
690 | @_, map all_objects ($_->inv), @_ |
553 | } |
691 | } |
554 | |
692 | |
|
|
693 | # TODO: compatibility cruft, remove when no longer needed |
555 | attach_to_players |
694 | attach_to_players |
556 | on_load => sub { |
695 | on_load => sub { |
557 | my ($pl, $path) = @_; |
696 | my ($pl, $path) = @_; |
558 | |
697 | |
559 | for my $o (all_objects $pl->ob) { |
698 | for my $o (all_objects $pl->ob) { |
… | |
… | |
561 | $o->set_ob_key_value ("_perl_data"); |
700 | $o->set_ob_key_value ("_perl_data"); |
562 | |
701 | |
563 | %$o = %{ Storable::thaw pack "H*", $value }; |
702 | %$o = %{ Storable::thaw pack "H*", $value }; |
564 | } |
703 | } |
565 | } |
704 | } |
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 | }, |
705 | }, |
573 | ; |
706 | ; |
574 | |
707 | |
575 | ############################################################################# |
708 | ############################################################################# |
576 | # core extensions - in perl |
709 | # core extensions - in perl |
… | |
… | |
624 | } |
757 | } |
625 | |
758 | |
626 | ############################################################################# |
759 | ############################################################################# |
627 | # map scripting support |
760 | # map scripting support |
628 | |
761 | |
629 | our $safe = new Safe "ext"; |
762 | our $safe = new Safe "safe"; |
630 | our $safe_hole = new Safe::Hole; |
763 | our $safe_hole = new Safe::Hole; |
631 | |
764 | |
632 | $SIG{FPE} = 'IGNORE'; |
765 | $SIG{FPE} = 'IGNORE'; |
633 | |
766 | |
634 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
767 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
… | |
… | |
654 | $qcode =~ s/\n/\\n/g; |
787 | $qcode =~ s/\n/\\n/g; |
655 | |
788 | |
656 | local $_; |
789 | local $_; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
790 | local @safe::cf::_safe_eval_args = values %vars; |
658 | |
791 | |
659 | $code = |
792 | my $eval = |
660 | "do {\n" |
793 | "do {\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
794 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
795 | . "#line 0 \"{$qcode}\"\n" |
663 | . $code |
796 | . $code |
664 | . "\n}" |
797 | . "\n}" |
665 | ; |
798 | ; |
666 | |
799 | |
667 | sub_generation_inc; |
800 | sub_generation_inc; |
668 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
801 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
669 | sub_generation_inc; |
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 | } |
670 | |
809 | |
671 | wantarray ? @res : $res[0] |
810 | wantarray ? @res : $res[0] |
672 | } |
811 | } |
673 | |
812 | |
674 | sub register_script_function { |
813 | sub register_script_function { |
… | |
… | |
677 | no strict 'refs'; |
816 | no strict 'refs'; |
678 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
679 | } |
818 | } |
680 | |
819 | |
681 | ############################################################################# |
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 | unless ($pid = fork) { |
|
|
878 | $db->{_meta}{version} = 1; |
|
|
879 | Storable::nstore $db, "$path~"; |
|
|
880 | rename "$path~", $path; |
|
|
881 | cf::_exit 0 if defined $pid; |
|
|
882 | } |
|
|
883 | } |
|
|
884 | |
|
|
885 | my $dirty; |
|
|
886 | |
|
|
887 | sub db_sync() { |
|
|
888 | db_save if $dirty; |
|
|
889 | undef $dirty; |
|
|
890 | } |
|
|
891 | |
|
|
892 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
|
|
893 | db_sync; |
|
|
894 | }); |
|
|
895 | |
|
|
896 | sub db_dirty() { |
|
|
897 | $dirty = 1; |
|
|
898 | $idle->start; |
|
|
899 | } |
|
|
900 | |
|
|
901 | sub db_get($;$) { |
|
|
902 | @_ >= 2 |
|
|
903 | ? $db->{$_[0]}{$_[1]} |
|
|
904 | : ($db->{$_[0]} ||= { }) |
|
|
905 | } |
|
|
906 | |
|
|
907 | sub db_put($$;$) { |
|
|
908 | if (@_ >= 3) { |
|
|
909 | $db->{$_[0]}{$_[1]} = $_[2]; |
|
|
910 | } else { |
|
|
911 | $db->{$_[0]} = $_[1]; |
|
|
912 | } |
|
|
913 | db_dirty; |
|
|
914 | } |
|
|
915 | } |
|
|
916 | |
|
|
917 | ############################################################################# |
682 | # the server's main() |
918 | # the server's main() |
683 | |
919 | |
684 | sub main { |
920 | sub main { |
|
|
921 | db_load; |
|
|
922 | load_extensions; |
685 | Event::loop; |
923 | Event::loop; |
686 | } |
924 | } |
687 | |
925 | |
688 | ############################################################################# |
926 | ############################################################################# |
689 | # initialisation |
927 | # initialisation |
690 | |
928 | |
|
|
929 | sub _perl_reload(&) { |
|
|
930 | my ($msg) = @_; |
|
|
931 | |
|
|
932 | $msg->("reloading..."); |
|
|
933 | |
|
|
934 | eval { |
|
|
935 | # cancel all watchers |
|
|
936 | $_->cancel for Event::all_watchers; |
|
|
937 | |
|
|
938 | # unload all extensions |
|
|
939 | for (@exts) { |
|
|
940 | $msg->("unloading <$_>"); |
|
|
941 | unload_extension $_; |
|
|
942 | } |
|
|
943 | |
|
|
944 | # unload all modules loaded from $LIBDIR |
|
|
945 | while (my ($k, $v) = each %INC) { |
|
|
946 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
947 | |
|
|
948 | $msg->("removing <$k>"); |
|
|
949 | delete $INC{$k}; |
|
|
950 | |
|
|
951 | $k =~ s/\.pm$//; |
|
|
952 | $k =~ s/\//::/g; |
|
|
953 | |
|
|
954 | if (my $cb = $k->can ("unload_module")) { |
|
|
955 | $cb->(); |
|
|
956 | } |
|
|
957 | |
|
|
958 | Symbol::delete_package $k; |
|
|
959 | } |
|
|
960 | |
|
|
961 | # sync database to disk |
|
|
962 | cf::db_sync; |
|
|
963 | |
|
|
964 | # get rid of safe::, as good as possible |
|
|
965 | Symbol::delete_package "safe::$_" |
|
|
966 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
967 | |
|
|
968 | # remove register_script_function callbacks |
|
|
969 | # TODO |
|
|
970 | |
|
|
971 | # unload cf.pm "a bit" |
|
|
972 | delete $INC{"cf.pm"}; |
|
|
973 | |
|
|
974 | # don't, removes xs symbols, too, |
|
|
975 | # and global variables created in xs |
|
|
976 | #Symbol::delete_package __PACKAGE__; |
|
|
977 | |
|
|
978 | # reload cf.pm |
|
|
979 | $msg->("reloading cf.pm"); |
|
|
980 | require cf; |
|
|
981 | |
|
|
982 | # load database again |
|
|
983 | cf::db_load; |
|
|
984 | |
|
|
985 | # load extensions |
|
|
986 | $msg->("load extensions"); |
|
|
987 | cf::load_extensions; |
|
|
988 | |
|
|
989 | # reattach attachments to objects |
|
|
990 | $msg->("reattach"); |
|
|
991 | _global_reattach; |
|
|
992 | }; |
|
|
993 | $msg->($@) if $@; |
|
|
994 | |
|
|
995 | $msg->("reloaded"); |
|
|
996 | }; |
|
|
997 | |
|
|
998 | sub perl_reload() { |
|
|
999 | _perl_reload { |
|
|
1000 | warn $_[0]; |
|
|
1001 | print "$_[0]\n"; |
|
|
1002 | }; |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | register_command "perl-reload", 0, sub { |
|
|
1006 | my ($who, $arg) = @_; |
|
|
1007 | |
|
|
1008 | if ($who->flag (FLAG_WIZ)) { |
|
|
1009 | _perl_reload { |
|
|
1010 | warn $_[0]; |
|
|
1011 | $who->message ($_[0]); |
|
|
1012 | }; |
|
|
1013 | } |
|
|
1014 | }; |
|
|
1015 | |
691 | register "<global>", __PACKAGE__; |
1016 | register "<global>", __PACKAGE__; |
692 | |
1017 | |
693 | unshift @INC, $LIBDIR; |
1018 | unshift @INC, $LIBDIR; |
694 | |
|
|
695 | load_extensions; |
|
|
696 | |
1019 | |
697 | $TICK_WATCHER = Event->timer ( |
1020 | $TICK_WATCHER = Event->timer ( |
698 | prio => 1, |
1021 | prio => 1, |
699 | at => $NEXT_TICK || 1, |
1022 | at => $NEXT_TICK || 1, |
700 | cb => sub { |
1023 | cb => sub { |