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.14 by root, Fri Mar 31 22:47:35 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;
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';
65 68
69$Event::DIED = sub {
70 warn "error in event callback: @_";
71};
72
66my %ext_pkg; 73my %ext_pkg;
67my @exts; 74my @exts;
68my @hook; 75my @hook;
69my %command; 76my %command;
77my %extcmd;
70 78
71sub inject_event { 79sub inject_event {
72 my $extension = shift; 80 my $extension = shift;
73 my $event_code = shift; 81 my $event_code = shift;
74 82
99 107
100sub register_command { 108sub register_command {
101 my ($name, $time, $cb) = @_; 109 my ($name, $time, $cb) = @_;
102 110
103 my $caller = caller; 111 my $caller = caller;
104
105 warn "registering command '$name/$time' to '$caller'"; 112 #warn "registering command '$name/$time' to '$caller'";
106 113
107 push @{ $command{$name} }, [$time, $cb, $caller]; 114 push @{ $command{$name} }, [$time, $cb, $caller];
108 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 115 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
109} 116}
110 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
111sub register { 127sub register {
112 my ($base, $pkg) = @_; 128 my ($base, $pkg) = @_;
113 129
114 for my $idx (0 .. $#EVENT) { 130 for my $idx (0 .. $#EVENT) {
115 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 131 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
116 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 132 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
117 $hook[$idx]{$base} = $ref; 133 $hook[$idx]{$base} = $ref;
118 } 134 }
119 } 135 }
120} 136}
121 137
172 delete $command{$name}; 188 delete $command{$name};
173 delete $COMMAND{"$name\000"}; 189 delete $COMMAND{"$name\000"};
174 } 190 }
175 } 191 }
176 192
193 # remove extcmds
194 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
195 delete $extcmd{$name};
196 }
197
177 Symbol::delete_package $pkg; 198 Symbol::delete_package $pkg;
178} 199}
179 200
180sub load_extensions { 201sub load_extensions {
181 my $LIBDIR = maps_directory "perl"; 202 my $LIBDIR = maps_directory "perl";
195 if ($who->flag (FLAG_WIZ)) { 216 if ($who->flag (FLAG_WIZ)) {
196 $who->message ("reloading..."); 217 $who->message ("reloading...");
197 218
198 warn "reloading...\n"; 219 warn "reloading...\n";
199 eval { 220 eval {
221 $_->cancel for Event::all_watchers;
222
200 unload_extension $_ for @exts; 223 unload_extension $_ for @exts;
201 delete $INC{"cf.pm"}; 224 delete $INC{"cf.pm"};
202 225
203 # don't, removes xs symbols, too 226 # don't, removes xs symbols, too
204 #Symbol::delete_package $pkg; 227 #Symbol::delete_package $pkg;
212 $who->message ("reloaded"); 235 $who->message ("reloaded");
213 } else { 236 } else {
214 $who->message ("Intruder Alert!"); 237 $who->message ("Intruder Alert!");
215 } 238 }
216}; 239};
240
241#############################################################################
242# extcmd framework, basically convert ext <id> <pkg> arg1 args
243# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
244
245sub on_extcmd {
246 my ($pl, $buf) = @_;
247
248 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
249
250 $extcmd{$type}[0]->($pl, $buf)
251 if $extcmd{$type};
252}
217 253
218############################################################################# 254#############################################################################
219# load/save/clean perl data associated with a map 255# load/save/clean perl data associated with a map
220 256
221*on_mapclean = sub { 257*on_mapclean = sub {
309 unlink "$path.cfperl";#d##TODO#remove 345 unlink "$path.cfperl";#d##TODO#remove
310}; 346};
311 347
312register "<global>", __PACKAGE__; 348register "<global>", __PACKAGE__;
313 349
350unshift @INC, maps_directory "perl";
351
314load_extensions; 352load_extensions;
315 353
3161 3541
317 355

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines