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 | |
25 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
22 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
26 | #include "logger.h" |
23 | #include "logger.h" |
27 | #include "cfperl.h" |
24 | #include "cfperl.h" |
28 | #include "kw_hash.h" |
25 | #include "kw_hash.h" |
29 | |
26 | |
30 | object_freezer::object_freezer () |
27 | object_freezer::object_freezer () |
31 | : dynbuf (128 * 1024, 64 * 1024) |
28 | : dynbuf_text (128 * 1024, 64 * 1024) |
32 | { |
29 | { |
33 | av = newAV (); |
30 | av = newAV (); |
34 | } |
31 | } |
35 | |
32 | |
36 | object_freezer::~object_freezer () |
33 | object_freezer::~object_freezer () |
… | |
… | |
61 | CALL_ARG_SV (newSVpv (path, 0)); |
58 | CALL_ARG_SV (newSVpv (path, 0)); |
62 | CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
59 | CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
63 | CALL_ARG_SV (newRV_inc ((SV *)av)); |
60 | CALL_ARG_SV (newRV_inc ((SV *)av)); |
64 | CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
61 | CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
65 | CALL_END; |
62 | CALL_END; |
|
|
63 | |
|
|
64 | return 1; |
66 | } |
65 | } |
67 | |
66 | |
68 | char * |
67 | char * |
69 | object_freezer::as_string () |
68 | object_freezer::as_string () |
70 | { |
69 | { |
… | |
… | |
80 | CALL_END; |
79 | CALL_END; |
81 | |
80 | |
82 | return res; |
81 | return res; |
83 | } |
82 | } |
84 | |
83 | |
85 | int |
84 | #if 0 |
|
|
85 | void |
86 | fprintf (object_freezer &freezer, const char *format, ...) |
86 | fprintf (object_freezer &freezer, const char *format, ...) |
87 | { |
87 | { |
88 | va_list ap; |
88 | va_list ap; |
89 | |
89 | |
90 | va_start (ap, format); |
90 | va_start (ap, format); |
… | |
… | |
95 | freezer.alloc (len); |
95 | freezer.alloc (len); |
96 | |
96 | |
97 | va_end (ap); |
97 | va_end (ap); |
98 | } |
98 | } |
99 | |
99 | |
100 | int |
100 | // XXX: function not returning an int |
|
|
101 | void |
101 | fputs (const char *s, object_freezer &freezer) |
102 | fputs (const char *s, object_freezer &freezer) |
102 | { |
103 | { |
103 | freezer.add (s); |
104 | freezer.add (s); |
104 | } |
105 | } |
|
|
106 | #endif |
105 | |
107 | |
106 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | |
109 | |
108 | object_thawer::object_thawer (const char *path) |
110 | object_thawer::object_thawer (const char *path) |
109 | : name (strdup (path)) |
111 | : name (strdup (path)) |
110 | { |
112 | { |
111 | static const char eof[] = "\n\n\n\0\0\0"; |
113 | static const char eof[] = "\n\n\n\0\0\0"; |
112 | |
114 | |
113 | av = 0; |
115 | av = 0; |
114 | text = 0; |
116 | text = 0; |
115 | line = 0; |
117 | line = 0; |
116 | linenum = 0; |
118 | linenum = 0; |
|
|
119 | |
|
|
120 | kw = KW_ERROR; |
|
|
121 | kw_str = 0; |
|
|
122 | value = 0; |
117 | |
123 | |
118 | if (path) |
124 | if (path) |
119 | { |
125 | { |
120 | CALL_BEGIN (1); |
126 | CALL_BEGIN (1); |
121 | CALL_ARG_SV (newSVpv (path, 0)); |
127 | CALL_ARG_SV (newSVpv (path, 0)); |
… | |
… | |
139 | SvCUR_set (text, len); |
145 | SvCUR_set (text, len); |
140 | memcpy (SvPVX (text), sv_, len); |
146 | memcpy (SvPVX (text), sv_, len); |
141 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
147 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
142 | |
148 | |
143 | line = SvPVX (text); |
149 | line = SvPVX (text); |
|
|
150 | next (); |
144 | } |
151 | } |
145 | } |
152 | } |
146 | |
153 | |
147 | CALL_END; |
154 | CALL_END; |
148 | } |
155 | } |
149 | } |
156 | } |
150 | |
157 | |
151 | object_thawer::object_thawer (const char *data, AV *perlav) |
158 | object_thawer::object_thawer (const char *data, AV *perlav) |
152 | : name (strdup ("(memory stream")) |
159 | : name (strdup ("(memory stream)")) |
153 | { |
160 | { |
154 | av = perlav; |
161 | av = perlav; |
155 | text = newSVpv (data, 0); |
162 | text = newSVpv (data, 0); |
156 | sv_catpv (text, thawer_eof); |
163 | sv_catpv (text, thawer_eof); |
157 | line = SvPVbyte_nolen (text); |
164 | line = SvPVbyte_nolen (text); |
|
|
165 | next (); |
158 | } |
166 | } |
159 | |
167 | |
160 | void |
168 | void |
161 | object_thawer::get (attachable *obj, int oid) |
169 | object_thawer::get (attachable *obj, int oid) |
162 | { |
170 | { |
… | |
… | |
165 | |
173 | |
166 | SV **svp = av_fetch ((AV *)av, oid, 0); |
174 | SV **svp = av_fetch ((AV *)av, oid, 0); |
167 | |
175 | |
168 | if (!svp || !SvROK (*svp)) |
176 | if (!svp || !SvROK (*svp)) |
169 | { |
177 | { |
170 | 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); |
171 | return; |
179 | return; |
172 | } |
180 | } |
173 | |
181 | |
174 | if (!SvROK (*svp)) |
182 | if (!SvROK (*svp)) |
175 | { |
183 | { |
176 | LOG (llevError, "deserialised perl object is not an RV"); |
184 | LOG (llevError, "deserialised perl object is not an RV\n"); |
177 | return; |
185 | return; |
178 | } |
186 | } |
179 | |
187 | |
180 | HV *hv = (HV *)SvRV (*svp); |
188 | HV *hv = (HV *)SvRV (*svp); |
181 | |
189 | |
182 | if (SvTYPE (hv) != SVt_PVHV) |
190 | if (SvTYPE (hv) != SVt_PVHV) |
183 | { |
191 | { |
184 | LOG (llevError, "deserialised perl object is not a PVHV"); |
192 | LOG (llevError, "deserialised perl object is not a PVHV\n"); |
185 | return; |
193 | return; |
186 | } |
194 | } |
187 | |
195 | |
188 | if (obj->self) |
196 | if (obj->self) |
189 | { |
197 | { |
… | |
… | |
213 | object_thawer::~object_thawer () |
221 | object_thawer::~object_thawer () |
214 | { |
222 | { |
215 | if (text) SvREFCNT_dec (text); |
223 | if (text) SvREFCNT_dec (text); |
216 | if (av) SvREFCNT_dec (av); |
224 | if (av) SvREFCNT_dec (av); |
217 | |
225 | |
|
|
226 | resolve_delayed_derefs (false); |
|
|
227 | |
218 | free ((void *)name); |
228 | free ((void *)name); |
219 | } |
229 | } |
220 | |
230 | |
221 | //TODO: remove |
231 | //TODO: remove |
222 | char * |
232 | char * |
… | |
… | |
246 | thawer.line = p; |
256 | thawer.line = p; |
247 | |
257 | |
248 | return s == q ? 0 : s; |
258 | return s == q ? 0 : s; |
249 | } |
259 | } |
250 | |
260 | |
|
|
261 | 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 | |
251 | bool |
271 | bool |
252 | object_thawer::parse_error (keyword kw, const char *type, const char *name, bool skip) |
272 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
253 | { |
273 | { |
254 | if (!type) type = "file section"; |
274 | if (!type) type = "file section"; |
255 | if (!name) name = "generic"; |
275 | if (!name) name = "generic"; |
256 | |
276 | |
257 | switch (kw) |
277 | switch (kw) |
… | |
… | |
260 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
280 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
261 | this->name, linenum, type, name); |
281 | this->name, linenum, type, name); |
262 | return false; |
282 | return false; |
263 | |
283 | |
264 | case KW_ERROR: |
284 | case KW_ERROR: |
265 | LOG (llevError, "%s:%d errornous line (%s) while reading %s '%s', aborting load.\n", |
285 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
266 | this->name, linenum, |
286 | this->name, linenum, |
267 | last_keyword ? last_keyword : "<null>", |
|
|
268 | type, name); |
287 | type, name, |
|
|
288 | kw_str ? kw_str : "<file load>"); |
269 | return false; |
289 | return false; |
270 | |
290 | |
271 | default: |
291 | default: |
272 | LOG (llevError, "%s:%d unexpected line (%s) while reading %s '%s', %s.\n", |
292 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
273 | this->name, linenum, |
293 | this->name, linenum, |
274 | last_keyword ? last_keyword : "<null>", |
294 | kw_str ? kw_str : "<null>", |
275 | last_value ? last_value : "<null>", |
295 | value ? value : "<null>", |
276 | type, name, |
296 | type, name, |
277 | skip ? "skipping line" : "aborting load"); |
297 | skip ? "skipping line" : "aborting load"); |
278 | return skip; |
298 | return skip; |
279 | } |
299 | } |
280 | } |
300 | } |
281 | |
301 | |
282 | keyword |
302 | void |
283 | object_thawer::get_kv () |
303 | object_thawer::next () |
284 | { |
304 | { |
285 | if (!line) |
305 | if (!line) |
286 | return KW_EOF; |
306 | { |
|
|
307 | kw = KW_ERROR; |
|
|
308 | return; |
|
|
309 | } |
287 | |
310 | |
288 | for (;;) |
311 | for (;;) |
289 | { |
312 | { |
290 | char *p = line; |
313 | char *p = line; |
291 | |
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 | |
292 | if (!*p) |
324 | if (!*p) |
293 | return KW_EOF; |
325 | { |
|
|
326 | kw = KW_EOF; |
|
|
327 | break; |
|
|
328 | } |
294 | |
329 | |
295 | // parse keyword |
330 | // parse keyword |
296 | while (*p > ' ') |
331 | while (*p > ' ') |
297 | p++; |
332 | p++; |
298 | |
333 | |
299 | int klen = p - line; |
334 | int klen = p - line; |
300 | |
335 | |
|
|
336 | value_nn = ""; |
|
|
337 | value = 0; |
|
|
338 | |
301 | if (*p++ != '\n') |
339 | if (*p++ != '\n') |
302 | { |
340 | { |
303 | // parse value |
341 | // parse value |
304 | while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
342 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
305 | ++p; |
343 | ++p; |
306 | |
344 | |
307 | last_value = p; |
345 | value_nn = value = p; |
308 | |
346 | |
309 | while (*p != '\n') |
347 | while (*p != '\n') |
310 | p++; |
348 | p++; |
311 | |
349 | |
312 | *p++ = 0; |
350 | *p++ = 0; |
313 | } |
351 | } |
314 | else |
|
|
315 | last_value = 0; |
|
|
316 | |
352 | |
317 | ++linenum; |
353 | ++linenum; |
318 | line [klen] = 0; |
354 | line [klen] = 0; |
319 | keyword_idx *kw = kw_lex::match (line, klen); |
355 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
320 | |
356 | |
321 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D |
357 | kw_str = line; |
322 | |
|
|
323 | last_keyword = line; |
|
|
324 | line = p; |
358 | line = p; |
325 | |
359 | |
326 | if (kw) |
360 | if (kw_idx) |
327 | return kw->index; |
361 | { |
|
|
362 | kw = kw_idx->index; |
|
|
363 | break; |
|
|
364 | } |
328 | else if (!*last_keyword || *last_keyword == '#') |
365 | else if (!*kw_str || *kw_str == '#') |
329 | ; // empty/comment line |
366 | ; // empty/comment line |
330 | else |
367 | else |
|
|
368 | { |
331 | return KW_ERROR; |
369 | kw = KW_ERROR; |
|
|
370 | break; |
|
|
371 | } |
332 | } |
372 | } |
333 | } |
373 | } |
334 | |
374 | |
335 | void |
375 | void |
336 | object_thawer::skip_kv (keyword kw) |
376 | object_thawer::skip () |
337 | { |
377 | { |
338 | shstr ml; |
378 | shstr ml; |
339 | |
379 | |
340 | switch (kw) |
380 | switch (kw) |
341 | { |
381 | { |
342 | case KW_msg: get_ml (KW_endmsg , ml); break; |
382 | case KW_msg: get_ml (KW_endmsg , ml); break; |
343 | case KW_lore: get_ml (KW_endlore , ml); break; |
383 | case KW_lore: get_ml (KW_endlore , ml); break; |
344 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
384 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
|
|
385 | default: break; |
345 | } |
386 | } |
|
|
387 | |
|
|
388 | next (); |
346 | } |
389 | } |
347 | |
390 | |
348 | void |
391 | void |
349 | object_thawer::get (shstr &sh) const |
392 | object_thawer::skip_block () |
350 | { |
393 | { |
351 | if (last_value) |
394 | while (kw != KW_EOF) |
352 | sh = last_value; |
|
|
353 | else |
|
|
354 | { |
395 | { |
355 | sh = "<value missing>"; |
396 | keyword w = kw; |
356 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", last_keyword);//TODO: add filename |
397 | skip (); |
|
|
398 | if (w == KW_end) |
|
|
399 | break; |
357 | } |
400 | } |
358 | } |
401 | } |
359 | |
402 | |
360 | void |
403 | void |
361 | object_thawer::get_ml (keyword kend, shstr &sh) |
404 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
409 | } |
452 | } |
410 | |
453 | |
411 | sint32 |
454 | sint32 |
412 | object_thawer::get_sint32 () const |
455 | object_thawer::get_sint32 () const |
413 | { |
456 | { |
414 | char *p = last_value; |
457 | char *p = value_nn; |
415 | |
|
|
416 | if (!p) |
|
|
417 | return 0; |
|
|
418 | |
458 | |
419 | sint32 val = 0; |
459 | sint32 val = 0; |
420 | bool negate; |
460 | bool negate; |
421 | |
461 | |
422 | if (*p == '-') |
462 | if (*p == '-') |
… | |
… | |
435 | while (*p); |
475 | while (*p); |
436 | |
476 | |
437 | return negate ? -val : val; |
477 | return negate ? -val : val; |
438 | } |
478 | } |
439 | |
479 | |
440 | sint64 |
480 | void |
441 | object_thawer::get_sint64 () const |
481 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
442 | { |
482 | { |
443 | 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; |
444 | } |
487 | } |
445 | |
488 | |
446 | double |
489 | void |
447 | object_thawer::get_double () const |
490 | object_thawer::resolve_delayed_derefs (bool deref) |
448 | { |
491 | { |
449 | return last_value ? atof (last_value) : 0; |
492 | while (!delrefs.empty ()) |
450 | } |
493 | { |
|
|
494 | delayed_ref r = delrefs.back (); |
|
|
495 | delrefs.pop_back (); |
451 | |
496 | |
|
|
497 | if (deref) |
|
|
498 | *r.ptr = object::deref (r.ref); |
|
|
499 | |
|
|
500 | free ((void *)r.ref); |
|
|
501 | r.op->refcnt_dec (); |
|
|
502 | } |
|
|
503 | } |