/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the Affero GNU General Public License * and the GNU General Public License along with this program. If not, see * . * * The authors can be reached via e-mail to */ #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff #include "logger.h" #include "cfperl.h" #include "kw_hash.h" object_freezer::object_freezer () : dynbuf_text (128 * 1024, 64 * 1024) { av = newAV (); } object_freezer::~object_freezer () { SvREFCNT_dec (av); } void object_freezer::put_ (attachable *ext) { ext->optimise (); if (ext->self) { int idx = AvFILLp ((AV *)av) + 1; av_store (av, idx, newRV_inc ((SV *)ext->self)); put (KW(oid), sint32(idx)); } } bool object_freezer::save (const char *path) { CALL_BEGIN (3); CALL_ARG_SV (newSVpv (path, 0)); CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); CALL_ARG_SV (newRV_inc ((SV *)av)); CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); CALL_END; return 1; } char * object_freezer::as_string () { CALL_BEGIN (2); CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); CALL_ARG_SV (newRV_inc ((SV *)av)); CALL_CALL ("cf::object_freezer_as_string", G_SCALAR); char *res = count > 0 ? strdup (SvPVX (POPs)) : strdup ("[fatal error]"); CALL_END; return res; } #if 0 void fprintf (object_freezer &freezer, const char *format, ...) { va_list ap; va_start (ap, format); int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap); if (len >= 0) freezer.alloc (len); va_end (ap); } // XXX: function not returning an int void fputs (const char *s, object_freezer &freezer) { freezer.add (s); } #endif static const char thawer_eof[] = "\n\n\n\0\0\0"; bool object_thawer::errors_are_fatal = true; object_thawer::object_thawer (const char *data, AV *perlav) : name (strdup ("(memory stream)")) { init ("(memory stream)"); av = perlav; text = newSVpv (data, 0); sv_catpv (text, thawer_eof); line = SvPVbyte_nolen (text); next (); } object_thawer::object_thawer (const_utf8_string path) { init_from_file (path); } // convenience constructor object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file) { init_from_file (format ("%s/%s", dir, file)); } void object_thawer::init (const_utf8_string path) { name = strdup (path); av = 0; text = 0; line = 0; linenum = 0; kw = KW_ERROR; kw_str = 0; value = 0; } void object_thawer::init_from_file (const_utf8_string path) { init (path); CALL_BEGIN (1); CALL_ARG_SV (newSVpv (path, 0)); CALL_CALL ("cf::object_thawer_load", G_ARRAY); if (count == 2) { // second value - perl objects { SV *sv = POPs; if (SvROK (sv)) av = (AV *)SvREFCNT_inc (SvRV (sv)); } // first value - text part, pad with 3 zeroes { SV *sv = POPs; STRLEN len; char *sv_ = SvPVbyte (sv, len); text = newSV (len + sizeof (thawer_eof)); SvCUR_set (text, len + sizeof (thawer_eof)); memcpy (SvPVX (text), sv_, len); memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); line = SvPVX (text); next (); } } CALL_END; } void object_thawer::get (attachable *obj, int oid) { if (!av || oid < 0) // this is actually an error of sorts return; SV **svp = av_fetch ((AV *)av, oid, 0); if (!svp || !SvROK (*svp)) { LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); return; } if (!SvROK (*svp)) { LOG (llevError, "deserialised perl object is not an RV\n"); return; } HV *hv = (HV *)SvRV (*svp); if (SvTYPE (hv) != SVt_PVHV) { LOG (llevError, "deserialised perl object is not a PVHV\n"); return; } if (obj->self) { // the hard way(?) CALL_BEGIN (2); CALL_ARG_SV (newRV_inc ((SV *)obj->self)); CALL_ARG_SV (newRV_inc ((SV *)hv)); PUTBACK; call_method ("thawer_merge", G_DISCARD | G_EVAL); SPAGAIN; CALL_END; } else { // the easy way(?) obj->self = hv; SvRV_set (*svp, &PL_sv_undef); sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); } obj->reattach (); } object_thawer::~object_thawer () { if (text) SvREFCNT_dec (text); if (av) SvREFCNT_dec (av); resolve_delayed_derefs (false); free ((void *)name); } static void error_out () { if (object_thawer::errors_are_fatal) { LOG (llevError, "(parse errors at this time are fatal, exiting)"); exit (1); } } void object_thawer::parse_warn (const char *msg) const { LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", this->name, linenum, kw_str ? kw_str : "", value ? value : "", msg); } bool object_thawer::parse_error (const char *type, const char *name, bool skip) const { if (!type) type = "file section"; if (!name) name = "generic"; switch (kw) { case KW_EOF: LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", this->name, linenum, type, name); error_out (); return false; case KW_ERROR: LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", this->name, linenum, type, name, kw_str ? kw_str : ""); error_out (); return false; default: LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", this->name, linenum, kw_str ? kw_str : "", value ? value : "", type, name, skip ? "skipping line" : "aborting load"); error_out (); return skip; } } void object_thawer::next () { if (!line) { kw = KW_ERROR; return; } for (;;) { char *p = line; if (expect_false (*p <= ' ')) { // skip whitespace (only some files need this) while (*p == ' ' || *p == '\t') p++; line = p; } if (!*p) { kw = KW_EOF; break; } // parse keyword while (*p > ' ') p++; int klen = p - line; value_nn = ""; value = 0; if (*p++ != '\n') { // parse value while (*(unsigned char *)p <= ' ' && *p != '\n') ++p; value_nn = value = p; while (*p != '\n') p++; *p++ = 0; } ++linenum; line [klen] = 0; keyword_idx *kw_idx = kw_lex::match (line, klen); kw_str = line; line = p; if (kw_idx) { kw = kw_idx->index; break; } else if (!*kw_str || *kw_str == '#') ; // empty/comment line else { kw = KW_ERROR; break; } } } bool object_thawer::next_line () { if (!line) { kw = KW_ERROR; return 0; } for (;;) { char *p = line; if (expect_false (*p <= ' ')) { // skip whitespace (only some files need this) while (*p == ' ' || *p == '\t') p++; line = p; } if (!*p) { kw = KW_EOF; return 0; } kw = KW_value; kw_str = p; value_nn = p; value = p; // parse till newline while (*p > '\n') p++; if (*p == '\n') *p++ = 0; ++linenum; line = p; if (*kw_str && *kw_str != '#') break; // empty/comment line -> skip it } return 1; } void object_thawer::skip () { shstr ml; switch (kw) { case KW_msg: get_ml (KW_endmsg , ml); break; case KW_lore: get_ml (KW_endlore , ml); break; case KW_maplore: get_ml (KW_endmaplore, ml); break; default: break; } next (); } void object_thawer::skip_block () { // must not stop at KW_ERROR, as those signify custom keys while (kw != KW_EOF) { keyword w = kw; skip (); if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) skip_block (); else if (w == KW_end) break; } } void object_thawer::get_ml (keyword kend, shstr &sh) { char kw[128]; int klen = keyword_len [kend]; kw [0] = '\n'; memcpy (kw + 1, keyword_str [kend], klen); kw [klen + 1] = '\n'; kw [klen + 2] = 0; ++linenum; // first test for completely empty msg... "endXXX\n" if (!strncmp (line, kw + 1, klen + 1)) { sh = 0; line += klen + 1; return; } else { // multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL) char *end = strstr (line, kw); if (!end) { sh = 0; return; } *end = 0; sh = line; // count line numbers while (line < end) linenum += *line++ == '\n'; line += keyword_len [kend]; while (*line++ != '\n') ; ++linenum; } } sint32 object_thawer::get_sint32 () const { const char *p = value_nn; sint32 val = 0; bool negate; if (*p == '-') { negate = true; ++p; } else negate = false; do { val *= 10; val += *p++ - '0'; } while (*p); return negate ? -val : val; } void object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) { op->refcnt_inc (); delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; delrefs.push_back (r); ptr = 0; } void object_thawer::resolve_delayed_derefs (bool deref) { while (!delrefs.empty ()) { delayed_ref r = delrefs.back (); delrefs.pop_back (); if (deref) *r.ptr = object::deref (r.ref); free ((void *)r.ref); r.op->refcnt_dec (); } }