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.37 by root, Thu Apr 15 04:18:09 2010 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,2008,2009,2010 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 ()
37{ 35{
38 SvREFCNT_dec (av); 36 SvREFCNT_dec (av);
39} 37}
40 38
41void 39void
42object_freezer::put (attachable *ext) 40object_freezer::put_ (attachable *ext)
43{ 41{
44 ext->optimise (); 42 ext->optimise ();
45 43
46 if (ext->self) 44 if (ext->self)
47 { 45 {
48 int idx = AvFILLp ((AV *)av) + 1; 46 int idx = AvFILLp ((AV *)av) + 1;
49 av_store (av, idx, newRV_inc ((SV *)ext->self)); 47 av_store (av, idx, newRV_inc ((SV *)ext->self));
50 48
51 add ((void *)"oid ", 4); 49 put (KW(oid), sint32(idx));
52 add ((sint32)idx);
53 add ('\n');
54 } 50 }
55} 51}
56 52
57bool 53bool
58object_freezer::save (const char *path) 54object_freezer::save (const char *path)
82 CALL_END; 78 CALL_END;
83 79
84 return res; 80 return res;
85} 81}
86 82
83#if 0
87void 84void
88fprintf (object_freezer &freezer, const char *format, ...) 85fprintf (object_freezer &freezer, const char *format, ...)
89{ 86{
90 va_list ap; 87 va_list ap;
91 88
103void 100void
104fputs (const char *s, object_freezer &freezer) 101fputs (const char *s, object_freezer &freezer)
105{ 102{
106 freezer.add (s); 103 freezer.add (s);
107} 104}
105#endif
108 106
109static const char thawer_eof[] = "\n\n\n\0\0\0"; 107static const char thawer_eof[] = "\n\n\n\0\0\0";
110 108
111object_thawer::object_thawer (const char *path) 109object_thawer::object_thawer (const char *path)
112: name (strdup (path)) 110: name (strdup (path))
113{ 111{
114 static const char eof[] = "\n\n\n\0\0\0";
115
116 av = 0; 112 av = 0;
117 text = 0; 113 text = 0;
118 line = 0; 114 line = 0;
119 linenum = 0; 115 linenum = 0;
120 116
140 // first value - text part, pad with 3 zeroes 136 // first value - text part, pad with 3 zeroes
141 { 137 {
142 SV *sv = POPs; 138 SV *sv = POPs;
143 STRLEN len; 139 STRLEN len;
144 char *sv_ = SvPVbyte (sv, len); 140 char *sv_ = SvPVbyte (sv, len);
145 text = newSV (len + sizeof (eof)); 141 text = newSV (len + sizeof (thawer_eof));
146 SvCUR_set (text, len); 142 SvCUR_set (text, len + sizeof (thawer_eof));
147 memcpy (SvPVX (text), sv_, len); 143 memcpy (SvPVX (text), sv_, len);
148 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 144 memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof));
149 145
150 line = SvPVX (text); 146 line = SvPVX (text);
147 next ();
151 } 148 }
152 } 149 }
153 150
154 CALL_END; 151 CALL_END;
155 } 152 }
156} 153}
157 154
158object_thawer::object_thawer (const char *data, AV *perlav) 155object_thawer::object_thawer (const char *data, AV *perlav)
159: name (strdup ("(memory stream")) 156: name (strdup ("(memory stream)"))
160{ 157{
161 av = perlav; 158 av = perlav;
162 text = newSVpv (data, 0); 159 text = newSVpv (data, 0);
163 sv_catpv (text, thawer_eof); 160 sv_catpv (text, thawer_eof);
164 line = SvPVbyte_nolen (text); 161 line = SvPVbyte_nolen (text);
162 next ();
165} 163}
166 164
167void 165void
168object_thawer::get (attachable *obj, int oid) 166object_thawer::get (attachable *obj, int oid)
169{ 167{
172 170
173 SV **svp = av_fetch ((AV *)av, oid, 0); 171 SV **svp = av_fetch ((AV *)av, oid, 0);
174 172
175 if (!svp || !SvROK (*svp)) 173 if (!svp || !SvROK (*svp))
176 { 174 {
177 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 175 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
178 return; 176 return;
179 } 177 }
180 178
181 if (!SvROK (*svp)) 179 if (!SvROK (*svp))
182 { 180 {
183 LOG (llevError, "deserialised perl object is not an RV"); 181 LOG (llevError, "deserialised perl object is not an RV\n");
184 return; 182 return;
185 } 183 }
186 184
187 HV *hv = (HV *)SvRV (*svp); 185 HV *hv = (HV *)SvRV (*svp);
188 186
189 if (SvTYPE (hv) != SVt_PVHV) 187 if (SvTYPE (hv) != SVt_PVHV)
190 { 188 {
191 LOG (llevError, "deserialised perl object is not a PVHV"); 189 LOG (llevError, "deserialised perl object is not a PVHV\n");
192 return; 190 return;
193 } 191 }
194 192
195 if (obj->self) 193 if (obj->self)
196 { 194 {
220object_thawer::~object_thawer () 218object_thawer::~object_thawer ()
221{ 219{
222 if (text) SvREFCNT_dec (text); 220 if (text) SvREFCNT_dec (text);
223 if (av) SvREFCNT_dec (av); 221 if (av) SvREFCNT_dec (av);
224 222
223 resolve_delayed_derefs (false);
224
225 free ((void *)name); 225 free ((void *)name);
226} 226}
227 227
228//TODO: remove 228void
229char * 229object_thawer::parse_warn (const char *msg)
230fgets (char *s, int n, object_thawer &thawer)
231{ 230{
232 char *p = thawer.line; 231 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
233 char *q = s; 232 this->name, linenum,
234 233 kw_str ? kw_str : "<null>",
235 if (!p) 234 value ? value : "<null>",
236 return 0; 235 msg);
237
238 while (--n)
239 {
240 if (!*p)
241 break;
242
243 *q++ = *p;
244
245 if (*p++ == '\n')
246 {
247 ++thawer.linenum;
248 break;
249 }
250 }
251
252 *q = 0;
253 thawer.line = p;
254
255 return s == q ? 0 : s;
256} 236}
257 237
258bool 238bool
259object_thawer::parse_error (const char *type, const char *name, bool skip) 239object_thawer::parse_error (const char *type, const char *name, bool skip)
260{ 240{
277 257
278 default: 258 default:
279 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", 259 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
280 this->name, linenum, 260 this->name, linenum,
281 kw_str ? kw_str : "<null>", 261 kw_str ? kw_str : "<null>",
282 value ? value : "<null>", 262 value ? value : "<null>",
283 type, name, 263 type, name,
284 skip ? "skipping line" : "aborting load"); 264 skip ? "skipping line" : "aborting load");
285 return skip; 265 return skip;
286 } 266 }
287} 267}
297 277
298 for (;;) 278 for (;;)
299 { 279 {
300 char *p = line; 280 char *p = line;
301 281
282 if (expect_false (*p <= ' '))
283 {
284 // skip whitespace (only some files need this)
285 while (*p == ' ' || *p == '\t')
286 p++;
287
288 line = p;
289 }
290
302 if (!*p) 291 if (!*p)
303 { 292 {
304 kw = KW_EOF; 293 kw = KW_EOF;
305 break; 294 break;
306 } 295 }
309 while (*p > ' ') 298 while (*p > ' ')
310 p++; 299 p++;
311 300
312 int klen = p - line; 301 int klen = p - line;
313 302
303 value_nn = "";
304 value = 0;
305
314 if (*p++ != '\n') 306 if (*p++ != '\n')
315 { 307 {
316 // parse value 308 // parse value
317 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 309 while (*(unsigned char *)p <= ' ' && *p != '\n')
318 ++p; 310 ++p;
319 311
320 value = p; 312 value_nn = value = p;
321 313
322 while (*p != '\n') 314 while (*p != '\n')
323 p++; 315 p++;
324 316
325 *p++ = 0; 317 *p++ = 0;
326 } 318 }
327 else
328 value = 0;
329 319
330 ++linenum; 320 ++linenum;
331 line [klen] = 0; 321 line [klen] = 0;
332 keyword_idx *kw_idx = kw_lex::match (line, klen); 322 keyword_idx *kw_idx = kw_lex::match (line, klen);
333 323
334 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D
335
336 kw_str = line; 324 kw_str = line;
337 line = p; 325 line = p;
338 326
339 if (kw_idx) 327 if (kw_idx)
340 { 328 {
346 else 334 else
347 { 335 {
348 kw = KW_ERROR; 336 kw = KW_ERROR;
349 break; 337 break;
350 } 338 }
339 }
340}
341
342void
343object_thawer::next_line ()
344{
345 if (!line)
346 {
347 kw = KW_ERROR;
348 return;
349 }
350
351 for (;;)
352 {
353 char *p = line;
354
355 if (expect_false (*p <= ' '))
356 {
357 // skip whitespace (only some files need this)
358 while (*p == ' ' || *p == '\t')
359 p++;
360
361 line = p;
362 }
363
364 if (!*p)
365 {
366 kw = KW_EOF;
367 break;
368 }
369
370 kw = KW_value;
371 kw_str = p;
372 value_nn = p;
373 value = p;
374
375 // parse keyword
376 while (*p > '\n')
377 p++;
378
379 if (*p == '\n')
380 *p++ = 0;
381
382 ++linenum;
383
384 line = p;
385
386 if (*kw_str && *kw_str != '#')
387 break;
388
389 // empty/comment line -> skip it
351 } 390 }
352} 391}
353 392
354void 393void
355object_thawer::skip () 394object_thawer::skip ()
366 405
367 next (); 406 next ();
368} 407}
369 408
370void 409void
371object_thawer::get (shstr &sh) const 410object_thawer::skip_block ()
372{ 411{
373 if (value) 412 // must not stop at KW_ERROR, as those signify custom keys
374 sh = value; 413 while (kw != KW_EOF)
375 else
376 { 414 {
377 sh = "<value missing>"; 415 keyword w = kw;
378 LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename 416 skip ();
417
418 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
419 skip_block ();
420 else if (w == KW_end)
421 break;
379 } 422 }
380} 423}
381 424
382void 425void
383object_thawer::get_ml (keyword kend, shstr &sh) 426object_thawer::get_ml (keyword kend, shstr &sh)
431} 474}
432 475
433sint32 476sint32
434object_thawer::get_sint32 () const 477object_thawer::get_sint32 () const
435{ 478{
436 char *p = value; 479 const char *p = value_nn;
437
438 if (!p)
439 return 0;
440 480
441 sint32 val = 0; 481 sint32 val = 0;
442 bool negate; 482 bool negate;
443 483
444 if (*p == '-') 484 if (*p == '-')
457 while (*p); 497 while (*p);
458 498
459 return negate ? -val : val; 499 return negate ? -val : val;
460} 500}
461 501
462sint64 502void
463object_thawer::get_sint64 () const 503object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
464{ 504{
465 return value ? atoll (value) : 0; 505 op->refcnt_inc ();
506 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
507 delrefs.push_back (r);
508 ptr = 0;
466} 509}
467 510
468double 511void
469object_thawer::get_double () const 512object_thawer::resolve_delayed_derefs (bool deref)
470{ 513{
471 return value ? atof (value) : 0; 514 while (!delrefs.empty ())
472} 515 {
516 delayed_ref r = delrefs.back ();
517 delrefs.pop_back ();
473 518
519 if (deref)
520 *r.ptr = object::deref (r.ref);
521
522 free ((void *)r.ref);
523 r.op->refcnt_dec ();
524 }
525}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines