1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
|
|
5 | * Copyright (©) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Crossfire TRT is free software: you can redistribute it and/or modify |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * 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 |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * (at your option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * 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, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * 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 |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | * and the GNU General Public License along with this program. If not, see |
|
|
19 | * <http://www.gnu.org/licenses/>. |
18 | * |
20 | * |
19 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | */ |
22 | */ |
21 | |
23 | |
22 | #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 |
23 | #include "logger.h" |
25 | #include "logger.h" |
24 | #include "cfperl.h" |
26 | #include "cfperl.h" |
… | |
… | |
34 | { |
36 | { |
35 | SvREFCNT_dec (av); |
37 | SvREFCNT_dec (av); |
36 | } |
38 | } |
37 | |
39 | |
38 | void |
40 | void |
39 | object_freezer::put (attachable *ext) |
41 | object_freezer::put_ (attachable *ext) |
40 | { |
42 | { |
41 | ext->optimise (); |
43 | ext->optimise (); |
42 | |
44 | |
43 | if (ext->self) |
45 | if (ext->self) |
44 | { |
46 | { |
45 | int idx = AvFILLp ((AV *)av) + 1; |
47 | int idx = AvFILLp ((AV *)av) + 1; |
46 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
47 | |
49 | |
48 | add ((void *)"oid ", 4); |
50 | put (KW(oid), sint32(idx)); |
49 | add ((sint32)idx); |
|
|
50 | add ('\n'); |
|
|
51 | } |
51 | } |
52 | } |
52 | } |
53 | |
53 | |
54 | bool |
54 | bool |
55 | object_freezer::save (const char *path) |
55 | object_freezer::save (const char *path) |
… | |
… | |
105 | } |
105 | } |
106 | #endif |
106 | #endif |
107 | |
107 | |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
109 | |
109 | |
|
|
110 | bool object_thawer::errors_are_fatal = true; |
|
|
111 | |
110 | 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 | { |
111 | : name (strdup (path)) |
137 | name = strdup (path); |
112 | { |
|
|
113 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
114 | |
|
|
115 | av = 0; |
138 | av = 0; |
116 | text = 0; |
139 | text = 0; |
117 | line = 0; |
140 | line = 0; |
118 | linenum = 0; |
141 | linenum = 0; |
119 | |
142 | |
120 | kw = KW_ERROR; |
143 | kw = KW_ERROR; |
121 | kw_str = 0; |
144 | kw_str = 0; |
122 | value = 0; |
145 | value = 0; |
|
|
146 | } |
123 | |
147 | |
|
|
148 | void |
|
|
149 | object_thawer::init_from_file (const_utf8_string path) |
|
|
150 | { |
124 | if (path) |
151 | init (path); |
125 | { |
152 | |
126 | CALL_BEGIN (1); |
153 | CALL_BEGIN (1); |
127 | CALL_ARG_SV (newSVpv (path, 0)); |
154 | CALL_ARG_SV (newSVpv (path, 0)); |
128 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
155 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
129 | |
156 | |
130 | if (count == 2) |
157 | if (count == 2) |
131 | { |
158 | { |
132 | // second value - perl objects |
159 | // second value - perl objects |
133 | { |
160 | { |
134 | SV *sv = POPs; |
161 | SV *sv = POPs; |
135 | if (SvROK (sv)) |
162 | if (SvROK (sv)) |
136 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
163 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
137 | } |
164 | } |
138 | |
165 | |
139 | // first value - text part, pad with 3 zeroes |
166 | // first value - text part, pad with 3 zeroes |
140 | { |
167 | { |
141 | SV *sv = POPs; |
168 | SV *sv = POPs; |
142 | STRLEN len; |
169 | STRLEN len; |
143 | char *sv_ = SvPVbyte (sv, len); |
170 | char *sv_ = SvPVbyte (sv, len); |
144 | text = newSV (len + sizeof (eof)); |
171 | text = newSV (len + sizeof (thawer_eof)); |
145 | SvCUR_set (text, len); |
172 | SvCUR_set (text, len + sizeof (thawer_eof)); |
146 | memcpy (SvPVX (text), sv_, len); |
173 | memcpy (SvPVX (text), sv_, len); |
147 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
174 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
148 | |
175 | |
149 | line = SvPVX (text); |
176 | line = SvPVX (text); |
150 | next (); |
177 | next (); |
151 | } |
|
|
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 | next (); |
|
|
166 | } |
182 | } |
167 | |
183 | |
168 | void |
184 | void |
169 | object_thawer::get (attachable *obj, int oid) |
185 | object_thawer::get (attachable *obj, int oid) |
170 | { |
186 | { |
… | |
… | |
226 | resolve_delayed_derefs (false); |
242 | resolve_delayed_derefs (false); |
227 | |
243 | |
228 | free ((void *)name); |
244 | free ((void *)name); |
229 | } |
245 | } |
230 | |
246 | |
231 | //TODO: remove |
247 | static void |
232 | char * |
248 | error_out () |
233 | fgets (char *s, int n, object_thawer &thawer) |
|
|
234 | { |
249 | { |
235 | char *p = thawer.line; |
250 | if (object_thawer::errors_are_fatal) |
236 | char *q = s; |
|
|
237 | |
|
|
238 | if (!p) |
|
|
239 | return 0; |
|
|
240 | |
|
|
241 | while (--n) |
|
|
242 | { |
|
|
243 | if (!*p) |
|
|
244 | break; |
|
|
245 | |
|
|
246 | *q++ = *p; |
|
|
247 | |
|
|
248 | if (*p++ == '\n') |
|
|
249 | { |
|
|
250 | ++thawer.linenum; |
|
|
251 | break; |
|
|
252 | } |
|
|
253 | } |
251 | { |
254 | |
252 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
255 | *q = 0; |
253 | exit (1); |
256 | thawer.line = p; |
254 | } |
257 | |
|
|
258 | return s == q ? 0 : s; |
|
|
259 | } |
255 | } |
260 | |
256 | |
261 | void |
257 | void |
262 | object_thawer::parse_warn (const char *msg) |
258 | object_thawer::parse_warn (const char *msg) const |
263 | { |
259 | { |
264 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
265 | this->name, linenum, |
261 | this->name, linenum, |
266 | kw_str ? kw_str : "<null>", |
262 | kw_str ? kw_str : "<null>", |
267 | value ? value : "<null>", |
263 | value ? value : "<null>", |
268 | msg); |
264 | msg); |
269 | } |
265 | } |
270 | |
266 | |
271 | bool |
267 | bool |
272 | 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 |
273 | { |
269 | { |
274 | if (!type) type = "file section"; |
270 | if (!type) type = "file section"; |
275 | if (!name) name = "generic"; |
271 | if (!name) name = "generic"; |
276 | |
272 | |
277 | switch (kw) |
273 | switch (kw) |
278 | { |
274 | { |
279 | case KW_EOF: |
275 | case KW_EOF: |
280 | 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", |
281 | this->name, linenum, type, name); |
277 | this->name, linenum, type, name); |
|
|
278 | error_out (); |
282 | return false; |
279 | return false; |
283 | |
280 | |
284 | case KW_ERROR: |
281 | case KW_ERROR: |
285 | 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", |
286 | this->name, linenum, |
283 | this->name, linenum, |
287 | type, name, |
284 | type, name, |
288 | kw_str ? kw_str : "<file load>"); |
285 | kw_str ? kw_str : "<file load>"); |
|
|
286 | error_out (); |
289 | return false; |
287 | return false; |
290 | |
288 | |
291 | default: |
289 | default: |
292 | 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", |
293 | this->name, linenum, |
291 | this->name, linenum, |
294 | kw_str ? kw_str : "<null>", |
292 | kw_str ? kw_str : "<null>", |
295 | value ? value : "<null>", |
293 | value ? value : "<null>", |
296 | type, name, |
294 | type, name, |
297 | skip ? "skipping line" : "aborting load"); |
295 | skip ? "skipping line" : "aborting load"); |
|
|
296 | error_out (); |
298 | return skip; |
297 | return skip; |
299 | } |
298 | } |
300 | } |
299 | } |
301 | |
300 | |
302 | void |
301 | void |
… | |
… | |
310 | |
309 | |
311 | for (;;) |
310 | for (;;) |
312 | { |
311 | { |
313 | char *p = line; |
312 | char *p = line; |
314 | |
313 | |
315 | if (*p <= ' ') |
314 | if (ecb_expect_false (*p <= ' ')) |
316 | { |
315 | { |
317 | // skip whitespace (only some files need this) |
316 | // skip whitespace (only some files need this) |
318 | while (*p == ' ' || *p == '\t') |
317 | while (*p == ' ' || *p == '\t') |
319 | p++; |
318 | p++; |
320 | |
319 | |
… | |
… | |
368 | { |
367 | { |
369 | kw = KW_ERROR; |
368 | kw = KW_ERROR; |
370 | break; |
369 | break; |
371 | } |
370 | } |
372 | } |
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; |
373 | } |
425 | } |
374 | |
426 | |
375 | void |
427 | void |
376 | object_thawer::skip () |
428 | object_thawer::skip () |
377 | { |
429 | { |
… | |
… | |
389 | } |
441 | } |
390 | |
442 | |
391 | void |
443 | void |
392 | object_thawer::skip_block () |
444 | object_thawer::skip_block () |
393 | { |
445 | { |
|
|
446 | // must not stop at KW_ERROR, as those signify custom keys |
394 | while (kw != KW_EOF) |
447 | while (kw != KW_EOF) |
395 | { |
448 | { |
396 | keyword w = kw; |
449 | keyword w = kw; |
397 | skip (); |
450 | skip (); |
|
|
451 | |
|
|
452 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
453 | skip_block (); |
398 | if (w == KW_end) |
454 | else if (w == KW_end) |
399 | break; |
455 | break; |
400 | } |
456 | } |
401 | } |
457 | } |
402 | |
458 | |
403 | void |
459 | void |
… | |
… | |
452 | } |
508 | } |
453 | |
509 | |
454 | sint32 |
510 | sint32 |
455 | object_thawer::get_sint32 () const |
511 | object_thawer::get_sint32 () const |
456 | { |
512 | { |
457 | char *p = value_nn; |
513 | const char *p = value_nn; |
458 | |
514 | |
459 | sint32 val = 0; |
515 | sint32 val = 0; |
460 | bool negate; |
516 | bool negate; |
461 | |
517 | |
462 | if (*p == '-') |
518 | if (*p == '-') |