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