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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines