… | |
… | |
14 | |
14 | |
15 | our %COMMAND; |
15 | our %COMMAND; |
16 | our @EVENT; |
16 | our @EVENT; |
17 | our %PROP_TYPE; |
17 | our %PROP_TYPE; |
18 | our %PROP_IDX; |
18 | our %PROP_IDX; |
|
|
19 | our $LIBDIR = maps_directory "perl"; |
19 | |
20 | |
20 | BEGIN { |
21 | BEGIN { |
21 | @EVENT = map lc, @EVENT; |
22 | @EVENT = map lc, @EVENT; |
22 | |
23 | |
23 | *CORE::GLOBAL::warn = sub { |
24 | *CORE::GLOBAL::warn = sub { |
… | |
… | |
230 | if ($who->flag (FLAG_WIZ)) { |
231 | if ($who->flag (FLAG_WIZ)) { |
231 | $who->message ("reloading..."); |
232 | $who->message ("reloading..."); |
232 | |
233 | |
233 | warn "reloading...\n"; |
234 | warn "reloading...\n"; |
234 | eval { |
235 | eval { |
|
|
236 | # 1. cancel all watchers |
235 | $_->cancel for Event::all_watchers; |
237 | $_->cancel for Event::all_watchers; |
236 | |
238 | |
|
|
239 | # 2. unload all extensions |
|
|
240 | for (@exts) { |
|
|
241 | $who->message ("unloading <$_>"); |
237 | unload_extension $_ for @exts; |
242 | unload_extension $_; |
|
|
243 | } |
|
|
244 | |
|
|
245 | # 3. unload all modules loaded from $LIBDIR |
|
|
246 | while (my ($k, $v) = each %INC) { |
|
|
247 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
248 | |
|
|
249 | $who->message ("removing <$k>"); |
|
|
250 | delete $INC{$k}; |
|
|
251 | |
|
|
252 | $k =~ s/\.pm$//; |
|
|
253 | $k =~ s/\//::/g; |
|
|
254 | |
|
|
255 | if (my $cb = $k->can ("unload_module")) { |
|
|
256 | $cb->(); |
|
|
257 | } |
|
|
258 | |
|
|
259 | Symbol::delete_package $k; |
|
|
260 | } |
|
|
261 | |
|
|
262 | # 4. get rid of ext::, as good as possible |
|
|
263 | Symbol::delete_package "ext::$_" |
|
|
264 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
265 | |
|
|
266 | # 5. remove register_script_function callbacks |
|
|
267 | # TODO |
|
|
268 | |
|
|
269 | # 6. unload cf.pm "a bit" |
238 | delete $INC{"cf.pm"}; |
270 | delete $INC{"cf.pm"}; |
239 | |
271 | |
240 | # don't, removes xs symbols, too |
272 | # don't, removes xs symbols, too |
241 | #Symbol::delete_package __PACKAGE__; |
273 | #Symbol::delete_package __PACKAGE__; |
242 | |
274 | |
|
|
275 | # 7. reload cf.pm |
|
|
276 | $who->message ("reloading cf.pm"); |
243 | require cf; |
277 | require cf; |
244 | }; |
278 | }; |
245 | warn $@ if $@; |
279 | warn $@ if $@; |
246 | $who->message ($@) if $@; |
280 | $who->message ($@) if $@; |
247 | warn "reloaded\n"; |
281 | warn "reloaded\n"; |
… | |
… | |
251 | $who->message ("Intruder Alert!"); |
285 | $who->message ("Intruder Alert!"); |
252 | } |
286 | } |
253 | }; |
287 | }; |
254 | |
288 | |
255 | ############################################################################# |
289 | ############################################################################# |
|
|
290 | # utility functions |
|
|
291 | |
|
|
292 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
293 | |
|
|
294 | $JSON::Syck::ImplicitUnicode = 1; |
|
|
295 | |
|
|
296 | sub from_json($) { |
|
|
297 | JSON::Syck::Load $_[0] |
|
|
298 | } |
|
|
299 | |
|
|
300 | sub to_json($) { |
|
|
301 | JSON::Syck::Dump $_[0] |
|
|
302 | } |
|
|
303 | |
|
|
304 | ############################################################################# |
256 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
305 | # extcmd framework, basically convert ext <msg> |
257 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
306 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
258 | |
307 | |
259 | sub on_extcmd { |
308 | sub on_extcmd { |
260 | my ($pl, $buf) = @_; |
309 | my ($pl, $buf) = @_; |
261 | |
310 | |
262 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
311 | my $msg = eval { from_json $buf }; |
263 | |
312 | |
264 | $extcmd{$type}[0]->($pl, $buf) |
313 | if (ref $msg) { |
265 | if $extcmd{$type}; |
314 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
315 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
316 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
317 | } |
|
|
318 | } |
|
|
319 | } else { |
|
|
320 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
321 | } |
|
|
322 | |
|
|
323 | 1 |
266 | } |
324 | } |
267 | |
325 | |
268 | ############################################################################# |
326 | ############################################################################# |
269 | # load/save/clean perl data associated with a map |
327 | # load/save/clean perl data associated with a map |
270 | |
328 | |
… | |
… | |
326 | # load/save perl data associated with player->ob objects |
384 | # load/save perl data associated with player->ob objects |
327 | |
385 | |
328 | *on_player_load = sub { |
386 | *on_player_load = sub { |
329 | my ($ob, $path) = @_; |
387 | my ($ob, $path) = @_; |
330 | |
388 | |
331 | if (open my $fh, "<:raw", "$path.cfperl") { |
|
|
332 | |
|
|
333 | #d##TODO#remove |
|
|
334 | |
|
|
335 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
336 | |
|
|
337 | $data->{version} <= 1 |
|
|
338 | or return; # too new |
|
|
339 | |
|
|
340 | %$ob = %{$data->{ob}}; |
|
|
341 | return; |
|
|
342 | } |
|
|
343 | |
|
|
344 | for my $o ($ob, $ob->inv) { |
389 | for my $o ($ob, $ob->inv) { |
345 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
390 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
346 | $o->set_ob_key_value ("_perl_data"); |
391 | $o->set_ob_key_value ("_perl_data"); |
347 | |
392 | |
348 | %$o = %{ Storable::thaw pack "H*", $value }; |
393 | %$o = %{ Storable::thaw pack "H*", $value }; |
… | |
… | |
353 | *on_player_save = sub { |
398 | *on_player_save = sub { |
354 | my ($ob, $path) = @_; |
399 | my ($ob, $path) = @_; |
355 | |
400 | |
356 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
401 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
357 | for grep %$_, $ob, $ob->inv; |
402 | for grep %$_, $ob, $ob->inv; |
358 | |
|
|
359 | unlink "$path.cfperl";#d##TODO#remove |
|
|
360 | }; |
403 | }; |
361 | |
404 | |
362 | ############################################################################# |
405 | ############################################################################# |
363 | # core extensions - in perl |
406 | # core extensions - in perl |
364 | |
407 | |
… | |
… | |
370 | |
413 | |
371 | sub cf::player::exists($) { |
414 | sub cf::player::exists($) { |
372 | cf::player::find $_[0] |
415 | cf::player::find $_[0] |
373 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
416 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
374 | } |
417 | } |
|
|
418 | |
|
|
419 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
420 | |
|
|
421 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
422 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
423 | dialogue with the given NPC character. |
|
|
424 | |
|
|
425 | =cut |
375 | |
426 | |
376 | # rough implementation of a future "reply" method that works |
427 | # rough implementation of a future "reply" method that works |
377 | # with dialog boxes. |
428 | # with dialog boxes. |
378 | sub cf::object::player::reply($$$;$) { |
429 | sub cf::object::player::reply($$$;$) { |
379 | my ($self, $npc, $msg, $flags) = @_; |
430 | my ($self, $npc, $msg, $flags) = @_; |
… | |
… | |
386 | $msg = $npc->name . " says: $msg" if $npc; |
437 | $msg = $npc->name . " says: $msg" if $npc; |
387 | $self->message ($msg, $flags); |
438 | $self->message ($msg, $flags); |
388 | } |
439 | } |
389 | } |
440 | } |
390 | |
441 | |
|
|
442 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
443 | |
|
|
444 | Sends an ext reply to the player. |
|
|
445 | |
|
|
446 | =cut |
|
|
447 | |
|
|
448 | sub cf::player::ext_reply($$$%) { |
|
|
449 | my ($self, $id, %msg) = @_; |
|
|
450 | |
|
|
451 | $msg{msgid} = $id; |
|
|
452 | |
|
|
453 | $self->send ("ext " . to_json \%msg); |
|
|
454 | } |
|
|
455 | |
391 | ############################################################################# |
456 | ############################################################################# |
392 | # map scripting support |
457 | # map scripting support |
393 | |
458 | |
394 | our $safe = new Safe "ext"; |
459 | our $safe = new Safe "ext"; |
395 | our $safe_hole = new Safe::Hole; |
460 | our $safe_hole = new Safe::Hole; |
… | |
… | |
399 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
464 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
400 | |
465 | |
401 | # here we export the classes and methods available to script code |
466 | # here we export the classes and methods available to script code |
402 | |
467 | |
403 | for ( |
468 | for ( |
404 | ["cf::object" => qw(contr)], |
469 | ["cf::object" => qw(contr pay_amount pay_player)], |
405 | ["cf::object::player" => qw(player)], |
470 | ["cf::object::player" => qw(player)], |
406 | ["cf::player" => qw(peaceful)], |
471 | ["cf::player" => qw(peaceful)], |
407 | ) { |
472 | ) { |
408 | no strict 'refs'; |
473 | no strict 'refs'; |
409 | my ($pkg, @funs) = @$_; |
474 | my ($pkg, @funs) = @$_; |
… | |
… | |
446 | ############################################################################# |
511 | ############################################################################# |
447 | # initialisation |
512 | # initialisation |
448 | |
513 | |
449 | register "<global>", __PACKAGE__; |
514 | register "<global>", __PACKAGE__; |
450 | |
515 | |
451 | unshift @INC, maps_directory "perl"; |
516 | unshift @INC, $LIBDIR; |
452 | |
517 | |
453 | load_extensions; |
518 | load_extensions; |
454 | |
519 | |
455 | 1 |
520 | 1 |
456 | |
521 | |