ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/freezethaw.C
(Generate patch)

Comparing deliantra/server/server/freezethaw.C (file contents):
Revision 1.14 by pippijn, Fri Mar 2 10:15:40 2007 UTC vs.
Revision 1.33 by root, Mon Oct 12 14:00:59 2009 UTC

1/*****************************************************************************/
2/* CrossFire, A roguelike realtime multiplayer game */
3/*****************************************************************************/
4
5/* 1/*
6 * This code is placed under the GNU General Public Licence (GPL) 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
7 * 3 *
8 * Copyright (C) 2006 by Marc Lehmann <crossfire@schmorp.de> 4 * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
9 * 5 *
10 * This program is free software; you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
11 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
12 * the Free Software Foundation; either version 2 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
13 * (at your option) any later version. 9 * option) any later version.
14 * 10 *
15 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details. 14 * GNU General Public License for more details.
19 * 15 *
20 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
21 * along with this program; if not, write to the Free Software 17 * and the GNU General Public License along with this program. If not, see
22 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 * <http://www.gnu.org/licenses/>.
19 *
20 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 21 */
24 22
25#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff 23#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
26#include "logger.h" 24#include "logger.h"
27#include "cfperl.h" 25#include "cfperl.h"
28#include "kw_hash.h" 26#include "kw_hash.h"
29 27
30object_freezer::object_freezer () 28object_freezer::object_freezer ()
31: dynbuf (128 * 1024, 64 * 1024) 29: dynbuf_text (128 * 1024, 64 * 1024)
32{ 30{
33 av = newAV (); 31 av = newAV ();
34} 32}
35 33
36object_freezer::~object_freezer () 34object_freezer::~object_freezer ()
82 CALL_END; 80 CALL_END;
83 81
84 return res; 82 return res;
85} 83}
86 84
85#if 0
87void 86void
88fprintf (object_freezer &freezer, const char *format, ...) 87fprintf (object_freezer &freezer, const char *format, ...)
89{ 88{
90 va_list ap; 89 va_list ap;
91 90
103void 102void
104fputs (const char *s, object_freezer &freezer) 103fputs (const char *s, object_freezer &freezer)
105{ 104{
106 freezer.add (s); 105 freezer.add (s);
107} 106}
107#endif
108 108
109static const char thawer_eof[] = "\n\n\n\0\0\0"; 109static const char thawer_eof[] = "\n\n\n\0\0\0";
110 110
111object_thawer::object_thawer (const char *path) 111object_thawer::object_thawer (const char *path)
112: name (strdup (path)) 112: name (strdup (path))
141 { 141 {
142 SV *sv = POPs; 142 SV *sv = POPs;
143 STRLEN len; 143 STRLEN len;
144 char *sv_ = SvPVbyte (sv, len); 144 char *sv_ = SvPVbyte (sv, len);
145 text = newSV (len + sizeof (eof)); 145 text = newSV (len + sizeof (eof));
146 SvCUR_set (text, len); 146 SvCUR_set (text, len + sizeof (eof));
147 memcpy (SvPVX (text), sv_, len); 147 memcpy (SvPVX (text), sv_, len);
148 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 148 memcpy (SvPVX (text) + len, eof, sizeof (eof));
149 149
150 line = SvPVX (text); 150 line = SvPVX (text);
151 next ();
151 } 152 }
152 } 153 }
153 154
154 CALL_END; 155 CALL_END;
155 } 156 }
156} 157}
157 158
158object_thawer::object_thawer (const char *data, AV *perlav) 159object_thawer::object_thawer (const char *data, AV *perlav)
159: name (strdup ("(memory stream")) 160: name (strdup ("(memory stream)"))
160{ 161{
161 av = perlav; 162 av = perlav;
162 text = newSVpv (data, 0); 163 text = newSVpv (data, 0);
163 sv_catpv (text, thawer_eof); 164 sv_catpv (text, thawer_eof);
164 line = SvPVbyte_nolen (text); 165 line = SvPVbyte_nolen (text);
166 next ();
165} 167}
166 168
167void 169void
168object_thawer::get (attachable *obj, int oid) 170object_thawer::get (attachable *obj, int oid)
169{ 171{
172 174
173 SV **svp = av_fetch ((AV *)av, oid, 0); 175 SV **svp = av_fetch ((AV *)av, oid, 0);
174 176
175 if (!svp || !SvROK (*svp)) 177 if (!svp || !SvROK (*svp))
176 { 178 {
177 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 179 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
178 return; 180 return;
179 } 181 }
180 182
181 if (!SvROK (*svp)) 183 if (!SvROK (*svp))
182 { 184 {
183 LOG (llevError, "deserialised perl object is not an RV"); 185 LOG (llevError, "deserialised perl object is not an RV\n");
184 return; 186 return;
185 } 187 }
186 188
187 HV *hv = (HV *)SvRV (*svp); 189 HV *hv = (HV *)SvRV (*svp);
188 190
189 if (SvTYPE (hv) != SVt_PVHV) 191 if (SvTYPE (hv) != SVt_PVHV)
190 { 192 {
191 LOG (llevError, "deserialised perl object is not a PVHV"); 193 LOG (llevError, "deserialised perl object is not a PVHV\n");
192 return; 194 return;
193 } 195 }
194 196
195 if (obj->self) 197 if (obj->self)
196 { 198 {
220object_thawer::~object_thawer () 222object_thawer::~object_thawer ()
221{ 223{
222 if (text) SvREFCNT_dec (text); 224 if (text) SvREFCNT_dec (text);
223 if (av) SvREFCNT_dec (av); 225 if (av) SvREFCNT_dec (av);
224 226
227 resolve_delayed_derefs (false);
228
225 free ((void *)name); 229 free ((void *)name);
226} 230}
227 231
228//TODO: remove 232//TODO: remove
229char * 233char *
251 255
252 *q = 0; 256 *q = 0;
253 thawer.line = p; 257 thawer.line = p;
254 258
255 return s == q ? 0 : s; 259 return s == q ? 0 : s;
260}
261
262void
263object_thawer::parse_warn (const char *msg)
264{
265 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
266 this->name, linenum,
267 kw_str ? kw_str : "<null>",
268 value ? value : "<null>",
269 msg);
256} 270}
257 271
258bool 272bool
259object_thawer::parse_error (const char *type, const char *name, bool skip) 273object_thawer::parse_error (const char *type, const char *name, bool skip)
260{ 274{
277 291
278 default: 292 default:
279 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", 293 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
280 this->name, linenum, 294 this->name, linenum,
281 kw_str ? kw_str : "<null>", 295 kw_str ? kw_str : "<null>",
282 value ? value : "<null>", 296 value ? value : "<null>",
283 type, name, 297 type, name,
284 skip ? "skipping line" : "aborting load"); 298 skip ? "skipping line" : "aborting load");
285 return skip; 299 return skip;
286 } 300 }
287} 301}
297 311
298 for (;;) 312 for (;;)
299 { 313 {
300 char *p = line; 314 char *p = line;
301 315
316 if (*p <= ' ')
317 {
318 // skip whitespace (only some files need this)
319 while (*p == ' ' || *p == '\t')
320 p++;
321
322 line = p;
323 }
324
302 if (!*p) 325 if (!*p)
303 { 326 {
304 kw = KW_EOF; 327 kw = KW_EOF;
305 break; 328 break;
306 } 329 }
309 while (*p > ' ') 332 while (*p > ' ')
310 p++; 333 p++;
311 334
312 int klen = p - line; 335 int klen = p - line;
313 336
337 value_nn = "";
338 value = 0;
339
314 if (*p++ != '\n') 340 if (*p++ != '\n')
315 { 341 {
316 // parse value 342 // parse value
317 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 343 while (*(unsigned char *)p <= ' ' && *p != '\n')
318 ++p; 344 ++p;
319 345
320 value = p; 346 value_nn = value = p;
321 347
322 while (*p != '\n') 348 while (*p != '\n')
323 p++; 349 p++;
324 350
325 *p++ = 0; 351 *p++ = 0;
326 } 352 }
327 else
328 value = 0;
329 353
330 ++linenum; 354 ++linenum;
331 line [klen] = 0; 355 line [klen] = 0;
332 keyword_idx *kw_idx = kw_lex::match (line, klen); 356 keyword_idx *kw_idx = kw_lex::match (line, klen);
333
334 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D
335 357
336 kw_str = line; 358 kw_str = line;
337 line = p; 359 line = p;
338 360
339 if (kw_idx) 361 if (kw_idx)
366 388
367 next (); 389 next ();
368} 390}
369 391
370void 392void
371object_thawer::get (shstr &sh) const 393object_thawer::skip_block ()
372{ 394{
373 if (value) 395 // must not stop at KW_ERROR, as those signify custom keys
374 sh = value; 396 while (kw != KW_EOF)
375 else
376 { 397 {
377 sh = "<value missing>"; 398 keyword w = kw;
378 LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename 399 skip ();
400
401 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
402 skip_block ();
403 else if (w == KW_end)
404 break;
379 } 405 }
380} 406}
381 407
382void 408void
383object_thawer::get_ml (keyword kend, shstr &sh) 409object_thawer::get_ml (keyword kend, shstr &sh)
431} 457}
432 458
433sint32 459sint32
434object_thawer::get_sint32 () const 460object_thawer::get_sint32 () const
435{ 461{
436 char *p = value; 462 const char *p = value_nn;
437
438 if (!p)
439 return 0;
440 463
441 sint32 val = 0; 464 sint32 val = 0;
442 bool negate; 465 bool negate;
443 466
444 if (*p == '-') 467 if (*p == '-')
457 while (*p); 480 while (*p);
458 481
459 return negate ? -val : val; 482 return negate ? -val : val;
460} 483}
461 484
462sint64 485void
463object_thawer::get_sint64 () const 486object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
464{ 487{
465 return value ? atoll (value) : 0; 488 op->refcnt_inc ();
489 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
490 delrefs.push_back (r);
491 ptr = 0;
466} 492}
467 493
468double 494void
469object_thawer::get_double () const 495object_thawer::resolve_delayed_derefs (bool deref)
470{ 496{
471 return value ? atof (value) : 0; 497 while (!delrefs.empty ())
472} 498 {
499 delayed_ref r = delrefs.back ();
500 delrefs.pop_back ();
473 501
502 if (deref)
503 *r.ptr = object::deref (r.ref);
504
505 free ((void *)r.ref);
506 r.op->refcnt_dec ();
507 }
508}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines