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 Deliantra, the Roguelike Realtime MMORPG. |
7 | * |
3 | * |
8 | * Copyright (C) 2006 by Marc Lehmann <crossfire@schmorp.de> |
4 | * Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
9 | * |
5 | * |
10 | * This program is free software; you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
11 | * it under the terms of the GNU General Public License as published by |
7 | * the terms of the Affero GNU General Public License as published by the |
12 | * the Free Software Foundation; either version 2 of the License, or |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
13 | * (at your option) any later version. |
9 | * 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 Affero GNU General Public License |
21 | * along with this program; if not, write to the Free Software |
17 | * and the GNU General Public License along with this program. If not, see |
22 | * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | * <http://www.gnu.org/licenses/>. |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
21 | */ |
24 | |
22 | |
25 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
23 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
26 | #include "logger.h" |
24 | #include "logger.h" |
27 | #include "cfperl.h" |
25 | #include "cfperl.h" |
28 | #include "kw_hash.h" |
26 | #include "kw_hash.h" |
29 | |
27 | |
30 | object_freezer::object_freezer () |
28 | object_freezer::object_freezer () |
31 | : dynbuf (128 * 1024, 64 * 1024) |
29 | : dynbuf_text (128 * 1024, 64 * 1024) |
32 | { |
30 | { |
33 | av = newAV (); |
31 | av = newAV (); |
34 | } |
32 | } |
35 | |
33 | |
36 | object_freezer::~object_freezer () |
34 | object_freezer::~object_freezer () |
37 | { |
35 | { |
38 | SvREFCNT_dec (av); |
36 | SvREFCNT_dec (av); |
39 | } |
37 | } |
40 | |
38 | |
41 | void |
39 | void |
42 | object_freezer::put (attachable *ext) |
40 | object_freezer::put_ (attachable *ext) |
43 | { |
41 | { |
44 | ext->optimise (); |
42 | ext->optimise (); |
45 | |
43 | |
46 | if (ext->self) |
44 | if (ext->self) |
47 | { |
45 | { |
48 | int idx = AvFILLp ((AV *)av) + 1; |
46 | int idx = AvFILLp ((AV *)av) + 1; |
49 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
47 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
50 | |
48 | |
51 | add ((void *)"oid ", 4); |
49 | put (KW(oid), sint32(idx)); |
52 | add ((sint32)idx); |
|
|
53 | add ('\n'); |
|
|
54 | } |
50 | } |
55 | } |
51 | } |
56 | |
52 | |
57 | bool |
53 | bool |
58 | object_freezer::save (const char *path) |
54 | object_freezer::save (const char *path) |
… | |
… | |
82 | CALL_END; |
78 | CALL_END; |
83 | |
79 | |
84 | return res; |
80 | return res; |
85 | } |
81 | } |
86 | |
82 | |
|
|
83 | #if 0 |
87 | void |
84 | void |
88 | fprintf (object_freezer &freezer, const char *format, ...) |
85 | fprintf (object_freezer &freezer, const char *format, ...) |
89 | { |
86 | { |
90 | va_list ap; |
87 | va_list ap; |
91 | |
88 | |
… | |
… | |
103 | void |
100 | void |
104 | fputs (const char *s, object_freezer &freezer) |
101 | fputs (const char *s, object_freezer &freezer) |
105 | { |
102 | { |
106 | freezer.add (s); |
103 | freezer.add (s); |
107 | } |
104 | } |
|
|
105 | #endif |
108 | |
106 | |
109 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
|
|
108 | |
|
|
109 | bool object_thawer::errors_are_fatal = true; |
110 | |
110 | |
111 | object_thawer::object_thawer (const char *path) |
111 | object_thawer::object_thawer (const char *path) |
112 | : name (strdup (path)) |
112 | : name (strdup (path)) |
113 | { |
113 | { |
114 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
115 | |
|
|
116 | av = 0; |
114 | av = 0; |
117 | text = 0; |
115 | text = 0; |
118 | line = 0; |
116 | line = 0; |
119 | linenum = 0; |
117 | linenum = 0; |
120 | |
118 | |
… | |
… | |
140 | // first value - text part, pad with 3 zeroes |
138 | // first value - text part, pad with 3 zeroes |
141 | { |
139 | { |
142 | SV *sv = POPs; |
140 | SV *sv = POPs; |
143 | STRLEN len; |
141 | STRLEN len; |
144 | char *sv_ = SvPVbyte (sv, len); |
142 | char *sv_ = SvPVbyte (sv, len); |
145 | text = newSV (len + sizeof (eof)); |
143 | text = newSV (len + sizeof (thawer_eof)); |
146 | SvCUR_set (text, len); |
144 | SvCUR_set (text, len + sizeof (thawer_eof)); |
147 | memcpy (SvPVX (text), sv_, len); |
145 | memcpy (SvPVX (text), sv_, len); |
148 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
146 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
149 | |
147 | |
150 | line = SvPVX (text); |
148 | line = SvPVX (text); |
|
|
149 | next (); |
151 | } |
150 | } |
152 | } |
151 | } |
153 | |
152 | |
154 | CALL_END; |
153 | CALL_END; |
155 | } |
154 | } |
156 | } |
155 | } |
157 | |
156 | |
158 | object_thawer::object_thawer (const char *data, AV *perlav) |
157 | object_thawer::object_thawer (const char *data, AV *perlav) |
159 | : name (strdup ("(memory stream")) |
158 | : name (strdup ("(memory stream)")) |
160 | { |
159 | { |
161 | av = perlav; |
160 | av = perlav; |
162 | text = newSVpv (data, 0); |
161 | text = newSVpv (data, 0); |
163 | sv_catpv (text, thawer_eof); |
162 | sv_catpv (text, thawer_eof); |
164 | line = SvPVbyte_nolen (text); |
163 | line = SvPVbyte_nolen (text); |
|
|
164 | next (); |
165 | } |
165 | } |
166 | |
166 | |
167 | void |
167 | void |
168 | object_thawer::get (attachable *obj, int oid) |
168 | object_thawer::get (attachable *obj, int oid) |
169 | { |
169 | { |
… | |
… | |
172 | |
172 | |
173 | SV **svp = av_fetch ((AV *)av, oid, 0); |
173 | SV **svp = av_fetch ((AV *)av, oid, 0); |
174 | |
174 | |
175 | if (!svp || !SvROK (*svp)) |
175 | if (!svp || !SvROK (*svp)) |
176 | { |
176 | { |
177 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
177 | LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
178 | return; |
178 | return; |
179 | } |
179 | } |
180 | |
180 | |
181 | if (!SvROK (*svp)) |
181 | if (!SvROK (*svp)) |
182 | { |
182 | { |
183 | LOG (llevError, "deserialised perl object is not an RV"); |
183 | LOG (llevError, "deserialised perl object is not an RV\n"); |
184 | return; |
184 | return; |
185 | } |
185 | } |
186 | |
186 | |
187 | HV *hv = (HV *)SvRV (*svp); |
187 | HV *hv = (HV *)SvRV (*svp); |
188 | |
188 | |
189 | if (SvTYPE (hv) != SVt_PVHV) |
189 | if (SvTYPE (hv) != SVt_PVHV) |
190 | { |
190 | { |
191 | LOG (llevError, "deserialised perl object is not a PVHV"); |
191 | LOG (llevError, "deserialised perl object is not a PVHV\n"); |
192 | return; |
192 | return; |
193 | } |
193 | } |
194 | |
194 | |
195 | if (obj->self) |
195 | if (obj->self) |
196 | { |
196 | { |
… | |
… | |
220 | object_thawer::~object_thawer () |
220 | object_thawer::~object_thawer () |
221 | { |
221 | { |
222 | if (text) SvREFCNT_dec (text); |
222 | if (text) SvREFCNT_dec (text); |
223 | if (av) SvREFCNT_dec (av); |
223 | if (av) SvREFCNT_dec (av); |
224 | |
224 | |
|
|
225 | resolve_delayed_derefs (false); |
|
|
226 | |
225 | free ((void *)name); |
227 | free ((void *)name); |
226 | } |
228 | } |
227 | |
229 | |
228 | //TODO: remove |
230 | static void |
229 | char * |
231 | error_out () |
230 | fgets (char *s, int n, object_thawer &thawer) |
|
|
231 | { |
232 | { |
232 | char *p = thawer.line; |
233 | if (object_thawer::errors_are_fatal) |
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 | } |
234 | { |
|
|
235 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
|
|
236 | exit (1); |
|
|
237 | } |
|
|
238 | } |
251 | |
239 | |
252 | *q = 0; |
240 | void |
253 | thawer.line = p; |
241 | object_thawer::parse_warn (const char *msg) |
254 | |
242 | { |
255 | return s == q ? 0 : s; |
243 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
|
|
244 | this->name, linenum, |
|
|
245 | kw_str ? kw_str : "<null>", |
|
|
246 | value ? value : "<null>", |
|
|
247 | msg); |
256 | } |
248 | } |
257 | |
249 | |
258 | bool |
250 | bool |
259 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
251 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
260 | { |
252 | { |
… | |
… | |
264 | switch (kw) |
256 | switch (kw) |
265 | { |
257 | { |
266 | case KW_EOF: |
258 | case KW_EOF: |
267 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
259 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
268 | this->name, linenum, type, name); |
260 | this->name, linenum, type, name); |
|
|
261 | error_out (); |
269 | return false; |
262 | return false; |
270 | |
263 | |
271 | case KW_ERROR: |
264 | case KW_ERROR: |
272 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
265 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
273 | this->name, linenum, |
266 | this->name, linenum, |
274 | type, name, |
267 | type, name, |
275 | kw_str ? kw_str : "<file load>"); |
268 | kw_str ? kw_str : "<file load>"); |
|
|
269 | error_out (); |
276 | return false; |
270 | return false; |
277 | |
271 | |
278 | default: |
272 | default: |
279 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
273 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
280 | this->name, linenum, |
274 | this->name, linenum, |
281 | kw_str ? kw_str : "<null>", |
275 | kw_str ? kw_str : "<null>", |
282 | value ? value : "<null>", |
276 | value ? value : "<null>", |
283 | type, name, |
277 | type, name, |
284 | skip ? "skipping line" : "aborting load"); |
278 | skip ? "skipping line" : "aborting load"); |
|
|
279 | error_out (); |
285 | return skip; |
280 | return skip; |
286 | } |
281 | } |
287 | } |
282 | } |
288 | |
283 | |
289 | void |
284 | void |
… | |
… | |
296 | } |
291 | } |
297 | |
292 | |
298 | for (;;) |
293 | for (;;) |
299 | { |
294 | { |
300 | char *p = line; |
295 | char *p = line; |
|
|
296 | |
|
|
297 | if (expect_false (*p <= ' ')) |
|
|
298 | { |
|
|
299 | // skip whitespace (only some files need this) |
|
|
300 | while (*p == ' ' || *p == '\t') |
|
|
301 | p++; |
|
|
302 | |
|
|
303 | line = p; |
|
|
304 | } |
301 | |
305 | |
302 | if (!*p) |
306 | if (!*p) |
303 | { |
307 | { |
304 | kw = KW_EOF; |
308 | kw = KW_EOF; |
305 | break; |
309 | break; |
… | |
… | |
309 | while (*p > ' ') |
313 | while (*p > ' ') |
310 | p++; |
314 | p++; |
311 | |
315 | |
312 | int klen = p - line; |
316 | int klen = p - line; |
313 | |
317 | |
|
|
318 | value_nn = ""; |
|
|
319 | value = 0; |
|
|
320 | |
314 | if (*p++ != '\n') |
321 | if (*p++ != '\n') |
315 | { |
322 | { |
316 | // parse value |
323 | // parse value |
317 | while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
324 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
318 | ++p; |
325 | ++p; |
319 | |
326 | |
320 | value = p; |
327 | value_nn = value = p; |
321 | |
328 | |
322 | while (*p != '\n') |
329 | while (*p != '\n') |
323 | p++; |
330 | p++; |
324 | |
331 | |
325 | *p++ = 0; |
332 | *p++ = 0; |
326 | } |
333 | } |
327 | else |
|
|
328 | value = 0; |
|
|
329 | |
334 | |
330 | ++linenum; |
335 | ++linenum; |
331 | line [klen] = 0; |
336 | line [klen] = 0; |
332 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
337 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
333 | |
338 | |
334 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D |
|
|
335 | |
|
|
336 | kw_str = line; |
339 | kw_str = line; |
337 | line = p; |
340 | line = p; |
338 | |
341 | |
339 | if (kw_idx) |
342 | if (kw_idx) |
340 | { |
343 | { |
… | |
… | |
347 | { |
350 | { |
348 | kw = KW_ERROR; |
351 | kw = KW_ERROR; |
349 | break; |
352 | break; |
350 | } |
353 | } |
351 | } |
354 | } |
|
|
355 | } |
|
|
356 | |
|
|
357 | bool |
|
|
358 | object_thawer::next_line () |
|
|
359 | { |
|
|
360 | if (!line) |
|
|
361 | { |
|
|
362 | kw = KW_ERROR; |
|
|
363 | return 0; |
|
|
364 | } |
|
|
365 | |
|
|
366 | for (;;) |
|
|
367 | { |
|
|
368 | char *p = line; |
|
|
369 | |
|
|
370 | if (expect_false (*p <= ' ')) |
|
|
371 | { |
|
|
372 | // skip whitespace (only some files need this) |
|
|
373 | while (*p == ' ' || *p == '\t') |
|
|
374 | p++; |
|
|
375 | |
|
|
376 | line = p; |
|
|
377 | } |
|
|
378 | |
|
|
379 | if (!*p) |
|
|
380 | { |
|
|
381 | kw = KW_EOF; |
|
|
382 | return 0; |
|
|
383 | } |
|
|
384 | |
|
|
385 | kw = KW_value; |
|
|
386 | kw_str = p; |
|
|
387 | value_nn = p; |
|
|
388 | value = p; |
|
|
389 | |
|
|
390 | // parse till newline |
|
|
391 | while (*p > '\n') |
|
|
392 | p++; |
|
|
393 | |
|
|
394 | if (*p == '\n') |
|
|
395 | *p++ = 0; |
|
|
396 | |
|
|
397 | ++linenum; |
|
|
398 | |
|
|
399 | line = p; |
|
|
400 | |
|
|
401 | if (*kw_str && *kw_str != '#') |
|
|
402 | break; |
|
|
403 | |
|
|
404 | // empty/comment line -> skip it |
|
|
405 | } |
|
|
406 | |
|
|
407 | return 1; |
352 | } |
408 | } |
353 | |
409 | |
354 | void |
410 | void |
355 | object_thawer::skip () |
411 | object_thawer::skip () |
356 | { |
412 | { |
… | |
… | |
366 | |
422 | |
367 | next (); |
423 | next (); |
368 | } |
424 | } |
369 | |
425 | |
370 | void |
426 | void |
371 | object_thawer::get (shstr &sh) const |
427 | object_thawer::skip_block () |
372 | { |
428 | { |
373 | if (value) |
429 | // must not stop at KW_ERROR, as those signify custom keys |
374 | sh = value; |
430 | while (kw != KW_EOF) |
375 | else |
|
|
376 | { |
431 | { |
377 | sh = "<value missing>"; |
432 | keyword w = kw; |
378 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename |
433 | skip (); |
|
|
434 | |
|
|
435 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
436 | skip_block (); |
|
|
437 | else if (w == KW_end) |
|
|
438 | break; |
379 | } |
439 | } |
380 | } |
440 | } |
381 | |
441 | |
382 | void |
442 | void |
383 | object_thawer::get_ml (keyword kend, shstr &sh) |
443 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
431 | } |
491 | } |
432 | |
492 | |
433 | sint32 |
493 | sint32 |
434 | object_thawer::get_sint32 () const |
494 | object_thawer::get_sint32 () const |
435 | { |
495 | { |
436 | char *p = value; |
496 | const char *p = value_nn; |
437 | |
|
|
438 | if (!p) |
|
|
439 | return 0; |
|
|
440 | |
497 | |
441 | sint32 val = 0; |
498 | sint32 val = 0; |
442 | bool negate; |
499 | bool negate; |
443 | |
500 | |
444 | if (*p == '-') |
501 | if (*p == '-') |
… | |
… | |
457 | while (*p); |
514 | while (*p); |
458 | |
515 | |
459 | return negate ? -val : val; |
516 | return negate ? -val : val; |
460 | } |
517 | } |
461 | |
518 | |
462 | sint64 |
519 | void |
463 | object_thawer::get_sint64 () const |
520 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
464 | { |
521 | { |
465 | return value ? atoll (value) : 0; |
522 | op->refcnt_inc (); |
|
|
523 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
524 | delrefs.push_back (r); |
|
|
525 | ptr = 0; |
466 | } |
526 | } |
467 | |
527 | |
468 | double |
528 | void |
469 | object_thawer::get_double () const |
529 | object_thawer::resolve_delayed_derefs (bool deref) |
470 | { |
530 | { |
471 | return value ? atof (value) : 0; |
531 | while (!delrefs.empty ()) |
472 | } |
532 | { |
|
|
533 | delayed_ref r = delrefs.back (); |
|
|
534 | delrefs.pop_back (); |
473 | |
535 | |
|
|
536 | if (deref) |
|
|
537 | *r.ptr = object::deref (r.ref); |
|
|
538 | |
|
|
539 | free ((void *)r.ref); |
|
|
540 | r.op->refcnt_dec (); |
|
|
541 | } |
|
|
542 | } |