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.15 by root, Sun Jun 11 15:02:16 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
102 105
103 warn "registering command '$name/$time' to '$caller'"; 106 warn "registering command '$name/$time' to '$caller'";
104 107
105 push @{ $command{$name} }, [$time, $cb, $caller]; 108 push @{ $command{$name} }, [$time, $cb, $caller];
106 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110}
111
112sub register {
113 my ($base, $pkg) = @_;
114
115 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref;
119 }
120 }
107} 121}
108 122
109sub load_extension { 123sub load_extension {
110 my ($path) = @_; 124 my ($path) = @_;
111 125
130 or die "$path: $@"; 144 or die "$path: $@";
131 145
132 push @exts, $pkg; 146 push @exts, $pkg;
133 $ext_pkg{$base} = $pkg; 147 $ext_pkg{$base} = $pkg;
134 148
135 no strict 'refs'; 149# no strict 'refs';
136
137# @{"$pkg\::ISA"} = cf::ext::; 150# @{"$pkg\::ISA"} = cf::ext::;
138 151
139 for my $idx (0 .. $#EVENT) { 152 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} 153}
146 154
147sub unload_extension { 155sub unload_extension {
148 my ($pkg) = @_; 156 my ($pkg) = @_;
149 157
165 delete $command{$name}; 173 delete $command{$name};
166 delete $COMMAND{"$name\000"}; 174 delete $COMMAND{"$name\000"};
167 } 175 }
168 } 176 }
169 177
178 # remove extcmds
179 for my $name (keys %command) {
180 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
181
182 if (@cb) {
183 $command{$name} = \@cb;
184 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
185 } else {
186 delete $command{$name};
187 delete $COMMAND{"$name\000"};
188 }
189 }
190
191
170 Symbol::delete_package $pkg; 192 Symbol::delete_package $pkg;
171} 193}
172 194
173sub load_extensions { 195sub load_extensions {
174 my $LIBDIR = maps_directory "perl"; 196 my $LIBDIR = maps_directory "perl";
206 } else { 228 } else {
207 $who->message ("Intruder Alert!"); 229 $who->message ("Intruder Alert!");
208 } 230 }
209}; 231};
210 232
233#############################################################################
234# extcmd framework, basically convert ext <id> <pkg> arg1 args
235# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
236
237sub on_extcmd {
238 my ($pl, $buf) = @_;
239
240 my ($id, $pkg, $name, $data) = split / /, $buf, 4;
241
242 if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) {
243 $method->($pl, $id, $buf);
244 }
245}
246
247#############################################################################
248# load/save/clean perl data associated with a map
249
250*on_mapclean = sub {
251 my ($map) = @_;
252
253 my $path = $map->tmpname;
254 defined $path or return;
255
256 unlink "$path.cfperl";
257};
258
259*on_mapin =
260*on_mapload = sub {
261 my ($map) = @_;
262
263 my $path = $map->tmpname;
264 $path = $map->path unless defined $path;
265
266 open my $fh, "<:raw", "$path.cfperl"
267 or return; # no perl data
268
269 my $data = Storable::thaw do { local $/; <$fh> };
270
271 $data->{version} <= 1
272 or return; # too new
273
274 $map->_set_obs ($data->{obs});
275};
276
277*on_mapout = sub {
278 my ($map) = @_;
279
280 my $path = $map->tmpname;
281 $path = $map->path unless defined $path;
282
283 my $obs = $map->_get_obs;
284
285 if (defined $obs) {
286 open my $fh, ">:raw", "$path.cfperl"
287 or die "$path.cfperl: $!";
288
289 stat $path;
290
291 print $fh Storable::nfreeze {
292 size => (stat _)[7],
293 time => (stat _)[9],
294 version => 1,
295 obs => $obs,
296 };
297
298 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
299 } else {
300 unlink "$path.cfperl";
301 }
302};
303
304#############################################################################
305# load/save perl data associated with player->ob objects
306
307*on_player_load = sub {
308 my ($ob, $path) = @_;
309
310 if (open my $fh, "<:raw", "$path.cfperl") {
311
312 #d##TODO#remove
313
314 my $data = Storable::thaw do { local $/; <$fh> };
315
316 $data->{version} <= 1
317 or return; # too new
318
319 %$ob = %{$data->{ob}};
320 return;
321 }
322
323 for my $o ($ob, $ob->inv) {
324 if (my $value = $o->get_ob_key_value ("_perl_data")) {
325 $o->set_ob_key_value ("_perl_data");
326
327 %$o = %{ Storable::thaw pack "H*", $value };
328 }
329 }
330};
331
332*on_player_save = sub {
333 my ($ob, $path) = @_;
334
335 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
336 for grep %$_, $ob, $ob->inv;
337
338 unlink "$path.cfperl";#d##TODO#remove
339};
340
341register "<global>", __PACKAGE__;
342
211load_extensions; 343load_extensions;
212 344
2131 3451
214 346

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines