ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/freezethaw.C
Revision: 1.43
Committed: Fri May 21 00:31:14 2010 UTC (14 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.42: +39 -35 lines
Log Message:
*** empty log message ***

File Contents

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