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