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.10 by root, Thu Feb 15 21:07:49 2007 UTC vs.
Revision 1.25 by root, Tue Sep 4 05:43:22 2007 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 Crossfire TRT, the Roguelike Realtime MORPG.
7 * 3 *
8 * Copyright (C) 2006 by Marc Lehmann <crossfire@schmorp.de> 4 * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
9 * 5 *
10 * This program is free software; you can redistribute it and/or modify 6 * Crossfire TRT is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version. 9 * (at your 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 GNU General Public License
21 * along with this program; if not, write to the Free Software 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 *
19 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */ 20 */
24 21
25#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff 22#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
26#include "logger.h" 23#include "logger.h"
27#include "cfperl.h" 24#include "cfperl.h"
28#include "kw_hash.h" 25#include "kw_hash.h"
29 26
30object_freezer::object_freezer () 27object_freezer::object_freezer ()
31: dynbuf (128 * 1024, 64 * 1024) 28: dynbuf_text (128 * 1024, 64 * 1024)
32{ 29{
33 av = newAV (); 30 av = newAV ();
34} 31}
35 32
36object_freezer::~object_freezer () 33object_freezer::~object_freezer ()
61 CALL_ARG_SV (newSVpv (path, 0)); 58 CALL_ARG_SV (newSVpv (path, 0));
62 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 59 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
63 CALL_ARG_SV (newRV_inc ((SV *)av)); 60 CALL_ARG_SV (newRV_inc ((SV *)av));
64 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); 61 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
65 CALL_END; 62 CALL_END;
63
64 return 1;
66} 65}
67 66
68char * 67char *
69object_freezer::as_string () 68object_freezer::as_string ()
70{ 69{
80 CALL_END; 79 CALL_END;
81 80
82 return res; 81 return res;
83} 82}
84 83
85int 84#if 0
85void
86fprintf (object_freezer &freezer, const char *format, ...) 86fprintf (object_freezer &freezer, const char *format, ...)
87{ 87{
88 va_list ap; 88 va_list ap;
89 89
90 va_start (ap, format); 90 va_start (ap, format);
95 freezer.alloc (len); 95 freezer.alloc (len);
96 96
97 va_end (ap); 97 va_end (ap);
98} 98}
99 99
100int 100// XXX: function not returning an int
101void
101fputs (const char *s, object_freezer &freezer) 102fputs (const char *s, object_freezer &freezer)
102{ 103{
103 freezer.add (s); 104 freezer.add (s);
104} 105}
106#endif
105 107
106static const char thawer_eof[] = "\n\n\n\0\0\0"; 108static const char thawer_eof[] = "\n\n\n\0\0\0";
107 109
108object_thawer::object_thawer (const char *path) 110object_thawer::object_thawer (const char *path)
109: name (strdup (path)) 111: name (strdup (path))
143 SvCUR_set (text, len); 145 SvCUR_set (text, len);
144 memcpy (SvPVX (text), sv_, len); 146 memcpy (SvPVX (text), sv_, len);
145 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 147 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
146 148
147 line = SvPVX (text); 149 line = SvPVX (text);
150 next ();
148 } 151 }
149 } 152 }
150 153
151 CALL_END; 154 CALL_END;
152 } 155 }
153} 156}
154 157
155object_thawer::object_thawer (const char *data, AV *perlav) 158object_thawer::object_thawer (const char *data, AV *perlav)
156: name (strdup ("(memory stream")) 159: name (strdup ("(memory stream)"))
157{ 160{
158 av = perlav; 161 av = perlav;
159 text = newSVpv (data, 0); 162 text = newSVpv (data, 0);
160 sv_catpv (text, thawer_eof); 163 sv_catpv (text, thawer_eof);
161 line = SvPVbyte_nolen (text); 164 line = SvPVbyte_nolen (text);
165 next ();
162} 166}
163 167
164void 168void
165object_thawer::get (attachable *obj, int oid) 169object_thawer::get (attachable *obj, int oid)
166{ 170{
169 173
170 SV **svp = av_fetch ((AV *)av, oid, 0); 174 SV **svp = av_fetch ((AV *)av, oid, 0);
171 175
172 if (!svp || !SvROK (*svp)) 176 if (!svp || !SvROK (*svp))
173 { 177 {
174 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 178 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
175 return; 179 return;
176 } 180 }
177 181
178 if (!SvROK (*svp)) 182 if (!SvROK (*svp))
179 { 183 {
180 LOG (llevError, "deserialised perl object is not an RV"); 184 LOG (llevError, "deserialised perl object is not an RV\n");
181 return; 185 return;
182 } 186 }
183 187
184 HV *hv = (HV *)SvRV (*svp); 188 HV *hv = (HV *)SvRV (*svp);
185 189
186 if (SvTYPE (hv) != SVt_PVHV) 190 if (SvTYPE (hv) != SVt_PVHV)
187 { 191 {
188 LOG (llevError, "deserialised perl object is not a PVHV"); 192 LOG (llevError, "deserialised perl object is not a PVHV\n");
189 return; 193 return;
190 } 194 }
191 195
192 if (obj->self) 196 if (obj->self)
193 { 197 {
217object_thawer::~object_thawer () 221object_thawer::~object_thawer ()
218{ 222{
219 if (text) SvREFCNT_dec (text); 223 if (text) SvREFCNT_dec (text);
220 if (av) SvREFCNT_dec (av); 224 if (av) SvREFCNT_dec (av);
221 225
226 resolve_delayed_derefs (false);
227
222 free ((void *)name); 228 free ((void *)name);
223} 229}
224 230
225//TODO: remove 231//TODO: remove
226char * 232char *
248 254
249 *q = 0; 255 *q = 0;
250 thawer.line = p; 256 thawer.line = p;
251 257
252 return s == q ? 0 : s; 258 return s == q ? 0 : s;
259}
260
261void
262object_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);
253} 269}
254 270
255bool 271bool
256object_thawer::parse_error (const char *type, const char *name, bool skip) 272object_thawer::parse_error (const char *type, const char *name, bool skip)
257{ 273{
274 290
275 default: 291 default:
276 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", 292 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
277 this->name, linenum, 293 this->name, linenum,
278 kw_str ? kw_str : "<null>", 294 kw_str ? kw_str : "<null>",
279 value ? value : "<null>", 295 value ? value : "<null>",
280 type, name, 296 type, name,
281 skip ? "skipping line" : "aborting load"); 297 skip ? "skipping line" : "aborting load");
282 return skip; 298 return skip;
283 } 299 }
284} 300}
285 301
286void 302void
287object_thawer::next_kv () 303object_thawer::next ()
288{ 304{
289 if (!line) 305 if (!line)
290 { 306 {
291 kw = KW_ERROR; 307 kw = KW_ERROR;
292 return; 308 return;
293 } 309 }
294 310
295 for (;;) 311 for (;;)
296 { 312 {
297 char *p = line; 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 }
298 323
299 if (!*p) 324 if (!*p)
300 { 325 {
301 kw = KW_EOF; 326 kw = KW_EOF;
302 break; 327 break;
306 while (*p > ' ') 331 while (*p > ' ')
307 p++; 332 p++;
308 333
309 int klen = p - line; 334 int klen = p - line;
310 335
336 value_nn = "";
337 value = 0;
338
311 if (*p++ != '\n') 339 if (*p++ != '\n')
312 { 340 {
313 // parse value 341 // parse value
314 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 342 while (*(unsigned char *)p <= ' ' && *p != '\n')
315 ++p; 343 ++p;
316 344
317 value = p; 345 value_nn = value = p;
318 346
319 while (*p != '\n') 347 while (*p != '\n')
320 p++; 348 p++;
321 349
322 *p++ = 0; 350 *p++ = 0;
323 } 351 }
324 else
325 value = 0;
326 352
327 ++linenum; 353 ++linenum;
328 line [klen] = 0; 354 line [klen] = 0;
329 keyword_idx *kw_idx = kw_lex::match (line, klen); 355 keyword_idx *kw_idx = kw_lex::match (line, klen);
330 356
331 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D
332
333 kw_str = line; 357 kw_str = line;
334 line = p; 358 line = p;
335 359
336 if (kw_idx) 360 if (kw_idx)
337 { 361 {
347 } 371 }
348 } 372 }
349} 373}
350 374
351void 375void
352object_thawer::skip_kv () 376object_thawer::skip ()
353{ 377{
354 shstr ml; 378 shstr ml;
355 379
356 switch (kw) 380 switch (kw)
357 { 381 {
358 case KW_msg: get_ml (KW_endmsg , ml); break; 382 case KW_msg: get_ml (KW_endmsg , ml); break;
359 case KW_lore: get_ml (KW_endlore , ml); break; 383 case KW_lore: get_ml (KW_endlore , ml); break;
360 case KW_maplore: get_ml (KW_endmaplore, ml); break; 384 case KW_maplore: get_ml (KW_endmaplore, ml); break;
385 default: break;
361 } 386 }
362 387
363 next_kv (); 388 next ();
364} 389}
365 390
366void 391void
367object_thawer::get (shstr &sh) const 392object_thawer::skip_block ()
368{ 393{
369 if (value) 394 while (kw != KW_ERROR && kw != KW_EOF)
370 sh = value;
371 else
372 { 395 {
373 sh = "<value missing>"; 396 keyword w = kw;
374 LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename 397 skip ();
398 if (w == KW_end)
399 break;
375 } 400 }
376} 401}
377 402
378void 403void
379object_thawer::get_ml (keyword kend, shstr &sh) 404object_thawer::get_ml (keyword kend, shstr &sh)
427} 452}
428 453
429sint32 454sint32
430object_thawer::get_sint32 () const 455object_thawer::get_sint32 () const
431{ 456{
432 char *p = value; 457 char *p = value_nn;
433
434 if (!p)
435 return 0;
436 458
437 sint32 val = 0; 459 sint32 val = 0;
438 bool negate; 460 bool negate;
439 461
440 if (*p == '-') 462 if (*p == '-')
453 while (*p); 475 while (*p);
454 476
455 return negate ? -val : val; 477 return negate ? -val : val;
456} 478}
457 479
458sint64 480void
459object_thawer::get_sint64 () const 481object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
460{ 482{
461 return value ? atoll (value) : 0; 483 op->refcnt_inc ();
484 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
485 delrefs.push_back (r);
486 ptr = 0;
462} 487}
463 488
464double 489void
465object_thawer::get_double () const 490object_thawer::resolve_delayed_derefs (bool deref)
466{ 491{
467 return value ? atof (value) : 0; 492 while (!delrefs.empty ())
468} 493 {
494 delayed_ref r = delrefs.back ();
495 delrefs.pop_back ();
469 496
497 if (deref)
498 *r.ptr = object::deref (r.ref);
499
500 free ((void *)r.ref);
501 r.op->refcnt_dec ();
502 }
503}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines