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