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 () |
… | |
… | |
82 | CALL_END; |
79 | CALL_END; |
83 | |
80 | |
84 | return res; |
81 | return res; |
85 | } |
82 | } |
86 | |
83 | |
|
|
84 | #if 0 |
87 | void |
85 | void |
88 | fprintf (object_freezer &freezer, const char *format, ...) |
86 | fprintf (object_freezer &freezer, const char *format, ...) |
89 | { |
87 | { |
90 | va_list ap; |
88 | va_list ap; |
91 | |
89 | |
… | |
… | |
103 | void |
101 | void |
104 | fputs (const char *s, object_freezer &freezer) |
102 | fputs (const char *s, object_freezer &freezer) |
105 | { |
103 | { |
106 | freezer.add (s); |
104 | freezer.add (s); |
107 | } |
105 | } |
|
|
106 | #endif |
108 | |
107 | |
109 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
110 | |
109 | |
111 | object_thawer::object_thawer (const char *path) |
110 | object_thawer::object_thawer (const char *path) |
112 | : name (strdup (path)) |
111 | : name (strdup (path)) |
… | |
… | |
146 | SvCUR_set (text, len); |
145 | SvCUR_set (text, len); |
147 | memcpy (SvPVX (text), sv_, len); |
146 | memcpy (SvPVX (text), sv_, len); |
148 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
147 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
149 | |
148 | |
150 | line = SvPVX (text); |
149 | line = SvPVX (text); |
|
|
150 | next (); |
151 | } |
151 | } |
152 | } |
152 | } |
153 | |
153 | |
154 | CALL_END; |
154 | CALL_END; |
155 | } |
155 | } |
156 | } |
156 | } |
157 | |
157 | |
158 | object_thawer::object_thawer (const char *data, AV *perlav) |
158 | object_thawer::object_thawer (const char *data, AV *perlav) |
159 | : name (strdup ("(memory stream")) |
159 | : name (strdup ("(memory stream)")) |
160 | { |
160 | { |
161 | av = perlav; |
161 | av = perlav; |
162 | text = newSVpv (data, 0); |
162 | text = newSVpv (data, 0); |
163 | sv_catpv (text, thawer_eof); |
163 | sv_catpv (text, thawer_eof); |
164 | line = SvPVbyte_nolen (text); |
164 | line = SvPVbyte_nolen (text); |
|
|
165 | next (); |
165 | } |
166 | } |
166 | |
167 | |
167 | void |
168 | void |
168 | object_thawer::get (attachable *obj, int oid) |
169 | object_thawer::get (attachable *obj, int oid) |
169 | { |
170 | { |
… | |
… | |
172 | |
173 | |
173 | SV **svp = av_fetch ((AV *)av, oid, 0); |
174 | SV **svp = av_fetch ((AV *)av, oid, 0); |
174 | |
175 | |
175 | if (!svp || !SvROK (*svp)) |
176 | if (!svp || !SvROK (*svp)) |
176 | { |
177 | { |
177 | 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); |
178 | return; |
179 | return; |
179 | } |
180 | } |
180 | |
181 | |
181 | if (!SvROK (*svp)) |
182 | if (!SvROK (*svp)) |
182 | { |
183 | { |
183 | LOG (llevError, "deserialised perl object is not an RV"); |
184 | LOG (llevError, "deserialised perl object is not an RV\n"); |
184 | return; |
185 | return; |
185 | } |
186 | } |
186 | |
187 | |
187 | HV *hv = (HV *)SvRV (*svp); |
188 | HV *hv = (HV *)SvRV (*svp); |
188 | |
189 | |
189 | if (SvTYPE (hv) != SVt_PVHV) |
190 | if (SvTYPE (hv) != SVt_PVHV) |
190 | { |
191 | { |
191 | LOG (llevError, "deserialised perl object is not a PVHV"); |
192 | LOG (llevError, "deserialised perl object is not a PVHV\n"); |
192 | return; |
193 | return; |
193 | } |
194 | } |
194 | |
195 | |
195 | if (obj->self) |
196 | if (obj->self) |
196 | { |
197 | { |
… | |
… | |
220 | object_thawer::~object_thawer () |
221 | object_thawer::~object_thawer () |
221 | { |
222 | { |
222 | if (text) SvREFCNT_dec (text); |
223 | if (text) SvREFCNT_dec (text); |
223 | if (av) SvREFCNT_dec (av); |
224 | if (av) SvREFCNT_dec (av); |
224 | |
225 | |
|
|
226 | resolve_delayed_derefs (false); |
|
|
227 | |
225 | free ((void *)name); |
228 | free ((void *)name); |
226 | } |
229 | } |
227 | |
230 | |
228 | //TODO: remove |
231 | //TODO: remove |
229 | char * |
232 | char * |
… | |
… | |
251 | |
254 | |
252 | *q = 0; |
255 | *q = 0; |
253 | thawer.line = p; |
256 | thawer.line = p; |
254 | |
257 | |
255 | return s == q ? 0 : s; |
258 | return s == q ? 0 : s; |
|
|
259 | } |
|
|
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); |
256 | } |
269 | } |
257 | |
270 | |
258 | bool |
271 | bool |
259 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
272 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
260 | { |
273 | { |
… | |
… | |
277 | |
290 | |
278 | default: |
291 | default: |
279 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
292 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
280 | this->name, linenum, |
293 | this->name, linenum, |
281 | kw_str ? kw_str : "<null>", |
294 | kw_str ? kw_str : "<null>", |
282 | value ? value : "<null>", |
295 | value ? value : "<null>", |
283 | type, name, |
296 | type, name, |
284 | skip ? "skipping line" : "aborting load"); |
297 | skip ? "skipping line" : "aborting load"); |
285 | return skip; |
298 | return skip; |
286 | } |
299 | } |
287 | } |
300 | } |
… | |
… | |
297 | |
310 | |
298 | for (;;) |
311 | for (;;) |
299 | { |
312 | { |
300 | char *p = line; |
313 | char *p = line; |
301 | |
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 | |
302 | if (!*p) |
324 | if (!*p) |
303 | { |
325 | { |
304 | kw = KW_EOF; |
326 | kw = KW_EOF; |
305 | break; |
327 | break; |
306 | } |
328 | } |
… | |
… | |
309 | while (*p > ' ') |
331 | while (*p > ' ') |
310 | p++; |
332 | p++; |
311 | |
333 | |
312 | int klen = p - line; |
334 | int klen = p - line; |
313 | |
335 | |
|
|
336 | value_nn = ""; |
|
|
337 | value = 0; |
|
|
338 | |
314 | if (*p++ != '\n') |
339 | if (*p++ != '\n') |
315 | { |
340 | { |
316 | // parse value |
341 | // parse value |
317 | while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
342 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
318 | ++p; |
343 | ++p; |
319 | |
344 | |
320 | value = p; |
345 | value_nn = value = p; |
321 | |
346 | |
322 | while (*p != '\n') |
347 | while (*p != '\n') |
323 | p++; |
348 | p++; |
324 | |
349 | |
325 | *p++ = 0; |
350 | *p++ = 0; |
326 | } |
351 | } |
327 | else |
|
|
328 | value = 0; |
|
|
329 | |
352 | |
330 | ++linenum; |
353 | ++linenum; |
331 | line [klen] = 0; |
354 | line [klen] = 0; |
332 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
355 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
333 | |
356 | |
334 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D |
|
|
335 | |
|
|
336 | kw_str = line; |
357 | kw_str = line; |
337 | line = p; |
358 | line = p; |
338 | |
359 | |
339 | if (kw_idx) |
360 | if (kw_idx) |
340 | { |
361 | { |
… | |
… | |
359 | switch (kw) |
380 | switch (kw) |
360 | { |
381 | { |
361 | case KW_msg: get_ml (KW_endmsg , ml); break; |
382 | case KW_msg: get_ml (KW_endmsg , ml); break; |
362 | case KW_lore: get_ml (KW_endlore , ml); break; |
383 | case KW_lore: get_ml (KW_endlore , ml); break; |
363 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
384 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
|
|
385 | default: break; |
364 | } |
386 | } |
365 | |
387 | |
366 | next (); |
388 | next (); |
367 | } |
389 | } |
368 | |
390 | |
369 | void |
391 | void |
370 | object_thawer::get (shstr &sh) const |
392 | object_thawer::skip_block () |
371 | { |
393 | { |
372 | if (value) |
394 | while (kw != KW_ERROR && kw != KW_EOF) |
373 | sh = value; |
|
|
374 | else |
|
|
375 | { |
395 | { |
376 | sh = "<value missing>"; |
396 | keyword w = kw; |
377 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename |
397 | skip (); |
|
|
398 | if (w == KW_end) |
|
|
399 | break; |
378 | } |
400 | } |
379 | } |
401 | } |
380 | |
402 | |
381 | void |
403 | void |
382 | object_thawer::get_ml (keyword kend, shstr &sh) |
404 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
430 | } |
452 | } |
431 | |
453 | |
432 | sint32 |
454 | sint32 |
433 | object_thawer::get_sint32 () const |
455 | object_thawer::get_sint32 () const |
434 | { |
456 | { |
435 | char *p = value; |
457 | char *p = value_nn; |
436 | |
|
|
437 | if (!p) |
|
|
438 | return 0; |
|
|
439 | |
458 | |
440 | sint32 val = 0; |
459 | sint32 val = 0; |
441 | bool negate; |
460 | bool negate; |
442 | |
461 | |
443 | if (*p == '-') |
462 | if (*p == '-') |
… | |
… | |
456 | while (*p); |
475 | while (*p); |
457 | |
476 | |
458 | return negate ? -val : val; |
477 | return negate ? -val : val; |
459 | } |
478 | } |
460 | |
479 | |
461 | sint64 |
480 | void |
462 | object_thawer::get_sint64 () const |
481 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
463 | { |
482 | { |
464 | return value ? atoll (value) : 0; |
483 | op->refcnt_inc (); |
|
|
484 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
485 | delrefs.push_back (r); |
|
|
486 | ptr = 0; |
465 | } |
487 | } |
466 | |
488 | |
467 | double |
489 | void |
468 | object_thawer::get_double () const |
490 | object_thawer::resolve_delayed_derefs (bool deref) |
469 | { |
491 | { |
470 | return value ? atof (value) : 0; |
492 | while (!delrefs.empty ()) |
471 | } |
493 | { |
|
|
494 | delayed_ref r = delrefs.back (); |
|
|
495 | delrefs.pop_back (); |
472 | |
496 | |
|
|
497 | if (deref) |
|
|
498 | *r.ptr = object::deref (r.ref); |
|
|
499 | |
|
|
500 | free ((void *)r.ref); |
|
|
501 | r.op->refcnt_dec (); |
|
|
502 | } |
|
|
503 | } |