1 | package cf; |
1 | package cf; |
2 | |
2 | |
3 | use Symbol; |
3 | use Symbol; |
4 | use List::Util; |
4 | use List::Util; |
5 | use Storable; |
5 | use Storable; |
|
|
6 | |
|
|
7 | use Event; |
|
|
8 | $Event::Eval = 1; # no idea why this is required, but it is |
6 | |
9 | |
7 | use strict; |
10 | use strict; |
8 | |
11 | |
9 | our %COMMAND; |
12 | our %COMMAND; |
10 | our @EVENT; |
13 | our @EVENT; |
… | |
… | |
60 | |
63 | |
61 | # guessed hierarchies |
64 | # guessed hierarchies |
62 | |
65 | |
63 | @cf::object::player::ISA = 'cf::object'; |
66 | @cf::object::player::ISA = 'cf::object'; |
64 | @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 | }; |
65 | |
72 | |
66 | my %ext_pkg; |
73 | my %ext_pkg; |
67 | my @exts; |
74 | my @exts; |
68 | my @hook; |
75 | my @hook; |
69 | my %command; |
76 | my %command; |
… | |
… | |
100 | |
107 | |
101 | sub register_command { |
108 | sub register_command { |
102 | my ($name, $time, $cb) = @_; |
109 | my ($name, $time, $cb) = @_; |
103 | |
110 | |
104 | my $caller = caller; |
111 | my $caller = caller; |
105 | |
|
|
106 | warn "registering command '$name/$time' to '$caller'"; |
112 | #warn "registering command '$name/$time' to '$caller'"; |
107 | |
113 | |
108 | push @{ $command{$name} }, [$time, $cb, $caller]; |
114 | push @{ $command{$name} }, [$time, $cb, $caller]; |
109 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
115 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
110 | } |
116 | } |
111 | |
117 | |
|
|
118 | 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 | |
112 | sub register { |
127 | sub register { |
113 | my ($base, $pkg) = @_; |
128 | my ($base, $pkg) = @_; |
114 | |
129 | |
115 | for my $idx (0 .. $#EVENT) { |
130 | for my $idx (0 .. $#EVENT) { |
116 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
131 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
117 | warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
132 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
118 | $hook[$idx]{$base} = $ref; |
133 | $hook[$idx]{$base} = $ref; |
119 | } |
134 | } |
120 | } |
135 | } |
121 | } |
136 | } |
122 | |
137 | |
… | |
… | |
174 | delete $COMMAND{"$name\000"}; |
189 | delete $COMMAND{"$name\000"}; |
175 | } |
190 | } |
176 | } |
191 | } |
177 | |
192 | |
178 | # remove extcmds |
193 | # remove extcmds |
179 | for my $name (keys %command) { |
194 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
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}; |
195 | delete $extcmd{$name}; |
187 | delete $COMMAND{"$name\000"}; |
|
|
188 | } |
|
|
189 | } |
196 | } |
190 | |
|
|
191 | |
197 | |
192 | Symbol::delete_package $pkg; |
198 | Symbol::delete_package $pkg; |
193 | } |
199 | } |
194 | |
200 | |
195 | sub load_extensions { |
201 | sub load_extensions { |
… | |
… | |
235 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
241 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
236 | |
242 | |
237 | sub on_extcmd { |
243 | sub on_extcmd { |
238 | my ($pl, $buf) = @_; |
244 | my ($pl, $buf) = @_; |
239 | |
245 | |
240 | my ($id, $pkg, $name, $data) = split / /, $buf, 4; |
246 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
241 | |
247 | |
242 | if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) { |
248 | $extcmd{$type}[0]->($pl, $buf) |
243 | $method->($pl, $id, $buf); |
249 | if $extcmd{$type}; |
244 | } |
|
|
245 | } |
250 | } |
246 | |
251 | |
247 | ############################################################################# |
252 | ############################################################################# |
248 | # load/save/clean perl data associated with a map |
253 | # load/save/clean perl data associated with a map |
249 | |
254 | |
… | |
… | |
338 | unlink "$path.cfperl";#d##TODO#remove |
343 | unlink "$path.cfperl";#d##TODO#remove |
339 | }; |
344 | }; |
340 | |
345 | |
341 | register "<global>", __PACKAGE__; |
346 | register "<global>", __PACKAGE__; |
342 | |
347 | |
|
|
348 | unshift @INC, maps_directory "perl"; |
|
|
349 | |
343 | load_extensions; |
350 | load_extensions; |
344 | |
351 | |
345 | 1 |
352 | 1 |
346 | |
353 | |