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.36 by root, Thu Aug 24 14:04:29 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";
21
22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER;
24our $NEXT_TICK;
19 25
20BEGIN { 26BEGIN {
21 @EVENT = map lc, @EVENT; 27 @EVENT = map lc, @EVENT;
22 28
23 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
69@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
70@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 76@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
71 77
72# we bless all objects into derived classes to force a method lookup 78# we bless all objects into derived classes to force a method lookup
73# within the Safe compartment. 79# within the Safe compartment.
74for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { 80for 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'; 81 no strict 'refs';
76 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 82 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
77} 83}
78 84
79$Event::DIED = sub { 85$Event::DIED = sub {
179 185
180sub unload_extension { 186sub unload_extension {
181 my ($pkg) = @_; 187 my ($pkg) = @_;
182 188
183 warn "removing extension $pkg\n"; 189 warn "removing extension $pkg\n";
184
185 if (my $cb = $pkg->can ("on_unload")) {
186 $cb->($pkg);
187 }
188 190
189 # remove hooks 191 # remove hooks
190 for my $idx (0 .. $#EVENT) { 192 for my $idx (0 .. $#EVENT) {
191 delete $hook[$idx]{$pkg}; 193 delete $hook[$idx]{$pkg};
192 } 194 }
207 # remove extcmds 209 # remove extcmds
208 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
209 delete $extcmd{$name}; 211 delete $extcmd{$name};
210 } 212 }
211 213
214 if (my $cb = $pkg->can ("on_unload")) {
215 eval {
216 $cb->($pkg);
217 1
218 } or warn "$pkg unloaded, but with errors: $@";
219 }
220
212 Symbol::delete_package $pkg; 221 Symbol::delete_package $pkg;
213} 222}
214 223
215sub load_extensions { 224sub load_extensions {
216 my $LIBDIR = maps_directory "perl"; 225 my $LIBDIR = maps_directory "perl";
222 1 231 1
223 } or warn "$ext not loaded: $@"; 232 } or warn "$ext not loaded: $@";
224 } 233 }
225} 234}
226 235
236sub _perl_reload(&) {
237 my ($msg) = @_;
238
239 $msg->("reloading...");
240
241 eval {
242 # 1. cancel all watchers
243 $_->cancel for Event::all_watchers;
244
245 # 2. unload all extensions
246 for (@exts) {
247 $msg->("unloading <$_>");
248 unload_extension $_;
249 }
250
251 # 3. unload all modules loaded from $LIBDIR
252 while (my ($k, $v) = each %INC) {
253 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
254
255 $msg->("removing <$k>");
256 delete $INC{$k};
257
258 $k =~ s/\.pm$//;
259 $k =~ s/\//::/g;
260
261 if (my $cb = $k->can ("unload_module")) {
262 $cb->();
263 }
264
265 Symbol::delete_package $k;
266 }
267
268 # 4. get rid of ext::, as good as possible
269 Symbol::delete_package "ext::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
271
272 # 5. remove register_script_function callbacks
273 # TODO
274
275 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"};
277
278 # don't, removes xs symbols, too
279 #Symbol::delete_package __PACKAGE__;
280
281 # 7. reload cf.pm
282 $msg->("reloading cf.pm");
283 require cf;
284 };
285 $msg->($@) if $@;
286
287 $msg->("reloaded");
288};
289
290sub perl_reload() {
291 _perl_reload {
292 warn $_[0];
293 print "$_[0]\n";
294 };
295}
296
227register_command "perl-reload", 0, sub { 297register_command "perl-reload", 0, sub {
228 my ($who, $arg) = @_; 298 my ($who, $arg) = @_;
229 299
230 if ($who->flag (FLAG_WIZ)) { 300 if ($who->flag (FLAG_WIZ)) {
231 $who->message ("reloading..."); 301 _perl_reload {
232 302 warn $_[0];
233 warn "reloading...\n"; 303 $who->message ($_[0]);
234 eval {
235 $_->cancel for Event::all_watchers;
236
237 unload_extension $_ for @exts;
238 delete $INC{"cf.pm"};
239
240 # don't, removes xs symbols, too
241 #Symbol::delete_package __PACKAGE__;
242
243 require cf;
244 }; 304 };
245 warn $@ if $@;
246 $who->message ($@) if $@;
247 warn "reloaded\n";
248
249 $who->message ("reloaded");
250 } else {
251 $who->message ("Intruder Alert!");
252 } 305 }
253}; 306};
254 307
255############################################################################# 308#############################################################################
309# utility functions
310
311use JSON::Syck (); # TODO# replace by JSON::PC once working
312
313sub from_json($) {
314 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
315 JSON::Syck::Load $_[0]
316}
317
318sub to_json($) {
319 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
320 JSON::Syck::Dump $_[0]
321}
322
323#############################################################################
256# extcmd framework, basically convert ext <id> <pkg> arg1 args 324# extcmd framework, basically convert ext <msg>
257# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 325# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
258 326
259sub on_extcmd { 327sub on_extcmd {
260 my ($pl, $buf) = @_; 328 my ($pl, $buf) = @_;
261 329
262 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 330 my $msg = eval { from_json $buf };
263 331
264 $extcmd{$type}[0]->($pl, $buf) 332 if (ref $msg) {
265 if $extcmd{$type}; 333 if (my $cb = $extcmd{$msg->{msgtype}}) {
334 if (my %reply = $cb->[0]->($pl, $msg)) {
335 $pl->ext_reply ($msg->{msgid}, %reply);
336 }
337 }
338 } else {
339 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
340 }
341
342 1
266} 343}
267 344
268############################################################################# 345#############################################################################
269# load/save/clean perl data associated with a map 346# load/save/clean perl data associated with a map
270 347
323}; 400};
324 401
325############################################################################# 402#############################################################################
326# load/save perl data associated with player->ob objects 403# load/save perl data associated with player->ob objects
327 404
405sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_
407}
408
328*on_player_load = sub { 409*on_player_load = sub {
329 my ($ob, $path) = @_; 410 my ($ob, $path) = @_;
330 411
331 for my $o ($ob, $ob->inv) { 412 for my $o (all_objects $ob) {
332 if (my $value = $o->get_ob_key_value ("_perl_data")) { 413 if (my $value = $o->get_ob_key_value ("_perl_data")) {
333 $o->set_ob_key_value ("_perl_data"); 414 $o->set_ob_key_value ("_perl_data");
334 415
335 %$o = %{ Storable::thaw pack "H*", $value }; 416 %$o = %{ Storable::thaw pack "H*", $value };
336 } 417 }
339 420
340*on_player_save = sub { 421*on_player_save = sub {
341 my ($ob, $path) = @_; 422 my ($ob, $path) = @_;
342 423
343 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
344 for grep %$_, $ob, $ob->inv; 425 for grep %$_, all_objects $ob;
345}; 426};
346 427
347############################################################################# 428#############################################################################
348# core extensions - in perl 429# core extensions - in perl
349 430
355 436
356sub cf::player::exists($) { 437sub cf::player::exists($) {
357 cf::player::find $_[0] 438 cf::player::find $_[0]
358 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 439 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
359} 440}
441
442=item $player->reply ($npc, $msg[, $flags])
443
444Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
445can be C<undef>. Does the right thing when the player is currently in a
446dialogue with the given NPC character.
447
448=cut
360 449
361# rough implementation of a future "reply" method that works 450# rough implementation of a future "reply" method that works
362# with dialog boxes. 451# with dialog boxes.
363sub cf::object::player::reply($$$;$) { 452sub cf::object::player::reply($$$;$) {
364 my ($self, $npc, $msg, $flags) = @_; 453 my ($self, $npc, $msg, $flags) = @_;
371 $msg = $npc->name . " says: $msg" if $npc; 460 $msg = $npc->name . " says: $msg" if $npc;
372 $self->message ($msg, $flags); 461 $self->message ($msg, $flags);
373 } 462 }
374} 463}
375 464
465=item $player->ext_reply ($msgid, $msgtype, %msg)
466
467Sends an ext reply to the player.
468
469=cut
470
471sub cf::player::ext_reply($$$%) {
472 my ($self, $id, %msg) = @_;
473
474 $msg{msgid} = $id;
475
476 $self->send ("ext " . to_json \%msg);
477}
478
376############################################################################# 479#############################################################################
377# map scripting support 480# map scripting support
378 481
379our $safe = new Safe "ext"; 482our $safe = new Safe "ext";
380our $safe_hole = new Safe::Hole; 483our $safe_hole = new Safe::Hole;
384$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 487$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
385 488
386# here we export the classes and methods available to script code 489# here we export the classes and methods available to script code
387 490
388for ( 491for (
389 ["cf::object" => qw(contr)], 492 ["cf::object" => qw(contr pay_amount pay_player)],
390 ["cf::object::player" => qw(player)], 493 ["cf::object::player" => qw(player)],
391 ["cf::player" => qw(peaceful)], 494 ["cf::player" => qw(peaceful)],
392) { 495) {
393 no strict 'refs'; 496 no strict 'refs';
394 my ($pkg, @funs) = @$_; 497 my ($pkg, @funs) = @$_;
427 no strict 'refs'; 530 no strict 'refs';
428 *{"ext::$fun"} = $safe_hole->wrap ($cb); 531 *{"ext::$fun"} = $safe_hole->wrap ($cb);
429} 532}
430 533
431############################################################################# 534#############################################################################
535# the server's main()
536
537sub run {
538 Event::loop;
539}
540
541#############################################################################
432# initialisation 542# initialisation
433 543
434register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
435 545
436unshift @INC, maps_directory "perl"; 546unshift @INC, $LIBDIR;
437 547
438load_extensions; 548load_extensions;
439 549
550$TICK_WATCHER = Event->timer (
551 prio => 1,
552 at => $NEXT_TICK || 1,
553 cb => sub {
554 cf::server_tick; # one server iteration
555
556 my $NOW = Event::time;
557 $NEXT_TICK += $TICK;
558
559 # if we are delayed by > 0.25 second, skip ticks
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25;
561
562 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start;
564 },
565);
566
4401 5671
441 568

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines