ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
Revision: 1.21
Committed: Sun Jul 16 17:51:40 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.20: +5 -1 lines
Log Message:
implement on_unload hook

File Contents

# User Rev Content
1 root 1.1 package cf;
2    
3     use Symbol;
4     use List::Util;
5 root 1.6 use Storable;
6 root 1.19
7 root 1.18 use Event;
8 root 1.19 $Event::Eval = 1; # no idea why this is required, but it is
9 root 1.1
10     use strict;
11    
12     our %COMMAND;
13     our @EVENT;
14     our %PROP_TYPE;
15     our %PROP_IDX;
16    
17     BEGIN {
18     @EVENT = map lc, @EVENT;
19    
20     *CORE::GLOBAL::warn = sub {
21     my $msg = join "", @_;
22     $msg .= "\n"
23     unless $msg =~ /\n$/;
24    
25     print STDERR "cfperl: $msg";
26     LOG llevError, "cfperl: $msg";
27     };
28     }
29    
30 root 1.9 my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
31    
32 root 1.1 # generate property mutators
33     sub prop_gen {
34     my ($prefix, $class) = @_;
35    
36     no strict 'refs';
37    
38     for my $prop (keys %PROP_TYPE) {
39     $prop =~ /^\Q$prefix\E_(.*$)/ or next;
40     my $sub = lc $1;
41    
42     my $type = $PROP_TYPE{$prop};
43     my $idx = $PROP_IDX {$prop};
44    
45     *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub {
46     $_[0]->get_property ($type, $idx)
47     };
48    
49     *{"$class\::set_$sub"} = sub {
50     $_[0]->set_property ($type, $idx, $_[1]);
51 root 1.9 } unless $ignore_set{$prop};
52 root 1.1 }
53     }
54    
55     # auto-generate most of the API
56    
57     prop_gen OBJECT_PROP => "cf::object";
58     # CFAPI_OBJECT_ANIMATION?
59     prop_gen PLAYER_PROP => "cf::object::player";
60    
61     prop_gen MAP_PROP => "cf::map";
62     prop_gen ARCH_PROP => "cf::arch";
63    
64     # guessed hierarchies
65    
66     @cf::object::player::ISA = 'cf::object';
67     @cf::object::map::ISA = 'cf::object';
68    
69 root 1.18 $Event::DIED = sub {
70     warn "error in event callback: @_";
71     };
72    
73 root 1.5 my %ext_pkg;
74 root 1.1 my @exts;
75     my @hook;
76     my %command;
77 root 1.15 my %extcmd;
78 root 1.1
79     sub inject_event {
80 root 1.14 my $extension = shift;
81     my $event_code = shift;
82 root 1.1
83 root 1.14 my $cb = $hook[$event_code]{$extension}
84 root 1.5 or return;
85    
86 root 1.14 &$cb
87 root 1.5 }
88    
89     sub inject_global_event {
90 root 1.12 my $event = shift;
91 root 1.5
92 root 1.12 my $cb = $hook[$event]
93 root 1.1 or return;
94    
95 root 1.12 List::Util::max map &$_, values %$cb
96 root 1.1 }
97    
98     sub inject_command {
99     my ($name, $obj, $params) = @_;
100    
101     for my $cmd (@{ $command{$name} }) {
102     $cmd->[1]->($obj, $params);
103     }
104    
105     -1
106     }
107    
108     sub register_command {
109     my ($name, $time, $cb) = @_;
110    
111     my $caller = caller;
112 root 1.16 #warn "registering command '$name/$time' to '$caller'";
113 root 1.4
114 root 1.1 push @{ $command{$name} }, [$time, $cb, $caller];
115     $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
116     }
117    
118 root 1.16 sub 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    
127 root 1.6 sub register {
128     my ($base, $pkg) = @_;
129    
130     for my $idx (0 .. $#EVENT) {
131     if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
132 root 1.16 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
133 root 1.6 $hook[$idx]{$base} = $ref;
134     }
135     }
136     }
137    
138 root 1.1 sub load_extension {
139     my ($path) = @_;
140    
141     $path =~ /([^\/\\]+)\.ext$/ or die "$path";
142 root 1.5 my $base = $1;
143 root 1.1 my $pkg = $1;
144     $pkg =~ s/[^[:word:]]/_/g;
145     $pkg = "cf::ext::$pkg";
146    
147     warn "loading '$path' into '$pkg'\n";
148    
149     open my $fh, "<:utf8", $path
150     or die "$path: $!";
151    
152     my $source =
153     "package $pkg; use strict; use utf8;\n"
154     . "#line 1 \"$path\"\n{\n"
155     . (do { local $/; <$fh> })
156     . "\n};\n1";
157    
158     eval $source
159     or die "$path: $@";
160    
161     push @exts, $pkg;
162 root 1.5 $ext_pkg{$base} = $pkg;
163 root 1.1
164 root 1.6 # no strict 'refs';
165 root 1.1 # @{"$pkg\::ISA"} = cf::ext::;
166    
167 root 1.6 register $base, $pkg;
168 root 1.1 }
169    
170     sub unload_extension {
171     my ($pkg) = @_;
172    
173     warn "removing extension $pkg\n";
174    
175 root 1.21 if (my $cb = $pkg->can ("on_unload")) {
176     $cb->($pkg);
177     }
178    
179 root 1.1 # remove hooks
180     for my $idx (0 .. $#EVENT) {
181     delete $hook[$idx]{$pkg};
182     }
183    
184     # remove commands
185     for my $name (keys %command) {
186     my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
187    
188     if (@cb) {
189     $command{$name} = \@cb;
190     $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
191     } else {
192     delete $command{$name};
193     delete $COMMAND{"$name\000"};
194     }
195     }
196    
197 root 1.15 # remove extcmds
198 root 1.16 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
199     delete $extcmd{$name};
200 root 1.15 }
201    
202 root 1.1 Symbol::delete_package $pkg;
203     }
204    
205     sub load_extensions {
206     my $LIBDIR = maps_directory "perl";
207    
208     for my $ext (<$LIBDIR/*.ext>) {
209 root 1.3 next unless -r $ext;
210 root 1.2 eval {
211     load_extension $ext;
212     1
213     } or warn "$ext not loaded: $@";
214 root 1.1 }
215     }
216    
217     register_command "perl-reload", 0, sub {
218     my ($who, $arg) = @_;
219    
220     if ($who->flag (FLAG_WIZ)) {
221 root 1.3 $who->message ("reloading...");
222    
223 root 1.1 warn "reloading...\n";
224 root 1.4 eval {
225 root 1.20 $_->cancel for Event::all_watchers;
226    
227 root 1.4 unload_extension $_ for @exts;
228     delete $INC{"cf.pm"};
229    
230     # don't, removes xs symbols, too
231 root 1.21 #Symbol::delete_package __PACKAGE__;
232 root 1.4
233     require cf;
234     };
235     warn $@ if $@;
236     $who->message ($@) if $@;
237 root 1.1 warn "reloaded\n";
238 root 1.3
239     $who->message ("reloaded");
240     } else {
241     $who->message ("Intruder Alert!");
242 root 1.1 }
243     };
244    
245 root 1.8 #############################################################################
246 root 1.15 # extcmd framework, basically convert ext <id> <pkg> arg1 args
247     # into pkg::->on_extcmd_arg1 (...) while shortcutting a few
248    
249     sub on_extcmd {
250     my ($pl, $buf) = @_;
251    
252 root 1.16 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
253 root 1.15
254 root 1.16 $extcmd{$type}[0]->($pl, $buf)
255     if $extcmd{$type};
256 root 1.15 }
257    
258     #############################################################################
259 root 1.8 # load/save/clean perl data associated with a map
260    
261 root 1.7 *on_mapclean = sub {
262 root 1.13 my ($map) = @_;
263 root 1.7
264     my $path = $map->tmpname;
265     defined $path or return;
266    
267     unlink "$path.cfperl";
268     };
269    
270 root 1.6 *on_mapin =
271     *on_mapload = sub {
272 root 1.13 my ($map) = @_;
273 root 1.6
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 root 1.13 my ($map) = @_;
290 root 1.6
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 root 1.8 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 root 1.13 my ($ob, $path) = @_;
320 root 1.8
321 root 1.11 if (open my $fh, "<:raw", "$path.cfperl") {
322 root 1.8
323 root 1.11 #d##TODO#remove
324 root 1.8
325 root 1.11 my $data = Storable::thaw do { local $/; <$fh> };
326 root 1.8
327 root 1.11 $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 root 1.8 };
342    
343     *on_player_save = sub {
344 root 1.13 my ($ob, $path) = @_;
345 root 1.8
346 root 1.11 $_->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 root 1.6 };
351    
352     register "<global>", __PACKAGE__;
353    
354 root 1.17 unshift @INC, maps_directory "perl";
355    
356 root 1.1 load_extensions;
357    
358     1
359