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 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" |
28 | #include "kw_hash.h" |
26 | #include "kw_hash.h" |
29 | |
27 | |
30 | object_freezer::object_freezer () |
28 | object_freezer::object_freezer () |
31 | : dynbuf (128 * 1024, 64 * 1024) |
29 | : dynbuf_text (128 * 1024, 64 * 1024) |
32 | { |
30 | { |
33 | av = newAV (); |
31 | av = newAV (); |
34 | } |
32 | } |
35 | |
33 | |
36 | object_freezer::~object_freezer () |
34 | object_freezer::~object_freezer () |
… | |
… | |
61 | CALL_ARG_SV (newSVpv (path, 0)); |
59 | CALL_ARG_SV (newSVpv (path, 0)); |
62 | CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
60 | CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
63 | CALL_ARG_SV (newRV_inc ((SV *)av)); |
61 | CALL_ARG_SV (newRV_inc ((SV *)av)); |
64 | CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
62 | CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
65 | CALL_END; |
63 | CALL_END; |
|
|
64 | |
|
|
65 | return 1; |
66 | } |
66 | } |
67 | |
67 | |
68 | char * |
68 | char * |
69 | object_freezer::as_string () |
69 | object_freezer::as_string () |
70 | { |
70 | { |
… | |
… | |
80 | CALL_END; |
80 | CALL_END; |
81 | |
81 | |
82 | return res; |
82 | return res; |
83 | } |
83 | } |
84 | |
84 | |
85 | int |
85 | #if 0 |
|
|
86 | void |
86 | fprintf (object_freezer &freezer, const char *format, ...) |
87 | fprintf (object_freezer &freezer, const char *format, ...) |
87 | { |
88 | { |
88 | va_list ap; |
89 | va_list ap; |
89 | |
90 | |
90 | va_start (ap, format); |
91 | va_start (ap, format); |
… | |
… | |
95 | freezer.alloc (len); |
96 | freezer.alloc (len); |
96 | |
97 | |
97 | va_end (ap); |
98 | va_end (ap); |
98 | } |
99 | } |
99 | |
100 | |
100 | int |
101 | // XXX: function not returning an int |
|
|
102 | void |
101 | fputs (const char *s, object_freezer &freezer) |
103 | fputs (const char *s, object_freezer &freezer) |
102 | { |
104 | { |
103 | freezer.add (s); |
105 | freezer.add (s); |
104 | } |
106 | } |
|
|
107 | #endif |
105 | |
108 | |
106 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
109 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | |
110 | |
108 | object_thawer::object_thawer (const char *path) |
111 | object_thawer::object_thawer (const char *path) |
109 | : name (strdup (path)) |
112 | : name (strdup (path)) |
110 | { |
113 | { |
111 | static const char eof[] = "\n\n\n\0\0\0"; |
114 | static const char eof[] = "\n\n\n\0\0\0"; |
112 | |
115 | |
113 | av = 0; |
116 | av = 0; |
114 | text = 0; |
117 | text = 0; |
115 | line = 0; |
118 | line = 0; |
116 | linenum = 0; |
119 | linenum = 0; |
|
|
120 | |
|
|
121 | kw = KW_ERROR; |
|
|
122 | kw_str = 0; |
|
|
123 | value = 0; |
117 | |
124 | |
118 | if (path) |
125 | if (path) |
119 | { |
126 | { |
120 | CALL_BEGIN (1); |
127 | CALL_BEGIN (1); |
121 | CALL_ARG_SV (newSVpv (path, 0)); |
128 | CALL_ARG_SV (newSVpv (path, 0)); |
… | |
… | |
134 | { |
141 | { |
135 | SV *sv = POPs; |
142 | SV *sv = POPs; |
136 | STRLEN len; |
143 | STRLEN len; |
137 | char *sv_ = SvPVbyte (sv, len); |
144 | char *sv_ = SvPVbyte (sv, len); |
138 | text = newSV (len + sizeof (eof)); |
145 | text = newSV (len + sizeof (eof)); |
139 | SvCUR_set (text, len); |
146 | SvCUR_set (text, len + sizeof (eof)); |
140 | memcpy (SvPVX (text), sv_, len); |
147 | memcpy (SvPVX (text), sv_, len); |
141 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
148 | memcpy (SvPVX (text) + len, eof, sizeof (eof)); |
142 | |
149 | |
143 | line = SvPVX (text); |
150 | line = SvPVX (text); |
|
|
151 | next (); |
144 | } |
152 | } |
145 | } |
153 | } |
146 | |
154 | |
147 | CALL_END; |
155 | CALL_END; |
148 | } |
156 | } |
149 | } |
157 | } |
150 | |
158 | |
151 | object_thawer::object_thawer (const char *data, AV *perlav) |
159 | object_thawer::object_thawer (const char *data, AV *perlav) |
152 | : name (strdup ("(memory stream")) |
160 | : name (strdup ("(memory stream)")) |
153 | { |
161 | { |
154 | av = perlav; |
162 | av = perlav; |
155 | text = newSVpv (data, 0); |
163 | text = newSVpv (data, 0); |
156 | sv_catpv (text, thawer_eof); |
164 | sv_catpv (text, thawer_eof); |
157 | line = SvPVbyte_nolen (text); |
165 | line = SvPVbyte_nolen (text); |
|
|
166 | next (); |
158 | } |
167 | } |
159 | |
168 | |
160 | void |
169 | void |
161 | object_thawer::get (attachable *obj, int oid) |
170 | object_thawer::get (attachable *obj, int oid) |
162 | { |
171 | { |
… | |
… | |
165 | |
174 | |
166 | SV **svp = av_fetch ((AV *)av, oid, 0); |
175 | SV **svp = av_fetch ((AV *)av, oid, 0); |
167 | |
176 | |
168 | if (!svp || !SvROK (*svp)) |
177 | if (!svp || !SvROK (*svp)) |
169 | { |
178 | { |
170 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
179 | LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
171 | return; |
180 | return; |
172 | } |
181 | } |
173 | |
182 | |
174 | if (!SvROK (*svp)) |
183 | if (!SvROK (*svp)) |
175 | { |
184 | { |
176 | LOG (llevError, "deserialised perl object is not an RV"); |
185 | LOG (llevError, "deserialised perl object is not an RV\n"); |
177 | return; |
186 | return; |
178 | } |
187 | } |
179 | |
188 | |
180 | HV *hv = (HV *)SvRV (*svp); |
189 | HV *hv = (HV *)SvRV (*svp); |
181 | |
190 | |
182 | if (SvTYPE (hv) != SVt_PVHV) |
191 | if (SvTYPE (hv) != SVt_PVHV) |
183 | { |
192 | { |
184 | LOG (llevError, "deserialised perl object is not a PVHV"); |
193 | LOG (llevError, "deserialised perl object is not a PVHV\n"); |
185 | return; |
194 | return; |
186 | } |
195 | } |
187 | |
196 | |
188 | if (obj->self) |
197 | if (obj->self) |
189 | { |
198 | { |
… | |
… | |
213 | object_thawer::~object_thawer () |
222 | object_thawer::~object_thawer () |
214 | { |
223 | { |
215 | if (text) SvREFCNT_dec (text); |
224 | if (text) SvREFCNT_dec (text); |
216 | if (av) SvREFCNT_dec (av); |
225 | if (av) SvREFCNT_dec (av); |
217 | |
226 | |
|
|
227 | resolve_delayed_derefs (false); |
|
|
228 | |
218 | free ((void *)name); |
229 | free ((void *)name); |
219 | } |
230 | } |
220 | |
231 | |
221 | //TODO: remove |
232 | //TODO: remove |
222 | char * |
233 | char * |
… | |
… | |
246 | thawer.line = p; |
257 | thawer.line = p; |
247 | |
258 | |
248 | return s == q ? 0 : s; |
259 | return s == q ? 0 : s; |
249 | } |
260 | } |
250 | |
261 | |
|
|
262 | void |
|
|
263 | object_thawer::parse_warn (const char *msg) |
|
|
264 | { |
|
|
265 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
|
|
266 | this->name, linenum, |
|
|
267 | kw_str ? kw_str : "<null>", |
|
|
268 | value ? value : "<null>", |
|
|
269 | msg); |
|
|
270 | } |
|
|
271 | |
251 | bool |
272 | bool |
252 | object_thawer::parse_error (keyword kw, const char *type, const char *name, bool skip) |
273 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
253 | { |
274 | { |
254 | if (!type) type = "file section"; |
275 | if (!type) type = "file section"; |
255 | if (!name) name = "generic"; |
276 | if (!name) name = "generic"; |
256 | |
277 | |
257 | switch (kw) |
278 | switch (kw) |
… | |
… | |
260 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
281 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
261 | this->name, linenum, type, name); |
282 | this->name, linenum, type, name); |
262 | return false; |
283 | return false; |
263 | |
284 | |
264 | case KW_ERROR: |
285 | case KW_ERROR: |
265 | LOG (llevError, "%s:%d errornous line (%s) while reading %s '%s', aborting load.\n", |
286 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
266 | this->name, linenum, |
287 | this->name, linenum, |
267 | last_keyword ? last_keyword : "<null>", |
|
|
268 | type, name); |
288 | type, name, |
|
|
289 | kw_str ? kw_str : "<file load>"); |
269 | return false; |
290 | return false; |
270 | |
291 | |
271 | default: |
292 | default: |
272 | LOG (llevError, "%s:%d unexpected line (%s) while reading %s '%s', %s.\n", |
293 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
273 | this->name, linenum, |
294 | this->name, linenum, |
274 | last_keyword ? last_keyword : "<null>", |
295 | kw_str ? kw_str : "<null>", |
275 | last_value ? last_value : "<null>", |
296 | value ? value : "<null>", |
276 | type, name, |
297 | type, name, |
277 | skip ? "skipping line" : "aborting load"); |
298 | skip ? "skipping line" : "aborting load"); |
278 | return skip; |
299 | return skip; |
279 | } |
300 | } |
280 | } |
301 | } |
281 | |
302 | |
282 | keyword |
303 | void |
283 | object_thawer::get_kv () |
304 | object_thawer::next () |
284 | { |
305 | { |
285 | if (!line) |
306 | if (!line) |
286 | return KW_EOF; |
307 | { |
|
|
308 | kw = KW_ERROR; |
|
|
309 | return; |
|
|
310 | } |
287 | |
311 | |
288 | for (;;) |
312 | for (;;) |
289 | { |
313 | { |
290 | char *p = line; |
314 | char *p = line; |
291 | |
315 | |
|
|
316 | if (*p <= ' ') |
|
|
317 | { |
|
|
318 | // skip whitespace (only some files need this) |
|
|
319 | while (*p == ' ' || *p == '\t') |
|
|
320 | p++; |
|
|
321 | |
|
|
322 | line = p; |
|
|
323 | } |
|
|
324 | |
292 | if (!*p) |
325 | if (!*p) |
293 | return KW_EOF; |
326 | { |
|
|
327 | kw = KW_EOF; |
|
|
328 | break; |
|
|
329 | } |
294 | |
330 | |
295 | // parse keyword |
331 | // parse keyword |
296 | while (*p > ' ') |
332 | while (*p > ' ') |
297 | p++; |
333 | p++; |
298 | |
334 | |
299 | int klen = p - line; |
335 | int klen = p - line; |
300 | |
336 | |
|
|
337 | value_nn = ""; |
|
|
338 | value = 0; |
|
|
339 | |
301 | if (*p++ != '\n') |
340 | if (*p++ != '\n') |
302 | { |
341 | { |
303 | // parse value |
342 | // parse value |
304 | while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
343 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
305 | ++p; |
344 | ++p; |
306 | |
345 | |
307 | last_value = p; |
346 | value_nn = value = p; |
308 | |
347 | |
309 | while (*p != '\n') |
348 | while (*p != '\n') |
310 | p++; |
349 | p++; |
311 | |
350 | |
312 | *p++ = 0; |
351 | *p++ = 0; |
313 | } |
352 | } |
314 | else |
|
|
315 | last_value = 0; |
|
|
316 | |
353 | |
317 | ++linenum; |
354 | ++linenum; |
318 | line [klen] = 0; |
355 | line [klen] = 0; |
319 | keyword_idx *kw = kw_lex::match (line, klen); |
356 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
320 | |
357 | |
321 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D |
358 | kw_str = line; |
322 | |
|
|
323 | last_keyword = line; |
|
|
324 | line = p; |
359 | line = p; |
325 | |
360 | |
326 | if (kw) |
361 | if (kw_idx) |
327 | return kw->index; |
362 | { |
|
|
363 | kw = kw_idx->index; |
|
|
364 | break; |
|
|
365 | } |
328 | else if (!*last_keyword || *last_keyword == '#') |
366 | else if (!*kw_str || *kw_str == '#') |
329 | ; // empty/comment line |
367 | ; // empty/comment line |
330 | else |
368 | else |
|
|
369 | { |
331 | return KW_ERROR; |
370 | kw = KW_ERROR; |
|
|
371 | break; |
|
|
372 | } |
332 | } |
373 | } |
333 | } |
374 | } |
334 | |
375 | |
335 | void |
376 | void |
336 | object_thawer::skip_kv (keyword kw) |
377 | object_thawer::skip () |
337 | { |
378 | { |
338 | shstr ml; |
379 | shstr ml; |
339 | |
380 | |
340 | switch (kw) |
381 | switch (kw) |
341 | { |
382 | { |
342 | case KW_msg: get_ml (KW_endmsg , ml); break; |
383 | case KW_msg: get_ml (KW_endmsg , ml); break; |
343 | case KW_lore: get_ml (KW_endlore , ml); break; |
384 | case KW_lore: get_ml (KW_endlore , ml); break; |
344 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
385 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
|
|
386 | default: break; |
345 | } |
387 | } |
|
|
388 | |
|
|
389 | next (); |
346 | } |
390 | } |
347 | |
391 | |
348 | void |
392 | void |
349 | object_thawer::get (shstr &sh) const |
393 | object_thawer::skip_block () |
350 | { |
394 | { |
351 | if (last_value) |
395 | // must not stop at KW_ERROR, as those signify custom keys |
352 | sh = last_value; |
396 | while (kw != KW_EOF) |
353 | else |
|
|
354 | { |
397 | { |
355 | sh = "<value missing>"; |
398 | keyword w = kw; |
356 | LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", last_keyword);//TODO: add filename |
399 | skip (); |
|
|
400 | |
|
|
401 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
402 | skip_block (); |
|
|
403 | else if (w == KW_end) |
|
|
404 | break; |
357 | } |
405 | } |
358 | } |
406 | } |
359 | |
407 | |
360 | void |
408 | void |
361 | object_thawer::get_ml (keyword kend, shstr &sh) |
409 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
409 | } |
457 | } |
410 | |
458 | |
411 | sint32 |
459 | sint32 |
412 | object_thawer::get_sint32 () const |
460 | object_thawer::get_sint32 () const |
413 | { |
461 | { |
414 | char *p = last_value; |
462 | const char *p = value_nn; |
415 | |
|
|
416 | if (!p) |
|
|
417 | return 0; |
|
|
418 | |
463 | |
419 | sint32 val = 0; |
464 | sint32 val = 0; |
420 | bool negate; |
465 | bool negate; |
421 | |
466 | |
422 | if (*p == '-') |
467 | if (*p == '-') |
… | |
… | |
435 | while (*p); |
480 | while (*p); |
436 | |
481 | |
437 | return negate ? -val : val; |
482 | return negate ? -val : val; |
438 | } |
483 | } |
439 | |
484 | |
440 | sint64 |
485 | void |
441 | object_thawer::get_sint64 () const |
486 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
442 | { |
487 | { |
443 | return last_value ? atoll (last_value) : 0; |
488 | op->refcnt_inc (); |
|
|
489 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
490 | delrefs.push_back (r); |
|
|
491 | ptr = 0; |
444 | } |
492 | } |
445 | |
493 | |
446 | double |
494 | void |
447 | object_thawer::get_double () const |
495 | object_thawer::resolve_delayed_derefs (bool deref) |
448 | { |
496 | { |
449 | return last_value ? atof (last_value) : 0; |
497 | while (!delrefs.empty ()) |
450 | } |
498 | { |
|
|
499 | delayed_ref r = delrefs.back (); |
|
|
500 | delrefs.pop_back (); |
451 | |
501 | |
|
|
502 | if (deref) |
|
|
503 | *r.ptr = object::deref (r.ref); |
|
|
504 | |
|
|
505 | free ((void *)r.ref); |
|
|
506 | r.op->refcnt_dec (); |
|
|
507 | } |
|
|
508 | } |