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.2 by elmex, Sat Dec 30 14:34:10 2006 UTC vs.
Revision 1.38 by root, Thu Apr 15 04:56:47 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 STRLEN len;
73
74 char *res = count > 0 74 char *res = count > 0
75 ? strdup (SvPVx (POPs, len)) 75 ? strdup (SvPVX (POPs))
76 : strdup ("[fatal error]"); 76 : strdup ("[fatal error]");
77 77
78 CALL_END; 78 CALL_END;
79 79
80 return res; 80 return res;
81} 81}
82 82
83#if 0
84void
83int fprintf (object_freezer &freezer, const char *format, ...) 85fprintf (object_freezer &freezer, const char *format, ...)
84{ 86{
85 va_list ap; 87 va_list ap;
86 88
87 va_start (ap, format); 89 va_start (ap, format);
88 90
92 freezer.alloc (len); 94 freezer.alloc (len);
93 95
94 va_end (ap); 96 va_end (ap);
95} 97}
96 98
99// XXX: function not returning an int
100void
97int fputs (const char *s, object_freezer &freezer) 101fputs (const char *s, object_freezer &freezer)
98{ 102{
99 freezer.add (s); 103 freezer.add (s);
100} 104}
105#endif
101 106
102static const char thawer_eof[] = "\n\n\n\0\0\0"; 107static const char thawer_eof[] = "\n\n\n\0\0\0";
103 108
104object_thawer::object_thawer (const char *filename) 109object_thawer::object_thawer (const char *path)
110: name (strdup (path))
105{ 111{
106 static const char eof[] = "\n\n\n\0\0\0";
107
108 av = 0; 112 av = 0;
109 text = 0; 113 text = 0;
110 line = 0; 114 line = 0;
115 linenum = 0;
111 116
112 if (filename) 117 kw = KW_ERROR;
118 kw_str = 0;
119 value = 0;
120
121 if (path)
113 { 122 {
114 CALL_BEGIN (1); 123 CALL_BEGIN (1);
115 CALL_ARG_SV (newSVpv (filename, 0)); 124 CALL_ARG_SV (newSVpv (path, 0));
116 CALL_CALL ("cf::object_thawer_load", G_ARRAY); 125 CALL_CALL ("cf::object_thawer_load", G_ARRAY);
117 126
118 if (count == 2) 127 if (count == 2)
119 { 128 {
120 // second value - perl objects 129 // second value - perl objects
127 // first value - text part, pad with 3 zeroes 136 // first value - text part, pad with 3 zeroes
128 { 137 {
129 SV *sv = POPs; 138 SV *sv = POPs;
130 STRLEN len; 139 STRLEN len;
131 char *sv_ = SvPVbyte (sv, len); 140 char *sv_ = SvPVbyte (sv, len);
132 text = newSV (len + sizeof (eof)); 141 text = newSV (len + sizeof (thawer_eof));
133 SvCUR_set (text, len); 142 SvCUR_set (text, len + sizeof (thawer_eof));
134 memcpy (SvPVX (text), sv_, len); 143 memcpy (SvPVX (text), sv_, len);
135 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 144 memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof));
136 145
137 line = SvPVX (text); 146 line = SvPVX (text);
147 next ();
138 } 148 }
139 } 149 }
140 150
141 CALL_END; 151 CALL_END;
142 } 152 }
143} 153}
144 154
145object_thawer::object_thawer (const char *data, AV *perlav) 155object_thawer::object_thawer (const char *data, AV *perlav)
156: name (strdup ("(memory stream)"))
146{ 157{
147 av = perlav; 158 av = perlav;
148 text = newSVpv (data, 0); 159 text = newSVpv (data, 0);
149 sv_catpv (text, thawer_eof); 160 sv_catpv (text, thawer_eof);
150 line = SvPVbyte_nolen (text); 161 line = SvPVbyte_nolen (text);
162 next ();
151} 163}
152 164
165void
153void object_thawer::get (attachable *obj, int oid) 166object_thawer::get (attachable *obj, int oid)
154{ 167{
155 if (!av || oid < 0) // this is actually an error of sorts 168 if (!av || oid < 0) // this is actually an error of sorts
156 return; 169 return;
157 170
158 SV **svp = av_fetch ((AV *)av, oid, 0); 171 SV **svp = av_fetch ((AV *)av, oid, 0);
159 172
160 if (!svp || !SvROK (*svp)) 173 if (!svp || !SvROK (*svp))
161 { 174 {
162 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);
163 return; 176 return;
164 } 177 }
165 178
166 if (!SvROK (*svp)) 179 if (!SvROK (*svp))
167 { 180 {
168 LOG (llevError, "deserialised perl object is not an RV"); 181 LOG (llevError, "deserialised perl object is not an RV\n");
169 return; 182 return;
170 } 183 }
171 184
172 HV *hv = (HV *)SvRV (*svp); 185 HV *hv = (HV *)SvRV (*svp);
173 186
174 if (SvTYPE (hv) != SVt_PVHV) 187 if (SvTYPE (hv) != SVt_PVHV)
175 { 188 {
176 LOG (llevError, "deserialised perl object is not a PVHV"); 189 LOG (llevError, "deserialised perl object is not a PVHV\n");
177 return; 190 return;
178 } 191 }
179 192
180 if (obj->self) 193 if (obj->self)
181 { 194 {
182 // the hard way(?) 195 // the hard way(?)
183 196
184 // the dirty blues: "just" swap the XPVHV's 197 CALL_BEGIN (2);
185 swap (SvANY (obj->self), SvANY (hv)); 198 CALL_ARG_SV (newRV_inc ((SV *)obj->self));
186 199 CALL_ARG_SV (newRV_inc ((SV *)hv));
187 // do not swap magic, though 200 PUTBACK;
188 swap (SvMAGIC (obj->self), SvMAGIC (hv)); 201 call_method ("thawer_merge", G_DISCARD | G_EVAL);
202 SPAGAIN;
203 CALL_END;
189 } 204 }
190 else 205 else
191 { 206 {
192 // the easy way(?) 207 // the easy way(?)
193 208
194 obj->self = hv; 209 obj->self = hv;
195 SvRV_set (*svp, &PL_sv_undef); 210 SvRV_set (*svp, &PL_sv_undef);
196 211
197 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); 212 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
198
199 // borrow a refcount for the perl object
200 obj->flags |= attachable::F_BORROWED;
201 obj->refcnt_dec ();
202 } 213 }
203 214
204 obj->reattach (); 215 obj->reattach ();
205} 216}
206 217
207object_thawer::~object_thawer () 218object_thawer::~object_thawer ()
208{ 219{
209 if (text) SvREFCNT_dec (text); 220 if (text) SvREFCNT_dec (text);
210 if (av) SvREFCNT_dec (av); 221 if (av) SvREFCNT_dec (av);
211}
212 222
213char *fgets (char *s, int n, object_thawer &thawer) 223 resolve_delayed_derefs (false);
214{
215 char *p = thawer.line;
216 char *q = s;
217 224
225 free ((void *)name);
226}
227
228void
229object_thawer::parse_warn (const char *msg)
230{
231 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
232 this->name, linenum,
233 kw_str ? kw_str : "<null>",
234 value ? value : "<null>",
235 msg);
236}
237
238bool
239object_thawer::parse_error (const char *type, const char *name, bool skip)
240{
241 if (!type) type = "file section";
242 if (!name) name = "generic";
243
244 switch (kw)
245 {
246 case KW_EOF:
247 LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n",
248 this->name, linenum, type, name);
249 return false;
250
251 case KW_ERROR:
252 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
253 this->name, linenum,
254 type, name,
255 kw_str ? kw_str : "<file load>");
256 return false;
257
258 default:
259 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
260 this->name, linenum,
261 kw_str ? kw_str : "<null>",
262 value ? value : "<null>",
263 type, name,
264 skip ? "skipping line" : "aborting load");
265 return skip;
266 }
267}
268
269void
270object_thawer::next ()
271{
218 if (!p) 272 if (!line)
273 {
274 kw = KW_ERROR;
219 return 0; 275 return;
220
221 while (--n)
222 { 276 }
277
278 for (;;)
279 {
280 char *p = line;
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
223 if (!*p) 291 if (!*p)
292 {
293 kw = KW_EOF;
224 break; 294 break;
225 295 }
226 *q++ = *p;
227
228 if (*p++ == '\n')
229 break;
230 }
231
232 *q = 0;
233 thawer.line = p;
234
235 return s == q ? 0 : s;
236}
237
238keyword object_thawer::get_kv ()
239{
240 if (!line)
241 return KW_EOF;
242
243 for (;;)
244 {
245 char *p = line;
246
247 if (!*p)
248 return KW_EOF;
249 296
250 // parse keyword 297 // parse keyword
251 while (*p > ' ') 298 while (*p > ' ')
252 p++; 299 p++;
253 300
254 int klen = p - line; 301 int klen = p - line;
255 302
303 value_nn = "";
304 value = 0;
305
256 if (*p++ != '\n') 306 if (*p++ != '\n')
257 { 307 {
258 // parse value 308 // parse value
259 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 309 while (*(unsigned char *)p <= ' ' && *p != '\n')
260 ++p; 310 ++p;
261 311
262 last_value = p; 312 value_nn = value = p;
263 313
264 while (*p != '\n') 314 while (*p != '\n')
265 p++; 315 p++;
266 316
267 *p++ = 0; 317 *p++ = 0;
268 } 318 }
269 else
270 last_value = 0;
271 319
320 ++linenum;
272 line [klen] = 0; 321 line [klen] = 0;
273 keyword_idx *kw = kw_lex::match (line, klen); 322 keyword_idx *kw_idx = kw_lex::match (line, klen);
274 323
275 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D 324 kw_str = line;
276
277 last_keyword = line;
278 line = p; 325 line = p;
279 326
280 if (kw) 327 if (kw_idx)
281 return kw->index; 328 {
329 kw = kw_idx->index;
330 break;
331 }
282 else if (!*last_keyword || *last_keyword == '#') 332 else if (!*kw_str || *kw_str == '#')
283 ; // empty/comment line 333 ; // empty/comment line
284 else 334 else
335 {
285 return KW_ERROR; 336 kw = KW_ERROR;
337 break;
338 }
339 }
340}
341
342bool
343object_thawer::next_line ()
344{
345 if (!line)
286 } 346 {
287} 347 kw = KW_ERROR;
348 return 0;
349 }
288 350
289void object_thawer::skip_kv (keyword kw) 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 return 0;
368 }
369
370 kw = KW_value;
371 kw_str = p;
372 value_nn = p;
373 value = p;
374
375 // parse till newline
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
390 }
391
392 return 1;
393}
394
395void
396object_thawer::skip ()
290{ 397{
291 shstr ml; 398 shstr ml;
292 399
293 switch (kw) 400 switch (kw)
294 { 401 {
295 case KW_msg: get_ml (KW_endmsg , ml); break; 402 case KW_msg: get_ml (KW_endmsg , ml); break;
296 case KW_lore: get_ml (KW_endlore , ml); break; 403 case KW_lore: get_ml (KW_endlore , ml); break;
297 case KW_maplore: get_ml (KW_endmaplore, ml); break; 404 case KW_maplore: get_ml (KW_endmaplore, ml); break;
405 default: break;
298 } 406 }
407
408 next ();
299} 409}
300 410
301void object_thawer::get (shstr &sh) const 411void
412object_thawer::skip_block ()
302{ 413{
303 if (last_value) 414 // must not stop at KW_ERROR, as those signify custom keys
304 sh = last_value; 415 while (kw != KW_EOF)
305 else
306 {
307 sh = "<value missing>";
308 LOG (llevError, "keyword requires value: <%.320s>\n", line);//TODO: add filename
309 } 416 {
310} 417 keyword w = kw;
418 skip ();
311 419
420 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
421 skip_block ();
422 else if (w == KW_end)
423 break;
424 }
425}
426
427void
312void object_thawer::get_ml (keyword kend, shstr &sh) 428object_thawer::get_ml (keyword kend, shstr &sh)
313{ 429{
314 char kw[128]; 430 char kw[128];
315 431
316 int klen = keyword_len [kend]; 432 int klen = keyword_len [kend];
317 433
318 kw [0] = '\n'; 434 kw [0] = '\n';
319 memcpy (kw + 1, keyword_str [kend], klen); 435 memcpy (kw + 1, keyword_str [kend], klen);
320 kw [klen + 1] = '\n'; 436 kw [klen + 1] = '\n';
321 kw [klen + 2] = 0; 437 kw [klen + 2] = 0;
322 438
439 ++linenum;
440
323 // first test for completely empty msg... "endXXX\n" 441 // first test for completely empty msg... "endXXX\n"
324 if (!strncmp (line, kw + 1, klen + 1)) 442 if (!strncmp (line, kw + 1, klen + 1))
325 { 443 {
326 sh = 0; 444 sh = 0;
327 445
342 } 460 }
343 461
344 *end = 0; 462 *end = 0;
345 sh = line; 463 sh = line;
346 464
465 // count line numbers
466 while (line < end)
467 linenum += *line++ == '\n';
468
347 line = end + keyword_len [kend] + 1; 469 line += keyword_len [kend];
348 470
349 while (*line++ != '\n') 471 while (*line++ != '\n')
350 ; 472 ;
351 }
352}
353 473
474 ++linenum;
475 }
476}
477
478sint32
354sint32 object_thawer::get_sint32 () const 479object_thawer::get_sint32 () const
355{ 480{
356 char *p = last_value; 481 const char *p = value_nn;
357
358 if (!p)
359 return 0;
360 482
361 sint32 val = 0; 483 sint32 val = 0;
362 bool negate; 484 bool negate;
363 485
364 if (*p == '-') 486 if (*p == '-')
377 while (*p); 499 while (*p);
378 500
379 return negate ? -val : val; 501 return negate ? -val : val;
380} 502}
381 503
382sint64 object_thawer::get_sint64 () const 504void
505object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
383{ 506{
384 return last_value ? atoll (last_value) : 0; 507 op->refcnt_inc ();
508 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
509 delrefs.push_back (r);
510 ptr = 0;
385} 511}
386 512
387double object_thawer::get_double () const 513void
514object_thawer::resolve_delayed_derefs (bool deref)
388{ 515{
389 return last_value ? atof (last_value) : 0; 516 while (!delrefs.empty ())
390} 517 {
518 delayed_ref r = delrefs.back ();
519 delrefs.pop_back ();
391 520
521 if (deref)
522 *r.ptr = object::deref (r.ref);
523
524 free ((void *)r.ref);
525 r.op->refcnt_dec ();
526 }
527}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines