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.11 by root, Sun Mar 26 15:52:03 2006 UTC vs.
Revision 1.17 by root, Mon Jun 19 10:20:07 2006 UTC

65 65
66my %ext_pkg; 66my %ext_pkg;
67my @exts; 67my @exts;
68my @hook; 68my @hook;
69my %command; 69my %command;
70my %extcmd;
70 71
71sub inject_event { 72sub inject_event {
72 my ($data) = @_; 73 my $extension = shift;
74 my $event_code = shift;
73 75
74 my $cb = $hook[$data->{event_code}]{$data->{extension}} 76 my $cb = $hook[$event_code]{$extension}
75 or return; 77 or return;
76 78
77 $cb->($data) 79 &$cb
78} 80}
79 81
80sub inject_global_event { 82sub inject_global_event {
81 my ($data) = @_; 83 my $event = shift;
82 84
83 my $cb = $hook[$data->{event_code}] 85 my $cb = $hook[$event]
84 or return; 86 or return;
85 87
86 List::Util::max map $_->($data), values %$cb 88 List::Util::max map &$_, values %$cb
87} 89}
88 90
89sub inject_command { 91sub inject_command {
90 my ($name, $obj, $params) = @_; 92 my ($name, $obj, $params) = @_;
91 93
98 100
99sub register_command { 101sub register_command {
100 my ($name, $time, $cb) = @_; 102 my ($name, $time, $cb) = @_;
101 103
102 my $caller = caller; 104 my $caller = caller;
103
104 warn "registering command '$name/$time' to '$caller'"; 105 #warn "registering command '$name/$time' to '$caller'";
105 106
106 push @{ $command{$name} }, [$time, $cb, $caller]; 107 push @{ $command{$name} }, [$time, $cb, $caller];
107 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 108 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
108} 109}
109 110
111sub register_extcmd {
112 my ($name, $cb) = @_;
113
114 my $caller = caller;
115 #warn "registering extcmd '$name' to '$caller'";
116
117 $extcmd{$name} = [$cb, $caller];
118}
119
110sub register { 120sub register {
111 my ($base, $pkg) = @_; 121 my ($base, $pkg) = @_;
112 122
113 for my $idx (0 .. $#EVENT) { 123 for my $idx (0 .. $#EVENT) {
114 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 124 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
115 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 125 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
116 $hook[$idx]{$base} = $ref; 126 $hook[$idx]{$base} = $ref;
117 } 127 }
118 } 128 }
119} 129}
120 130
171 delete $command{$name}; 181 delete $command{$name};
172 delete $COMMAND{"$name\000"}; 182 delete $COMMAND{"$name\000"};
173 } 183 }
174 } 184 }
175 185
186 # remove extcmds
187 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
188 delete $extcmd{$name};
189 }
190
176 Symbol::delete_package $pkg; 191 Symbol::delete_package $pkg;
177} 192}
178 193
179sub load_extensions { 194sub load_extensions {
180 my $LIBDIR = maps_directory "perl"; 195 my $LIBDIR = maps_directory "perl";
213 $who->message ("Intruder Alert!"); 228 $who->message ("Intruder Alert!");
214 } 229 }
215}; 230};
216 231
217############################################################################# 232#############################################################################
233# extcmd framework, basically convert ext <id> <pkg> arg1 args
234# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
235
236sub on_extcmd {
237 my ($pl, $buf) = @_;
238
239 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
240
241 $extcmd{$type}[0]->($pl, $buf)
242 if $extcmd{$type};
243}
244
245#############################################################################
218# load/save/clean perl data associated with a map 246# load/save/clean perl data associated with a map
219 247
220*on_mapclean = sub { 248*on_mapclean = sub {
221 my $map = shift->{map}; 249 my ($map) = @_;
222 250
223 my $path = $map->tmpname; 251 my $path = $map->tmpname;
224 defined $path or return; 252 defined $path or return;
225 253
226 unlink "$path.cfperl"; 254 unlink "$path.cfperl";
227}; 255};
228 256
229*on_mapin = 257*on_mapin =
230*on_mapload = sub { 258*on_mapload = sub {
231 my $map = shift->{map}; 259 my ($map) = @_;
232 260
233 my $path = $map->tmpname; 261 my $path = $map->tmpname;
234 $path = $map->path unless defined $path; 262 $path = $map->path unless defined $path;
235 263
236 open my $fh, "<:raw", "$path.cfperl" 264 open my $fh, "<:raw", "$path.cfperl"
243 271
244 $map->_set_obs ($data->{obs}); 272 $map->_set_obs ($data->{obs});
245}; 273};
246 274
247*on_mapout = sub { 275*on_mapout = sub {
248 my $map = shift->{map}; 276 my ($map) = @_;
249 277
250 my $path = $map->tmpname; 278 my $path = $map->tmpname;
251 $path = $map->path unless defined $path; 279 $path = $map->path unless defined $path;
252 280
253 my $obs = $map->_get_obs; 281 my $obs = $map->_get_obs;
273 301
274############################################################################# 302#############################################################################
275# load/save perl data associated with player->ob objects 303# load/save perl data associated with player->ob objects
276 304
277*on_player_load = sub { 305*on_player_load = sub {
278 my ($event) = @_; 306 my ($ob, $path) = @_;
279 my $path = $event->{message};
280 my $ob = $event->{who};
281 307
282 if (open my $fh, "<:raw", "$path.cfperl") { 308 if (open my $fh, "<:raw", "$path.cfperl") {
283 309
284 #d##TODO#remove 310 #d##TODO#remove
285 311
300 } 326 }
301 } 327 }
302}; 328};
303 329
304*on_player_save = sub { 330*on_player_save = sub {
305 my ($event) = @_; 331 my ($ob, $path) = @_;
306 my $path = $event->{message};
307 my $ob = $event->{who};
308 332
309 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 333 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
310 for grep %$_, $ob, $ob->inv; 334 for grep %$_, $ob, $ob->inv;
311 335
312 unlink "$path.cfperl";#d##TODO#remove 336 unlink "$path.cfperl";#d##TODO#remove
313}; 337};
314 338
315register "<global>", __PACKAGE__; 339register "<global>", __PACKAGE__;
316 340
341unshift @INC, maps_directory "perl";
342
317load_extensions; 343load_extensions;
318 344
3191 3451
320 346

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines