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