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; |
6 | |
9 | |
|
|
10 | use Time::HiRes; |
7 | use Event; |
11 | use Event; |
8 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
9 | |
13 | |
10 | use strict; |
14 | use strict; |
11 | |
15 | |
12 | our %COMMAND; |
16 | our %COMMAND = (); |
13 | our @EVENT; |
17 | our @EVENT; |
|
|
18 | our @PLUGIN_EVENT; |
14 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
15 | our %PROP_IDX; |
20 | our %PROP_IDX; |
|
|
21 | our $LIBDIR = maps_directory "perl"; |
|
|
22 | |
|
|
23 | our $TICK = MAX_TIME * 1e-6; |
|
|
24 | our $TICK_WATCHER; |
|
|
25 | our $NEXT_TICK; |
16 | |
26 | |
17 | BEGIN { |
27 | BEGIN { |
18 | @EVENT = map lc, @EVENT; |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
19 | |
29 | |
20 | *CORE::GLOBAL::warn = sub { |
30 | *CORE::GLOBAL::warn = sub { |
21 | my $msg = join "", @_; |
31 | my $msg = join "", @_; |
22 | $msg .= "\n" |
32 | $msg .= "\n" |
23 | unless $msg =~ /\n$/; |
33 | unless $msg =~ /\n$/; |
… | |
… | |
61 | prop_gen MAP_PROP => "cf::map"; |
71 | prop_gen MAP_PROP => "cf::map"; |
62 | prop_gen ARCH_PROP => "cf::arch"; |
72 | prop_gen ARCH_PROP => "cf::arch"; |
63 | |
73 | |
64 | # guessed hierarchies |
74 | # guessed hierarchies |
65 | |
75 | |
66 | @cf::object::player::ISA = 'cf::object'; |
76 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
67 | @cf::object::map::ISA = 'cf::object'; |
77 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
|
|
78 | |
|
|
79 | # we bless all objects into derived classes to force a method lookup |
|
|
80 | # within the Safe compartment. |
|
|
81 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
|
|
82 | no strict 'refs'; |
|
|
83 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
|
|
84 | } |
68 | |
85 | |
69 | $Event::DIED = sub { |
86 | $Event::DIED = sub { |
70 | warn "error in event callback: @_"; |
87 | warn "error in event callback: @_"; |
71 | }; |
88 | }; |
72 | |
89 | |
… | |
… | |
74 | my @exts; |
91 | my @exts; |
75 | my @hook; |
92 | my @hook; |
76 | my %command; |
93 | my %command; |
77 | my %extcmd; |
94 | my %extcmd; |
78 | |
95 | |
|
|
96 | ############################################################################# |
|
|
97 | # "new" plug-in system |
|
|
98 | |
|
|
99 | =item cf::object::attach ... # NYI |
|
|
100 | |
|
|
101 | =item cf::attach_global ... |
|
|
102 | |
|
|
103 | =item cf::attach_to_type ... |
|
|
104 | |
|
|
105 | =item cf::attach_to_objects ... |
|
|
106 | |
|
|
107 | =item cf::attach_to_players ... |
|
|
108 | |
|
|
109 | =item cf::attach_to_maps ... |
|
|
110 | |
|
|
111 | prio => $number, # lower is earlier |
|
|
112 | on_xxx => \&cb, |
|
|
113 | package => package::, |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | # the following variables are defined in .xs and must not be re-created |
|
|
118 | our @CB_GLOBAL = (); # registry for all global events |
|
|
119 | our @CB_OBJECT = (); |
|
|
120 | our @CB_PLAYER = (); |
|
|
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
122 | our @CB_MAP = (); |
|
|
123 | |
|
|
124 | sub _attach_cb($\%$$$) { |
|
|
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
126 | |
|
|
127 | use sort 'stable'; |
|
|
128 | |
|
|
129 | $cb = [$prio, $cb]; |
|
|
130 | |
|
|
131 | @{$registry->[$event]} = sort |
|
|
132 | { $a->[0] cmp $b->[0] } |
|
|
133 | @{$registry->[$event] || []}, $cb; |
|
|
134 | |
|
|
135 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
136 | } |
|
|
137 | |
|
|
138 | # attach handles attaching event callbacks |
|
|
139 | # the only thing the caller has to do is pass the correct |
|
|
140 | # registry (== where the callback attaches to). |
|
|
141 | sub _attach(\@$\@) { |
|
|
142 | my ($registry, $klass, $arg) = @_; |
|
|
143 | |
|
|
144 | my $prio = 0; |
|
|
145 | |
|
|
146 | my %undo = ( |
|
|
147 | registry => $registry, |
|
|
148 | cb => [], |
|
|
149 | ); |
|
|
150 | |
|
|
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
152 | |
|
|
153 | while (@$arg) { |
|
|
154 | my $type = shift @$arg; |
|
|
155 | |
|
|
156 | if ($type eq "prio") { |
|
|
157 | $prio = shift @$arg; |
|
|
158 | |
|
|
159 | } elsif ($type eq "package") { |
|
|
160 | my $pkg = shift @$arg; |
|
|
161 | |
|
|
162 | while (my ($name, $id) = each %cb_id) { |
|
|
163 | if (my $cb = $pkg->can ($name)) { |
|
|
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
165 | } |
|
|
166 | } |
|
|
167 | |
|
|
168 | } elsif (exists $cb_id{$type}) { |
|
|
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
|
|
170 | |
|
|
171 | } elsif (ref $type) { |
|
|
172 | warn "attaching objects not supported, ignoring.\n"; |
|
|
173 | |
|
|
174 | } else { |
|
|
175 | shift @$arg; |
|
|
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
177 | } |
|
|
178 | } |
|
|
179 | |
|
|
180 | \%undo |
|
|
181 | } |
|
|
182 | |
|
|
183 | sub cf::object::attach { |
|
|
184 | die; |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub attach_global { |
|
|
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub attach_to_type { |
|
|
192 | my $type = shift; |
|
|
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
|
|
194 | } |
|
|
195 | |
|
|
196 | sub attach_to_objects { |
|
|
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub attach_to_players { |
|
|
201 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
202 | } |
|
|
203 | |
|
|
204 | sub attach_to_maps { |
|
|
205 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
206 | } |
|
|
207 | |
|
|
208 | our $override; |
|
|
209 | |
|
|
210 | sub override() { |
|
|
211 | $override = 1 |
|
|
212 | } |
|
|
213 | |
|
|
214 | sub invoke { |
|
|
215 | my $event = shift; |
|
|
216 | my $callbacks = shift; |
|
|
217 | |
|
|
218 | local $override; |
|
|
219 | |
|
|
220 | for (@$callbacks) { |
|
|
221 | eval { &{$_->[1]} }; |
|
|
222 | |
|
|
223 | if ($@) { |
|
|
224 | warn "$@"; |
|
|
225 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
|
|
226 | override; |
|
|
227 | } |
|
|
228 | |
|
|
229 | return 1 if $override; |
|
|
230 | } |
|
|
231 | |
|
|
232 | 0 |
|
|
233 | } |
|
|
234 | |
|
|
235 | ############################################################################# |
|
|
236 | # old plug-in events |
|
|
237 | |
79 | sub inject_event { |
238 | sub inject_event { |
80 | my $extension = shift; |
239 | my $extension = shift; |
81 | my $event_code = shift; |
240 | my $event_code = shift; |
82 | |
241 | |
83 | my $cb = $hook[$event_code]{$extension} |
242 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
125 | } |
284 | } |
126 | |
285 | |
127 | sub register { |
286 | sub register { |
128 | my ($base, $pkg) = @_; |
287 | my ($base, $pkg) = @_; |
129 | |
288 | |
130 | for my $idx (0 .. $#EVENT) { |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
131 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
290 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
132 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
291 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
133 | $hook[$idx]{$base} = $ref; |
292 | $hook[$idx]{$base} = $ref; |
134 | } |
293 | } |
135 | } |
294 | } |
136 | } |
295 | } |
137 | |
296 | |
… | |
… | |
160 | |
319 | |
161 | push @exts, $pkg; |
320 | push @exts, $pkg; |
162 | $ext_pkg{$base} = $pkg; |
321 | $ext_pkg{$base} = $pkg; |
163 | |
322 | |
164 | # no strict 'refs'; |
323 | # no strict 'refs'; |
165 | # @{"$pkg\::ISA"} = cf::ext::; |
324 | # @{"$pkg\::ISA"} = ext::; |
166 | |
325 | |
167 | register $base, $pkg; |
326 | register $base, $pkg; |
168 | } |
327 | } |
169 | |
328 | |
170 | sub unload_extension { |
329 | sub unload_extension { |
171 | my ($pkg) = @_; |
330 | my ($pkg) = @_; |
172 | |
331 | |
173 | warn "removing extension $pkg\n"; |
332 | warn "removing extension $pkg\n"; |
174 | |
333 | |
175 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
176 | $cb->($pkg); |
|
|
177 | } |
|
|
178 | |
|
|
179 | # remove hooks |
334 | # remove hooks |
180 | for my $idx (0 .. $#EVENT) { |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
181 | delete $hook[$idx]{$pkg}; |
336 | delete $hook[$idx]{$pkg}; |
182 | } |
337 | } |
183 | |
338 | |
184 | # remove commands |
339 | # remove commands |
185 | for my $name (keys %command) { |
340 | for my $name (keys %command) { |
… | |
… | |
197 | # remove extcmds |
352 | # remove extcmds |
198 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
353 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
199 | delete $extcmd{$name}; |
354 | delete $extcmd{$name}; |
200 | } |
355 | } |
201 | |
356 | |
|
|
357 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
358 | eval { |
|
|
359 | $cb->($pkg); |
|
|
360 | 1 |
|
|
361 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
362 | } |
|
|
363 | |
202 | Symbol::delete_package $pkg; |
364 | Symbol::delete_package $pkg; |
203 | } |
365 | } |
204 | |
366 | |
205 | sub load_extensions { |
367 | sub load_extensions { |
206 | my $LIBDIR = maps_directory "perl"; |
368 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
212 | 1 |
374 | 1 |
213 | } or warn "$ext not loaded: $@"; |
375 | } or warn "$ext not loaded: $@"; |
214 | } |
376 | } |
215 | } |
377 | } |
216 | |
378 | |
|
|
379 | sub _perl_reload(&) { |
|
|
380 | my ($msg) = @_; |
|
|
381 | |
|
|
382 | $msg->("reloading..."); |
|
|
383 | |
|
|
384 | eval { |
|
|
385 | # 1. cancel all watchers |
|
|
386 | $_->cancel for Event::all_watchers; |
|
|
387 | |
|
|
388 | # 2. unload all extensions |
|
|
389 | for (@exts) { |
|
|
390 | $msg->("unloading <$_>"); |
|
|
391 | unload_extension $_; |
|
|
392 | } |
|
|
393 | |
|
|
394 | # 3. unload all modules loaded from $LIBDIR |
|
|
395 | while (my ($k, $v) = each %INC) { |
|
|
396 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
397 | |
|
|
398 | $msg->("removing <$k>"); |
|
|
399 | delete $INC{$k}; |
|
|
400 | |
|
|
401 | $k =~ s/\.pm$//; |
|
|
402 | $k =~ s/\//::/g; |
|
|
403 | |
|
|
404 | if (my $cb = $k->can ("unload_module")) { |
|
|
405 | $cb->(); |
|
|
406 | } |
|
|
407 | |
|
|
408 | Symbol::delete_package $k; |
|
|
409 | } |
|
|
410 | |
|
|
411 | # 4. get rid of ext::, as good as possible |
|
|
412 | Symbol::delete_package "ext::$_" |
|
|
413 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
414 | |
|
|
415 | # 5. remove register_script_function callbacks |
|
|
416 | # TODO |
|
|
417 | |
|
|
418 | # 6. unload cf.pm "a bit" |
|
|
419 | delete $INC{"cf.pm"}; |
|
|
420 | |
|
|
421 | # don't, removes xs symbols, too |
|
|
422 | #Symbol::delete_package __PACKAGE__; |
|
|
423 | |
|
|
424 | # 7. reload cf.pm |
|
|
425 | $msg->("reloading cf.pm"); |
|
|
426 | require cf; |
|
|
427 | }; |
|
|
428 | $msg->($@) if $@; |
|
|
429 | |
|
|
430 | $msg->("reloaded"); |
|
|
431 | }; |
|
|
432 | |
|
|
433 | sub perl_reload() { |
|
|
434 | _perl_reload { |
|
|
435 | warn $_[0]; |
|
|
436 | print "$_[0]\n"; |
|
|
437 | }; |
|
|
438 | } |
|
|
439 | |
217 | register_command "perl-reload", 0, sub { |
440 | register_command "perl-reload", 0, sub { |
218 | my ($who, $arg) = @_; |
441 | my ($who, $arg) = @_; |
219 | |
442 | |
220 | if ($who->flag (FLAG_WIZ)) { |
443 | if ($who->flag (FLAG_WIZ)) { |
221 | $who->message ("reloading..."); |
444 | _perl_reload { |
222 | |
445 | warn $_[0]; |
223 | warn "reloading...\n"; |
446 | $who->message ($_[0]); |
224 | eval { |
|
|
225 | $_->cancel for Event::all_watchers; |
|
|
226 | |
|
|
227 | unload_extension $_ for @exts; |
|
|
228 | delete $INC{"cf.pm"}; |
|
|
229 | |
|
|
230 | # don't, removes xs symbols, too |
|
|
231 | #Symbol::delete_package __PACKAGE__; |
|
|
232 | |
|
|
233 | require cf; |
|
|
234 | }; |
447 | }; |
235 | warn $@ if $@; |
|
|
236 | $who->message ($@) if $@; |
|
|
237 | warn "reloaded\n"; |
|
|
238 | |
|
|
239 | $who->message ("reloaded"); |
|
|
240 | } else { |
|
|
241 | $who->message ("Intruder Alert!"); |
|
|
242 | } |
448 | } |
243 | }; |
449 | }; |
244 | |
450 | |
245 | ############################################################################# |
451 | ############################################################################# |
|
|
452 | # utility functions |
|
|
453 | |
|
|
454 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
455 | |
|
|
456 | sub from_json($) { |
|
|
457 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
458 | JSON::Syck::Load $_[0] |
|
|
459 | } |
|
|
460 | |
|
|
461 | sub to_json($) { |
|
|
462 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
463 | JSON::Syck::Dump $_[0] |
|
|
464 | } |
|
|
465 | |
|
|
466 | ############################################################################# |
246 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
467 | # extcmd framework, basically convert ext <msg> |
247 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
468 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
248 | |
469 | |
249 | sub on_extcmd { |
470 | sub on_extcmd { |
250 | my ($pl, $buf) = @_; |
471 | my ($pl, $buf) = @_; |
251 | |
472 | |
252 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
473 | my $msg = eval { from_json $buf }; |
253 | |
474 | |
254 | $extcmd{$type}[0]->($pl, $buf) |
475 | if (ref $msg) { |
255 | if $extcmd{$type}; |
476 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
477 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
478 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
479 | } |
|
|
480 | } |
|
|
481 | } else { |
|
|
482 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
483 | } |
|
|
484 | |
|
|
485 | 1 |
256 | } |
486 | } |
257 | |
487 | |
258 | ############################################################################# |
488 | ############################################################################# |
259 | # load/save/clean perl data associated with a map |
489 | # load/save/clean perl data associated with a map |
260 | |
490 | |
261 | *on_mapclean = sub { |
491 | *cf::mapsupport::on_clean = sub { |
262 | my ($map) = @_; |
492 | my ($map) = @_; |
263 | |
493 | |
264 | my $path = $map->tmpname; |
494 | my $path = $map->tmpname; |
265 | defined $path or return; |
495 | defined $path or return; |
266 | |
496 | |
267 | unlink "$path.cfperl"; |
497 | unlink "$path.cfperl"; |
268 | }; |
498 | }; |
269 | |
499 | |
270 | *on_mapin = |
500 | *cf::mapsupport::on_swapin = |
271 | *on_mapload = sub { |
501 | *cf::mapsupport::on_load = sub { |
272 | my ($map) = @_; |
502 | my ($map) = @_; |
273 | |
503 | |
274 | my $path = $map->tmpname; |
504 | my $path = $map->tmpname; |
275 | $path = $map->path unless defined $path; |
505 | $path = $map->path unless defined $path; |
276 | |
506 | |
… | |
… | |
283 | or return; # too new |
513 | or return; # too new |
284 | |
514 | |
285 | $map->_set_obs ($data->{obs}); |
515 | $map->_set_obs ($data->{obs}); |
286 | }; |
516 | }; |
287 | |
517 | |
288 | *on_mapout = sub { |
518 | *cf::mapsupport::on_swapout = sub { |
289 | my ($map) = @_; |
519 | my ($map) = @_; |
290 | |
520 | |
291 | my $path = $map->tmpname; |
521 | my $path = $map->tmpname; |
292 | $path = $map->path unless defined $path; |
522 | $path = $map->path unless defined $path; |
293 | |
523 | |
… | |
… | |
310 | } else { |
540 | } else { |
311 | unlink "$path.cfperl"; |
541 | unlink "$path.cfperl"; |
312 | } |
542 | } |
313 | }; |
543 | }; |
314 | |
544 | |
|
|
545 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
546 | |
315 | ############################################################################# |
547 | ############################################################################# |
316 | # load/save perl data associated with player->ob objects |
548 | # load/save perl data associated with player->ob objects |
317 | |
549 | |
318 | *on_player_load = sub { |
550 | sub all_objects(@) { |
|
|
551 | @_, map all_objects ($_->inv), @_ |
|
|
552 | } |
|
|
553 | |
|
|
554 | attach_to_players |
|
|
555 | on_load => sub { |
319 | my ($ob, $path) = @_; |
556 | my ($pl, $path) = @_; |
320 | |
557 | |
321 | if (open my $fh, "<:raw", "$path.cfperl") { |
558 | for my $o (all_objects $pl->ob) { |
322 | |
|
|
323 | #d##TODO#remove |
|
|
324 | |
|
|
325 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
326 | |
|
|
327 | $data->{version} <= 1 |
|
|
328 | or return; # too new |
|
|
329 | |
|
|
330 | %$ob = %{$data->{ob}}; |
|
|
331 | return; |
|
|
332 | } |
|
|
333 | |
|
|
334 | for my $o ($ob, $ob->inv) { |
|
|
335 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
559 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
336 | $o->set_ob_key_value ("_perl_data"); |
560 | $o->set_ob_key_value ("_perl_data"); |
337 | |
561 | |
338 | %$o = %{ Storable::thaw pack "H*", $value }; |
562 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
563 | } |
339 | } |
564 | } |
340 | } |
565 | }, |
341 | }; |
566 | on_save => sub { |
342 | |
|
|
343 | *on_player_save = sub { |
|
|
344 | my ($ob, $path) = @_; |
567 | my ($pl, $path) = @_; |
345 | |
568 | |
346 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
569 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
347 | for grep %$_, $ob, $ob->inv; |
570 | for grep %$_, all_objects $pl->ob; |
|
|
571 | }, |
|
|
572 | ; |
348 | |
573 | |
349 | unlink "$path.cfperl";#d##TODO#remove |
574 | ############################################################################# |
350 | }; |
575 | # core extensions - in perl |
|
|
576 | |
|
|
577 | =item cf::player::exists $login |
|
|
578 | |
|
|
579 | Returns true when the given account exists. |
|
|
580 | |
|
|
581 | =cut |
|
|
582 | |
|
|
583 | sub cf::player::exists($) { |
|
|
584 | cf::player::find $_[0] |
|
|
585 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
586 | } |
|
|
587 | |
|
|
588 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
589 | |
|
|
590 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
591 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
592 | dialogue with the given NPC character. |
|
|
593 | |
|
|
594 | =cut |
|
|
595 | |
|
|
596 | # rough implementation of a future "reply" method that works |
|
|
597 | # with dialog boxes. |
|
|
598 | sub cf::object::player::reply($$$;$) { |
|
|
599 | my ($self, $npc, $msg, $flags) = @_; |
|
|
600 | |
|
|
601 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
|
|
602 | |
|
|
603 | if ($self->{record_replies}) { |
|
|
604 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
605 | } else { |
|
|
606 | $msg = $npc->name . " says: $msg" if $npc; |
|
|
607 | $self->message ($msg, $flags); |
|
|
608 | } |
|
|
609 | } |
|
|
610 | |
|
|
611 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
612 | |
|
|
613 | Sends an ext reply to the player. |
|
|
614 | |
|
|
615 | =cut |
|
|
616 | |
|
|
617 | sub cf::player::ext_reply($$$%) { |
|
|
618 | my ($self, $id, %msg) = @_; |
|
|
619 | |
|
|
620 | $msg{msgid} = $id; |
|
|
621 | |
|
|
622 | $self->send ("ext " . to_json \%msg); |
|
|
623 | } |
|
|
624 | |
|
|
625 | ############################################################################# |
|
|
626 | # map scripting support |
|
|
627 | |
|
|
628 | our $safe = new Safe "ext"; |
|
|
629 | our $safe_hole = new Safe::Hole; |
|
|
630 | |
|
|
631 | $SIG{FPE} = 'IGNORE'; |
|
|
632 | |
|
|
633 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
|
|
634 | |
|
|
635 | # here we export the classes and methods available to script code |
|
|
636 | |
|
|
637 | for ( |
|
|
638 | ["cf::object" => qw(contr pay_amount pay_player)], |
|
|
639 | ["cf::object::player" => qw(player)], |
|
|
640 | ["cf::player" => qw(peaceful)], |
|
|
641 | ) { |
|
|
642 | no strict 'refs'; |
|
|
643 | my ($pkg, @funs) = @$_; |
|
|
644 | *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
|
|
645 | for @funs; |
|
|
646 | } |
|
|
647 | |
|
|
648 | sub safe_eval($;@) { |
|
|
649 | my ($code, %vars) = @_; |
|
|
650 | |
|
|
651 | my $qcode = $code; |
|
|
652 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
|
|
653 | $qcode =~ s/\n/\\n/g; |
|
|
654 | |
|
|
655 | local $_; |
|
|
656 | local @ext::cf::_safe_eval_args = values %vars; |
|
|
657 | |
|
|
658 | $code = |
|
|
659 | "do {\n" |
|
|
660 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
|
|
661 | . "#line 0 \"{$qcode}\"\n" |
|
|
662 | . $code |
|
|
663 | . "\n}" |
|
|
664 | ; |
|
|
665 | |
|
|
666 | sub_generation_inc; |
|
|
667 | my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); |
|
|
668 | sub_generation_inc; |
|
|
669 | |
|
|
670 | wantarray ? @res : $res[0] |
|
|
671 | } |
|
|
672 | |
|
|
673 | sub register_script_function { |
|
|
674 | my ($fun, $cb) = @_; |
|
|
675 | |
|
|
676 | no strict 'refs'; |
|
|
677 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
|
|
678 | } |
|
|
679 | |
|
|
680 | ############################################################################# |
|
|
681 | # the server's main() |
|
|
682 | |
|
|
683 | sub main { |
|
|
684 | Event::loop; |
|
|
685 | } |
|
|
686 | |
|
|
687 | ############################################################################# |
|
|
688 | # initialisation |
351 | |
689 | |
352 | register "<global>", __PACKAGE__; |
690 | register "<global>", __PACKAGE__; |
353 | |
691 | |
354 | unshift @INC, maps_directory "perl"; |
692 | unshift @INC, $LIBDIR; |
355 | |
693 | |
356 | load_extensions; |
694 | load_extensions; |
357 | |
695 | |
|
|
696 | $TICK_WATCHER = Event->timer ( |
|
|
697 | prio => 1, |
|
|
698 | at => $NEXT_TICK || 1, |
|
|
699 | cb => sub { |
|
|
700 | cf::server_tick; # one server iteration |
|
|
701 | |
|
|
702 | my $NOW = Event::time; |
|
|
703 | $NEXT_TICK += $TICK; |
|
|
704 | |
|
|
705 | # if we are delayed by four ticks, skip them all |
|
|
706 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
707 | |
|
|
708 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
709 | $TICK_WATCHER->start; |
|
|
710 | }, |
|
|
711 | ); |
|
|
712 | |
358 | 1 |
713 | 1 |
359 | |
714 | |