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.1 by root, Sat Dec 30 10:16:11 2006 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
23#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
25#include "logger.h" 24#include "logger.h"
26#include "cfperl.h" 25#include "cfperl.h"
27#include "kw_hash.h" 26#include "kw_hash.h"
28 27
29object_freezer::object_freezer () 28object_freezer::object_freezer ()
30: dynbuf (128 * 1024, 64 * 1024) 29: dynbuf_text (128 * 1024, 64 * 1024)
31{ 30{
32 av = newAV (); 31 av = newAV ();
33} 32}
34 33
35object_freezer::~object_freezer () 34object_freezer::~object_freezer ()
36{ 35{
37 SvREFCNT_dec (av); 36 SvREFCNT_dec (av);
38} 37}
39 38
39void
40void object_freezer::put (attachable *ext) 40object_freezer::put_ (attachable *ext)
41{ 41{
42 ext->optimise (); 42 ext->optimise ();
43 43
44 if (ext->self) 44 if (ext->self)
45 { 45 {
46 int idx = AvFILLp ((AV *)av) + 1; 46 int idx = AvFILLp ((AV *)av) + 1;
47 av_store (av, idx, newRV_inc ((SV *)ext->self)); 47 av_store (av, idx, newRV_inc ((SV *)ext->self));
48 48
49 add ((void *)"oid ", 4); 49 put (KW(oid), sint32(idx));
50 add ((sint32)idx);
51 add ('\n');
52 } 50 }
53} 51}
54 52
53bool
55bool object_freezer::save (const char *filename) 54object_freezer::save (const char *path)
56{ 55{
57 CALL_BEGIN (3); 56 CALL_BEGIN (3);
58 CALL_ARG_SV (newSVpv (filename, 0)); 57 CALL_ARG_SV (newSVpv (path, 0));
59 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 58 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
60 CALL_ARG_SV (newRV_inc ((SV *)av)); 59 CALL_ARG_SV (newRV_inc ((SV *)av));
61 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); 60 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
62 CALL_END; 61 CALL_END;
63}
64 62
63 return 1;
64}
65
66char *
65char *object_freezer::as_string () 67object_freezer::as_string ()
66{ 68{
67 CALL_BEGIN (2); 69 CALL_BEGIN (2);
68 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 70 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
69 CALL_ARG_SV (newRV_inc ((SV *)av)); 71 CALL_ARG_SV (newRV_inc ((SV *)av));
70 CALL_CALL ("cf::object_freezer_as_string", G_SCALAR); 72 CALL_CALL ("cf::object_freezer_as_string", G_SCALAR);
71 73
72 char *res = count > 0 74 char *res = count > 0
73 ? strdup (SvPVbyte_nolen (POPs)) 75 ? strdup (SvPVX (POPs))
74 : strdup ("[fatal error]"); 76 : strdup ("[fatal error]");
75 77
76 CALL_END; 78 CALL_END;
77 79
78 return res; 80 return res;
79} 81}
80 82
83#if 0
84void
81int fprintf (object_freezer &freezer, const char *format, ...) 85fprintf (object_freezer &freezer, const char *format, ...)
82{ 86{
83 va_list ap; 87 va_list ap;
84 88
85 va_start (ap, format); 89 va_start (ap, format);
86 90
90 freezer.alloc (len); 94 freezer.alloc (len);
91 95
92 va_end (ap); 96 va_end (ap);
93} 97}
94 98
99// XXX: function not returning an int
100void
95int fputs (const char *s, object_freezer &freezer) 101fputs (const char *s, object_freezer &freezer)
96{ 102{
97 freezer.add (s); 103 freezer.add (s);
98} 104}
105#endif
99 106
100static const char thawer_eof[] = "\n\n\n\0\0\0"; 107static const char thawer_eof[] = "\n\n\n\0\0\0";
101 108
109bool object_thawer::errors_are_fatal = true;
110
102object_thawer::object_thawer (const char *filename) 111object_thawer::object_thawer (const char *data, AV *perlav)
112: name (strdup ("(memory stream)"))
103{ 113{
104 static const char eof[] = "\n\n\n\0\0\0"; 114 av = perlav;
115 text = newSVpv (data, 0);
116 sv_catpv (text, thawer_eof);
117 line = SvPVbyte_nolen (text);
118 next ();
119}
105 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{
135 name = strdup (path);
106 av = 0; 136 av = 0;
107 text = 0; 137 text = 0;
108 line = 0; 138 line = 0;
139 linenum = 0;
109 140
110 if (filename) 141 kw = KW_ERROR;
142 kw_str = 0;
143 value = 0;
144
145 if (path)
111 { 146 {
112 CALL_BEGIN (1); 147 CALL_BEGIN (1);
113 CALL_ARG_SV (newSVpv (filename, 0)); 148 CALL_ARG_SV (newSVpv (path, 0));
114 CALL_CALL ("cf::object_thawer_load", G_ARRAY); 149 CALL_CALL ("cf::object_thawer_load", G_ARRAY);
115 150
116 if (count == 2) 151 if (count == 2)
117 { 152 {
118 // second value - perl objects 153 // second value - perl objects
125 // first value - text part, pad with 3 zeroes 160 // first value - text part, pad with 3 zeroes
126 { 161 {
127 SV *sv = POPs; 162 SV *sv = POPs;
128 STRLEN len; 163 STRLEN len;
129 char *sv_ = SvPVbyte (sv, len); 164 char *sv_ = SvPVbyte (sv, len);
130 text = newSV (len + sizeof (eof)); 165 text = newSV (len + sizeof (thawer_eof));
131 SvCUR_set (text, len); 166 SvCUR_set (text, len + sizeof (thawer_eof));
132 memcpy (SvPVX (text), sv_, len); 167 memcpy (SvPVX (text), sv_, len);
133 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 168 memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof));
134 169
135 line = SvPVX (text); 170 line = SvPVX (text);
171 next ();
136 } 172 }
137 } 173 }
138 174
139 CALL_END; 175 CALL_END;
140 } 176 }
141} 177}
142 178
143object_thawer::object_thawer (const char *data, AV *perlav) 179void
144{
145 av = perlav;
146 text = newSVpv (data, 0);
147 sv_catpv (text, thawer_eof);
148 line = SvPVbyte_nolen (text);
149}
150
151void object_thawer::get (attachable *obj, int oid) 180object_thawer::get (attachable *obj, int oid)
152{ 181{
153 if (!av || oid < 0) // this is actually an error of sorts 182 if (!av || oid < 0) // this is actually an error of sorts
154 return; 183 return;
155 184
156 SV **svp = av_fetch ((AV *)av, oid, 0); 185 SV **svp = av_fetch ((AV *)av, oid, 0);
157 186
158 if (!svp || !SvROK (*svp)) 187 if (!svp || !SvROK (*svp))
159 { 188 {
160 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);
161 return; 190 return;
162 } 191 }
163 192
164 if (!SvROK (*svp)) 193 if (!SvROK (*svp))
165 { 194 {
166 LOG (llevError, "deserialised perl object is not an RV"); 195 LOG (llevError, "deserialised perl object is not an RV\n");
167 return; 196 return;
168 } 197 }
169 198
170 HV *hv = (HV *)SvRV (*svp); 199 HV *hv = (HV *)SvRV (*svp);
171 200
172 if (SvTYPE (hv) != SVt_PVHV) 201 if (SvTYPE (hv) != SVt_PVHV)
173 { 202 {
174 LOG (llevError, "deserialised perl object is not a PVHV"); 203 LOG (llevError, "deserialised perl object is not a PVHV\n");
175 return; 204 return;
176 } 205 }
177 206
178 if (obj->self) 207 if (obj->self)
179 { 208 {
180 // the hard way(?) 209 // the hard way(?)
181 210
182 // the dirty blues: "just" swap the XPVHV's 211 CALL_BEGIN (2);
183 swap (SvANY (obj->self), SvANY (hv)); 212 CALL_ARG_SV (newRV_inc ((SV *)obj->self));
184 213 CALL_ARG_SV (newRV_inc ((SV *)hv));
185 // do not swap magic, though 214 PUTBACK;
186 swap (SvMAGIC (obj->self), SvMAGIC (hv)); 215 call_method ("thawer_merge", G_DISCARD | G_EVAL);
216 SPAGAIN;
217 CALL_END;
187 } 218 }
188 else 219 else
189 { 220 {
190 // the easy way(?) 221 // the easy way(?)
191 222
192 obj->self = hv; 223 obj->self = hv;
193 SvRV_set (*svp, &PL_sv_undef); 224 SvRV_set (*svp, &PL_sv_undef);
194 225
195 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); 226 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
196
197 // borrow a refcount for the perl object
198 obj->flags |= attachable::F_BORROWED;
199 obj->refcnt_dec ();
200 } 227 }
201 228
202 obj->reattach (); 229 obj->reattach ();
203} 230}
204 231
205object_thawer::~object_thawer () 232object_thawer::~object_thawer ()
206{ 233{
207 if (text) SvREFCNT_dec (text); 234 if (text) SvREFCNT_dec (text);
208 if (av) SvREFCNT_dec (av); 235 if (av) SvREFCNT_dec (av);
209}
210 236
211char *fgets (char *s, int n, object_thawer &thawer) 237 resolve_delayed_derefs (false);
212{
213 char *p = thawer.line;
214 char *q = s;
215 238
239 free ((void *)name);
240}
241
242static void
243error_out ()
244{
245 if (object_thawer::errors_are_fatal)
246 {
247 LOG (llevError, "(parse errors at this time are fatal, exiting)");
248 exit (1);
249 }
250}
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{
216 if (!p) 299 if (!line)
300 {
301 kw = KW_ERROR;
217 return 0; 302 return;
218
219 while (--n)
220 { 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
221 if (!*p) 318 if (!*p)
319 {
320 kw = KW_EOF;
222 break; 321 break;
223 322 }
224 *q++ = *p;
225
226 if (*p++ == '\n')
227 break;
228 }
229
230 *q = 0;
231 thawer.line = p;
232
233 return s == q ? 0 : s;
234}
235
236keyword object_thawer::get_kv ()
237{
238 if (!line)
239 return KW_EOF;
240
241 for (;;)
242 {
243 char *p = line;
244
245 if (!*p)
246 return KW_EOF;
247 323
248 // parse keyword 324 // parse keyword
249 while (*p > ' ') 325 while (*p > ' ')
250 p++; 326 p++;
251 327
252 int klen = p - line; 328 int klen = p - line;
253 329
330 value_nn = "";
331 value = 0;
332
254 if (*p++ != '\n') 333 if (*p++ != '\n')
255 { 334 {
256 // parse value 335 // parse value
257 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 336 while (*(unsigned char *)p <= ' ' && *p != '\n')
258 ++p; 337 ++p;
259 338
260 last_value = p; 339 value_nn = value = p;
261 340
262 while (*p != '\n') 341 while (*p != '\n')
263 p++; 342 p++;
264 343
265 *p++ = 0; 344 *p++ = 0;
266 } 345 }
267 else
268 last_value = 0;
269 346
347 ++linenum;
270 line [klen] = 0; 348 line [klen] = 0;
271 keyword_idx *kw = kw_lex::match (line, klen); 349 keyword_idx *kw_idx = kw_lex::match (line, klen);
272 350
273 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D 351 kw_str = line;
274
275 last_keyword = line;
276 line = p; 352 line = p;
277 353
278 if (kw) 354 if (kw_idx)
279 return kw->index; 355 {
356 kw = kw_idx->index;
357 break;
358 }
280 else if (!*last_keyword || *last_keyword == '#') 359 else if (!*kw_str || *kw_str == '#')
281 ; // empty/comment line 360 ; // empty/comment line
282 else 361 else
362 {
283 return KW_ERROR; 363 kw = KW_ERROR;
364 break;
365 }
366 }
367}
368
369bool
370object_thawer::next_line ()
371{
372 if (!line)
284 } 373 {
285} 374 kw = KW_ERROR;
375 return 0;
376 }
286 377
287void object_thawer::skip_kv (keyword kw) 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
422void
423object_thawer::skip ()
288{ 424{
289 shstr ml; 425 shstr ml;
290 426
291 switch (kw) 427 switch (kw)
292 { 428 {
293 case KW_msg: get_ml (KW_endmsg , ml); break; 429 case KW_msg: get_ml (KW_endmsg , ml); break;
294 case KW_lore: get_ml (KW_endlore , ml); break; 430 case KW_lore: get_ml (KW_endlore , ml); break;
295 case KW_maplore: get_ml (KW_endmaplore, ml); break; 431 case KW_maplore: get_ml (KW_endmaplore, ml); break;
432 default: break;
296 } 433 }
434
435 next ();
297} 436}
298 437
299void object_thawer::get (shstr &sh) const 438void
439object_thawer::skip_block ()
300{ 440{
301 if (last_value) 441 // must not stop at KW_ERROR, as those signify custom keys
302 sh = last_value; 442 while (kw != KW_EOF)
303 else
304 {
305 sh = "<value missing>";
306 LOG (llevError, "keyword requires value: <%.320s>\n", line);//TODO: add filename
307 } 443 {
308} 444 keyword w = kw;
445 skip ();
309 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;
451 }
452}
453
454void
310void object_thawer::get_ml (keyword kend, shstr &sh) 455object_thawer::get_ml (keyword kend, shstr &sh)
311{ 456{
312 char kw[128]; 457 char kw[128];
313 458
314 int klen = keyword_len [kend]; 459 int klen = keyword_len [kend];
315 460
316 kw [0] = '\n'; 461 kw [0] = '\n';
317 memcpy (kw + 1, keyword_str [kend], klen); 462 memcpy (kw + 1, keyword_str [kend], klen);
318 kw [klen + 1] = '\n'; 463 kw [klen + 1] = '\n';
319 kw [klen + 2] = 0; 464 kw [klen + 2] = 0;
320 465
466 ++linenum;
467
321 // first test for completely empty msg... "endXXX\n" 468 // first test for completely empty msg... "endXXX\n"
322 if (!strncmp (line, kw + 1, klen + 1)) 469 if (!strncmp (line, kw + 1, klen + 1))
323 { 470 {
324 sh = 0; 471 sh = 0;
325 472
340 } 487 }
341 488
342 *end = 0; 489 *end = 0;
343 sh = line; 490 sh = line;
344 491
492 // count line numbers
493 while (line < end)
494 linenum += *line++ == '\n';
495
345 line = end + keyword_len [kend] + 1; 496 line += keyword_len [kend];
346 497
347 while (*line++ != '\n') 498 while (*line++ != '\n')
348 ; 499 ;
349 }
350}
351 500
501 ++linenum;
502 }
503}
504
505sint32
352sint32 object_thawer::get_sint32 () const 506object_thawer::get_sint32 () const
353{ 507{
354 char *p = last_value; 508 const char *p = value_nn;
355
356 if (!p)
357 return 0;
358 509
359 sint32 val = 0; 510 sint32 val = 0;
360 bool negate; 511 bool negate;
361 512
362 if (*p == '-') 513 if (*p == '-')
375 while (*p); 526 while (*p);
376 527
377 return negate ? -val : val; 528 return negate ? -val : val;
378} 529}
379 530
380sint64 object_thawer::get_sint64 () const 531void
532object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
381{ 533{
382 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;
383} 538}
384 539
385double object_thawer::get_double () const 540void
541object_thawer::resolve_delayed_derefs (bool deref)
386{ 542{
387 return last_value ? atof (last_value) : 0; 543 while (!delrefs.empty ())
388} 544 {
545 delayed_ref r = delrefs.back ();
546 delrefs.pop_back ();
389 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