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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines