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.40 by root, Fri Aug 25 15:21:57 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 {
84my @exts; 91my @exts;
85my @hook; 92my @hook;
86my %command; 93my %command;
87my %extcmd; 94my %extcmd;
88 95
96#############################################################################
97# "new" plug-in system
98
99=item cf::object::attach ... # NYI
100
101=item cf::attach_global ...
102
103=item cf::attach_to_type ...
104
105=item cf::attach_to_objects ...
106
107=item cf::attach_to_players ...
108
109=item cf::attach_to_maps ...
110
111 prio => $number, # lower is earlier
112 on_xxx => \&cb,
113 package => package::,
114
115=cut
116
117# the following variables are defined in .xs and must not be re-created
118our @CB_GLOBAL = (); # registry for all global events
119our @CB_OBJECT = ();
120our @CB_PLAYER = ();
121our @CB_TYPE = (); # registry for type (cf-object class) based events
122our @CB_MAP = ();
123
124sub _attach_cb($\%$$$) {
125 my ($registry, $undo, $event, $prio, $cb) = @_;
126
127 use sort 'stable';
128
129 $cb = [$prio, $cb];
130
131 @{$registry->[$event]} = sort
132 { $a->[0] cmp $b->[0] }
133 @{$registry->[$event] || []}, $cb;
134
135 push @{$undo->{cb}}, [$event, $cb];
136}
137
138# attach handles attaching event callbacks
139# the only thing the caller has to do is pass the correct
140# registry (== where the callback attaches to).
141sub _attach(\@$\@) {
142 my ($registry, $klass, $arg) = @_;
143
144 my $prio = 0;
145
146 my %undo = (
147 registry => $registry,
148 cb => [],
149 );
150
151 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
152
153 while (@$arg) {
154 my $type = shift @$arg;
155
156 if ($type eq "prio") {
157 $prio = shift @$arg;
158
159 } elsif ($type eq "package") {
160 my $pkg = shift @$arg;
161
162 while (my ($name, $id) = each %cb_id) {
163 if (my $cb = $pkg->can ($name)) {
164 _attach_cb $registry, %undo, $id, $prio, $cb;
165 }
166 }
167
168 } elsif (exists $cb_id{$type}) {
169 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg;
170
171 } elsif (ref $type) {
172 warn "attaching objects not supported, ignoring.\n";
173
174 } else {
175 shift @$arg;
176 warn "attach argument '$type' not supported, ignoring.\n";
177 }
178 }
179
180 \%undo
181}
182
183sub cf::object::attach {
184 die;
185}
186
187sub attach_global {
188 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
189}
190
191sub attach_to_type {
192 my $type = shift;
193 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
194}
195
196sub attach_to_objects {
197 _attach @CB_OBJECT, KLASS_OBJECT, @_
198}
199
200sub attach_to_players {
201 _attach @CB_PLAYER, KLASS_PLAYER, @_
202}
203
204sub attach_to_maps {
205 _attach @CB_MAP, KLASS_MAP, @_
206}
207
208our $override;
209
210sub override() {
211 $override = 1
212}
213
214sub invoke {
215 my $event = shift;
216 my $callbacks = shift;
217
218 local $override;
219
220 for (@$callbacks) {
221 eval { &{$_->[1]} };
222
223 if ($@) {
224 warn "$@";
225 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
226 override;
227 }
228
229 return 1 if $override;
230 }
231
232 0
233}
234
235#############################################################################
236# old plug-in events
237
89sub inject_event { 238sub inject_event {
90 my $extension = shift; 239 my $extension = shift;
91 my $event_code = shift; 240 my $event_code = shift;
92 241
93 my $cb = $hook[$event_code]{$extension} 242 my $cb = $hook[$event_code]{$extension}
135} 284}
136 285
137sub register { 286sub register {
138 my ($base, $pkg) = @_; 287 my ($base, $pkg) = @_;
139 288
140 for my $idx (0 .. $#EVENT) { 289 for my $idx (0 .. $#PLUGIN_EVENT) {
141 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 290 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
142 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 291 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
143 $hook[$idx]{$base} = $ref; 292 $hook[$idx]{$base} = $ref;
144 } 293 }
145 } 294 }
146} 295}
147 296
180sub unload_extension { 329sub unload_extension {
181 my ($pkg) = @_; 330 my ($pkg) = @_;
182 331
183 warn "removing extension $pkg\n"; 332 warn "removing extension $pkg\n";
184 333
185 if (my $cb = $pkg->can ("on_unload")) {
186 $cb->($pkg);
187 }
188
189 # remove hooks 334 # remove hooks
190 for my $idx (0 .. $#EVENT) { 335 for my $idx (0 .. $#PLUGIN_EVENT) {
191 delete $hook[$idx]{$pkg}; 336 delete $hook[$idx]{$pkg};
192 } 337 }
193 338
194 # remove commands 339 # remove commands
195 for my $name (keys %command) { 340 for my $name (keys %command) {
207 # remove extcmds 352 # remove extcmds
208 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 353 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
209 delete $extcmd{$name}; 354 delete $extcmd{$name};
210 } 355 }
211 356
357 if (my $cb = $pkg->can ("on_unload")) {
358 eval {
359 $cb->($pkg);
360 1
361 } or warn "$pkg unloaded, but with errors: $@";
362 }
363
212 Symbol::delete_package $pkg; 364 Symbol::delete_package $pkg;
213} 365}
214 366
215sub load_extensions { 367sub load_extensions {
216 my $LIBDIR = maps_directory "perl"; 368 my $LIBDIR = maps_directory "perl";
222 1 374 1
223 } or warn "$ext not loaded: $@"; 375 } or warn "$ext not loaded: $@";
224 } 376 }
225} 377}
226 378
379sub _perl_reload(&) {
380 my ($msg) = @_;
381
382 $msg->("reloading...");
383
384 eval {
385 # 1. cancel all watchers
386 $_->cancel for Event::all_watchers;
387
388 # 2. unload all extensions
389 for (@exts) {
390 $msg->("unloading <$_>");
391 unload_extension $_;
392 }
393
394 # 3. unload all modules loaded from $LIBDIR
395 while (my ($k, $v) = each %INC) {
396 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
397
398 $msg->("removing <$k>");
399 delete $INC{$k};
400
401 $k =~ s/\.pm$//;
402 $k =~ s/\//::/g;
403
404 if (my $cb = $k->can ("unload_module")) {
405 $cb->();
406 }
407
408 Symbol::delete_package $k;
409 }
410
411 # 4. get rid of ext::, as good as possible
412 Symbol::delete_package "ext::$_"
413 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
414
415 # 5. remove register_script_function callbacks
416 # TODO
417
418 # 6. unload cf.pm "a bit"
419 delete $INC{"cf.pm"};
420
421 # don't, removes xs symbols, too
422 #Symbol::delete_package __PACKAGE__;
423
424 # 7. reload cf.pm
425 $msg->("reloading cf.pm");
426 require cf;
427 };
428 $msg->($@) if $@;
429
430 $msg->("reloaded");
431};
432
433sub perl_reload() {
434 _perl_reload {
435 warn $_[0];
436 print "$_[0]\n";
437 };
438}
439
227register_command "perl-reload", 0, sub { 440register_command "perl-reload", 0, sub {
228 my ($who, $arg) = @_; 441 my ($who, $arg) = @_;
229 442
230 if ($who->flag (FLAG_WIZ)) { 443 if ($who->flag (FLAG_WIZ)) {
231 $who->message ("reloading..."); 444 _perl_reload {
232 445 warn $_[0];
233 warn "reloading...\n"; 446 $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 }; 447 };
245 warn $@ if $@;
246 $who->message ($@) if $@;
247 warn "reloaded\n";
248
249 $who->message ("reloaded");
250 } else {
251 $who->message ("Intruder Alert!");
252 } 448 }
253}; 449};
254 450
255############################################################################# 451#############################################################################
452# utility functions
453
454use JSON::Syck (); # TODO# replace by JSON::PC once working
455
456sub from_json($) {
457 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
458 JSON::Syck::Load $_[0]
459}
460
461sub to_json($) {
462 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
463 JSON::Syck::Dump $_[0]
464}
465
466#############################################################################
256# extcmd framework, basically convert ext <id> <pkg> arg1 args 467# extcmd framework, basically convert ext <msg>
257# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 468# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
258 469
259sub on_extcmd { 470sub on_extcmd {
260 my ($pl, $buf) = @_; 471 my ($pl, $buf) = @_;
261 472
262 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 473 my $msg = eval { from_json $buf };
263 474
264 $extcmd{$type}[0]->($pl, $buf) 475 if (ref $msg) {
265 if $extcmd{$type}; 476 if (my $cb = $extcmd{$msg->{msgtype}}) {
477 if (my %reply = $cb->[0]->($pl, $msg)) {
478 $pl->ext_reply ($msg->{msgid}, %reply);
479 }
480 }
481 } else {
482 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
483 }
484
485 1
266} 486}
267 487
268############################################################################# 488#############################################################################
269# load/save/clean perl data associated with a map 489# load/save/clean perl data associated with a map
270 490
271*on_mapclean = sub { 491*cf::mapsupport::on_clean = sub {
272 my ($map) = @_; 492 my ($map) = @_;
273 493
274 my $path = $map->tmpname; 494 my $path = $map->tmpname;
275 defined $path or return; 495 defined $path or return;
276 496
277 unlink "$path.cfperl"; 497 unlink "$path.cfperl";
278}; 498};
279 499
280*on_mapin = 500*cf::mapsupport::on_swapin =
281*on_mapload = sub { 501*cf::mapsupport::on_load = sub {
282 my ($map) = @_; 502 my ($map) = @_;
283 503
284 my $path = $map->tmpname; 504 my $path = $map->tmpname;
285 $path = $map->path unless defined $path; 505 $path = $map->path unless defined $path;
286 506
293 or return; # too new 513 or return; # too new
294 514
295 $map->_set_obs ($data->{obs}); 515 $map->_set_obs ($data->{obs});
296}; 516};
297 517
298*on_mapout = sub { 518*cf::mapsupport::on_swapout = sub {
299 my ($map) = @_; 519 my ($map) = @_;
300 520
301 my $path = $map->tmpname; 521 my $path = $map->tmpname;
302 $path = $map->path unless defined $path; 522 $path = $map->path unless defined $path;
303 523
320 } else { 540 } else {
321 unlink "$path.cfperl"; 541 unlink "$path.cfperl";
322 } 542 }
323}; 543};
324 544
545attach_to_maps prio => -10000, package => cf::mapsupport::;
546
325############################################################################# 547#############################################################################
326# load/save perl data associated with player->ob objects 548# load/save perl data associated with player->ob objects
327 549
328*on_player_load = sub { 550sub all_objects(@) {
551 @_, map all_objects ($_->inv), @_
552}
553
554attach_to_players
555 on_load => sub {
329 my ($ob, $path) = @_; 556 my ($pl, $path) = @_;
330 557
331 if (open my $fh, "<:raw", "$path.cfperl") { 558 for my $o (all_objects $pl->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")) { 559 if (my $value = $o->get_ob_key_value ("_perl_data")) {
346 $o->set_ob_key_value ("_perl_data"); 560 $o->set_ob_key_value ("_perl_data");
347 561
348 %$o = %{ Storable::thaw pack "H*", $value }; 562 %$o = %{ Storable::thaw pack "H*", $value };
563 }
349 } 564 }
350 } 565 },
351}; 566 on_save => sub {
352
353*on_player_save = sub {
354 my ($ob, $path) = @_; 567 my ($pl, $path) = @_;
355 568
356 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 569 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
357 for grep %$_, $ob, $ob->inv; 570 for grep %$_, all_objects $pl->ob;
358 571 },
359 unlink "$path.cfperl";#d##TODO#remove 572;
360};
361 573
362############################################################################# 574#############################################################################
363# core extensions - in perl 575# core extensions - in perl
364 576
365=item cf::player::exists $login 577=item cf::player::exists $login
370 582
371sub cf::player::exists($) { 583sub cf::player::exists($) {
372 cf::player::find $_[0] 584 cf::player::find $_[0]
373 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 585 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
374} 586}
587
588=item $player->reply ($npc, $msg[, $flags])
589
590Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
591can be C<undef>. Does the right thing when the player is currently in a
592dialogue with the given NPC character.
593
594=cut
375 595
376# rough implementation of a future "reply" method that works 596# rough implementation of a future "reply" method that works
377# with dialog boxes. 597# with dialog boxes.
378sub cf::object::player::reply($$$;$) { 598sub cf::object::player::reply($$$;$) {
379 my ($self, $npc, $msg, $flags) = @_; 599 my ($self, $npc, $msg, $flags) = @_;
386 $msg = $npc->name . " says: $msg" if $npc; 606 $msg = $npc->name . " says: $msg" if $npc;
387 $self->message ($msg, $flags); 607 $self->message ($msg, $flags);
388 } 608 }
389} 609}
390 610
611=item $player->ext_reply ($msgid, $msgtype, %msg)
612
613Sends an ext reply to the player.
614
615=cut
616
617sub cf::player::ext_reply($$$%) {
618 my ($self, $id, %msg) = @_;
619
620 $msg{msgid} = $id;
621
622 $self->send ("ext " . to_json \%msg);
623}
624
391############################################################################# 625#############################################################################
392# map scripting support 626# map scripting support
393 627
394our $safe = new Safe "ext"; 628our $safe = new Safe "ext";
395our $safe_hole = new Safe::Hole; 629our $safe_hole = new Safe::Hole;
399$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 633$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
400 634
401# here we export the classes and methods available to script code 635# here we export the classes and methods available to script code
402 636
403for ( 637for (
404 ["cf::object" => qw(contr)], 638 ["cf::object" => qw(contr pay_amount pay_player)],
405 ["cf::object::player" => qw(player)], 639 ["cf::object::player" => qw(player)],
406 ["cf::player" => qw(peaceful)], 640 ["cf::player" => qw(peaceful)],
407) { 641) {
408 no strict 'refs'; 642 no strict 'refs';
409 my ($pkg, @funs) = @$_; 643 my ($pkg, @funs) = @$_;
442 no strict 'refs'; 676 no strict 'refs';
443 *{"ext::$fun"} = $safe_hole->wrap ($cb); 677 *{"ext::$fun"} = $safe_hole->wrap ($cb);
444} 678}
445 679
446############################################################################# 680#############################################################################
681# the server's main()
682
683sub main {
684 Event::loop;
685}
686
687#############################################################################
447# initialisation 688# initialisation
448 689
449register "<global>", __PACKAGE__; 690register "<global>", __PACKAGE__;
450 691
451unshift @INC, maps_directory "perl"; 692unshift @INC, $LIBDIR;
452 693
453load_extensions; 694load_extensions;
454 695
696$TICK_WATCHER = Event->timer (
697 prio => 1,
698 at => $NEXT_TICK || 1,
699 cb => sub {
700 cf::server_tick; # one server iteration
701
702 my $NOW = Event::time;
703 $NEXT_TICK += $TICK;
704
705 # if we are delayed by four ticks, skip them all
706 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
707
708 $TICK_WATCHER->at ($NEXT_TICK);
709 $TICK_WATCHER->start;
710 },
711);
712
4551 7131
456 714

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines