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; |
… | |
… | |
60 | |
66 | |
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'; |
|
|
71 | |
|
|
72 | $Event::DIED = sub { |
|
|
73 | warn "error in event callback: @_"; |
|
|
74 | }; |
65 | |
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; |
… | |
… | |
100 | |
110 | |
101 | sub register_command { |
111 | sub register_command { |
102 | my ($name, $time, $cb) = @_; |
112 | my ($name, $time, $cb) = @_; |
103 | |
113 | |
104 | my $caller = caller; |
114 | my $caller = caller; |
105 | |
|
|
106 | warn "registering command '$name/$time' to '$caller'"; |
115 | #warn "registering command '$name/$time' to '$caller'"; |
107 | |
116 | |
108 | push @{ $command{$name} }, [$time, $cb, $caller]; |
117 | push @{ $command{$name} }, [$time, $cb, $caller]; |
109 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
118 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
110 | } |
119 | } |
111 | |
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 | |
112 | sub register { |
130 | sub register { |
113 | my ($base, $pkg) = @_; |
131 | my ($base, $pkg) = @_; |
114 | |
132 | |
115 | for my $idx (0 .. $#EVENT) { |
133 | for my $idx (0 .. $#EVENT) { |
116 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
134 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
117 | warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
135 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
118 | $hook[$idx]{$base} = $ref; |
136 | $hook[$idx]{$base} = $ref; |
119 | } |
137 | } |
120 | } |
138 | } |
121 | } |
139 | } |
122 | |
140 | |
… | |
… | |
145 | |
163 | |
146 | push @exts, $pkg; |
164 | push @exts, $pkg; |
147 | $ext_pkg{$base} = $pkg; |
165 | $ext_pkg{$base} = $pkg; |
148 | |
166 | |
149 | # no strict 'refs'; |
167 | # no strict 'refs'; |
150 | # @{"$pkg\::ISA"} = cf::ext::; |
168 | # @{"$pkg\::ISA"} = ext::; |
151 | |
169 | |
152 | register $base, $pkg; |
170 | register $base, $pkg; |
153 | } |
171 | } |
154 | |
172 | |
155 | sub unload_extension { |
173 | sub unload_extension { |
156 | my ($pkg) = @_; |
174 | my ($pkg) = @_; |
157 | |
175 | |
158 | warn "removing extension $pkg\n"; |
176 | warn "removing extension $pkg\n"; |
|
|
177 | |
|
|
178 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
179 | $cb->($pkg); |
|
|
180 | } |
159 | |
181 | |
160 | # remove hooks |
182 | # remove hooks |
161 | for my $idx (0 .. $#EVENT) { |
183 | for my $idx (0 .. $#EVENT) { |
162 | delete $hook[$idx]{$pkg}; |
184 | delete $hook[$idx]{$pkg}; |
163 | } |
185 | } |
… | |
… | |
174 | delete $COMMAND{"$name\000"}; |
196 | delete $COMMAND{"$name\000"}; |
175 | } |
197 | } |
176 | } |
198 | } |
177 | |
199 | |
178 | # remove extcmds |
200 | # remove extcmds |
179 | for my $name (keys %command) { |
201 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
180 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
|
|
181 | |
|
|
182 | if (@cb) { |
|
|
183 | $command{$name} = \@cb; |
|
|
184 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
|
|
185 | } else { |
|
|
186 | delete $command{$name}; |
202 | delete $extcmd{$name}; |
187 | delete $COMMAND{"$name\000"}; |
|
|
188 | } |
|
|
189 | } |
203 | } |
190 | |
|
|
191 | |
204 | |
192 | Symbol::delete_package $pkg; |
205 | Symbol::delete_package $pkg; |
193 | } |
206 | } |
194 | |
207 | |
195 | sub load_extensions { |
208 | sub load_extensions { |
… | |
… | |
210 | if ($who->flag (FLAG_WIZ)) { |
223 | if ($who->flag (FLAG_WIZ)) { |
211 | $who->message ("reloading..."); |
224 | $who->message ("reloading..."); |
212 | |
225 | |
213 | warn "reloading...\n"; |
226 | warn "reloading...\n"; |
214 | eval { |
227 | eval { |
|
|
228 | $_->cancel for Event::all_watchers; |
|
|
229 | |
215 | unload_extension $_ for @exts; |
230 | unload_extension $_ for @exts; |
216 | delete $INC{"cf.pm"}; |
231 | delete $INC{"cf.pm"}; |
217 | |
232 | |
218 | # don't, removes xs symbols, too |
233 | # don't, removes xs symbols, too |
219 | #Symbol::delete_package $pkg; |
234 | #Symbol::delete_package __PACKAGE__; |
220 | |
235 | |
221 | require cf; |
236 | require cf; |
222 | }; |
237 | }; |
223 | warn $@ if $@; |
238 | warn $@ if $@; |
224 | $who->message ($@) if $@; |
239 | $who->message ($@) if $@; |
… | |
… | |
235 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
250 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
236 | |
251 | |
237 | sub on_extcmd { |
252 | sub on_extcmd { |
238 | my ($pl, $buf) = @_; |
253 | my ($pl, $buf) = @_; |
239 | |
254 | |
240 | my ($id, $pkg, $name, $data) = split / /, $buf, 4; |
255 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
241 | |
256 | |
242 | if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) { |
257 | $extcmd{$type}[0]->($pl, $buf) |
243 | $method->($pl, $id, $buf); |
258 | if $extcmd{$type}; |
244 | } |
|
|
245 | } |
259 | } |
246 | |
260 | |
247 | ############################################################################# |
261 | ############################################################################# |
248 | # load/save/clean perl data associated with a map |
262 | # load/save/clean perl data associated with a map |
249 | |
263 | |
… | |
… | |
336 | for grep %$_, $ob, $ob->inv; |
350 | for grep %$_, $ob, $ob->inv; |
337 | |
351 | |
338 | unlink "$path.cfperl";#d##TODO#remove |
352 | unlink "$path.cfperl";#d##TODO#remove |
339 | }; |
353 | }; |
340 | |
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 | if ($self->{record_replies}) { |
|
|
377 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
378 | } else { |
|
|
379 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
380 | $self->message ($msg, $flags); |
|
|
381 | } |
|
|
382 | } |
|
|
383 | |
|
|
384 | ############################################################################# |
|
|
385 | # map scripting support |
|
|
386 | |
|
|
387 | our $safe = new Safe "ext"; |
|
|
388 | our $safe_hole = new Safe::Hole; |
|
|
389 | |
|
|
390 | $SIG{FPE} = 'IGNORE'; |
|
|
391 | |
|
|
392 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
393 | |
|
|
394 | # here we would export the classes and methods available to script code |
|
|
395 | #@ext::cf::object::player::ISA = @cf::object::player::ISA; |
|
|
396 | #@ext::cf::object::map::ISA = @cf::object::map::ISA; |
|
|
397 | |
|
|
398 | sub safe_eval($;@) { |
|
|
399 | my ($code, %vars) = @_; |
|
|
400 | |
|
|
401 | my $qcode = $code; |
|
|
402 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
403 | $qcode =~ s/\n/\\n/g; |
|
|
404 | |
|
|
405 | local $_; |
|
|
406 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
407 | |
|
|
408 | $safe->reval ( |
|
|
409 | "do {\n" |
|
|
410 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
411 | . "#line 0 \"{$qcode}\"\n" |
|
|
412 | . $code |
|
|
413 | . "\n}" |
|
|
414 | ) |
|
|
415 | } |
|
|
416 | |
|
|
417 | sub register_script_function { |
|
|
418 | my ($fun, $cb) = @_; |
|
|
419 | |
|
|
420 | no strict 'refs'; |
|
|
421 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
422 | } |
|
|
423 | |
|
|
424 | ############################################################################# |
|
|
425 | # initialisation |
|
|
426 | |
341 | register "<global>", __PACKAGE__; |
427 | register "<global>", __PACKAGE__; |
342 | |
428 | |
|
|
429 | unshift @INC, maps_directory "perl"; |
|
|
430 | |
343 | load_extensions; |
431 | load_extensions; |
344 | |
432 | |
345 | 1 |
433 | 1 |
346 | |
434 | |