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; |
… | |
… | |
61 | # guessed hierarchies |
67 | # guessed hierarchies |
62 | |
68 | |
63 | @cf::object::player::ISA = 'cf::object'; |
69 | @cf::object::player::ISA = 'cf::object'; |
64 | @cf::object::map::ISA = 'cf::object'; |
70 | @cf::object::map::ISA = 'cf::object'; |
65 | |
71 | |
|
|
72 | $Event::DIED = sub { |
|
|
73 | warn "error in event callback: @_"; |
|
|
74 | }; |
|
|
75 | |
66 | my %ext_pkg; |
76 | my %ext_pkg; |
67 | my @exts; |
77 | my @exts; |
68 | my @hook; |
78 | my @hook; |
69 | my %command; |
79 | my %command; |
|
|
80 | my %extcmd; |
70 | |
81 | |
71 | sub inject_event { |
82 | sub inject_event { |
72 | my $extension = shift; |
83 | my $extension = shift; |
73 | my $event_code = shift; |
84 | my $event_code = shift; |
74 | |
85 | |
… | |
… | |
99 | |
110 | |
100 | sub register_command { |
111 | sub register_command { |
101 | my ($name, $time, $cb) = @_; |
112 | my ($name, $time, $cb) = @_; |
102 | |
113 | |
103 | my $caller = caller; |
114 | my $caller = caller; |
104 | |
|
|
105 | warn "registering command '$name/$time' to '$caller'"; |
115 | #warn "registering command '$name/$time' to '$caller'"; |
106 | |
116 | |
107 | push @{ $command{$name} }, [$time, $cb, $caller]; |
117 | push @{ $command{$name} }, [$time, $cb, $caller]; |
108 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
118 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
109 | } |
119 | } |
110 | |
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 | |
111 | sub register { |
130 | sub register { |
112 | my ($base, $pkg) = @_; |
131 | my ($base, $pkg) = @_; |
113 | |
132 | |
114 | for my $idx (0 .. $#EVENT) { |
133 | for my $idx (0 .. $#EVENT) { |
115 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
134 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
116 | warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
135 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
117 | $hook[$idx]{$base} = $ref; |
136 | $hook[$idx]{$base} = $ref; |
118 | } |
137 | } |
119 | } |
138 | } |
120 | } |
139 | } |
121 | |
140 | |
… | |
… | |
144 | |
163 | |
145 | push @exts, $pkg; |
164 | push @exts, $pkg; |
146 | $ext_pkg{$base} = $pkg; |
165 | $ext_pkg{$base} = $pkg; |
147 | |
166 | |
148 | # no strict 'refs'; |
167 | # no strict 'refs'; |
149 | # @{"$pkg\::ISA"} = cf::ext::; |
168 | # @{"$pkg\::ISA"} = ext::; |
150 | |
169 | |
151 | register $base, $pkg; |
170 | register $base, $pkg; |
152 | } |
171 | } |
153 | |
172 | |
154 | sub unload_extension { |
173 | sub unload_extension { |
155 | my ($pkg) = @_; |
174 | my ($pkg) = @_; |
156 | |
175 | |
157 | warn "removing extension $pkg\n"; |
176 | warn "removing extension $pkg\n"; |
|
|
177 | |
|
|
178 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
179 | $cb->($pkg); |
|
|
180 | } |
158 | |
181 | |
159 | # remove hooks |
182 | # remove hooks |
160 | for my $idx (0 .. $#EVENT) { |
183 | for my $idx (0 .. $#EVENT) { |
161 | delete $hook[$idx]{$pkg}; |
184 | delete $hook[$idx]{$pkg}; |
162 | } |
185 | } |
… | |
… | |
172 | delete $command{$name}; |
195 | delete $command{$name}; |
173 | delete $COMMAND{"$name\000"}; |
196 | delete $COMMAND{"$name\000"}; |
174 | } |
197 | } |
175 | } |
198 | } |
176 | |
199 | |
|
|
200 | # remove extcmds |
|
|
201 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
|
|
202 | delete $extcmd{$name}; |
|
|
203 | } |
|
|
204 | |
177 | Symbol::delete_package $pkg; |
205 | Symbol::delete_package $pkg; |
178 | } |
206 | } |
179 | |
207 | |
180 | sub load_extensions { |
208 | sub load_extensions { |
181 | my $LIBDIR = maps_directory "perl"; |
209 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
195 | if ($who->flag (FLAG_WIZ)) { |
223 | if ($who->flag (FLAG_WIZ)) { |
196 | $who->message ("reloading..."); |
224 | $who->message ("reloading..."); |
197 | |
225 | |
198 | warn "reloading...\n"; |
226 | warn "reloading...\n"; |
199 | eval { |
227 | eval { |
|
|
228 | $_->cancel for Event::all_watchers; |
|
|
229 | |
200 | unload_extension $_ for @exts; |
230 | unload_extension $_ for @exts; |
201 | delete $INC{"cf.pm"}; |
231 | delete $INC{"cf.pm"}; |
202 | |
232 | |
203 | # don't, removes xs symbols, too |
233 | # don't, removes xs symbols, too |
204 | #Symbol::delete_package $pkg; |
234 | #Symbol::delete_package __PACKAGE__; |
205 | |
235 | |
206 | require cf; |
236 | require cf; |
207 | }; |
237 | }; |
208 | warn $@ if $@; |
238 | warn $@ if $@; |
209 | $who->message ($@) if $@; |
239 | $who->message ($@) if $@; |
… | |
… | |
212 | $who->message ("reloaded"); |
242 | $who->message ("reloaded"); |
213 | } else { |
243 | } else { |
214 | $who->message ("Intruder Alert!"); |
244 | $who->message ("Intruder Alert!"); |
215 | } |
245 | } |
216 | }; |
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 | } |
217 | |
260 | |
218 | ############################################################################# |
261 | ############################################################################# |
219 | # load/save/clean perl data associated with a map |
262 | # load/save/clean perl data associated with a map |
220 | |
263 | |
221 | *on_mapclean = sub { |
264 | *on_mapclean = sub { |
… | |
… | |
307 | for grep %$_, $ob, $ob->inv; |
350 | for grep %$_, $ob, $ob->inv; |
308 | |
351 | |
309 | unlink "$path.cfperl";#d##TODO#remove |
352 | unlink "$path.cfperl";#d##TODO#remove |
310 | }; |
353 | }; |
311 | |
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}" |
|
|
411 | ) |
|
|
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 | |
312 | register "<global>", __PACKAGE__; |
424 | register "<global>", __PACKAGE__; |
313 | |
425 | |
|
|
426 | unshift @INC, maps_directory "perl"; |
|
|
427 | |
314 | load_extensions; |
428 | load_extensions; |
315 | |
429 | |
316 | 1 |
430 | 1 |
317 | |
431 | |