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.7 by root, Tue Jan 23 03:56:45 2007 UTC vs.
Revision 1.42 by root, Thu Apr 29 12:24:04 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)
61 CALL_ARG_SV (newSVpv (path, 0)); 57 CALL_ARG_SV (newSVpv (path, 0));
62 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 58 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
63 CALL_ARG_SV (newRV_inc ((SV *)av)); 59 CALL_ARG_SV (newRV_inc ((SV *)av));
64 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); 60 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
65 CALL_END; 61 CALL_END;
62
63 return 1;
66} 64}
67 65
68char * 66char *
69object_freezer::as_string () 67object_freezer::as_string ()
70{ 68{
80 CALL_END; 78 CALL_END;
81 79
82 return res; 80 return res;
83} 81}
84 82
85int 83#if 0
84void
86fprintf (object_freezer &freezer, const char *format, ...) 85fprintf (object_freezer &freezer, const char *format, ...)
87{ 86{
88 va_list ap; 87 va_list ap;
89 88
90 va_start (ap, format); 89 va_start (ap, format);
95 freezer.alloc (len); 94 freezer.alloc (len);
96 95
97 va_end (ap); 96 va_end (ap);
98} 97}
99 98
100int 99// XXX: function not returning an int
100void
101fputs (const char *s, object_freezer &freezer) 101fputs (const char *s, object_freezer &freezer)
102{ 102{
103 freezer.add (s); 103 freezer.add (s);
104} 104}
105#endif
105 106
106static const char thawer_eof[] = "\n\n\n\0\0\0"; 107static const char thawer_eof[] = "\n\n\n\0\0\0";
107 108
109bool object_thawer::errors_are_fatal = true;
110
108object_thawer::object_thawer (const char *path) 111object_thawer::object_thawer (const char *data, AV *perlav)
112: name (strdup ("(memory stream)"))
113{
114 av = perlav;
115 text = newSVpv (data, 0);
116 sv_catpv (text, thawer_eof);
117 line = SvPVbyte_nolen (text);
118 next ();
119}
120
121object_thawer::object_thawer (const_utf8_string path)
122{
123 new_from_path (path);
124}
125
126// convenience constructor
127object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file)
128{
129 new_from_path (format ("%s/%s", dir, file));
130}
131
132void
133object_thawer::new_from_path (const_utf8_string path)
134{
109: name (strdup (path)) 135 name = strdup (path);
110{
111 static const char eof[] = "\n\n\n\0\0\0";
112
113 av = 0; 136 av = 0;
114 text = 0; 137 text = 0;
115 line = 0; 138 line = 0;
139 linenum = 0;
140
141 kw = KW_ERROR;
142 kw_str = 0;
143 value = 0;
116 144
117 if (path) 145 if (path)
118 { 146 {
119 CALL_BEGIN (1); 147 CALL_BEGIN (1);
120 CALL_ARG_SV (newSVpv (path, 0)); 148 CALL_ARG_SV (newSVpv (path, 0));
132 // first value - text part, pad with 3 zeroes 160 // first value - text part, pad with 3 zeroes
133 { 161 {
134 SV *sv = POPs; 162 SV *sv = POPs;
135 STRLEN len; 163 STRLEN len;
136 char *sv_ = SvPVbyte (sv, len); 164 char *sv_ = SvPVbyte (sv, len);
137 text = newSV (len + sizeof (eof)); 165 text = newSV (len + sizeof (thawer_eof));
138 SvCUR_set (text, len); 166 SvCUR_set (text, len + sizeof (thawer_eof));
139 memcpy (SvPVX (text), sv_, len); 167 memcpy (SvPVX (text), sv_, len);
140 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 168 memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof));
141 169
142 line = SvPVX (text); 170 line = SvPVX (text);
171 next ();
143 } 172 }
144 } 173 }
145 174
146 CALL_END; 175 CALL_END;
147 } 176 }
148}
149
150object_thawer::object_thawer (const char *data, AV *perlav)
151: name (strdup ("(memory stream"))
152{
153 av = perlav;
154 text = newSVpv (data, 0);
155 sv_catpv (text, thawer_eof);
156 line = SvPVbyte_nolen (text);
157} 177}
158 178
159void 179void
160object_thawer::get (attachable *obj, int oid) 180object_thawer::get (attachable *obj, int oid)
161{ 181{
164 184
165 SV **svp = av_fetch ((AV *)av, oid, 0); 185 SV **svp = av_fetch ((AV *)av, oid, 0);
166 186
167 if (!svp || !SvROK (*svp)) 187 if (!svp || !SvROK (*svp))
168 { 188 {
169 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 189 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
170 return; 190 return;
171 } 191 }
172 192
173 if (!SvROK (*svp)) 193 if (!SvROK (*svp))
174 { 194 {
175 LOG (llevError, "deserialised perl object is not an RV"); 195 LOG (llevError, "deserialised perl object is not an RV\n");
176 return; 196 return;
177 } 197 }
178 198
179 HV *hv = (HV *)SvRV (*svp); 199 HV *hv = (HV *)SvRV (*svp);
180 200
181 if (SvTYPE (hv) != SVt_PVHV) 201 if (SvTYPE (hv) != SVt_PVHV)
182 { 202 {
183 LOG (llevError, "deserialised perl object is not a PVHV"); 203 LOG (llevError, "deserialised perl object is not a PVHV\n");
184 return; 204 return;
185 } 205 }
186 206
187 if (obj->self) 207 if (obj->self)
188 { 208 {
212object_thawer::~object_thawer () 232object_thawer::~object_thawer ()
213{ 233{
214 if (text) SvREFCNT_dec (text); 234 if (text) SvREFCNT_dec (text);
215 if (av) SvREFCNT_dec (av); 235 if (av) SvREFCNT_dec (av);
216 236
237 resolve_delayed_derefs (false);
238
217 free ((void *)name); 239 free ((void *)name);
218} 240}
219 241
220char * 242static void
221fgets (char *s, int n, object_thawer &thawer) 243error_out ()
222{ 244{
223 char *p = thawer.line; 245 if (object_thawer::errors_are_fatal)
224 char *q = s; 246 {
247 LOG (llevError, "(parse errors at this time are fatal, exiting)");
248 exit (1);
249 }
250}
225 251
252void
253object_thawer::parse_warn (const char *msg) const
254{
255 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
256 this->name, linenum,
257 kw_str ? kw_str : "<null>",
258 value ? value : "<null>",
259 msg);
260}
261
262bool
263object_thawer::parse_error (const char *type, const char *name, bool skip) const
264{
265 if (!type) type = "file section";
266 if (!name) name = "generic";
267
268 switch (kw)
269 {
270 case KW_EOF:
271 LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n",
272 this->name, linenum, type, name);
273 error_out ();
274 return false;
275
276 case KW_ERROR:
277 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
278 this->name, linenum,
279 type, name,
280 kw_str ? kw_str : "<file load>");
281 error_out ();
282 return false;
283
284 default:
285 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
286 this->name, linenum,
287 kw_str ? kw_str : "<null>",
288 value ? value : "<null>",
289 type, name,
290 skip ? "skipping line" : "aborting load");
291 error_out ();
292 return skip;
293 }
294}
295
296void
297object_thawer::next ()
298{
226 if (!p) 299 if (!line)
300 {
301 kw = KW_ERROR;
227 return 0; 302 return;
228
229 while (--n)
230 { 303 }
304
305 for (;;)
306 {
307 char *p = line;
308
309 if (expect_false (*p <= ' '))
310 {
311 // skip whitespace (only some files need this)
312 while (*p == ' ' || *p == '\t')
313 p++;
314
315 line = p;
316 }
317
231 if (!*p) 318 if (!*p)
319 {
320 kw = KW_EOF;
232 break; 321 break;
233 322 }
234 *q++ = *p;
235
236 if (*p++ == '\n')
237 break;
238 }
239
240 *q = 0;
241 thawer.line = p;
242
243 return s == q ? 0 : s;
244}
245
246keyword
247object_thawer::get_kv ()
248{
249 if (!line)
250 return KW_EOF;
251
252 for (;;)
253 {
254 char *p = line;
255
256 if (!*p)
257 return KW_EOF;
258 323
259 // parse keyword 324 // parse keyword
260 while (*p > ' ') 325 while (*p > ' ')
261 p++; 326 p++;
262 327
263 int klen = p - line; 328 int klen = p - line;
264 329
330 value_nn = "";
331 value = 0;
332
265 if (*p++ != '\n') 333 if (*p++ != '\n')
266 { 334 {
267 // parse value 335 // parse value
268 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 336 while (*(unsigned char *)p <= ' ' && *p != '\n')
269 ++p; 337 ++p;
270 338
271 last_value = p; 339 value_nn = value = p;
272 340
273 while (*p != '\n') 341 while (*p != '\n')
274 p++; 342 p++;
275 343
276 *p++ = 0; 344 *p++ = 0;
277 } 345 }
278 else
279 last_value = 0;
280 346
347 ++linenum;
281 line [klen] = 0; 348 line [klen] = 0;
282 keyword_idx *kw = kw_lex::match (line, klen); 349 keyword_idx *kw_idx = kw_lex::match (line, klen);
283 350
284 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D 351 kw_str = line;
285
286 last_keyword = line;
287 line = p; 352 line = p;
288 353
289 if (kw) 354 if (kw_idx)
290 return kw->index; 355 {
356 kw = kw_idx->index;
357 break;
358 }
291 else if (!*last_keyword || *last_keyword == '#') 359 else if (!*kw_str || *kw_str == '#')
292 ; // empty/comment line 360 ; // empty/comment line
293 else 361 else
362 {
294 return KW_ERROR; 363 kw = KW_ERROR;
364 break;
365 }
366 }
367}
368
369bool
370object_thawer::next_line ()
371{
372 if (!line)
295 } 373 {
296} 374 kw = KW_ERROR;
375 return 0;
376 }
297 377
378 for (;;)
379 {
380 char *p = line;
381
382 if (expect_false (*p <= ' '))
383 {
384 // skip whitespace (only some files need this)
385 while (*p == ' ' || *p == '\t')
386 p++;
387
388 line = p;
389 }
390
391 if (!*p)
392 {
393 kw = KW_EOF;
394 return 0;
395 }
396
397 kw = KW_value;
398 kw_str = p;
399 value_nn = p;
400 value = p;
401
402 // parse till newline
403 while (*p > '\n')
404 p++;
405
406 if (*p == '\n')
407 *p++ = 0;
408
409 ++linenum;
410
411 line = p;
412
413 if (*kw_str && *kw_str != '#')
414 break;
415
416 // empty/comment line -> skip it
417 }
418
419 return 1;
420}
421
298void 422void
299object_thawer::skip_kv (keyword kw) 423object_thawer::skip ()
300{ 424{
301 shstr ml; 425 shstr ml;
302 426
303 switch (kw) 427 switch (kw)
304 { 428 {
305 case KW_msg: get_ml (KW_endmsg , ml); break; 429 case KW_msg: get_ml (KW_endmsg , ml); break;
306 case KW_lore: get_ml (KW_endlore , ml); break; 430 case KW_lore: get_ml (KW_endlore , ml); break;
307 case KW_maplore: get_ml (KW_endmaplore, ml); break; 431 case KW_maplore: get_ml (KW_endmaplore, ml); break;
432 default: break;
308 } 433 }
434
435 next ();
309} 436}
310 437
311void 438void
312object_thawer::get (shstr &sh) const 439object_thawer::skip_block ()
313{ 440{
314 if (last_value) 441 // must not stop at KW_ERROR, as those signify custom keys
315 sh = last_value; 442 while (kw != KW_EOF)
316 else
317 { 443 {
318 sh = "<value missing>"; 444 keyword w = kw;
319 LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", last_keyword);//TODO: add filename 445 skip ();
446
447 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
448 skip_block ();
449 else if (w == KW_end)
450 break;
320 } 451 }
321} 452}
322 453
323void 454void
324object_thawer::get_ml (keyword kend, shstr &sh) 455object_thawer::get_ml (keyword kend, shstr &sh)
330 kw [0] = '\n'; 461 kw [0] = '\n';
331 memcpy (kw + 1, keyword_str [kend], klen); 462 memcpy (kw + 1, keyword_str [kend], klen);
332 kw [klen + 1] = '\n'; 463 kw [klen + 1] = '\n';
333 kw [klen + 2] = 0; 464 kw [klen + 2] = 0;
334 465
466 ++linenum;
467
335 // first test for completely empty msg... "endXXX\n" 468 // first test for completely empty msg... "endXXX\n"
336 if (!strncmp (line, kw + 1, klen + 1)) 469 if (!strncmp (line, kw + 1, klen + 1))
337 { 470 {
338 sh = 0; 471 sh = 0;
339 472
354 } 487 }
355 488
356 *end = 0; 489 *end = 0;
357 sh = line; 490 sh = line;
358 491
492 // count line numbers
493 while (line < end)
494 linenum += *line++ == '\n';
495
359 line = end + keyword_len [kend] + 1; 496 line += keyword_len [kend];
360 497
361 while (*line++ != '\n') 498 while (*line++ != '\n')
362 ; 499 ;
500
501 ++linenum;
363 } 502 }
364} 503}
365 504
366sint32 505sint32
367object_thawer::get_sint32 () const 506object_thawer::get_sint32 () const
368{ 507{
369 char *p = last_value; 508 const char *p = value_nn;
370
371 if (!p)
372 return 0;
373 509
374 sint32 val = 0; 510 sint32 val = 0;
375 bool negate; 511 bool negate;
376 512
377 if (*p == '-') 513 if (*p == '-')
390 while (*p); 526 while (*p);
391 527
392 return negate ? -val : val; 528 return negate ? -val : val;
393} 529}
394 530
395sint64 531void
396object_thawer::get_sint64 () const 532object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
397{ 533{
398 return last_value ? atoll (last_value) : 0; 534 op->refcnt_inc ();
535 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
536 delrefs.push_back (r);
537 ptr = 0;
399} 538}
400 539
401double 540void
402object_thawer::get_double () const 541object_thawer::resolve_delayed_derefs (bool deref)
403{ 542{
404 return last_value ? atof (last_value) : 0; 543 while (!delrefs.empty ())
405} 544 {
545 delayed_ref r = delrefs.back ();
546 delrefs.pop_back ();
406 547
548 if (deref)
549 *r.ptr = object::deref (r.ref);
550
551 free ((void *)r.ref);
552 r.op->refcnt_dec ();
553 }
554}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines