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