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.34 by root, Sun Nov 8 00:01:34 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))
113{ 113{
114 static const char eof[] = "\n\n\n\0\0\0";
115
116 av = 0; 114 av = 0;
117 text = 0; 115 text = 0;
118 line = 0; 116 line = 0;
119 linenum = 0; 117 linenum = 0;
120 118
140 // first value - text part, pad with 3 zeroes 138 // first value - text part, pad with 3 zeroes
141 { 139 {
142 SV *sv = POPs; 140 SV *sv = POPs;
143 STRLEN len; 141 STRLEN len;
144 char *sv_ = SvPVbyte (sv, len); 142 char *sv_ = SvPVbyte (sv, len);
145 text = newSV (len + sizeof (eof)); 143 text = newSV (len + sizeof (thawer_eof));
146 SvCUR_set (text, len); 144 SvCUR_set (text, len + sizeof (thawer_eof));
147 memcpy (SvPVX (text), sv_, len); 145 memcpy (SvPVX (text), sv_, len);
148 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 146 memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof));
149 147
150 line = SvPVX (text); 148 line = SvPVX (text);
149 next ();
151 } 150 }
152 } 151 }
153 152
154 CALL_END; 153 CALL_END;
155 } 154 }
156} 155}
157 156
158object_thawer::object_thawer (const char *data, AV *perlav) 157object_thawer::object_thawer (const char *data, AV *perlav)
159: name (strdup ("(memory stream")) 158: name (strdup ("(memory stream)"))
160{ 159{
161 av = perlav; 160 av = perlav;
162 text = newSVpv (data, 0); 161 text = newSVpv (data, 0);
163 sv_catpv (text, thawer_eof); 162 sv_catpv (text, thawer_eof);
164 line = SvPVbyte_nolen (text); 163 line = SvPVbyte_nolen (text);
164 next ();
165} 165}
166 166
167void 167void
168object_thawer::get (attachable *obj, int oid) 168object_thawer::get (attachable *obj, int oid)
169{ 169{
172 172
173 SV **svp = av_fetch ((AV *)av, oid, 0); 173 SV **svp = av_fetch ((AV *)av, oid, 0);
174 174
175 if (!svp || !SvROK (*svp)) 175 if (!svp || !SvROK (*svp))
176 { 176 {
177 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 177 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
178 return; 178 return;
179 } 179 }
180 180
181 if (!SvROK (*svp)) 181 if (!SvROK (*svp))
182 { 182 {
183 LOG (llevError, "deserialised perl object is not an RV"); 183 LOG (llevError, "deserialised perl object is not an RV\n");
184 return; 184 return;
185 } 185 }
186 186
187 HV *hv = (HV *)SvRV (*svp); 187 HV *hv = (HV *)SvRV (*svp);
188 188
189 if (SvTYPE (hv) != SVt_PVHV) 189 if (SvTYPE (hv) != SVt_PVHV)
190 { 190 {
191 LOG (llevError, "deserialised perl object is not a PVHV"); 191 LOG (llevError, "deserialised perl object is not a PVHV\n");
192 return; 192 return;
193 } 193 }
194 194
195 if (obj->self) 195 if (obj->self)
196 { 196 {
220object_thawer::~object_thawer () 220object_thawer::~object_thawer ()
221{ 221{
222 if (text) SvREFCNT_dec (text); 222 if (text) SvREFCNT_dec (text);
223 if (av) SvREFCNT_dec (av); 223 if (av) SvREFCNT_dec (av);
224 224
225 resolve_delayed_derefs (false);
226
225 free ((void *)name); 227 free ((void *)name);
226} 228}
227 229
228//TODO: remove 230//TODO: remove
229char * 231char *
251 253
252 *q = 0; 254 *q = 0;
253 thawer.line = p; 255 thawer.line = p;
254 256
255 return s == q ? 0 : s; 257 return s == q ? 0 : s;
258}
259
260void
261object_thawer::parse_warn (const char *msg)
262{
263 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
264 this->name, linenum,
265 kw_str ? kw_str : "<null>",
266 value ? value : "<null>",
267 msg);
256} 268}
257 269
258bool 270bool
259object_thawer::parse_error (const char *type, const char *name, bool skip) 271object_thawer::parse_error (const char *type, const char *name, bool skip)
260{ 272{
277 289
278 default: 290 default:
279 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", 291 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
280 this->name, linenum, 292 this->name, linenum,
281 kw_str ? kw_str : "<null>", 293 kw_str ? kw_str : "<null>",
282 value ? value : "<null>", 294 value ? value : "<null>",
283 type, name, 295 type, name,
284 skip ? "skipping line" : "aborting load"); 296 skip ? "skipping line" : "aborting load");
285 return skip; 297 return skip;
286 } 298 }
287} 299}
297 309
298 for (;;) 310 for (;;)
299 { 311 {
300 char *p = line; 312 char *p = line;
301 313
314 if (*p <= ' ')
315 {
316 // skip whitespace (only some files need this)
317 while (*p == ' ' || *p == '\t')
318 p++;
319
320 line = p;
321 }
322
302 if (!*p) 323 if (!*p)
303 { 324 {
304 kw = KW_EOF; 325 kw = KW_EOF;
305 break; 326 break;
306 } 327 }
309 while (*p > ' ') 330 while (*p > ' ')
310 p++; 331 p++;
311 332
312 int klen = p - line; 333 int klen = p - line;
313 334
335 value_nn = "";
336 value = 0;
337
314 if (*p++ != '\n') 338 if (*p++ != '\n')
315 { 339 {
316 // parse value 340 // parse value
317 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 341 while (*(unsigned char *)p <= ' ' && *p != '\n')
318 ++p; 342 ++p;
319 343
320 value = p; 344 value_nn = value = p;
321 345
322 while (*p != '\n') 346 while (*p != '\n')
323 p++; 347 p++;
324 348
325 *p++ = 0; 349 *p++ = 0;
326 } 350 }
327 else
328 value = 0;
329 351
330 ++linenum; 352 ++linenum;
331 line [klen] = 0; 353 line [klen] = 0;
332 keyword_idx *kw_idx = kw_lex::match (line, klen); 354 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 355
336 kw_str = line; 356 kw_str = line;
337 line = p; 357 line = p;
338 358
339 if (kw_idx) 359 if (kw_idx)
366 386
367 next (); 387 next ();
368} 388}
369 389
370void 390void
371object_thawer::get (shstr &sh) const 391object_thawer::skip_block ()
372{ 392{
373 if (value) 393 // must not stop at KW_ERROR, as those signify custom keys
374 sh = value; 394 while (kw != KW_EOF)
375 else
376 { 395 {
377 sh = "<value missing>"; 396 keyword w = kw;
378 LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename 397 skip ();
398
399 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
400 skip_block ();
401 else if (w == KW_end)
402 break;
379 } 403 }
380} 404}
381 405
382void 406void
383object_thawer::get_ml (keyword kend, shstr &sh) 407object_thawer::get_ml (keyword kend, shstr &sh)
431} 455}
432 456
433sint32 457sint32
434object_thawer::get_sint32 () const 458object_thawer::get_sint32 () const
435{ 459{
436 char *p = value; 460 const char *p = value_nn;
437
438 if (!p)
439 return 0;
440 461
441 sint32 val = 0; 462 sint32 val = 0;
442 bool negate; 463 bool negate;
443 464
444 if (*p == '-') 465 if (*p == '-')
457 while (*p); 478 while (*p);
458 479
459 return negate ? -val : val; 480 return negate ? -val : val;
460} 481}
461 482
462sint64 483void
463object_thawer::get_sint64 () const 484object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
464{ 485{
465 return value ? atoll (value) : 0; 486 op->refcnt_inc ();
487 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
488 delrefs.push_back (r);
489 ptr = 0;
466} 490}
467 491
468double 492void
469object_thawer::get_double () const 493object_thawer::resolve_delayed_derefs (bool deref)
470{ 494{
471 return value ? atof (value) : 0; 495 while (!delrefs.empty ())
472} 496 {
497 delayed_ref r = delrefs.back ();
498 delrefs.pop_back ();
473 499
500 if (deref)
501 *r.ptr = object::deref (r.ref);
502
503 free ((void *)r.ref);
504 r.op->refcnt_dec ();
505 }
506}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines