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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines