ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/freezethaw.C
Revision: 1.27
Committed: Thu Sep 6 06:53:09 2007 UTC (16 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.26: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2 root 1.21 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3     *
4     * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5     *
6 root 1.22 * Crossfire TRT is free software: you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10 root 1.21 *
11 root 1.22 * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15 root 1.21 *
16 root 1.22 * You should have received a copy of the GNU General Public License
17     * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 root 1.21 *
19     * The authors can be reached via e-mail to <crossfire@schmorp.de>
20 root 1.1 */
21    
22 root 1.4 #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
23 root 1.1 #include "logger.h"
24     #include "cfperl.h"
25     #include "kw_hash.h"
26    
27     object_freezer::object_freezer ()
28 root 1.17 : dynbuf_text (128 * 1024, 64 * 1024)
29 root 1.1 {
30     av = newAV ();
31     }
32    
33     object_freezer::~object_freezer ()
34     {
35     SvREFCNT_dec (av);
36     }
37    
38 root 1.4 void
39     object_freezer::put (attachable *ext)
40 root 1.1 {
41     ext->optimise ();
42    
43     if (ext->self)
44     {
45     int idx = AvFILLp ((AV *)av) + 1;
46     av_store (av, idx, newRV_inc ((SV *)ext->self));
47    
48     add ((void *)"oid ", 4);
49     add ((sint32)idx);
50     add ('\n');
51     }
52     }
53    
54 root 1.4 bool
55     object_freezer::save (const char *path)
56 root 1.1 {
57     CALL_BEGIN (3);
58 root 1.4 CALL_ARG_SV (newSVpv (path, 0));
59 root 1.1 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
60     CALL_ARG_SV (newRV_inc ((SV *)av));
61     CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
62     CALL_END;
63 root 1.13
64     return 1;
65 root 1.1 }
66    
67 root 1.4 char *
68     object_freezer::as_string ()
69 root 1.1 {
70     CALL_BEGIN (2);
71     CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
72     CALL_ARG_SV (newRV_inc ((SV *)av));
73     CALL_CALL ("cf::object_freezer_as_string", G_SCALAR);
74    
75     char *res = count > 0
76 root 1.3 ? strdup (SvPVX (POPs))
77 root 1.1 : strdup ("[fatal error]");
78    
79     CALL_END;
80    
81     return res;
82     }
83    
84 root 1.20 #if 0
85 root 1.13 void
86 root 1.4 fprintf (object_freezer &freezer, const char *format, ...)
87 root 1.1 {
88     va_list ap;
89    
90     va_start (ap, format);
91    
92     int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap);
93    
94     if (len >= 0)
95     freezer.alloc (len);
96    
97     va_end (ap);
98     }
99    
100 pippijn 1.12 // XXX: function not returning an int
101 root 1.13 void
102 root 1.4 fputs (const char *s, object_freezer &freezer)
103 root 1.1 {
104     freezer.add (s);
105     }
106 root 1.20 #endif
107 root 1.1
108     static const char thawer_eof[] = "\n\n\n\0\0\0";
109    
110 root 1.4 object_thawer::object_thawer (const char *path)
111     : name (strdup (path))
112 root 1.1 {
113     static const char eof[] = "\n\n\n\0\0\0";
114    
115 root 1.10 av = 0;
116     text = 0;
117     line = 0;
118 root 1.8 linenum = 0;
119 root 1.1
120 root 1.10 kw = KW_ERROR;
121     kw_str = 0;
122     value = 0;
123    
124 root 1.4 if (path)
125 root 1.1 {
126     CALL_BEGIN (1);
127 root 1.4 CALL_ARG_SV (newSVpv (path, 0));
128 root 1.1 CALL_CALL ("cf::object_thawer_load", G_ARRAY);
129    
130     if (count == 2)
131     {
132     // second value - perl objects
133     {
134     SV *sv = POPs;
135     if (SvROK (sv))
136     av = (AV *)SvREFCNT_inc (SvRV (sv));
137     }
138    
139     // first value - text part, pad with 3 zeroes
140     {
141     SV *sv = POPs;
142     STRLEN len;
143     char *sv_ = SvPVbyte (sv, len);
144     text = newSV (len + sizeof (eof));
145     SvCUR_set (text, len);
146     memcpy (SvPVX (text), sv_, len);
147     memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
148    
149     line = SvPVX (text);
150 root 1.25 next ();
151 root 1.1 }
152     }
153    
154     CALL_END;
155     }
156     }
157    
158     object_thawer::object_thawer (const char *data, AV *perlav)
159 root 1.23 : name (strdup ("(memory stream)"))
160 root 1.1 {
161     av = perlav;
162     text = newSVpv (data, 0);
163     sv_catpv (text, thawer_eof);
164     line = SvPVbyte_nolen (text);
165 root 1.25 next ();
166 root 1.1 }
167    
168 root 1.4 void
169     object_thawer::get (attachable *obj, int oid)
170 root 1.1 {
171     if (!av || oid < 0) // this is actually an error of sorts
172     return;
173    
174     SV **svp = av_fetch ((AV *)av, oid, 0);
175    
176     if (!svp || !SvROK (*svp))
177     {
178 root 1.15 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
179 root 1.1 return;
180     }
181    
182     if (!SvROK (*svp))
183     {
184 root 1.15 LOG (llevError, "deserialised perl object is not an RV\n");
185 root 1.1 return;
186     }
187    
188     HV *hv = (HV *)SvRV (*svp);
189    
190     if (SvTYPE (hv) != SVt_PVHV)
191     {
192 root 1.15 LOG (llevError, "deserialised perl object is not a PVHV\n");
193 root 1.1 return;
194     }
195    
196     if (obj->self)
197     {
198     // the hard way(?)
199    
200 root 1.6 CALL_BEGIN (2);
201     CALL_ARG_SV (newRV_inc ((SV *)obj->self));
202     CALL_ARG_SV (newRV_inc ((SV *)hv));
203     PUTBACK;
204     call_method ("thawer_merge", G_DISCARD | G_EVAL);
205     SPAGAIN;
206     CALL_END;
207 root 1.1 }
208     else
209     {
210     // the easy way(?)
211    
212     obj->self = hv;
213     SvRV_set (*svp, &PL_sv_undef);
214    
215     sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
216     }
217    
218     obj->reattach ();
219     }
220    
221     object_thawer::~object_thawer ()
222     {
223     if (text) SvREFCNT_dec (text);
224     if (av) SvREFCNT_dec (av);
225 root 1.4
226 root 1.25 resolve_delayed_derefs (false);
227    
228 root 1.4 free ((void *)name);
229 root 1.1 }
230    
231 root 1.8 //TODO: remove
232 root 1.4 char *
233     fgets (char *s, int n, object_thawer &thawer)
234 root 1.1 {
235     char *p = thawer.line;
236     char *q = s;
237    
238     if (!p)
239     return 0;
240    
241     while (--n)
242     {
243     if (!*p)
244     break;
245    
246     *q++ = *p;
247    
248     if (*p++ == '\n')
249 root 1.8 {
250     ++thawer.linenum;
251     break;
252     }
253 root 1.1 }
254    
255     *q = 0;
256     thawer.line = p;
257    
258     return s == q ? 0 : s;
259     }
260    
261 root 1.24 void
262     object_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    
271 root 1.8 bool
272 root 1.10 object_thawer::parse_error (const char *type, const char *name, bool skip)
273 root 1.8 {
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 root 1.10 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
286 root 1.8 this->name, linenum,
287 root 1.10 type, name,
288     kw_str ? kw_str : "<file load>");
289 root 1.8 return false;
290    
291     default:
292 root 1.9 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
293 root 1.8 this->name, linenum,
294 root 1.10 kw_str ? kw_str : "<null>",
295 root 1.24 value ? value : "<null>",
296 root 1.8 type, name,
297     skip ? "skipping line" : "aborting load");
298     return skip;
299     }
300     }
301    
302 root 1.10 void
303 root 1.11 object_thawer::next ()
304 root 1.1 {
305     if (!line)
306 root 1.10 {
307     kw = KW_ERROR;
308     return;
309     }
310 root 1.1
311     for (;;)
312     {
313     char *p = line;
314    
315 root 1.16 if (*p <= ' ')
316     {
317     // skip whitespace (only some files need this)
318     while (*p == ' ' || *p == '\t')
319     p++;
320    
321     line = p;
322     }
323    
324 root 1.1 if (!*p)
325 root 1.10 {
326     kw = KW_EOF;
327     break;
328     }
329 root 1.1
330     // parse keyword
331     while (*p > ' ')
332     p++;
333    
334     int klen = p - line;
335    
336 root 1.19 value_nn = "";
337 root 1.16 value = 0;
338    
339 root 1.1 if (*p++ != '\n')
340     {
341     // parse value
342 root 1.16 while (*(unsigned char *)p <= ' ' && *p != '\n')
343 root 1.1 ++p;
344    
345 root 1.19 value_nn = value = p;
346 root 1.1
347     while (*p != '\n')
348     p++;
349    
350     *p++ = 0;
351     }
352    
353 root 1.8 ++linenum;
354 root 1.1 line [klen] = 0;
355 root 1.10 keyword_idx *kw_idx = kw_lex::match (line, klen);
356 root 1.1
357 root 1.10 kw_str = line;
358 root 1.1 line = p;
359    
360 root 1.10 if (kw_idx)
361     {
362     kw = kw_idx->index;
363     break;
364     }
365     else if (!*kw_str || *kw_str == '#')
366 root 1.1 ; // empty/comment line
367     else
368 root 1.10 {
369     kw = KW_ERROR;
370     break;
371     }
372 root 1.1 }
373     }
374    
375 root 1.4 void
376 root 1.11 object_thawer::skip ()
377 root 1.1 {
378     shstr ml;
379    
380     switch (kw)
381     {
382     case KW_msg: get_ml (KW_endmsg , ml); break;
383     case KW_lore: get_ml (KW_endlore , ml); break;
384     case KW_maplore: get_ml (KW_endmaplore, ml); break;
385 pippijn 1.14 default: break;
386 root 1.1 }
387 root 1.10
388 root 1.11 next ();
389 root 1.1 }
390    
391 root 1.4 void
392 root 1.25 object_thawer::skip_block ()
393     {
394 root 1.27 // must not stop at KW_ERROR, as those signify custom keys
395 root 1.26 while (kw != KW_EOF)
396 root 1.25 {
397     keyword w = kw;
398     skip ();
399     if (w == KW_end)
400     break;
401     }
402     }
403    
404     void
405 root 1.4 object_thawer::get_ml (keyword kend, shstr &sh)
406 root 1.1 {
407     char kw[128];
408    
409     int klen = keyword_len [kend];
410    
411     kw [0] = '\n';
412     memcpy (kw + 1, keyword_str [kend], klen);
413     kw [klen + 1] = '\n';
414     kw [klen + 2] = 0;
415    
416 root 1.8 ++linenum;
417    
418 root 1.1 // first test for completely empty msg... "endXXX\n"
419     if (!strncmp (line, kw + 1, klen + 1))
420     {
421     sh = 0;
422    
423     line += klen + 1;
424    
425     return;
426     }
427     else
428     {
429     // multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL)
430    
431     char *end = strstr (line, kw);
432    
433     if (!end)
434     {
435     sh = 0;
436     return;
437     }
438    
439     *end = 0;
440     sh = line;
441    
442 root 1.8 // count line numbers
443     while (line < end)
444     linenum += *line++ == '\n';
445    
446     line += keyword_len [kend];
447 root 1.1
448     while (*line++ != '\n')
449     ;
450 root 1.8
451     ++linenum;
452 root 1.1 }
453     }
454    
455 root 1.4 sint32
456     object_thawer::get_sint32 () const
457 root 1.1 {
458 root 1.19 char *p = value_nn;
459 root 1.1
460     sint32 val = 0;
461     bool negate;
462    
463     if (*p == '-')
464     {
465     negate = true;
466     ++p;
467     }
468     else
469     negate = false;
470    
471     do
472     {
473     val *= 10;
474     val += *p++ - '0';
475     }
476     while (*p);
477    
478     return negate ? -val : val;
479     }
480    
481 root 1.25 void
482     object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
483     {
484     op->refcnt_inc ();
485     delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
486     delrefs.push_back (r);
487     ptr = 0;
488     }
489    
490     void
491     object_thawer::resolve_delayed_derefs (bool deref)
492     {
493     while (!delrefs.empty ())
494     {
495     delayed_ref r = delrefs.back ();
496     delrefs.pop_back ();
497    
498     if (deref)
499     *r.ptr = object::deref (r.ref);
500    
501     free ((void *)r.ref);
502     r.op->refcnt_dec ();
503     }
504     }