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)); |
… | |
… | |
147 | CALL_END; |
153 | CALL_END; |
148 | } |
154 | } |
149 | } |
155 | } |
150 | |
156 | |
151 | object_thawer::object_thawer (const char *data, AV *perlav) |
157 | object_thawer::object_thawer (const char *data, AV *perlav) |
152 | : name (strdup ("(memory stream")) |
158 | : name (strdup ("(memory stream)")) |
153 | { |
159 | { |
154 | av = perlav; |
160 | av = perlav; |
155 | text = newSVpv (data, 0); |
161 | text = newSVpv (data, 0); |
156 | sv_catpv (text, thawer_eof); |
162 | sv_catpv (text, thawer_eof); |
157 | line = SvPVbyte_nolen (text); |
163 | line = SvPVbyte_nolen (text); |
… | |
… | |
165 | |
171 | |
166 | SV **svp = av_fetch ((AV *)av, oid, 0); |
172 | SV **svp = av_fetch ((AV *)av, oid, 0); |
167 | |
173 | |
168 | if (!svp || !SvROK (*svp)) |
174 | if (!svp || !SvROK (*svp)) |
169 | { |
175 | { |
170 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
176 | LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
171 | return; |
177 | return; |
172 | } |
178 | } |
173 | |
179 | |
174 | if (!SvROK (*svp)) |
180 | if (!SvROK (*svp)) |
175 | { |
181 | { |
176 | LOG (llevError, "deserialised perl object is not an RV"); |
182 | LOG (llevError, "deserialised perl object is not an RV\n"); |
177 | return; |
183 | return; |
178 | } |
184 | } |
179 | |
185 | |
180 | HV *hv = (HV *)SvRV (*svp); |
186 | HV *hv = (HV *)SvRV (*svp); |
181 | |
187 | |
182 | if (SvTYPE (hv) != SVt_PVHV) |
188 | if (SvTYPE (hv) != SVt_PVHV) |
183 | { |
189 | { |
184 | LOG (llevError, "deserialised perl object is not a PVHV"); |
190 | LOG (llevError, "deserialised perl object is not a PVHV\n"); |
185 | return; |
191 | return; |
186 | } |
192 | } |
187 | |
193 | |
188 | if (obj->self) |
194 | if (obj->self) |
189 | { |
195 | { |
… | |
… | |
247 | |
253 | |
248 | return s == q ? 0 : s; |
254 | return s == q ? 0 : s; |
249 | } |
255 | } |
250 | |
256 | |
251 | bool |
257 | bool |
252 | object_thawer::parse_error (keyword kw, const char *type, const char *name, bool skip) |
258 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
253 | { |
259 | { |
254 | if (!type) type = "file section"; |
260 | if (!type) type = "file section"; |
255 | if (!name) name = "generic"; |
261 | if (!name) name = "generic"; |
256 | |
262 | |
257 | switch (kw) |
263 | switch (kw) |
… | |
… | |
260 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
266 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
261 | this->name, linenum, type, name); |
267 | this->name, linenum, type, name); |
262 | return false; |
268 | return false; |
263 | |
269 | |
264 | case KW_ERROR: |
270 | case KW_ERROR: |
265 | LOG (llevError, "%s:%d errornous line (%s) while reading %s '%s', aborting load.\n", |
271 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
266 | this->name, linenum, |
272 | this->name, linenum, |
267 | last_keyword ? last_keyword : "<null>", |
|
|
268 | type, name); |
273 | type, name, |
|
|
274 | kw_str ? kw_str : "<file load>"); |
269 | return false; |
275 | return false; |
270 | |
276 | |
271 | default: |
277 | default: |
272 | LOG (llevError, "%s:%d unexpected line (%s) while reading %s '%s', %s.\n", |
278 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
273 | this->name, linenum, |
279 | this->name, linenum, |
274 | last_keyword ? last_keyword : "<null>", |
280 | kw_str ? kw_str : "<null>", |
275 | last_value ? last_value : "<null>", |
281 | value ? value : "<null>", |
276 | type, name, |
282 | type, name, |
277 | skip ? "skipping line" : "aborting load"); |
283 | skip ? "skipping line" : "aborting load"); |
278 | return skip; |
284 | return skip; |
279 | } |
285 | } |
280 | } |
286 | } |
281 | |
287 | |
282 | keyword |
288 | void |
283 | object_thawer::get_kv () |
289 | object_thawer::next () |
284 | { |
290 | { |
285 | if (!line) |
291 | if (!line) |
286 | return KW_EOF; |
292 | { |
|
|
293 | kw = KW_ERROR; |
|
|
294 | return; |
|
|
295 | } |
287 | |
296 | |
288 | for (;;) |
297 | for (;;) |
289 | { |
298 | { |
290 | char *p = line; |
299 | char *p = line; |
291 | |
300 | |
|
|
301 | if (*p <= ' ') |
|
|
302 | { |
|
|
303 | // skip whitespace (only some files need this) |
|
|
304 | while (*p == ' ' || *p == '\t') |
|
|
305 | p++; |
|
|
306 | |
|
|
307 | line = p; |
|
|
308 | } |
|
|
309 | |
292 | if (!*p) |
310 | if (!*p) |
293 | return KW_EOF; |
311 | { |
|
|
312 | kw = KW_EOF; |
|
|
313 | break; |
|
|
314 | } |
294 | |
315 | |
295 | // parse keyword |
316 | // parse keyword |
296 | while (*p > ' ') |
317 | while (*p > ' ') |
297 | p++; |
318 | p++; |
298 | |
319 | |
299 | int klen = p - line; |
320 | int klen = p - line; |
300 | |
321 | |
|
|
322 | value_nn = ""; |
|
|
323 | value = 0; |
|
|
324 | |
301 | if (*p++ != '\n') |
325 | if (*p++ != '\n') |
302 | { |
326 | { |
303 | // parse value |
327 | // parse value |
304 | while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
328 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
305 | ++p; |
329 | ++p; |
306 | |
330 | |
307 | last_value = p; |
331 | value_nn = value = p; |
308 | |
332 | |
309 | while (*p != '\n') |
333 | while (*p != '\n') |
310 | p++; |
334 | p++; |
311 | |
335 | |
312 | *p++ = 0; |
336 | *p++ = 0; |
313 | } |
337 | } |
314 | else |
|
|
315 | last_value = 0; |
|
|
316 | |
338 | |
317 | ++linenum; |
339 | ++linenum; |
318 | line [klen] = 0; |
340 | line [klen] = 0; |
319 | keyword_idx *kw = kw_lex::match (line, klen); |
341 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
320 | |
342 | |
321 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D |
343 | kw_str = line; |
322 | |
|
|
323 | last_keyword = line; |
|
|
324 | line = p; |
344 | line = p; |
325 | |
345 | |
326 | if (kw) |
346 | if (kw_idx) |
327 | return kw->index; |
347 | { |
|
|
348 | kw = kw_idx->index; |
|
|
349 | break; |
|
|
350 | } |
328 | else if (!*last_keyword || *last_keyword == '#') |
351 | else if (!*kw_str || *kw_str == '#') |
329 | ; // empty/comment line |
352 | ; // empty/comment line |
330 | else |
353 | else |
|
|
354 | { |
331 | return KW_ERROR; |
355 | kw = KW_ERROR; |
|
|
356 | break; |
|
|
357 | } |
332 | } |
358 | } |
333 | } |
359 | } |
334 | |
360 | |
335 | void |
361 | void |
336 | object_thawer::skip_kv (keyword kw) |
362 | object_thawer::skip () |
337 | { |
363 | { |
338 | shstr ml; |
364 | shstr ml; |
339 | |
365 | |
340 | switch (kw) |
366 | switch (kw) |
341 | { |
367 | { |
342 | case KW_msg: get_ml (KW_endmsg , ml); break; |
368 | case KW_msg: get_ml (KW_endmsg , ml); break; |
343 | case KW_lore: get_ml (KW_endlore , ml); break; |
369 | case KW_lore: get_ml (KW_endlore , ml); break; |
344 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
370 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
|
|
371 | default: break; |
345 | } |
372 | } |
|
|
373 | |
|
|
374 | next (); |
346 | } |
375 | } |
347 | |
376 | |
348 | void |
|
|
349 | object_thawer::get (shstr &sh) const |
|
|
350 | { |
|
|
351 | if (last_value) |
|
|
352 | sh = last_value; |
|
|
353 | else |
|
|
354 | { |
|
|
355 | sh = "<value missing>"; |
|
|
356 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", last_keyword);//TODO: add filename |
|
|
357 | } |
|
|
358 | } |
|
|
359 | |
|
|
360 | void |
377 | void |
361 | object_thawer::get_ml (keyword kend, shstr &sh) |
378 | object_thawer::get_ml (keyword kend, shstr &sh) |
362 | { |
379 | { |
363 | char kw[128]; |
380 | char kw[128]; |
364 | |
381 | |
… | |
… | |
409 | } |
426 | } |
410 | |
427 | |
411 | sint32 |
428 | sint32 |
412 | object_thawer::get_sint32 () const |
429 | object_thawer::get_sint32 () const |
413 | { |
430 | { |
414 | char *p = last_value; |
431 | char *p = value_nn; |
415 | |
|
|
416 | if (!p) |
|
|
417 | return 0; |
|
|
418 | |
432 | |
419 | sint32 val = 0; |
433 | sint32 val = 0; |
420 | bool negate; |
434 | bool negate; |
421 | |
435 | |
422 | if (*p == '-') |
436 | if (*p == '-') |
… | |
… | |
435 | while (*p); |
449 | while (*p); |
436 | |
450 | |
437 | return negate ? -val : val; |
451 | return negate ? -val : val; |
438 | } |
452 | } |
439 | |
453 | |
440 | sint64 |
|
|
441 | object_thawer::get_sint64 () const |
|
|
442 | { |
|
|
443 | return last_value ? atoll (last_value) : 0; |
|
|
444 | } |
|
|
445 | |
|
|
446 | double |
|
|
447 | object_thawer::get_double () const |
|
|
448 | { |
|
|
449 | return last_value ? atof (last_value) : 0; |
|
|
450 | } |
|
|
451 | |
|
|