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.15 by root, Sun Jun 11 15:02:16 2006 UTC vs.
Revision 1.18 by root, Tue Jul 11 14:24:15 2006 UTC

1package cf; 1package cf;
2 2
3use Symbol; 3use Symbol;
4use List::Util; 4use List::Util;
5use Storable; 5use Storable;
6use Event;
6 7
7use strict; 8use strict;
8 9
9our %COMMAND; 10our %COMMAND;
10our @EVENT; 11our @EVENT;
60 61
61# guessed hierarchies 62# guessed hierarchies
62 63
63@cf::object::player::ISA = 'cf::object'; 64@cf::object::player::ISA = 'cf::object';
64@cf::object::map::ISA = 'cf::object'; 65@cf::object::map::ISA = 'cf::object';
66
67$Event::DIED = sub {
68 warn "error in event callback: @_";
69};
65 70
66my %ext_pkg; 71my %ext_pkg;
67my @exts; 72my @exts;
68my @hook; 73my @hook;
69my %command; 74my %command;
100 105
101sub register_command { 106sub register_command {
102 my ($name, $time, $cb) = @_; 107 my ($name, $time, $cb) = @_;
103 108
104 my $caller = caller; 109 my $caller = caller;
105
106 warn "registering command '$name/$time' to '$caller'"; 110 #warn "registering command '$name/$time' to '$caller'";
107 111
108 push @{ $command{$name} }, [$time, $cb, $caller]; 112 push @{ $command{$name} }, [$time, $cb, $caller];
109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 113 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110} 114}
111 115
116sub register_extcmd {
117 my ($name, $cb) = @_;
118
119 my $caller = caller;
120 #warn "registering extcmd '$name' to '$caller'";
121
122 $extcmd{$name} = [$cb, $caller];
123}
124
112sub register { 125sub register {
113 my ($base, $pkg) = @_; 126 my ($base, $pkg) = @_;
114 127
115 for my $idx (0 .. $#EVENT) { 128 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 129 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 130 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref; 131 $hook[$idx]{$base} = $ref;
119 } 132 }
120 } 133 }
121} 134}
122 135
174 delete $COMMAND{"$name\000"}; 187 delete $COMMAND{"$name\000"};
175 } 188 }
176 } 189 }
177 190
178 # remove extcmds 191 # remove extcmds
179 for my $name (keys %command) { 192 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
180 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
181
182 if (@cb) {
183 $command{$name} = \@cb;
184 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
185 } else {
186 delete $command{$name}; 193 delete $extcmd{$name};
187 delete $COMMAND{"$name\000"};
188 }
189 } 194 }
190
191 195
192 Symbol::delete_package $pkg; 196 Symbol::delete_package $pkg;
193} 197}
194 198
195sub load_extensions { 199sub load_extensions {
235# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 239# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
236 240
237sub on_extcmd { 241sub on_extcmd {
238 my ($pl, $buf) = @_; 242 my ($pl, $buf) = @_;
239 243
240 my ($id, $pkg, $name, $data) = split / /, $buf, 4; 244 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
241 245
242 if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) { 246 $extcmd{$type}[0]->($pl, $buf)
243 $method->($pl, $id, $buf); 247 if $extcmd{$type};
244 }
245} 248}
246 249
247############################################################################# 250#############################################################################
248# load/save/clean perl data associated with a map 251# load/save/clean perl data associated with a map
249 252
338 unlink "$path.cfperl";#d##TODO#remove 341 unlink "$path.cfperl";#d##TODO#remove
339}; 342};
340 343
341register "<global>", __PACKAGE__; 344register "<global>", __PACKAGE__;
342 345
346unshift @INC, maps_directory "perl";
347
343load_extensions; 348load_extensions;
344 349
3451 3501
346 351

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines