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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
9 | * |
5 | * Copyright (©) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
6 | * |
10 | * This program is free software; you can redistribute it and/or modify |
7 | * 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 |
8 | * 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 |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
13 | * (at your option) any later version. |
10 | * option) any later version. |
14 | * |
11 | * |
15 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
19 | * |
16 | * |
20 | * You should have received a copy of the GNU General Public License |
17 | * 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 |
18 | * 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 |
19 | * <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
24 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
26 | #include "logger.h" |
25 | #include "logger.h" |
27 | #include "cfperl.h" |
26 | #include "cfperl.h" |
… | |
… | |
37 | { |
36 | { |
38 | SvREFCNT_dec (av); |
37 | SvREFCNT_dec (av); |
39 | } |
38 | } |
40 | |
39 | |
41 | void |
40 | void |
42 | object_freezer::put (attachable *ext) |
41 | object_freezer::put_ (attachable *ext) |
43 | { |
42 | { |
44 | ext->optimise (); |
43 | ext->optimise (); |
45 | |
44 | |
46 | if (ext->self) |
45 | if (ext->self) |
47 | { |
46 | { |
48 | int idx = AvFILLp ((AV *)av) + 1; |
47 | int idx = AvFILLp ((AV *)av) + 1; |
49 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
50 | |
49 | |
51 | add ((void *)"oid ", 4); |
50 | put (KW(oid), sint32(idx)); |
52 | add ((sint32)idx); |
|
|
53 | add ('\n'); |
|
|
54 | } |
51 | } |
55 | } |
52 | } |
56 | |
53 | |
57 | bool |
54 | bool |
58 | object_freezer::save (const char *path) |
55 | object_freezer::save (const char *path) |
… | |
… | |
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 | |
|
|
110 | bool object_thawer::errors_are_fatal = true; |
|
|
111 | |
111 | object_thawer::object_thawer (const char *path) |
112 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
113 | : name (strdup ("(memory stream)")) |
|
|
114 | { |
|
|
115 | init ("(memory stream)"); |
|
|
116 | |
|
|
117 | av = perlav; |
|
|
118 | text = newSVpv (data, 0); sv_catpv (text, thawer_eof); |
|
|
119 | line = SvPVbyte_nolen (text); |
|
|
120 | next (); |
|
|
121 | } |
|
|
122 | |
|
|
123 | object_thawer::object_thawer (const_utf8_string path) |
|
|
124 | { |
|
|
125 | init_from_file (path); |
|
|
126 | } |
|
|
127 | |
|
|
128 | // convenience constructor |
|
|
129 | object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file) |
|
|
130 | { |
|
|
131 | init_from_file (format ("%s/%s", dir, file)); |
|
|
132 | } |
|
|
133 | |
|
|
134 | void |
|
|
135 | object_thawer::init (const_utf8_string path) |
|
|
136 | { |
112 | : name (strdup (path)) |
137 | name = strdup (path); |
113 | { |
|
|
114 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
115 | |
|
|
116 | av = 0; |
138 | av = 0; |
117 | text = 0; |
139 | text = 0; |
118 | line = 0; |
140 | line = 0; |
119 | linenum = 0; |
141 | linenum = 0; |
120 | |
142 | |
121 | kw = KW_ERROR; |
143 | kw = KW_ERROR; |
122 | kw_str = 0; |
144 | kw_str = 0; |
123 | value = 0; |
145 | value = 0; |
|
|
146 | } |
124 | |
147 | |
|
|
148 | void |
|
|
149 | object_thawer::init_from_file (const_utf8_string path) |
|
|
150 | { |
125 | if (path) |
151 | init (path); |
126 | { |
152 | |
127 | CALL_BEGIN (1); |
153 | CALL_BEGIN (1); |
128 | CALL_ARG_SV (newSVpv (path, 0)); |
154 | CALL_ARG_SV (newSVpv (path, 0)); |
129 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
155 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
130 | |
156 | |
131 | if (count == 2) |
157 | if (count == 2) |
132 | { |
158 | { |
133 | // second value - perl objects |
159 | // second value - perl objects |
134 | { |
160 | { |
135 | SV *sv = POPs; |
161 | SV *sv = POPs; |
136 | if (SvROK (sv)) |
162 | if (SvROK (sv)) |
137 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
163 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
138 | } |
164 | } |
139 | |
165 | |
140 | // first value - text part, pad with 3 zeroes |
166 | // first value - text part, pad with 3 zeroes |
141 | { |
167 | { |
142 | SV *sv = POPs; |
168 | SV *sv = POPs; |
143 | STRLEN len; |
169 | STRLEN len; |
144 | char *sv_ = SvPVbyte (sv, len); |
170 | char *sv_ = SvPVbyte (sv, len); |
145 | text = newSV (len + sizeof (eof)); |
171 | text = newSV (len + sizeof (thawer_eof)); |
146 | SvCUR_set (text, len); |
172 | SvCUR_set (text, len + sizeof (thawer_eof)); |
147 | memcpy (SvPVX (text), sv_, len); |
173 | memcpy (SvPVX (text), sv_, len); |
148 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
174 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
149 | |
175 | |
150 | line = SvPVX (text); |
176 | line = SvPVX (text); |
151 | } |
177 | next (); |
152 | } |
178 | } |
|
|
179 | } |
153 | |
180 | |
154 | CALL_END; |
181 | 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 | } |
182 | } |
166 | |
183 | |
167 | void |
184 | void |
168 | object_thawer::get (attachable *obj, int oid) |
185 | object_thawer::get (attachable *obj, int oid) |
169 | { |
186 | { |
… | |
… | |
220 | object_thawer::~object_thawer () |
237 | object_thawer::~object_thawer () |
221 | { |
238 | { |
222 | if (text) SvREFCNT_dec (text); |
239 | if (text) SvREFCNT_dec (text); |
223 | if (av) SvREFCNT_dec (av); |
240 | if (av) SvREFCNT_dec (av); |
224 | |
241 | |
|
|
242 | resolve_delayed_derefs (false); |
|
|
243 | |
225 | free ((void *)name); |
244 | free ((void *)name); |
226 | } |
245 | } |
227 | |
246 | |
228 | //TODO: remove |
247 | static void |
229 | char * |
248 | error_out () |
230 | fgets (char *s, int n, object_thawer &thawer) |
|
|
231 | { |
249 | { |
232 | char *p = thawer.line; |
250 | 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 | } |
251 | { |
|
|
252 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
|
|
253 | exit (1); |
|
|
254 | } |
|
|
255 | } |
251 | |
256 | |
252 | *q = 0; |
257 | void |
253 | thawer.line = p; |
258 | object_thawer::parse_warn (const char *msg) const |
254 | |
259 | { |
255 | return s == q ? 0 : s; |
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
|
|
261 | this->name, linenum, |
|
|
262 | kw_str ? kw_str : "<null>", |
|
|
263 | value ? value : "<null>", |
|
|
264 | msg); |
256 | } |
265 | } |
257 | |
266 | |
258 | bool |
267 | bool |
259 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
268 | object_thawer::parse_error (const char *type, const char *name, bool skip) const |
260 | { |
269 | { |
261 | if (!type) type = "file section"; |
270 | if (!type) type = "file section"; |
262 | if (!name) name = "generic"; |
271 | if (!name) name = "generic"; |
263 | |
272 | |
264 | switch (kw) |
273 | switch (kw) |
265 | { |
274 | { |
266 | case KW_EOF: |
275 | case KW_EOF: |
267 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
276 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
268 | this->name, linenum, type, name); |
277 | this->name, linenum, type, name); |
|
|
278 | error_out (); |
269 | return false; |
279 | return false; |
270 | |
280 | |
271 | case KW_ERROR: |
281 | case KW_ERROR: |
272 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
282 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
273 | this->name, linenum, |
283 | this->name, linenum, |
274 | type, name, |
284 | type, name, |
275 | kw_str ? kw_str : "<file load>"); |
285 | kw_str ? kw_str : "<file load>"); |
|
|
286 | error_out (); |
276 | return false; |
287 | return false; |
277 | |
288 | |
278 | default: |
289 | default: |
279 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
290 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
280 | this->name, linenum, |
291 | this->name, linenum, |
281 | kw_str ? kw_str : "<null>", |
292 | kw_str ? kw_str : "<null>", |
282 | value ? value : "<null>", |
293 | value ? value : "<null>", |
283 | type, name, |
294 | type, name, |
284 | skip ? "skipping line" : "aborting load"); |
295 | skip ? "skipping line" : "aborting load"); |
|
|
296 | error_out (); |
285 | return skip; |
297 | return skip; |
286 | } |
298 | } |
287 | } |
299 | } |
288 | |
300 | |
289 | void |
301 | void |
… | |
… | |
297 | |
309 | |
298 | for (;;) |
310 | for (;;) |
299 | { |
311 | { |
300 | char *p = line; |
312 | char *p = line; |
301 | |
313 | |
302 | if (*p <= ' ') |
314 | if (ecb_expect_false (*p <= ' ')) |
303 | { |
315 | { |
304 | // skip whitespace (only some files need this) |
316 | // skip whitespace (only some files need this) |
305 | while (*p == ' ' || *p == '\t') |
317 | while (*p == ' ' || *p == '\t') |
306 | p++; |
318 | p++; |
307 | |
319 | |
… | |
… | |
318 | while (*p > ' ') |
330 | while (*p > ' ') |
319 | p++; |
331 | p++; |
320 | |
332 | |
321 | int klen = p - line; |
333 | int klen = p - line; |
322 | |
334 | |
|
|
335 | value_nn = ""; |
323 | value = 0; |
336 | value = 0; |
324 | |
337 | |
325 | if (*p++ != '\n') |
338 | if (*p++ != '\n') |
326 | { |
339 | { |
327 | // parse value |
340 | // parse value |
328 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
341 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
329 | ++p; |
342 | ++p; |
330 | |
343 | |
331 | value = p; |
344 | value_nn = value = p; |
332 | |
345 | |
333 | while (*p != '\n') |
346 | while (*p != '\n') |
334 | p++; |
347 | p++; |
335 | |
348 | |
336 | *p++ = 0; |
349 | *p++ = 0; |
… | |
… | |
338 | |
351 | |
339 | ++linenum; |
352 | ++linenum; |
340 | line [klen] = 0; |
353 | line [klen] = 0; |
341 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
354 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
342 | |
355 | |
343 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D |
|
|
344 | |
|
|
345 | kw_str = line; |
356 | kw_str = line; |
346 | line = p; |
357 | line = p; |
347 | |
358 | |
348 | if (kw_idx) |
359 | if (kw_idx) |
349 | { |
360 | { |
… | |
… | |
356 | { |
367 | { |
357 | kw = KW_ERROR; |
368 | kw = KW_ERROR; |
358 | break; |
369 | break; |
359 | } |
370 | } |
360 | } |
371 | } |
|
|
372 | } |
|
|
373 | |
|
|
374 | bool |
|
|
375 | object_thawer::next_line () |
|
|
376 | { |
|
|
377 | if (!line) |
|
|
378 | { |
|
|
379 | kw = KW_ERROR; |
|
|
380 | return 0; |
|
|
381 | } |
|
|
382 | |
|
|
383 | for (;;) |
|
|
384 | { |
|
|
385 | char *p = line; |
|
|
386 | |
|
|
387 | if (ecb_expect_false (*p <= ' ')) |
|
|
388 | { |
|
|
389 | // skip whitespace (only some files need this) |
|
|
390 | while (*p == ' ' || *p == '\t') |
|
|
391 | p++; |
|
|
392 | |
|
|
393 | line = p; |
|
|
394 | } |
|
|
395 | |
|
|
396 | if (!*p) |
|
|
397 | { |
|
|
398 | kw = KW_EOF; |
|
|
399 | return 0; |
|
|
400 | } |
|
|
401 | |
|
|
402 | kw = KW_value; |
|
|
403 | kw_str = p; |
|
|
404 | value_nn = p; |
|
|
405 | value = p; |
|
|
406 | |
|
|
407 | // parse till newline |
|
|
408 | while (*p > '\n') |
|
|
409 | p++; |
|
|
410 | |
|
|
411 | if (*p == '\n') |
|
|
412 | *p++ = 0; |
|
|
413 | |
|
|
414 | ++linenum; |
|
|
415 | |
|
|
416 | line = p; |
|
|
417 | |
|
|
418 | if (*kw_str && *kw_str != '#') |
|
|
419 | break; |
|
|
420 | |
|
|
421 | // empty/comment line -> skip it |
|
|
422 | } |
|
|
423 | |
|
|
424 | return 1; |
361 | } |
425 | } |
362 | |
426 | |
363 | void |
427 | void |
364 | object_thawer::skip () |
428 | object_thawer::skip () |
365 | { |
429 | { |
… | |
… | |
375 | |
439 | |
376 | next (); |
440 | next (); |
377 | } |
441 | } |
378 | |
442 | |
379 | void |
443 | void |
380 | object_thawer::get (shstr &sh) const |
444 | object_thawer::skip_block () |
381 | { |
445 | { |
382 | if (value) |
446 | // must not stop at KW_ERROR, as those signify custom keys |
383 | sh = value; |
447 | while (kw != KW_EOF) |
384 | else |
|
|
385 | { |
448 | { |
386 | sh = "<value missing>"; |
449 | keyword w = kw; |
387 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename |
450 | skip (); |
|
|
451 | |
|
|
452 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
453 | skip_block (); |
|
|
454 | else if (w == KW_end) |
|
|
455 | break; |
388 | } |
456 | } |
389 | } |
457 | } |
390 | |
458 | |
391 | void |
459 | void |
392 | object_thawer::get_ml (keyword kend, shstr &sh) |
460 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
440 | } |
508 | } |
441 | |
509 | |
442 | sint32 |
510 | sint32 |
443 | object_thawer::get_sint32 () const |
511 | object_thawer::get_sint32 () const |
444 | { |
512 | { |
445 | char *p = value; |
513 | const char *p = value_nn; |
446 | |
|
|
447 | if (!p) |
|
|
448 | return 0; |
|
|
449 | |
514 | |
450 | sint32 val = 0; |
515 | sint32 val = 0; |
451 | bool negate; |
516 | bool negate; |
452 | |
517 | |
453 | if (*p == '-') |
518 | if (*p == '-') |
… | |
… | |
466 | while (*p); |
531 | while (*p); |
467 | |
532 | |
468 | return negate ? -val : val; |
533 | return negate ? -val : val; |
469 | } |
534 | } |
470 | |
535 | |
471 | sint64 |
536 | void |
472 | object_thawer::get_sint64 () const |
537 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
473 | { |
538 | { |
474 | return value ? atoll (value) : 0; |
539 | op->refcnt_inc (); |
|
|
540 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
541 | delrefs.push_back (r); |
|
|
542 | ptr = 0; |
475 | } |
543 | } |
476 | |
544 | |
477 | double |
545 | void |
478 | object_thawer::get_double () const |
546 | object_thawer::resolve_delayed_derefs (bool deref) |
479 | { |
547 | { |
480 | return value ? atof (value) : 0; |
548 | while (!delrefs.empty ()) |
481 | } |
549 | { |
|
|
550 | delayed_ref r = delrefs.back (); |
|
|
551 | delrefs.pop_back (); |
482 | |
552 | |
|
|
553 | if (deref) |
|
|
554 | *r.ptr = object::deref (r.ref); |
|
|
555 | |
|
|
556 | free ((void *)r.ref); |
|
|
557 | r.op->refcnt_dec (); |
|
|
558 | } |
|
|
559 | } |