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