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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
|
|
5 | * Copyright (©) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * 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 |
8 | * 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 |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * 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 |
18 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
24 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
24 | #include "logger.h" |
25 | #include "logger.h" |
… | |
… | |
35 | { |
36 | { |
36 | SvREFCNT_dec (av); |
37 | SvREFCNT_dec (av); |
37 | } |
38 | } |
38 | |
39 | |
39 | void |
40 | void |
40 | object_freezer::put (attachable *ext) |
41 | object_freezer::put_ (attachable *ext) |
41 | { |
42 | { |
42 | ext->optimise (); |
43 | ext->optimise (); |
43 | |
44 | |
44 | if (ext->self) |
45 | if (ext->self) |
45 | { |
46 | { |
46 | int idx = AvFILLp ((AV *)av) + 1; |
47 | int idx = AvFILLp ((AV *)av) + 1; |
47 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 | |
49 | |
49 | add ((void *)"oid ", 4); |
50 | put (KW(oid), sint32(idx)); |
50 | add ((sint32)idx); |
|
|
51 | add ('\n'); |
|
|
52 | } |
51 | } |
53 | } |
52 | } |
54 | |
53 | |
55 | bool |
54 | bool |
56 | object_freezer::save (const char *path) |
55 | object_freezer::save (const char *path) |
… | |
… | |
106 | } |
105 | } |
107 | #endif |
106 | #endif |
108 | |
107 | |
109 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
110 | |
109 | |
|
|
110 | bool object_thawer::errors_are_fatal = true; |
|
|
111 | |
111 | object_thawer::object_thawer (const char *path) |
112 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
113 | : name (strdup ("(memory stream)")) |
|
|
114 | { |
|
|
115 | init ("(memory stream)"); |
|
|
116 | |
|
|
117 | av = perlav; |
|
|
118 | text = newSVpv (data, 0); sv_catpv (text, thawer_eof); |
|
|
119 | line = SvPVbyte_nolen (text); |
|
|
120 | next (); |
|
|
121 | } |
|
|
122 | |
|
|
123 | object_thawer::object_thawer (const_utf8_string path) |
|
|
124 | { |
|
|
125 | init_from_file (path); |
|
|
126 | } |
|
|
127 | |
|
|
128 | // convenience constructor |
|
|
129 | object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file) |
|
|
130 | { |
|
|
131 | init_from_file (format ("%s/%s", dir, file)); |
|
|
132 | } |
|
|
133 | |
|
|
134 | void |
|
|
135 | object_thawer::init (const_utf8_string path) |
|
|
136 | { |
112 | : name (strdup (path)) |
137 | name = strdup (path); |
113 | { |
|
|
114 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
115 | |
|
|
116 | av = 0; |
138 | av = 0; |
117 | text = 0; |
139 | text = 0; |
118 | line = 0; |
140 | line = 0; |
119 | linenum = 0; |
141 | linenum = 0; |
120 | |
142 | |
121 | kw = KW_ERROR; |
143 | kw = KW_ERROR; |
122 | kw_str = 0; |
144 | kw_str = 0; |
123 | value = 0; |
145 | value = 0; |
|
|
146 | } |
124 | |
147 | |
|
|
148 | void |
|
|
149 | object_thawer::init_from_file (const_utf8_string path) |
|
|
150 | { |
125 | if (path) |
151 | init (path); |
126 | { |
152 | |
127 | CALL_BEGIN (1); |
153 | CALL_BEGIN (1); |
128 | CALL_ARG_SV (newSVpv (path, 0)); |
154 | CALL_ARG_SV (newSVpv (path, 0)); |
129 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
155 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
130 | |
156 | |
131 | if (count == 2) |
157 | if (count == 2) |
132 | { |
158 | { |
133 | // second value - perl objects |
159 | // second value - perl objects |
134 | { |
160 | { |
135 | SV *sv = POPs; |
161 | SV *sv = POPs; |
136 | if (SvROK (sv)) |
162 | if (SvROK (sv)) |
137 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
163 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
138 | } |
164 | } |
139 | |
165 | |
140 | // first value - text part, pad with 3 zeroes |
166 | // first value - text part, pad with 3 zeroes |
141 | { |
167 | { |
142 | SV *sv = POPs; |
168 | SV *sv = POPs; |
143 | STRLEN len; |
169 | STRLEN len; |
144 | char *sv_ = SvPVbyte (sv, len); |
170 | char *sv_ = SvPVbyte (sv, len); |
145 | text = newSV (len + sizeof (eof)); |
171 | text = newSV (len + sizeof (thawer_eof)); |
146 | SvCUR_set (text, len + sizeof (eof)); |
172 | SvCUR_set (text, len + sizeof (thawer_eof)); |
147 | memcpy (SvPVX (text), sv_, len); |
173 | memcpy (SvPVX (text), sv_, len); |
148 | memcpy (SvPVX (text) + len, eof, sizeof (eof)); |
174 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
149 | |
175 | |
150 | line = SvPVX (text); |
176 | line = SvPVX (text); |
151 | next (); |
177 | next (); |
152 | } |
|
|
153 | } |
178 | } |
|
|
179 | } |
154 | |
180 | |
155 | CALL_END; |
181 | CALL_END; |
156 | } |
|
|
157 | } |
|
|
158 | |
|
|
159 | object_thawer::object_thawer (const char *data, AV *perlav) |
|
|
160 | : name (strdup ("(memory stream)")) |
|
|
161 | { |
|
|
162 | av = perlav; |
|
|
163 | text = newSVpv (data, 0); |
|
|
164 | sv_catpv (text, thawer_eof); |
|
|
165 | line = SvPVbyte_nolen (text); |
|
|
166 | next (); |
|
|
167 | } |
182 | } |
168 | |
183 | |
169 | void |
184 | void |
170 | object_thawer::get (attachable *obj, int oid) |
185 | object_thawer::get (attachable *obj, int oid) |
171 | { |
186 | { |
… | |
… | |
227 | resolve_delayed_derefs (false); |
242 | resolve_delayed_derefs (false); |
228 | |
243 | |
229 | free ((void *)name); |
244 | free ((void *)name); |
230 | } |
245 | } |
231 | |
246 | |
232 | //TODO: remove |
247 | static void |
233 | char * |
248 | error_out () |
234 | fgets (char *s, int n, object_thawer &thawer) |
|
|
235 | { |
249 | { |
236 | char *p = thawer.line; |
250 | if (object_thawer::errors_are_fatal) |
237 | char *q = s; |
|
|
238 | |
|
|
239 | if (!p) |
|
|
240 | return 0; |
|
|
241 | |
|
|
242 | while (--n) |
|
|
243 | { |
|
|
244 | if (!*p) |
|
|
245 | break; |
|
|
246 | |
|
|
247 | *q++ = *p; |
|
|
248 | |
|
|
249 | if (*p++ == '\n') |
|
|
250 | { |
|
|
251 | ++thawer.linenum; |
|
|
252 | break; |
|
|
253 | } |
|
|
254 | } |
251 | { |
255 | |
252 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
256 | *q = 0; |
253 | exit (1); |
257 | thawer.line = p; |
254 | } |
258 | |
|
|
259 | return s == q ? 0 : s; |
|
|
260 | } |
255 | } |
261 | |
256 | |
262 | void |
257 | void |
263 | object_thawer::parse_warn (const char *msg) |
258 | object_thawer::parse_warn (const char *msg) const |
264 | { |
259 | { |
265 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
266 | this->name, linenum, |
261 | this->name, linenum, |
267 | kw_str ? kw_str : "<null>", |
262 | kw_str ? kw_str : "<null>", |
268 | value ? value : "<null>", |
263 | value ? value : "<null>", |
269 | msg); |
264 | msg); |
270 | } |
265 | } |
271 | |
266 | |
272 | bool |
267 | bool |
273 | object_thawer::parse_error (const char *type, const char *name, bool skip) |
268 | object_thawer::parse_error (const char *type, const char *name, bool skip) const |
274 | { |
269 | { |
275 | if (!type) type = "file section"; |
270 | if (!type) type = "file section"; |
276 | if (!name) name = "generic"; |
271 | if (!name) name = "generic"; |
277 | |
272 | |
278 | switch (kw) |
273 | switch (kw) |
279 | { |
274 | { |
280 | case KW_EOF: |
275 | case KW_EOF: |
281 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
276 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
282 | this->name, linenum, type, name); |
277 | this->name, linenum, type, name); |
|
|
278 | error_out (); |
283 | return false; |
279 | return false; |
284 | |
280 | |
285 | case KW_ERROR: |
281 | case KW_ERROR: |
286 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
282 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
287 | this->name, linenum, |
283 | this->name, linenum, |
288 | type, name, |
284 | type, name, |
289 | kw_str ? kw_str : "<file load>"); |
285 | kw_str ? kw_str : "<file load>"); |
|
|
286 | error_out (); |
290 | return false; |
287 | return false; |
291 | |
288 | |
292 | default: |
289 | default: |
293 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
290 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
294 | this->name, linenum, |
291 | this->name, linenum, |
295 | kw_str ? kw_str : "<null>", |
292 | kw_str ? kw_str : "<null>", |
296 | value ? value : "<null>", |
293 | value ? value : "<null>", |
297 | type, name, |
294 | type, name, |
298 | skip ? "skipping line" : "aborting load"); |
295 | skip ? "skipping line" : "aborting load"); |
|
|
296 | error_out (); |
299 | return skip; |
297 | return skip; |
300 | } |
298 | } |
301 | } |
299 | } |
302 | |
300 | |
303 | void |
301 | void |
… | |
… | |
311 | |
309 | |
312 | for (;;) |
310 | for (;;) |
313 | { |
311 | { |
314 | char *p = line; |
312 | char *p = line; |
315 | |
313 | |
316 | if (*p <= ' ') |
314 | if (ecb_expect_false (*p <= ' ')) |
317 | { |
315 | { |
318 | // skip whitespace (only some files need this) |
316 | // skip whitespace (only some files need this) |
319 | while (*p == ' ' || *p == '\t') |
317 | while (*p == ' ' || *p == '\t') |
320 | p++; |
318 | p++; |
321 | |
319 | |
… | |
… | |
369 | { |
367 | { |
370 | kw = KW_ERROR; |
368 | kw = KW_ERROR; |
371 | break; |
369 | break; |
372 | } |
370 | } |
373 | } |
371 | } |
|
|
372 | } |
|
|
373 | |
|
|
374 | bool |
|
|
375 | object_thawer::next_line () |
|
|
376 | { |
|
|
377 | if (!line) |
|
|
378 | { |
|
|
379 | kw = KW_ERROR; |
|
|
380 | return 0; |
|
|
381 | } |
|
|
382 | |
|
|
383 | for (;;) |
|
|
384 | { |
|
|
385 | char *p = line; |
|
|
386 | |
|
|
387 | if (ecb_expect_false (*p <= ' ')) |
|
|
388 | { |
|
|
389 | // skip whitespace (only some files need this) |
|
|
390 | while (*p == ' ' || *p == '\t') |
|
|
391 | p++; |
|
|
392 | |
|
|
393 | line = p; |
|
|
394 | } |
|
|
395 | |
|
|
396 | if (!*p) |
|
|
397 | { |
|
|
398 | kw = KW_EOF; |
|
|
399 | return 0; |
|
|
400 | } |
|
|
401 | |
|
|
402 | kw = KW_value; |
|
|
403 | kw_str = p; |
|
|
404 | value_nn = p; |
|
|
405 | value = p; |
|
|
406 | |
|
|
407 | // parse till newline |
|
|
408 | while (*p > '\n') |
|
|
409 | p++; |
|
|
410 | |
|
|
411 | if (*p == '\n') |
|
|
412 | *p++ = 0; |
|
|
413 | |
|
|
414 | ++linenum; |
|
|
415 | |
|
|
416 | line = p; |
|
|
417 | |
|
|
418 | if (*kw_str && *kw_str != '#') |
|
|
419 | break; |
|
|
420 | |
|
|
421 | // empty/comment line -> skip it |
|
|
422 | } |
|
|
423 | |
|
|
424 | return 1; |
374 | } |
425 | } |
375 | |
426 | |
376 | void |
427 | void |
377 | object_thawer::skip () |
428 | object_thawer::skip () |
378 | { |
429 | { |