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.38 by root, Thu Aug 24 17:29:30 2006 UTC vs.
Revision 1.40 by root, Fri Aug 25 15:21:57 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 @PLUGIN_EVENT;
19our %PROP_TYPE; 19our %PROP_TYPE;
20our %PROP_IDX; 20our %PROP_IDX;
21our $LIBDIR = maps_directory "perl"; 21our $LIBDIR = maps_directory "perl";
90my %ext_pkg; 90my %ext_pkg;
91my @exts; 91my @exts;
92my @hook; 92my @hook;
93my %command; 93my %command;
94my %extcmd; 94my %extcmd;
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
95 237
96sub inject_event { 238sub inject_event {
97 my $extension = shift; 239 my $extension = shift;
98 my $event_code = shift; 240 my $event_code = shift;
99 241
344} 486}
345 487
346############################################################################# 488#############################################################################
347# load/save/clean perl data associated with a map 489# load/save/clean perl data associated with a map
348 490
349*on_mapclean = sub { 491*cf::mapsupport::on_clean = sub {
350 my ($map) = @_; 492 my ($map) = @_;
351 493
352 my $path = $map->tmpname; 494 my $path = $map->tmpname;
353 defined $path or return; 495 defined $path or return;
354 496
355 unlink "$path.cfperl"; 497 unlink "$path.cfperl";
356}; 498};
357 499
358*on_mapin = 500*cf::mapsupport::on_swapin =
359*on_mapload = sub { 501*cf::mapsupport::on_load = sub {
360 my ($map) = @_; 502 my ($map) = @_;
361 503
362 my $path = $map->tmpname; 504 my $path = $map->tmpname;
363 $path = $map->path unless defined $path; 505 $path = $map->path unless defined $path;
364 506
371 or return; # too new 513 or return; # too new
372 514
373 $map->_set_obs ($data->{obs}); 515 $map->_set_obs ($data->{obs});
374}; 516};
375 517
376*on_mapout = sub { 518*cf::mapsupport::on_swapout = sub {
377 my ($map) = @_; 519 my ($map) = @_;
378 520
379 my $path = $map->tmpname; 521 my $path = $map->tmpname;
380 $path = $map->path unless defined $path; 522 $path = $map->path unless defined $path;
381 523
398 } else { 540 } else {
399 unlink "$path.cfperl"; 541 unlink "$path.cfperl";
400 } 542 }
401}; 543};
402 544
545attach_to_maps prio => -10000, package => cf::mapsupport::;
546
403############################################################################# 547#############################################################################
404# load/save perl data associated with player->ob objects 548# load/save perl data associated with player->ob objects
405 549
406sub all_objects(@) { 550sub all_objects(@) {
407 @_, map all_objects ($_->inv), @_ 551 @_, map all_objects ($_->inv), @_
408} 552}
409 553
410*on_player_load = sub { 554attach_to_players
555 on_load => sub {
411 my ($ob, $path) = @_; 556 my ($pl, $path) = @_;
412 557
413 for my $o (all_objects $ob) { 558 for my $o (all_objects $pl->ob) {
414 if (my $value = $o->get_ob_key_value ("_perl_data")) { 559 if (my $value = $o->get_ob_key_value ("_perl_data")) {
415 $o->set_ob_key_value ("_perl_data"); 560 $o->set_ob_key_value ("_perl_data");
416 561
417 %$o = %{ Storable::thaw pack "H*", $value }; 562 %$o = %{ Storable::thaw pack "H*", $value };
563 }
418 } 564 }
419 } 565 },
420}; 566 on_save => sub {
421
422*on_player_save = sub {
423 my ($ob, $path) = @_; 567 my ($pl, $path) = @_;
424 568
425 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 569 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
426 for grep %$_, all_objects $ob; 570 for grep %$_, all_objects $pl->ob;
427}; 571 },
572;
428 573
429############################################################################# 574#############################################################################
430# core extensions - in perl 575# core extensions - in perl
431 576
432=item cf::player::exists $login 577=item cf::player::exists $login
533} 678}
534 679
535############################################################################# 680#############################################################################
536# the server's main() 681# the server's main()
537 682
538sub run { 683sub main {
539 Event::loop; 684 Event::loop;
540} 685}
541 686
542############################################################################# 687#############################################################################
543# initialisation 688# initialisation

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines