1 |
root |
1.1 |
/* |
2 |
root |
1.21 |
* This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 |
|
|
* |
4 |
|
|
* Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 |
|
|
* |
6 |
|
|
* Crossfire TRT is free software; you can redistribute it and/or modify it |
7 |
|
|
* under the terms of the GNU General Public License as published by the Free |
8 |
|
|
* Software Foundation; either version 2 of the License, or (at your option) |
9 |
|
|
* any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, but |
12 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 |
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
|
|
* for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License along |
17 |
|
|
* with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
18 |
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 |
|
|
* |
20 |
|
|
* The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 |
root |
1.1 |
*/ |
22 |
|
|
|
23 |
root |
1.4 |
#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
24 |
root |
1.1 |
#include "logger.h" |
25 |
|
|
#include "cfperl.h" |
26 |
|
|
#include "kw_hash.h" |
27 |
|
|
|
28 |
|
|
object_freezer::object_freezer () |
29 |
root |
1.17 |
: dynbuf_text (128 * 1024, 64 * 1024) |
30 |
root |
1.1 |
{ |
31 |
|
|
av = newAV (); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
object_freezer::~object_freezer () |
35 |
|
|
{ |
36 |
|
|
SvREFCNT_dec (av); |
37 |
|
|
} |
38 |
|
|
|
39 |
root |
1.4 |
void |
40 |
|
|
object_freezer::put (attachable *ext) |
41 |
root |
1.1 |
{ |
42 |
|
|
ext->optimise (); |
43 |
|
|
|
44 |
|
|
if (ext->self) |
45 |
|
|
{ |
46 |
|
|
int idx = AvFILLp ((AV *)av) + 1; |
47 |
|
|
av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 |
|
|
|
49 |
|
|
add ((void *)"oid ", 4); |
50 |
|
|
add ((sint32)idx); |
51 |
|
|
add ('\n'); |
52 |
|
|
} |
53 |
|
|
} |
54 |
|
|
|
55 |
root |
1.4 |
bool |
56 |
|
|
object_freezer::save (const char *path) |
57 |
root |
1.1 |
{ |
58 |
|
|
CALL_BEGIN (3); |
59 |
root |
1.4 |
CALL_ARG_SV (newSVpv (path, 0)); |
60 |
root |
1.1 |
CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
61 |
|
|
CALL_ARG_SV (newRV_inc ((SV *)av)); |
62 |
|
|
CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
63 |
|
|
CALL_END; |
64 |
root |
1.13 |
|
65 |
|
|
return 1; |
66 |
root |
1.1 |
} |
67 |
|
|
|
68 |
root |
1.4 |
char * |
69 |
|
|
object_freezer::as_string () |
70 |
root |
1.1 |
{ |
71 |
|
|
CALL_BEGIN (2); |
72 |
|
|
CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
73 |
|
|
CALL_ARG_SV (newRV_inc ((SV *)av)); |
74 |
|
|
CALL_CALL ("cf::object_freezer_as_string", G_SCALAR); |
75 |
|
|
|
76 |
|
|
char *res = count > 0 |
77 |
root |
1.3 |
? strdup (SvPVX (POPs)) |
78 |
root |
1.1 |
: strdup ("[fatal error]"); |
79 |
|
|
|
80 |
|
|
CALL_END; |
81 |
|
|
|
82 |
|
|
return res; |
83 |
|
|
} |
84 |
|
|
|
85 |
root |
1.20 |
#if 0 |
86 |
root |
1.13 |
void |
87 |
root |
1.4 |
fprintf (object_freezer &freezer, const char *format, ...) |
88 |
root |
1.1 |
{ |
89 |
|
|
va_list ap; |
90 |
|
|
|
91 |
|
|
va_start (ap, format); |
92 |
|
|
|
93 |
|
|
int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap); |
94 |
|
|
|
95 |
|
|
if (len >= 0) |
96 |
|
|
freezer.alloc (len); |
97 |
|
|
|
98 |
|
|
va_end (ap); |
99 |
|
|
} |
100 |
|
|
|
101 |
pippijn |
1.12 |
// XXX: function not returning an int |
102 |
root |
1.13 |
void |
103 |
root |
1.4 |
fputs (const char *s, object_freezer &freezer) |
104 |
root |
1.1 |
{ |
105 |
|
|
freezer.add (s); |
106 |
|
|
} |
107 |
root |
1.20 |
#endif |
108 |
root |
1.1 |
|
109 |
|
|
static const char thawer_eof[] = "\n\n\n\0\0\0"; |
110 |
|
|
|
111 |
root |
1.4 |
object_thawer::object_thawer (const char *path) |
112 |
|
|
: name (strdup (path)) |
113 |
root |
1.1 |
{ |
114 |
|
|
static const char eof[] = "\n\n\n\0\0\0"; |
115 |
|
|
|
116 |
root |
1.10 |
av = 0; |
117 |
|
|
text = 0; |
118 |
|
|
line = 0; |
119 |
root |
1.8 |
linenum = 0; |
120 |
root |
1.1 |
|
121 |
root |
1.10 |
kw = KW_ERROR; |
122 |
|
|
kw_str = 0; |
123 |
|
|
value = 0; |
124 |
|
|
|
125 |
root |
1.4 |
if (path) |
126 |
root |
1.1 |
{ |
127 |
|
|
CALL_BEGIN (1); |
128 |
root |
1.4 |
CALL_ARG_SV (newSVpv (path, 0)); |
129 |
root |
1.1 |
CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
130 |
|
|
|
131 |
|
|
if (count == 2) |
132 |
|
|
{ |
133 |
|
|
// second value - perl objects |
134 |
|
|
{ |
135 |
|
|
SV *sv = POPs; |
136 |
|
|
if (SvROK (sv)) |
137 |
|
|
av = (AV *)SvREFCNT_inc (SvRV (sv)); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
// first value - text part, pad with 3 zeroes |
141 |
|
|
{ |
142 |
|
|
SV *sv = POPs; |
143 |
|
|
STRLEN len; |
144 |
|
|
char *sv_ = SvPVbyte (sv, len); |
145 |
|
|
text = newSV (len + sizeof (eof)); |
146 |
|
|
SvCUR_set (text, len); |
147 |
|
|
memcpy (SvPVX (text), sv_, len); |
148 |
|
|
memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
149 |
|
|
|
150 |
|
|
line = SvPVX (text); |
151 |
|
|
} |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
CALL_END; |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
object_thawer::object_thawer (const char *data, AV *perlav) |
159 |
root |
1.4 |
: name (strdup ("(memory stream")) |
160 |
root |
1.1 |
{ |
161 |
|
|
av = perlav; |
162 |
|
|
text = newSVpv (data, 0); |
163 |
|
|
sv_catpv (text, thawer_eof); |
164 |
|
|
line = SvPVbyte_nolen (text); |
165 |
|
|
} |
166 |
|
|
|
167 |
root |
1.4 |
void |
168 |
|
|
object_thawer::get (attachable *obj, int oid) |
169 |
root |
1.1 |
{ |
170 |
|
|
if (!av || oid < 0) // this is actually an error of sorts |
171 |
|
|
return; |
172 |
|
|
|
173 |
|
|
SV **svp = av_fetch ((AV *)av, oid, 0); |
174 |
|
|
|
175 |
|
|
if (!svp || !SvROK (*svp)) |
176 |
|
|
{ |
177 |
root |
1.15 |
LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
178 |
root |
1.1 |
return; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
if (!SvROK (*svp)) |
182 |
|
|
{ |
183 |
root |
1.15 |
LOG (llevError, "deserialised perl object is not an RV\n"); |
184 |
root |
1.1 |
return; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
HV *hv = (HV *)SvRV (*svp); |
188 |
|
|
|
189 |
|
|
if (SvTYPE (hv) != SVt_PVHV) |
190 |
|
|
{ |
191 |
root |
1.15 |
LOG (llevError, "deserialised perl object is not a PVHV\n"); |
192 |
root |
1.1 |
return; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
if (obj->self) |
196 |
|
|
{ |
197 |
|
|
// the hard way(?) |
198 |
|
|
|
199 |
root |
1.6 |
CALL_BEGIN (2); |
200 |
|
|
CALL_ARG_SV (newRV_inc ((SV *)obj->self)); |
201 |
|
|
CALL_ARG_SV (newRV_inc ((SV *)hv)); |
202 |
|
|
PUTBACK; |
203 |
|
|
call_method ("thawer_merge", G_DISCARD | G_EVAL); |
204 |
|
|
SPAGAIN; |
205 |
|
|
CALL_END; |
206 |
root |
1.1 |
} |
207 |
|
|
else |
208 |
|
|
{ |
209 |
|
|
// the easy way(?) |
210 |
|
|
|
211 |
|
|
obj->self = hv; |
212 |
|
|
SvRV_set (*svp, &PL_sv_undef); |
213 |
|
|
|
214 |
|
|
sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
obj->reattach (); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
object_thawer::~object_thawer () |
221 |
|
|
{ |
222 |
|
|
if (text) SvREFCNT_dec (text); |
223 |
|
|
if (av) SvREFCNT_dec (av); |
224 |
root |
1.4 |
|
225 |
|
|
free ((void *)name); |
226 |
root |
1.1 |
} |
227 |
|
|
|
228 |
root |
1.8 |
//TODO: remove |
229 |
root |
1.4 |
char * |
230 |
|
|
fgets (char *s, int n, object_thawer &thawer) |
231 |
root |
1.1 |
{ |
232 |
|
|
char *p = thawer.line; |
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 |
root |
1.8 |
{ |
247 |
|
|
++thawer.linenum; |
248 |
|
|
break; |
249 |
|
|
} |
250 |
root |
1.1 |
} |
251 |
|
|
|
252 |
|
|
*q = 0; |
253 |
|
|
thawer.line = p; |
254 |
|
|
|
255 |
|
|
return s == q ? 0 : s; |
256 |
|
|
} |
257 |
|
|
|
258 |
root |
1.8 |
bool |
259 |
root |
1.10 |
object_thawer::parse_error (const char *type, const char *name, bool skip) |
260 |
root |
1.8 |
{ |
261 |
|
|
if (!type) type = "file section"; |
262 |
|
|
if (!name) name = "generic"; |
263 |
|
|
|
264 |
|
|
switch (kw) |
265 |
|
|
{ |
266 |
|
|
case KW_EOF: |
267 |
|
|
LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
268 |
|
|
this->name, linenum, type, name); |
269 |
|
|
return false; |
270 |
|
|
|
271 |
|
|
case KW_ERROR: |
272 |
root |
1.10 |
LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
273 |
root |
1.8 |
this->name, linenum, |
274 |
root |
1.10 |
type, name, |
275 |
|
|
kw_str ? kw_str : "<file load>"); |
276 |
root |
1.8 |
return false; |
277 |
|
|
|
278 |
|
|
default: |
279 |
root |
1.9 |
LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
280 |
root |
1.8 |
this->name, linenum, |
281 |
root |
1.10 |
kw_str ? kw_str : "<null>", |
282 |
|
|
value ? value : "<null>", |
283 |
root |
1.8 |
type, name, |
284 |
|
|
skip ? "skipping line" : "aborting load"); |
285 |
|
|
return skip; |
286 |
|
|
} |
287 |
|
|
} |
288 |
|
|
|
289 |
root |
1.10 |
void |
290 |
root |
1.11 |
object_thawer::next () |
291 |
root |
1.1 |
{ |
292 |
|
|
if (!line) |
293 |
root |
1.10 |
{ |
294 |
|
|
kw = KW_ERROR; |
295 |
|
|
return; |
296 |
|
|
} |
297 |
root |
1.1 |
|
298 |
|
|
for (;;) |
299 |
|
|
{ |
300 |
|
|
char *p = line; |
301 |
|
|
|
302 |
root |
1.16 |
if (*p <= ' ') |
303 |
|
|
{ |
304 |
|
|
// skip whitespace (only some files need this) |
305 |
|
|
while (*p == ' ' || *p == '\t') |
306 |
|
|
p++; |
307 |
|
|
|
308 |
|
|
line = p; |
309 |
|
|
} |
310 |
|
|
|
311 |
root |
1.1 |
if (!*p) |
312 |
root |
1.10 |
{ |
313 |
|
|
kw = KW_EOF; |
314 |
|
|
break; |
315 |
|
|
} |
316 |
root |
1.1 |
|
317 |
|
|
// parse keyword |
318 |
|
|
while (*p > ' ') |
319 |
|
|
p++; |
320 |
|
|
|
321 |
|
|
int klen = p - line; |
322 |
|
|
|
323 |
root |
1.19 |
value_nn = ""; |
324 |
root |
1.16 |
value = 0; |
325 |
|
|
|
326 |
root |
1.1 |
if (*p++ != '\n') |
327 |
|
|
{ |
328 |
|
|
// parse value |
329 |
root |
1.16 |
while (*(unsigned char *)p <= ' ' && *p != '\n') |
330 |
root |
1.1 |
++p; |
331 |
|
|
|
332 |
root |
1.19 |
value_nn = value = p; |
333 |
root |
1.1 |
|
334 |
|
|
while (*p != '\n') |
335 |
|
|
p++; |
336 |
|
|
|
337 |
|
|
*p++ = 0; |
338 |
|
|
} |
339 |
|
|
|
340 |
root |
1.8 |
++linenum; |
341 |
root |
1.1 |
line [klen] = 0; |
342 |
root |
1.10 |
keyword_idx *kw_idx = kw_lex::match (line, klen); |
343 |
root |
1.1 |
|
344 |
root |
1.10 |
kw_str = line; |
345 |
root |
1.1 |
line = p; |
346 |
|
|
|
347 |
root |
1.10 |
if (kw_idx) |
348 |
|
|
{ |
349 |
|
|
kw = kw_idx->index; |
350 |
|
|
break; |
351 |
|
|
} |
352 |
|
|
else if (!*kw_str || *kw_str == '#') |
353 |
root |
1.1 |
; // empty/comment line |
354 |
|
|
else |
355 |
root |
1.10 |
{ |
356 |
|
|
kw = KW_ERROR; |
357 |
|
|
break; |
358 |
|
|
} |
359 |
root |
1.1 |
} |
360 |
|
|
} |
361 |
|
|
|
362 |
root |
1.4 |
void |
363 |
root |
1.11 |
object_thawer::skip () |
364 |
root |
1.1 |
{ |
365 |
|
|
shstr ml; |
366 |
|
|
|
367 |
|
|
switch (kw) |
368 |
|
|
{ |
369 |
|
|
case KW_msg: get_ml (KW_endmsg , ml); break; |
370 |
|
|
case KW_lore: get_ml (KW_endlore , ml); break; |
371 |
|
|
case KW_maplore: get_ml (KW_endmaplore, ml); break; |
372 |
pippijn |
1.14 |
default: break; |
373 |
root |
1.1 |
} |
374 |
root |
1.10 |
|
375 |
root |
1.11 |
next (); |
376 |
root |
1.1 |
} |
377 |
|
|
|
378 |
root |
1.4 |
void |
379 |
|
|
object_thawer::get_ml (keyword kend, shstr &sh) |
380 |
root |
1.1 |
{ |
381 |
|
|
char kw[128]; |
382 |
|
|
|
383 |
|
|
int klen = keyword_len [kend]; |
384 |
|
|
|
385 |
|
|
kw [0] = '\n'; |
386 |
|
|
memcpy (kw + 1, keyword_str [kend], klen); |
387 |
|
|
kw [klen + 1] = '\n'; |
388 |
|
|
kw [klen + 2] = 0; |
389 |
|
|
|
390 |
root |
1.8 |
++linenum; |
391 |
|
|
|
392 |
root |
1.1 |
// first test for completely empty msg... "endXXX\n" |
393 |
|
|
if (!strncmp (line, kw + 1, klen + 1)) |
394 |
|
|
{ |
395 |
|
|
sh = 0; |
396 |
|
|
|
397 |
|
|
line += klen + 1; |
398 |
|
|
|
399 |
|
|
return; |
400 |
|
|
} |
401 |
|
|
else |
402 |
|
|
{ |
403 |
|
|
// multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL) |
404 |
|
|
|
405 |
|
|
char *end = strstr (line, kw); |
406 |
|
|
|
407 |
|
|
if (!end) |
408 |
|
|
{ |
409 |
|
|
sh = 0; |
410 |
|
|
return; |
411 |
|
|
} |
412 |
|
|
|
413 |
|
|
*end = 0; |
414 |
|
|
sh = line; |
415 |
|
|
|
416 |
root |
1.8 |
// count line numbers |
417 |
|
|
while (line < end) |
418 |
|
|
linenum += *line++ == '\n'; |
419 |
|
|
|
420 |
|
|
line += keyword_len [kend]; |
421 |
root |
1.1 |
|
422 |
|
|
while (*line++ != '\n') |
423 |
|
|
; |
424 |
root |
1.8 |
|
425 |
|
|
++linenum; |
426 |
root |
1.1 |
} |
427 |
|
|
} |
428 |
|
|
|
429 |
root |
1.4 |
sint32 |
430 |
|
|
object_thawer::get_sint32 () const |
431 |
root |
1.1 |
{ |
432 |
root |
1.19 |
char *p = value_nn; |
433 |
root |
1.1 |
|
434 |
|
|
sint32 val = 0; |
435 |
|
|
bool negate; |
436 |
|
|
|
437 |
|
|
if (*p == '-') |
438 |
|
|
{ |
439 |
|
|
negate = true; |
440 |
|
|
++p; |
441 |
|
|
} |
442 |
|
|
else |
443 |
|
|
negate = false; |
444 |
|
|
|
445 |
|
|
do |
446 |
|
|
{ |
447 |
|
|
val *= 10; |
448 |
|
|
val += *p++ - '0'; |
449 |
|
|
} |
450 |
|
|
while (*p); |
451 |
|
|
|
452 |
|
|
return negate ? -val : val; |
453 |
|
|
} |
454 |
|
|
|