… | |
… | |
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 ... # NYI |
|
|
102 | |
|
|
103 | =item cf::attach_to_type ... # NYI |
|
|
104 | |
|
|
105 | =item cf::attach_to_objects ... # NYI |
|
|
106 | |
|
|
107 | =item cf::attach_to_players ... # NYI |
|
|
108 | |
|
|
109 | =item cf::attach_to_maps ... # NYI |
|
|
110 | |
|
|
111 | prio => $number, # higehr is earlier |
|
|
112 | on_xxx => \&cb, |
|
|
113 | package => package::, |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | our %CB_CLASS = (); # registry for class-based events |
|
|
118 | our @CB_GLOBAL = (); # registry for all global events |
|
|
119 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
120 | |
|
|
121 | sub _attach_cb($\%$$$) { |
|
|
122 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
123 | |
|
|
124 | use sort 'stable'; |
|
|
125 | |
|
|
126 | $cb = [$prio, $cb]; |
|
|
127 | |
|
|
128 | @{$registry->[$event]} = sort |
|
|
129 | { $a->[0] cmp $b->[0] } |
|
|
130 | @{$registry->[$event] || []}, $cb; |
|
|
131 | |
|
|
132 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
133 | } |
|
|
134 | |
|
|
135 | # attach handles attaching event callbacks |
|
|
136 | # the only thing the caller has to do is pass the correct |
|
|
137 | # registry (== where the callback attaches to). |
|
|
138 | sub _attach(\@$\@) { |
|
|
139 | my ($registry, $klass, $arg) = @_; |
|
|
140 | |
|
|
141 | my $prio = 0; |
|
|
142 | |
|
|
143 | my %undo = ( |
|
|
144 | registry => $registry, |
|
|
145 | cb => [], |
|
|
146 | ); |
|
|
147 | |
|
|
148 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
149 | |
|
|
150 | while (@$arg) { |
|
|
151 | my $type = shift @$arg; |
|
|
152 | |
|
|
153 | if ($type eq "prio") { |
|
|
154 | $prio = shift @$arg; |
|
|
155 | |
|
|
156 | } elsif ($type eq "package") { |
|
|
157 | my $pkg = shift @$arg; |
|
|
158 | |
|
|
159 | while (my ($name, $id) = each %cb_id) { |
|
|
160 | if (my $cb = $pkg->can ($name)) { |
|
|
161 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
162 | } |
|
|
163 | } |
|
|
164 | |
|
|
165 | } elsif (exists $cb_id{$type}) { |
|
|
166 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
|
|
167 | |
|
|
168 | } elsif (ref $type) { |
|
|
169 | warn "attaching objects not supported, ignoring.\n"; |
|
|
170 | |
|
|
171 | } else { |
|
|
172 | shift @$arg; |
|
|
173 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | \%undo |
|
|
178 | } |
|
|
179 | |
|
|
180 | sub cf::object::attach { |
|
|
181 | die; |
|
|
182 | } |
|
|
183 | |
|
|
184 | sub attach_global { |
|
|
185 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
186 | } |
|
|
187 | |
|
|
188 | sub attach_type { |
|
|
189 | my $type = shift; |
|
|
190 | _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_ |
|
|
191 | } |
|
|
192 | |
|
|
193 | sub attach_to_objects { |
|
|
194 | _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_ |
|
|
195 | } |
|
|
196 | |
|
|
197 | sub attach_to_players { |
|
|
198 | _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_ |
|
|
199 | } |
|
|
200 | |
|
|
201 | sub attach_to_maps { |
|
|
202 | _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_ |
|
|
203 | } |
|
|
204 | |
|
|
205 | our $override; |
|
|
206 | |
|
|
207 | sub override() { |
|
|
208 | $override = 1 |
|
|
209 | } |
|
|
210 | |
|
|
211 | sub invoke { |
|
|
212 | my $event = shift; |
|
|
213 | |
|
|
214 | my @cb; |
|
|
215 | |
|
|
216 | if (my $ref = ref $_[0]) { |
|
|
217 | # 1. object-specific (NYI) |
|
|
218 | # 2. class-specific |
|
|
219 | push @cb, @{$CB_CLASS{$ref}[$event] || []}; |
|
|
220 | } |
|
|
221 | |
|
|
222 | # global |
|
|
223 | push @cb, @{$CB_GLOBAL[$event] || []}; |
|
|
224 | |
|
|
225 | # warn "invoke id $EVENT[$event][0], args <@_> <=> @cb\n";#d# |
|
|
226 | |
|
|
227 | local $override; |
|
|
228 | |
|
|
229 | for (@cb) { |
|
|
230 | eval { &{$_->[1]} }; |
|
|
231 | |
|
|
232 | if ($@) { |
|
|
233 | warn "$@"; |
|
|
234 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
|
|
235 | override; |
|
|
236 | } |
|
|
237 | |
|
|
238 | return 1 if $override; |
|
|
239 | } |
|
|
240 | |
|
|
241 | 0 |
|
|
242 | } |
|
|
243 | |
|
|
244 | ############################################################################# |
|
|
245 | # old plug-in events |
|
|
246 | |
91 | sub inject_event { |
247 | sub inject_event { |
92 | my $extension = shift; |
248 | my $extension = shift; |
93 | my $event_code = shift; |
249 | my $event_code = shift; |
94 | |
250 | |
95 | my $cb = $hook[$event_code]{$extension} |
251 | my $cb = $hook[$event_code]{$extension} |
… | |
… | |
137 | } |
293 | } |
138 | |
294 | |
139 | sub register { |
295 | sub register { |
140 | my ($base, $pkg) = @_; |
296 | my ($base, $pkg) = @_; |
141 | |
297 | |
142 | for my $idx (0 .. $#EVENT) { |
298 | for my $idx (0 .. $#PLUGIN_EVENT) { |
143 | if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { |
299 | if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
144 | #warn "registering $EVENT[$idx] hook to '$pkg'\n"; |
300 | #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
145 | $hook[$idx]{$base} = $ref; |
301 | $hook[$idx]{$base} = $ref; |
146 | } |
302 | } |
147 | } |
303 | } |
148 | } |
304 | } |
149 | |
305 | |
… | |
… | |
183 | my ($pkg) = @_; |
339 | my ($pkg) = @_; |
184 | |
340 | |
185 | warn "removing extension $pkg\n"; |
341 | warn "removing extension $pkg\n"; |
186 | |
342 | |
187 | # remove hooks |
343 | # remove hooks |
188 | for my $idx (0 .. $#EVENT) { |
344 | for my $idx (0 .. $#PLUGIN_EVENT) { |
189 | delete $hook[$idx]{$pkg}; |
345 | delete $hook[$idx]{$pkg}; |
190 | } |
346 | } |
191 | |
347 | |
192 | # remove commands |
348 | # remove commands |
193 | for my $name (keys %command) { |
349 | for my $name (keys %command) { |
… | |
… | |
227 | 1 |
383 | 1 |
228 | } or warn "$ext not loaded: $@"; |
384 | } or warn "$ext not loaded: $@"; |
229 | } |
385 | } |
230 | } |
386 | } |
231 | |
387 | |
|
|
388 | sub _perl_reload(&) { |
|
|
389 | my ($msg) = @_; |
|
|
390 | |
|
|
391 | $msg->("reloading..."); |
|
|
392 | |
|
|
393 | eval { |
|
|
394 | # 1. cancel all watchers |
|
|
395 | $_->cancel for Event::all_watchers; |
|
|
396 | |
|
|
397 | # 2. unload all extensions |
|
|
398 | for (@exts) { |
|
|
399 | $msg->("unloading <$_>"); |
|
|
400 | unload_extension $_; |
|
|
401 | } |
|
|
402 | |
|
|
403 | # 3. unload all modules loaded from $LIBDIR |
|
|
404 | while (my ($k, $v) = each %INC) { |
|
|
405 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
406 | |
|
|
407 | $msg->("removing <$k>"); |
|
|
408 | delete $INC{$k}; |
|
|
409 | |
|
|
410 | $k =~ s/\.pm$//; |
|
|
411 | $k =~ s/\//::/g; |
|
|
412 | |
|
|
413 | if (my $cb = $k->can ("unload_module")) { |
|
|
414 | $cb->(); |
|
|
415 | } |
|
|
416 | |
|
|
417 | Symbol::delete_package $k; |
|
|
418 | } |
|
|
419 | |
|
|
420 | # 4. get rid of ext::, as good as possible |
|
|
421 | Symbol::delete_package "ext::$_" |
|
|
422 | for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); |
|
|
423 | |
|
|
424 | # 5. remove register_script_function callbacks |
|
|
425 | # TODO |
|
|
426 | |
|
|
427 | # 6. unload cf.pm "a bit" |
|
|
428 | delete $INC{"cf.pm"}; |
|
|
429 | |
|
|
430 | # don't, removes xs symbols, too |
|
|
431 | #Symbol::delete_package __PACKAGE__; |
|
|
432 | |
|
|
433 | # 7. reload cf.pm |
|
|
434 | $msg->("reloading cf.pm"); |
|
|
435 | require cf; |
|
|
436 | }; |
|
|
437 | $msg->($@) if $@; |
|
|
438 | |
|
|
439 | $msg->("reloaded"); |
|
|
440 | }; |
|
|
441 | |
|
|
442 | sub perl_reload() { |
|
|
443 | _perl_reload { |
|
|
444 | warn $_[0]; |
|
|
445 | print "$_[0]\n"; |
|
|
446 | }; |
|
|
447 | } |
|
|
448 | |
232 | register_command "perl-reload", 0, sub { |
449 | register_command "perl-reload", 0, sub { |
233 | my ($who, $arg) = @_; |
450 | my ($who, $arg) = @_; |
234 | |
451 | |
235 | if ($who->flag (FLAG_WIZ)) { |
452 | if ($who->flag (FLAG_WIZ)) { |
236 | $who->message ("reloading..."); |
453 | _perl_reload { |
237 | |
454 | warn $_[0]; |
238 | warn "reloading...\n"; |
455 | $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 | }; |
456 | }; |
283 | warn $@ if $@; |
|
|
284 | $who->message ($@) if $@; |
|
|
285 | warn "reloaded\n"; |
|
|
286 | |
|
|
287 | $who->message ("reloaded"); |
|
|
288 | } else { |
|
|
289 | $who->message ("Intruder Alert!"); |
|
|
290 | } |
457 | } |
291 | }; |
458 | }; |
292 | |
459 | |
293 | ############################################################################# |
460 | ############################################################################# |
294 | # utility functions |
461 | # utility functions |
… | |
… | |
328 | } |
495 | } |
329 | |
496 | |
330 | ############################################################################# |
497 | ############################################################################# |
331 | # load/save/clean perl data associated with a map |
498 | # load/save/clean perl data associated with a map |
332 | |
499 | |
333 | *on_mapclean = sub { |
500 | *cf::mapsupport::on_clean = sub { |
334 | my ($map) = @_; |
501 | my ($map) = @_; |
335 | |
502 | |
336 | my $path = $map->tmpname; |
503 | my $path = $map->tmpname; |
337 | defined $path or return; |
504 | defined $path or return; |
338 | |
505 | |
339 | unlink "$path.cfperl"; |
506 | unlink "$path.cfperl"; |
340 | }; |
507 | }; |
341 | |
508 | |
342 | *on_mapin = |
509 | *cf::mapsupport::on_swapin = |
343 | *on_mapload = sub { |
510 | *cf::mapsupport::on_load = sub { |
344 | my ($map) = @_; |
511 | my ($map) = @_; |
345 | |
512 | |
346 | my $path = $map->tmpname; |
513 | my $path = $map->tmpname; |
347 | $path = $map->path unless defined $path; |
514 | $path = $map->path unless defined $path; |
348 | |
515 | |
… | |
… | |
355 | or return; # too new |
522 | or return; # too new |
356 | |
523 | |
357 | $map->_set_obs ($data->{obs}); |
524 | $map->_set_obs ($data->{obs}); |
358 | }; |
525 | }; |
359 | |
526 | |
360 | *on_mapout = sub { |
527 | *cf::mapsupport::on_swapout = sub { |
361 | my ($map) = @_; |
528 | my ($map) = @_; |
362 | |
529 | |
363 | my $path = $map->tmpname; |
530 | my $path = $map->tmpname; |
364 | $path = $map->path unless defined $path; |
531 | $path = $map->path unless defined $path; |
365 | |
532 | |
… | |
… | |
382 | } else { |
549 | } else { |
383 | unlink "$path.cfperl"; |
550 | unlink "$path.cfperl"; |
384 | } |
551 | } |
385 | }; |
552 | }; |
386 | |
553 | |
|
|
554 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
555 | |
387 | ############################################################################# |
556 | ############################################################################# |
388 | # load/save perl data associated with player->ob objects |
557 | # load/save perl data associated with player->ob objects |
389 | |
558 | |
390 | sub all_objects(@) { |
559 | sub all_objects(@) { |
391 | @_, map all_objects ($_->inv), @_ |
560 | @_, map all_objects ($_->inv), @_ |
392 | } |
561 | } |
393 | |
562 | |
394 | *on_player_load = sub { |
563 | attach_to_players |
|
|
564 | on_load => sub { |
395 | my ($ob, $path) = @_; |
565 | my ($pl, $path) = @_; |
396 | |
566 | |
397 | for my $o (all_objects $ob) { |
567 | for my $o (all_objects $pl->ob) { |
398 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
568 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
399 | $o->set_ob_key_value ("_perl_data"); |
569 | $o->set_ob_key_value ("_perl_data"); |
400 | |
570 | |
401 | %$o = %{ Storable::thaw pack "H*", $value }; |
571 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
572 | } |
402 | } |
573 | } |
403 | } |
574 | }, |
404 | }; |
575 | on_save => sub { |
405 | |
|
|
406 | *on_player_save = sub { |
|
|
407 | my ($ob, $path) = @_; |
576 | my ($pl, $path) = @_; |
408 | |
577 | |
409 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
578 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
410 | for grep %$_, all_objects $ob; |
579 | for grep %$_, all_objects $pl->ob; |
411 | }; |
580 | }, |
|
|
581 | ; |
412 | |
582 | |
413 | ############################################################################# |
583 | ############################################################################# |
414 | # core extensions - in perl |
584 | # core extensions - in perl |
415 | |
|
|
416 | my $delta_timer = Event->timer ( |
|
|
417 | parked => 1, |
|
|
418 | prio => Event::PRIO_HIGH, |
|
|
419 | cb => sub { Event::unloop (undef) }, |
|
|
420 | ); |
|
|
421 | |
|
|
422 | sub sleep_delta($) { |
|
|
423 | $delta_timer->at (Event::time + $_[0]); |
|
|
424 | $delta_timer->start; |
|
|
425 | Event::loop; |
|
|
426 | } |
|
|
427 | |
585 | |
428 | =item cf::player::exists $login |
586 | =item cf::player::exists $login |
429 | |
587 | |
430 | Returns true when the given account exists. |
588 | Returns true when the given account exists. |
431 | |
589 | |
… | |
… | |
527 | no strict 'refs'; |
685 | no strict 'refs'; |
528 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
686 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
529 | } |
687 | } |
530 | |
688 | |
531 | ############################################################################# |
689 | ############################################################################# |
|
|
690 | # the server's main() |
|
|
691 | |
|
|
692 | sub main { |
|
|
693 | Event::loop; |
|
|
694 | } |
|
|
695 | |
|
|
696 | ############################################################################# |
532 | # initialisation |
697 | # initialisation |
533 | |
698 | |
534 | register "<global>", __PACKAGE__; |
699 | register "<global>", __PACKAGE__; |
535 | |
700 | |
536 | unshift @INC, $LIBDIR; |
701 | unshift @INC, $LIBDIR; |
537 | |
702 | |
538 | load_extensions; |
703 | load_extensions; |
539 | |
704 | |
|
|
705 | $TICK_WATCHER = Event->timer ( |
|
|
706 | prio => 1, |
|
|
707 | at => $NEXT_TICK || 1, |
|
|
708 | cb => sub { |
|
|
709 | cf::server_tick; # one server iteration |
|
|
710 | |
|
|
711 | my $NOW = Event::time; |
|
|
712 | $NEXT_TICK += $TICK; |
|
|
713 | |
|
|
714 | # if we are delayed by four ticks, skip them all |
|
|
715 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
|
|
716 | |
|
|
717 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
718 | $TICK_WATCHER->start; |
|
|
719 | }, |
|
|
720 | ); |
|
|
721 | |
540 | 1 |
722 | 1 |
541 | |
723 | |