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