… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use IO::AIO (); |
|
|
11 | use YAML::Syck (); |
10 | use Time::HiRes; |
12 | use Time::HiRes; |
11 | use Event; |
13 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
15 | |
|
|
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
17 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
18 | |
14 | use strict; |
19 | use strict; |
15 | |
20 | |
|
|
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
22 | |
|
|
23 | our %COMMAND = (); |
|
|
24 | our %COMMAND_TIME = (); |
|
|
25 | our %EXTCMD = (); |
|
|
26 | |
16 | _init_vars; |
27 | _init_vars; |
17 | |
28 | |
18 | our %COMMAND = (); |
|
|
19 | our @EVENT; |
29 | our @EVENT; |
20 | our $LIBDIR = maps_directory "perl"; |
30 | our $LIBDIR = datadir . "/ext"; |
21 | |
31 | |
22 | our $TICK = MAX_TIME * 1e-6; |
32 | our $TICK = MAX_TIME * 1e-6; |
23 | our $TICK_WATCHER; |
33 | our $TICK_WATCHER; |
24 | our $NEXT_TICK; |
34 | our $NEXT_TICK; |
25 | |
35 | |
26 | our %CFG; |
36 | our %CFG; |
27 | |
37 | |
|
|
38 | our $UPTIME; $UPTIME ||= time; |
|
|
39 | |
28 | ############################################################################# |
40 | ############################################################################# |
29 | |
41 | |
30 | =head2 GLOBAL VARIABLES |
42 | =head2 GLOBAL VARIABLES |
31 | |
43 | |
32 | =over 4 |
44 | =over 4 |
|
|
45 | |
|
|
46 | =item $cf::UPTIME |
|
|
47 | |
|
|
48 | The timestamp of the server start (so not actually an uptime). |
33 | |
49 | |
34 | =item $cf::LIBDIR |
50 | =item $cf::LIBDIR |
35 | |
51 | |
36 | The perl library directory, where extensions and cf-specific modules can |
52 | The perl library directory, where extensions and cf-specific modules can |
37 | be found. It will be added to C<@INC> automatically. |
53 | be found. It will be added to C<@INC> automatically. |
… | |
… | |
58 | print STDERR "cfperl: $msg"; |
74 | print STDERR "cfperl: $msg"; |
59 | LOG llevError, "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
60 | }; |
76 | }; |
61 | } |
77 | } |
62 | |
78 | |
|
|
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
|
|
80 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
|
|
81 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
|
|
82 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
|
|
83 | @safe::cf::map::ISA = @cf::map::ISA = 'cf::attachable'; |
63 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
64 | |
85 | |
65 | # we bless all objects into (empty) derived classes to force a method lookup |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
66 | # within the Safe compartment. |
87 | # within the Safe compartment. |
67 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
88 | for my $pkg (qw( |
|
|
89 | cf::global |
|
|
90 | cf::object cf::object::player |
|
|
91 | cf::client cf::player |
|
|
92 | cf::arch cf::living |
|
|
93 | cf::map cf::party cf::region |
|
|
94 | )) { |
68 | no strict 'refs'; |
95 | no strict 'refs'; |
69 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
96 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
70 | } |
97 | } |
71 | |
98 | |
72 | $Event::DIED = sub { |
99 | $Event::DIED = sub { |
… | |
… | |
74 | }; |
101 | }; |
75 | |
102 | |
76 | my %ext_pkg; |
103 | my %ext_pkg; |
77 | my @exts; |
104 | my @exts; |
78 | my @hook; |
105 | my @hook; |
79 | my %command; |
|
|
80 | my %extcmd; |
|
|
81 | |
106 | |
82 | =head2 UTILITY FUNCTIONS |
107 | =head2 UTILITY FUNCTIONS |
83 | |
108 | |
84 | =over 4 |
109 | =over 4 |
85 | |
110 | |
… | |
… | |
113 | |
138 | |
114 | =cut |
139 | =cut |
115 | |
140 | |
116 | ############################################################################# |
141 | ############################################################################# |
117 | |
142 | |
118 | =head2 EVENTS AND OBJECT ATTACHMENTS |
143 | =head2 ATTACHABLE OBJECTS |
|
|
144 | |
|
|
145 | You can define and attach attachments to each "attachable" object in |
|
|
146 | crossfire+ (objects, players, clients, maps and the special "global" |
|
|
147 | class). In the following description, CLASS can be any of C<global>, |
|
|
148 | C<object> C<player>, C<client> or C<map>. |
119 | |
149 | |
120 | =over 4 |
150 | =over 4 |
121 | |
151 | |
122 | =item $object->attach ($attachment, key => $value...) |
152 | =item cf::CLASS::attachment $name, ... |
123 | |
153 | |
124 | =item $object->detach ($attachment) |
154 | Register an attachment by name through which attachable objects can refer |
|
|
155 | to this attachment. |
125 | |
156 | |
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) |
157 | =item $bool = $attachable->attached ($name) |
145 | |
158 | |
146 | Checks wether the named attachment is currently attached to the object. |
159 | Checks wether the named attachment is currently attached to the object. |
147 | |
160 | |
148 | =item cf::attach_global ... |
161 | =item $attachable->attach ($attachment, key => $value...) |
149 | |
162 | |
150 | Attach handlers for global events. |
163 | =item $attachable->detach ($attachment) |
151 | |
164 | |
152 | This and all following C<attach_*>-functions expect any number of the |
165 | Attach/detach a pre-registered attachment either to a specific object |
153 | following handler/hook descriptions: |
166 | (C<$attachable>) or all objects of the given class (if C<$attachable> is a |
|
|
167 | class in a static method call). |
|
|
168 | |
|
|
169 | You can attach to global events by using the C<cf::global> class. |
|
|
170 | |
|
|
171 | These method calls expect any number of the following handler/hook |
|
|
172 | descriptions: |
154 | |
173 | |
155 | =over 4 |
174 | =over 4 |
156 | |
175 | |
157 | =item prio => $number |
176 | =item prio => $number |
158 | |
177 | |
… | |
… | |
160 | by another C<prio> setting). Lower priority handlers get executed |
179 | by another C<prio> setting). Lower priority handlers get executed |
161 | earlier. The default priority is C<0>, and many built-in handlers are |
180 | 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 |
181 | registered at priority C<-1000>, so lower priorities should not be used |
163 | unless you know what you are doing. |
182 | unless you know what you are doing. |
164 | |
183 | |
|
|
184 | =item type => $type |
|
|
185 | |
|
|
186 | (Only for C<< cf::object->attach >> calls), limits the attachment to the |
|
|
187 | given type of objects only (the additional parameter C<subtype> can be |
|
|
188 | used to further limit to the given subtype). |
|
|
189 | |
165 | =item on_I<event> => \&cb |
190 | =item on_I<event> => \&cb |
166 | |
191 | |
167 | Call the given code reference whenever the named event happens (event is |
192 | 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 |
193 | 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 |
194 | handlers are recognised generally depends on the type of object these |
… | |
… | |
178 | package and register them. Only handlers for eevents supported by the |
203 | package and register them. Only handlers for eevents supported by the |
179 | object/class are recognised. |
204 | object/class are recognised. |
180 | |
205 | |
181 | =back |
206 | =back |
182 | |
207 | |
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. |
|
|
188 | |
|
|
189 | =item cf::attach_to_objects ... |
|
|
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 | |
|
|
195 | =item cf::attach_to_players ... |
|
|
196 | |
|
|
197 | Attach handlers to all players. |
|
|
198 | |
|
|
199 | =item cf::attach_to_maps ... |
|
|
200 | |
|
|
201 | Attach handlers to all maps. |
|
|
202 | |
|
|
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. |
|
|
217 | |
|
|
218 | =cut |
208 | =cut |
219 | |
209 | |
220 | # the following variables are defined in .xs and must not be re-created |
210 | # the following variables are defined in .xs and must not be re-created |
221 | our @CB_GLOBAL = (); # registry for all global events |
211 | our @CB_GLOBAL = (); # registry for all global events |
222 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
212 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
223 | our @CB_PLAYER = (); |
213 | our @CB_PLAYER = (); |
|
|
214 | our @CB_CLIENT = (); |
224 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
215 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
225 | our @CB_MAP = (); |
216 | our @CB_MAP = (); |
226 | |
217 | |
227 | my %attachment; |
218 | my %attachment; |
228 | |
219 | |
229 | sub _attach_cb($\%$$$) { |
220 | sub _attach_cb($$$$) { |
230 | my ($registry, $undo, $event, $prio, $cb) = @_; |
221 | my ($registry, $event, $prio, $cb) = @_; |
231 | |
222 | |
232 | use sort 'stable'; |
223 | use sort 'stable'; |
233 | |
224 | |
234 | $cb = [$prio, $cb]; |
225 | $cb = [$prio, $cb]; |
235 | |
226 | |
236 | @{$registry->[$event]} = sort |
227 | @{$registry->[$event]} = sort |
237 | { $a->[0] cmp $b->[0] } |
228 | { $a->[0] cmp $b->[0] } |
238 | @{$registry->[$event] || []}, $cb; |
229 | @{$registry->[$event] || []}, $cb; |
239 | |
|
|
240 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
241 | } |
230 | } |
242 | |
231 | |
243 | # attach handles attaching event callbacks |
232 | # attach handles attaching event callbacks |
244 | # the only thing the caller has to do is pass the correct |
233 | # the only thing the caller has to do is pass the correct |
245 | # registry (== where the callback attaches to). |
234 | # registry (== where the callback attaches to). |
246 | sub _attach(\@$@) { |
235 | sub _attach { |
247 | my ($registry, $klass, @arg) = @_; |
236 | my ($registry, $klass, @arg) = @_; |
248 | |
237 | |
|
|
238 | my $object_type; |
249 | my $prio = 0; |
239 | my $prio = 0; |
250 | |
|
|
251 | my %undo = ( |
|
|
252 | registry => $registry, |
|
|
253 | cb => [], |
|
|
254 | ); |
|
|
255 | |
|
|
256 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
240 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
257 | |
241 | |
258 | while (@arg) { |
242 | while (@arg) { |
259 | my $type = shift @arg; |
243 | my $type = shift @arg; |
260 | |
244 | |
261 | if ($type eq "prio") { |
245 | if ($type eq "prio") { |
262 | $prio = shift @arg; |
246 | $prio = shift @arg; |
263 | |
247 | |
|
|
248 | } elsif ($type eq "type") { |
|
|
249 | $object_type = shift @arg; |
|
|
250 | $registry = $CB_TYPE[$object_type] ||= []; |
|
|
251 | |
|
|
252 | } elsif ($type eq "subtype") { |
|
|
253 | defined $object_type or Carp::croak "subtype specified without type"; |
|
|
254 | my $object_subtype = shift @arg; |
|
|
255 | $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= []; |
|
|
256 | |
264 | } elsif ($type eq "package") { |
257 | } elsif ($type eq "package") { |
265 | my $pkg = shift @arg; |
258 | my $pkg = shift @arg; |
266 | |
259 | |
267 | while (my ($name, $id) = each %cb_id) { |
260 | while (my ($name, $id) = each %cb_id) { |
268 | if (my $cb = $pkg->can ($name)) { |
261 | if (my $cb = $pkg->can ($name)) { |
269 | _attach_cb $registry, %undo, $id, $prio, $cb; |
262 | _attach_cb $registry, $id, $prio, $cb; |
270 | } |
263 | } |
271 | } |
264 | } |
272 | |
265 | |
273 | } elsif (exists $cb_id{$type}) { |
266 | } elsif (exists $cb_id{$type}) { |
274 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
267 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
275 | |
268 | |
276 | } elsif (ref $type) { |
269 | } elsif (ref $type) { |
277 | warn "attaching objects not supported, ignoring.\n"; |
270 | warn "attaching objects not supported, ignoring.\n"; |
278 | |
271 | |
279 | } else { |
272 | } else { |
280 | shift @arg; |
273 | shift @arg; |
281 | warn "attach argument '$type' not supported, ignoring.\n"; |
274 | warn "attach argument '$type' not supported, ignoring.\n"; |
282 | } |
275 | } |
283 | } |
276 | } |
284 | |
|
|
285 | \%undo |
|
|
286 | } |
277 | } |
287 | |
278 | |
288 | sub _attach_attachment { |
279 | sub _object_attach { |
289 | my ($obj, $name, %arg) = @_; |
280 | my ($obj, $name, %arg) = @_; |
290 | |
281 | |
291 | return if exists $obj->{_attachment}{$name}; |
282 | return if exists $obj->{_attachment}{$name}; |
292 | |
|
|
293 | my $res; |
|
|
294 | |
283 | |
295 | if (my $attach = $attachment{$name}) { |
284 | if (my $attach = $attachment{$name}) { |
296 | my $registry = $obj->registry; |
285 | my $registry = $obj->registry; |
297 | |
286 | |
298 | for (@$attach) { |
287 | for (@$attach) { |
299 | my ($klass, @attach) = @$_; |
288 | my ($klass, @attach) = @$_; |
300 | $res = _attach @$registry, $klass, @attach; |
289 | _attach $registry, $klass, @attach; |
301 | } |
290 | } |
302 | |
291 | |
303 | $obj->{$name} = \%arg; |
292 | $obj->{$name} = \%arg; |
304 | } else { |
293 | } else { |
305 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
294 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
306 | } |
295 | } |
307 | |
296 | |
308 | $obj->{_attachment}{$name} = undef; |
297 | $obj->{_attachment}{$name} = undef; |
309 | |
|
|
310 | $res->{attachment} = $name; |
|
|
311 | $res |
|
|
312 | } |
298 | } |
313 | |
299 | |
314 | *cf::object::attach = |
300 | sub cf::attachable::attach { |
315 | *cf::player::attach = |
301 | if (ref $_[0]) { |
316 | *cf::map::attach = sub { |
302 | _object_attach @_; |
317 | my ($obj, $name, %arg) = @_; |
303 | } else { |
318 | |
304 | _attach shift->_attach_registry, @_; |
319 | _attach_attachment $obj, $name, %arg; |
305 | } |
320 | }; |
306 | }; |
321 | |
307 | |
322 | # all those should be optimised |
308 | # all those should be optimised |
323 | *cf::object::detach = |
309 | sub cf::attachable::detach { |
324 | *cf::player::detach = |
|
|
325 | *cf::map::detach = sub { |
|
|
326 | my ($obj, $name) = @_; |
310 | my ($obj, $name) = @_; |
327 | |
311 | |
|
|
312 | if (ref $obj) { |
328 | delete $obj->{_attachment}{$name}; |
313 | delete $obj->{_attachment}{$name}; |
329 | reattach ($obj); |
314 | reattach ($obj); |
|
|
315 | } else { |
|
|
316 | Carp::croak "cannot, currently, detach class attachments"; |
|
|
317 | } |
330 | }; |
318 | }; |
331 | |
319 | |
332 | *cf::object::attached = |
320 | sub cf::attachable::attached { |
333 | *cf::player::attached = |
|
|
334 | *cf::map::attached = sub { |
|
|
335 | my ($obj, $name) = @_; |
321 | my ($obj, $name) = @_; |
336 | |
322 | |
337 | exists $obj->{_attachment}{$name} |
323 | exists $obj->{_attachment}{$name} |
338 | }; |
|
|
339 | |
|
|
340 | sub attach_global { |
|
|
341 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
342 | } |
324 | } |
343 | |
325 | |
344 | sub attach_to_type { |
326 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
345 | my $type = shift; |
327 | eval "#line " . __LINE__ . " 'cf.pm' |
346 | my $subtype = shift; |
328 | sub cf::\L$klass\E::_attach_registry { |
|
|
329 | (\\\@CB_$klass, KLASS_$klass) |
|
|
330 | } |
347 | |
331 | |
348 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
332 | sub cf::\L$klass\E::attachment { |
349 | } |
|
|
350 | |
|
|
351 | sub attach_to_objects { |
|
|
352 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
353 | } |
|
|
354 | |
|
|
355 | sub attach_to_players { |
|
|
356 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
357 | } |
|
|
358 | |
|
|
359 | sub attach_to_maps { |
|
|
360 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
361 | } |
|
|
362 | |
|
|
363 | sub register_attachment { |
|
|
364 | my $name = shift; |
333 | my \$name = shift; |
365 | |
334 | |
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, @_]]; |
335 | \$attachment{\$name} = [[KLASS_$klass, \@_]]; |
|
|
336 | } |
|
|
337 | "; |
|
|
338 | die if $@; |
379 | } |
339 | } |
380 | |
340 | |
381 | our $override; |
341 | our $override; |
382 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
342 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
383 | |
343 | |
… | |
… | |
413 | |
373 | |
414 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
374 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
415 | |
375 | |
416 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
376 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
417 | |
377 | |
|
|
378 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
|
|
379 | |
418 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
380 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
419 | |
381 | |
420 | Generate a global/object/player/map-specific event with the given arguments. |
382 | Generate a global/object/player/map-specific event with the given arguments. |
421 | |
383 | |
422 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
384 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
… | |
… | |
427 | |
389 | |
428 | =cut |
390 | =cut |
429 | |
391 | |
430 | ############################################################################# |
392 | ############################################################################# |
431 | |
393 | |
432 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
394 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
395 | |
|
|
396 | Attachable objects includes objects, players, clients and maps. |
433 | |
397 | |
434 | =over 4 |
398 | =over 4 |
435 | |
399 | |
436 | =item $object->valid, $player->valid, $map->valid |
400 | =item $object->valid |
437 | |
401 | |
438 | Just because you have a perl object does not mean that the corresponding |
402 | 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 |
403 | 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 |
404 | 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 |
405 | can be used to test for existence of the C object part without causing an |
… | |
… | |
443 | |
407 | |
444 | =back |
408 | =back |
445 | |
409 | |
446 | =cut |
410 | =cut |
447 | |
411 | |
448 | *cf::object::valid = |
|
|
449 | *cf::player::valid = |
|
|
450 | *cf::map::valid = \&cf::_valid; |
|
|
451 | |
|
|
452 | ############################################################################# |
412 | ############################################################################# |
453 | # object support |
413 | # object support |
454 | |
414 | |
455 | sub instantiate { |
415 | sub instantiate { |
456 | my ($obj, $data) = @_; |
416 | my ($obj, $data) = @_; |
… | |
… | |
475 | |
435 | |
476 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
436 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
477 | if (my $attach = $attachment{$name}) { |
437 | if (my $attach = $attachment{$name}) { |
478 | for (@$attach) { |
438 | for (@$attach) { |
479 | my ($klass, @attach) = @$_; |
439 | my ($klass, @attach) = @$_; |
480 | _attach @$registry, $klass, @attach; |
440 | _attach $registry, $klass, @attach; |
481 | } |
441 | } |
482 | } else { |
442 | } else { |
483 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
443 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
484 | } |
444 | } |
485 | } |
445 | } |
… | |
… | |
514 | unlink $filename; |
474 | unlink $filename; |
515 | unlink "$filename.pst"; |
475 | unlink "$filename.pst"; |
516 | } |
476 | } |
517 | } |
477 | } |
518 | |
478 | |
|
|
479 | sub object_freezer_as_string { |
|
|
480 | my ($rdata, $objs) = @_; |
|
|
481 | |
|
|
482 | use Data::Dumper; |
|
|
483 | |
|
|
484 | $$rdata . Dumper $objs |
|
|
485 | } |
|
|
486 | |
519 | sub object_thawer_load { |
487 | sub object_thawer_load { |
520 | my ($filename) = @_; |
488 | my ($filename) = @_; |
521 | |
489 | |
522 | local $/; |
490 | local $/; |
523 | |
491 | |
… | |
… | |
533 | } |
501 | } |
534 | |
502 | |
535 | () |
503 | () |
536 | } |
504 | } |
537 | |
505 | |
538 | attach_to_objects |
506 | cf::object->attach ( |
539 | prio => -1000000, |
507 | prio => -1000000, |
540 | on_clone => sub { |
508 | on_clone => sub { |
541 | my ($src, $dst) = @_; |
509 | my ($src, $dst) = @_; |
542 | |
510 | |
543 | @{$dst->registry} = @{$src->registry}; |
511 | @{$dst->registry} = @{$src->registry}; |
… | |
… | |
545 | %$dst = %$src; |
513 | %$dst = %$src; |
546 | |
514 | |
547 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
515 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
548 | if exists $src->{_attachment}; |
516 | if exists $src->{_attachment}; |
549 | }, |
517 | }, |
550 | ; |
518 | ); |
551 | |
519 | |
552 | ############################################################################# |
520 | ############################################################################# |
553 | # old plug-in events |
521 | # command handling &c |
554 | |
522 | |
555 | sub inject_event { |
523 | =item cf::register_command $name => \&callback($ob,$args); |
556 | my $extension = shift; |
|
|
557 | my $event_code = shift; |
|
|
558 | |
524 | |
559 | my $cb = $hook[$event_code]{$extension} |
525 | Register a callback for execution when the client sends the user command |
560 | or return; |
526 | $name. |
561 | |
527 | |
562 | &$cb |
528 | =cut |
563 | } |
|
|
564 | |
|
|
565 | sub inject_global_event { |
|
|
566 | my $event = shift; |
|
|
567 | |
|
|
568 | my $cb = $hook[$event] |
|
|
569 | or return; |
|
|
570 | |
|
|
571 | List::Util::max map &$_, values %$cb |
|
|
572 | } |
|
|
573 | |
|
|
574 | sub inject_command { |
|
|
575 | my ($name, $obj, $params) = @_; |
|
|
576 | |
|
|
577 | for my $cmd (@{ $command{$name} }) { |
|
|
578 | $cmd->[1]->($obj, $params); |
|
|
579 | } |
|
|
580 | |
|
|
581 | -1 |
|
|
582 | } |
|
|
583 | |
529 | |
584 | sub register_command { |
530 | sub register_command { |
585 | my ($name, $time, $cb) = @_; |
531 | my ($name, $cb) = @_; |
586 | |
532 | |
587 | my $caller = caller; |
533 | my $caller = caller; |
588 | #warn "registering command '$name/$time' to '$caller'"; |
534 | #warn "registering command '$name/$time' to '$caller'"; |
589 | |
535 | |
590 | push @{ $command{$name} }, [$time, $cb, $caller]; |
536 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
591 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
|
|
592 | } |
537 | } |
|
|
538 | |
|
|
539 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
|
|
540 | |
|
|
541 | Register a callbackf ro execution when the client sends an extcmd packet. |
|
|
542 | |
|
|
543 | If the callback returns something, it is sent back as if reply was being |
|
|
544 | called. |
|
|
545 | |
|
|
546 | =cut |
593 | |
547 | |
594 | sub register_extcmd { |
548 | sub register_extcmd { |
595 | my ($name, $cb) = @_; |
549 | my ($name, $cb) = @_; |
596 | |
550 | |
597 | my $caller = caller; |
551 | my $caller = caller; |
598 | #warn "registering extcmd '$name' to '$caller'"; |
552 | #warn "registering extcmd '$name' to '$caller'"; |
599 | |
553 | |
600 | $extcmd{$name} = [$cb, $caller]; |
554 | $EXTCMD{$name} = [$cb, $caller]; |
601 | } |
555 | } |
|
|
556 | |
|
|
557 | cf::player->attach ( |
|
|
558 | on_command => sub { |
|
|
559 | my ($pl, $name, $params) = @_; |
|
|
560 | |
|
|
561 | my $cb = $COMMAND{$name} |
|
|
562 | or return; |
|
|
563 | |
|
|
564 | for my $cmd (@$cb) { |
|
|
565 | $cmd->[1]->($pl->ob, $params); |
|
|
566 | } |
|
|
567 | |
|
|
568 | cf::override; |
|
|
569 | }, |
|
|
570 | on_extcmd => sub { |
|
|
571 | my ($pl, $buf) = @_; |
|
|
572 | |
|
|
573 | my $msg = eval { from_json $buf }; |
|
|
574 | |
|
|
575 | if (ref $msg) { |
|
|
576 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
|
|
577 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
578 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
579 | } |
|
|
580 | } |
|
|
581 | } else { |
|
|
582 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
583 | } |
|
|
584 | |
|
|
585 | cf::override; |
|
|
586 | }, |
|
|
587 | ); |
602 | |
588 | |
603 | sub register { |
589 | sub register { |
604 | my ($base, $pkg) = @_; |
590 | my ($base, $pkg) = @_; |
605 | |
591 | |
606 | #TODO |
592 | #TODO |
… | |
… | |
625 | . "#line 1 \"$path\"\n{\n" |
611 | . "#line 1 \"$path\"\n{\n" |
626 | . (do { local $/; <$fh> }) |
612 | . (do { local $/; <$fh> }) |
627 | . "\n};\n1"; |
613 | . "\n};\n1"; |
628 | |
614 | |
629 | eval $source |
615 | eval $source |
630 | or die "$path: $@"; |
616 | or die $@ ? "$path: $@\n" |
|
|
617 | : "extension disabled.\n"; |
631 | |
618 | |
632 | push @exts, $pkg; |
619 | push @exts, $pkg; |
633 | $ext_pkg{$base} = $pkg; |
620 | $ext_pkg{$base} = $pkg; |
634 | |
621 | |
635 | # no strict 'refs'; |
622 | # no strict 'refs'; |
… | |
… | |
648 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
635 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
649 | # delete $hook[$idx]{$pkg}; |
636 | # delete $hook[$idx]{$pkg}; |
650 | # } |
637 | # } |
651 | |
638 | |
652 | # remove commands |
639 | # remove commands |
653 | for my $name (keys %command) { |
640 | for my $name (keys %COMMAND) { |
654 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
641 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
655 | |
642 | |
656 | if (@cb) { |
643 | if (@cb) { |
657 | $command{$name} = \@cb; |
644 | $COMMAND{$name} = \@cb; |
658 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
|
|
659 | } else { |
645 | } else { |
660 | delete $command{$name}; |
|
|
661 | delete $COMMAND{"$name\000"}; |
646 | delete $COMMAND{$name}; |
662 | } |
647 | } |
663 | } |
648 | } |
664 | |
649 | |
665 | # remove extcmds |
650 | # remove extcmds |
666 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
651 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
667 | delete $extcmd{$name}; |
652 | delete $EXTCMD{$name}; |
668 | } |
653 | } |
669 | |
654 | |
670 | if (my $cb = $pkg->can ("unload")) { |
655 | if (my $cb = $pkg->can ("unload")) { |
671 | eval { |
656 | eval { |
672 | $cb->($pkg); |
657 | $cb->($pkg); |
… | |
… | |
676 | |
661 | |
677 | Symbol::delete_package $pkg; |
662 | Symbol::delete_package $pkg; |
678 | } |
663 | } |
679 | |
664 | |
680 | sub load_extensions { |
665 | sub load_extensions { |
681 | my $LIBDIR = maps_directory "perl"; |
|
|
682 | |
|
|
683 | for my $ext (<$LIBDIR/*.ext>) { |
666 | for my $ext (<$LIBDIR/*.ext>) { |
684 | next unless -r $ext; |
667 | next unless -r $ext; |
685 | eval { |
668 | eval { |
686 | load_extension $ext; |
669 | load_extension $ext; |
687 | 1 |
670 | 1 |
688 | } or warn "$ext not loaded: $@"; |
671 | } or warn "$ext not loaded: $@"; |
689 | } |
672 | } |
690 | } |
673 | } |
691 | |
674 | |
692 | ############################################################################# |
675 | ############################################################################# |
693 | # extcmd framework, basically convert ext <msg> |
|
|
694 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
695 | |
|
|
696 | attach_to_players |
|
|
697 | on_extcmd => sub { |
|
|
698 | my ($pl, $buf) = @_; |
|
|
699 | |
|
|
700 | my $msg = eval { from_json $buf }; |
|
|
701 | |
|
|
702 | if (ref $msg) { |
|
|
703 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
704 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
705 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
706 | } |
|
|
707 | } |
|
|
708 | } else { |
|
|
709 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
710 | } |
|
|
711 | |
|
|
712 | cf::override; |
|
|
713 | }, |
|
|
714 | ; |
|
|
715 | |
|
|
716 | ############################################################################# |
|
|
717 | # load/save/clean perl data associated with a map |
676 | # load/save/clean perl data associated with a map |
718 | |
677 | |
719 | *cf::mapsupport::on_clean = sub { |
678 | *cf::mapsupport::on_clean = sub { |
720 | my ($map) = @_; |
679 | my ($map) = @_; |
721 | |
680 | |
… | |
… | |
723 | defined $path or return; |
682 | defined $path or return; |
724 | |
683 | |
725 | unlink "$path.pst"; |
684 | unlink "$path.pst"; |
726 | }; |
685 | }; |
727 | |
686 | |
728 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
687 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
729 | |
688 | |
730 | ############################################################################# |
689 | ############################################################################# |
731 | # load/save perl data associated with player->ob objects |
690 | # load/save perl data associated with player->ob objects |
732 | |
691 | |
733 | sub all_objects(@) { |
692 | sub all_objects(@) { |
734 | @_, map all_objects ($_->inv), @_ |
693 | @_, map all_objects ($_->inv), @_ |
735 | } |
694 | } |
736 | |
695 | |
737 | # TODO: compatibility cruft, remove when no longer needed |
696 | # TODO: compatibility cruft, remove when no longer needed |
738 | attach_to_players |
697 | cf::player->attach ( |
739 | on_load => sub { |
698 | on_load => sub { |
740 | my ($pl, $path) = @_; |
699 | my ($pl, $path) = @_; |
741 | |
700 | |
742 | for my $o (all_objects $pl->ob) { |
701 | for my $o (all_objects $pl->ob) { |
743 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
702 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
… | |
… | |
745 | |
704 | |
746 | %$o = %{ Storable::thaw pack "H*", $value }; |
705 | %$o = %{ Storable::thaw pack "H*", $value }; |
747 | } |
706 | } |
748 | } |
707 | } |
749 | }, |
708 | }, |
750 | ; |
709 | ); |
751 | |
710 | |
752 | ############################################################################# |
711 | ############################################################################# |
753 | |
712 | |
754 | =head2 CORE EXTENSIONS |
713 | =head2 CORE EXTENSIONS |
755 | |
714 | |
… | |
… | |
766 | sub cf::player::exists($) { |
725 | sub cf::player::exists($) { |
767 | cf::player::find $_[0] |
726 | cf::player::find $_[0] |
768 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
727 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
769 | } |
728 | } |
770 | |
729 | |
771 | =item $player->reply ($npc, $msg[, $flags]) |
730 | =item $player_object->reply ($npc, $msg[, $flags]) |
772 | |
731 | |
773 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
732 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
774 | can be C<undef>. Does the right thing when the player is currently in a |
733 | can be C<undef>. Does the right thing when the player is currently in a |
775 | dialogue with the given NPC character. |
734 | dialogue with the given NPC character. |
776 | |
735 | |
… | |
… | |
803 | $msg{msgid} = $id; |
762 | $msg{msgid} = $id; |
804 | |
763 | |
805 | $self->send ("ext " . to_json \%msg); |
764 | $self->send ("ext " . to_json \%msg); |
806 | } |
765 | } |
807 | |
766 | |
808 | =back |
767 | =item $player_object->may ("access") |
|
|
768 | |
|
|
769 | Returns wether the given player is authorized to access resource "access" |
|
|
770 | (e.g. "command_wizcast"). |
|
|
771 | |
|
|
772 | =cut |
|
|
773 | |
|
|
774 | sub cf::object::player::may { |
|
|
775 | my ($self, $access) = @_; |
|
|
776 | |
|
|
777 | $self->flag (cf::FLAG_WIZ) || |
|
|
778 | (ref $cf::CFG{"may_$access"} |
|
|
779 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
|
|
780 | : $cf::CFG{"may_$access"}) |
|
|
781 | } |
809 | |
782 | |
810 | =cut |
783 | =cut |
811 | |
784 | |
812 | ############################################################################# |
785 | ############################################################################# |
813 | |
786 | |
… | |
… | |
815 | |
788 | |
816 | Functions that provide a safe environment to compile and execute |
789 | Functions that provide a safe environment to compile and execute |
817 | snippets of perl code without them endangering the safety of the server |
790 | snippets of perl code without them endangering the safety of the server |
818 | itself. Looping constructs, I/O operators and other built-in functionality |
791 | itself. Looping constructs, I/O operators and other built-in functionality |
819 | is not available in the safe scripting environment, and the number of |
792 | is not available in the safe scripting environment, and the number of |
820 | functions and methods that cna be called is greatly reduced. |
793 | functions and methods that can be called is greatly reduced. |
821 | |
794 | |
822 | =cut |
795 | =cut |
823 | |
796 | |
824 | our $safe = new Safe "safe"; |
797 | our $safe = new Safe "safe"; |
825 | our $safe_hole = new Safe::Hole; |
798 | our $safe_hole = new Safe::Hole; |
… | |
… | |
832 | |
805 | |
833 | =pod |
806 | =pod |
834 | |
807 | |
835 | The following fucntions and emthods are available within a safe environment: |
808 | The following fucntions and emthods are available within a safe environment: |
836 | |
809 | |
837 | cf::object contr pay_amount pay_player |
810 | cf::object contr pay_amount pay_player map |
838 | cf::object::player player |
811 | cf::object::player player |
839 | cf::player peaceful |
812 | cf::player peaceful |
|
|
813 | cf::map trigger |
840 | |
814 | |
841 | =cut |
815 | =cut |
842 | |
816 | |
843 | for ( |
817 | for ( |
844 | ["cf::object" => qw(contr pay_amount pay_player)], |
818 | ["cf::object" => qw(contr pay_amount pay_player map)], |
845 | ["cf::object::player" => qw(player)], |
819 | ["cf::object::player" => qw(player)], |
846 | ["cf::player" => qw(peaceful)], |
820 | ["cf::player" => qw(peaceful)], |
|
|
821 | ["cf::map" => qw(trigger)], |
847 | ) { |
822 | ) { |
848 | no strict 'refs'; |
823 | no strict 'refs'; |
849 | my ($pkg, @funs) = @$_; |
824 | my ($pkg, @funs) = @$_; |
850 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
825 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
851 | for @funs; |
826 | for @funs; |
… | |
… | |
961 | |
936 | |
962 | Immediately write the database to disk I<if it is dirty>. |
937 | Immediately write the database to disk I<if it is dirty>. |
963 | |
938 | |
964 | =cut |
939 | =cut |
965 | |
940 | |
|
|
941 | our $DB; |
|
|
942 | |
966 | { |
943 | { |
967 | my $db; |
|
|
968 | my $path = cf::localdir . "/database.pst"; |
944 | my $path = cf::localdir . "/database.pst"; |
969 | |
945 | |
970 | sub db_load() { |
946 | sub db_load() { |
971 | warn "loading database $path\n";#d# remove later |
947 | warn "loading database $path\n";#d# remove later |
972 | $db = stat $path ? Storable::retrieve $path : { }; |
948 | $DB = stat $path ? Storable::retrieve $path : { }; |
973 | } |
949 | } |
974 | |
950 | |
975 | my $pid; |
951 | my $pid; |
976 | |
952 | |
977 | sub db_save() { |
953 | sub db_save() { |
978 | warn "saving database $path\n";#d# remove later |
954 | warn "saving database $path\n";#d# remove later |
979 | waitpid $pid, 0 if $pid; |
955 | waitpid $pid, 0 if $pid; |
980 | if (0 == ($pid = fork)) { |
956 | if (0 == ($pid = fork)) { |
981 | $db->{_meta}{version} = 1; |
957 | $DB->{_meta}{version} = 1; |
982 | Storable::nstore $db, "$path~"; |
958 | Storable::nstore $DB, "$path~"; |
983 | rename "$path~", $path; |
959 | rename "$path~", $path; |
984 | cf::_exit 0 if defined $pid; |
960 | cf::_exit 0 if defined $pid; |
985 | } |
961 | } |
986 | } |
962 | } |
987 | |
963 | |
… | |
… | |
990 | sub db_sync() { |
966 | sub db_sync() { |
991 | db_save if $dirty; |
967 | db_save if $dirty; |
992 | undef $dirty; |
968 | undef $dirty; |
993 | } |
969 | } |
994 | |
970 | |
995 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
971 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
996 | db_sync; |
972 | db_sync; |
997 | }); |
973 | }); |
998 | |
974 | |
999 | sub db_dirty() { |
975 | sub db_dirty() { |
1000 | $dirty = 1; |
976 | $dirty = 1; |
1001 | $idle->start; |
977 | $idle->start; |
1002 | } |
978 | } |
1003 | |
979 | |
1004 | sub db_get($;$) { |
980 | sub db_get($;$) { |
1005 | @_ >= 2 |
981 | @_ >= 2 |
1006 | ? $db->{$_[0]}{$_[1]} |
982 | ? $DB->{$_[0]}{$_[1]} |
1007 | : ($db->{$_[0]} ||= { }) |
983 | : ($DB->{$_[0]} ||= { }) |
1008 | } |
984 | } |
1009 | |
985 | |
1010 | sub db_put($$;$) { |
986 | sub db_put($$;$) { |
1011 | if (@_ >= 3) { |
987 | if (@_ >= 3) { |
1012 | $db->{$_[0]}{$_[1]} = $_[2]; |
988 | $DB->{$_[0]}{$_[1]} = $_[2]; |
1013 | } else { |
989 | } else { |
1014 | $db->{$_[0]} = $_[1]; |
990 | $DB->{$_[0]} = $_[1]; |
1015 | } |
991 | } |
1016 | db_dirty; |
992 | db_dirty; |
1017 | } |
993 | } |
1018 | |
994 | |
1019 | attach_global |
995 | cf::global->attach ( |
1020 | prio => 10000, |
996 | prio => 10000, |
1021 | on_cleanup => sub { |
997 | on_cleanup => sub { |
1022 | db_sync; |
998 | db_sync; |
1023 | }, |
999 | }, |
1024 | ; |
1000 | ); |
1025 | } |
1001 | } |
1026 | |
1002 | |
1027 | ############################################################################# |
1003 | ############################################################################# |
1028 | # the server's main() |
1004 | # the server's main() |
1029 | |
1005 | |
|
|
1006 | sub cfg_load { |
|
|
1007 | open my $fh, "<:utf8", cf::confdir . "/config" |
|
|
1008 | or return; |
|
|
1009 | |
|
|
1010 | local $/; |
|
|
1011 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1012 | } |
|
|
1013 | |
1030 | sub main { |
1014 | sub main { |
|
|
1015 | cfg_load; |
1031 | db_load; |
1016 | db_load; |
1032 | load_extensions; |
1017 | load_extensions; |
1033 | Event::loop; |
1018 | Event::loop; |
1034 | } |
1019 | } |
1035 | |
1020 | |
… | |
… | |
1041 | |
1026 | |
1042 | $msg->("reloading..."); |
1027 | $msg->("reloading..."); |
1043 | |
1028 | |
1044 | eval { |
1029 | eval { |
1045 | # cancel all watchers |
1030 | # cancel all watchers |
1046 | $_->cancel for Event::all_watchers; |
1031 | for (Event::all_watchers) { |
|
|
1032 | $_->cancel if $_->data & WF_AUTOCANCEL; |
|
|
1033 | } |
1047 | |
1034 | |
1048 | # unload all extensions |
1035 | # unload all extensions |
1049 | for (@exts) { |
1036 | for (@exts) { |
1050 | $msg->("unloading <$_>"); |
1037 | $msg->("unloading <$_>"); |
1051 | unload_extension $_; |
1038 | unload_extension $_; |
… | |
… | |
1087 | |
1074 | |
1088 | # reload cf.pm |
1075 | # reload cf.pm |
1089 | $msg->("reloading cf.pm"); |
1076 | $msg->("reloading cf.pm"); |
1090 | require cf; |
1077 | require cf; |
1091 | |
1078 | |
1092 | # load database again |
1079 | # load config and database again |
|
|
1080 | cf::cfg_load; |
1093 | cf::db_load; |
1081 | cf::db_load; |
1094 | |
1082 | |
1095 | # load extensions |
1083 | # load extensions |
1096 | $msg->("load extensions"); |
1084 | $msg->("load extensions"); |
1097 | cf::load_extensions; |
1085 | cf::load_extensions; |
… | |
… | |
1110 | warn $_[0]; |
1098 | warn $_[0]; |
1111 | print "$_[0]\n"; |
1099 | print "$_[0]\n"; |
1112 | }; |
1100 | }; |
1113 | } |
1101 | } |
1114 | |
1102 | |
|
|
1103 | register "<global>", __PACKAGE__; |
|
|
1104 | |
1115 | register_command "perl-reload", 0, sub { |
1105 | register_command "perl-reload" => sub { |
1116 | my ($who, $arg) = @_; |
1106 | my ($who, $arg) = @_; |
1117 | |
1107 | |
1118 | if ($who->flag (FLAG_WIZ)) { |
1108 | if ($who->flag (FLAG_WIZ)) { |
1119 | _perl_reload { |
1109 | _perl_reload { |
1120 | warn $_[0]; |
1110 | warn $_[0]; |
1121 | $who->message ($_[0]); |
1111 | $who->message ($_[0]); |
1122 | }; |
1112 | }; |
1123 | } |
1113 | } |
1124 | }; |
1114 | }; |
1125 | |
1115 | |
1126 | register "<global>", __PACKAGE__; |
|
|
1127 | |
|
|
1128 | unshift @INC, $LIBDIR; |
1116 | unshift @INC, $LIBDIR; |
1129 | |
1117 | |
1130 | $TICK_WATCHER = Event->timer ( |
1118 | $TICK_WATCHER = Event->timer ( |
1131 | prio => 1, |
1119 | prio => 0, |
1132 | at => $NEXT_TICK || 1, |
1120 | at => $NEXT_TICK || 1, |
|
|
1121 | data => WF_AUTOCANCEL, |
1133 | cb => sub { |
1122 | cb => sub { |
1134 | cf::server_tick; # one server iteration |
1123 | cf::server_tick; # one server iteration |
1135 | |
1124 | |
1136 | my $NOW = Event::time; |
1125 | my $NOW = Event::time; |
1137 | $NEXT_TICK += $TICK; |
1126 | $NEXT_TICK += $TICK; |
1138 | |
1127 | |
1139 | # if we are delayed by four ticks, skip them all |
1128 | # if we are delayed by four ticks or more, skip them all |
1140 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1129 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1141 | |
1130 | |
1142 | $TICK_WATCHER->at ($NEXT_TICK); |
1131 | $TICK_WATCHER->at ($NEXT_TICK); |
1143 | $TICK_WATCHER->start; |
1132 | $TICK_WATCHER->start; |
1144 | }, |
1133 | }, |
1145 | ); |
1134 | ); |
1146 | |
1135 | |
|
|
1136 | IO::AIO::max_poll_time $TICK * 0.2; |
|
|
1137 | |
|
|
1138 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
1139 | poll => 'r', |
|
|
1140 | prio => 5, |
|
|
1141 | data => WF_AUTOCANCEL, |
|
|
1142 | cb => \&IO::AIO::poll_cb); |
|
|
1143 | |
1147 | 1 |
1144 | 1 |
1148 | |
1145 | |