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.25 by root, Tue Sep 4 05:43:22 2007 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 Crossfire TRT, the Roguelike Realtime MORPG.
7 * 3 *
8 * Copyright (C) 2006 by Marc Lehmann <crossfire@schmorp.de> 4 * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
9 * 5 *
10 * This program is free software; you can redistribute it and/or modify 6 * Crossfire TRT is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version. 9 * (at your 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 GNU General Public License
21 * along with this program; if not, write to the Free Software 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 *
19 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */ 20 */
24 21
25#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff 22#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
26#include "logger.h" 23#include "logger.h"
27#include "cfperl.h" 24#include "cfperl.h"
28#include "kw_hash.h" 25#include "kw_hash.h"
29 26
30object_freezer::object_freezer () 27object_freezer::object_freezer ()
31: dynbuf (128 * 1024, 64 * 1024) 28: dynbuf_text (128 * 1024, 64 * 1024)
32{ 29{
33 av = newAV (); 30 av = newAV ();
34} 31}
35 32
36object_freezer::~object_freezer () 33object_freezer::~object_freezer ()
61 CALL_ARG_SV (newSVpv (path, 0)); 58 CALL_ARG_SV (newSVpv (path, 0));
62 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 59 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
63 CALL_ARG_SV (newRV_inc ((SV *)av)); 60 CALL_ARG_SV (newRV_inc ((SV *)av));
64 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); 61 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
65 CALL_END; 62 CALL_END;
63
64 return 1;
66} 65}
67 66
68char * 67char *
69object_freezer::as_string () 68object_freezer::as_string ()
70{ 69{
80 CALL_END; 79 CALL_END;
81 80
82 return res; 81 return res;
83} 82}
84 83
85int 84#if 0
85void
86fprintf (object_freezer &freezer, const char *format, ...) 86fprintf (object_freezer &freezer, const char *format, ...)
87{ 87{
88 va_list ap; 88 va_list ap;
89 89
90 va_start (ap, format); 90 va_start (ap, format);
95 freezer.alloc (len); 95 freezer.alloc (len);
96 96
97 va_end (ap); 97 va_end (ap);
98} 98}
99 99
100int 100// XXX: function not returning an int
101void
101fputs (const char *s, object_freezer &freezer) 102fputs (const char *s, object_freezer &freezer)
102{ 103{
103 freezer.add (s); 104 freezer.add (s);
104} 105}
106#endif
105 107
106static const char thawer_eof[] = "\n\n\n\0\0\0"; 108static const char thawer_eof[] = "\n\n\n\0\0\0";
107 109
108object_thawer::object_thawer (const char *path) 110object_thawer::object_thawer (const char *path)
109: name (strdup (path)) 111: name (strdup (path))
110{ 112{
111 static const char eof[] = "\n\n\n\0\0\0"; 113 static const char eof[] = "\n\n\n\0\0\0";
112 114
113 av = 0; 115 av = 0;
114 text = 0; 116 text = 0;
115 line = 0; 117 line = 0;
118 linenum = 0;
119
120 kw = KW_ERROR;
121 kw_str = 0;
122 value = 0;
116 123
117 if (path) 124 if (path)
118 { 125 {
119 CALL_BEGIN (1); 126 CALL_BEGIN (1);
120 CALL_ARG_SV (newSVpv (path, 0)); 127 CALL_ARG_SV (newSVpv (path, 0));
138 SvCUR_set (text, len); 145 SvCUR_set (text, len);
139 memcpy (SvPVX (text), sv_, len); 146 memcpy (SvPVX (text), sv_, len);
140 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 147 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
141 148
142 line = SvPVX (text); 149 line = SvPVX (text);
150 next ();
143 } 151 }
144 } 152 }
145 153
146 CALL_END; 154 CALL_END;
147 } 155 }
148} 156}
149 157
150object_thawer::object_thawer (const char *data, AV *perlav) 158object_thawer::object_thawer (const char *data, AV *perlav)
151: name (strdup ("(memory stream")) 159: name (strdup ("(memory stream)"))
152{ 160{
153 av = perlav; 161 av = perlav;
154 text = newSVpv (data, 0); 162 text = newSVpv (data, 0);
155 sv_catpv (text, thawer_eof); 163 sv_catpv (text, thawer_eof);
156 line = SvPVbyte_nolen (text); 164 line = SvPVbyte_nolen (text);
165 next ();
157} 166}
158 167
159void 168void
160object_thawer::get (attachable *obj, int oid) 169object_thawer::get (attachable *obj, int oid)
161{ 170{
164 173
165 SV **svp = av_fetch ((AV *)av, oid, 0); 174 SV **svp = av_fetch ((AV *)av, oid, 0);
166 175
167 if (!svp || !SvROK (*svp)) 176 if (!svp || !SvROK (*svp))
168 { 177 {
169 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 178 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
170 return; 179 return;
171 } 180 }
172 181
173 if (!SvROK (*svp)) 182 if (!SvROK (*svp))
174 { 183 {
175 LOG (llevError, "deserialised perl object is not an RV"); 184 LOG (llevError, "deserialised perl object is not an RV\n");
176 return; 185 return;
177 } 186 }
178 187
179 HV *hv = (HV *)SvRV (*svp); 188 HV *hv = (HV *)SvRV (*svp);
180 189
181 if (SvTYPE (hv) != SVt_PVHV) 190 if (SvTYPE (hv) != SVt_PVHV)
182 { 191 {
183 LOG (llevError, "deserialised perl object is not a PVHV"); 192 LOG (llevError, "deserialised perl object is not a PVHV\n");
184 return; 193 return;
185 } 194 }
186 195
187 if (obj->self) 196 if (obj->self)
188 { 197 {
189 // the hard way(?) 198 // the hard way(?)
190 199
191 // the dirty blues: "just" swap the XPVHV's 200 CALL_BEGIN (2);
192 swap (SvANY (obj->self), SvANY (hv)); 201 CALL_ARG_SV (newRV_inc ((SV *)obj->self));
193 202 CALL_ARG_SV (newRV_inc ((SV *)hv));
194 // do not swap magic, though 203 PUTBACK;
195 swap (SvMAGIC (obj->self), SvMAGIC (hv)); 204 call_method ("thawer_merge", G_DISCARD | G_EVAL);
205 SPAGAIN;
206 CALL_END;
196 } 207 }
197 else 208 else
198 { 209 {
199 // the easy way(?) 210 // the easy way(?)
200 211
201 obj->self = hv; 212 obj->self = hv;
202 SvRV_set (*svp, &PL_sv_undef); 213 SvRV_set (*svp, &PL_sv_undef);
203 214
204 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); 215 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 } 216 }
210 217
211 obj->reattach (); 218 obj->reattach ();
212} 219}
213 220
214object_thawer::~object_thawer () 221object_thawer::~object_thawer ()
215{ 222{
216 if (text) SvREFCNT_dec (text); 223 if (text) SvREFCNT_dec (text);
217 if (av) SvREFCNT_dec (av); 224 if (av) SvREFCNT_dec (av);
218 225
226 resolve_delayed_derefs (false);
227
219 free ((void *)name); 228 free ((void *)name);
220} 229}
221 230
231//TODO: remove
222char * 232char *
223fgets (char *s, int n, object_thawer &thawer) 233fgets (char *s, int n, object_thawer &thawer)
224{ 234{
225 char *p = thawer.line; 235 char *p = thawer.line;
226 char *q = s; 236 char *q = s;
234 break; 244 break;
235 245
236 *q++ = *p; 246 *q++ = *p;
237 247
238 if (*p++ == '\n') 248 if (*p++ == '\n')
249 {
250 ++thawer.linenum;
239 break; 251 break;
252 }
240 } 253 }
241 254
242 *q = 0; 255 *q = 0;
243 thawer.line = p; 256 thawer.line = p;
244 257
245 return s == q ? 0 : s; 258 return s == q ? 0 : s;
246} 259}
247 260
248keyword 261void
262object_thawer::parse_warn (const char *msg)
263{
264 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
265 this->name, linenum,
266 kw_str ? kw_str : "<null>",
267 value ? value : "<null>",
268 msg);
269}
270
271bool
272object_thawer::parse_error (const char *type, const char *name, bool skip)
273{
274 if (!type) type = "file section";
275 if (!name) name = "generic";
276
277 switch (kw)
278 {
279 case KW_EOF:
280 LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n",
281 this->name, linenum, type, name);
282 return false;
283
284 case KW_ERROR:
285 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
286 this->name, linenum,
287 type, name,
288 kw_str ? kw_str : "<file load>");
289 return false;
290
291 default:
292 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
293 this->name, linenum,
294 kw_str ? kw_str : "<null>",
295 value ? value : "<null>",
296 type, name,
297 skip ? "skipping line" : "aborting load");
298 return skip;
299 }
300}
301
302void
249object_thawer::get_kv () 303object_thawer::next ()
250{ 304{
251 if (!line) 305 if (!line)
252 return KW_EOF; 306 {
307 kw = KW_ERROR;
308 return;
309 }
253 310
254 for (;;) 311 for (;;)
255 { 312 {
256 char *p = line; 313 char *p = line;
257 314
315 if (*p <= ' ')
316 {
317 // skip whitespace (only some files need this)
318 while (*p == ' ' || *p == '\t')
319 p++;
320
321 line = p;
322 }
323
258 if (!*p) 324 if (!*p)
259 return KW_EOF; 325 {
326 kw = KW_EOF;
327 break;
328 }
260 329
261 // parse keyword 330 // parse keyword
262 while (*p > ' ') 331 while (*p > ' ')
263 p++; 332 p++;
264 333
265 int klen = p - line; 334 int klen = p - line;
266 335
336 value_nn = "";
337 value = 0;
338
267 if (*p++ != '\n') 339 if (*p++ != '\n')
268 { 340 {
269 // parse value 341 // parse value
270 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 342 while (*(unsigned char *)p <= ' ' && *p != '\n')
271 ++p; 343 ++p;
272 344
273 last_value = p; 345 value_nn = value = p;
274 346
275 while (*p != '\n') 347 while (*p != '\n')
276 p++; 348 p++;
277 349
278 *p++ = 0; 350 *p++ = 0;
279 } 351 }
280 else
281 last_value = 0;
282 352
353 ++linenum;
283 line [klen] = 0; 354 line [klen] = 0;
284 keyword_idx *kw = kw_lex::match (line, klen); 355 keyword_idx *kw_idx = kw_lex::match (line, klen);
285 356
286 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D 357 kw_str = line;
287
288 last_keyword = line;
289 line = p; 358 line = p;
290 359
291 if (kw) 360 if (kw_idx)
292 return kw->index; 361 {
362 kw = kw_idx->index;
363 break;
364 }
293 else if (!*last_keyword || *last_keyword == '#') 365 else if (!*kw_str || *kw_str == '#')
294 ; // empty/comment line 366 ; // empty/comment line
295 else 367 else
368 {
296 return KW_ERROR; 369 kw = KW_ERROR;
370 break;
371 }
297 } 372 }
298} 373}
299 374
300void 375void
301object_thawer::skip_kv (keyword kw) 376object_thawer::skip ()
302{ 377{
303 shstr ml; 378 shstr ml;
304 379
305 switch (kw) 380 switch (kw)
306 { 381 {
307 case KW_msg: get_ml (KW_endmsg , ml); break; 382 case KW_msg: get_ml (KW_endmsg , ml); break;
308 case KW_lore: get_ml (KW_endlore , ml); break; 383 case KW_lore: get_ml (KW_endlore , ml); break;
309 case KW_maplore: get_ml (KW_endmaplore, ml); break; 384 case KW_maplore: get_ml (KW_endmaplore, ml); break;
385 default: break;
310 } 386 }
387
388 next ();
311} 389}
312 390
313void 391void
314object_thawer::get (shstr &sh) const 392object_thawer::skip_block ()
315{ 393{
316 if (last_value) 394 while (kw != KW_ERROR && kw != KW_EOF)
317 sh = last_value;
318 else
319 { 395 {
320 sh = "<value missing>"; 396 keyword w = kw;
321 LOG (llevError, "keyword requires value: <%.320s>\n", line);//TODO: add filename 397 skip ();
398 if (w == KW_end)
399 break;
322 } 400 }
323} 401}
324 402
325void 403void
326object_thawer::get_ml (keyword kend, shstr &sh) 404object_thawer::get_ml (keyword kend, shstr &sh)
332 kw [0] = '\n'; 410 kw [0] = '\n';
333 memcpy (kw + 1, keyword_str [kend], klen); 411 memcpy (kw + 1, keyword_str [kend], klen);
334 kw [klen + 1] = '\n'; 412 kw [klen + 1] = '\n';
335 kw [klen + 2] = 0; 413 kw [klen + 2] = 0;
336 414
415 ++linenum;
416
337 // first test for completely empty msg... "endXXX\n" 417 // first test for completely empty msg... "endXXX\n"
338 if (!strncmp (line, kw + 1, klen + 1)) 418 if (!strncmp (line, kw + 1, klen + 1))
339 { 419 {
340 sh = 0; 420 sh = 0;
341 421
356 } 436 }
357 437
358 *end = 0; 438 *end = 0;
359 sh = line; 439 sh = line;
360 440
441 // count line numbers
442 while (line < end)
443 linenum += *line++ == '\n';
444
361 line = end + keyword_len [kend] + 1; 445 line += keyword_len [kend];
362 446
363 while (*line++ != '\n') 447 while (*line++ != '\n')
364 ; 448 ;
449
450 ++linenum;
365 } 451 }
366} 452}
367 453
368sint32 454sint32
369object_thawer::get_sint32 () const 455object_thawer::get_sint32 () const
370{ 456{
371 char *p = last_value; 457 char *p = value_nn;
372
373 if (!p)
374 return 0;
375 458
376 sint32 val = 0; 459 sint32 val = 0;
377 bool negate; 460 bool negate;
378 461
379 if (*p == '-') 462 if (*p == '-')
392 while (*p); 475 while (*p);
393 476
394 return negate ? -val : val; 477 return negate ? -val : val;
395} 478}
396 479
397sint64 480void
398object_thawer::get_sint64 () const 481object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
399{ 482{
400 return last_value ? atoll (last_value) : 0; 483 op->refcnt_inc ();
484 delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
485 delrefs.push_back (r);
486 ptr = 0;
401} 487}
402 488
403double 489void
404object_thawer::get_double () const 490object_thawer::resolve_delayed_derefs (bool deref)
405{ 491{
406 return last_value ? atof (last_value) : 0; 492 while (!delrefs.empty ())
407} 493 {
494 delayed_ref r = delrefs.back ();
495 delrefs.pop_back ();
408 496
497 if (deref)
498 *r.ptr = object::deref (r.ref);
499
500 free ((void *)r.ref);
501 r.op->refcnt_dec ();
502 }
503}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines