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