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.20 by root, Sun Jul 16 17:29:02 2006 UTC

1package cf; 1package cf;
2 2
3use Symbol; 3use Symbol;
4use List::Util; 4use List::Util;
5use Storable; 5use Storable;
6
7use Event;
8$Event::Eval = 1; # no idea why this is required, but it is
6 9
7use strict; 10use strict;
8 11
9our %COMMAND; 12our %COMMAND;
10our @EVENT; 13our @EVENT;
60 63
61# guessed hierarchies 64# guessed hierarchies
62 65
63@cf::object::player::ISA = 'cf::object'; 66@cf::object::player::ISA = 'cf::object';
64@cf::object::map::ISA = 'cf::object'; 67@cf::object::map::ISA = 'cf::object';
68
69$Event::DIED = sub {
70 warn "error in event callback: @_";
71};
65 72
66my %ext_pkg; 73my %ext_pkg;
67my @exts; 74my @exts;
68my @hook; 75my @hook;
69my %command; 76my %command;
100 107
101sub register_command { 108sub register_command {
102 my ($name, $time, $cb) = @_; 109 my ($name, $time, $cb) = @_;
103 110
104 my $caller = caller; 111 my $caller = caller;
105
106 warn "registering command '$name/$time' to '$caller'"; 112 #warn "registering command '$name/$time' to '$caller'";
107 113
108 push @{ $command{$name} }, [$time, $cb, $caller]; 114 push @{ $command{$name} }, [$time, $cb, $caller];
109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 115 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110} 116}
111 117
118sub register_extcmd {
119 my ($name, $cb) = @_;
120
121 my $caller = caller;
122 #warn "registering extcmd '$name' to '$caller'";
123
124 $extcmd{$name} = [$cb, $caller];
125}
126
112sub register { 127sub register {
113 my ($base, $pkg) = @_; 128 my ($base, $pkg) = @_;
114 129
115 for my $idx (0 .. $#EVENT) { 130 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 131 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 132 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref; 133 $hook[$idx]{$base} = $ref;
119 } 134 }
120 } 135 }
121} 136}
122 137
174 delete $COMMAND{"$name\000"}; 189 delete $COMMAND{"$name\000"};
175 } 190 }
176 } 191 }
177 192
178 # remove extcmds 193 # remove extcmds
179 for my $name (keys %command) { 194 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}; 195 delete $extcmd{$name};
187 delete $COMMAND{"$name\000"};
188 }
189 } 196 }
190
191 197
192 Symbol::delete_package $pkg; 198 Symbol::delete_package $pkg;
193} 199}
194 200
195sub load_extensions { 201sub load_extensions {
210 if ($who->flag (FLAG_WIZ)) { 216 if ($who->flag (FLAG_WIZ)) {
211 $who->message ("reloading..."); 217 $who->message ("reloading...");
212 218
213 warn "reloading...\n"; 219 warn "reloading...\n";
214 eval { 220 eval {
221 $_->cancel for Event::all_watchers;
222
215 unload_extension $_ for @exts; 223 unload_extension $_ for @exts;
216 delete $INC{"cf.pm"}; 224 delete $INC{"cf.pm"};
217 225
218 # don't, removes xs symbols, too 226 # don't, removes xs symbols, too
219 #Symbol::delete_package $pkg; 227 #Symbol::delete_package $pkg;
235# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 243# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
236 244
237sub on_extcmd { 245sub on_extcmd {
238 my ($pl, $buf) = @_; 246 my ($pl, $buf) = @_;
239 247
240 my ($id, $pkg, $name, $data) = split / /, $buf, 4; 248 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
241 249
242 if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) { 250 $extcmd{$type}[0]->($pl, $buf)
243 $method->($pl, $id, $buf); 251 if $extcmd{$type};
244 }
245} 252}
246 253
247############################################################################# 254#############################################################################
248# load/save/clean perl data associated with a map 255# load/save/clean perl data associated with a map
249 256
338 unlink "$path.cfperl";#d##TODO#remove 345 unlink "$path.cfperl";#d##TODO#remove
339}; 346};
340 347
341register "<global>", __PACKAGE__; 348register "<global>", __PACKAGE__;
342 349
350unshift @INC, maps_directory "perl";
351
343load_extensions; 352load_extensions;
344 353
3451 3541
346 355

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines