ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.26 by root, Thu Jul 20 22:03:36 2006 UTC vs.
Revision 1.28 by root, Sun Jul 30 17:42:14 2006 UTC

14 14
15our %COMMAND; 15our %COMMAND;
16our @EVENT; 16our @EVENT;
17our %PROP_TYPE; 17our %PROP_TYPE;
18our %PROP_IDX; 18our %PROP_IDX;
19our $LIBDIR = maps_directory "perl";
19 20
20BEGIN { 21BEGIN {
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
292use JSON::Syck (); # TODO# replace by JSON::PC once working
293
294$JSON::Syck::ImplicitUnicode = 1;
295
296sub from_json($) {
297 JSON::Syck::Load $_[0]
298}
299
300sub 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
259sub on_extcmd { 308sub 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
356sub cf::player::exists($) { 414sub cf::player::exists($) {
357 cf::player::find $_[0] 415 cf::player::find $_[0]
358 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;
359} 417}
360 418
419=item $player->reply ($npc, $msg[, $flags])
420
421Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
422can be C<undef>. Does the right thing when the player is currently in a
423dialogue with the given NPC character.
424
425=cut
426
361# rough implementation of a future "reply" method that works 427# rough implementation of a future "reply" method that works
362# with dialog boxes. 428# with dialog boxes.
363sub cf::object::player::reply($$$;$) { 429sub cf::object::player::reply($$$;$) {
364 my ($self, $npc, $msg, $flags) = @_; 430 my ($self, $npc, $msg, $flags) = @_;
365 431
371 $msg = $npc->name . " says: $msg" if $npc; 437 $msg = $npc->name . " says: $msg" if $npc;
372 $self->message ($msg, $flags); 438 $self->message ($msg, $flags);
373 } 439 }
374} 440}
375 441
442=item $player->ext_reply ($msgid, $msgtype, %msg)
443
444Sends an ext reply to the player.
445
446=cut
447
448sub cf::player::ext_reply($$$%) {
449 my ($self, $id, %msg) = @_;
450
451 $msg{msgid} = $id;
452
453 $self->send ("ext " . to_json \%msg);
454}
455
376############################################################################# 456#############################################################################
377# map scripting support 457# map scripting support
378 458
379our $safe = new Safe "ext"; 459our $safe = new Safe "ext";
380our $safe_hole = new Safe::Hole; 460our $safe_hole = new Safe::Hole;
384$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));
385 465
386# here we export the classes and methods available to script code 466# here we export the classes and methods available to script code
387 467
388for ( 468for (
389 ["cf::object" => qw(contr)], 469 ["cf::object" => qw(contr pay_amount pay_player)],
390 ["cf::object::player" => qw(player)], 470 ["cf::object::player" => qw(player)],
391 ["cf::player" => qw(peaceful)], 471 ["cf::player" => qw(peaceful)],
392) { 472) {
393 no strict 'refs'; 473 no strict 'refs';
394 my ($pkg, @funs) = @$_; 474 my ($pkg, @funs) = @$_;
431############################################################################# 511#############################################################################
432# initialisation 512# initialisation
433 513
434register "<global>", __PACKAGE__; 514register "<global>", __PACKAGE__;
435 515
436unshift @INC, maps_directory "perl"; 516unshift @INC, $LIBDIR;
437 517
438load_extensions; 518load_extensions;
439 519
4401 5201
441 521

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines