| 1 |
root |
1.1 |
package cf; |
| 2 |
|
|
|
| 3 |
|
|
use Symbol; |
| 4 |
|
|
use List::Util; |
| 5 |
root |
1.6 |
use Storable; |
| 6 |
root |
1.23 |
use Opcode; |
| 7 |
|
|
use Safe; |
| 8 |
|
|
use Safe::Hole; |
| 9 |
root |
1.19 |
|
| 10 |
root |
1.32 |
use Time::HiRes; |
| 11 |
root |
1.18 |
use Event; |
| 12 |
root |
1.19 |
$Event::Eval = 1; # no idea why this is required, but it is |
| 13 |
root |
1.1 |
|
| 14 |
|
|
use strict; |
| 15 |
|
|
|
| 16 |
root |
1.39 |
our %COMMAND = (); |
| 17 |
root |
1.1 |
our @EVENT; |
| 18 |
root |
1.38 |
our @PLUGIN_EVENT; |
| 19 |
root |
1.1 |
our %PROP_TYPE; |
| 20 |
|
|
our %PROP_IDX; |
| 21 |
root |
1.27 |
our $LIBDIR = maps_directory "perl"; |
| 22 |
root |
1.1 |
|
| 23 |
root |
1.35 |
our $TICK = MAX_TIME * 1e-6; |
| 24 |
|
|
our $TICK_WATCHER; |
| 25 |
|
|
our $NEXT_TICK; |
| 26 |
|
|
|
| 27 |
root |
1.1 |
BEGIN { |
| 28 |
root |
1.38 |
@PLUGIN_EVENT = map lc, @PLUGIN_EVENT; |
| 29 |
root |
1.1 |
|
| 30 |
|
|
*CORE::GLOBAL::warn = sub { |
| 31 |
|
|
my $msg = join "", @_; |
| 32 |
|
|
$msg .= "\n" |
| 33 |
|
|
unless $msg =~ /\n$/; |
| 34 |
|
|
|
| 35 |
|
|
print STDERR "cfperl: $msg"; |
| 36 |
|
|
LOG llevError, "cfperl: $msg"; |
| 37 |
|
|
}; |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
root |
1.9 |
my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
| 41 |
|
|
|
| 42 |
root |
1.1 |
# generate property mutators |
| 43 |
|
|
sub prop_gen { |
| 44 |
|
|
my ($prefix, $class) = @_; |
| 45 |
|
|
|
| 46 |
|
|
no strict 'refs'; |
| 47 |
|
|
|
| 48 |
|
|
for my $prop (keys %PROP_TYPE) { |
| 49 |
|
|
$prop =~ /^\Q$prefix\E_(.*$)/ or next; |
| 50 |
|
|
my $sub = lc $1; |
| 51 |
|
|
|
| 52 |
|
|
my $type = $PROP_TYPE{$prop}; |
| 53 |
|
|
my $idx = $PROP_IDX {$prop}; |
| 54 |
|
|
|
| 55 |
|
|
*{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { |
| 56 |
|
|
$_[0]->get_property ($type, $idx) |
| 57 |
|
|
}; |
| 58 |
|
|
|
| 59 |
|
|
*{"$class\::set_$sub"} = sub { |
| 60 |
|
|
$_[0]->set_property ($type, $idx, $_[1]); |
| 61 |
root |
1.9 |
} unless $ignore_set{$prop}; |
| 62 |
root |
1.1 |
} |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
|
# auto-generate most of the API |
| 66 |
|
|
|
| 67 |
|
|
prop_gen OBJECT_PROP => "cf::object"; |
| 68 |
|
|
# CFAPI_OBJECT_ANIMATION? |
| 69 |
|
|
prop_gen PLAYER_PROP => "cf::object::player"; |
| 70 |
|
|
|
| 71 |
|
|
prop_gen MAP_PROP => "cf::map"; |
| 72 |
|
|
prop_gen ARCH_PROP => "cf::arch"; |
| 73 |
|
|
|
| 74 |
|
|
# guessed hierarchies |
| 75 |
|
|
|
| 76 |
root |
1.41 |
@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
| 77 |
|
|
@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
| 78 |
root |
1.25 |
|
| 79 |
root |
1.41 |
# we bless all objects into (empty) derived classes to force a method lookup |
| 80 |
root |
1.25 |
# within the Safe compartment. |
| 81 |
elmex |
1.30 |
for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
| 82 |
root |
1.25 |
no strict 'refs'; |
| 83 |
root |
1.41 |
@{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
| 84 |
root |
1.25 |
} |
| 85 |
root |
1.1 |
|
| 86 |
root |
1.18 |
$Event::DIED = sub { |
| 87 |
|
|
warn "error in event callback: @_"; |
| 88 |
|
|
}; |
| 89 |
|
|
|
| 90 |
root |
1.5 |
my %ext_pkg; |
| 91 |
root |
1.1 |
my @exts; |
| 92 |
|
|
my @hook; |
| 93 |
|
|
my %command; |
| 94 |
root |
1.15 |
my %extcmd; |
| 95 |
root |
1.1 |
|
| 96 |
root |
1.39 |
############################################################################# |
| 97 |
|
|
# "new" plug-in system |
| 98 |
|
|
|
| 99 |
|
|
=item cf::object::attach ... # NYI |
| 100 |
|
|
|
| 101 |
root |
1.40 |
=item cf::attach_global ... |
| 102 |
root |
1.39 |
|
| 103 |
root |
1.40 |
=item cf::attach_to_type ... |
| 104 |
root |
1.39 |
|
| 105 |
root |
1.40 |
=item cf::attach_to_objects ... |
| 106 |
root |
1.39 |
|
| 107 |
root |
1.40 |
=item cf::attach_to_players ... |
| 108 |
root |
1.39 |
|
| 109 |
root |
1.40 |
=item cf::attach_to_maps ... |
| 110 |
root |
1.39 |
|
| 111 |
root |
1.40 |
prio => $number, # lower is earlier |
| 112 |
root |
1.39 |
on_xxx => \&cb, |
| 113 |
|
|
package => package::, |
| 114 |
|
|
|
| 115 |
|
|
=cut |
| 116 |
|
|
|
| 117 |
root |
1.40 |
# the following variables are defined in .xs and must not be re-created |
| 118 |
root |
1.39 |
our @CB_GLOBAL = (); # registry for all global events |
| 119 |
root |
1.40 |
our @CB_OBJECT = (); |
| 120 |
|
|
our @CB_PLAYER = (); |
| 121 |
root |
1.39 |
our @CB_TYPE = (); # registry for type (cf-object class) based events |
| 122 |
root |
1.40 |
our @CB_MAP = (); |
| 123 |
root |
1.39 |
|
| 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 |
root |
1.40 |
sub attach_to_type { |
| 192 |
root |
1.39 |
my $type = shift; |
| 193 |
root |
1.40 |
_attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
| 194 |
root |
1.39 |
} |
| 195 |
|
|
|
| 196 |
|
|
sub attach_to_objects { |
| 197 |
root |
1.40 |
_attach @CB_OBJECT, KLASS_OBJECT, @_ |
| 198 |
root |
1.39 |
} |
| 199 |
|
|
|
| 200 |
|
|
sub attach_to_players { |
| 201 |
root |
1.40 |
_attach @CB_PLAYER, KLASS_PLAYER, @_ |
| 202 |
root |
1.39 |
} |
| 203 |
|
|
|
| 204 |
|
|
sub attach_to_maps { |
| 205 |
root |
1.40 |
_attach @CB_MAP, KLASS_MAP, @_ |
| 206 |
root |
1.39 |
} |
| 207 |
|
|
|
| 208 |
|
|
our $override; |
| 209 |
|
|
|
| 210 |
|
|
sub override() { |
| 211 |
|
|
$override = 1 |
| 212 |
|
|
} |
| 213 |
|
|
|
| 214 |
|
|
sub invoke { |
| 215 |
|
|
my $event = shift; |
| 216 |
root |
1.40 |
my $callbacks = shift; |
| 217 |
root |
1.39 |
|
| 218 |
|
|
local $override; |
| 219 |
|
|
|
| 220 |
root |
1.40 |
for (@$callbacks) { |
| 221 |
root |
1.39 |
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 |
|
|
|
| 238 |
root |
1.1 |
sub inject_event { |
| 239 |
root |
1.14 |
my $extension = shift; |
| 240 |
|
|
my $event_code = shift; |
| 241 |
root |
1.1 |
|
| 242 |
root |
1.14 |
my $cb = $hook[$event_code]{$extension} |
| 243 |
root |
1.5 |
or return; |
| 244 |
|
|
|
| 245 |
root |
1.14 |
&$cb |
| 246 |
root |
1.5 |
} |
| 247 |
|
|
|
| 248 |
|
|
sub inject_global_event { |
| 249 |
root |
1.12 |
my $event = shift; |
| 250 |
root |
1.5 |
|
| 251 |
root |
1.12 |
my $cb = $hook[$event] |
| 252 |
root |
1.1 |
or return; |
| 253 |
|
|
|
| 254 |
root |
1.12 |
List::Util::max map &$_, values %$cb |
| 255 |
root |
1.1 |
} |
| 256 |
|
|
|
| 257 |
|
|
sub inject_command { |
| 258 |
|
|
my ($name, $obj, $params) = @_; |
| 259 |
|
|
|
| 260 |
|
|
for my $cmd (@{ $command{$name} }) { |
| 261 |
|
|
$cmd->[1]->($obj, $params); |
| 262 |
|
|
} |
| 263 |
|
|
|
| 264 |
|
|
-1 |
| 265 |
|
|
} |
| 266 |
|
|
|
| 267 |
|
|
sub register_command { |
| 268 |
|
|
my ($name, $time, $cb) = @_; |
| 269 |
|
|
|
| 270 |
|
|
my $caller = caller; |
| 271 |
root |
1.16 |
#warn "registering command '$name/$time' to '$caller'"; |
| 272 |
root |
1.4 |
|
| 273 |
root |
1.1 |
push @{ $command{$name} }, [$time, $cb, $caller]; |
| 274 |
|
|
$COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
| 275 |
|
|
} |
| 276 |
|
|
|
| 277 |
root |
1.16 |
sub register_extcmd { |
| 278 |
|
|
my ($name, $cb) = @_; |
| 279 |
|
|
|
| 280 |
|
|
my $caller = caller; |
| 281 |
|
|
#warn "registering extcmd '$name' to '$caller'"; |
| 282 |
|
|
|
| 283 |
|
|
$extcmd{$name} = [$cb, $caller]; |
| 284 |
|
|
} |
| 285 |
|
|
|
| 286 |
root |
1.6 |
sub register { |
| 287 |
|
|
my ($base, $pkg) = @_; |
| 288 |
|
|
|
| 289 |
root |
1.38 |
for my $idx (0 .. $#PLUGIN_EVENT) { |
| 290 |
|
|
if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) { |
| 291 |
|
|
#warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n"; |
| 292 |
root |
1.6 |
$hook[$idx]{$base} = $ref; |
| 293 |
|
|
} |
| 294 |
|
|
} |
| 295 |
|
|
} |
| 296 |
|
|
|
| 297 |
root |
1.1 |
sub load_extension { |
| 298 |
|
|
my ($path) = @_; |
| 299 |
|
|
|
| 300 |
|
|
$path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
| 301 |
root |
1.5 |
my $base = $1; |
| 302 |
root |
1.1 |
my $pkg = $1; |
| 303 |
|
|
$pkg =~ s/[^[:word:]]/_/g; |
| 304 |
root |
1.41 |
$pkg = "ext::$pkg"; |
| 305 |
root |
1.1 |
|
| 306 |
|
|
warn "loading '$path' into '$pkg'\n"; |
| 307 |
|
|
|
| 308 |
|
|
open my $fh, "<:utf8", $path |
| 309 |
|
|
or die "$path: $!"; |
| 310 |
|
|
|
| 311 |
|
|
my $source = |
| 312 |
|
|
"package $pkg; use strict; use utf8;\n" |
| 313 |
|
|
. "#line 1 \"$path\"\n{\n" |
| 314 |
|
|
. (do { local $/; <$fh> }) |
| 315 |
|
|
. "\n};\n1"; |
| 316 |
|
|
|
| 317 |
|
|
eval $source |
| 318 |
|
|
or die "$path: $@"; |
| 319 |
|
|
|
| 320 |
|
|
push @exts, $pkg; |
| 321 |
root |
1.5 |
$ext_pkg{$base} = $pkg; |
| 322 |
root |
1.1 |
|
| 323 |
root |
1.6 |
# no strict 'refs'; |
| 324 |
root |
1.23 |
# @{"$pkg\::ISA"} = ext::; |
| 325 |
root |
1.1 |
|
| 326 |
root |
1.6 |
register $base, $pkg; |
| 327 |
root |
1.1 |
} |
| 328 |
|
|
|
| 329 |
|
|
sub unload_extension { |
| 330 |
|
|
my ($pkg) = @_; |
| 331 |
|
|
|
| 332 |
|
|
warn "removing extension $pkg\n"; |
| 333 |
|
|
|
| 334 |
|
|
# remove hooks |
| 335 |
root |
1.38 |
for my $idx (0 .. $#PLUGIN_EVENT) { |
| 336 |
root |
1.1 |
delete $hook[$idx]{$pkg}; |
| 337 |
|
|
} |
| 338 |
|
|
|
| 339 |
|
|
# remove commands |
| 340 |
|
|
for my $name (keys %command) { |
| 341 |
|
|
my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
| 342 |
|
|
|
| 343 |
|
|
if (@cb) { |
| 344 |
|
|
$command{$name} = \@cb; |
| 345 |
|
|
$COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
| 346 |
|
|
} else { |
| 347 |
|
|
delete $command{$name}; |
| 348 |
|
|
delete $COMMAND{"$name\000"}; |
| 349 |
|
|
} |
| 350 |
|
|
} |
| 351 |
|
|
|
| 352 |
root |
1.15 |
# remove extcmds |
| 353 |
root |
1.16 |
for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
| 354 |
|
|
delete $extcmd{$name}; |
| 355 |
root |
1.15 |
} |
| 356 |
|
|
|
| 357 |
root |
1.43 |
if (my $cb = $pkg->can ("unload")) { |
| 358 |
elmex |
1.31 |
eval { |
| 359 |
|
|
$cb->($pkg); |
| 360 |
|
|
1 |
| 361 |
|
|
} or warn "$pkg unloaded, but with errors: $@"; |
| 362 |
|
|
} |
| 363 |
|
|
|
| 364 |
root |
1.1 |
Symbol::delete_package $pkg; |
| 365 |
|
|
} |
| 366 |
|
|
|
| 367 |
|
|
sub load_extensions { |
| 368 |
|
|
my $LIBDIR = maps_directory "perl"; |
| 369 |
|
|
|
| 370 |
|
|
for my $ext (<$LIBDIR/*.ext>) { |
| 371 |
root |
1.3 |
next unless -r $ext; |
| 372 |
root |
1.2 |
eval { |
| 373 |
|
|
load_extension $ext; |
| 374 |
|
|
1 |
| 375 |
|
|
} or warn "$ext not loaded: $@"; |
| 376 |
root |
1.1 |
} |
| 377 |
|
|
} |
| 378 |
|
|
|
| 379 |
root |
1.36 |
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 |
root |
1.1 |
|
| 401 |
root |
1.36 |
$k =~ s/\.pm$//; |
| 402 |
|
|
$k =~ s/\//::/g; |
| 403 |
root |
1.3 |
|
| 404 |
root |
1.36 |
if (my $cb = $k->can ("unload_module")) { |
| 405 |
|
|
$cb->(); |
| 406 |
root |
1.27 |
} |
| 407 |
|
|
|
| 408 |
root |
1.36 |
Symbol::delete_package $k; |
| 409 |
|
|
} |
| 410 |
root |
1.27 |
|
| 411 |
root |
1.41 |
# 4. get rid of safe::, as good as possible |
| 412 |
|
|
Symbol::delete_package "safe::$_" |
| 413 |
root |
1.36 |
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 |
root |
1.41 |
# don't, removes xs symbols, too, |
| 422 |
|
|
# and global variables created in xs |
| 423 |
root |
1.36 |
#Symbol::delete_package __PACKAGE__; |
| 424 |
|
|
|
| 425 |
|
|
# 7. reload cf.pm |
| 426 |
|
|
$msg->("reloading cf.pm"); |
| 427 |
|
|
require cf; |
| 428 |
|
|
}; |
| 429 |
|
|
$msg->($@) if $@; |
| 430 |
root |
1.27 |
|
| 431 |
root |
1.36 |
$msg->("reloaded"); |
| 432 |
|
|
}; |
| 433 |
root |
1.27 |
|
| 434 |
root |
1.36 |
sub perl_reload() { |
| 435 |
|
|
_perl_reload { |
| 436 |
|
|
warn $_[0]; |
| 437 |
|
|
print "$_[0]\n"; |
| 438 |
|
|
}; |
| 439 |
|
|
} |
| 440 |
root |
1.27 |
|
| 441 |
root |
1.36 |
register_command "perl-reload", 0, sub { |
| 442 |
|
|
my ($who, $arg) = @_; |
| 443 |
root |
1.27 |
|
| 444 |
root |
1.36 |
if ($who->flag (FLAG_WIZ)) { |
| 445 |
|
|
_perl_reload { |
| 446 |
|
|
warn $_[0]; |
| 447 |
|
|
$who->message ($_[0]); |
| 448 |
root |
1.4 |
}; |
| 449 |
root |
1.1 |
} |
| 450 |
|
|
}; |
| 451 |
|
|
|
| 452 |
root |
1.8 |
############################################################################# |
| 453 |
root |
1.28 |
# utility functions |
| 454 |
|
|
|
| 455 |
|
|
use JSON::Syck (); # TODO# replace by JSON::PC once working |
| 456 |
|
|
|
| 457 |
|
|
sub from_json($) { |
| 458 |
root |
1.29 |
$JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
| 459 |
root |
1.28 |
JSON::Syck::Load $_[0] |
| 460 |
|
|
} |
| 461 |
|
|
|
| 462 |
|
|
sub to_json($) { |
| 463 |
root |
1.29 |
$JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
| 464 |
root |
1.28 |
JSON::Syck::Dump $_[0] |
| 465 |
|
|
} |
| 466 |
|
|
|
| 467 |
|
|
############################################################################# |
| 468 |
|
|
# extcmd framework, basically convert ext <msg> |
| 469 |
root |
1.15 |
# into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
| 470 |
|
|
|
| 471 |
root |
1.43 |
attach_global |
| 472 |
|
|
on_extcmd => sub { |
| 473 |
|
|
my ($pl, $buf) = @_; |
| 474 |
|
|
|
| 475 |
|
|
my $msg = eval { from_json $buf }; |
| 476 |
|
|
|
| 477 |
|
|
if (ref $msg) { |
| 478 |
|
|
if (my $cb = $extcmd{$msg->{msgtype}}) { |
| 479 |
|
|
if (my %reply = $cb->[0]->($pl, $msg)) { |
| 480 |
|
|
$pl->ext_reply ($msg->{msgid}, %reply); |
| 481 |
|
|
} |
| 482 |
root |
1.28 |
} |
| 483 |
root |
1.43 |
} else { |
| 484 |
|
|
warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
| 485 |
root |
1.28 |
} |
| 486 |
root |
1.15 |
|
| 487 |
root |
1.43 |
cf::override; |
| 488 |
|
|
}, |
| 489 |
|
|
; |
| 490 |
root |
1.15 |
|
| 491 |
|
|
############################################################################# |
| 492 |
root |
1.8 |
# load/save/clean perl data associated with a map |
| 493 |
|
|
|
| 494 |
root |
1.39 |
*cf::mapsupport::on_clean = sub { |
| 495 |
root |
1.13 |
my ($map) = @_; |
| 496 |
root |
1.7 |
|
| 497 |
|
|
my $path = $map->tmpname; |
| 498 |
|
|
defined $path or return; |
| 499 |
|
|
|
| 500 |
|
|
unlink "$path.cfperl"; |
| 501 |
|
|
}; |
| 502 |
|
|
|
| 503 |
root |
1.39 |
*cf::mapsupport::on_swapin = |
| 504 |
|
|
*cf::mapsupport::on_load = sub { |
| 505 |
root |
1.13 |
my ($map) = @_; |
| 506 |
root |
1.6 |
|
| 507 |
|
|
my $path = $map->tmpname; |
| 508 |
|
|
$path = $map->path unless defined $path; |
| 509 |
|
|
|
| 510 |
|
|
open my $fh, "<:raw", "$path.cfperl" |
| 511 |
|
|
or return; # no perl data |
| 512 |
|
|
|
| 513 |
|
|
my $data = Storable::thaw do { local $/; <$fh> }; |
| 514 |
|
|
|
| 515 |
|
|
$data->{version} <= 1 |
| 516 |
|
|
or return; # too new |
| 517 |
|
|
|
| 518 |
|
|
$map->_set_obs ($data->{obs}); |
| 519 |
|
|
}; |
| 520 |
|
|
|
| 521 |
root |
1.39 |
*cf::mapsupport::on_swapout = sub { |
| 522 |
root |
1.13 |
my ($map) = @_; |
| 523 |
root |
1.6 |
|
| 524 |
|
|
my $path = $map->tmpname; |
| 525 |
|
|
$path = $map->path unless defined $path; |
| 526 |
|
|
|
| 527 |
|
|
my $obs = $map->_get_obs; |
| 528 |
|
|
|
| 529 |
|
|
if (defined $obs) { |
| 530 |
|
|
open my $fh, ">:raw", "$path.cfperl" |
| 531 |
|
|
or die "$path.cfperl: $!"; |
| 532 |
|
|
|
| 533 |
root |
1.8 |
stat $path; |
| 534 |
|
|
|
| 535 |
|
|
print $fh Storable::nfreeze { |
| 536 |
|
|
size => (stat _)[7], |
| 537 |
|
|
time => (stat _)[9], |
| 538 |
|
|
version => 1, |
| 539 |
|
|
obs => $obs, |
| 540 |
|
|
}; |
| 541 |
|
|
|
| 542 |
|
|
chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
| 543 |
|
|
} else { |
| 544 |
|
|
unlink "$path.cfperl"; |
| 545 |
|
|
} |
| 546 |
|
|
}; |
| 547 |
|
|
|
| 548 |
root |
1.39 |
attach_to_maps prio => -10000, package => cf::mapsupport::; |
| 549 |
|
|
|
| 550 |
root |
1.8 |
############################################################################# |
| 551 |
|
|
# load/save perl data associated with player->ob objects |
| 552 |
|
|
|
| 553 |
root |
1.33 |
sub all_objects(@) { |
| 554 |
|
|
@_, map all_objects ($_->inv), @_ |
| 555 |
|
|
} |
| 556 |
|
|
|
| 557 |
root |
1.39 |
attach_to_players |
| 558 |
|
|
on_load => sub { |
| 559 |
|
|
my ($pl, $path) = @_; |
| 560 |
|
|
|
| 561 |
|
|
for my $o (all_objects $pl->ob) { |
| 562 |
|
|
if (my $value = $o->get_ob_key_value ("_perl_data")) { |
| 563 |
|
|
$o->set_ob_key_value ("_perl_data"); |
| 564 |
root |
1.8 |
|
| 565 |
root |
1.39 |
%$o = %{ Storable::thaw pack "H*", $value }; |
| 566 |
|
|
} |
| 567 |
root |
1.11 |
} |
| 568 |
root |
1.39 |
}, |
| 569 |
|
|
on_save => sub { |
| 570 |
|
|
my ($pl, $path) = @_; |
| 571 |
root |
1.8 |
|
| 572 |
root |
1.39 |
$_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
| 573 |
|
|
for grep %$_, all_objects $pl->ob; |
| 574 |
|
|
}, |
| 575 |
|
|
; |
| 576 |
root |
1.6 |
|
| 577 |
root |
1.22 |
############################################################################# |
| 578 |
|
|
# core extensions - in perl |
| 579 |
|
|
|
| 580 |
root |
1.23 |
=item cf::player::exists $login |
| 581 |
|
|
|
| 582 |
|
|
Returns true when the given account exists. |
| 583 |
|
|
|
| 584 |
|
|
=cut |
| 585 |
|
|
|
| 586 |
|
|
sub cf::player::exists($) { |
| 587 |
|
|
cf::player::find $_[0] |
| 588 |
|
|
or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
| 589 |
|
|
} |
| 590 |
|
|
|
| 591 |
root |
1.28 |
=item $player->reply ($npc, $msg[, $flags]) |
| 592 |
|
|
|
| 593 |
|
|
Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
| 594 |
|
|
can be C<undef>. Does the right thing when the player is currently in a |
| 595 |
|
|
dialogue with the given NPC character. |
| 596 |
|
|
|
| 597 |
|
|
=cut |
| 598 |
|
|
|
| 599 |
root |
1.22 |
# rough implementation of a future "reply" method that works |
| 600 |
|
|
# with dialog boxes. |
| 601 |
root |
1.23 |
sub cf::object::player::reply($$$;$) { |
| 602 |
|
|
my ($self, $npc, $msg, $flags) = @_; |
| 603 |
|
|
|
| 604 |
|
|
$flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
| 605 |
root |
1.22 |
|
| 606 |
root |
1.24 |
if ($self->{record_replies}) { |
| 607 |
|
|
push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
| 608 |
|
|
} else { |
| 609 |
|
|
$msg = $npc->name . " says: $msg" if $npc; |
| 610 |
|
|
$self->message ($msg, $flags); |
| 611 |
|
|
} |
| 612 |
root |
1.22 |
} |
| 613 |
|
|
|
| 614 |
root |
1.28 |
=item $player->ext_reply ($msgid, $msgtype, %msg) |
| 615 |
|
|
|
| 616 |
|
|
Sends an ext reply to the player. |
| 617 |
|
|
|
| 618 |
|
|
=cut |
| 619 |
|
|
|
| 620 |
|
|
sub cf::player::ext_reply($$$%) { |
| 621 |
|
|
my ($self, $id, %msg) = @_; |
| 622 |
|
|
|
| 623 |
|
|
$msg{msgid} = $id; |
| 624 |
|
|
|
| 625 |
|
|
$self->send ("ext " . to_json \%msg); |
| 626 |
|
|
} |
| 627 |
|
|
|
| 628 |
root |
1.22 |
############################################################################# |
| 629 |
root |
1.23 |
# map scripting support |
| 630 |
|
|
|
| 631 |
root |
1.42 |
our $safe = new Safe "safe"; |
| 632 |
root |
1.23 |
our $safe_hole = new Safe::Hole; |
| 633 |
|
|
|
| 634 |
|
|
$SIG{FPE} = 'IGNORE'; |
| 635 |
|
|
|
| 636 |
|
|
$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
| 637 |
|
|
|
| 638 |
root |
1.25 |
# here we export the classes and methods available to script code |
| 639 |
|
|
|
| 640 |
|
|
for ( |
| 641 |
root |
1.27 |
["cf::object" => qw(contr pay_amount pay_player)], |
| 642 |
root |
1.25 |
["cf::object::player" => qw(player)], |
| 643 |
|
|
["cf::player" => qw(peaceful)], |
| 644 |
|
|
) { |
| 645 |
|
|
no strict 'refs'; |
| 646 |
|
|
my ($pkg, @funs) = @$_; |
| 647 |
root |
1.41 |
*{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
| 648 |
root |
1.25 |
for @funs; |
| 649 |
|
|
} |
| 650 |
root |
1.23 |
|
| 651 |
|
|
sub safe_eval($;@) { |
| 652 |
|
|
my ($code, %vars) = @_; |
| 653 |
|
|
|
| 654 |
|
|
my $qcode = $code; |
| 655 |
|
|
$qcode =~ s/"/‟/g; # not allowed in #line filenames |
| 656 |
|
|
$qcode =~ s/\n/\\n/g; |
| 657 |
|
|
|
| 658 |
|
|
local $_; |
| 659 |
root |
1.41 |
local @safe::cf::_safe_eval_args = values %vars; |
| 660 |
root |
1.23 |
|
| 661 |
root |
1.42 |
my $eval = |
| 662 |
root |
1.23 |
"do {\n" |
| 663 |
|
|
. "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
| 664 |
|
|
. "#line 0 \"{$qcode}\"\n" |
| 665 |
|
|
. $code |
| 666 |
|
|
. "\n}" |
| 667 |
root |
1.25 |
; |
| 668 |
|
|
|
| 669 |
|
|
sub_generation_inc; |
| 670 |
root |
1.42 |
my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
| 671 |
root |
1.25 |
sub_generation_inc; |
| 672 |
|
|
|
| 673 |
root |
1.42 |
if ($@) { |
| 674 |
|
|
warn "$@"; |
| 675 |
|
|
warn "while executing safe code '$code'\n"; |
| 676 |
|
|
warn "with arguments " . (join " ", %vars) . "\n"; |
| 677 |
|
|
} |
| 678 |
|
|
|
| 679 |
root |
1.25 |
wantarray ? @res : $res[0] |
| 680 |
root |
1.23 |
} |
| 681 |
|
|
|
| 682 |
|
|
sub register_script_function { |
| 683 |
|
|
my ($fun, $cb) = @_; |
| 684 |
|
|
|
| 685 |
|
|
no strict 'refs'; |
| 686 |
root |
1.41 |
*{"safe::$fun"} = $safe_hole->wrap ($cb); |
| 687 |
root |
1.23 |
} |
| 688 |
|
|
|
| 689 |
|
|
############################################################################# |
| 690 |
root |
1.34 |
# the server's main() |
| 691 |
|
|
|
| 692 |
root |
1.39 |
sub main { |
| 693 |
root |
1.34 |
Event::loop; |
| 694 |
|
|
} |
| 695 |
|
|
|
| 696 |
|
|
############################################################################# |
| 697 |
root |
1.22 |
# initialisation |
| 698 |
|
|
|
| 699 |
root |
1.6 |
register "<global>", __PACKAGE__; |
| 700 |
|
|
|
| 701 |
root |
1.27 |
unshift @INC, $LIBDIR; |
| 702 |
root |
1.17 |
|
| 703 |
root |
1.1 |
load_extensions; |
| 704 |
|
|
|
| 705 |
root |
1.35 |
$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 |
root |
1.37 |
# if we are delayed by four ticks, skip them all |
| 715 |
|
|
$NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
| 716 |
root |
1.35 |
|
| 717 |
|
|
$TICK_WATCHER->at ($NEXT_TICK); |
| 718 |
|
|
$TICK_WATCHER->start; |
| 719 |
|
|
}, |
| 720 |
|
|
); |
| 721 |
|
|
|
| 722 |
root |
1.1 |
1 |
| 723 |
|
|
|