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.36 by root, Thu Aug 24 14:04:29 2006 UTC vs.
Revision 1.39 by root, Fri Aug 25 13:24:50 2006 UTC

11use Event; 11use Event;
12$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
13 13
14use strict; 14use strict;
15 15
16our %COMMAND; 16our %COMMAND = ();
17our @EVENT; 17our @EVENT;
18our @PLUGIN_EVENT;
18our %PROP_TYPE; 19our %PROP_TYPE;
19our %PROP_IDX; 20our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 21our $LIBDIR = maps_directory "perl";
21 22
22our $TICK = MAX_TIME * 1e-6; 23our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 24our $TICK_WATCHER;
24our $NEXT_TICK; 25our $NEXT_TICK;
25 26
26BEGIN { 27BEGIN {
27 @EVENT = map lc, @EVENT; 28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
28 29
29 *CORE::GLOBAL::warn = sub { 30 *CORE::GLOBAL::warn = sub {
30 my $msg = join "", @_; 31 my $msg = join "", @_;
31 $msg .= "\n" 32 $msg .= "\n"
32 unless $msg =~ /\n$/; 33 unless $msg =~ /\n$/;
90my @exts; 91my @exts;
91my @hook; 92my @hook;
92my %command; 93my %command;
93my %extcmd; 94my %extcmd;
94 95
96#############################################################################
97# "new" plug-in system
98
99=item cf::object::attach ... # NYI
100
101=item cf::attach_global ... # NYI
102
103=item cf::attach_to_type ... # NYI
104
105=item cf::attach_to_objects ... # NYI
106
107=item cf::attach_to_players ... # NYI
108
109=item cf::attach_to_maps ... # NYI
110
111 prio => $number, # higehr is earlier
112 on_xxx => \&cb,
113 package => package::,
114
115=cut
116
117our %CB_CLASS = (); # registry for class-based events
118our @CB_GLOBAL = (); # registry for all global events
119our @CB_TYPE = (); # registry for type (cf-object class) based events
120
121sub _attach_cb($\%$$$) {
122 my ($registry, $undo, $event, $prio, $cb) = @_;
123
124 use sort 'stable';
125
126 $cb = [$prio, $cb];
127
128 @{$registry->[$event]} = sort
129 { $a->[0] cmp $b->[0] }
130 @{$registry->[$event] || []}, $cb;
131
132 push @{$undo->{cb}}, [$event, $cb];
133}
134
135# attach handles attaching event callbacks
136# the only thing the caller has to do is pass the correct
137# registry (== where the callback attaches to).
138sub _attach(\@$\@) {
139 my ($registry, $klass, $arg) = @_;
140
141 my $prio = 0;
142
143 my %undo = (
144 registry => $registry,
145 cb => [],
146 );
147
148 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
149
150 while (@$arg) {
151 my $type = shift @$arg;
152
153 if ($type eq "prio") {
154 $prio = shift @$arg;
155
156 } elsif ($type eq "package") {
157 my $pkg = shift @$arg;
158
159 while (my ($name, $id) = each %cb_id) {
160 if (my $cb = $pkg->can ($name)) {
161 _attach_cb $registry, %undo, $id, $prio, $cb;
162 }
163 }
164
165 } elsif (exists $cb_id{$type}) {
166 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg;
167
168 } elsif (ref $type) {
169 warn "attaching objects not supported, ignoring.\n";
170
171 } else {
172 shift @$arg;
173 warn "attach argument '$type' not supported, ignoring.\n";
174 }
175 }
176
177 \%undo
178}
179
180sub cf::object::attach {
181 die;
182}
183
184sub attach_global {
185 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
186}
187
188sub attach_type {
189 my $type = shift;
190 _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_
191}
192
193sub attach_to_objects {
194 _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_
195}
196
197sub attach_to_players {
198 _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_
199}
200
201sub attach_to_maps {
202 _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_
203}
204
205our $override;
206
207sub override() {
208 $override = 1
209}
210
211sub invoke {
212 my $event = shift;
213
214 my @cb;
215
216 if (my $ref = ref $_[0]) {
217 # 1. object-specific (NYI)
218 # 2. class-specific
219 push @cb, @{$CB_CLASS{$ref}[$event] || []};
220 }
221
222 # global
223 push @cb, @{$CB_GLOBAL[$event] || []};
224
225# warn "invoke id $EVENT[$event][0], args <@_> <=> @cb\n";#d#
226
227 local $override;
228
229 for (@cb) {
230 eval { &{$_->[1]} };
231
232 if ($@) {
233 warn "$@";
234 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
235 override;
236 }
237
238 return 1 if $override;
239 }
240
241 0
242}
243
244#############################################################################
245# old plug-in events
246
95sub inject_event { 247sub inject_event {
96 my $extension = shift; 248 my $extension = shift;
97 my $event_code = shift; 249 my $event_code = shift;
98 250
99 my $cb = $hook[$event_code]{$extension} 251 my $cb = $hook[$event_code]{$extension}
141} 293}
142 294
143sub register { 295sub register {
144 my ($base, $pkg) = @_; 296 my ($base, $pkg) = @_;
145 297
146 for my $idx (0 .. $#EVENT) { 298 for my $idx (0 .. $#PLUGIN_EVENT) {
147 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 299 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
148 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 300 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
149 $hook[$idx]{$base} = $ref; 301 $hook[$idx]{$base} = $ref;
150 } 302 }
151 } 303 }
152} 304}
153 305
187 my ($pkg) = @_; 339 my ($pkg) = @_;
188 340
189 warn "removing extension $pkg\n"; 341 warn "removing extension $pkg\n";
190 342
191 # remove hooks 343 # remove hooks
192 for my $idx (0 .. $#EVENT) { 344 for my $idx (0 .. $#PLUGIN_EVENT) {
193 delete $hook[$idx]{$pkg}; 345 delete $hook[$idx]{$pkg};
194 } 346 }
195 347
196 # remove commands 348 # remove commands
197 for my $name (keys %command) { 349 for my $name (keys %command) {
343} 495}
344 496
345############################################################################# 497#############################################################################
346# load/save/clean perl data associated with a map 498# load/save/clean perl data associated with a map
347 499
348*on_mapclean = sub { 500*cf::mapsupport::on_clean = sub {
349 my ($map) = @_; 501 my ($map) = @_;
350 502
351 my $path = $map->tmpname; 503 my $path = $map->tmpname;
352 defined $path or return; 504 defined $path or return;
353 505
354 unlink "$path.cfperl"; 506 unlink "$path.cfperl";
355}; 507};
356 508
357*on_mapin = 509*cf::mapsupport::on_swapin =
358*on_mapload = sub { 510*cf::mapsupport::on_load = sub {
359 my ($map) = @_; 511 my ($map) = @_;
360 512
361 my $path = $map->tmpname; 513 my $path = $map->tmpname;
362 $path = $map->path unless defined $path; 514 $path = $map->path unless defined $path;
363 515
370 or return; # too new 522 or return; # too new
371 523
372 $map->_set_obs ($data->{obs}); 524 $map->_set_obs ($data->{obs});
373}; 525};
374 526
375*on_mapout = sub { 527*cf::mapsupport::on_swapout = sub {
376 my ($map) = @_; 528 my ($map) = @_;
377 529
378 my $path = $map->tmpname; 530 my $path = $map->tmpname;
379 $path = $map->path unless defined $path; 531 $path = $map->path unless defined $path;
380 532
397 } else { 549 } else {
398 unlink "$path.cfperl"; 550 unlink "$path.cfperl";
399 } 551 }
400}; 552};
401 553
554attach_to_maps prio => -10000, package => cf::mapsupport::;
555
402############################################################################# 556#############################################################################
403# load/save perl data associated with player->ob objects 557# load/save perl data associated with player->ob objects
404 558
405sub all_objects(@) { 559sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_ 560 @_, map all_objects ($_->inv), @_
407} 561}
408 562
409*on_player_load = sub { 563attach_to_players
564 on_load => sub {
410 my ($ob, $path) = @_; 565 my ($pl, $path) = @_;
411 566
412 for my $o (all_objects $ob) { 567 for my $o (all_objects $pl->ob) {
413 if (my $value = $o->get_ob_key_value ("_perl_data")) { 568 if (my $value = $o->get_ob_key_value ("_perl_data")) {
414 $o->set_ob_key_value ("_perl_data"); 569 $o->set_ob_key_value ("_perl_data");
415 570
416 %$o = %{ Storable::thaw pack "H*", $value }; 571 %$o = %{ Storable::thaw pack "H*", $value };
572 }
417 } 573 }
418 } 574 },
419}; 575 on_save => sub {
420
421*on_player_save = sub {
422 my ($ob, $path) = @_; 576 my ($pl, $path) = @_;
423 577
424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 578 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
425 for grep %$_, all_objects $ob; 579 for grep %$_, all_objects $pl->ob;
426}; 580 },
581;
427 582
428############################################################################# 583#############################################################################
429# core extensions - in perl 584# core extensions - in perl
430 585
431=item cf::player::exists $login 586=item cf::player::exists $login
532} 687}
533 688
534############################################################################# 689#############################################################################
535# the server's main() 690# the server's main()
536 691
537sub run { 692sub main {
538 Event::loop; 693 Event::loop;
539} 694}
540 695
541############################################################################# 696#############################################################################
542# initialisation 697# initialisation
554 cf::server_tick; # one server iteration 709 cf::server_tick; # one server iteration
555 710
556 my $NOW = Event::time; 711 my $NOW = Event::time;
557 $NEXT_TICK += $TICK; 712 $NEXT_TICK += $TICK;
558 713
559 # if we are delayed by > 0.25 second, skip ticks 714 # if we are delayed by four ticks, skip them all
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; 715 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
561 716
562 $TICK_WATCHER->at ($NEXT_TICK); 717 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start; 718 $TICK_WATCHER->start;
564 }, 719 },
565); 720);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines