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 Event; |
|
|
11 | $Event::Eval = 1; # no idea why this is required, but it is |
6 | |
12 | |
7 | use strict; |
13 | use strict; |
8 | |
14 | |
9 | our %COMMAND; |
15 | our %COMMAND; |
10 | our @EVENT; |
16 | our @EVENT; |
… | |
… | |
22 | print STDERR "cfperl: $msg"; |
28 | print STDERR "cfperl: $msg"; |
23 | LOG llevError, "cfperl: $msg"; |
29 | LOG llevError, "cfperl: $msg"; |
24 | }; |
30 | }; |
25 | } |
31 | } |
26 | |
32 | |
|
|
33 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
|
|
34 | |
27 | # generate property mutators |
35 | # generate property mutators |
28 | sub prop_gen { |
36 | sub prop_gen { |
29 | my ($prefix, $class) = @_; |
37 | my ($prefix, $class) = @_; |
30 | |
38 | |
31 | no strict 'refs'; |
39 | no strict 'refs'; |
… | |
… | |
41 | $_[0]->get_property ($type, $idx) |
49 | $_[0]->get_property ($type, $idx) |
42 | }; |
50 | }; |
43 | |
51 | |
44 | *{"$class\::set_$sub"} = sub { |
52 | *{"$class\::set_$sub"} = sub { |
45 | $_[0]->set_property ($type, $idx, $_[1]); |
53 | $_[0]->set_property ($type, $idx, $_[1]); |
46 | }; |
54 | } unless $ignore_set{$prop}; |
47 | } |
55 | } |
48 | } |
56 | } |
49 | |
57 | |
50 | # auto-generate most of the API |
58 | # auto-generate most of the API |
51 | |
59 | |
… | |
… | |
56 | prop_gen MAP_PROP => "cf::map"; |
64 | prop_gen MAP_PROP => "cf::map"; |
57 | prop_gen ARCH_PROP => "cf::arch"; |
65 | prop_gen ARCH_PROP => "cf::arch"; |
58 | |
66 | |
59 | # guessed hierarchies |
67 | # guessed hierarchies |
60 | |
68 | |
61 | @cf::object::player::ISA = 'cf::object'; |
69 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
62 | @cf::object::map::ISA = 'cf::object'; |
70 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
71 | |
|
|
72 | # we bless all objects into derived classes to force a method lookup |
|
|
73 | # within the Safe compartment. |
|
|
74 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { |
|
|
75 | no strict 'refs'; |
|
|
76 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
|
|
77 | } |
|
|
78 | |
|
|
79 | $Event::DIED = sub { |
|
|
80 | warn "error in event callback: @_"; |
|
|
81 | }; |
63 | |
82 | |
64 | my %ext_pkg; |
83 | my %ext_pkg; |
65 | my @exts; |
84 | my @exts; |
66 | my @hook; |
85 | my @hook; |
67 | my %command; |
86 | my %command; |
|
|
87 | my %extcmd; |
68 | |
88 | |
69 | sub inject_event { |
89 | sub inject_event { |
70 | my ($data) = @_; |
90 | my $extension = shift; |
|
|
91 | my $event_code = shift; |
71 | |
92 | |
72 | my $cb = $hook[$data->{event_code}]{$data->{extension}} |
93 | my $cb = $hook[$event_code]{$extension} |
73 | or return; |
94 | or return; |
74 | |
95 | |
75 | $cb->($data) |
96 | &$cb |
76 | } |
97 | } |
77 | |
98 | |
78 | sub inject_global_event { |
99 | sub inject_global_event { |
79 | my ($data) = @_; |
100 | my $event = shift; |
80 | |
101 | |
81 | my $cb = $hook[$data->{event_code}] |
102 | my $cb = $hook[$event] |
82 | or return; |
103 | or return; |
83 | |
104 | |
84 | $_->($data) for values %$cb; |
105 | List::Util::max map &$_, values %$cb |
85 | |
|
|
86 | () |
|
|
87 | } |
106 | } |
88 | |
107 | |
89 | sub inject_command { |
108 | sub inject_command { |
90 | my ($name, $obj, $params) = @_; |
109 | my ($name, $obj, $params) = @_; |
91 | |
110 | |
… | |
… | |
98 | |
117 | |
99 | sub register_command { |
118 | sub register_command { |
100 | my ($name, $time, $cb) = @_; |
119 | my ($name, $time, $cb) = @_; |
101 | |
120 | |
102 | my $caller = caller; |
121 | my $caller = caller; |
103 | |
|
|
104 | warn "registering command '$name/$time' to '$caller'"; |
122 | #warn "registering command '$name/$time' to '$caller'"; |
105 | |
123 | |
106 | push @{ $command{$name} }, [$time, $cb, $caller]; |
124 | push @{ $command{$name} }, [$time, $cb, $caller]; |
107 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
125 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
108 | } |
126 | } |
109 | |
127 | |
|
|
128 | sub register_extcmd { |
|
|
129 | my ($name, $cb) = @_; |
|
|
130 | |
|
|
131 | my $caller = caller; |
|
|
132 | #warn "registering extcmd '$name' to '$caller'"; |
|
|
133 | |
|
|
134 | $extcmd{$name} = [$cb, $caller]; |
|
|
135 | } |
|
|
136 | |
110 | sub register { |
137 | sub register { |
111 | my ($base, $pkg) = @_; |
138 | my ($base, $pkg) = @_; |
112 | |
139 | |
113 | for my $idx (0 .. $#EVENT) { |
140 | for my $idx (0 .. $#EVENT) { |
114 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
141 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
115 | warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
142 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
116 | $hook[$idx]{$base} = $ref; |
143 | $hook[$idx]{$base} = $ref; |
117 | } |
144 | } |
118 | } |
145 | } |
119 | } |
146 | } |
120 | |
147 | |
… | |
… | |
143 | |
170 | |
144 | push @exts, $pkg; |
171 | push @exts, $pkg; |
145 | $ext_pkg{$base} = $pkg; |
172 | $ext_pkg{$base} = $pkg; |
146 | |
173 | |
147 | # no strict 'refs'; |
174 | # no strict 'refs'; |
148 | # @{"$pkg\::ISA"} = cf::ext::; |
175 | # @{"$pkg\::ISA"} = ext::; |
149 | |
176 | |
150 | register $base, $pkg; |
177 | register $base, $pkg; |
151 | } |
178 | } |
152 | |
179 | |
153 | sub unload_extension { |
180 | sub unload_extension { |
154 | my ($pkg) = @_; |
181 | my ($pkg) = @_; |
155 | |
182 | |
156 | warn "removing extension $pkg\n"; |
183 | warn "removing extension $pkg\n"; |
|
|
184 | |
|
|
185 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
186 | $cb->($pkg); |
|
|
187 | } |
157 | |
188 | |
158 | # remove hooks |
189 | # remove hooks |
159 | for my $idx (0 .. $#EVENT) { |
190 | for my $idx (0 .. $#EVENT) { |
160 | delete $hook[$idx]{$pkg}; |
191 | delete $hook[$idx]{$pkg}; |
161 | } |
192 | } |
… | |
… | |
171 | delete $command{$name}; |
202 | delete $command{$name}; |
172 | delete $COMMAND{"$name\000"}; |
203 | delete $COMMAND{"$name\000"}; |
173 | } |
204 | } |
174 | } |
205 | } |
175 | |
206 | |
|
|
207 | # remove extcmds |
|
|
208 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
|
|
209 | delete $extcmd{$name}; |
|
|
210 | } |
|
|
211 | |
176 | Symbol::delete_package $pkg; |
212 | Symbol::delete_package $pkg; |
177 | } |
213 | } |
178 | |
214 | |
179 | sub load_extensions { |
215 | sub load_extensions { |
180 | my $LIBDIR = maps_directory "perl"; |
216 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
194 | if ($who->flag (FLAG_WIZ)) { |
230 | if ($who->flag (FLAG_WIZ)) { |
195 | $who->message ("reloading..."); |
231 | $who->message ("reloading..."); |
196 | |
232 | |
197 | warn "reloading...\n"; |
233 | warn "reloading...\n"; |
198 | eval { |
234 | eval { |
|
|
235 | $_->cancel for Event::all_watchers; |
|
|
236 | |
199 | unload_extension $_ for @exts; |
237 | unload_extension $_ for @exts; |
200 | delete $INC{"cf.pm"}; |
238 | delete $INC{"cf.pm"}; |
201 | |
239 | |
202 | # don't, removes xs symbols, too |
240 | # don't, removes xs symbols, too |
203 | #Symbol::delete_package $pkg; |
241 | #Symbol::delete_package __PACKAGE__; |
204 | |
242 | |
205 | require cf; |
243 | require cf; |
206 | }; |
244 | }; |
207 | warn $@ if $@; |
245 | warn $@ if $@; |
208 | $who->message ($@) if $@; |
246 | $who->message ($@) if $@; |
… | |
… | |
213 | $who->message ("Intruder Alert!"); |
251 | $who->message ("Intruder Alert!"); |
214 | } |
252 | } |
215 | }; |
253 | }; |
216 | |
254 | |
217 | ############################################################################# |
255 | ############################################################################# |
|
|
256 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
|
|
257 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
258 | |
|
|
259 | sub on_extcmd { |
|
|
260 | my ($pl, $buf) = @_; |
|
|
261 | |
|
|
262 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
|
|
263 | |
|
|
264 | $extcmd{$type}[0]->($pl, $buf) |
|
|
265 | if $extcmd{$type}; |
|
|
266 | } |
|
|
267 | |
|
|
268 | ############################################################################# |
218 | # load/save/clean perl data associated with a map |
269 | # load/save/clean perl data associated with a map |
219 | |
270 | |
220 | *on_mapclean = sub { |
271 | *on_mapclean = sub { |
221 | my $map = shift->{map}; |
272 | my ($map) = @_; |
222 | |
273 | |
223 | my $path = $map->tmpname; |
274 | my $path = $map->tmpname; |
224 | defined $path or return; |
275 | defined $path or return; |
225 | |
276 | |
226 | unlink "$path.cfperl"; |
277 | unlink "$path.cfperl"; |
227 | }; |
278 | }; |
228 | |
279 | |
229 | *on_mapin = |
280 | *on_mapin = |
230 | *on_mapload = sub { |
281 | *on_mapload = sub { |
231 | my $map = shift->{map}; |
282 | my ($map) = @_; |
232 | |
283 | |
233 | my $path = $map->tmpname; |
284 | my $path = $map->tmpname; |
234 | $path = $map->path unless defined $path; |
285 | $path = $map->path unless defined $path; |
235 | |
286 | |
236 | open my $fh, "<:raw", "$path.cfperl" |
287 | open my $fh, "<:raw", "$path.cfperl" |
… | |
… | |
243 | |
294 | |
244 | $map->_set_obs ($data->{obs}); |
295 | $map->_set_obs ($data->{obs}); |
245 | }; |
296 | }; |
246 | |
297 | |
247 | *on_mapout = sub { |
298 | *on_mapout = sub { |
248 | my $map = shift->{map}; |
299 | my ($map) = @_; |
249 | |
300 | |
250 | my $path = $map->tmpname; |
301 | my $path = $map->tmpname; |
251 | $path = $map->path unless defined $path; |
302 | $path = $map->path unless defined $path; |
252 | |
303 | |
253 | my $obs = $map->_get_obs; |
304 | my $obs = $map->_get_obs; |
… | |
… | |
273 | |
324 | |
274 | ############################################################################# |
325 | ############################################################################# |
275 | # load/save perl data associated with player->ob objects |
326 | # load/save perl data associated with player->ob objects |
276 | |
327 | |
277 | *on_player_load = sub { |
328 | *on_player_load = sub { |
278 | my ($event) = @_; |
329 | my ($ob, $path) = @_; |
279 | my $path = $event->{message}; |
|
|
280 | my $ob = $event->{who}; |
|
|
281 | |
330 | |
282 | open my $fh, "<:raw", "$path.cfperl" |
331 | for my $o ($ob, $ob->inv) { |
283 | or return; # no perl data |
332 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
333 | $o->set_ob_key_value ("_perl_data"); |
284 | |
334 | |
285 | my $data = Storable::thaw do { local $/; <$fh> }; |
335 | %$o = %{ Storable::thaw pack "H*", $value }; |
286 | |
336 | } |
287 | $data->{version} <= 1 |
337 | } |
288 | or return; # too new |
|
|
289 | |
|
|
290 | %$ob = %{$data->{ob}}; |
|
|
291 | }; |
338 | }; |
292 | |
339 | |
293 | *on_player_save = sub { |
340 | *on_player_save = sub { |
294 | my ($event) = @_; |
341 | my ($ob, $path) = @_; |
295 | my $path = $event->{message}; |
|
|
296 | my $ob = $event->{who}; |
|
|
297 | |
342 | |
298 | if (keys %$ob) { |
343 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
299 | open my $fh, ">:raw", "$path.cfperl" |
344 | for grep %$_, $ob, $ob->inv; |
300 | or die "$path.cfperl: $!"; |
345 | }; |
301 | |
346 | |
302 | stat $path; |
347 | ############################################################################# |
|
|
348 | # core extensions - in perl |
303 | |
349 | |
304 | print $fh Storable::nfreeze { |
350 | =item cf::player::exists $login |
305 | size => (stat _)[7], |
|
|
306 | time => (stat _)[9], |
|
|
307 | version => 1, |
|
|
308 | ob => $ob, |
|
|
309 | }; |
|
|
310 | |
351 | |
311 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
352 | Returns true when the given account exists. |
|
|
353 | |
|
|
354 | =cut |
|
|
355 | |
|
|
356 | sub cf::player::exists($) { |
|
|
357 | cf::player::find $_[0] |
|
|
358 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
359 | } |
|
|
360 | |
|
|
361 | # rough implementation of a future "reply" method that works |
|
|
362 | # with dialog boxes. |
|
|
363 | sub cf::object::player::reply($$$;$) { |
|
|
364 | my ($self, $npc, $msg, $flags) = @_; |
|
|
365 | |
|
|
366 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
367 | |
|
|
368 | if ($self->{record_replies}) { |
|
|
369 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
312 | } else { |
370 | } else { |
313 | unlink "$path.cfperl"; |
371 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
372 | $self->message ($msg, $flags); |
|
|
373 | } |
|
|
374 | } |
|
|
375 | |
|
|
376 | ############################################################################# |
|
|
377 | # map scripting support |
|
|
378 | |
|
|
379 | our $safe = new Safe "ext"; |
|
|
380 | our $safe_hole = new Safe::Hole; |
|
|
381 | |
|
|
382 | $SIG{FPE} = 'IGNORE'; |
|
|
383 | |
|
|
384 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
385 | |
|
|
386 | # here we export the classes and methods available to script code |
|
|
387 | |
|
|
388 | for ( |
|
|
389 | ["cf::object" => qw(contr)], |
|
|
390 | ["cf::object::player" => qw(player)], |
|
|
391 | ["cf::player" => qw(peaceful)], |
|
|
392 | ) { |
|
|
393 | no strict 'refs'; |
|
|
394 | my ($pkg, @funs) = @$_; |
|
|
395 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
396 | for @funs; |
|
|
397 | } |
|
|
398 | |
|
|
399 | sub safe_eval($;@) { |
|
|
400 | my ($code, %vars) = @_; |
|
|
401 | |
|
|
402 | my $qcode = $code; |
|
|
403 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
404 | $qcode =~ s/\n/\\n/g; |
|
|
405 | |
|
|
406 | local $_; |
|
|
407 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
408 | |
|
|
409 | $code = |
|
|
410 | "do {\n" |
|
|
411 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
412 | . "#line 0 \"{$qcode}\"\n" |
|
|
413 | . $code |
|
|
414 | . "\n}" |
314 | } |
415 | ; |
315 | }; |
416 | |
|
|
417 | sub_generation_inc; |
|
|
418 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
|
|
419 | sub_generation_inc; |
|
|
420 | |
|
|
421 | wantarray ? @res : $res[0] |
|
|
422 | } |
|
|
423 | |
|
|
424 | sub register_script_function { |
|
|
425 | my ($fun, $cb) = @_; |
|
|
426 | |
|
|
427 | no strict 'refs'; |
|
|
428 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
429 | } |
|
|
430 | |
|
|
431 | ############################################################################# |
|
|
432 | # initialisation |
316 | |
433 | |
317 | register "<global>", __PACKAGE__; |
434 | register "<global>", __PACKAGE__; |
318 | |
435 | |
|
|
436 | unshift @INC, maps_directory "perl"; |
|
|
437 | |
319 | load_extensions; |
438 | load_extensions; |
320 | |
439 | |
321 | 1 |
440 | 1 |
322 | |
441 | |