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