… | |
… | |
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 Time::HiRes; |
10 | use Event; |
11 | use Event; |
11 | $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 |
12 | |
13 | |
13 | use strict; |
14 | use strict; |
14 | |
15 | |
15 | our %COMMAND; |
16 | our %COMMAND = (); |
16 | our @EVENT; |
17 | our @EVENT; |
|
|
18 | our @PLUGIN_EVENT; |
17 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
18 | our %PROP_IDX; |
20 | our %PROP_IDX; |
19 | our $LIBDIR = maps_directory "perl"; |
21 | our $LIBDIR = maps_directory "perl"; |
20 | |
22 | |
|
|
23 | our $TICK = MAX_TIME * 1e-6; |
|
|
24 | our $TICK_WATCHER; |
|
|
25 | our $NEXT_TICK; |
|
|
26 | |
21 | BEGIN { |
27 | BEGIN { |
22 | @EVENT = map lc, @EVENT; |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
23 | |
29 | |
24 | *CORE::GLOBAL::warn = sub { |
30 | *CORE::GLOBAL::warn = sub { |
25 | my $msg = join "", @_; |
31 | my $msg = join "", @_; |
26 | $msg .= "\n" |
32 | $msg .= "\n" |
27 | unless $msg =~ /\n$/; |
33 | unless $msg =~ /\n$/; |
… | |
… | |
65 | prop_gen MAP_PROP => "cf::map"; |
71 | prop_gen MAP_PROP => "cf::map"; |
66 | prop_gen ARCH_PROP => "cf::arch"; |
72 | prop_gen ARCH_PROP => "cf::arch"; |
67 | |
73 | |
68 | # guessed hierarchies |
74 | # guessed hierarchies |
69 | |
75 | |
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
76 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
77 | @safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
72 | |
78 | |
73 | # we bless all objects into derived classes to force a method lookup |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
74 | # within the Safe compartment. |
80 | # 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)) { |
81 | 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'; |
82 | no strict 'refs'; |
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
78 | } |
84 | } |
79 | |
85 | |
80 | $Event::DIED = sub { |
86 | $Event::DIED = sub { |
81 | warn "error in event callback: @_"; |
87 | warn "error in event callback: @_"; |
82 | }; |
88 | }; |
… | |
… | |
85 | my @exts; |
91 | my @exts; |
86 | my @hook; |
92 | my @hook; |
87 | my %command; |
93 | my %command; |
88 | my %extcmd; |
94 | my %extcmd; |
89 | |
95 | |
|
|
96 | ############################################################################# |
|
|
97 | # "new" plug-in system |
|
|
98 | |
|
|
99 | =item cf::object::attach ... # NYI |
|
|
100 | |
|
|
101 | =item cf::attach_global ... |
|
|
102 | |
|
|
103 | =item cf::attach_to_type ... |
|
|
104 | |
|
|
105 | =item cf::attach_to_objects ... |
|
|
106 | |
|
|
107 | =item cf::attach_to_players ... |
|
|
108 | |
|
|
109 | =item cf::attach_to_maps ... |
|
|
110 | |
|
|
111 | prio => $number, # lower is earlier |
|
|
112 | on_xxx => \&cb, |
|
|
113 | package => package::, |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | # the following variables are defined in .xs and must not be re-created |
|
|
118 | our @CB_GLOBAL = (); # registry for all global events |
|
|
119 | our @CB_OBJECT = (); |
|
|
120 | our @CB_PLAYER = (); |
|
|
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
122 | our @CB_MAP = (); |
|
|
123 | |
|
|
124 | sub _attach_cb($\%$$$) { |
|
|
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
126 | |
|
|
127 | use sort 'stable'; |
|
|
128 | |
|
|
129 | $cb = [$prio, $cb]; |
|
|
130 | |
|
|
131 | @{$registry->[$event]} = sort |
|
|
132 | { $a->[0] cmp $b->[0] } |
|
|
133 | @{$registry->[$event] || []}, $cb; |
|
|
134 | |
|
|
135 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
136 | } |
|
|
137 | |
|
|
138 | # attach handles attaching event callbacks |
|
|
139 | # the only thing the caller has to do is pass the correct |
|
|
140 | # registry (== where the callback attaches to). |
|
|
141 | sub _attach(\@$\@) { |
|
|
142 | my ($registry, $klass, $arg) = @_; |
|
|
143 | |
|
|
144 | my $prio = 0; |
|
|
145 | |
|
|
146 | my %undo = ( |
|
|
147 | registry => $registry, |
|
|
148 | cb => [], |
|
|
149 | ); |
|
|
150 | |
|
|
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
152 | |
|
|
153 | while (@$arg) { |
|
|
154 | my $type = shift @$arg; |
|
|
155 | |
|
|
156 | if ($type eq "prio") { |
|
|
157 | $prio = shift @$arg; |
|
|
158 | |
|
|
159 | } elsif ($type eq "package") { |
|
|
160 | my $pkg = shift @$arg; |
|
|
161 | |
|
|
162 | while (my ($name, $id) = each %cb_id) { |
|
|
163 | if (my $cb = $pkg->can ($name)) { |
|
|
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
165 | } |
|
|
166 | } |
|
|
167 | |
|
|
168 | } elsif (exists $cb_id{$type}) { |
|
|
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
|
|
170 | |
|
|
171 | } elsif (ref $type) { |
|
|
172 | warn "attaching objects not supported, ignoring.\n"; |
|
|
173 | |
|
|
174 | } else { |
|
|
175 | shift @$arg; |
|
|
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
177 | } |
|
|
178 | } |
|
|
179 | |
|
|
180 | \%undo |
|
|
181 | } |
|
|
182 | |
|
|
183 | sub cf::object::attach { |
|
|
184 | die; |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub attach_global { |
|
|
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub attach_to_type { |
|
|
192 | my $type = shift; |
|
|
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
|
|
194 | } |
|
|
195 | |
|
|
196 | sub attach_to_objects { |
|
|
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub attach_to_players { |
|
|
201 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
202 | } |
|
|
203 | |
|
|
204 | sub attach_to_maps { |
|
|
205 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
206 | } |
|
|
207 | |
|
|
208 | our $override; |
|
|
209 | |
|
|
210 | sub override() { |
|
|
211 | $override = 1 |
|
|
212 | } |
|
|
213 | |
|
|
214 | sub invoke { |
|
|
215 | my $event = shift; |
|
|
216 | my $callbacks = shift; |
|
|
217 | |
|
|
218 | local $override; |
|
|
219 | |
|
|
220 | for (@$callbacks) { |
|
|
221 | eval { &{$_->[1]} }; |
|
|
222 | |
|
|
223 | if ($@) { |
|
|
224 | warn "$@"; |
|
|
225 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
|
|
226 | override; |
|
|
227 | } |
|
|
228 | |
|
|
229 | return 1 if $override; |
|
|
230 | } |
|
|
231 | |
|
|
232 | 0 |
|
|
233 | } |
|
|
234 | |
|
|
235 | ############################################################################# |
|
|
236 | # old plug-in events |
|
|
237 | |
90 | sub inject_event { |
238 | sub inject_event { |
91 | my $extension = shift; |
239 | my $extension = shift; |
92 | my $event_code = shift; |
240 | my $event_code = shift; |
93 | |
241 | |
94 | my $cb = $hook[$event_code]{$extension} |
242 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
136 | } |
284 | } |
137 | |
285 | |
138 | sub register { |
286 | sub register { |
139 | my ($base, $pkg) = @_; |
287 | my ($base, $pkg) = @_; |
140 | |
288 | |
141 | for my $idx (0 .. $#EVENT) { |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
142 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
290 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
143 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
291 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
144 | $hook[$idx]{$base} = $ref; |
292 | $hook[$idx]{$base} = $ref; |
145 | } |
293 | } |
146 | } |
294 | } |
147 | } |
295 | } |
148 | |
296 | |
… | |
… | |
151 | |
299 | |
152 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
300 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
153 | my $base = $1; |
301 | my $base = $1; |
154 | my $pkg = $1; |
302 | my $pkg = $1; |
155 | $pkg =~ s/[^[:word:]]/_/g; |
303 | $pkg =~ s/[^[:word:]]/_/g; |
156 | $pkg = "cf::ext::$pkg"; |
304 | $pkg = "ext::$pkg"; |
157 | |
305 | |
158 | warn "loading '$path' into '$pkg'\n"; |
306 | warn "loading '$path' into '$pkg'\n"; |
159 | |
307 | |
160 | open my $fh, "<:utf8", $path |
308 | open my $fh, "<:utf8", $path |
161 | or die "$path: $!"; |
309 | or die "$path: $!"; |
… | |
… | |
181 | sub unload_extension { |
329 | sub unload_extension { |
182 | my ($pkg) = @_; |
330 | my ($pkg) = @_; |
183 | |
331 | |
184 | warn "removing extension $pkg\n"; |
332 | warn "removing extension $pkg\n"; |
185 | |
333 | |
186 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
187 | $cb->($pkg); |
|
|
188 | } |
|
|
189 | |
|
|
190 | # remove hooks |
334 | # remove hooks |
191 | for my $idx (0 .. $#EVENT) { |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
192 | delete $hook[$idx]{$pkg}; |
336 | delete $hook[$idx]{$pkg}; |
193 | } |
337 | } |
194 | |
338 | |
195 | # remove commands |
339 | # remove commands |
196 | for my $name (keys %command) { |
340 | for my $name (keys %command) { |
… | |
… | |
208 | # remove extcmds |
352 | # remove extcmds |
209 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
210 | delete $extcmd{$name}; |
354 | delete $extcmd{$name}; |
211 | } |
355 | } |
212 | |
356 | |
|
|
357 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
358 | eval { |
|
|
359 | $cb->($pkg); |
|
|
360 | 1 |
|
|
361 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
362 | } |
|
|
363 | |
213 | Symbol::delete_package $pkg; |
364 | Symbol::delete_package $pkg; |
214 | } |
365 | } |
215 | |
366 | |
216 | sub load_extensions { |
367 | sub load_extensions { |
217 | my $LIBDIR = maps_directory "perl"; |
368 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
223 | 1 |
374 | 1 |
224 | } or warn "$ext not loaded: $@"; |
375 | } or warn "$ext not loaded: $@"; |
225 | } |
376 | } |
226 | } |
377 | } |
227 | |
378 | |
|
|
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 | |
228 | register_command "perl-reload", 0, sub { |
441 | register_command "perl-reload", 0, sub { |
229 | my ($who, $arg) = @_; |
442 | my ($who, $arg) = @_; |
230 | |
443 | |
231 | if ($who->flag (FLAG_WIZ)) { |
444 | if ($who->flag (FLAG_WIZ)) { |
232 | $who->message ("reloading..."); |
445 | _perl_reload { |
233 | |
446 | warn $_[0]; |
234 | warn "reloading...\n"; |
447 | $who->message ($_[0]); |
235 | eval { |
|
|
236 | # 1. cancel all watchers |
|
|
237 | $_->cancel for Event::all_watchers; |
|
|
238 | |
|
|
239 | # 2. unload all extensions |
|
|
240 | for (@exts) { |
|
|
241 | $who->message ("unloading <$_>"); |
|
|
242 | unload_extension $_; |
|
|
243 | } |
|
|
244 | |
|
|
245 | # 3. unload all modules loaded from $LIBDIR |
|
|
246 | while (my ($k, $v) = each %INC) { |
|
|
247 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
248 | |
|
|
249 | $who->message ("removing <$k>"); |
|
|
250 | delete $INC{$k}; |
|
|
251 | |
|
|
252 | $k =~ s/\.pm$//; |
|
|
253 | $k =~ s/\//::/g; |
|
|
254 | |
|
|
255 | if (my $cb = $k->can ("unload_module")) { |
|
|
256 | $cb->(); |
|
|
257 | } |
|
|
258 | |
|
|
259 | Symbol::delete_package $k; |
|
|
260 | } |
|
|
261 | |
|
|
262 | # 4. get rid of ext::, as good as possible |
|
|
263 | Symbol::delete_package "ext::$_" |
|
|
264 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
265 | |
|
|
266 | # 5. remove register_script_function callbacks |
|
|
267 | # TODO |
|
|
268 | |
|
|
269 | # 6. unload cf.pm "a bit" |
|
|
270 | delete $INC{"cf.pm"}; |
|
|
271 | |
|
|
272 | # don't, removes xs symbols, too |
|
|
273 | #Symbol::delete_package __PACKAGE__; |
|
|
274 | |
|
|
275 | # 7. reload cf.pm |
|
|
276 | $who->message ("reloading cf.pm"); |
|
|
277 | require cf; |
|
|
278 | }; |
448 | }; |
279 | warn $@ if $@; |
|
|
280 | $who->message ($@) if $@; |
|
|
281 | warn "reloaded\n"; |
|
|
282 | |
|
|
283 | $who->message ("reloaded"); |
|
|
284 | } else { |
|
|
285 | $who->message ("Intruder Alert!"); |
|
|
286 | } |
449 | } |
287 | }; |
450 | }; |
288 | |
451 | |
289 | ############################################################################# |
452 | ############################################################################# |
290 | # utility functions |
453 | # utility functions |
291 | |
454 | |
292 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
455 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
293 | |
456 | |
294 | $JSON::Syck::ImplicitUnicode = 1; |
|
|
295 | |
|
|
296 | sub from_json($) { |
457 | sub from_json($) { |
|
|
458 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
297 | JSON::Syck::Load $_[0] |
459 | JSON::Syck::Load $_[0] |
298 | } |
460 | } |
299 | |
461 | |
300 | sub to_json($) { |
462 | sub to_json($) { |
|
|
463 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
301 | JSON::Syck::Dump $_[0] |
464 | JSON::Syck::Dump $_[0] |
302 | } |
465 | } |
303 | |
466 | |
304 | ############################################################################# |
467 | ############################################################################# |
305 | # extcmd framework, basically convert ext <msg> |
468 | # extcmd framework, basically convert ext <msg> |
… | |
… | |
324 | } |
487 | } |
325 | |
488 | |
326 | ############################################################################# |
489 | ############################################################################# |
327 | # load/save/clean perl data associated with a map |
490 | # load/save/clean perl data associated with a map |
328 | |
491 | |
329 | *on_mapclean = sub { |
492 | *cf::mapsupport::on_clean = sub { |
330 | my ($map) = @_; |
493 | my ($map) = @_; |
331 | |
494 | |
332 | my $path = $map->tmpname; |
495 | my $path = $map->tmpname; |
333 | defined $path or return; |
496 | defined $path or return; |
334 | |
497 | |
335 | unlink "$path.cfperl"; |
498 | unlink "$path.cfperl"; |
336 | }; |
499 | }; |
337 | |
500 | |
338 | *on_mapin = |
501 | *cf::mapsupport::on_swapin = |
339 | *on_mapload = sub { |
502 | *cf::mapsupport::on_load = sub { |
340 | my ($map) = @_; |
503 | my ($map) = @_; |
341 | |
504 | |
342 | my $path = $map->tmpname; |
505 | my $path = $map->tmpname; |
343 | $path = $map->path unless defined $path; |
506 | $path = $map->path unless defined $path; |
344 | |
507 | |
… | |
… | |
351 | or return; # too new |
514 | or return; # too new |
352 | |
515 | |
353 | $map->_set_obs ($data->{obs}); |
516 | $map->_set_obs ($data->{obs}); |
354 | }; |
517 | }; |
355 | |
518 | |
356 | *on_mapout = sub { |
519 | *cf::mapsupport::on_swapout = sub { |
357 | my ($map) = @_; |
520 | my ($map) = @_; |
358 | |
521 | |
359 | my $path = $map->tmpname; |
522 | my $path = $map->tmpname; |
360 | $path = $map->path unless defined $path; |
523 | $path = $map->path unless defined $path; |
361 | |
524 | |
… | |
… | |
378 | } else { |
541 | } else { |
379 | unlink "$path.cfperl"; |
542 | unlink "$path.cfperl"; |
380 | } |
543 | } |
381 | }; |
544 | }; |
382 | |
545 | |
|
|
546 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
547 | |
383 | ############################################################################# |
548 | ############################################################################# |
384 | # load/save perl data associated with player->ob objects |
549 | # load/save perl data associated with player->ob objects |
385 | |
550 | |
386 | *on_player_load = sub { |
551 | sub all_objects(@) { |
|
|
552 | @_, map all_objects ($_->inv), @_ |
|
|
553 | } |
|
|
554 | |
|
|
555 | attach_to_players |
|
|
556 | on_load => sub { |
387 | my ($ob, $path) = @_; |
557 | my ($pl, $path) = @_; |
388 | |
558 | |
389 | for my $o ($ob, $ob->inv) { |
559 | for my $o (all_objects $pl->ob) { |
390 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
560 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
391 | $o->set_ob_key_value ("_perl_data"); |
561 | $o->set_ob_key_value ("_perl_data"); |
392 | |
562 | |
393 | %$o = %{ Storable::thaw pack "H*", $value }; |
563 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
564 | } |
394 | } |
565 | } |
395 | } |
566 | }, |
396 | }; |
567 | on_save => sub { |
397 | |
|
|
398 | *on_player_save = sub { |
|
|
399 | my ($ob, $path) = @_; |
568 | my ($pl, $path) = @_; |
400 | |
569 | |
401 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
570 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
402 | for grep %$_, $ob, $ob->inv; |
571 | for grep %$_, all_objects $pl->ob; |
403 | }; |
572 | }, |
|
|
573 | ; |
404 | |
574 | |
405 | ############################################################################# |
575 | ############################################################################# |
406 | # core extensions - in perl |
576 | # core extensions - in perl |
407 | |
577 | |
408 | =item cf::player::exists $login |
578 | =item cf::player::exists $login |
… | |
… | |
470 | ["cf::object::player" => qw(player)], |
640 | ["cf::object::player" => qw(player)], |
471 | ["cf::player" => qw(peaceful)], |
641 | ["cf::player" => qw(peaceful)], |
472 | ) { |
642 | ) { |
473 | no strict 'refs'; |
643 | no strict 'refs'; |
474 | my ($pkg, @funs) = @$_; |
644 | my ($pkg, @funs) = @$_; |
475 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
645 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
476 | for @funs; |
646 | for @funs; |
477 | } |
647 | } |
478 | |
648 | |
479 | sub safe_eval($;@) { |
649 | sub safe_eval($;@) { |
480 | my ($code, %vars) = @_; |
650 | my ($code, %vars) = @_; |
… | |
… | |
482 | my $qcode = $code; |
652 | my $qcode = $code; |
483 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
653 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
484 | $qcode =~ s/\n/\\n/g; |
654 | $qcode =~ s/\n/\\n/g; |
485 | |
655 | |
486 | local $_; |
656 | local $_; |
487 | local @ext::cf::_safe_eval_args = values %vars; |
657 | local @safe::cf::_safe_eval_args = values %vars; |
488 | |
658 | |
489 | $code = |
659 | $code = |
490 | "do {\n" |
660 | "do {\n" |
491 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
661 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
492 | . "#line 0 \"{$qcode}\"\n" |
662 | . "#line 0 \"{$qcode}\"\n" |
… | |
… | |
503 | |
673 | |
504 | sub register_script_function { |
674 | sub register_script_function { |
505 | my ($fun, $cb) = @_; |
675 | my ($fun, $cb) = @_; |
506 | |
676 | |
507 | no strict 'refs'; |
677 | no strict 'refs'; |
508 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
678 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
|
|
679 | } |
|
|
680 | |
|
|
681 | ############################################################################# |
|
|
682 | # the server's main() |
|
|
683 | |
|
|
684 | sub main { |
|
|
685 | Event::loop; |
509 | } |
686 | } |
510 | |
687 | |
511 | ############################################################################# |
688 | ############################################################################# |
512 | # initialisation |
689 | # initialisation |
513 | |
690 | |
… | |
… | |
515 | |
692 | |
516 | unshift @INC, $LIBDIR; |
693 | unshift @INC, $LIBDIR; |
517 | |
694 | |
518 | load_extensions; |
695 | load_extensions; |
519 | |
696 | |
|
|
697 | $TICK_WATCHER = Event->timer ( |
|
|
698 | prio => 1, |
|
|
699 | at => $NEXT_TICK || 1, |
|
|
700 | cb => sub { |
|
|
701 | cf::server_tick; # one server iteration |
|
|
702 | |
|
|
703 | my $NOW = Event::time; |
|
|
704 | $NEXT_TICK += $TICK; |
|
|
705 | |
|
|
706 | # if we are delayed by four ticks, skip them all |
|
|
707 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
708 | |
|
|
709 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
710 | $TICK_WATCHER->start; |
|
|
711 | }, |
|
|
712 | ); |
|
|
713 | |
520 | 1 |
714 | 1 |
521 | |
715 | |