| 1 |
/* |
| 2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
* |
| 4 |
* Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
| 5 |
* |
| 6 |
* Deliantra is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* The authors can be reached via e-mail to <support@deliantra.net> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
| 23 |
#include "logger.h" |
| 24 |
#include "cfperl.h" |
| 25 |
#include "kw_hash.h" |
| 26 |
|
| 27 |
object_freezer::object_freezer () |
| 28 |
: dynbuf_text (128 * 1024, 64 * 1024) |
| 29 |
{ |
| 30 |
av = newAV (); |
| 31 |
} |
| 32 |
|
| 33 |
object_freezer::~object_freezer () |
| 34 |
{ |
| 35 |
SvREFCNT_dec (av); |
| 36 |
} |
| 37 |
|
| 38 |
void |
| 39 |
object_freezer::put (attachable *ext) |
| 40 |
{ |
| 41 |
ext->optimise (); |
| 42 |
|
| 43 |
if (ext->self) |
| 44 |
{ |
| 45 |
int idx = AvFILLp ((AV *)av) + 1; |
| 46 |
av_store (av, idx, newRV_inc ((SV *)ext->self)); |
| 47 |
|
| 48 |
add ((void *)"oid ", 4); |
| 49 |
add ((sint32)idx); |
| 50 |
add ('\n'); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
bool |
| 55 |
object_freezer::save (const char *path) |
| 56 |
{ |
| 57 |
CALL_BEGIN (3); |
| 58 |
CALL_ARG_SV (newSVpv (path, 0)); |
| 59 |
CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
| 60 |
CALL_ARG_SV (newRV_inc ((SV *)av)); |
| 61 |
CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
| 62 |
CALL_END; |
| 63 |
|
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
char * |
| 68 |
object_freezer::as_string () |
| 69 |
{ |
| 70 |
CALL_BEGIN (2); |
| 71 |
CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
| 72 |
CALL_ARG_SV (newRV_inc ((SV *)av)); |
| 73 |
CALL_CALL ("cf::object_freezer_as_string", G_SCALAR); |
| 74 |
|
| 75 |
char *res = count > 0 |
| 76 |
? strdup (SvPVX (POPs)) |
| 77 |
: strdup ("[fatal error]"); |
| 78 |
|
| 79 |
CALL_END; |
| 80 |
|
| 81 |
return res; |
| 82 |
} |
| 83 |
|
| 84 |
#if 0 |
| 85 |
void |
| 86 |
fprintf (object_freezer &freezer, const char *format, ...) |
| 87 |
{ |
| 88 |
va_list ap; |
| 89 |
|
| 90 |
va_start (ap, format); |
| 91 |
|
| 92 |
int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap); |
| 93 |
|
| 94 |
if (len >= 0) |
| 95 |
freezer.alloc (len); |
| 96 |
|
| 97 |
va_end (ap); |
| 98 |
} |
| 99 |
|
| 100 |
// XXX: function not returning an int |
| 101 |
void |
| 102 |
fputs (const char *s, object_freezer &freezer) |
| 103 |
{ |
| 104 |
freezer.add (s); |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
static const char thawer_eof[] = "\n\n\n\0\0\0"; |
| 109 |
|
| 110 |
object_thawer::object_thawer (const char *path) |
| 111 |
: name (strdup (path)) |
| 112 |
{ |
| 113 |
static const char eof[] = "\n\n\n\0\0\0"; |
| 114 |
|
| 115 |
av = 0; |
| 116 |
text = 0; |
| 117 |
line = 0; |
| 118 |
linenum = 0; |
| 119 |
|
| 120 |
kw = KW_ERROR; |
| 121 |
kw_str = 0; |
| 122 |
value = 0; |
| 123 |
|
| 124 |
if (path) |
| 125 |
{ |
| 126 |
CALL_BEGIN (1); |
| 127 |
CALL_ARG_SV (newSVpv (path, 0)); |
| 128 |
CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
| 129 |
|
| 130 |
if (count == 2) |
| 131 |
{ |
| 132 |
// second value - perl objects |
| 133 |
{ |
| 134 |
SV *sv = POPs; |
| 135 |
if (SvROK (sv)) |
| 136 |
av = (AV *)SvREFCNT_inc (SvRV (sv)); |
| 137 |
} |
| 138 |
|
| 139 |
// first value - text part, pad with 3 zeroes |
| 140 |
{ |
| 141 |
SV *sv = POPs; |
| 142 |
STRLEN len; |
| 143 |
char *sv_ = SvPVbyte (sv, len); |
| 144 |
text = newSV (len + sizeof (eof)); |
| 145 |
SvCUR_set (text, len + sizeof (eof)); |
| 146 |
memcpy (SvPVX (text), sv_, len); |
| 147 |
memcpy (SvPVX (text) + len, eof, sizeof (eof)); |
| 148 |
|
| 149 |
line = SvPVX (text); |
| 150 |
next (); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
CALL_END; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
object_thawer::object_thawer (const char *data, AV *perlav) |
| 159 |
: name (strdup ("(memory stream)")) |
| 160 |
{ |
| 161 |
av = perlav; |
| 162 |
text = newSVpv (data, 0); |
| 163 |
sv_catpv (text, thawer_eof); |
| 164 |
line = SvPVbyte_nolen (text); |
| 165 |
next (); |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
object_thawer::get (attachable *obj, int oid) |
| 170 |
{ |
| 171 |
if (!av || oid < 0) // this is actually an error of sorts |
| 172 |
return; |
| 173 |
|
| 174 |
SV **svp = av_fetch ((AV *)av, oid, 0); |
| 175 |
|
| 176 |
if (!svp || !SvROK (*svp)) |
| 177 |
{ |
| 178 |
LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
if (!SvROK (*svp)) |
| 183 |
{ |
| 184 |
LOG (llevError, "deserialised perl object is not an RV\n"); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
HV *hv = (HV *)SvRV (*svp); |
| 189 |
|
| 190 |
if (SvTYPE (hv) != SVt_PVHV) |
| 191 |
{ |
| 192 |
LOG (llevError, "deserialised perl object is not a PVHV\n"); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
if (obj->self) |
| 197 |
{ |
| 198 |
// the hard way(?) |
| 199 |
|
| 200 |
CALL_BEGIN (2); |
| 201 |
CALL_ARG_SV (newRV_inc ((SV *)obj->self)); |
| 202 |
CALL_ARG_SV (newRV_inc ((SV *)hv)); |
| 203 |
PUTBACK; |
| 204 |
call_method ("thawer_merge", G_DISCARD | G_EVAL); |
| 205 |
SPAGAIN; |
| 206 |
CALL_END; |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
// the easy way(?) |
| 211 |
|
| 212 |
obj->self = hv; |
| 213 |
SvRV_set (*svp, &PL_sv_undef); |
| 214 |
|
| 215 |
sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
| 216 |
} |
| 217 |
|
| 218 |
obj->reattach (); |
| 219 |
} |
| 220 |
|
| 221 |
object_thawer::~object_thawer () |
| 222 |
{ |
| 223 |
if (text) SvREFCNT_dec (text); |
| 224 |
if (av) SvREFCNT_dec (av); |
| 225 |
|
| 226 |
resolve_delayed_derefs (false); |
| 227 |
|
| 228 |
free ((void *)name); |
| 229 |
} |
| 230 |
|
| 231 |
//TODO: remove |
| 232 |
char * |
| 233 |
fgets (char *s, int n, object_thawer &thawer) |
| 234 |
{ |
| 235 |
char *p = thawer.line; |
| 236 |
char *q = s; |
| 237 |
|
| 238 |
if (!p) |
| 239 |
return 0; |
| 240 |
|
| 241 |
while (--n) |
| 242 |
{ |
| 243 |
if (!*p) |
| 244 |
break; |
| 245 |
|
| 246 |
*q++ = *p; |
| 247 |
|
| 248 |
if (*p++ == '\n') |
| 249 |
{ |
| 250 |
++thawer.linenum; |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
*q = 0; |
| 256 |
thawer.line = p; |
| 257 |
|
| 258 |
return s == q ? 0 : s; |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
object_thawer::parse_warn (const char *msg) |
| 263 |
{ |
| 264 |
LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
| 265 |
this->name, linenum, |
| 266 |
kw_str ? kw_str : "<null>", |
| 267 |
value ? value : "<null>", |
| 268 |
msg); |
| 269 |
} |
| 270 |
|
| 271 |
bool |
| 272 |
object_thawer::parse_error (const char *type, const char *name, bool skip) |
| 273 |
{ |
| 274 |
if (!type) type = "file section"; |
| 275 |
if (!name) name = "generic"; |
| 276 |
|
| 277 |
switch (kw) |
| 278 |
{ |
| 279 |
case KW_EOF: |
| 280 |
LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
| 281 |
this->name, linenum, type, name); |
| 282 |
return false; |
| 283 |
|
| 284 |
case KW_ERROR: |
| 285 |
LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
| 286 |
this->name, linenum, |
| 287 |
type, name, |
| 288 |
kw_str ? kw_str : "<file load>"); |
| 289 |
return false; |
| 290 |
|
| 291 |
default: |
| 292 |
LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
| 293 |
this->name, linenum, |
| 294 |
kw_str ? kw_str : "<null>", |
| 295 |
value ? value : "<null>", |
| 296 |
type, name, |
| 297 |
skip ? "skipping line" : "aborting load"); |
| 298 |
return skip; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
void |
| 303 |
object_thawer::next () |
| 304 |
{ |
| 305 |
if (!line) |
| 306 |
{ |
| 307 |
kw = KW_ERROR; |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
for (;;) |
| 312 |
{ |
| 313 |
char *p = line; |
| 314 |
|
| 315 |
if (*p <= ' ') |
| 316 |
{ |
| 317 |
// skip whitespace (only some files need this) |
| 318 |
while (*p == ' ' || *p == '\t') |
| 319 |
p++; |
| 320 |
|
| 321 |
line = p; |
| 322 |
} |
| 323 |
|
| 324 |
if (!*p) |
| 325 |
{ |
| 326 |
kw = KW_EOF; |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
// parse keyword |
| 331 |
while (*p > ' ') |
| 332 |
p++; |
| 333 |
|
| 334 |
int klen = p - line; |
| 335 |
|
| 336 |
value_nn = ""; |
| 337 |
value = 0; |
| 338 |
|
| 339 |
if (*p++ != '\n') |
| 340 |
{ |
| 341 |
// parse value |
| 342 |
while (*(unsigned char *)p <= ' ' && *p != '\n') |
| 343 |
++p; |
| 344 |
|
| 345 |
value_nn = value = p; |
| 346 |
|
| 347 |
while (*p != '\n') |
| 348 |
p++; |
| 349 |
|
| 350 |
*p++ = 0; |
| 351 |
} |
| 352 |
|
| 353 |
++linenum; |
| 354 |
line [klen] = 0; |
| 355 |
keyword_idx *kw_idx = kw_lex::match (line, klen); |
| 356 |
|
| 357 |
kw_str = line; |
| 358 |
line = p; |
| 359 |
|
| 360 |
if (kw_idx) |
| 361 |
{ |
| 362 |
kw = kw_idx->index; |
| 363 |
break; |
| 364 |
} |
| 365 |
else if (!*kw_str || *kw_str == '#') |
| 366 |
; // empty/comment line |
| 367 |
else |
| 368 |
{ |
| 369 |
kw = KW_ERROR; |
| 370 |
break; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
void |
| 376 |
object_thawer::skip () |
| 377 |
{ |
| 378 |
shstr ml; |
| 379 |
|
| 380 |
switch (kw) |
| 381 |
{ |
| 382 |
case KW_msg: get_ml (KW_endmsg , ml); break; |
| 383 |
case KW_lore: get_ml (KW_endlore , ml); break; |
| 384 |
case KW_maplore: get_ml (KW_endmaplore, ml); break; |
| 385 |
default: break; |
| 386 |
} |
| 387 |
|
| 388 |
next (); |
| 389 |
} |
| 390 |
|
| 391 |
void |
| 392 |
object_thawer::skip_block () |
| 393 |
{ |
| 394 |
// must not stop at KW_ERROR, as those signify custom keys |
| 395 |
while (kw != KW_EOF) |
| 396 |
{ |
| 397 |
keyword w = kw; |
| 398 |
skip (); |
| 399 |
|
| 400 |
if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
| 401 |
skip_block (); |
| 402 |
else if (w == KW_end) |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void |
| 408 |
object_thawer::get_ml (keyword kend, shstr &sh) |
| 409 |
{ |
| 410 |
char kw[128]; |
| 411 |
|
| 412 |
int klen = keyword_len [kend]; |
| 413 |
|
| 414 |
kw [0] = '\n'; |
| 415 |
memcpy (kw + 1, keyword_str [kend], klen); |
| 416 |
kw [klen + 1] = '\n'; |
| 417 |
kw [klen + 2] = 0; |
| 418 |
|
| 419 |
++linenum; |
| 420 |
|
| 421 |
// first test for completely empty msg... "endXXX\n" |
| 422 |
if (!strncmp (line, kw + 1, klen + 1)) |
| 423 |
{ |
| 424 |
sh = 0; |
| 425 |
|
| 426 |
line += klen + 1; |
| 427 |
|
| 428 |
return; |
| 429 |
} |
| 430 |
else |
| 431 |
{ |
| 432 |
// multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL) |
| 433 |
|
| 434 |
char *end = strstr (line, kw); |
| 435 |
|
| 436 |
if (!end) |
| 437 |
{ |
| 438 |
sh = 0; |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
*end = 0; |
| 443 |
sh = line; |
| 444 |
|
| 445 |
// count line numbers |
| 446 |
while (line < end) |
| 447 |
linenum += *line++ == '\n'; |
| 448 |
|
| 449 |
line += keyword_len [kend]; |
| 450 |
|
| 451 |
while (*line++ != '\n') |
| 452 |
; |
| 453 |
|
| 454 |
++linenum; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
sint32 |
| 459 |
object_thawer::get_sint32 () const |
| 460 |
{ |
| 461 |
const char *p = value_nn; |
| 462 |
|
| 463 |
sint32 val = 0; |
| 464 |
bool negate; |
| 465 |
|
| 466 |
if (*p == '-') |
| 467 |
{ |
| 468 |
negate = true; |
| 469 |
++p; |
| 470 |
} |
| 471 |
else |
| 472 |
negate = false; |
| 473 |
|
| 474 |
do |
| 475 |
{ |
| 476 |
val *= 10; |
| 477 |
val += *p++ - '0'; |
| 478 |
} |
| 479 |
while (*p); |
| 480 |
|
| 481 |
return negate ? -val : val; |
| 482 |
} |
| 483 |
|
| 484 |
void |
| 485 |
object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
| 486 |
{ |
| 487 |
op->refcnt_inc (); |
| 488 |
delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
| 489 |
delrefs.push_back (r); |
| 490 |
ptr = 0; |
| 491 |
} |
| 492 |
|
| 493 |
void |
| 494 |
object_thawer::resolve_delayed_derefs (bool deref) |
| 495 |
{ |
| 496 |
while (!delrefs.empty ()) |
| 497 |
{ |
| 498 |
delayed_ref r = delrefs.back (); |
| 499 |
delrefs.pop_back (); |
| 500 |
|
| 501 |
if (deref) |
| 502 |
*r.ptr = object::deref (r.ref); |
| 503 |
|
| 504 |
free ((void *)r.ref); |
| 505 |
r.op->refcnt_dec (); |
| 506 |
} |
| 507 |
} |