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.13 by root, Fri Mar 31 22:09:36 2006 UTC vs.
Revision 1.22 by root, Wed Jul 19 08:50:42 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 ($data) = @_; 80 my $extension = shift;
81 my $event_code = shift;
73 82
74 my $cb = $hook[$data->{event_code}]{$data->{extension}} 83 my $cb = $hook[$event_code]{$extension}
75 or return; 84 or return;
76 85
77 $cb->($data) 86 &$cb
78} 87}
79 88
80sub inject_global_event { 89sub inject_global_event {
81 my $event = shift; 90 my $event = shift;
82 91
98 107
99sub register_command { 108sub register_command {
100 my ($name, $time, $cb) = @_; 109 my ($name, $time, $cb) = @_;
101 110
102 my $caller = caller; 111 my $caller = caller;
103
104 warn "registering command '$name/$time' to '$caller'"; 112 #warn "registering command '$name/$time' to '$caller'";
105 113
106 push @{ $command{$name} }, [$time, $cb, $caller]; 114 push @{ $command{$name} }, [$time, $cb, $caller];
107 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 115 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
108} 116}
109 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
110sub register { 127sub register {
111 my ($base, $pkg) = @_; 128 my ($base, $pkg) = @_;
112 129
113 for my $idx (0 .. $#EVENT) { 130 for my $idx (0 .. $#EVENT) {
114 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 131 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
115 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 132 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
116 $hook[$idx]{$base} = $ref; 133 $hook[$idx]{$base} = $ref;
117 } 134 }
118 } 135 }
119} 136}
120 137
152 169
153sub unload_extension { 170sub unload_extension {
154 my ($pkg) = @_; 171 my ($pkg) = @_;
155 172
156 warn "removing extension $pkg\n"; 173 warn "removing extension $pkg\n";
174
175 if (my $cb = $pkg->can ("on_unload")) {
176 $cb->($pkg);
177 }
157 178
158 # remove hooks 179 # remove hooks
159 for my $idx (0 .. $#EVENT) { 180 for my $idx (0 .. $#EVENT) {
160 delete $hook[$idx]{$pkg}; 181 delete $hook[$idx]{$pkg};
161 } 182 }
171 delete $command{$name}; 192 delete $command{$name};
172 delete $COMMAND{"$name\000"}; 193 delete $COMMAND{"$name\000"};
173 } 194 }
174 } 195 }
175 196
197 # remove extcmds
198 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
199 delete $extcmd{$name};
200 }
201
176 Symbol::delete_package $pkg; 202 Symbol::delete_package $pkg;
177} 203}
178 204
179sub load_extensions { 205sub load_extensions {
180 my $LIBDIR = maps_directory "perl"; 206 my $LIBDIR = maps_directory "perl";
194 if ($who->flag (FLAG_WIZ)) { 220 if ($who->flag (FLAG_WIZ)) {
195 $who->message ("reloading..."); 221 $who->message ("reloading...");
196 222
197 warn "reloading...\n"; 223 warn "reloading...\n";
198 eval { 224 eval {
225 $_->cancel for Event::all_watchers;
226
199 unload_extension $_ for @exts; 227 unload_extension $_ for @exts;
200 delete $INC{"cf.pm"}; 228 delete $INC{"cf.pm"};
201 229
202 # don't, removes xs symbols, too 230 # don't, removes xs symbols, too
203 #Symbol::delete_package $pkg; 231 #Symbol::delete_package __PACKAGE__;
204 232
205 require cf; 233 require cf;
206 }; 234 };
207 warn $@ if $@; 235 warn $@ if $@;
208 $who->message ($@) if $@; 236 $who->message ($@) if $@;
211 $who->message ("reloaded"); 239 $who->message ("reloaded");
212 } else { 240 } else {
213 $who->message ("Intruder Alert!"); 241 $who->message ("Intruder Alert!");
214 } 242 }
215}; 243};
244
245#############################################################################
246# extcmd framework, basically convert ext <id> <pkg> arg1 args
247# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
248
249sub on_extcmd {
250 my ($pl, $buf) = @_;
251
252 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
253
254 $extcmd{$type}[0]->($pl, $buf)
255 if $extcmd{$type};
256}
216 257
217############################################################################# 258#############################################################################
218# load/save/clean perl data associated with a map 259# load/save/clean perl data associated with a map
219 260
220*on_mapclean = sub { 261*on_mapclean = sub {
306 for grep %$_, $ob, $ob->inv; 347 for grep %$_, $ob, $ob->inv;
307 348
308 unlink "$path.cfperl";#d##TODO#remove 349 unlink "$path.cfperl";#d##TODO#remove
309}; 350};
310 351
352#############################################################################
353# core extensions - in perl
354
355# rough implementation of a future "reply" method that works
356# with dialog boxes.
357sub cf::object::player::reply($$;$) {
358 my ($self, $msg, $flags) = @_;
359
360 $flags = cf::NDI_WHITE unless @_ >= 3;
361
362 $self->message ($msg, $flags);
363}
364
365#############################################################################
366# initialisation
367
311register "<global>", __PACKAGE__; 368register "<global>", __PACKAGE__;
312 369
370unshift @INC, maps_directory "perl";
371
313load_extensions; 372load_extensions;
314 373
3151 3741
316 375

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines