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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / 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" |
… | |
… | |
106 | |
107 | |
107 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | |
109 | |
109 | bool object_thawer::errors_are_fatal = true; |
110 | bool object_thawer::errors_are_fatal = true; |
110 | |
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 | av = 0; |
138 | av = 0; |
115 | text = 0; |
139 | text = 0; |
116 | line = 0; |
140 | line = 0; |
117 | linenum = 0; |
141 | linenum = 0; |
118 | |
142 | |
119 | kw = KW_ERROR; |
143 | kw = KW_ERROR; |
120 | kw_str = 0; |
144 | kw_str = 0; |
121 | value = 0; |
145 | value = 0; |
|
|
146 | } |
122 | |
147 | |
|
|
148 | void |
|
|
149 | object_thawer::init_from_file (const_utf8_string path) |
|
|
150 | { |
123 | if (path) |
151 | init (path); |
124 | { |
152 | |
125 | CALL_BEGIN (1); |
153 | CALL_BEGIN (1); |
126 | CALL_ARG_SV (newSVpv (path, 0)); |
154 | CALL_ARG_SV (newSVpv (path, 0)); |
127 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
155 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
128 | |
156 | |
129 | if (count == 2) |
157 | if (count == 2) |
130 | { |
158 | { |
131 | // second value - perl objects |
159 | // second value - perl objects |
132 | { |
160 | { |
133 | SV *sv = POPs; |
161 | SV *sv = POPs; |
134 | if (SvROK (sv)) |
162 | if (SvROK (sv)) |
135 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
163 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
136 | } |
164 | } |
137 | |
165 | |
138 | // first value - text part, pad with 3 zeroes |
166 | // first value - text part, pad with 3 zeroes |
139 | { |
167 | { |
140 | SV *sv = POPs; |
168 | SV *sv = POPs; |
141 | STRLEN len; |
169 | STRLEN len; |
142 | char *sv_ = SvPVbyte (sv, len); |
170 | char *sv_ = SvPVbyte (sv, len); |
143 | text = newSV (len + sizeof (thawer_eof)); |
171 | text = newSV (len + sizeof (thawer_eof)); |
144 | SvCUR_set (text, len + sizeof (thawer_eof)); |
172 | SvCUR_set (text, len + sizeof (thawer_eof)); |
145 | memcpy (SvPVX (text), sv_, len); |
173 | memcpy (SvPVX (text), sv_, len); |
146 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
174 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
147 | |
175 | |
148 | line = SvPVX (text); |
176 | line = SvPVX (text); |
149 | next (); |
177 | next (); |
150 | } |
|
|
151 | } |
178 | } |
|
|
179 | } |
152 | |
180 | |
153 | CALL_END; |
181 | 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 | } |
182 | } |
166 | |
183 | |
167 | void |
184 | void |
168 | object_thawer::get (attachable *obj, int oid) |
185 | object_thawer::get (attachable *obj, int oid) |
169 | { |
186 | { |
… | |
… | |
236 | exit (1); |
253 | exit (1); |
237 | } |
254 | } |
238 | } |
255 | } |
239 | |
256 | |
240 | void |
257 | void |
241 | object_thawer::parse_warn (const char *msg) |
258 | object_thawer::parse_warn (const char *msg) const |
242 | { |
259 | { |
243 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
244 | this->name, linenum, |
261 | this->name, linenum, |
245 | kw_str ? kw_str : "<null>", |
262 | kw_str ? kw_str : "<null>", |
246 | value ? value : "<null>", |
263 | value ? value : "<null>", |
247 | msg); |
264 | msg); |
248 | } |
265 | } |
249 | |
266 | |
250 | bool |
267 | bool |
251 | 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 |
252 | { |
269 | { |
253 | if (!type) type = "file section"; |
270 | if (!type) type = "file section"; |
254 | if (!name) name = "generic"; |
271 | if (!name) name = "generic"; |
255 | |
272 | |
256 | switch (kw) |
273 | switch (kw) |
… | |
… | |
292 | |
309 | |
293 | for (;;) |
310 | for (;;) |
294 | { |
311 | { |
295 | char *p = line; |
312 | char *p = line; |
296 | |
313 | |
297 | if (expect_false (*p <= ' ')) |
314 | if (ecb_expect_false (*p <= ' ')) |
298 | { |
315 | { |
299 | // skip whitespace (only some files need this) |
316 | // skip whitespace (only some files need this) |
300 | while (*p == ' ' || *p == '\t') |
317 | while (*p == ' ' || *p == '\t') |
301 | p++; |
318 | p++; |
302 | |
319 | |
… | |
… | |
365 | |
382 | |
366 | for (;;) |
383 | for (;;) |
367 | { |
384 | { |
368 | char *p = line; |
385 | char *p = line; |
369 | |
386 | |
370 | if (expect_false (*p <= ' ')) |
387 | if (ecb_expect_false (*p <= ' ')) |
371 | { |
388 | { |
372 | // skip whitespace (only some files need this) |
389 | // skip whitespace (only some files need this) |
373 | while (*p == ' ' || *p == '\t') |
390 | while (*p == ' ' || *p == '\t') |
374 | p++; |
391 | p++; |
375 | |
392 | |