… | |
… | |
11 | use Event; |
11 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | our %COMMAND; |
16 | our %COMMAND = (); |
17 | our @EVENT; |
17 | our @EVENT; |
|
|
18 | our @PLUGIN_EVENT; |
18 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
19 | our %PROP_IDX; |
20 | our %PROP_IDX; |
20 | our $LIBDIR = maps_directory "perl"; |
21 | our $LIBDIR = maps_directory "perl"; |
21 | |
22 | |
|
|
23 | our $TICK = MAX_TIME * 1e-6; |
|
|
24 | our $TICK_WATCHER; |
|
|
25 | our $NEXT_TICK; |
|
|
26 | |
22 | BEGIN { |
27 | BEGIN { |
23 | @EVENT = map lc, @EVENT; |
28 | @PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
24 | |
29 | |
25 | *CORE::GLOBAL::warn = sub { |
30 | *CORE::GLOBAL::warn = sub { |
26 | my $msg = join "", @_; |
31 | my $msg = join "", @_; |
27 | $msg .= "\n" |
32 | $msg .= "\n" |
28 | unless $msg =~ /\n$/; |
33 | unless $msg =~ /\n$/; |
… | |
… | |
86 | my @exts; |
91 | my @exts; |
87 | my @hook; |
92 | my @hook; |
88 | my %command; |
93 | my %command; |
89 | my %extcmd; |
94 | my %extcmd; |
90 | |
95 | |
|
|
96 | ############################################################################# |
|
|
97 | # "new" plug-in system |
|
|
98 | |
|
|
99 | =item cf::object::attach ... # NYI |
|
|
100 | |
|
|
101 | =item cf::attach_global ... |
|
|
102 | |
|
|
103 | =item cf::attach_to_type ... |
|
|
104 | |
|
|
105 | =item cf::attach_to_objects ... |
|
|
106 | |
|
|
107 | =item cf::attach_to_players ... |
|
|
108 | |
|
|
109 | =item cf::attach_to_maps ... |
|
|
110 | |
|
|
111 | prio => $number, # lower is earlier |
|
|
112 | on_xxx => \&cb, |
|
|
113 | package => package::, |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | # the following variables are defined in .xs and must not be re-created |
|
|
118 | our @CB_GLOBAL = (); # registry for all global events |
|
|
119 | our @CB_OBJECT = (); |
|
|
120 | our @CB_PLAYER = (); |
|
|
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
122 | our @CB_MAP = (); |
|
|
123 | |
|
|
124 | sub _attach_cb($\%$$$) { |
|
|
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
126 | |
|
|
127 | use sort 'stable'; |
|
|
128 | |
|
|
129 | $cb = [$prio, $cb]; |
|
|
130 | |
|
|
131 | @{$registry->[$event]} = sort |
|
|
132 | { $a->[0] cmp $b->[0] } |
|
|
133 | @{$registry->[$event] || []}, $cb; |
|
|
134 | |
|
|
135 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
136 | } |
|
|
137 | |
|
|
138 | # attach handles attaching event callbacks |
|
|
139 | # the only thing the caller has to do is pass the correct |
|
|
140 | # registry (== where the callback attaches to). |
|
|
141 | sub _attach(\@$\@) { |
|
|
142 | my ($registry, $klass, $arg) = @_; |
|
|
143 | |
|
|
144 | my $prio = 0; |
|
|
145 | |
|
|
146 | my %undo = ( |
|
|
147 | registry => $registry, |
|
|
148 | cb => [], |
|
|
149 | ); |
|
|
150 | |
|
|
151 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
152 | |
|
|
153 | while (@$arg) { |
|
|
154 | my $type = shift @$arg; |
|
|
155 | |
|
|
156 | if ($type eq "prio") { |
|
|
157 | $prio = shift @$arg; |
|
|
158 | |
|
|
159 | } elsif ($type eq "package") { |
|
|
160 | my $pkg = shift @$arg; |
|
|
161 | |
|
|
162 | while (my ($name, $id) = each %cb_id) { |
|
|
163 | if (my $cb = $pkg->can ($name)) { |
|
|
164 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
165 | } |
|
|
166 | } |
|
|
167 | |
|
|
168 | } elsif (exists $cb_id{$type}) { |
|
|
169 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
|
|
170 | |
|
|
171 | } elsif (ref $type) { |
|
|
172 | warn "attaching objects not supported, ignoring.\n"; |
|
|
173 | |
|
|
174 | } else { |
|
|
175 | shift @$arg; |
|
|
176 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
177 | } |
|
|
178 | } |
|
|
179 | |
|
|
180 | \%undo |
|
|
181 | } |
|
|
182 | |
|
|
183 | sub cf::object::attach { |
|
|
184 | die; |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub attach_global { |
|
|
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub attach_to_type { |
|
|
192 | my $type = shift; |
|
|
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
|
|
194 | } |
|
|
195 | |
|
|
196 | sub attach_to_objects { |
|
|
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub attach_to_players { |
|
|
201 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
202 | } |
|
|
203 | |
|
|
204 | sub attach_to_maps { |
|
|
205 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
206 | } |
|
|
207 | |
|
|
208 | our $override; |
|
|
209 | |
|
|
210 | sub override() { |
|
|
211 | $override = 1 |
|
|
212 | } |
|
|
213 | |
|
|
214 | sub invoke { |
|
|
215 | my $event = shift; |
|
|
216 | my $callbacks = shift; |
|
|
217 | |
|
|
218 | local $override; |
|
|
219 | |
|
|
220 | for (@$callbacks) { |
|
|
221 | eval { &{$_->[1]} }; |
|
|
222 | |
|
|
223 | if ($@) { |
|
|
224 | warn "$@"; |
|
|
225 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
|
|
226 | override; |
|
|
227 | } |
|
|
228 | |
|
|
229 | return 1 if $override; |
|
|
230 | } |
|
|
231 | |
|
|
232 | 0 |
|
|
233 | } |
|
|
234 | |
|
|
235 | ############################################################################# |
|
|
236 | # old plug-in events |
|
|
237 | |
91 | sub inject_event { |
238 | sub inject_event { |
92 | my $extension = shift; |
239 | my $extension = shift; |
93 | my $event_code = shift; |
240 | my $event_code = shift; |
94 | |
241 | |
95 | my $cb = $hook[$event_code]{$extension} |
242 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
137 | } |
284 | } |
138 | |
285 | |
139 | sub register { |
286 | sub register { |
140 | my ($base, $pkg) = @_; |
287 | my ($base, $pkg) = @_; |
141 | |
288 | |
142 | for my $idx (0 .. $#EVENT) { |
289 | for my $idx (0 .. $#PLUGIN_EVENT) { |
143 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
290 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
144 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
291 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
145 | $hook[$idx]{$base} = $ref; |
292 | $hook[$idx]{$base} = $ref; |
146 | } |
293 | } |
147 | } |
294 | } |
148 | } |
295 | } |
149 | |
296 | |
… | |
… | |
183 | my ($pkg) = @_; |
330 | my ($pkg) = @_; |
184 | |
331 | |
185 | warn "removing extension $pkg\n"; |
332 | warn "removing extension $pkg\n"; |
186 | |
333 | |
187 | # remove hooks |
334 | # remove hooks |
188 | for my $idx (0 .. $#EVENT) { |
335 | for my $idx (0 .. $#PLUGIN_EVENT) { |
189 | delete $hook[$idx]{$pkg}; |
336 | delete $hook[$idx]{$pkg}; |
190 | } |
337 | } |
191 | |
338 | |
192 | # remove commands |
339 | # remove commands |
193 | for my $name (keys %command) { |
340 | for my $name (keys %command) { |
… | |
… | |
227 | 1 |
374 | 1 |
228 | } or warn "$ext not loaded: $@"; |
375 | } or warn "$ext not loaded: $@"; |
229 | } |
376 | } |
230 | } |
377 | } |
231 | |
378 | |
|
|
379 | sub _perl_reload(&) { |
|
|
380 | my ($msg) = @_; |
|
|
381 | |
|
|
382 | $msg->("reloading..."); |
|
|
383 | |
|
|
384 | eval { |
|
|
385 | # 1. cancel all watchers |
|
|
386 | $_->cancel for Event::all_watchers; |
|
|
387 | |
|
|
388 | # 2. unload all extensions |
|
|
389 | for (@exts) { |
|
|
390 | $msg->("unloading <$_>"); |
|
|
391 | unload_extension $_; |
|
|
392 | } |
|
|
393 | |
|
|
394 | # 3. unload all modules loaded from $LIBDIR |
|
|
395 | while (my ($k, $v) = each %INC) { |
|
|
396 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
397 | |
|
|
398 | $msg->("removing <$k>"); |
|
|
399 | delete $INC{$k}; |
|
|
400 | |
|
|
401 | $k =~ s/\.pm$//; |
|
|
402 | $k =~ s/\//::/g; |
|
|
403 | |
|
|
404 | if (my $cb = $k->can ("unload_module")) { |
|
|
405 | $cb->(); |
|
|
406 | } |
|
|
407 | |
|
|
408 | Symbol::delete_package $k; |
|
|
409 | } |
|
|
410 | |
|
|
411 | # 4. get rid of ext::, as good as possible |
|
|
412 | Symbol::delete_package "ext::$_" |
|
|
413 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
414 | |
|
|
415 | # 5. remove register_script_function callbacks |
|
|
416 | # TODO |
|
|
417 | |
|
|
418 | # 6. unload cf.pm "a bit" |
|
|
419 | delete $INC{"cf.pm"}; |
|
|
420 | |
|
|
421 | # don't, removes xs symbols, too |
|
|
422 | #Symbol::delete_package __PACKAGE__; |
|
|
423 | |
|
|
424 | # 7. reload cf.pm |
|
|
425 | $msg->("reloading cf.pm"); |
|
|
426 | require cf; |
|
|
427 | }; |
|
|
428 | $msg->($@) if $@; |
|
|
429 | |
|
|
430 | $msg->("reloaded"); |
|
|
431 | }; |
|
|
432 | |
|
|
433 | sub perl_reload() { |
|
|
434 | _perl_reload { |
|
|
435 | warn $_[0]; |
|
|
436 | print "$_[0]\n"; |
|
|
437 | }; |
|
|
438 | } |
|
|
439 | |
232 | register_command "perl-reload", 0, sub { |
440 | register_command "perl-reload", 0, sub { |
233 | my ($who, $arg) = @_; |
441 | my ($who, $arg) = @_; |
234 | |
442 | |
235 | if ($who->flag (FLAG_WIZ)) { |
443 | if ($who->flag (FLAG_WIZ)) { |
236 | $who->message ("reloading..."); |
444 | _perl_reload { |
237 | |
445 | warn $_[0]; |
238 | warn "reloading...\n"; |
446 | $who->message ($_[0]); |
239 | eval { |
|
|
240 | # 1. cancel all watchers |
|
|
241 | $_->cancel for Event::all_watchers; |
|
|
242 | |
|
|
243 | # 2. unload all extensions |
|
|
244 | for (@exts) { |
|
|
245 | $who->message ("unloading <$_>"); |
|
|
246 | unload_extension $_; |
|
|
247 | } |
|
|
248 | |
|
|
249 | # 3. unload all modules loaded from $LIBDIR |
|
|
250 | while (my ($k, $v) = each %INC) { |
|
|
251 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
252 | |
|
|
253 | $who->message ("removing <$k>"); |
|
|
254 | delete $INC{$k}; |
|
|
255 | |
|
|
256 | $k =~ s/\.pm$//; |
|
|
257 | $k =~ s/\//::/g; |
|
|
258 | |
|
|
259 | if (my $cb = $k->can ("unload_module")) { |
|
|
260 | $cb->(); |
|
|
261 | } |
|
|
262 | |
|
|
263 | Symbol::delete_package $k; |
|
|
264 | } |
|
|
265 | |
|
|
266 | # 4. get rid of ext::, as good as possible |
|
|
267 | Symbol::delete_package "ext::$_" |
|
|
268 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
269 | |
|
|
270 | # 5. remove register_script_function callbacks |
|
|
271 | # TODO |
|
|
272 | |
|
|
273 | # 6. unload cf.pm "a bit" |
|
|
274 | delete $INC{"cf.pm"}; |
|
|
275 | |
|
|
276 | # don't, removes xs symbols, too |
|
|
277 | #Symbol::delete_package __PACKAGE__; |
|
|
278 | |
|
|
279 | # 7. reload cf.pm |
|
|
280 | $who->message ("reloading cf.pm"); |
|
|
281 | require cf; |
|
|
282 | }; |
447 | }; |
283 | warn $@ if $@; |
|
|
284 | $who->message ($@) if $@; |
|
|
285 | warn "reloaded\n"; |
|
|
286 | |
|
|
287 | $who->message ("reloaded"); |
|
|
288 | } else { |
|
|
289 | $who->message ("Intruder Alert!"); |
|
|
290 | } |
448 | } |
291 | }; |
449 | }; |
292 | |
450 | |
293 | ############################################################################# |
451 | ############################################################################# |
294 | # utility functions |
452 | # utility functions |
… | |
… | |
328 | } |
486 | } |
329 | |
487 | |
330 | ############################################################################# |
488 | ############################################################################# |
331 | # load/save/clean perl data associated with a map |
489 | # load/save/clean perl data associated with a map |
332 | |
490 | |
333 | *on_mapclean = sub { |
491 | *cf::mapsupport::on_clean = sub { |
334 | my ($map) = @_; |
492 | my ($map) = @_; |
335 | |
493 | |
336 | my $path = $map->tmpname; |
494 | my $path = $map->tmpname; |
337 | defined $path or return; |
495 | defined $path or return; |
338 | |
496 | |
339 | unlink "$path.cfperl"; |
497 | unlink "$path.cfperl"; |
340 | }; |
498 | }; |
341 | |
499 | |
342 | *on_mapin = |
500 | *cf::mapsupport::on_swapin = |
343 | *on_mapload = sub { |
501 | *cf::mapsupport::on_load = sub { |
344 | my ($map) = @_; |
502 | my ($map) = @_; |
345 | |
503 | |
346 | my $path = $map->tmpname; |
504 | my $path = $map->tmpname; |
347 | $path = $map->path unless defined $path; |
505 | $path = $map->path unless defined $path; |
348 | |
506 | |
… | |
… | |
355 | or return; # too new |
513 | or return; # too new |
356 | |
514 | |
357 | $map->_set_obs ($data->{obs}); |
515 | $map->_set_obs ($data->{obs}); |
358 | }; |
516 | }; |
359 | |
517 | |
360 | *on_mapout = sub { |
518 | *cf::mapsupport::on_swapout = sub { |
361 | my ($map) = @_; |
519 | my ($map) = @_; |
362 | |
520 | |
363 | my $path = $map->tmpname; |
521 | my $path = $map->tmpname; |
364 | $path = $map->path unless defined $path; |
522 | $path = $map->path unless defined $path; |
365 | |
523 | |
… | |
… | |
382 | } else { |
540 | } else { |
383 | unlink "$path.cfperl"; |
541 | unlink "$path.cfperl"; |
384 | } |
542 | } |
385 | }; |
543 | }; |
386 | |
544 | |
|
|
545 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
546 | |
387 | ############################################################################# |
547 | ############################################################################# |
388 | # load/save perl data associated with player->ob objects |
548 | # load/save perl data associated with player->ob objects |
389 | |
549 | |
390 | sub all_objects(@) { |
550 | sub all_objects(@) { |
391 | @_, map all_objects ($_->inv), @_ |
551 | @_, map all_objects ($_->inv), @_ |
392 | } |
552 | } |
393 | |
553 | |
394 | *on_player_load = sub { |
554 | attach_to_players |
|
|
555 | on_load => sub { |
395 | my ($ob, $path) = @_; |
556 | my ($pl, $path) = @_; |
396 | |
557 | |
397 | for my $o (all_objects $ob) { |
558 | for my $o (all_objects $pl->ob) { |
398 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
559 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
399 | $o->set_ob_key_value ("_perl_data"); |
560 | $o->set_ob_key_value ("_perl_data"); |
400 | |
561 | |
401 | %$o = %{ Storable::thaw pack "H*", $value }; |
562 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
563 | } |
402 | } |
564 | } |
403 | } |
565 | }, |
404 | }; |
566 | on_save => sub { |
405 | |
|
|
406 | *on_player_save = sub { |
|
|
407 | my ($ob, $path) = @_; |
567 | my ($pl, $path) = @_; |
408 | |
568 | |
409 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
569 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
410 | for grep %$_, all_objects $ob; |
570 | for grep %$_, all_objects $pl->ob; |
411 | }; |
571 | }, |
|
|
572 | ; |
412 | |
573 | |
413 | ############################################################################# |
574 | ############################################################################# |
414 | # core extensions - in perl |
575 | # core extensions - in perl |
415 | |
576 | |
416 | =item cf::player::exists $login |
577 | =item cf::player::exists $login |
… | |
… | |
517 | } |
678 | } |
518 | |
679 | |
519 | ############################################################################# |
680 | ############################################################################# |
520 | # the server's main() |
681 | # the server's main() |
521 | |
682 | |
522 | sub run { |
683 | sub main { |
523 | my $tick = MAX_TIME * 1e-6; |
684 | Event::loop; |
524 | my $next = Event::time; |
685 | } |
525 | my $timer = Event->timer (at => $next, cb => sub { |
686 | |
|
|
687 | ############################################################################# |
|
|
688 | # initialisation |
|
|
689 | |
|
|
690 | register "<global>", __PACKAGE__; |
|
|
691 | |
|
|
692 | unshift @INC, $LIBDIR; |
|
|
693 | |
|
|
694 | load_extensions; |
|
|
695 | |
|
|
696 | $TICK_WATCHER = Event->timer ( |
|
|
697 | prio => 1, |
|
|
698 | at => $NEXT_TICK || 1, |
|
|
699 | cb => sub { |
526 | cf::server_tick; # one server iteration |
700 | cf::server_tick; # one server iteration |
527 | |
701 | |
528 | $next += $tick; |
|
|
529 | my $NOW = Event::time; |
702 | my $NOW = Event::time; |
|
|
703 | $NEXT_TICK += $TICK; |
530 | |
704 | |
531 | # if we are delayd by > 0.25 second, skip ticks |
705 | # if we are delayed by four ticks, skip them all |
532 | $next = $NOW if $NOW >= $next + .25; |
706 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
533 | |
707 | |
534 | $_[0]->w->at ($next); |
708 | $TICK_WATCHER->at ($NEXT_TICK); |
535 | $_[0]->w->start; |
709 | $TICK_WATCHER->start; |
536 | }); |
710 | }, |
537 | |
711 | ); |
538 | Event::loop; |
|
|
539 | } |
|
|
540 | |
|
|
541 | ############################################################################# |
|
|
542 | # initialisation |
|
|
543 | |
|
|
544 | register "<global>", __PACKAGE__; |
|
|
545 | |
|
|
546 | unshift @INC, $LIBDIR; |
|
|
547 | |
|
|
548 | load_extensions; |
|
|
549 | |
712 | |
550 | 1 |
713 | 1 |
551 | |
714 | |