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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines