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.4 by root, Tue Feb 7 01:03:44 2006 UTC vs.
Revision 1.19 by root, Sun Jul 16 17:15:39 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
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';
62 68
69$Event::DIED = sub {
70 warn "error in event callback: @_";
71};
72
73my %ext_pkg;
63my @exts; 74my @exts;
64my @hook; 75my @hook;
65my %command; 76my %command;
77my %extcmd;
66 78
67sub inject_event { 79sub inject_event {
68 my ($data) = @_; 80 my $extension = shift;
81 my $event_code = shift;
69 82
70 my $cb = $hook[$data->{event_code}] 83 my $cb = $hook[$event_code]{$extension}
71 or return; 84 or return;
72 85
73 $_->($data) for values %$cb; 86 &$cb
87}
74 88
75 () 89sub inject_global_event {
90 my $event = shift;
91
92 my $cb = $hook[$event]
93 or return;
94
95 List::Util::max map &$_, values %$cb
76} 96}
77 97
78sub inject_command { 98sub inject_command {
79 my ($name, $obj, $params) = @_; 99 my ($name, $obj, $params) = @_;
80 100
87 107
88sub register_command { 108sub register_command {
89 my ($name, $time, $cb) = @_; 109 my ($name, $time, $cb) = @_;
90 110
91 my $caller = caller; 111 my $caller = caller;
92
93 warn "registering command '$name/$time' to '$caller'"; 112 #warn "registering command '$name/$time' to '$caller'";
94 113
95 push @{ $command{$name} }, [$time, $cb, $caller]; 114 push @{ $command{$name} }, [$time, $cb, $caller];
96 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 115 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
97} 116}
98 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 }
136}
137
99sub load_extension { 138sub load_extension {
100 my ($path) = @_; 139 my ($path) = @_;
101 140
102 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 141 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
142 my $base = $1;
103 my $pkg = $1; 143 my $pkg = $1;
104 $pkg =~ s/[^[:word:]]/_/g; 144 $pkg =~ s/[^[:word:]]/_/g;
105 $pkg = "cf::ext::$pkg"; 145 $pkg = "cf::ext::$pkg";
106 146
107 warn "loading '$path' into '$pkg'\n"; 147 warn "loading '$path' into '$pkg'\n";
117 157
118 eval $source 158 eval $source
119 or die "$path: $@"; 159 or die "$path: $@";
120 160
121 push @exts, $pkg; 161 push @exts, $pkg;
162 $ext_pkg{$base} = $pkg;
122 163
123 no strict 'refs'; 164# no strict 'refs';
124
125# @{"$pkg\::ISA"} = cf::ext::; 165# @{"$pkg\::ISA"} = cf::ext::;
126 166
127 for my $idx (0 .. $#EVENT) { 167 register $base, $pkg;
128 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
129 warn "registering $EVENT[$idx] hook\n";
130 $hook[$idx]{$pkg} = $ref;
131 }
132 }
133} 168}
134 169
135sub unload_extension { 170sub unload_extension {
136 my ($pkg) = @_; 171 my ($pkg) = @_;
137 172
153 delete $command{$name}; 188 delete $command{$name};
154 delete $COMMAND{"$name\000"}; 189 delete $COMMAND{"$name\000"};
155 } 190 }
156 } 191 }
157 192
193 # remove extcmds
194 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
195 delete $extcmd{$name};
196 }
197
158 Symbol::delete_package $pkg; 198 Symbol::delete_package $pkg;
159} 199}
160 200
161sub load_extensions { 201sub load_extensions {
162 my $LIBDIR = maps_directory "perl"; 202 my $LIBDIR = maps_directory "perl";
194 } else { 234 } else {
195 $who->message ("Intruder Alert!"); 235 $who->message ("Intruder Alert!");
196 } 236 }
197}; 237};
198 238
239#############################################################################
240# extcmd framework, basically convert ext <id> <pkg> arg1 args
241# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
242
243sub on_extcmd {
244 my ($pl, $buf) = @_;
245
246 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
247
248 $extcmd{$type}[0]->($pl, $buf)
249 if $extcmd{$type};
250}
251
252#############################################################################
253# load/save/clean perl data associated with a map
254
255*on_mapclean = sub {
256 my ($map) = @_;
257
258 my $path = $map->tmpname;
259 defined $path or return;
260
261 unlink "$path.cfperl";
262};
263
264*on_mapin =
265*on_mapload = sub {
266 my ($map) = @_;
267
268 my $path = $map->tmpname;
269 $path = $map->path unless defined $path;
270
271 open my $fh, "<:raw", "$path.cfperl"
272 or return; # no perl data
273
274 my $data = Storable::thaw do { local $/; <$fh> };
275
276 $data->{version} <= 1
277 or return; # too new
278
279 $map->_set_obs ($data->{obs});
280};
281
282*on_mapout = sub {
283 my ($map) = @_;
284
285 my $path = $map->tmpname;
286 $path = $map->path unless defined $path;
287
288 my $obs = $map->_get_obs;
289
290 if (defined $obs) {
291 open my $fh, ">:raw", "$path.cfperl"
292 or die "$path.cfperl: $!";
293
294 stat $path;
295
296 print $fh Storable::nfreeze {
297 size => (stat _)[7],
298 time => (stat _)[9],
299 version => 1,
300 obs => $obs,
301 };
302
303 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
304 } else {
305 unlink "$path.cfperl";
306 }
307};
308
309#############################################################################
310# load/save perl data associated with player->ob objects
311
312*on_player_load = sub {
313 my ($ob, $path) = @_;
314
315 if (open my $fh, "<:raw", "$path.cfperl") {
316
317 #d##TODO#remove
318
319 my $data = Storable::thaw do { local $/; <$fh> };
320
321 $data->{version} <= 1
322 or return; # too new
323
324 %$ob = %{$data->{ob}};
325 return;
326 }
327
328 for my $o ($ob, $ob->inv) {
329 if (my $value = $o->get_ob_key_value ("_perl_data")) {
330 $o->set_ob_key_value ("_perl_data");
331
332 %$o = %{ Storable::thaw pack "H*", $value };
333 }
334 }
335};
336
337*on_player_save = sub {
338 my ($ob, $path) = @_;
339
340 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
341 for grep %$_, $ob, $ob->inv;
342
343 unlink "$path.cfperl";#d##TODO#remove
344};
345
346register "<global>", __PACKAGE__;
347
348unshift @INC, maps_directory "perl";
349
199load_extensions; 350load_extensions;
200 351
2011 3521
202 353

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines