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 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 | av = perlav; |
|
|
115 | text = newSVpv (data, 0); |
|
|
116 | sv_catpv (text, thawer_eof); |
|
|
117 | line = SvPVbyte_nolen (text); |
|
|
118 | next (); |
|
|
119 | } |
|
|
120 | |
|
|
121 | object_thawer::object_thawer (const_utf8_string path) |
|
|
122 | { |
|
|
123 | new_from_path (path); |
|
|
124 | } |
|
|
125 | |
|
|
126 | // convenience constructor |
|
|
127 | object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file) |
|
|
128 | { |
|
|
129 | new_from_path (format ("%s/%s", dir, file)); |
|
|
130 | } |
|
|
131 | |
|
|
132 | void |
|
|
133 | object_thawer::new_from_path (const_utf8_string path) |
|
|
134 | { |
111 | : name (strdup (path)) |
135 | name = strdup (path); |
112 | { |
|
|
113 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
114 | |
|
|
115 | av = 0; |
136 | av = 0; |
116 | text = 0; |
137 | text = 0; |
117 | line = 0; |
138 | line = 0; |
118 | linenum = 0; |
139 | linenum = 0; |
119 | |
140 | |
… | |
… | |
139 | // first value - text part, pad with 3 zeroes |
160 | // first value - text part, pad with 3 zeroes |
140 | { |
161 | { |
141 | SV *sv = POPs; |
162 | SV *sv = POPs; |
142 | STRLEN len; |
163 | STRLEN len; |
143 | char *sv_ = SvPVbyte (sv, len); |
164 | char *sv_ = SvPVbyte (sv, len); |
144 | text = newSV (len + sizeof (eof)); |
165 | text = newSV (len + sizeof (thawer_eof)); |
145 | SvCUR_set (text, len); |
166 | SvCUR_set (text, len + sizeof (thawer_eof)); |
146 | memcpy (SvPVX (text), sv_, len); |
167 | memcpy (SvPVX (text), sv_, len); |
147 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
168 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
148 | |
169 | |
149 | line = SvPVX (text); |
170 | line = SvPVX (text); |
|
|
171 | next (); |
150 | } |
172 | } |
151 | } |
173 | } |
152 | |
174 | |
153 | CALL_END; |
175 | CALL_END; |
154 | } |
176 | } |
155 | } |
|
|
156 | |
|
|
157 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
158 | : name (strdup ("(memory stream)")) |
|
|
159 | { |
|
|
160 | av = perlav; |
|
|
161 | text = newSVpv (data, 0); |
|
|
162 | sv_catpv (text, thawer_eof); |
|
|
163 | line = SvPVbyte_nolen (text); |
|
|
164 | } |
177 | } |
165 | |
178 | |
166 | void |
179 | void |
167 | object_thawer::get (attachable *obj, int oid) |
180 | object_thawer::get (attachable *obj, int oid) |
168 | { |
181 | { |
… | |
… | |
219 | object_thawer::~object_thawer () |
232 | object_thawer::~object_thawer () |
220 | { |
233 | { |
221 | if (text) SvREFCNT_dec (text); |
234 | if (text) SvREFCNT_dec (text); |
222 | if (av) SvREFCNT_dec (av); |
235 | if (av) SvREFCNT_dec (av); |
223 | |
236 | |
|
|
237 | resolve_delayed_derefs (false); |
|
|
238 | |
224 | free ((void *)name); |
239 | free ((void *)name); |
225 | } |
240 | } |
226 | |
241 | |
227 | //TODO: remove |
242 | static void |
228 | char * |
243 | error_out () |
229 | fgets (char *s, int n, object_thawer &thawer) |
|
|
230 | { |
244 | { |
231 | char *p = thawer.line; |
245 | if (object_thawer::errors_are_fatal) |
232 | char *q = s; |
|
|
233 | |
|
|
234 | if (!p) |
|
|
235 | return 0; |
|
|
236 | |
|
|
237 | while (--n) |
|
|
238 | { |
|
|
239 | if (!*p) |
|
|
240 | break; |
|
|
241 | |
|
|
242 | *q++ = *p; |
|
|
243 | |
|
|
244 | if (*p++ == '\n') |
|
|
245 | { |
|
|
246 | ++thawer.linenum; |
|
|
247 | break; |
|
|
248 | } |
|
|
249 | } |
246 | { |
250 | |
247 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
251 | *q = 0; |
248 | exit (1); |
252 | thawer.line = p; |
249 | } |
253 | |
|
|
254 | return s == q ? 0 : s; |
|
|
255 | } |
250 | } |
256 | |
251 | |
257 | void |
252 | void |
258 | object_thawer::parse_warn (const char *msg) |
253 | object_thawer::parse_warn (const char *msg) const |
259 | { |
254 | { |
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
255 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
261 | this->name, linenum, |
256 | this->name, linenum, |
262 | kw_str ? kw_str : "<null>", |
257 | kw_str ? kw_str : "<null>", |
263 | value ? value : "<null>", |
258 | value ? value : "<null>", |
264 | msg); |
259 | msg); |
265 | } |
260 | } |
266 | |
261 | |
267 | bool |
262 | bool |
268 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
263 | object_thawer::parse_error (const char *type, const char *name, bool skip) const |
269 | { |
264 | { |
270 | if (!type) type = "file section"; |
265 | if (!type) type = "file section"; |
271 | if (!name) name = "generic"; |
266 | if (!name) name = "generic"; |
272 | |
267 | |
273 | switch (kw) |
268 | switch (kw) |
274 | { |
269 | { |
275 | case KW_EOF: |
270 | case KW_EOF: |
276 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
271 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
277 | this->name, linenum, type, name); |
272 | this->name, linenum, type, name); |
|
|
273 | error_out (); |
278 | return false; |
274 | return false; |
279 | |
275 | |
280 | case KW_ERROR: |
276 | case KW_ERROR: |
281 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
277 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
282 | this->name, linenum, |
278 | this->name, linenum, |
283 | type, name, |
279 | type, name, |
284 | kw_str ? kw_str : "<file load>"); |
280 | kw_str ? kw_str : "<file load>"); |
|
|
281 | error_out (); |
285 | return false; |
282 | return false; |
286 | |
283 | |
287 | default: |
284 | default: |
288 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
285 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
289 | this->name, linenum, |
286 | this->name, linenum, |
290 | kw_str ? kw_str : "<null>", |
287 | kw_str ? kw_str : "<null>", |
291 | value ? value : "<null>", |
288 | value ? value : "<null>", |
292 | type, name, |
289 | type, name, |
293 | skip ? "skipping line" : "aborting load"); |
290 | skip ? "skipping line" : "aborting load"); |
|
|
291 | error_out (); |
294 | return skip; |
292 | return skip; |
295 | } |
293 | } |
296 | } |
294 | } |
297 | |
295 | |
298 | void |
296 | void |
… | |
… | |
306 | |
304 | |
307 | for (;;) |
305 | for (;;) |
308 | { |
306 | { |
309 | char *p = line; |
307 | char *p = line; |
310 | |
308 | |
311 | if (*p <= ' ') |
309 | if (expect_false (*p <= ' ')) |
312 | { |
310 | { |
313 | // skip whitespace (only some files need this) |
311 | // skip whitespace (only some files need this) |
314 | while (*p == ' ' || *p == '\t') |
312 | while (*p == ' ' || *p == '\t') |
315 | p++; |
313 | p++; |
316 | |
314 | |
… | |
… | |
364 | { |
362 | { |
365 | kw = KW_ERROR; |
363 | kw = KW_ERROR; |
366 | break; |
364 | break; |
367 | } |
365 | } |
368 | } |
366 | } |
|
|
367 | } |
|
|
368 | |
|
|
369 | bool |
|
|
370 | object_thawer::next_line () |
|
|
371 | { |
|
|
372 | if (!line) |
|
|
373 | { |
|
|
374 | kw = KW_ERROR; |
|
|
375 | return 0; |
|
|
376 | } |
|
|
377 | |
|
|
378 | for (;;) |
|
|
379 | { |
|
|
380 | char *p = line; |
|
|
381 | |
|
|
382 | if (expect_false (*p <= ' ')) |
|
|
383 | { |
|
|
384 | // skip whitespace (only some files need this) |
|
|
385 | while (*p == ' ' || *p == '\t') |
|
|
386 | p++; |
|
|
387 | |
|
|
388 | line = p; |
|
|
389 | } |
|
|
390 | |
|
|
391 | if (!*p) |
|
|
392 | { |
|
|
393 | kw = KW_EOF; |
|
|
394 | return 0; |
|
|
395 | } |
|
|
396 | |
|
|
397 | kw = KW_value; |
|
|
398 | kw_str = p; |
|
|
399 | value_nn = p; |
|
|
400 | value = p; |
|
|
401 | |
|
|
402 | // parse till newline |
|
|
403 | while (*p > '\n') |
|
|
404 | p++; |
|
|
405 | |
|
|
406 | if (*p == '\n') |
|
|
407 | *p++ = 0; |
|
|
408 | |
|
|
409 | ++linenum; |
|
|
410 | |
|
|
411 | line = p; |
|
|
412 | |
|
|
413 | if (*kw_str && *kw_str != '#') |
|
|
414 | break; |
|
|
415 | |
|
|
416 | // empty/comment line -> skip it |
|
|
417 | } |
|
|
418 | |
|
|
419 | return 1; |
369 | } |
420 | } |
370 | |
421 | |
371 | void |
422 | void |
372 | object_thawer::skip () |
423 | object_thawer::skip () |
373 | { |
424 | { |
… | |
… | |
383 | |
434 | |
384 | next (); |
435 | next (); |
385 | } |
436 | } |
386 | |
437 | |
387 | void |
438 | void |
|
|
439 | object_thawer::skip_block () |
|
|
440 | { |
|
|
441 | // must not stop at KW_ERROR, as those signify custom keys |
|
|
442 | while (kw != KW_EOF) |
|
|
443 | { |
|
|
444 | keyword w = kw; |
|
|
445 | skip (); |
|
|
446 | |
|
|
447 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
448 | skip_block (); |
|
|
449 | else if (w == KW_end) |
|
|
450 | break; |
|
|
451 | } |
|
|
452 | } |
|
|
453 | |
|
|
454 | void |
388 | object_thawer::get_ml (keyword kend, shstr &sh) |
455 | object_thawer::get_ml (keyword kend, shstr &sh) |
389 | { |
456 | { |
390 | char kw[128]; |
457 | char kw[128]; |
391 | |
458 | |
392 | int klen = keyword_len [kend]; |
459 | int klen = keyword_len [kend]; |
… | |
… | |
436 | } |
503 | } |
437 | |
504 | |
438 | sint32 |
505 | sint32 |
439 | object_thawer::get_sint32 () const |
506 | object_thawer::get_sint32 () const |
440 | { |
507 | { |
441 | char *p = value_nn; |
508 | const char *p = value_nn; |
442 | |
509 | |
443 | sint32 val = 0; |
510 | sint32 val = 0; |
444 | bool negate; |
511 | bool negate; |
445 | |
512 | |
446 | if (*p == '-') |
513 | if (*p == '-') |
… | |
… | |
459 | while (*p); |
526 | while (*p); |
460 | |
527 | |
461 | return negate ? -val : val; |
528 | return negate ? -val : val; |
462 | } |
529 | } |
463 | |
530 | |
|
|
531 | void |
|
|
532 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
|
|
533 | { |
|
|
534 | op->refcnt_inc (); |
|
|
535 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
536 | delrefs.push_back (r); |
|
|
537 | ptr = 0; |
|
|
538 | } |
|
|
539 | |
|
|
540 | void |
|
|
541 | object_thawer::resolve_delayed_derefs (bool deref) |
|
|
542 | { |
|
|
543 | while (!delrefs.empty ()) |
|
|
544 | { |
|
|
545 | delayed_ref r = delrefs.back (); |
|
|
546 | delrefs.pop_back (); |
|
|
547 | |
|
|
548 | if (deref) |
|
|
549 | *r.ptr = object::deref (r.ref); |
|
|
550 | |
|
|
551 | free ((void *)r.ref); |
|
|
552 | r.op->refcnt_dec (); |
|
|
553 | } |
|
|
554 | } |