1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * 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 Affero GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
19 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
21 | */ |
22 | |
22 | |
23 | #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 |
24 | #include "logger.h" |
24 | #include "logger.h" |
… | |
… | |
35 | { |
35 | { |
36 | SvREFCNT_dec (av); |
36 | SvREFCNT_dec (av); |
37 | } |
37 | } |
38 | |
38 | |
39 | void |
39 | void |
40 | object_freezer::put (attachable *ext) |
40 | object_freezer::put_ (attachable *ext) |
41 | { |
41 | { |
42 | ext->optimise (); |
42 | ext->optimise (); |
43 | |
43 | |
44 | if (ext->self) |
44 | if (ext->self) |
45 | { |
45 | { |
46 | int idx = AvFILLp ((AV *)av) + 1; |
46 | int idx = AvFILLp ((AV *)av) + 1; |
47 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
47 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 | |
48 | |
49 | add ((void *)"oid ", 4); |
49 | put (KW(oid), sint32(idx)); |
50 | add ((sint32)idx); |
|
|
51 | add ('\n'); |
|
|
52 | } |
50 | } |
53 | } |
51 | } |
54 | |
52 | |
55 | bool |
53 | bool |
56 | object_freezer::save (const char *path) |
54 | object_freezer::save (const char *path) |
… | |
… | |
106 | } |
104 | } |
107 | #endif |
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 | 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 | { |
112 | : name (strdup (path)) |
136 | name = strdup (path); |
113 | { |
|
|
114 | av = 0; |
137 | av = 0; |
115 | text = 0; |
138 | text = 0; |
116 | line = 0; |
139 | line = 0; |
117 | linenum = 0; |
140 | linenum = 0; |
118 | |
141 | |
119 | kw = KW_ERROR; |
142 | kw = KW_ERROR; |
120 | kw_str = 0; |
143 | kw_str = 0; |
121 | value = 0; |
144 | value = 0; |
|
|
145 | } |
122 | |
146 | |
|
|
147 | void |
|
|
148 | object_thawer::init_from_file (const_utf8_string path) |
|
|
149 | { |
123 | if (path) |
150 | init (path); |
124 | { |
151 | |
125 | CALL_BEGIN (1); |
152 | CALL_BEGIN (1); |
126 | CALL_ARG_SV (newSVpv (path, 0)); |
153 | CALL_ARG_SV (newSVpv (path, 0)); |
127 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
154 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
128 | |
155 | |
129 | if (count == 2) |
156 | if (count == 2) |
130 | { |
157 | { |
131 | // second value - perl objects |
158 | // second value - perl objects |
132 | { |
159 | { |
133 | SV *sv = POPs; |
160 | SV *sv = POPs; |
134 | if (SvROK (sv)) |
161 | if (SvROK (sv)) |
135 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
162 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
136 | } |
163 | } |
137 | |
164 | |
138 | // first value - text part, pad with 3 zeroes |
165 | // first value - text part, pad with 3 zeroes |
139 | { |
166 | { |
140 | SV *sv = POPs; |
167 | SV *sv = POPs; |
141 | STRLEN len; |
168 | STRLEN len; |
142 | char *sv_ = SvPVbyte (sv, len); |
169 | char *sv_ = SvPVbyte (sv, len); |
143 | text = newSV (len + sizeof (thawer_eof)); |
170 | text = newSV (len + sizeof (thawer_eof)); |
144 | SvCUR_set (text, len + sizeof (thawer_eof)); |
171 | SvCUR_set (text, len + sizeof (thawer_eof)); |
145 | memcpy (SvPVX (text), sv_, len); |
172 | memcpy (SvPVX (text), sv_, len); |
146 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
173 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
147 | |
174 | |
148 | line = SvPVX (text); |
175 | line = SvPVX (text); |
149 | next (); |
176 | next (); |
150 | } |
|
|
151 | } |
177 | } |
|
|
178 | } |
152 | |
179 | |
153 | CALL_END; |
180 | CALL_END; |
154 | } |
|
|
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 | next (); |
|
|
165 | } |
181 | } |
166 | |
182 | |
167 | void |
183 | void |
168 | object_thawer::get (attachable *obj, int oid) |
184 | object_thawer::get (attachable *obj, int oid) |
169 | { |
185 | { |
… | |
… | |
225 | resolve_delayed_derefs (false); |
241 | resolve_delayed_derefs (false); |
226 | |
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 | { |
253 | |
251 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
254 | *q = 0; |
252 | exit (1); |
255 | thawer.line = p; |
253 | } |
256 | |
|
|
257 | return s == q ? 0 : s; |
|
|
258 | } |
254 | } |
259 | |
255 | |
260 | void |
256 | void |
261 | object_thawer::parse_warn (const char *msg) |
257 | object_thawer::parse_warn (const char *msg) const |
262 | { |
258 | { |
263 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
259 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
264 | this->name, linenum, |
260 | this->name, linenum, |
265 | kw_str ? kw_str : "<null>", |
261 | kw_str ? kw_str : "<null>", |
266 | value ? value : "<null>", |
262 | value ? value : "<null>", |
267 | msg); |
263 | msg); |
268 | } |
264 | } |
269 | |
265 | |
270 | bool |
266 | bool |
271 | 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 |
272 | { |
268 | { |
273 | if (!type) type = "file section"; |
269 | if (!type) type = "file section"; |
274 | if (!name) name = "generic"; |
270 | if (!name) name = "generic"; |
275 | |
271 | |
276 | switch (kw) |
272 | switch (kw) |
277 | { |
273 | { |
278 | case KW_EOF: |
274 | case KW_EOF: |
279 | 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", |
280 | this->name, linenum, type, name); |
276 | this->name, linenum, type, name); |
|
|
277 | error_out (); |
281 | return false; |
278 | return false; |
282 | |
279 | |
283 | case KW_ERROR: |
280 | case KW_ERROR: |
284 | 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", |
285 | this->name, linenum, |
282 | this->name, linenum, |
286 | type, name, |
283 | type, name, |
287 | kw_str ? kw_str : "<file load>"); |
284 | kw_str ? kw_str : "<file load>"); |
|
|
285 | error_out (); |
288 | return false; |
286 | return false; |
289 | |
287 | |
290 | default: |
288 | default: |
291 | 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", |
292 | this->name, linenum, |
290 | this->name, linenum, |
293 | kw_str ? kw_str : "<null>", |
291 | kw_str ? kw_str : "<null>", |
294 | value ? value : "<null>", |
292 | value ? value : "<null>", |
295 | type, name, |
293 | type, name, |
296 | skip ? "skipping line" : "aborting load"); |
294 | skip ? "skipping line" : "aborting load"); |
|
|
295 | error_out (); |
297 | return skip; |
296 | return skip; |
298 | } |
297 | } |
299 | } |
298 | } |
300 | |
299 | |
301 | void |
300 | void |
… | |
… | |
309 | |
308 | |
310 | for (;;) |
309 | for (;;) |
311 | { |
310 | { |
312 | char *p = line; |
311 | char *p = line; |
313 | |
312 | |
314 | if (*p <= ' ') |
313 | if (expect_false (*p <= ' ')) |
315 | { |
314 | { |
316 | // skip whitespace (only some files need this) |
315 | // skip whitespace (only some files need this) |
317 | while (*p == ' ' || *p == '\t') |
316 | while (*p == ' ' || *p == '\t') |
318 | p++; |
317 | p++; |
319 | |
318 | |
… | |
… | |
367 | { |
366 | { |
368 | kw = KW_ERROR; |
367 | kw = KW_ERROR; |
369 | break; |
368 | break; |
370 | } |
369 | } |
371 | } |
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; |
372 | } |
424 | } |
373 | |
425 | |
374 | void |
426 | void |
375 | object_thawer::skip () |
427 | object_thawer::skip () |
376 | { |
428 | { |