/*****************************************************************************/ /* CrossFire, A roguelike realtime multiplayer game */ /*****************************************************************************/ /* * This code is placed under the GNU General Public Licence (GPL) * * Copyright (C) 2006 by Marc Lehmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 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 GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #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 (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)); add ((void *)"oid ", 4); add ((sint32)idx); add ('\n'); } } 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; } 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; } int 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); } int fputs (const char *s, object_freezer &freezer) { freezer.add (s); } static const char thawer_eof[] = "\n\n\n\0\0\0"; object_thawer::object_thawer (const char *path) : name (strdup (path)) { static const char eof[] = "\n\n\n\0\0\0"; av = 0; text = 0; line = 0; if (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 (eof)); SvCUR_set (text, len); memcpy (SvPVX (text), sv_, len); memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure line = SvPVX (text); } } CALL_END; } } object_thawer::object_thawer (const char *data, AV *perlav) : name (strdup ("(memory stream")) { av = perlav; text = newSVpv (data, 0); sv_catpv (text, thawer_eof); line = SvPVbyte_nolen (text); } 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)) { printf ("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"); return; } HV *hv = (HV *)SvRV (*svp); if (SvTYPE (hv) != SVt_PVHV) { LOG (llevError, "deserialised perl object is not a PVHV"); 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); // borrow a refcount for the perl object obj->flags |= attachable::F_BORROWED; obj->refcnt_dec (); } obj->reattach (); } object_thawer::~object_thawer () { if (text) SvREFCNT_dec (text); if (av) SvREFCNT_dec (av); free ((void *)name); } char * fgets (char *s, int n, object_thawer &thawer) { char *p = thawer.line; char *q = s; if (!p) return 0; while (--n) { if (!*p) break; *q++ = *p; if (*p++ == '\n') break; } *q = 0; thawer.line = p; return s == q ? 0 : s; } keyword object_thawer::get_kv () { if (!line) return KW_EOF; for (;;) { char *p = line; if (!*p) return KW_EOF; // parse keyword while (*p > ' ') p++; int klen = p - line; if (*p++ != '\n') { // parse value while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 ++p; last_value = p; while (*p != '\n') p++; *p++ = 0; } else last_value = 0; line [klen] = 0; keyword_idx *kw = kw_lex::match (line, klen); //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D last_keyword = line; line = p; if (kw) return kw->index; else if (!*last_keyword || *last_keyword == '#') ; // empty/comment line else return KW_ERROR; } } void object_thawer::skip_kv (keyword kw) { 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; } } void object_thawer::get (shstr &sh) const { if (last_value) sh = last_value; else { sh = ""; LOG (llevError, "keyword \"%s\" requires value, substituting with \n", last_keyword);//TODO: add filename } } 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; // 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; line = end + keyword_len [kend] + 1; while (*line++ != '\n') ; } } sint32 object_thawer::get_sint32 () const { char *p = last_value; if (!p) return 0; 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; } sint64 object_thawer::get_sint64 () const { return last_value ? atoll (last_value) : 0; } double object_thawer::get_double () const { return last_value ? atof (last_value) : 0; }