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,2008,2009,2010 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" |
… | |
… | |
104 | } |
104 | } |
105 | #endif |
105 | #endif |
106 | |
106 | |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | |
108 | |
|
|
109 | bool object_thawer::errors_are_fatal = true; |
|
|
110 | |
109 | 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 | { |
110 | : name (strdup (path)) |
136 | name = strdup (path); |
111 | { |
|
|
112 | av = 0; |
137 | av = 0; |
113 | text = 0; |
138 | text = 0; |
114 | line = 0; |
139 | line = 0; |
115 | linenum = 0; |
140 | linenum = 0; |
116 | |
141 | |
117 | kw = KW_ERROR; |
142 | kw = KW_ERROR; |
118 | kw_str = 0; |
143 | kw_str = 0; |
119 | value = 0; |
144 | value = 0; |
|
|
145 | } |
120 | |
146 | |
|
|
147 | void |
|
|
148 | object_thawer::init_from_file (const_utf8_string path) |
|
|
149 | { |
121 | if (path) |
150 | init (path); |
122 | { |
151 | |
123 | CALL_BEGIN (1); |
152 | CALL_BEGIN (1); |
124 | CALL_ARG_SV (newSVpv (path, 0)); |
153 | CALL_ARG_SV (newSVpv (path, 0)); |
125 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
154 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
126 | |
155 | |
127 | if (count == 2) |
156 | if (count == 2) |
128 | { |
157 | { |
129 | // second value - perl objects |
158 | // second value - perl objects |
130 | { |
159 | { |
131 | SV *sv = POPs; |
160 | SV *sv = POPs; |
132 | if (SvROK (sv)) |
161 | if (SvROK (sv)) |
133 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
162 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
134 | } |
163 | } |
135 | |
164 | |
136 | // first value - text part, pad with 3 zeroes |
165 | // first value - text part, pad with 3 zeroes |
137 | { |
166 | { |
138 | SV *sv = POPs; |
167 | SV *sv = POPs; |
139 | STRLEN len; |
168 | STRLEN len; |
140 | char *sv_ = SvPVbyte (sv, len); |
169 | char *sv_ = SvPVbyte (sv, len); |
141 | text = newSV (len + sizeof (thawer_eof)); |
170 | text = newSV (len + sizeof (thawer_eof)); |
142 | SvCUR_set (text, len + sizeof (thawer_eof)); |
171 | SvCUR_set (text, len + sizeof (thawer_eof)); |
143 | memcpy (SvPVX (text), sv_, len); |
172 | memcpy (SvPVX (text), sv_, len); |
144 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
173 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
145 | |
174 | |
146 | line = SvPVX (text); |
175 | line = SvPVX (text); |
147 | next (); |
176 | next (); |
148 | } |
|
|
149 | } |
177 | } |
|
|
178 | } |
150 | |
179 | |
151 | CALL_END; |
180 | CALL_END; |
152 | } |
|
|
153 | } |
|
|
154 | |
|
|
155 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
156 | : name (strdup ("(memory stream)")) |
|
|
157 | { |
|
|
158 | av = perlav; |
|
|
159 | text = newSVpv (data, 0); |
|
|
160 | sv_catpv (text, thawer_eof); |
|
|
161 | line = SvPVbyte_nolen (text); |
|
|
162 | next (); |
|
|
163 | } |
181 | } |
164 | |
182 | |
165 | void |
183 | void |
166 | object_thawer::get (attachable *obj, int oid) |
184 | object_thawer::get (attachable *obj, int oid) |
167 | { |
185 | { |
… | |
… | |
223 | resolve_delayed_derefs (false); |
241 | resolve_delayed_derefs (false); |
224 | |
242 | |
225 | free ((void *)name); |
243 | free ((void *)name); |
226 | } |
244 | } |
227 | |
245 | |
228 | //TODO: remove |
246 | static void |
229 | char * |
247 | error_out () |
230 | fgets (char *s, int n, object_thawer &thawer) |
|
|
231 | { |
248 | { |
232 | char *p = thawer.line; |
249 | 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 | } |
250 | { |
251 | |
251 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
252 | *q = 0; |
252 | exit (1); |
253 | thawer.line = p; |
253 | } |
254 | |
|
|
255 | return s == q ? 0 : s; |
|
|
256 | } |
254 | } |
257 | |
255 | |
258 | void |
256 | void |
259 | object_thawer::parse_warn (const char *msg) |
257 | object_thawer::parse_warn (const char *msg) const |
260 | { |
258 | { |
261 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
259 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
262 | this->name, linenum, |
260 | this->name, linenum, |
263 | kw_str ? kw_str : "<null>", |
261 | kw_str ? kw_str : "<null>", |
264 | value ? value : "<null>", |
262 | value ? value : "<null>", |
265 | msg); |
263 | msg); |
266 | } |
264 | } |
267 | |
265 | |
268 | bool |
266 | bool |
269 | 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 |
270 | { |
268 | { |
271 | if (!type) type = "file section"; |
269 | if (!type) type = "file section"; |
272 | if (!name) name = "generic"; |
270 | if (!name) name = "generic"; |
273 | |
271 | |
274 | switch (kw) |
272 | switch (kw) |
275 | { |
273 | { |
276 | case KW_EOF: |
274 | case KW_EOF: |
277 | 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", |
278 | this->name, linenum, type, name); |
276 | this->name, linenum, type, name); |
|
|
277 | error_out (); |
279 | return false; |
278 | return false; |
280 | |
279 | |
281 | case KW_ERROR: |
280 | case KW_ERROR: |
282 | 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", |
283 | this->name, linenum, |
282 | this->name, linenum, |
284 | type, name, |
283 | type, name, |
285 | kw_str ? kw_str : "<file load>"); |
284 | kw_str ? kw_str : "<file load>"); |
|
|
285 | error_out (); |
286 | return false; |
286 | return false; |
287 | |
287 | |
288 | default: |
288 | default: |
289 | 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", |
290 | this->name, linenum, |
290 | this->name, linenum, |
291 | kw_str ? kw_str : "<null>", |
291 | kw_str ? kw_str : "<null>", |
292 | value ? value : "<null>", |
292 | value ? value : "<null>", |
293 | type, name, |
293 | type, name, |
294 | skip ? "skipping line" : "aborting load"); |
294 | skip ? "skipping line" : "aborting load"); |
|
|
295 | error_out (); |
295 | return skip; |
296 | return skip; |
296 | } |
297 | } |
297 | } |
298 | } |
298 | |
299 | |
299 | void |
300 | void |
… | |
… | |
307 | |
308 | |
308 | for (;;) |
309 | for (;;) |
309 | { |
310 | { |
310 | char *p = line; |
311 | char *p = line; |
311 | |
312 | |
312 | if (*p <= ' ') |
313 | if (expect_false (*p <= ' ')) |
313 | { |
314 | { |
314 | // skip whitespace (only some files need this) |
315 | // skip whitespace (only some files need this) |
315 | while (*p == ' ' || *p == '\t') |
316 | while (*p == ' ' || *p == '\t') |
316 | p++; |
317 | p++; |
317 | |
318 | |
… | |
… | |
365 | { |
366 | { |
366 | kw = KW_ERROR; |
367 | kw = KW_ERROR; |
367 | break; |
368 | break; |
368 | } |
369 | } |
369 | } |
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; |
370 | } |
424 | } |
371 | |
425 | |
372 | void |
426 | void |
373 | object_thawer::skip () |
427 | object_thawer::skip () |
374 | { |
428 | { |