1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
|
|
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / 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 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * 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 Affero GNU General Public License |
17 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | #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 |
24 | #include "logger.h" |
25 | #include "logger.h" |
… | |
… | |
104 | } |
105 | } |
105 | #endif |
106 | #endif |
106 | |
107 | |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | |
109 | |
|
|
110 | bool object_thawer::errors_are_fatal = true; |
|
|
111 | |
109 | 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 | { |
110 | : name (strdup (path)) |
137 | name = strdup (path); |
111 | { |
|
|
112 | av = 0; |
138 | av = 0; |
113 | text = 0; |
139 | text = 0; |
114 | line = 0; |
140 | line = 0; |
115 | linenum = 0; |
141 | linenum = 0; |
116 | |
142 | |
117 | kw = KW_ERROR; |
143 | kw = KW_ERROR; |
118 | kw_str = 0; |
144 | kw_str = 0; |
119 | value = 0; |
145 | value = 0; |
|
|
146 | } |
120 | |
147 | |
|
|
148 | void |
|
|
149 | object_thawer::init_from_file (const_utf8_string path) |
|
|
150 | { |
121 | if (path) |
151 | init (path); |
122 | { |
152 | |
123 | CALL_BEGIN (1); |
153 | CALL_BEGIN (1); |
124 | CALL_ARG_SV (newSVpv (path, 0)); |
154 | CALL_ARG_SV (newSVpv (path, 0)); |
125 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
155 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
126 | |
156 | |
127 | if (count == 2) |
157 | if (count == 2) |
128 | { |
158 | { |
129 | // second value - perl objects |
159 | // second value - perl objects |
130 | { |
160 | { |
131 | SV *sv = POPs; |
161 | SV *sv = POPs; |
132 | if (SvROK (sv)) |
162 | if (SvROK (sv)) |
133 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
163 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
134 | } |
164 | } |
135 | |
165 | |
136 | // first value - text part, pad with 3 zeroes |
166 | // first value - text part, pad with 3 zeroes |
137 | { |
167 | { |
138 | SV *sv = POPs; |
168 | SV *sv = POPs; |
139 | STRLEN len; |
169 | STRLEN len; |
140 | char *sv_ = SvPVbyte (sv, len); |
170 | char *sv_ = SvPVbyte (sv, len); |
141 | text = newSV (len + sizeof (thawer_eof)); |
171 | text = newSV (len + sizeof (thawer_eof)); |
142 | SvCUR_set (text, len + sizeof (thawer_eof)); |
172 | SvCUR_set (text, len + sizeof (thawer_eof)); |
143 | memcpy (SvPVX (text), sv_, len); |
173 | memcpy (SvPVX (text), sv_, len); |
144 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
174 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
145 | |
175 | |
146 | line = SvPVX (text); |
176 | line = SvPVX (text); |
147 | next (); |
177 | next (); |
148 | } |
|
|
149 | } |
178 | } |
|
|
179 | } |
150 | |
180 | |
151 | CALL_END; |
181 | CALL_END; |
152 | } |
|
|
153 | } |
|
|
154 | |
|
|
155 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
156 | : name (strdup ("(memory stream)")) |
|
|
157 | { |
|
|
158 | av = perlav; |
|
|
159 | text = newSVpv (data, 0); |
|
|
160 | sv_catpv (text, thawer_eof); |
|
|
161 | line = SvPVbyte_nolen (text); |
|
|
162 | next (); |
|
|
163 | } |
182 | } |
164 | |
183 | |
165 | void |
184 | void |
166 | object_thawer::get (attachable *obj, int oid) |
185 | object_thawer::get (attachable *obj, int oid) |
167 | { |
186 | { |
… | |
… | |
223 | resolve_delayed_derefs (false); |
242 | resolve_delayed_derefs (false); |
224 | |
243 | |
225 | free ((void *)name); |
244 | free ((void *)name); |
226 | } |
245 | } |
227 | |
246 | |
|
|
247 | static void |
|
|
248 | error_out () |
|
|
249 | { |
|
|
250 | if (object_thawer::errors_are_fatal) |
|
|
251 | { |
|
|
252 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
|
|
253 | exit (1); |
|
|
254 | } |
|
|
255 | } |
|
|
256 | |
228 | void |
257 | void |
229 | object_thawer::parse_warn (const char *msg) |
258 | object_thawer::parse_warn (const char *msg) const |
230 | { |
259 | { |
231 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
232 | this->name, linenum, |
261 | this->name, linenum, |
233 | kw_str ? kw_str : "<null>", |
262 | kw_str ? kw_str : "<null>", |
234 | value ? value : "<null>", |
263 | value ? value : "<null>", |
235 | msg); |
264 | msg); |
236 | } |
265 | } |
237 | |
266 | |
238 | bool |
267 | bool |
239 | 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 |
240 | { |
269 | { |
241 | if (!type) type = "file section"; |
270 | if (!type) type = "file section"; |
242 | if (!name) name = "generic"; |
271 | if (!name) name = "generic"; |
243 | |
272 | |
244 | switch (kw) |
273 | switch (kw) |
245 | { |
274 | { |
246 | case KW_EOF: |
275 | case KW_EOF: |
247 | 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", |
248 | this->name, linenum, type, name); |
277 | this->name, linenum, type, name); |
|
|
278 | error_out (); |
249 | return false; |
279 | return false; |
250 | |
280 | |
251 | case KW_ERROR: |
281 | case KW_ERROR: |
252 | 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", |
253 | this->name, linenum, |
283 | this->name, linenum, |
254 | type, name, |
284 | type, name, |
255 | kw_str ? kw_str : "<file load>"); |
285 | kw_str ? kw_str : "<file load>"); |
|
|
286 | error_out (); |
256 | return false; |
287 | return false; |
257 | |
288 | |
258 | default: |
289 | default: |
259 | 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", |
260 | this->name, linenum, |
291 | this->name, linenum, |
261 | kw_str ? kw_str : "<null>", |
292 | kw_str ? kw_str : "<null>", |
262 | value ? value : "<null>", |
293 | value ? value : "<null>", |
263 | type, name, |
294 | type, name, |
264 | skip ? "skipping line" : "aborting load"); |
295 | skip ? "skipping line" : "aborting load"); |
|
|
296 | error_out (); |
265 | return skip; |
297 | return skip; |
266 | } |
298 | } |
267 | } |
299 | } |
268 | |
300 | |
269 | void |
301 | void |
… | |
… | |
277 | |
309 | |
278 | for (;;) |
310 | for (;;) |
279 | { |
311 | { |
280 | char *p = line; |
312 | char *p = line; |
281 | |
313 | |
282 | if (expect_false (*p <= ' ')) |
314 | if (ecb_expect_false (*p <= ' ')) |
283 | { |
315 | { |
284 | // skip whitespace (only some files need this) |
316 | // skip whitespace (only some files need this) |
285 | while (*p == ' ' || *p == '\t') |
317 | while (*p == ' ' || *p == '\t') |
286 | p++; |
318 | p++; |
287 | |
319 | |
… | |
… | |
337 | break; |
369 | break; |
338 | } |
370 | } |
339 | } |
371 | } |
340 | } |
372 | } |
341 | |
373 | |
342 | void |
374 | bool |
343 | object_thawer::next_line () |
375 | object_thawer::next_line () |
344 | { |
376 | { |
345 | if (!line) |
377 | if (!line) |
346 | { |
378 | { |
347 | kw = KW_ERROR; |
379 | kw = KW_ERROR; |
348 | return; |
380 | return 0; |
349 | } |
381 | } |
350 | |
382 | |
351 | for (;;) |
383 | for (;;) |
352 | { |
384 | { |
353 | char *p = line; |
385 | char *p = line; |
354 | |
386 | |
355 | if (expect_false (*p <= ' ')) |
387 | if (ecb_expect_false (*p <= ' ')) |
356 | { |
388 | { |
357 | // skip whitespace (only some files need this) |
389 | // skip whitespace (only some files need this) |
358 | while (*p == ' ' || *p == '\t') |
390 | while (*p == ' ' || *p == '\t') |
359 | p++; |
391 | p++; |
360 | |
392 | |
… | |
… | |
362 | } |
394 | } |
363 | |
395 | |
364 | if (!*p) |
396 | if (!*p) |
365 | { |
397 | { |
366 | kw = KW_EOF; |
398 | kw = KW_EOF; |
367 | break; |
399 | return 0; |
368 | } |
400 | } |
369 | |
401 | |
370 | kw = KW_value; |
402 | kw = KW_value; |
371 | kw_str = p; |
403 | kw_str = p; |
372 | value_nn = p; |
404 | value_nn = p; |
373 | value = p; |
405 | value = p; |
374 | |
406 | |
375 | // parse keyword |
407 | // parse till newline |
376 | while (*p > '\n') |
408 | while (*p > '\n') |
377 | p++; |
409 | p++; |
378 | |
410 | |
379 | if (*p == '\n') |
411 | if (*p == '\n') |
380 | *p++ = 0; |
412 | *p++ = 0; |
… | |
… | |
386 | if (*kw_str && *kw_str != '#') |
418 | if (*kw_str && *kw_str != '#') |
387 | break; |
419 | break; |
388 | |
420 | |
389 | // empty/comment line -> skip it |
421 | // empty/comment line -> skip it |
390 | } |
422 | } |
|
|
423 | |
|
|
424 | return 1; |
391 | } |
425 | } |
392 | |
426 | |
393 | void |
427 | void |
394 | object_thawer::skip () |
428 | object_thawer::skip () |
395 | { |
429 | { |