1 | package cf; |
1 | package cf; |
2 | |
2 | |
3 | use Symbol; |
3 | use Symbol; |
4 | use List::Util; |
4 | use List::Util; |
5 | use Storable; |
5 | use Storable; |
|
|
6 | use Opcode; |
|
|
7 | use Safe; |
|
|
8 | use Safe::Hole; |
|
|
9 | |
|
|
10 | use Time::HiRes; |
6 | use Event; |
11 | use Event; |
|
|
12 | $Event::Eval = 1; # no idea why this is required, but it is |
7 | |
13 | |
8 | use strict; |
14 | use strict; |
9 | |
15 | |
10 | our %COMMAND; |
16 | our %COMMAND; |
11 | our @EVENT; |
17 | our @EVENT; |
|
|
18 | our @PLUGIN_EVENT; |
12 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
13 | our %PROP_IDX; |
20 | our %PROP_IDX; |
|
|
21 | our $LIBDIR = maps_directory "perl"; |
|
|
22 | |
|
|
23 | our $TICK = MAX_TIME * 1e-6; |
|
|
24 | our $TICK_WATCHER; |
|
|
25 | our $NEXT_TICK; |
14 | |
26 | |
15 | BEGIN { |
27 | BEGIN { |
16 | @EVENT = map lc, @EVENT; |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
17 | |
29 | |
18 | *CORE::GLOBAL::warn = sub { |
30 | *CORE::GLOBAL::warn = sub { |
19 | my $msg = join "", @_; |
31 | my $msg = join "", @_; |
20 | $msg .= "\n" |
32 | $msg .= "\n" |
21 | unless $msg =~ /\n$/; |
33 | unless $msg =~ /\n$/; |
… | |
… | |
59 | prop_gen MAP_PROP => "cf::map"; |
71 | prop_gen MAP_PROP => "cf::map"; |
60 | prop_gen ARCH_PROP => "cf::arch"; |
72 | prop_gen ARCH_PROP => "cf::arch"; |
61 | |
73 | |
62 | # guessed hierarchies |
74 | # guessed hierarchies |
63 | |
75 | |
64 | @cf::object::player::ISA = 'cf::object'; |
76 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
65 | @cf::object::map::ISA = 'cf::object'; |
77 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
|
|
79 | # we bless all objects into derived classes to force a method lookup |
|
|
80 | # 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)) { |
|
|
82 | no strict 'refs'; |
|
|
83 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
|
|
84 | } |
66 | |
85 | |
67 | $Event::DIED = sub { |
86 | $Event::DIED = sub { |
68 | warn "error in event callback: @_"; |
87 | warn "error in event callback: @_"; |
69 | }; |
88 | }; |
70 | |
89 | |
… | |
… | |
123 | } |
142 | } |
124 | |
143 | |
125 | sub register { |
144 | sub register { |
126 | my ($base, $pkg) = @_; |
145 | my ($base, $pkg) = @_; |
127 | |
146 | |
128 | for my $idx (0 .. $#EVENT) { |
147 | for my $idx (0 .. $#PLUGIN_EVENT) { |
129 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
148 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
130 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
149 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
131 | $hook[$idx]{$base} = $ref; |
150 | $hook[$idx]{$base} = $ref; |
132 | } |
151 | } |
133 | } |
152 | } |
134 | } |
153 | } |
135 | |
154 | |
… | |
… | |
158 | |
177 | |
159 | push @exts, $pkg; |
178 | push @exts, $pkg; |
160 | $ext_pkg{$base} = $pkg; |
179 | $ext_pkg{$base} = $pkg; |
161 | |
180 | |
162 | # no strict 'refs'; |
181 | # no strict 'refs'; |
163 | # @{"$pkg\::ISA"} = cf::ext::; |
182 | # @{"$pkg\::ISA"} = ext::; |
164 | |
183 | |
165 | register $base, $pkg; |
184 | register $base, $pkg; |
166 | } |
185 | } |
167 | |
186 | |
168 | sub unload_extension { |
187 | sub unload_extension { |
169 | my ($pkg) = @_; |
188 | my ($pkg) = @_; |
170 | |
189 | |
171 | warn "removing extension $pkg\n"; |
190 | warn "removing extension $pkg\n"; |
172 | |
191 | |
173 | # remove hooks |
192 | # remove hooks |
174 | for my $idx (0 .. $#EVENT) { |
193 | for my $idx (0 .. $#PLUGIN_EVENT) { |
175 | delete $hook[$idx]{$pkg}; |
194 | delete $hook[$idx]{$pkg}; |
176 | } |
195 | } |
177 | |
196 | |
178 | # remove commands |
197 | # remove commands |
179 | for my $name (keys %command) { |
198 | for my $name (keys %command) { |
… | |
… | |
191 | # remove extcmds |
210 | # remove extcmds |
192 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
211 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
193 | delete $extcmd{$name}; |
212 | delete $extcmd{$name}; |
194 | } |
213 | } |
195 | |
214 | |
|
|
215 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
216 | eval { |
|
|
217 | $cb->($pkg); |
|
|
218 | 1 |
|
|
219 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
220 | } |
|
|
221 | |
196 | Symbol::delete_package $pkg; |
222 | Symbol::delete_package $pkg; |
197 | } |
223 | } |
198 | |
224 | |
199 | sub load_extensions { |
225 | sub load_extensions { |
200 | my $LIBDIR = maps_directory "perl"; |
226 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
206 | 1 |
232 | 1 |
207 | } or warn "$ext not loaded: $@"; |
233 | } or warn "$ext not loaded: $@"; |
208 | } |
234 | } |
209 | } |
235 | } |
210 | |
236 | |
|
|
237 | sub _perl_reload(&) { |
|
|
238 | my ($msg) = @_; |
|
|
239 | |
|
|
240 | $msg->("reloading..."); |
|
|
241 | |
|
|
242 | eval { |
|
|
243 | # 1. cancel all watchers |
|
|
244 | $_->cancel for Event::all_watchers; |
|
|
245 | |
|
|
246 | # 2. unload all extensions |
|
|
247 | for (@exts) { |
|
|
248 | $msg->("unloading <$_>"); |
|
|
249 | unload_extension $_; |
|
|
250 | } |
|
|
251 | |
|
|
252 | # 3. unload all modules loaded from $LIBDIR |
|
|
253 | while (my ($k, $v) = each %INC) { |
|
|
254 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
255 | |
|
|
256 | $msg->("removing <$k>"); |
|
|
257 | delete $INC{$k}; |
|
|
258 | |
|
|
259 | $k =~ s/\.pm$//; |
|
|
260 | $k =~ s/\//::/g; |
|
|
261 | |
|
|
262 | if (my $cb = $k->can ("unload_module")) { |
|
|
263 | $cb->(); |
|
|
264 | } |
|
|
265 | |
|
|
266 | Symbol::delete_package $k; |
|
|
267 | } |
|
|
268 | |
|
|
269 | # 4. get rid of ext::, as good as possible |
|
|
270 | Symbol::delete_package "ext::$_" |
|
|
271 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
272 | |
|
|
273 | # 5. remove register_script_function callbacks |
|
|
274 | # TODO |
|
|
275 | |
|
|
276 | # 6. unload cf.pm "a bit" |
|
|
277 | delete $INC{"cf.pm"}; |
|
|
278 | |
|
|
279 | # don't, removes xs symbols, too |
|
|
280 | #Symbol::delete_package __PACKAGE__; |
|
|
281 | |
|
|
282 | # 7. reload cf.pm |
|
|
283 | $msg->("reloading cf.pm"); |
|
|
284 | require cf; |
|
|
285 | }; |
|
|
286 | $msg->($@) if $@; |
|
|
287 | |
|
|
288 | $msg->("reloaded"); |
|
|
289 | }; |
|
|
290 | |
|
|
291 | sub perl_reload() { |
|
|
292 | _perl_reload { |
|
|
293 | warn $_[0]; |
|
|
294 | print "$_[0]\n"; |
|
|
295 | }; |
|
|
296 | } |
|
|
297 | |
211 | register_command "perl-reload", 0, sub { |
298 | register_command "perl-reload", 0, sub { |
212 | my ($who, $arg) = @_; |
299 | my ($who, $arg) = @_; |
213 | |
300 | |
214 | if ($who->flag (FLAG_WIZ)) { |
301 | if ($who->flag (FLAG_WIZ)) { |
215 | $who->message ("reloading..."); |
302 | _perl_reload { |
216 | |
303 | warn $_[0]; |
217 | warn "reloading...\n"; |
304 | $who->message ($_[0]); |
218 | eval { |
|
|
219 | unload_extension $_ for @exts; |
|
|
220 | delete $INC{"cf.pm"}; |
|
|
221 | |
|
|
222 | # don't, removes xs symbols, too |
|
|
223 | #Symbol::delete_package $pkg; |
|
|
224 | |
|
|
225 | require cf; |
|
|
226 | }; |
305 | }; |
227 | warn $@ if $@; |
|
|
228 | $who->message ($@) if $@; |
|
|
229 | warn "reloaded\n"; |
|
|
230 | |
|
|
231 | $who->message ("reloaded"); |
|
|
232 | } else { |
|
|
233 | $who->message ("Intruder Alert!"); |
|
|
234 | } |
306 | } |
235 | }; |
307 | }; |
236 | |
308 | |
237 | ############################################################################# |
309 | ############################################################################# |
|
|
310 | # utility functions |
|
|
311 | |
|
|
312 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
313 | |
|
|
314 | sub from_json($) { |
|
|
315 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
316 | JSON::Syck::Load $_[0] |
|
|
317 | } |
|
|
318 | |
|
|
319 | sub to_json($) { |
|
|
320 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
321 | JSON::Syck::Dump $_[0] |
|
|
322 | } |
|
|
323 | |
|
|
324 | ############################################################################# |
238 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
325 | # extcmd framework, basically convert ext <msg> |
239 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
326 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
240 | |
327 | |
241 | sub on_extcmd { |
328 | sub on_extcmd { |
242 | my ($pl, $buf) = @_; |
329 | my ($pl, $buf) = @_; |
243 | |
330 | |
244 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
331 | my $msg = eval { from_json $buf }; |
245 | |
332 | |
246 | $extcmd{$type}[0]->($pl, $buf) |
333 | if (ref $msg) { |
247 | if $extcmd{$type}; |
334 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
335 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
336 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
337 | } |
|
|
338 | } |
|
|
339 | } else { |
|
|
340 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
341 | } |
|
|
342 | |
|
|
343 | 1 |
248 | } |
344 | } |
249 | |
345 | |
250 | ############################################################################# |
346 | ############################################################################# |
251 | # load/save/clean perl data associated with a map |
347 | # load/save/clean perl data associated with a map |
252 | |
348 | |
… | |
… | |
305 | }; |
401 | }; |
306 | |
402 | |
307 | ############################################################################# |
403 | ############################################################################# |
308 | # load/save perl data associated with player->ob objects |
404 | # load/save perl data associated with player->ob objects |
309 | |
405 | |
|
|
406 | sub all_objects(@) { |
|
|
407 | @_, map all_objects ($_->inv), @_ |
|
|
408 | } |
|
|
409 | |
310 | *on_player_load = sub { |
410 | *on_player_load = sub { |
311 | my ($ob, $path) = @_; |
411 | my ($ob, $path) = @_; |
312 | |
412 | |
313 | if (open my $fh, "<:raw", "$path.cfperl") { |
413 | for my $o (all_objects $ob) { |
314 | |
|
|
315 | #d##TODO#remove |
|
|
316 | |
|
|
317 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
318 | |
|
|
319 | $data->{version} <= 1 |
|
|
320 | or return; # too new |
|
|
321 | |
|
|
322 | %$ob = %{$data->{ob}}; |
|
|
323 | return; |
|
|
324 | } |
|
|
325 | |
|
|
326 | for my $o ($ob, $ob->inv) { |
|
|
327 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
414 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
328 | $o->set_ob_key_value ("_perl_data"); |
415 | $o->set_ob_key_value ("_perl_data"); |
329 | |
416 | |
330 | %$o = %{ Storable::thaw pack "H*", $value }; |
417 | %$o = %{ Storable::thaw pack "H*", $value }; |
331 | } |
418 | } |
… | |
… | |
334 | |
421 | |
335 | *on_player_save = sub { |
422 | *on_player_save = sub { |
336 | my ($ob, $path) = @_; |
423 | my ($ob, $path) = @_; |
337 | |
424 | |
338 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
425 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
339 | for grep %$_, $ob, $ob->inv; |
426 | for grep %$_, all_objects $ob; |
340 | |
|
|
341 | unlink "$path.cfperl";#d##TODO#remove |
|
|
342 | }; |
427 | }; |
|
|
428 | |
|
|
429 | ############################################################################# |
|
|
430 | # core extensions - in perl |
|
|
431 | |
|
|
432 | =item cf::player::exists $login |
|
|
433 | |
|
|
434 | Returns true when the given account exists. |
|
|
435 | |
|
|
436 | =cut |
|
|
437 | |
|
|
438 | sub cf::player::exists($) { |
|
|
439 | cf::player::find $_[0] |
|
|
440 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
441 | } |
|
|
442 | |
|
|
443 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
444 | |
|
|
445 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
446 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
447 | dialogue with the given NPC character. |
|
|
448 | |
|
|
449 | =cut |
|
|
450 | |
|
|
451 | # rough implementation of a future "reply" method that works |
|
|
452 | # with dialog boxes. |
|
|
453 | sub cf::object::player::reply($$$;$) { |
|
|
454 | my ($self, $npc, $msg, $flags) = @_; |
|
|
455 | |
|
|
456 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
457 | |
|
|
458 | if ($self->{record_replies}) { |
|
|
459 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
460 | } else { |
|
|
461 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
462 | $self->message ($msg, $flags); |
|
|
463 | } |
|
|
464 | } |
|
|
465 | |
|
|
466 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
467 | |
|
|
468 | Sends an ext reply to the player. |
|
|
469 | |
|
|
470 | =cut |
|
|
471 | |
|
|
472 | sub cf::player::ext_reply($$$%) { |
|
|
473 | my ($self, $id, %msg) = @_; |
|
|
474 | |
|
|
475 | $msg{msgid} = $id; |
|
|
476 | |
|
|
477 | $self->send ("ext " . to_json \%msg); |
|
|
478 | } |
|
|
479 | |
|
|
480 | ############################################################################# |
|
|
481 | # map scripting support |
|
|
482 | |
|
|
483 | our $safe = new Safe "ext"; |
|
|
484 | our $safe_hole = new Safe::Hole; |
|
|
485 | |
|
|
486 | $SIG{FPE} = 'IGNORE'; |
|
|
487 | |
|
|
488 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
489 | |
|
|
490 | # here we export the classes and methods available to script code |
|
|
491 | |
|
|
492 | for ( |
|
|
493 | ["cf::object" => qw(contr pay_amount pay_player)], |
|
|
494 | ["cf::object::player" => qw(player)], |
|
|
495 | ["cf::player" => qw(peaceful)], |
|
|
496 | ) { |
|
|
497 | no strict 'refs'; |
|
|
498 | my ($pkg, @funs) = @$_; |
|
|
499 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
500 | for @funs; |
|
|
501 | } |
|
|
502 | |
|
|
503 | sub safe_eval($;@) { |
|
|
504 | my ($code, %vars) = @_; |
|
|
505 | |
|
|
506 | my $qcode = $code; |
|
|
507 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
508 | $qcode =~ s/\n/\\n/g; |
|
|
509 | |
|
|
510 | local $_; |
|
|
511 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
512 | |
|
|
513 | $code = |
|
|
514 | "do {\n" |
|
|
515 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
516 | . "#line 0 \"{$qcode}\"\n" |
|
|
517 | . $code |
|
|
518 | . "\n}" |
|
|
519 | ; |
|
|
520 | |
|
|
521 | sub_generation_inc; |
|
|
522 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
|
|
523 | sub_generation_inc; |
|
|
524 | |
|
|
525 | wantarray ? @res : $res[0] |
|
|
526 | } |
|
|
527 | |
|
|
528 | sub register_script_function { |
|
|
529 | my ($fun, $cb) = @_; |
|
|
530 | |
|
|
531 | no strict 'refs'; |
|
|
532 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
533 | } |
|
|
534 | |
|
|
535 | ############################################################################# |
|
|
536 | # the server's main() |
|
|
537 | |
|
|
538 | sub run { |
|
|
539 | Event::loop; |
|
|
540 | } |
|
|
541 | |
|
|
542 | ############################################################################# |
|
|
543 | # initialisation |
343 | |
544 | |
344 | register "<global>", __PACKAGE__; |
545 | register "<global>", __PACKAGE__; |
345 | |
546 | |
346 | unshift @INC, maps_directory "perl"; |
547 | unshift @INC, $LIBDIR; |
347 | |
548 | |
348 | load_extensions; |
549 | load_extensions; |
349 | |
550 | |
|
|
551 | $TICK_WATCHER = Event->timer ( |
|
|
552 | prio => 1, |
|
|
553 | at => $NEXT_TICK || 1, |
|
|
554 | cb => sub { |
|
|
555 | cf::server_tick; # one server iteration |
|
|
556 | |
|
|
557 | my $NOW = Event::time; |
|
|
558 | $NEXT_TICK += $TICK; |
|
|
559 | |
|
|
560 | # if we are delayed by four ticks, skip them all |
|
|
561 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
562 | |
|
|
563 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
564 | $TICK_WATCHER->start; |
|
|
565 | }, |
|
|
566 | ); |
|
|
567 | |
350 | 1 |
568 | 1 |
351 | |
569 | |