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 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) |
… | |
… | |
82 | CALL_END; |
78 | CALL_END; |
83 | |
79 | |
84 | return res; |
80 | return res; |
85 | } |
81 | } |
86 | |
82 | |
|
|
83 | #if 0 |
87 | void |
84 | void |
88 | fprintf (object_freezer &freezer, const char *format, ...) |
85 | fprintf (object_freezer &freezer, const char *format, ...) |
89 | { |
86 | { |
90 | va_list ap; |
87 | va_list ap; |
91 | |
88 | |
… | |
… | |
103 | void |
100 | void |
104 | fputs (const char *s, object_freezer &freezer) |
101 | fputs (const char *s, object_freezer &freezer) |
105 | { |
102 | { |
106 | freezer.add (s); |
103 | freezer.add (s); |
107 | } |
104 | } |
|
|
105 | #endif |
108 | |
106 | |
109 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
110 | |
108 | |
|
|
109 | bool object_thawer::errors_are_fatal = true; |
|
|
110 | |
111 | 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 | { |
112 | : name (strdup (path)) |
135 | name = strdup (path); |
113 | { |
|
|
114 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
115 | |
|
|
116 | av = 0; |
136 | av = 0; |
117 | text = 0; |
137 | text = 0; |
118 | line = 0; |
138 | line = 0; |
119 | linenum = 0; |
139 | linenum = 0; |
120 | |
140 | |
… | |
… | |
140 | // first value - text part, pad with 3 zeroes |
160 | // first value - text part, pad with 3 zeroes |
141 | { |
161 | { |
142 | SV *sv = POPs; |
162 | SV *sv = POPs; |
143 | STRLEN len; |
163 | STRLEN len; |
144 | char *sv_ = SvPVbyte (sv, len); |
164 | char *sv_ = SvPVbyte (sv, len); |
145 | text = newSV (len + sizeof (eof)); |
165 | text = newSV (len + sizeof (thawer_eof)); |
146 | SvCUR_set (text, len); |
166 | SvCUR_set (text, len + sizeof (thawer_eof)); |
147 | memcpy (SvPVX (text), sv_, len); |
167 | memcpy (SvPVX (text), sv_, len); |
148 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
168 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
149 | |
169 | |
150 | line = SvPVX (text); |
170 | line = SvPVX (text); |
|
|
171 | next (); |
151 | } |
172 | } |
152 | } |
173 | } |
153 | |
174 | |
154 | CALL_END; |
175 | CALL_END; |
155 | } |
176 | } |
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 | } |
177 | } |
166 | |
178 | |
167 | void |
179 | void |
168 | object_thawer::get (attachable *obj, int oid) |
180 | object_thawer::get (attachable *obj, int oid) |
169 | { |
181 | { |
… | |
… | |
220 | object_thawer::~object_thawer () |
232 | object_thawer::~object_thawer () |
221 | { |
233 | { |
222 | if (text) SvREFCNT_dec (text); |
234 | if (text) SvREFCNT_dec (text); |
223 | if (av) SvREFCNT_dec (av); |
235 | if (av) SvREFCNT_dec (av); |
224 | |
236 | |
|
|
237 | resolve_delayed_derefs (false); |
|
|
238 | |
225 | free ((void *)name); |
239 | free ((void *)name); |
226 | } |
240 | } |
227 | |
241 | |
228 | //TODO: remove |
242 | static void |
229 | char * |
243 | error_out () |
230 | fgets (char *s, int n, object_thawer &thawer) |
|
|
231 | { |
244 | { |
232 | char *p = thawer.line; |
245 | if (object_thawer::errors_are_fatal) |
233 | char *q = s; |
|
|
234 | |
|
|
235 | if (!p) |
|
|
236 | return 0; |
|
|
237 | |
|
|
238 | while (--n) |
|
|
239 | { |
|
|
240 | if (!*p) |
|
|
241 | break; |
|
|
242 | |
|
|
243 | *q++ = *p; |
|
|
244 | |
|
|
245 | if (*p++ == '\n') |
|
|
246 | { |
|
|
247 | ++thawer.linenum; |
|
|
248 | break; |
|
|
249 | } |
|
|
250 | } |
246 | { |
|
|
247 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
|
|
248 | exit (1); |
|
|
249 | } |
|
|
250 | } |
251 | |
251 | |
252 | *q = 0; |
252 | void |
253 | thawer.line = p; |
253 | object_thawer::parse_warn (const char *msg) const |
254 | |
254 | { |
255 | return s == q ? 0 : s; |
255 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
|
|
256 | this->name, linenum, |
|
|
257 | kw_str ? kw_str : "<null>", |
|
|
258 | value ? value : "<null>", |
|
|
259 | msg); |
256 | } |
260 | } |
257 | |
261 | |
258 | bool |
262 | bool |
259 | 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 |
260 | { |
264 | { |
261 | if (!type) type = "file section"; |
265 | if (!type) type = "file section"; |
262 | if (!name) name = "generic"; |
266 | if (!name) name = "generic"; |
263 | |
267 | |
264 | switch (kw) |
268 | switch (kw) |
265 | { |
269 | { |
266 | case KW_EOF: |
270 | case KW_EOF: |
267 | 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", |
268 | this->name, linenum, type, name); |
272 | this->name, linenum, type, name); |
|
|
273 | error_out (); |
269 | return false; |
274 | return false; |
270 | |
275 | |
271 | case KW_ERROR: |
276 | case KW_ERROR: |
272 | 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", |
273 | this->name, linenum, |
278 | this->name, linenum, |
274 | type, name, |
279 | type, name, |
275 | kw_str ? kw_str : "<file load>"); |
280 | kw_str ? kw_str : "<file load>"); |
|
|
281 | error_out (); |
276 | return false; |
282 | return false; |
277 | |
283 | |
278 | default: |
284 | default: |
279 | 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", |
280 | this->name, linenum, |
286 | this->name, linenum, |
281 | kw_str ? kw_str : "<null>", |
287 | kw_str ? kw_str : "<null>", |
282 | value ? value : "<null>", |
288 | value ? value : "<null>", |
283 | type, name, |
289 | type, name, |
284 | skip ? "skipping line" : "aborting load"); |
290 | skip ? "skipping line" : "aborting load"); |
|
|
291 | error_out (); |
285 | return skip; |
292 | return skip; |
286 | } |
293 | } |
287 | } |
294 | } |
288 | |
295 | |
289 | void |
296 | void |
… | |
… | |
297 | |
304 | |
298 | for (;;) |
305 | for (;;) |
299 | { |
306 | { |
300 | char *p = line; |
307 | char *p = line; |
301 | |
308 | |
302 | if (*p <= ' ') |
309 | if (expect_false (*p <= ' ')) |
303 | { |
310 | { |
304 | // skip whitespace (only some files need this) |
311 | // skip whitespace (only some files need this) |
305 | while (*p == ' ' || *p == '\t') |
312 | while (*p == ' ' || *p == '\t') |
306 | p++; |
313 | p++; |
307 | |
314 | |
… | |
… | |
318 | while (*p > ' ') |
325 | while (*p > ' ') |
319 | p++; |
326 | p++; |
320 | |
327 | |
321 | int klen = p - line; |
328 | int klen = p - line; |
322 | |
329 | |
|
|
330 | value_nn = ""; |
323 | value = 0; |
331 | value = 0; |
324 | |
332 | |
325 | if (*p++ != '\n') |
333 | if (*p++ != '\n') |
326 | { |
334 | { |
327 | // parse value |
335 | // parse value |
328 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
336 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
329 | ++p; |
337 | ++p; |
330 | |
338 | |
331 | value = p; |
339 | value_nn = value = p; |
332 | |
340 | |
333 | while (*p != '\n') |
341 | while (*p != '\n') |
334 | p++; |
342 | p++; |
335 | |
343 | |
336 | *p++ = 0; |
344 | *p++ = 0; |
… | |
… | |
354 | { |
362 | { |
355 | kw = KW_ERROR; |
363 | kw = KW_ERROR; |
356 | break; |
364 | break; |
357 | } |
365 | } |
358 | } |
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; |
359 | } |
420 | } |
360 | |
421 | |
361 | void |
422 | void |
362 | object_thawer::skip () |
423 | object_thawer::skip () |
363 | { |
424 | { |
… | |
… | |
373 | |
434 | |
374 | next (); |
435 | next (); |
375 | } |
436 | } |
376 | |
437 | |
377 | void |
438 | void |
378 | object_thawer::get (shstr &sh) const |
439 | object_thawer::skip_block () |
379 | { |
440 | { |
380 | if (value) |
441 | // must not stop at KW_ERROR, as those signify custom keys |
381 | sh = value; |
442 | while (kw != KW_EOF) |
382 | else |
|
|
383 | { |
443 | { |
384 | sh = "<value missing>"; |
444 | keyword w = kw; |
385 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename |
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; |
386 | } |
451 | } |
387 | } |
452 | } |
388 | |
453 | |
389 | void |
454 | void |
390 | object_thawer::get_ml (keyword kend, shstr &sh) |
455 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
438 | } |
503 | } |
439 | |
504 | |
440 | sint32 |
505 | sint32 |
441 | object_thawer::get_sint32 () const |
506 | object_thawer::get_sint32 () const |
442 | { |
507 | { |
443 | char *p = value; |
508 | const char *p = value_nn; |
444 | |
|
|
445 | if (!p) |
|
|
446 | return 0; |
|
|
447 | |
509 | |
448 | sint32 val = 0; |
510 | sint32 val = 0; |
449 | bool negate; |
511 | bool negate; |
450 | |
512 | |
451 | if (*p == '-') |
513 | if (*p == '-') |
… | |
… | |
464 | while (*p); |
526 | while (*p); |
465 | |
527 | |
466 | return negate ? -val : val; |
528 | return negate ? -val : val; |
467 | } |
529 | } |
468 | |
530 | |
469 | sint64 |
531 | void |
470 | object_thawer::get_sint64 () const |
532 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
471 | { |
533 | { |
472 | return value ? atoll (value) : 0; |
534 | op->refcnt_inc (); |
|
|
535 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
536 | delrefs.push_back (r); |
|
|
537 | ptr = 0; |
473 | } |
538 | } |
474 | |
539 | |
475 | double |
540 | void |
476 | object_thawer::get_double () const |
541 | object_thawer::resolve_delayed_derefs (bool deref) |
477 | { |
542 | { |
478 | return value ? atof (value) : 0; |
543 | while (!delrefs.empty ()) |
479 | } |
544 | { |
|
|
545 | delayed_ref r = delrefs.back (); |
|
|
546 | delrefs.pop_back (); |
480 | |
547 | |
|
|
548 | if (deref) |
|
|
549 | *r.ptr = object::deref (r.ref); |
|
|
550 | |
|
|
551 | free ((void *)r.ref); |
|
|
552 | r.op->refcnt_dec (); |
|
|
553 | } |
|
|
554 | } |