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 (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
9 | * |
5 | * |
10 | * This program is free software; you can redistribute it and/or modify |
6 | * 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 |
7 | * 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 |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
13 | * (at your option) any later version. |
9 | * option) any later version. |
14 | * |
10 | * |
15 | * 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, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
19 | * |
15 | * |
20 | * 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 |
21 | * along with this program; if not, write to the Free Software |
17 | * 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 |
18 | * <http://www.gnu.org/licenses/>. |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
21 | */ |
24 | |
22 | |
25 | #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 |
26 | #include "logger.h" |
24 | #include "logger.h" |
27 | #include "cfperl.h" |
25 | #include "cfperl.h" |
… | |
… | |
37 | { |
35 | { |
38 | SvREFCNT_dec (av); |
36 | SvREFCNT_dec (av); |
39 | } |
37 | } |
40 | |
38 | |
41 | void |
39 | void |
42 | object_freezer::put (attachable *ext) |
40 | object_freezer::put_ (attachable *ext) |
43 | { |
41 | { |
44 | ext->optimise (); |
42 | ext->optimise (); |
45 | |
43 | |
46 | if (ext->self) |
44 | if (ext->self) |
47 | { |
45 | { |
48 | int idx = AvFILLp ((AV *)av) + 1; |
46 | int idx = AvFILLp ((AV *)av) + 1; |
49 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
47 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
50 | |
48 | |
51 | add ((void *)"oid ", 4); |
49 | put (KW(oid), sint32(idx)); |
52 | add ((sint32)idx); |
|
|
53 | add ('\n'); |
|
|
54 | } |
50 | } |
55 | } |
51 | } |
56 | |
52 | |
57 | bool |
53 | bool |
58 | object_freezer::save (const char *path) |
54 | object_freezer::save (const char *path) |
… | |
… | |
108 | } |
104 | } |
109 | #endif |
105 | #endif |
110 | |
106 | |
111 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
112 | |
108 | |
|
|
109 | bool object_thawer::errors_are_fatal = true; |
|
|
110 | |
113 | 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 | { |
114 | : name (strdup (path)) |
136 | name = strdup (path); |
115 | { |
|
|
116 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
117 | |
|
|
118 | av = 0; |
137 | av = 0; |
119 | text = 0; |
138 | text = 0; |
120 | line = 0; |
139 | line = 0; |
121 | linenum = 0; |
140 | linenum = 0; |
122 | |
141 | |
123 | kw = KW_ERROR; |
142 | kw = KW_ERROR; |
124 | kw_str = 0; |
143 | kw_str = 0; |
125 | value = 0; |
144 | value = 0; |
|
|
145 | } |
126 | |
146 | |
|
|
147 | void |
|
|
148 | object_thawer::init_from_file (const_utf8_string path) |
|
|
149 | { |
127 | if (path) |
150 | init (path); |
128 | { |
151 | |
129 | CALL_BEGIN (1); |
152 | CALL_BEGIN (1); |
130 | CALL_ARG_SV (newSVpv (path, 0)); |
153 | CALL_ARG_SV (newSVpv (path, 0)); |
131 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
154 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
132 | |
155 | |
133 | if (count == 2) |
156 | if (count == 2) |
134 | { |
157 | { |
135 | // second value - perl objects |
158 | // second value - perl objects |
136 | { |
159 | { |
137 | SV *sv = POPs; |
160 | SV *sv = POPs; |
138 | if (SvROK (sv)) |
161 | if (SvROK (sv)) |
139 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
162 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
140 | } |
163 | } |
141 | |
164 | |
142 | // first value - text part, pad with 3 zeroes |
165 | // first value - text part, pad with 3 zeroes |
143 | { |
166 | { |
144 | SV *sv = POPs; |
167 | SV *sv = POPs; |
145 | STRLEN len; |
168 | STRLEN len; |
146 | char *sv_ = SvPVbyte (sv, len); |
169 | char *sv_ = SvPVbyte (sv, len); |
147 | text = newSV (len + sizeof (eof)); |
170 | text = newSV (len + sizeof (thawer_eof)); |
148 | SvCUR_set (text, len); |
171 | SvCUR_set (text, len + sizeof (thawer_eof)); |
149 | memcpy (SvPVX (text), sv_, len); |
172 | memcpy (SvPVX (text), sv_, len); |
150 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
173 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
151 | |
174 | |
152 | line = SvPVX (text); |
175 | line = SvPVX (text); |
153 | } |
176 | next (); |
154 | } |
177 | } |
|
|
178 | } |
155 | |
179 | |
156 | CALL_END; |
180 | CALL_END; |
157 | } |
|
|
158 | } |
|
|
159 | |
|
|
160 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
161 | : name (strdup ("(memory stream")) |
|
|
162 | { |
|
|
163 | av = perlav; |
|
|
164 | text = newSVpv (data, 0); |
|
|
165 | sv_catpv (text, thawer_eof); |
|
|
166 | line = SvPVbyte_nolen (text); |
|
|
167 | } |
181 | } |
168 | |
182 | |
169 | void |
183 | void |
170 | object_thawer::get (attachable *obj, int oid) |
184 | object_thawer::get (attachable *obj, int oid) |
171 | { |
185 | { |
… | |
… | |
222 | object_thawer::~object_thawer () |
236 | object_thawer::~object_thawer () |
223 | { |
237 | { |
224 | if (text) SvREFCNT_dec (text); |
238 | if (text) SvREFCNT_dec (text); |
225 | if (av) SvREFCNT_dec (av); |
239 | if (av) SvREFCNT_dec (av); |
226 | |
240 | |
|
|
241 | resolve_delayed_derefs (false); |
|
|
242 | |
227 | free ((void *)name); |
243 | free ((void *)name); |
228 | } |
244 | } |
229 | |
245 | |
230 | //TODO: remove |
246 | static void |
231 | char * |
247 | error_out () |
232 | fgets (char *s, int n, object_thawer &thawer) |
|
|
233 | { |
248 | { |
234 | char *p = thawer.line; |
249 | if (object_thawer::errors_are_fatal) |
235 | char *q = s; |
|
|
236 | |
|
|
237 | if (!p) |
|
|
238 | return 0; |
|
|
239 | |
|
|
240 | while (--n) |
|
|
241 | { |
|
|
242 | if (!*p) |
|
|
243 | break; |
|
|
244 | |
|
|
245 | *q++ = *p; |
|
|
246 | |
|
|
247 | if (*p++ == '\n') |
|
|
248 | { |
|
|
249 | ++thawer.linenum; |
|
|
250 | break; |
|
|
251 | } |
|
|
252 | } |
250 | { |
|
|
251 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
|
|
252 | exit (1); |
|
|
253 | } |
|
|
254 | } |
253 | |
255 | |
254 | *q = 0; |
256 | void |
255 | thawer.line = p; |
257 | object_thawer::parse_warn (const char *msg) const |
256 | |
258 | { |
257 | return s == q ? 0 : s; |
259 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
|
|
260 | this->name, linenum, |
|
|
261 | kw_str ? kw_str : "<null>", |
|
|
262 | value ? value : "<null>", |
|
|
263 | msg); |
258 | } |
264 | } |
259 | |
265 | |
260 | bool |
266 | bool |
261 | 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 |
262 | { |
268 | { |
263 | if (!type) type = "file section"; |
269 | if (!type) type = "file section"; |
264 | if (!name) name = "generic"; |
270 | if (!name) name = "generic"; |
265 | |
271 | |
266 | switch (kw) |
272 | switch (kw) |
267 | { |
273 | { |
268 | case KW_EOF: |
274 | case KW_EOF: |
269 | 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", |
270 | this->name, linenum, type, name); |
276 | this->name, linenum, type, name); |
|
|
277 | error_out (); |
271 | return false; |
278 | return false; |
272 | |
279 | |
273 | case KW_ERROR: |
280 | case KW_ERROR: |
274 | 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", |
275 | this->name, linenum, |
282 | this->name, linenum, |
276 | type, name, |
283 | type, name, |
277 | kw_str ? kw_str : "<file load>"); |
284 | kw_str ? kw_str : "<file load>"); |
|
|
285 | error_out (); |
278 | return false; |
286 | return false; |
279 | |
287 | |
280 | default: |
288 | default: |
281 | 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", |
282 | this->name, linenum, |
290 | this->name, linenum, |
283 | kw_str ? kw_str : "<null>", |
291 | kw_str ? kw_str : "<null>", |
284 | value ? value : "<null>", |
292 | value ? value : "<null>", |
285 | type, name, |
293 | type, name, |
286 | skip ? "skipping line" : "aborting load"); |
294 | skip ? "skipping line" : "aborting load"); |
|
|
295 | error_out (); |
287 | return skip; |
296 | return skip; |
288 | } |
297 | } |
289 | } |
298 | } |
290 | |
299 | |
291 | void |
300 | void |
… | |
… | |
299 | |
308 | |
300 | for (;;) |
309 | for (;;) |
301 | { |
310 | { |
302 | char *p = line; |
311 | char *p = line; |
303 | |
312 | |
304 | if (*p <= ' ') |
313 | if (expect_false (*p <= ' ')) |
305 | { |
314 | { |
306 | // skip whitespace (only some files need this) |
315 | // skip whitespace (only some files need this) |
307 | while (*p == ' ' || *p == '\t') |
316 | while (*p == ' ' || *p == '\t') |
308 | p++; |
317 | p++; |
309 | |
318 | |
… | |
… | |
357 | { |
366 | { |
358 | kw = KW_ERROR; |
367 | kw = KW_ERROR; |
359 | break; |
368 | break; |
360 | } |
369 | } |
361 | } |
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; |
362 | } |
424 | } |
363 | |
425 | |
364 | void |
426 | void |
365 | object_thawer::skip () |
427 | object_thawer::skip () |
366 | { |
428 | { |
… | |
… | |
376 | |
438 | |
377 | next (); |
439 | next (); |
378 | } |
440 | } |
379 | |
441 | |
380 | void |
442 | void |
|
|
443 | object_thawer::skip_block () |
|
|
444 | { |
|
|
445 | // must not stop at KW_ERROR, as those signify custom keys |
|
|
446 | while (kw != KW_EOF) |
|
|
447 | { |
|
|
448 | keyword w = kw; |
|
|
449 | skip (); |
|
|
450 | |
|
|
451 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
452 | skip_block (); |
|
|
453 | else if (w == KW_end) |
|
|
454 | break; |
|
|
455 | } |
|
|
456 | } |
|
|
457 | |
|
|
458 | void |
381 | object_thawer::get_ml (keyword kend, shstr &sh) |
459 | object_thawer::get_ml (keyword kend, shstr &sh) |
382 | { |
460 | { |
383 | char kw[128]; |
461 | char kw[128]; |
384 | |
462 | |
385 | int klen = keyword_len [kend]; |
463 | int klen = keyword_len [kend]; |
… | |
… | |
429 | } |
507 | } |
430 | |
508 | |
431 | sint32 |
509 | sint32 |
432 | object_thawer::get_sint32 () const |
510 | object_thawer::get_sint32 () const |
433 | { |
511 | { |
434 | char *p = value_nn; |
512 | const char *p = value_nn; |
435 | |
513 | |
436 | sint32 val = 0; |
514 | sint32 val = 0; |
437 | bool negate; |
515 | bool negate; |
438 | |
516 | |
439 | if (*p == '-') |
517 | if (*p == '-') |
… | |
… | |
452 | while (*p); |
530 | while (*p); |
453 | |
531 | |
454 | return negate ? -val : val; |
532 | return negate ? -val : val; |
455 | } |
533 | } |
456 | |
534 | |
|
|
535 | void |
|
|
536 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
|
|
537 | { |
|
|
538 | op->refcnt_inc (); |
|
|
539 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
540 | delrefs.push_back (r); |
|
|
541 | ptr = 0; |
|
|
542 | } |
|
|
543 | |
|
|
544 | void |
|
|
545 | object_thawer::resolve_delayed_derefs (bool deref) |
|
|
546 | { |
|
|
547 | while (!delrefs.empty ()) |
|
|
548 | { |
|
|
549 | delayed_ref r = delrefs.back (); |
|
|
550 | delrefs.pop_back (); |
|
|
551 | |
|
|
552 | if (deref) |
|
|
553 | *r.ptr = object::deref (r.ref); |
|
|
554 | |
|
|
555 | free ((void *)r.ref); |
|
|
556 | r.op->refcnt_dec (); |
|
|
557 | } |
|
|
558 | } |