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