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.17 by root, Mon Jun 19 10:20:07 2006 UTC

1package cf; 1package cf;
2 2
3use Symbol; 3use Symbol;
4use List::Util; 4use List::Util;
5use Storable;
5 6
6use strict; 7use strict;
7 8
8our %COMMAND; 9our %COMMAND;
9our @EVENT; 10our @EVENT;
21 print STDERR "cfperl: $msg"; 22 print STDERR "cfperl: $msg";
22 LOG llevError, "cfperl: $msg"; 23 LOG llevError, "cfperl: $msg";
23 }; 24 };
24} 25}
25 26
27my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
28
26# generate property mutators 29# generate property mutators
27sub prop_gen { 30sub prop_gen {
28 my ($prefix, $class) = @_; 31 my ($prefix, $class) = @_;
29 32
30 no strict 'refs'; 33 no strict 'refs';
40 $_[0]->get_property ($type, $idx) 43 $_[0]->get_property ($type, $idx)
41 }; 44 };
42 45
43 *{"$class\::set_$sub"} = sub { 46 *{"$class\::set_$sub"} = sub {
44 $_[0]->set_property ($type, $idx, $_[1]); 47 $_[0]->set_property ($type, $idx, $_[1]);
45 }; 48 } unless $ignore_set{$prop};
46 } 49 }
47} 50}
48 51
49# auto-generate most of the API 52# auto-generate most of the API
50 53
62 65
63my %ext_pkg; 66my %ext_pkg;
64my @exts; 67my @exts;
65my @hook; 68my @hook;
66my %command; 69my %command;
70my %extcmd;
67 71
68sub inject_event { 72sub inject_event {
69 my ($data) = @_; 73 my $extension = shift;
74 my $event_code = shift;
70 75
71 my $cb = $hook[$data->{event_code}]{$data->{extension}} 76 my $cb = $hook[$event_code]{$extension}
72 or return; 77 or return;
73 78
74 $cb->($data) 79 &$cb
75} 80}
76 81
77sub inject_global_event { 82sub inject_global_event {
78 my ($data) = @_; 83 my $event = shift;
79 84
80 my $cb = $hook[$data->{event_code}] 85 my $cb = $hook[$event]
81 or return; 86 or return;
82 87
83 $_->($data) for values %$cb; 88 List::Util::max map &$_, values %$cb
84
85 ()
86} 89}
87 90
88sub inject_command { 91sub inject_command {
89 my ($name, $obj, $params) = @_; 92 my ($name, $obj, $params) = @_;
90 93
97 100
98sub register_command { 101sub register_command {
99 my ($name, $time, $cb) = @_; 102 my ($name, $time, $cb) = @_;
100 103
101 my $caller = caller; 104 my $caller = caller;
102
103 warn "registering command '$name/$time' to '$caller'"; 105 #warn "registering command '$name/$time' to '$caller'";
104 106
105 push @{ $command{$name} }, [$time, $cb, $caller]; 107 push @{ $command{$name} }, [$time, $cb, $caller];
106 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 108 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
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
120sub register {
121 my ($base, $pkg) = @_;
122
123 for my $idx (0 .. $#EVENT) {
124 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
125 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
126 $hook[$idx]{$base} = $ref;
127 }
128 }
107} 129}
108 130
109sub load_extension { 131sub load_extension {
110 my ($path) = @_; 132 my ($path) = @_;
111 133
130 or die "$path: $@"; 152 or die "$path: $@";
131 153
132 push @exts, $pkg; 154 push @exts, $pkg;
133 $ext_pkg{$base} = $pkg; 155 $ext_pkg{$base} = $pkg;
134 156
135 no strict 'refs'; 157# no strict 'refs';
136
137# @{"$pkg\::ISA"} = cf::ext::; 158# @{"$pkg\::ISA"} = cf::ext::;
138 159
139 for my $idx (0 .. $#EVENT) { 160 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} 161}
146 162
147sub unload_extension { 163sub unload_extension {
148 my ($pkg) = @_; 164 my ($pkg) = @_;
149 165
165 delete $command{$name}; 181 delete $command{$name};
166 delete $COMMAND{"$name\000"}; 182 delete $COMMAND{"$name\000"};
167 } 183 }
168 } 184 }
169 185
186 # remove extcmds
187 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
188 delete $extcmd{$name};
189 }
190
170 Symbol::delete_package $pkg; 191 Symbol::delete_package $pkg;
171} 192}
172 193
173sub load_extensions { 194sub load_extensions {
174 my $LIBDIR = maps_directory "perl"; 195 my $LIBDIR = maps_directory "perl";
206 } else { 227 } else {
207 $who->message ("Intruder Alert!"); 228 $who->message ("Intruder Alert!");
208 } 229 }
209}; 230};
210 231
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#############################################################################
246# load/save/clean perl data associated with a map
247
248*on_mapclean = sub {
249 my ($map) = @_;
250
251 my $path = $map->tmpname;
252 defined $path or return;
253
254 unlink "$path.cfperl";
255};
256
257*on_mapin =
258*on_mapload = sub {
259 my ($map) = @_;
260
261 my $path = $map->tmpname;
262 $path = $map->path unless defined $path;
263
264 open my $fh, "<:raw", "$path.cfperl"
265 or return; # no perl data
266
267 my $data = Storable::thaw do { local $/; <$fh> };
268
269 $data->{version} <= 1
270 or return; # too new
271
272 $map->_set_obs ($data->{obs});
273};
274
275*on_mapout = sub {
276 my ($map) = @_;
277
278 my $path = $map->tmpname;
279 $path = $map->path unless defined $path;
280
281 my $obs = $map->_get_obs;
282
283 if (defined $obs) {
284 open my $fh, ">:raw", "$path.cfperl"
285 or die "$path.cfperl: $!";
286
287 stat $path;
288
289 print $fh Storable::nfreeze {
290 size => (stat _)[7],
291 time => (stat _)[9],
292 version => 1,
293 obs => $obs,
294 };
295
296 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
297 } else {
298 unlink "$path.cfperl";
299 }
300};
301
302#############################################################################
303# load/save perl data associated with player->ob objects
304
305*on_player_load = sub {
306 my ($ob, $path) = @_;
307
308 if (open my $fh, "<:raw", "$path.cfperl") {
309
310 #d##TODO#remove
311
312 my $data = Storable::thaw do { local $/; <$fh> };
313
314 $data->{version} <= 1
315 or return; # too new
316
317 %$ob = %{$data->{ob}};
318 return;
319 }
320
321 for my $o ($ob, $ob->inv) {
322 if (my $value = $o->get_ob_key_value ("_perl_data")) {
323 $o->set_ob_key_value ("_perl_data");
324
325 %$o = %{ Storable::thaw pack "H*", $value };
326 }
327 }
328};
329
330*on_player_save = sub {
331 my ($ob, $path) = @_;
332
333 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
334 for grep %$_, $ob, $ob->inv;
335
336 unlink "$path.cfperl";#d##TODO#remove
337};
338
339register "<global>", __PACKAGE__;
340
341unshift @INC, maps_directory "perl";
342
211load_extensions; 343load_extensions;
212 344
2131 3451
214 346

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines