… | |
… | |
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 | |
|
|
26 | our %CFG; |
|
|
27 | |
|
|
28 | ############################################################################# |
|
|
29 | |
|
|
30 | =head2 GLOBAL VARIABLES |
|
|
31 | |
|
|
32 | =over 4 |
|
|
33 | |
|
|
34 | =item $cf::LIBDIR |
|
|
35 | |
|
|
36 | The perl library directory, where extensions and cf-specific modules can |
|
|
37 | be found. It will be added to C<@INC> automatically. |
|
|
38 | |
|
|
39 | =item $cf::TICK |
|
|
40 | |
|
|
41 | The interval between server ticks, in seconds. |
|
|
42 | |
|
|
43 | =item %cf::CFG |
|
|
44 | |
|
|
45 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
|
|
46 | from wherever your confdir points to. |
|
|
47 | |
|
|
48 | =back |
|
|
49 | |
|
|
50 | =cut |
|
|
51 | |
27 | BEGIN { |
52 | BEGIN { |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
|
|
29 | |
|
|
30 | *CORE::GLOBAL::warn = sub { |
53 | *CORE::GLOBAL::warn = sub { |
31 | my $msg = join "", @_; |
54 | my $msg = join "", @_; |
32 | $msg .= "\n" |
55 | $msg .= "\n" |
33 | unless $msg =~ /\n$/; |
56 | unless $msg =~ /\n$/; |
34 | |
57 | |
35 | print STDERR "cfperl: $msg"; |
58 | print STDERR "cfperl: $msg"; |
36 | LOG llevError, "cfperl: $msg"; |
59 | LOG llevError, "cfperl: $msg"; |
37 | }; |
60 | }; |
38 | } |
61 | } |
39 | |
62 | |
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'; |
63 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
77 | @safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
64 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
65 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
66 | # 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)) { |
67 | 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'; |
68 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
69 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
70 | } |
85 | |
71 | |
86 | $Event::DIED = sub { |
72 | $Event::DIED = sub { |
… | |
… | |
91 | my @exts; |
77 | my @exts; |
92 | my @hook; |
78 | my @hook; |
93 | my %command; |
79 | my %command; |
94 | my %extcmd; |
80 | my %extcmd; |
95 | |
81 | |
96 | ############################################################################# |
82 | =head2 UTILITY FUNCTIONS |
97 | # object support |
|
|
98 | |
83 | |
99 | sub reattach { |
84 | =over 4 |
100 | warn "reattach<@_>\n"; |
|
|
101 | } |
|
|
102 | |
85 | |
103 | sub instantiate { |
86 | =cut |
104 | warn "instantiate<@_>\n"; |
|
|
105 | } |
|
|
106 | |
87 | |
107 | sub clone { |
88 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
108 | warn "clone<@_>\n"; |
|
|
109 | } |
|
|
110 | |
89 | |
111 | ############################################################################# |
90 | =item $ref = cf::from_json $json |
112 | # "new" plug-in system |
|
|
113 | |
91 | |
114 | =item cf::object::attach ... # NYI |
92 | Converts a JSON string into the corresponding perl data structure. |
|
|
93 | |
|
|
94 | =cut |
|
|
95 | |
|
|
96 | sub from_json($) { |
|
|
97 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
98 | JSON::Syck::Load $_[0] |
|
|
99 | } |
|
|
100 | |
|
|
101 | =item $json = cf::to_json $ref |
|
|
102 | |
|
|
103 | Converts a perl data structure into its JSON representation. |
|
|
104 | |
|
|
105 | =cut |
|
|
106 | |
|
|
107 | sub to_json($) { |
|
|
108 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
109 | JSON::Syck::Dump $_[0] |
|
|
110 | } |
|
|
111 | |
|
|
112 | =back |
|
|
113 | |
|
|
114 | =cut |
|
|
115 | |
|
|
116 | ############################################################################# |
|
|
117 | |
|
|
118 | =head2 EVENTS AND OBJECT ATTACHMENTS |
|
|
119 | |
|
|
120 | =over 4 |
|
|
121 | |
|
|
122 | =item $object->attach ($attachment, key => $value...) |
|
|
123 | |
|
|
124 | =item $object->detach ($attachment) |
|
|
125 | |
|
|
126 | Attach/detach a pre-registered attachment to an object. |
|
|
127 | |
|
|
128 | =item $player->attach ($attachment, key => $value...) |
|
|
129 | |
|
|
130 | =item $player->detach ($attachment) |
|
|
131 | |
|
|
132 | Attach/detach a pre-registered attachment to a player. |
|
|
133 | |
|
|
134 | =item $map->attach ($attachment, key => $value...) |
|
|
135 | |
|
|
136 | =item $map->detach ($attachment) |
|
|
137 | |
|
|
138 | Attach/detach a pre-registered attachment to a map. |
|
|
139 | |
|
|
140 | =item $bool = $object->attached ($name) |
|
|
141 | |
|
|
142 | =item $bool = $player->attached ($name) |
|
|
143 | |
|
|
144 | =item $bool = $map->attached ($name) |
|
|
145 | |
|
|
146 | Checks wether the named attachment is currently attached to the object. |
115 | |
147 | |
116 | =item cf::attach_global ... |
148 | =item cf::attach_global ... |
117 | |
149 | |
118 | =item cf::attach_to_type ... |
150 | Attach handlers for global events. |
|
|
151 | |
|
|
152 | This and all following C<attach_*>-functions expect any number of the |
|
|
153 | following handler/hook descriptions: |
|
|
154 | |
|
|
155 | =over 4 |
|
|
156 | |
|
|
157 | =item prio => $number |
|
|
158 | |
|
|
159 | Set the priority for all following handlers/hooks (unless overwritten |
|
|
160 | by another C<prio> setting). Lower priority handlers get executed |
|
|
161 | earlier. The default priority is C<0>, and many built-in handlers are |
|
|
162 | registered at priority C<-1000>, so lower priorities should not be used |
|
|
163 | unless you know what you are doing. |
|
|
164 | |
|
|
165 | =item on_I<event> => \&cb |
|
|
166 | |
|
|
167 | Call the given code reference whenever the named event happens (event is |
|
|
168 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
|
|
169 | handlers are recognised generally depends on the type of object these |
|
|
170 | handlers attach to). |
|
|
171 | |
|
|
172 | See F<include/eventinc.h> for the full list of events supported, and their |
|
|
173 | class. |
|
|
174 | |
|
|
175 | =item package => package:: |
|
|
176 | |
|
|
177 | Look for sub functions of the name C<< on_I<event> >> in the given |
|
|
178 | package and register them. Only handlers for eevents supported by the |
|
|
179 | object/class are recognised. |
|
|
180 | |
|
|
181 | =back |
|
|
182 | |
|
|
183 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
184 | |
|
|
185 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
186 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
187 | type. |
119 | |
188 | |
120 | =item cf::attach_to_objects ... |
189 | =item cf::attach_to_objects ... |
121 | |
190 | |
|
|
191 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
192 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
193 | the game. |
|
|
194 | |
122 | =item cf::attach_to_players ... |
195 | =item cf::attach_to_players ... |
123 | |
196 | |
|
|
197 | Attach handlers to all players. |
|
|
198 | |
124 | =item cf::attach_to_maps ... |
199 | =item cf::attach_to_maps ... |
125 | |
200 | |
126 | prio => $number, # lower is earlier |
201 | Attach handlers to all maps. |
127 | on_xxx => \&cb, |
202 | |
128 | package => package::, |
203 | =item cf:register_attachment $name, ... |
|
|
204 | |
|
|
205 | Register an attachment by name through which objects can refer to this |
|
|
206 | attachment. |
|
|
207 | |
|
|
208 | =item cf:register_player_attachment $name, ... |
|
|
209 | |
|
|
210 | Register an attachment by name through which players can refer to this |
|
|
211 | attachment. |
|
|
212 | |
|
|
213 | =item cf:register_map_attachment $name, ... |
|
|
214 | |
|
|
215 | Register an attachment by name through which maps can refer to this |
|
|
216 | attachment. |
129 | |
217 | |
130 | =cut |
218 | =cut |
131 | |
219 | |
132 | # the following variables are defined in .xs and must not be re-created |
220 | # the following variables are defined in .xs and must not be re-created |
133 | our @CB_GLOBAL = (); # registry for all global events |
221 | our @CB_GLOBAL = (); # registry for all global events |
134 | our @CB_OBJECT = (); |
222 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
135 | our @CB_PLAYER = (); |
223 | our @CB_PLAYER = (); |
136 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
224 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
137 | our @CB_MAP = (); |
225 | our @CB_MAP = (); |
138 | |
226 | |
|
|
227 | my %attachment; |
|
|
228 | |
139 | sub _attach_cb($\%$$$) { |
229 | sub _attach_cb($\%$$$) { |
140 | my ($registry, $undo, $event, $prio, $cb) = @_; |
230 | my ($registry, $undo, $event, $prio, $cb) = @_; |
141 | |
231 | |
142 | use sort 'stable'; |
232 | use sort 'stable'; |
143 | |
233 | |
… | |
… | |
151 | } |
241 | } |
152 | |
242 | |
153 | # attach handles attaching event callbacks |
243 | # attach handles attaching event callbacks |
154 | # the only thing the caller has to do is pass the correct |
244 | # the only thing the caller has to do is pass the correct |
155 | # registry (== where the callback attaches to). |
245 | # registry (== where the callback attaches to). |
156 | sub _attach(\@$\@) { |
246 | sub _attach(\@$@) { |
157 | my ($registry, $klass, $arg) = @_; |
247 | my ($registry, $klass, @arg) = @_; |
158 | |
248 | |
159 | my $prio = 0; |
249 | my $prio = 0; |
160 | |
250 | |
161 | my %undo = ( |
251 | my %undo = ( |
162 | registry => $registry, |
252 | registry => $registry, |
163 | cb => [], |
253 | cb => [], |
164 | ); |
254 | ); |
165 | |
255 | |
166 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
256 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
167 | |
257 | |
168 | while (@$arg) { |
258 | while (@arg) { |
169 | my $type = shift @$arg; |
259 | my $type = shift @arg; |
170 | |
260 | |
171 | if ($type eq "prio") { |
261 | if ($type eq "prio") { |
172 | $prio = shift @$arg; |
262 | $prio = shift @arg; |
173 | |
263 | |
174 | } elsif ($type eq "package") { |
264 | } elsif ($type eq "package") { |
175 | my $pkg = shift @$arg; |
265 | my $pkg = shift @arg; |
176 | |
266 | |
177 | while (my ($name, $id) = each %cb_id) { |
267 | while (my ($name, $id) = each %cb_id) { |
178 | if (my $cb = $pkg->can ($name)) { |
268 | if (my $cb = $pkg->can ($name)) { |
179 | _attach_cb $registry, %undo, $id, $prio, $cb; |
269 | _attach_cb $registry, %undo, $id, $prio, $cb; |
180 | } |
270 | } |
181 | } |
271 | } |
182 | |
272 | |
183 | } elsif (exists $cb_id{$type}) { |
273 | } elsif (exists $cb_id{$type}) { |
184 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
274 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
185 | |
275 | |
186 | } elsif (ref $type) { |
276 | } elsif (ref $type) { |
187 | warn "attaching objects not supported, ignoring.\n"; |
277 | warn "attaching objects not supported, ignoring.\n"; |
188 | |
278 | |
189 | } else { |
279 | } else { |
190 | shift @$arg; |
280 | shift @arg; |
191 | warn "attach argument '$type' not supported, ignoring.\n"; |
281 | warn "attach argument '$type' not supported, ignoring.\n"; |
192 | } |
282 | } |
193 | } |
283 | } |
194 | |
284 | |
195 | \%undo |
285 | \%undo |
196 | } |
286 | } |
197 | |
287 | |
|
|
288 | sub _attach_attachment { |
|
|
289 | my ($obj, $name, %arg) = @_; |
|
|
290 | |
|
|
291 | return if exists $obj->{_attachment}{$name}; |
|
|
292 | |
|
|
293 | my $res; |
|
|
294 | |
|
|
295 | if (my $attach = $attachment{$name}) { |
|
|
296 | my $registry = $obj->registry; |
|
|
297 | |
|
|
298 | for (@$attach) { |
|
|
299 | my ($klass, @attach) = @$_; |
|
|
300 | $res = _attach @$registry, $klass, @attach; |
|
|
301 | } |
|
|
302 | |
|
|
303 | $obj->{$name} = \%arg; |
|
|
304 | } else { |
|
|
305 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
306 | } |
|
|
307 | |
|
|
308 | $obj->{_attachment}{$name} = undef; |
|
|
309 | |
|
|
310 | $res->{attachment} = $name; |
|
|
311 | $res |
|
|
312 | } |
|
|
313 | |
198 | sub cf::object::attach { |
314 | *cf::object::attach = |
199 | die; |
315 | *cf::player::attach = |
200 | } |
316 | *cf::map::attach = sub { |
|
|
317 | my ($obj, $name, %arg) = @_; |
|
|
318 | |
|
|
319 | _attach_attachment $obj, $name, %arg; |
|
|
320 | }; |
|
|
321 | |
|
|
322 | # all those should be optimised |
|
|
323 | *cf::object::detach = |
|
|
324 | *cf::player::detach = |
|
|
325 | *cf::map::detach = sub { |
|
|
326 | my ($obj, $name) = @_; |
|
|
327 | |
|
|
328 | delete $obj->{_attachment}{$name}; |
|
|
329 | reattach ($obj); |
|
|
330 | }; |
|
|
331 | |
|
|
332 | *cf::object::attached = |
|
|
333 | *cf::player::attached = |
|
|
334 | *cf::map::attached = sub { |
|
|
335 | my ($obj, $name) = @_; |
|
|
336 | |
|
|
337 | exists $obj->{_attachment}{$name} |
|
|
338 | }; |
201 | |
339 | |
202 | sub attach_global { |
340 | sub attach_global { |
203 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
341 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
204 | } |
342 | } |
205 | |
343 | |
206 | sub attach_to_type { |
344 | sub attach_to_type { |
207 | my $type = shift; |
345 | my $type = shift; |
|
|
346 | my $subtype = shift; |
|
|
347 | |
208 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
348 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
209 | } |
349 | } |
210 | |
350 | |
211 | sub attach_to_objects { |
351 | sub attach_to_objects { |
212 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
352 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
213 | } |
353 | } |
… | |
… | |
218 | |
358 | |
219 | sub attach_to_maps { |
359 | sub attach_to_maps { |
220 | _attach @CB_MAP, KLASS_MAP, @_ |
360 | _attach @CB_MAP, KLASS_MAP, @_ |
221 | } |
361 | } |
222 | |
362 | |
|
|
363 | sub register_attachment { |
|
|
364 | my $name = shift; |
|
|
365 | |
|
|
366 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
367 | } |
|
|
368 | |
|
|
369 | sub register_player_attachment { |
|
|
370 | my $name = shift; |
|
|
371 | |
|
|
372 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
373 | } |
|
|
374 | |
|
|
375 | sub register_map_attachment { |
|
|
376 | my $name = shift; |
|
|
377 | |
|
|
378 | $attachment{$name} = [[KLASS_MAP, @_]]; |
|
|
379 | } |
|
|
380 | |
223 | our $override; |
381 | our $override; |
|
|
382 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
224 | |
383 | |
225 | sub override() { |
384 | sub override { |
226 | $override = 1 |
385 | $override = 1; |
|
|
386 | @invoke_results = (); |
227 | } |
387 | } |
228 | |
388 | |
229 | sub invoke { |
389 | sub do_invoke { |
230 | my $event = shift; |
390 | my $event = shift; |
231 | my $callbacks = shift; |
391 | my $callbacks = shift; |
232 | |
392 | |
|
|
393 | @invoke_results = (); |
|
|
394 | |
233 | local $override; |
395 | local $override; |
234 | |
396 | |
235 | for (@$callbacks) { |
397 | for (@$callbacks) { |
236 | eval { &{$_->[1]} }; |
398 | eval { &{$_->[1]} }; |
237 | |
399 | |
238 | if ($@) { |
400 | if ($@) { |
239 | warn "$@"; |
401 | warn "$@"; |
240 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
402 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
241 | override; |
403 | override; |
242 | } |
404 | } |
243 | |
405 | |
244 | return 1 if $override; |
406 | return 1 if $override; |
245 | } |
407 | } |
246 | |
408 | |
247 | 0 |
409 | 0 |
248 | } |
410 | } |
|
|
411 | |
|
|
412 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
|
|
413 | |
|
|
414 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
415 | |
|
|
416 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
417 | |
|
|
418 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
419 | |
|
|
420 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
421 | |
|
|
422 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
423 | removed in future versions), and there is no public API to access override |
|
|
424 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
425 | |
|
|
426 | =back |
|
|
427 | |
|
|
428 | =cut |
|
|
429 | |
|
|
430 | ############################################################################# |
|
|
431 | |
|
|
432 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
|
|
433 | |
|
|
434 | =over 4 |
|
|
435 | |
|
|
436 | =item $object->valid, $player->valid, $map->valid |
|
|
437 | |
|
|
438 | Just because you have a perl object does not mean that the corresponding |
|
|
439 | C-level object still exists. If you try to access an object that has no |
|
|
440 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
441 | can be used to test for existence of the C object part without causing an |
|
|
442 | exception. |
|
|
443 | |
|
|
444 | =back |
|
|
445 | |
|
|
446 | =cut |
|
|
447 | |
|
|
448 | *cf::object::valid = |
|
|
449 | *cf::player::valid = |
|
|
450 | *cf::map::valid = \&cf::_valid; |
|
|
451 | |
|
|
452 | ############################################################################# |
|
|
453 | # object support |
|
|
454 | |
|
|
455 | sub instantiate { |
|
|
456 | my ($obj, $data) = @_; |
|
|
457 | |
|
|
458 | $data = from_json $data; |
|
|
459 | |
|
|
460 | for (@$data) { |
|
|
461 | my ($name, $args) = @$_; |
|
|
462 | |
|
|
463 | $obj->attach ($name, %{$args || {} }); |
|
|
464 | } |
|
|
465 | } |
|
|
466 | |
|
|
467 | # basically do the same as instantiate, without calling instantiate |
|
|
468 | sub reattach { |
|
|
469 | my ($obj) = @_; |
|
|
470 | my $registry = $obj->registry; |
|
|
471 | |
|
|
472 | @$registry = (); |
|
|
473 | |
|
|
474 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
475 | |
|
|
476 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
|
|
477 | if (my $attach = $attachment{$name}) { |
|
|
478 | for (@$attach) { |
|
|
479 | my ($klass, @attach) = @$_; |
|
|
480 | _attach @$registry, $klass, @attach; |
|
|
481 | } |
|
|
482 | } else { |
|
|
483 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
484 | } |
|
|
485 | } |
|
|
486 | } |
|
|
487 | |
|
|
488 | sub object_freezer_save { |
|
|
489 | my ($filename, $rdata, $objs) = @_; |
|
|
490 | |
|
|
491 | if (length $$rdata) { |
|
|
492 | warn sprintf "saving %s (%d,%d)\n", |
|
|
493 | $filename, length $$rdata, scalar @$objs; |
|
|
494 | |
|
|
495 | if (open my $fh, ">:raw", "$filename~") { |
|
|
496 | chmod SAVE_MODE, $fh; |
|
|
497 | syswrite $fh, $$rdata; |
|
|
498 | close $fh; |
|
|
499 | |
|
|
500 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
501 | chmod SAVE_MODE, $fh; |
|
|
502 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
503 | close $fh; |
|
|
504 | rename "$filename.pst~", "$filename.pst"; |
|
|
505 | } else { |
|
|
506 | unlink "$filename.pst"; |
|
|
507 | } |
|
|
508 | |
|
|
509 | rename "$filename~", $filename; |
|
|
510 | } else { |
|
|
511 | warn "FATAL: $filename~: $!\n"; |
|
|
512 | } |
|
|
513 | } else { |
|
|
514 | unlink $filename; |
|
|
515 | unlink "$filename.pst"; |
|
|
516 | } |
|
|
517 | } |
|
|
518 | |
|
|
519 | sub object_thawer_load { |
|
|
520 | my ($filename) = @_; |
|
|
521 | |
|
|
522 | local $/; |
|
|
523 | |
|
|
524 | my $av; |
|
|
525 | |
|
|
526 | #TODO: use sysread etc. |
|
|
527 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
528 | $data = <$data>; |
|
|
529 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
|
|
530 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
531 | } |
|
|
532 | return ($data, $av); |
|
|
533 | } |
|
|
534 | |
|
|
535 | () |
|
|
536 | } |
|
|
537 | |
|
|
538 | attach_to_objects |
|
|
539 | prio => -1000000, |
|
|
540 | on_clone => sub { |
|
|
541 | my ($src, $dst) = @_; |
|
|
542 | |
|
|
543 | @{$dst->registry} = @{$src->registry}; |
|
|
544 | |
|
|
545 | %$dst = %$src; |
|
|
546 | |
|
|
547 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
|
|
548 | if exists $src->{_attachment}; |
|
|
549 | }, |
|
|
550 | ; |
249 | |
551 | |
250 | ############################################################################# |
552 | ############################################################################# |
251 | # old plug-in events |
553 | # old plug-in events |
252 | |
554 | |
253 | sub inject_event { |
555 | sub inject_event { |
… | |
… | |
299 | } |
601 | } |
300 | |
602 | |
301 | sub register { |
603 | sub register { |
302 | my ($base, $pkg) = @_; |
604 | my ($base, $pkg) = @_; |
303 | |
605 | |
304 | for my $idx (0 .. $#PLUGIN_EVENT) { |
606 | #TODO |
305 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
|
|
306 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
|
|
307 | $hook[$idx]{$base} = $ref; |
|
|
308 | } |
|
|
309 | } |
|
|
310 | } |
607 | } |
311 | |
608 | |
312 | sub load_extension { |
609 | sub load_extension { |
313 | my ($path) = @_; |
610 | my ($path) = @_; |
314 | |
611 | |
… | |
… | |
345 | my ($pkg) = @_; |
642 | my ($pkg) = @_; |
346 | |
643 | |
347 | warn "removing extension $pkg\n"; |
644 | warn "removing extension $pkg\n"; |
348 | |
645 | |
349 | # remove hooks |
646 | # remove hooks |
|
|
647 | #TODO |
350 | for my $idx (0 .. $#PLUGIN_EVENT) { |
648 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
351 | delete $hook[$idx]{$pkg}; |
649 | # delete $hook[$idx]{$pkg}; |
352 | } |
650 | # } |
353 | |
651 | |
354 | # remove commands |
652 | # remove commands |
355 | for my $name (keys %command) { |
653 | for my $name (keys %command) { |
356 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
654 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
357 | |
655 | |
… | |
… | |
387 | eval { |
685 | eval { |
388 | load_extension $ext; |
686 | load_extension $ext; |
389 | 1 |
687 | 1 |
390 | } or warn "$ext not loaded: $@"; |
688 | } or warn "$ext not loaded: $@"; |
391 | } |
689 | } |
392 | } |
|
|
393 | |
|
|
394 | sub _perl_reload(&) { |
|
|
395 | my ($msg) = @_; |
|
|
396 | |
|
|
397 | $msg->("reloading..."); |
|
|
398 | |
|
|
399 | eval { |
|
|
400 | # 1. cancel all watchers |
|
|
401 | $_->cancel for Event::all_watchers; |
|
|
402 | |
|
|
403 | # 2. unload all extensions |
|
|
404 | for (@exts) { |
|
|
405 | $msg->("unloading <$_>"); |
|
|
406 | unload_extension $_; |
|
|
407 | } |
|
|
408 | |
|
|
409 | # 3. unload all modules loaded from $LIBDIR |
|
|
410 | while (my ($k, $v) = each %INC) { |
|
|
411 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
412 | |
|
|
413 | $msg->("removing <$k>"); |
|
|
414 | delete $INC{$k}; |
|
|
415 | |
|
|
416 | $k =~ s/\.pm$//; |
|
|
417 | $k =~ s/\//::/g; |
|
|
418 | |
|
|
419 | if (my $cb = $k->can ("unload_module")) { |
|
|
420 | $cb->(); |
|
|
421 | } |
|
|
422 | |
|
|
423 | Symbol::delete_package $k; |
|
|
424 | } |
|
|
425 | |
|
|
426 | # 4. get rid of safe::, as good as possible |
|
|
427 | Symbol::delete_package "safe::$_" |
|
|
428 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
429 | |
|
|
430 | # 5. remove register_script_function callbacks |
|
|
431 | # TODO |
|
|
432 | |
|
|
433 | # 6. unload cf.pm "a bit" |
|
|
434 | delete $INC{"cf.pm"}; |
|
|
435 | |
|
|
436 | # don't, removes xs symbols, too, |
|
|
437 | # and global variables created in xs |
|
|
438 | #Symbol::delete_package __PACKAGE__; |
|
|
439 | |
|
|
440 | # 7. reload cf.pm |
|
|
441 | $msg->("reloading cf.pm"); |
|
|
442 | require cf; |
|
|
443 | }; |
|
|
444 | $msg->($@) if $@; |
|
|
445 | |
|
|
446 | $msg->("reloaded"); |
|
|
447 | }; |
|
|
448 | |
|
|
449 | sub perl_reload() { |
|
|
450 | _perl_reload { |
|
|
451 | warn $_[0]; |
|
|
452 | print "$_[0]\n"; |
|
|
453 | }; |
|
|
454 | } |
|
|
455 | |
|
|
456 | register_command "perl-reload", 0, sub { |
|
|
457 | my ($who, $arg) = @_; |
|
|
458 | |
|
|
459 | if ($who->flag (FLAG_WIZ)) { |
|
|
460 | _perl_reload { |
|
|
461 | warn $_[0]; |
|
|
462 | $who->message ($_[0]); |
|
|
463 | }; |
|
|
464 | } |
|
|
465 | }; |
|
|
466 | |
|
|
467 | ############################################################################# |
|
|
468 | # utility functions |
|
|
469 | |
|
|
470 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
471 | |
|
|
472 | sub from_json($) { |
|
|
473 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
474 | JSON::Syck::Load $_[0] |
|
|
475 | } |
|
|
476 | |
|
|
477 | sub to_json($) { |
|
|
478 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
479 | JSON::Syck::Dump $_[0] |
|
|
480 | } |
690 | } |
481 | |
691 | |
482 | ############################################################################# |
692 | ############################################################################# |
483 | # extcmd framework, basically convert ext <msg> |
693 | # extcmd framework, basically convert ext <msg> |
484 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
694 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
… | |
… | |
510 | my ($map) = @_; |
720 | my ($map) = @_; |
511 | |
721 | |
512 | my $path = $map->tmpname; |
722 | my $path = $map->tmpname; |
513 | defined $path or return; |
723 | defined $path or return; |
514 | |
724 | |
515 | unlink "$path.cfperl"; |
725 | unlink "$path.pst"; |
516 | }; |
|
|
517 | |
|
|
518 | *cf::mapsupport::on_swapin = |
|
|
519 | *cf::mapsupport::on_load = sub { |
|
|
520 | my ($map) = @_; |
|
|
521 | |
|
|
522 | my $path = $map->tmpname; |
|
|
523 | $path = $map->path unless defined $path; |
|
|
524 | |
|
|
525 | open my $fh, "<:raw", "$path.cfperl" |
|
|
526 | or return; # no perl data |
|
|
527 | |
|
|
528 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
529 | |
|
|
530 | $data->{version} <= 1 |
|
|
531 | or return; # too new |
|
|
532 | |
|
|
533 | $map->_set_obs ($data->{obs}); |
|
|
534 | }; |
|
|
535 | |
|
|
536 | *cf::mapsupport::on_swapout = sub { |
|
|
537 | my ($map) = @_; |
|
|
538 | |
|
|
539 | my $path = $map->tmpname; |
|
|
540 | $path = $map->path unless defined $path; |
|
|
541 | |
|
|
542 | my $obs = $map->_get_obs; |
|
|
543 | |
|
|
544 | if (defined $obs) { |
|
|
545 | open my $fh, ">:raw", "$path.cfperl" |
|
|
546 | or die "$path.cfperl: $!"; |
|
|
547 | |
|
|
548 | stat $path; |
|
|
549 | |
|
|
550 | print $fh Storable::nfreeze { |
|
|
551 | size => (stat _)[7], |
|
|
552 | time => (stat _)[9], |
|
|
553 | version => 1, |
|
|
554 | obs => $obs, |
|
|
555 | }; |
|
|
556 | |
|
|
557 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
558 | } else { |
|
|
559 | unlink "$path.cfperl"; |
|
|
560 | } |
|
|
561 | }; |
726 | }; |
562 | |
727 | |
563 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
728 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
564 | |
729 | |
565 | ############################################################################# |
730 | ############################################################################# |
… | |
… | |
567 | |
732 | |
568 | sub all_objects(@) { |
733 | sub all_objects(@) { |
569 | @_, map all_objects ($_->inv), @_ |
734 | @_, map all_objects ($_->inv), @_ |
570 | } |
735 | } |
571 | |
736 | |
|
|
737 | # TODO: compatibility cruft, remove when no longer needed |
572 | attach_to_players |
738 | attach_to_players |
573 | on_load => sub { |
739 | on_load => sub { |
574 | my ($pl, $path) = @_; |
740 | my ($pl, $path) = @_; |
575 | |
741 | |
576 | for my $o (all_objects $pl->ob) { |
742 | for my $o (all_objects $pl->ob) { |
… | |
… | |
579 | |
745 | |
580 | %$o = %{ Storable::thaw pack "H*", $value }; |
746 | %$o = %{ Storable::thaw pack "H*", $value }; |
581 | } |
747 | } |
582 | } |
748 | } |
583 | }, |
749 | }, |
584 | on_save => sub { |
|
|
585 | my ($pl, $path) = @_; |
|
|
586 | |
|
|
587 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
588 | for grep %$_, all_objects $pl->ob; |
|
|
589 | }, |
|
|
590 | ; |
750 | ; |
591 | |
751 | |
592 | ############################################################################# |
752 | ############################################################################# |
593 | # core extensions - in perl |
753 | |
|
|
754 | =head2 CORE EXTENSIONS |
|
|
755 | |
|
|
756 | Functions and methods that extend core crossfire objects. |
|
|
757 | |
|
|
758 | =over 4 |
594 | |
759 | |
595 | =item cf::player::exists $login |
760 | =item cf::player::exists $login |
596 | |
761 | |
597 | Returns true when the given account exists. |
762 | Returns true when the given account exists. |
598 | |
763 | |
… | |
… | |
638 | $msg{msgid} = $id; |
803 | $msg{msgid} = $id; |
639 | |
804 | |
640 | $self->send ("ext " . to_json \%msg); |
805 | $self->send ("ext " . to_json \%msg); |
641 | } |
806 | } |
642 | |
807 | |
|
|
808 | =back |
|
|
809 | |
|
|
810 | =cut |
|
|
811 | |
643 | ############################################################################# |
812 | ############################################################################# |
644 | # map scripting support |
813 | |
|
|
814 | =head2 SAFE SCRIPTING |
|
|
815 | |
|
|
816 | Functions that provide a safe environment to compile and execute |
|
|
817 | snippets of perl code without them endangering the safety of the server |
|
|
818 | itself. Looping constructs, I/O operators and other built-in functionality |
|
|
819 | is not available in the safe scripting environment, and the number of |
|
|
820 | functions and methods that cna be called is greatly reduced. |
|
|
821 | |
|
|
822 | =cut |
645 | |
823 | |
646 | our $safe = new Safe "safe"; |
824 | our $safe = new Safe "safe"; |
647 | our $safe_hole = new Safe::Hole; |
825 | our $safe_hole = new Safe::Hole; |
648 | |
826 | |
649 | $SIG{FPE} = 'IGNORE'; |
827 | $SIG{FPE} = 'IGNORE'; |
650 | |
828 | |
651 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
829 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
652 | |
830 | |
653 | # here we export the classes and methods available to script code |
831 | # here we export the classes and methods available to script code |
|
|
832 | |
|
|
833 | =pod |
|
|
834 | |
|
|
835 | The following fucntions and emthods are available within a safe environment: |
|
|
836 | |
|
|
837 | cf::object contr pay_amount pay_player |
|
|
838 | cf::object::player player |
|
|
839 | cf::player peaceful |
|
|
840 | |
|
|
841 | =cut |
654 | |
842 | |
655 | for ( |
843 | for ( |
656 | ["cf::object" => qw(contr pay_amount pay_player)], |
844 | ["cf::object" => qw(contr pay_amount pay_player)], |
657 | ["cf::object::player" => qw(player)], |
845 | ["cf::object::player" => qw(player)], |
658 | ["cf::player" => qw(peaceful)], |
846 | ["cf::player" => qw(peaceful)], |
… | |
… | |
661 | my ($pkg, @funs) = @$_; |
849 | my ($pkg, @funs) = @$_; |
662 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
850 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
663 | for @funs; |
851 | for @funs; |
664 | } |
852 | } |
665 | |
853 | |
|
|
854 | =over 4 |
|
|
855 | |
|
|
856 | =item @retval = safe_eval $code, [var => value, ...] |
|
|
857 | |
|
|
858 | Compiled and executes the given perl code snippet. additional var/value |
|
|
859 | pairs result in temporary local (my) scalar variables of the given name |
|
|
860 | that are available in the code snippet. Example: |
|
|
861 | |
|
|
862 | my $five = safe_eval '$first + $second', first => 1, second => 4; |
|
|
863 | |
|
|
864 | =cut |
|
|
865 | |
666 | sub safe_eval($;@) { |
866 | sub safe_eval($;@) { |
667 | my ($code, %vars) = @_; |
867 | my ($code, %vars) = @_; |
668 | |
868 | |
669 | my $qcode = $code; |
869 | my $qcode = $code; |
670 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
870 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
… | |
… | |
692 | } |
892 | } |
693 | |
893 | |
694 | wantarray ? @res : $res[0] |
894 | wantarray ? @res : $res[0] |
695 | } |
895 | } |
696 | |
896 | |
|
|
897 | =item cf::register_script_function $function => $cb |
|
|
898 | |
|
|
899 | Register a function that can be called from within map/npc scripts. The |
|
|
900 | function should be reasonably secure and should be put into a package name |
|
|
901 | like the extension. |
|
|
902 | |
|
|
903 | Example: register a function that gets called whenever a map script calls |
|
|
904 | C<rent::overview>, as used by the C<rent> extension. |
|
|
905 | |
|
|
906 | cf::register_script_function "rent::overview" => sub { |
|
|
907 | ... |
|
|
908 | }; |
|
|
909 | |
|
|
910 | =cut |
|
|
911 | |
697 | sub register_script_function { |
912 | sub register_script_function { |
698 | my ($fun, $cb) = @_; |
913 | my ($fun, $cb) = @_; |
699 | |
914 | |
700 | no strict 'refs'; |
915 | no strict 'refs'; |
701 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
916 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
702 | } |
917 | } |
703 | |
918 | |
|
|
919 | =back |
|
|
920 | |
|
|
921 | =cut |
|
|
922 | |
|
|
923 | ############################################################################# |
|
|
924 | |
|
|
925 | =head2 EXTENSION DATABASE SUPPORT |
|
|
926 | |
|
|
927 | Crossfire maintains a very simple database for extension use. It can |
|
|
928 | currently store anything that can be serialised using Storable, which |
|
|
929 | excludes objects. |
|
|
930 | |
|
|
931 | The parameter C<$family> should best start with the name of the extension |
|
|
932 | using it, it should be unique. |
|
|
933 | |
|
|
934 | =over 4 |
|
|
935 | |
|
|
936 | =item $hashref = cf::db_get $family |
|
|
937 | |
|
|
938 | Return a hashref for use by the extension C<$family>, which can be |
|
|
939 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
940 | C<cf::db_sync>. |
|
|
941 | |
|
|
942 | =item $value = cf::db_get $family => $key |
|
|
943 | |
|
|
944 | Returns a single value from the database |
|
|
945 | |
|
|
946 | =item cf::db_put $family => $hashref |
|
|
947 | |
|
|
948 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
949 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
950 | |
|
|
951 | =item cf::db_put $family => $key => $value |
|
|
952 | |
|
|
953 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
|
|
954 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
955 | |
|
|
956 | =item cf::db_dirty |
|
|
957 | |
|
|
958 | Marks the database as dirty, to be updated at a later time. |
|
|
959 | |
|
|
960 | =item cf::db_sync |
|
|
961 | |
|
|
962 | Immediately write the database to disk I<if it is dirty>. |
|
|
963 | |
|
|
964 | =cut |
|
|
965 | |
|
|
966 | { |
|
|
967 | my $db; |
|
|
968 | my $path = cf::localdir . "/database.pst"; |
|
|
969 | |
|
|
970 | sub db_load() { |
|
|
971 | warn "loading database $path\n";#d# remove later |
|
|
972 | $db = stat $path ? Storable::retrieve $path : { }; |
|
|
973 | } |
|
|
974 | |
|
|
975 | my $pid; |
|
|
976 | |
|
|
977 | sub db_save() { |
|
|
978 | warn "saving database $path\n";#d# remove later |
|
|
979 | waitpid $pid, 0 if $pid; |
|
|
980 | if (0 == ($pid = fork)) { |
|
|
981 | $db->{_meta}{version} = 1; |
|
|
982 | Storable::nstore $db, "$path~"; |
|
|
983 | rename "$path~", $path; |
|
|
984 | cf::_exit 0 if defined $pid; |
|
|
985 | } |
|
|
986 | } |
|
|
987 | |
|
|
988 | my $dirty; |
|
|
989 | |
|
|
990 | sub db_sync() { |
|
|
991 | db_save if $dirty; |
|
|
992 | undef $dirty; |
|
|
993 | } |
|
|
994 | |
|
|
995 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
|
|
996 | db_sync; |
|
|
997 | }); |
|
|
998 | |
|
|
999 | sub db_dirty() { |
|
|
1000 | $dirty = 1; |
|
|
1001 | $idle->start; |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | sub db_get($;$) { |
|
|
1005 | @_ >= 2 |
|
|
1006 | ? $db->{$_[0]}{$_[1]} |
|
|
1007 | : ($db->{$_[0]} ||= { }) |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | sub db_put($$;$) { |
|
|
1011 | if (@_ >= 3) { |
|
|
1012 | $db->{$_[0]}{$_[1]} = $_[2]; |
|
|
1013 | } else { |
|
|
1014 | $db->{$_[0]} = $_[1]; |
|
|
1015 | } |
|
|
1016 | db_dirty; |
|
|
1017 | } |
|
|
1018 | |
|
|
1019 | attach_global |
|
|
1020 | prio => 10000, |
|
|
1021 | on_cleanup => sub { |
|
|
1022 | db_sync; |
|
|
1023 | }, |
|
|
1024 | ; |
|
|
1025 | } |
|
|
1026 | |
704 | ############################################################################# |
1027 | ############################################################################# |
705 | # the server's main() |
1028 | # the server's main() |
706 | |
1029 | |
707 | sub main { |
1030 | sub main { |
|
|
1031 | db_load; |
|
|
1032 | load_extensions; |
708 | Event::loop; |
1033 | Event::loop; |
709 | } |
1034 | } |
710 | |
1035 | |
711 | ############################################################################# |
1036 | ############################################################################# |
712 | # initialisation |
1037 | # initialisation |
713 | |
1038 | |
|
|
1039 | sub _perl_reload(&) { |
|
|
1040 | my ($msg) = @_; |
|
|
1041 | |
|
|
1042 | $msg->("reloading..."); |
|
|
1043 | |
|
|
1044 | eval { |
|
|
1045 | # cancel all watchers |
|
|
1046 | $_->cancel for Event::all_watchers; |
|
|
1047 | |
|
|
1048 | # unload all extensions |
|
|
1049 | for (@exts) { |
|
|
1050 | $msg->("unloading <$_>"); |
|
|
1051 | unload_extension $_; |
|
|
1052 | } |
|
|
1053 | |
|
|
1054 | # unload all modules loaded from $LIBDIR |
|
|
1055 | while (my ($k, $v) = each %INC) { |
|
|
1056 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
1057 | |
|
|
1058 | $msg->("removing <$k>"); |
|
|
1059 | delete $INC{$k}; |
|
|
1060 | |
|
|
1061 | $k =~ s/\.pm$//; |
|
|
1062 | $k =~ s/\//::/g; |
|
|
1063 | |
|
|
1064 | if (my $cb = $k->can ("unload_module")) { |
|
|
1065 | $cb->(); |
|
|
1066 | } |
|
|
1067 | |
|
|
1068 | Symbol::delete_package $k; |
|
|
1069 | } |
|
|
1070 | |
|
|
1071 | # sync database to disk |
|
|
1072 | cf::db_sync; |
|
|
1073 | |
|
|
1074 | # get rid of safe::, as good as possible |
|
|
1075 | Symbol::delete_package "safe::$_" |
|
|
1076 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
1077 | |
|
|
1078 | # remove register_script_function callbacks |
|
|
1079 | # TODO |
|
|
1080 | |
|
|
1081 | # unload cf.pm "a bit" |
|
|
1082 | delete $INC{"cf.pm"}; |
|
|
1083 | |
|
|
1084 | # don't, removes xs symbols, too, |
|
|
1085 | # and global variables created in xs |
|
|
1086 | #Symbol::delete_package __PACKAGE__; |
|
|
1087 | |
|
|
1088 | # reload cf.pm |
|
|
1089 | $msg->("reloading cf.pm"); |
|
|
1090 | require cf; |
|
|
1091 | |
|
|
1092 | # load database again |
|
|
1093 | cf::db_load; |
|
|
1094 | |
|
|
1095 | # load extensions |
|
|
1096 | $msg->("load extensions"); |
|
|
1097 | cf::load_extensions; |
|
|
1098 | |
|
|
1099 | # reattach attachments to objects |
|
|
1100 | $msg->("reattach"); |
|
|
1101 | _global_reattach; |
|
|
1102 | }; |
|
|
1103 | $msg->($@) if $@; |
|
|
1104 | |
|
|
1105 | $msg->("reloaded"); |
|
|
1106 | }; |
|
|
1107 | |
|
|
1108 | sub perl_reload() { |
|
|
1109 | _perl_reload { |
|
|
1110 | warn $_[0]; |
|
|
1111 | print "$_[0]\n"; |
|
|
1112 | }; |
|
|
1113 | } |
|
|
1114 | |
|
|
1115 | register_command "perl-reload", 0, sub { |
|
|
1116 | my ($who, $arg) = @_; |
|
|
1117 | |
|
|
1118 | if ($who->flag (FLAG_WIZ)) { |
|
|
1119 | _perl_reload { |
|
|
1120 | warn $_[0]; |
|
|
1121 | $who->message ($_[0]); |
|
|
1122 | }; |
|
|
1123 | } |
|
|
1124 | }; |
|
|
1125 | |
714 | register "<global>", __PACKAGE__; |
1126 | register "<global>", __PACKAGE__; |
715 | |
1127 | |
716 | unshift @INC, $LIBDIR; |
1128 | unshift @INC, $LIBDIR; |
717 | |
|
|
718 | load_extensions; |
|
|
719 | |
1129 | |
720 | $TICK_WATCHER = Event->timer ( |
1130 | $TICK_WATCHER = Event->timer ( |
721 | prio => 1, |
1131 | prio => 1, |
722 | at => $NEXT_TICK || 1, |
1132 | at => $NEXT_TICK || 1, |
723 | cb => sub { |
1133 | cb => sub { |