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.5 by root, Wed Feb 8 03:46:15 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;
6
7use Event;
8$Event::Eval = 1; # no idea why this is required, but it is
5 9
6use strict; 10use strict;
7 11
8our %COMMAND; 12our %COMMAND;
9our @EVENT; 13our @EVENT;
21 print STDERR "cfperl: $msg"; 25 print STDERR "cfperl: $msg";
22 LOG llevError, "cfperl: $msg"; 26 LOG llevError, "cfperl: $msg";
23 }; 27 };
24} 28}
25 29
30my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
31
26# generate property mutators 32# generate property mutators
27sub prop_gen { 33sub prop_gen {
28 my ($prefix, $class) = @_; 34 my ($prefix, $class) = @_;
29 35
30 no strict 'refs'; 36 no strict 'refs';
40 $_[0]->get_property ($type, $idx) 46 $_[0]->get_property ($type, $idx)
41 }; 47 };
42 48
43 *{"$class\::set_$sub"} = sub { 49 *{"$class\::set_$sub"} = sub {
44 $_[0]->set_property ($type, $idx, $_[1]); 50 $_[0]->set_property ($type, $idx, $_[1]);
45 }; 51 } unless $ignore_set{$prop};
46 } 52 }
47} 53}
48 54
49# auto-generate most of the API 55# auto-generate most of the API
50 56
57 63
58# guessed hierarchies 64# guessed hierarchies
59 65
60@cf::object::player::ISA = 'cf::object'; 66@cf::object::player::ISA = 'cf::object';
61@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};
62 72
63my %ext_pkg; 73my %ext_pkg;
64my @exts; 74my @exts;
65my @hook; 75my @hook;
66my %command; 76my %command;
77my %extcmd;
67 78
68sub inject_event { 79sub inject_event {
69 my ($data) = @_; 80 my $extension = shift;
81 my $event_code = shift;
70 82
71 my $cb = $hook[$data->{event_code}]{$data->{extension}} 83 my $cb = $hook[$event_code]{$extension}
72 or return; 84 or return;
73 85
74 $cb->($data) 86 &$cb
75} 87}
76 88
77sub inject_global_event { 89sub inject_global_event {
78 my ($data) = @_; 90 my $event = shift;
79 91
80 my $cb = $hook[$data->{event_code}] 92 my $cb = $hook[$event]
81 or return; 93 or return;
82 94
83 $_->($data) for values %$cb; 95 List::Util::max map &$_, values %$cb
84
85 ()
86} 96}
87 97
88sub inject_command { 98sub inject_command {
89 my ($name, $obj, $params) = @_; 99 my ($name, $obj, $params) = @_;
90 100
97 107
98sub register_command { 108sub register_command {
99 my ($name, $time, $cb) = @_; 109 my ($name, $time, $cb) = @_;
100 110
101 my $caller = caller; 111 my $caller = caller;
102
103 warn "registering command '$name/$time' to '$caller'"; 112 #warn "registering command '$name/$time' to '$caller'";
104 113
105 push @{ $command{$name} }, [$time, $cb, $caller]; 114 push @{ $command{$name} }, [$time, $cb, $caller];
106 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 115 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
116}
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
127sub register {
128 my ($base, $pkg) = @_;
129
130 for my $idx (0 .. $#EVENT) {
131 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
132 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
133 $hook[$idx]{$base} = $ref;
134 }
135 }
107} 136}
108 137
109sub load_extension { 138sub load_extension {
110 my ($path) = @_; 139 my ($path) = @_;
111 140
130 or die "$path: $@"; 159 or die "$path: $@";
131 160
132 push @exts, $pkg; 161 push @exts, $pkg;
133 $ext_pkg{$base} = $pkg; 162 $ext_pkg{$base} = $pkg;
134 163
135 no strict 'refs'; 164# no strict 'refs';
136
137# @{"$pkg\::ISA"} = cf::ext::; 165# @{"$pkg\::ISA"} = cf::ext::;
138 166
139 for my $idx (0 .. $#EVENT) { 167 register $base, $pkg;
140 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
141 warn "registering $EVENT[$idx] hook\n";
142 $hook[$idx]{$base} = $ref;
143 }
144 }
145} 168}
146 169
147sub unload_extension { 170sub unload_extension {
148 my ($pkg) = @_; 171 my ($pkg) = @_;
149 172
150 warn "removing extension $pkg\n"; 173 warn "removing extension $pkg\n";
174
175 if (my $cb = $pkg->can ("on_unload")) {
176 $cb->($pkg);
177 }
151 178
152 # remove hooks 179 # remove hooks
153 for my $idx (0 .. $#EVENT) { 180 for my $idx (0 .. $#EVENT) {
154 delete $hook[$idx]{$pkg}; 181 delete $hook[$idx]{$pkg};
155 } 182 }
165 delete $command{$name}; 192 delete $command{$name};
166 delete $COMMAND{"$name\000"}; 193 delete $COMMAND{"$name\000"};
167 } 194 }
168 } 195 }
169 196
197 # remove extcmds
198 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
199 delete $extcmd{$name};
200 }
201
170 Symbol::delete_package $pkg; 202 Symbol::delete_package $pkg;
171} 203}
172 204
173sub load_extensions { 205sub load_extensions {
174 my $LIBDIR = maps_directory "perl"; 206 my $LIBDIR = maps_directory "perl";
188 if ($who->flag (FLAG_WIZ)) { 220 if ($who->flag (FLAG_WIZ)) {
189 $who->message ("reloading..."); 221 $who->message ("reloading...");
190 222
191 warn "reloading...\n"; 223 warn "reloading...\n";
192 eval { 224 eval {
225 $_->cancel for Event::all_watchers;
226
193 unload_extension $_ for @exts; 227 unload_extension $_ for @exts;
194 delete $INC{"cf.pm"}; 228 delete $INC{"cf.pm"};
195 229
196 # don't, removes xs symbols, too 230 # don't, removes xs symbols, too
197 #Symbol::delete_package $pkg; 231 #Symbol::delete_package __PACKAGE__;
198 232
199 require cf; 233 require cf;
200 }; 234 };
201 warn $@ if $@; 235 warn $@ if $@;
202 $who->message ($@) if $@; 236 $who->message ($@) if $@;
206 } else { 240 } else {
207 $who->message ("Intruder Alert!"); 241 $who->message ("Intruder Alert!");
208 } 242 }
209}; 243};
210 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}
257
258#############################################################################
259# load/save/clean perl data associated with a map
260
261*on_mapclean = sub {
262 my ($map) = @_;
263
264 my $path = $map->tmpname;
265 defined $path or return;
266
267 unlink "$path.cfperl";
268};
269
270*on_mapin =
271*on_mapload = sub {
272 my ($map) = @_;
273
274 my $path = $map->tmpname;
275 $path = $map->path unless defined $path;
276
277 open my $fh, "<:raw", "$path.cfperl"
278 or return; # no perl data
279
280 my $data = Storable::thaw do { local $/; <$fh> };
281
282 $data->{version} <= 1
283 or return; # too new
284
285 $map->_set_obs ($data->{obs});
286};
287
288*on_mapout = sub {
289 my ($map) = @_;
290
291 my $path = $map->tmpname;
292 $path = $map->path unless defined $path;
293
294 my $obs = $map->_get_obs;
295
296 if (defined $obs) {
297 open my $fh, ">:raw", "$path.cfperl"
298 or die "$path.cfperl: $!";
299
300 stat $path;
301
302 print $fh Storable::nfreeze {
303 size => (stat _)[7],
304 time => (stat _)[9],
305 version => 1,
306 obs => $obs,
307 };
308
309 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
310 } else {
311 unlink "$path.cfperl";
312 }
313};
314
315#############################################################################
316# load/save perl data associated with player->ob objects
317
318*on_player_load = sub {
319 my ($ob, $path) = @_;
320
321 if (open my $fh, "<:raw", "$path.cfperl") {
322
323 #d##TODO#remove
324
325 my $data = Storable::thaw do { local $/; <$fh> };
326
327 $data->{version} <= 1
328 or return; # too new
329
330 %$ob = %{$data->{ob}};
331 return;
332 }
333
334 for my $o ($ob, $ob->inv) {
335 if (my $value = $o->get_ob_key_value ("_perl_data")) {
336 $o->set_ob_key_value ("_perl_data");
337
338 %$o = %{ Storable::thaw pack "H*", $value };
339 }
340 }
341};
342
343*on_player_save = sub {
344 my ($ob, $path) = @_;
345
346 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
347 for grep %$_, $ob, $ob->inv;
348
349 unlink "$path.cfperl";#d##TODO#remove
350};
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
368register "<global>", __PACKAGE__;
369
370unshift @INC, maps_directory "perl";
371
211load_extensions; 372load_extensions;
212 373
2131 3741
214 375

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines