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.25 by root, Thu Jul 20 07:22:40 2006 UTC vs.
Revision 1.34 by root, Thu Aug 24 13:13:49 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
323}; 385};
324 386
325############################################################################# 387#############################################################################
326# load/save perl data associated with player->ob objects 388# load/save perl data associated with player->ob objects
327 389
390sub all_objects(@) {
391 @_, map all_objects ($_->inv), @_
392}
393
328*on_player_load = sub { 394*on_player_load = sub {
329 my ($ob, $path) = @_; 395 my ($ob, $path) = @_;
330 396
331 if (open my $fh, "<:raw", "$path.cfperl") { 397 for my $o (all_objects $ob) {
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) {
345 if (my $value = $o->get_ob_key_value ("_perl_data")) { 398 if (my $value = $o->get_ob_key_value ("_perl_data")) {
346 $o->set_ob_key_value ("_perl_data"); 399 $o->set_ob_key_value ("_perl_data");
347 400
348 %$o = %{ Storable::thaw pack "H*", $value }; 401 %$o = %{ Storable::thaw pack "H*", $value };
349 } 402 }
352 405
353*on_player_save = sub { 406*on_player_save = sub {
354 my ($ob, $path) = @_; 407 my ($ob, $path) = @_;
355 408
356 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 409 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
357 for grep %$_, $ob, $ob->inv; 410 for grep %$_, all_objects $ob;
358
359 unlink "$path.cfperl";#d##TODO#remove
360}; 411};
361 412
362############################################################################# 413#############################################################################
363# core extensions - in perl 414# core extensions - in perl
364 415
370 421
371sub cf::player::exists($) { 422sub cf::player::exists($) {
372 cf::player::find $_[0] 423 cf::player::find $_[0]
373 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 424 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
374} 425}
426
427=item $player->reply ($npc, $msg[, $flags])
428
429Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
430can be C<undef>. Does the right thing when the player is currently in a
431dialogue with the given NPC character.
432
433=cut
375 434
376# rough implementation of a future "reply" method that works 435# rough implementation of a future "reply" method that works
377# with dialog boxes. 436# with dialog boxes.
378sub cf::object::player::reply($$$;$) { 437sub cf::object::player::reply($$$;$) {
379 my ($self, $npc, $msg, $flags) = @_; 438 my ($self, $npc, $msg, $flags) = @_;
386 $msg = $npc->name . " says: $msg" if $npc; 445 $msg = $npc->name . " says: $msg" if $npc;
387 $self->message ($msg, $flags); 446 $self->message ($msg, $flags);
388 } 447 }
389} 448}
390 449
450=item $player->ext_reply ($msgid, $msgtype, %msg)
451
452Sends an ext reply to the player.
453
454=cut
455
456sub cf::player::ext_reply($$$%) {
457 my ($self, $id, %msg) = @_;
458
459 $msg{msgid} = $id;
460
461 $self->send ("ext " . to_json \%msg);
462}
463
391############################################################################# 464#############################################################################
392# map scripting support 465# map scripting support
393 466
394our $safe = new Safe "ext"; 467our $safe = new Safe "ext";
395our $safe_hole = new Safe::Hole; 468our $safe_hole = new Safe::Hole;
399$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 472$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
400 473
401# here we export the classes and methods available to script code 474# here we export the classes and methods available to script code
402 475
403for ( 476for (
404 ["cf::object" => qw(contr)], 477 ["cf::object" => qw(contr pay_amount pay_player)],
405 ["cf::object::player" => qw(player)], 478 ["cf::object::player" => qw(player)],
406 ["cf::player" => qw(peaceful)], 479 ["cf::player" => qw(peaceful)],
407) { 480) {
408 no strict 'refs'; 481 no strict 'refs';
409 my ($pkg, @funs) = @$_; 482 my ($pkg, @funs) = @$_;
442 no strict 'refs'; 515 no strict 'refs';
443 *{"ext::$fun"} = $safe_hole->wrap ($cb); 516 *{"ext::$fun"} = $safe_hole->wrap ($cb);
444} 517}
445 518
446############################################################################# 519#############################################################################
520# the server's main()
521
522sub run {
523 my $tick = MAX_TIME * 1e-6;
524 my $next = Event::time;
525 my $timer = Event->timer (at => $next, cb => sub {
526 cf::server_tick; # one server iteration
527
528 $next += $tick;
529 my $NOW = Event::time;
530
531 # if we are delayd by > 0.25 second, skip ticks
532 $next = $NOW if $NOW >= $next + .25;
533
534 $_[0]->w->at ($next);
535 $_[0]->w->start;
536 });
537
538 Event::loop;
539}
540
541#############################################################################
447# initialisation 542# initialisation
448 543
449register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
450 545
451unshift @INC, maps_directory "perl"; 546unshift @INC, $LIBDIR;
452 547
453load_extensions; 548load_extensions;
454 549
4551 5501
456 551

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines