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.32 by root, Tue Aug 15 18:07:25 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use Time::HiRes;
10use Event; 11use Event;
11$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
12 13
13use strict; 14use strict;
14 15
15our %COMMAND; 16our %COMMAND;
16our @EVENT; 17our @EVENT;
17our %PROP_TYPE; 18our %PROP_TYPE;
18our %PROP_IDX; 19our %PROP_IDX;
20our $LIBDIR = maps_directory "perl";
19 21
20BEGIN { 22BEGIN {
21 @EVENT = map lc, @EVENT; 23 @EVENT = map lc, @EVENT;
22 24
23 *CORE::GLOBAL::warn = sub { 25 *CORE::GLOBAL::warn = sub {
69@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 71@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
70@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 72@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
71 73
72# we bless all objects into derived classes to force a method lookup 74# we bless all objects into derived classes to force a method lookup
73# within the Safe compartment. 75# within the Safe compartment.
74for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { 76for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
75 no strict 'refs'; 77 no strict 'refs';
76 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 78 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
77} 79}
78 80
79$Event::DIED = sub { 81$Event::DIED = sub {
179 181
180sub unload_extension { 182sub unload_extension {
181 my ($pkg) = @_; 183 my ($pkg) = @_;
182 184
183 warn "removing extension $pkg\n"; 185 warn "removing extension $pkg\n";
184
185 if (my $cb = $pkg->can ("on_unload")) {
186 $cb->($pkg);
187 }
188 186
189 # remove hooks 187 # remove hooks
190 for my $idx (0 .. $#EVENT) { 188 for my $idx (0 .. $#EVENT) {
191 delete $hook[$idx]{$pkg}; 189 delete $hook[$idx]{$pkg};
192 } 190 }
207 # remove extcmds 205 # remove extcmds
208 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 206 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
209 delete $extcmd{$name}; 207 delete $extcmd{$name};
210 } 208 }
211 209
210 if (my $cb = $pkg->can ("on_unload")) {
211 eval {
212 $cb->($pkg);
213 1
214 } or warn "$pkg unloaded, but with errors: $@";
215 }
216
212 Symbol::delete_package $pkg; 217 Symbol::delete_package $pkg;
213} 218}
214 219
215sub load_extensions { 220sub load_extensions {
216 my $LIBDIR = maps_directory "perl"; 221 my $LIBDIR = maps_directory "perl";
230 if ($who->flag (FLAG_WIZ)) { 235 if ($who->flag (FLAG_WIZ)) {
231 $who->message ("reloading..."); 236 $who->message ("reloading...");
232 237
233 warn "reloading...\n"; 238 warn "reloading...\n";
234 eval { 239 eval {
240 # 1. cancel all watchers
235 $_->cancel for Event::all_watchers; 241 $_->cancel for Event::all_watchers;
236 242
243 # 2. unload all extensions
244 for (@exts) {
245 $who->message ("unloading <$_>");
237 unload_extension $_ for @exts; 246 unload_extension $_;
247 }
248
249 # 3. unload all modules loaded from $LIBDIR
250 while (my ($k, $v) = each %INC) {
251 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
252
253 $who->message ("removing <$k>");
254 delete $INC{$k};
255
256 $k =~ s/\.pm$//;
257 $k =~ s/\//::/g;
258
259 if (my $cb = $k->can ("unload_module")) {
260 $cb->();
261 }
262
263 Symbol::delete_package $k;
264 }
265
266 # 4. get rid of ext::, as good as possible
267 Symbol::delete_package "ext::$_"
268 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
269
270 # 5. remove register_script_function callbacks
271 # TODO
272
273 # 6. unload cf.pm "a bit"
238 delete $INC{"cf.pm"}; 274 delete $INC{"cf.pm"};
239 275
240 # don't, removes xs symbols, too 276 # don't, removes xs symbols, too
241 #Symbol::delete_package __PACKAGE__; 277 #Symbol::delete_package __PACKAGE__;
242 278
279 # 7. reload cf.pm
280 $who->message ("reloading cf.pm");
243 require cf; 281 require cf;
244 }; 282 };
245 warn $@ if $@; 283 warn $@ if $@;
246 $who->message ($@) if $@; 284 $who->message ($@) if $@;
247 warn "reloaded\n"; 285 warn "reloaded\n";
251 $who->message ("Intruder Alert!"); 289 $who->message ("Intruder Alert!");
252 } 290 }
253}; 291};
254 292
255############################################################################# 293#############################################################################
294# utility functions
295
296use JSON::Syck (); # TODO# replace by JSON::PC once working
297
298sub from_json($) {
299 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
300 JSON::Syck::Load $_[0]
301}
302
303sub to_json($) {
304 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
305 JSON::Syck::Dump $_[0]
306}
307
308#############################################################################
256# extcmd framework, basically convert ext <id> <pkg> arg1 args 309# extcmd framework, basically convert ext <msg>
257# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 310# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
258 311
259sub on_extcmd { 312sub on_extcmd {
260 my ($pl, $buf) = @_; 313 my ($pl, $buf) = @_;
261 314
262 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 315 my $msg = eval { from_json $buf };
263 316
264 $extcmd{$type}[0]->($pl, $buf) 317 if (ref $msg) {
265 if $extcmd{$type}; 318 if (my $cb = $extcmd{$msg->{msgtype}}) {
319 if (my %reply = $cb->[0]->($pl, $msg)) {
320 $pl->ext_reply ($msg->{msgid}, %reply);
321 }
322 }
323 } else {
324 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
325 }
326
327 1
266} 328}
267 329
268############################################################################# 330#############################################################################
269# load/save/clean perl data associated with a map 331# load/save/clean perl data associated with a map
270 332
345}; 407};
346 408
347############################################################################# 409#############################################################################
348# core extensions - in perl 410# core extensions - in perl
349 411
412my $delta_timer = Event->timer (
413 parked => 1,
414 prio => Event::PRIO_HIGH,
415 cb => sub { Event::unloop (undef) },
416);
417
418sub sleep_delta($) {
419 $delta_timer->at (Event::time + $_[0]);
420 $delta_timer->start;
421 Event::loop;
422}
423
350=item cf::player::exists $login 424=item cf::player::exists $login
351 425
352Returns true when the given account exists. 426Returns true when the given account exists.
353 427
354=cut 428=cut
355 429
356sub cf::player::exists($) { 430sub cf::player::exists($) {
357 cf::player::find $_[0] 431 cf::player::find $_[0]
358 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 432 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
359} 433}
434
435=item $player->reply ($npc, $msg[, $flags])
436
437Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
438can be C<undef>. Does the right thing when the player is currently in a
439dialogue with the given NPC character.
440
441=cut
360 442
361# rough implementation of a future "reply" method that works 443# rough implementation of a future "reply" method that works
362# with dialog boxes. 444# with dialog boxes.
363sub cf::object::player::reply($$$;$) { 445sub cf::object::player::reply($$$;$) {
364 my ($self, $npc, $msg, $flags) = @_; 446 my ($self, $npc, $msg, $flags) = @_;
371 $msg = $npc->name . " says: $msg" if $npc; 453 $msg = $npc->name . " says: $msg" if $npc;
372 $self->message ($msg, $flags); 454 $self->message ($msg, $flags);
373 } 455 }
374} 456}
375 457
458=item $player->ext_reply ($msgid, $msgtype, %msg)
459
460Sends an ext reply to the player.
461
462=cut
463
464sub cf::player::ext_reply($$$%) {
465 my ($self, $id, %msg) = @_;
466
467 $msg{msgid} = $id;
468
469 $self->send ("ext " . to_json \%msg);
470}
471
376############################################################################# 472#############################################################################
377# map scripting support 473# map scripting support
378 474
379our $safe = new Safe "ext"; 475our $safe = new Safe "ext";
380our $safe_hole = new Safe::Hole; 476our $safe_hole = new Safe::Hole;
384$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 480$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
385 481
386# here we export the classes and methods available to script code 482# here we export the classes and methods available to script code
387 483
388for ( 484for (
389 ["cf::object" => qw(contr)], 485 ["cf::object" => qw(contr pay_amount pay_player)],
390 ["cf::object::player" => qw(player)], 486 ["cf::object::player" => qw(player)],
391 ["cf::player" => qw(peaceful)], 487 ["cf::player" => qw(peaceful)],
392) { 488) {
393 no strict 'refs'; 489 no strict 'refs';
394 my ($pkg, @funs) = @$_; 490 my ($pkg, @funs) = @$_;
431############################################################################# 527#############################################################################
432# initialisation 528# initialisation
433 529
434register "<global>", __PACKAGE__; 530register "<global>", __PACKAGE__;
435 531
436unshift @INC, maps_directory "perl"; 532unshift @INC, $LIBDIR;
437 533
438load_extensions; 534load_extensions;
439 535
4401 5361
441 537

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines