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 (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 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. |
… | |
… | |
24 | // cfperl.h perl interface |
24 | // cfperl.h perl interface |
25 | // |
25 | // |
26 | #ifndef CFPERL_H__ |
26 | #ifndef CFPERL_H__ |
27 | #define CFPERL_H__ |
27 | #define CFPERL_H__ |
28 | |
28 | |
29 | #include <cstdarg> |
|
|
30 | #include <cstdio> |
|
|
31 | #include <bitset> |
|
|
32 | |
|
|
33 | using namespace std; |
29 | using namespace std; |
34 | |
30 | |
35 | #include <EXTERN.h> |
31 | #include <EXTERN.h> |
36 | #include <perl.h> |
32 | #include <perl.h> |
37 | #include <XSUB.h> |
33 | #include <XSUB.h> |
… | |
… | |
85 | LOG (llevError, "runtime error in %s: %s", __func__, SvPVutf8_nolen (ERRSV)); |
81 | LOG (llevError, "runtime error in %s: %s", __func__, SvPVutf8_nolen (ERRSV)); |
86 | |
82 | |
87 | inline int call_pvsv (const char *ob, I32 flags) { return call_pv (ob, flags); } |
83 | inline int call_pvsv (const char *ob, I32 flags) { return call_pv (ob, flags); } |
88 | inline int call_pvsv (SV *ob, I32 flags) { return call_sv (ob, flags); } |
84 | inline int call_pvsv (SV *ob, I32 flags) { return call_sv (ob, flags); } |
89 | |
85 | |
|
|
86 | // TODO: temporarily enabled pushstack/popstack for all calls from the core, to maybe fix object memleak? |
|
|
87 | #define CALL_PUSH PUSHSTACKi (PERLSI_UNKNOWN) |
|
|
88 | #define CALL_POP PUTBACK; POPSTACK; SPAGAIN |
90 | #define CALL_BEGIN(args) dSP; ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, args) |
89 | #define CALL_BEGIN(args) dSP; CALL_PUSH; ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, args) |
91 | #define CALL_ARG_SV(sv) PUSHs (sv_2mortal (sv)) // separate because no refcount inc |
90 | #define CALL_ARG_SV(sv) PUSHs (sv_2mortal (sv)) // separate because no refcount inc |
92 | #define CALL_ARG(expr) PUSHs (sv_2mortal (to_sv (expr))) |
91 | #define CALL_ARG(expr) PUSHs (sv_2mortal (to_sv (expr))) |
93 | #define CALL_CALL(name, flags) PUTBACK; int count = call_pvsv (name, (flags) | G_EVAL); SPAGAIN; |
92 | #define CALL_CALL(name, flags) PUTBACK; int count = call_pvsv (name, (flags) | G_EVAL); SPAGAIN; |
94 | #define CALL_END PUTBACK; CHECK_ERROR; FREETMPS; LEAVE |
93 | #define CALL_END PUTBACK; CHECK_ERROR; FREETMPS; LEAVE; CALL_POP |
95 | |
94 | |
96 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
95 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
97 | |
96 | |
98 | void cfperl_init (); |
97 | void cfperl_init (); |
99 | void cfperl_main (); |
98 | void cfperl_main (); |
… | |
… | |
214 | MTH void destroy (); |
213 | MTH void destroy (); |
215 | |
214 | |
216 | // return wether an object was destroyed already |
215 | // return wether an object was destroyed already |
217 | MTH bool destroyed () const { return attachable_flags & F_DESTROYED; } |
216 | MTH bool destroyed () const { return attachable_flags & F_DESTROYED; } |
218 | |
217 | |
|
|
218 | // destruct and free the memory for this object |
|
|
219 | virtual void do_delete (); |
|
|
220 | |
219 | virtual void gather_callbacks (AV *&callbacks, event_type event) const; |
221 | virtual void gather_callbacks (AV *&callbacks, event_type event) const; |
220 | |
222 | |
221 | #if 0 |
223 | #if 0 |
222 | private: |
224 | private: |
223 | static refcounted *rc_first; |
225 | static refcounted *rc_first; |
… | |
… | |
254 | set_key (key, value, 0); |
256 | set_key (key, value, 0); |
255 | } |
257 | } |
256 | |
258 | |
257 | attachable &operator =(const attachable &src); |
259 | attachable &operator =(const attachable &src); |
258 | |
260 | |
259 | // used to _quickly_ device wether to shortcut the evaluation |
261 | // used to _quickly_ decide wether to shortcut the evaluation |
260 | bool should_invoke (event_type event) |
262 | bool should_invoke (event_type event) |
261 | { |
263 | { |
262 | return ev_want_event [event] || cb; |
264 | return ev_want_event [event] || cb; |
263 | } |
265 | } |
264 | |
266 | |
… | |
… | |
286 | |
288 | |
287 | extern struct global gbl_ev; |
289 | extern struct global gbl_ev; |
288 | |
290 | |
289 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
291 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
290 | |
292 | |
291 | // a little dirty hack, maybe unify with something else at a later time |
293 | #include "freezethaw.h" |
292 | struct keyword_string |
|
|
293 | { |
|
|
294 | const char *s; |
|
|
295 | const int l; |
|
|
296 | |
|
|
297 | keyword_string (const char *s, int l) |
|
|
298 | : s(s), l(l) |
|
|
299 | { } |
|
|
300 | |
|
|
301 | keyword_string (keyword kw) |
|
|
302 | : s(keyword_str [kw]), l(keyword_len [kw]) |
|
|
303 | { |
|
|
304 | } |
|
|
305 | |
|
|
306 | keyword_string (shstr_tmp sh) |
|
|
307 | : s(&sh), l(sh.length ()) |
|
|
308 | { |
|
|
309 | } |
|
|
310 | }; |
|
|
311 | |
|
|
312 | #define CS(keyword) keyword_string (# keyword, sizeof (# keyword) - 1) |
|
|
313 | #define KW(keyword) CS(keyword) |
|
|
314 | |
|
|
315 | INTERFACE_CLASS(object_freezer) |
|
|
316 | struct object_freezer : dynbuf_text |
|
|
317 | { |
|
|
318 | AV *av; |
|
|
319 | |
|
|
320 | object_freezer (); |
|
|
321 | ~object_freezer (); |
|
|
322 | |
|
|
323 | // serialise perl part |
|
|
324 | void put_ (attachable *ext); |
|
|
325 | void put (attachable *ext) |
|
|
326 | { |
|
|
327 | if (expect_false (ext->self)) |
|
|
328 | put_ (ext); |
|
|
329 | } |
|
|
330 | |
|
|
331 | // null value (== no space after keyword) |
|
|
332 | void put (const keyword_string k) |
|
|
333 | { |
|
|
334 | char *p = force (k.l + 1); |
|
|
335 | memcpy (p, k.s, k.l); p += k.l; *p++ = '\n'; |
|
|
336 | alloc (p); |
|
|
337 | } |
|
|
338 | |
|
|
339 | void put (const keyword_string k, const keyword_string v) |
|
|
340 | { |
|
|
341 | char *p = force (k.l + 1 + v.l + 1); |
|
|
342 | memcpy (p, k.s, k.l); p += k.l; *p++ = ' '; |
|
|
343 | memcpy (p, v.s, v.l); p += v.l; *p++ = '\n'; |
|
|
344 | alloc (p); |
|
|
345 | } |
|
|
346 | |
|
|
347 | void put (const keyword_string k, const_utf8_string v) |
|
|
348 | { |
|
|
349 | if (expect_true (v)) |
|
|
350 | put (k, keyword_string (v, strlen (v))); |
|
|
351 | else |
|
|
352 | put (k); |
|
|
353 | } |
|
|
354 | |
|
|
355 | void put (const keyword_string k, shstr_tmp v) |
|
|
356 | { |
|
|
357 | put (k, keyword_string (v)); |
|
|
358 | } |
|
|
359 | |
|
|
360 | void put (const keyword_string k, double v) |
|
|
361 | { |
|
|
362 | char *p = force (MAX_KEYWORD_LEN + 2 + 32); |
|
|
363 | memcpy (p, k.s, k.l); p += k.l; *p++ = ' '; |
|
|
364 | p += sprintf (p, "%.7g", v); *p++ = '\n'; |
|
|
365 | alloc (p); |
|
|
366 | } |
|
|
367 | |
|
|
368 | void put_(const keyword_string k, sint64 v) |
|
|
369 | { |
|
|
370 | force (MAX_KEYWORD_LEN + 2 + sint64_digits); |
|
|
371 | fadd (k.s, k.l); |
|
|
372 | fadd (' '); |
|
|
373 | add (v); |
|
|
374 | fadd ('\n'); |
|
|
375 | } |
|
|
376 | |
|
|
377 | void put_(const keyword_string k, sint32 v) |
|
|
378 | { |
|
|
379 | force (MAX_KEYWORD_LEN + 2 + sint32_digits); |
|
|
380 | fadd (k.s, k.l); |
|
|
381 | fadd (' '); |
|
|
382 | add (v); |
|
|
383 | fadd ('\n'); |
|
|
384 | } |
|
|
385 | |
|
|
386 | void put (const keyword_string k, float v) { put (k, (double)v); } |
|
|
387 | void put (const keyword_string k, signed char v) { put_(k, (sint32)v); } |
|
|
388 | void put (const keyword_string k, unsigned char v) { put_(k, (sint32)v); } |
|
|
389 | void put (const keyword_string k, signed short v) { put_(k, (sint32)v); } |
|
|
390 | void put (const keyword_string k, unsigned short v) { put_(k, (sint32)v); } |
|
|
391 | void put (const keyword_string k, signed int v) { put_(k, (sint32)v); } |
|
|
392 | void put (const keyword_string k, unsigned int v) { put_(k, (sint64)v); } |
|
|
393 | void put (const keyword_string k, signed long v) { put_(k, (sint64)v); } |
|
|
394 | void put (const keyword_string k, unsigned long v) { put_(k, (sint64)v); } |
|
|
395 | void put (const keyword_string k, signed long long v) { put_(k, (sint64)v); } |
|
|
396 | void put (const keyword_string k, unsigned long long v) { put_(k, (sint64)v); } |
|
|
397 | |
|
|
398 | void put (const keyword_string kbeg, const keyword_string kend, shstr_tmp v) |
|
|
399 | { |
|
|
400 | force (MAX_KEYWORD_LEN + 1); |
|
|
401 | fadd (kbeg.s, kbeg.l); fadd ('\n'); |
|
|
402 | |
|
|
403 | if (expect_true (v)) |
|
|
404 | { |
|
|
405 | add (v); |
|
|
406 | add ('\n'); |
|
|
407 | } |
|
|
408 | |
|
|
409 | force (MAX_KEYWORD_LEN + 1); |
|
|
410 | fadd (kend.s, kend.l); fadd ('\n'); |
|
|
411 | } |
|
|
412 | |
|
|
413 | void put (const keyword_string k, archetype *v); |
|
|
414 | void put (const keyword_string k, treasurelist *v); |
|
|
415 | void put (const keyword_string k, faceinfo *v); |
|
|
416 | |
|
|
417 | template<typename T> |
|
|
418 | void put (const keyword_string k, const refptr<T> &v) |
|
|
419 | { |
|
|
420 | put (k, (T *)v); |
|
|
421 | } |
|
|
422 | |
|
|
423 | MTH bool save (const_octet_string path); |
|
|
424 | utf8_string as_string (); // like strdup |
|
|
425 | |
|
|
426 | operator bool () { return !!av; } |
|
|
427 | }; |
|
|
428 | |
|
|
429 | INTERFACE_CLASS(object_thawer) |
|
|
430 | struct object_thawer |
|
|
431 | { |
|
|
432 | char *line; // current beginning of line |
|
|
433 | SV *text; // text part |
|
|
434 | AV *av; // perl part |
|
|
435 | int linenum; |
|
|
436 | keyword kw; |
|
|
437 | char *kw_str; // the keyword parsed, as string |
|
|
438 | char *value; // the value, or 0 if no value |
|
|
439 | const char *value_nn; // the value, or the empty string if no value |
|
|
440 | const char *name; |
|
|
441 | |
|
|
442 | operator bool () const { return !!text; } |
|
|
443 | |
|
|
444 | object_thawer (const_utf8_string path = 0); |
|
|
445 | object_thawer (const_utf8_string data, AV *perlav); |
|
|
446 | ~object_thawer (); |
|
|
447 | |
|
|
448 | void get (attachable *obj, int oid); |
|
|
449 | |
|
|
450 | // parse next line |
|
|
451 | MTH void next (); |
|
|
452 | // skip the current key-value (usually fetch next line, for |
|
|
453 | // multiline-fields, skips till the corresponding end-kw |
|
|
454 | MTH void skip (); |
|
|
455 | MTH void skip_block (); // skips till and over KW_end |
|
|
456 | |
|
|
457 | bool has_value () { return value; } |
|
|
458 | const_utf8_string get_str () { return value_nn; } // empty string when missing |
|
|
459 | void get_ml (keyword kend, shstr &sh); |
|
|
460 | |
|
|
461 | void get_ornull (shstr &sh) const { sh = value; } |
|
|
462 | void get (shstr &sh) const { sh = value; } // might want to check for non-null here |
|
|
463 | |
|
|
464 | bool get_bool () const { return *value_nn == '1'; } |
|
|
465 | sint32 get_sint32 () const; |
|
|
466 | sint64 get_sint64 () const { return strtoll (value_nn, 0, 10); } |
|
|
467 | double get_double () const { return strtod (value_nn, 0); } |
|
|
468 | |
|
|
469 | void get (float &v) { v = get_double (); } |
|
|
470 | void get (double &v) { v = get_double (); } |
|
|
471 | |
|
|
472 | void get (bool &i) { i = get_bool (); } |
|
|
473 | void get (sint8 &i) { i = get_sint32 (); } |
|
|
474 | void get (uint8 &i) { i = get_sint32 (); } |
|
|
475 | void get (sint16 &i) { i = get_sint32 (); } |
|
|
476 | void get (uint16 &i) { i = get_sint32 (); } |
|
|
477 | void get (sint32 &i) { i = get_sint32 (); } |
|
|
478 | |
|
|
479 | void get (uint32 &i) { i = get_sint64 (); } |
|
|
480 | void get (sint64 &i) { i = get_sint64 (); } |
|
|
481 | |
|
|
482 | MTH void parse_warn (const_utf8_string msg); |
|
|
483 | MTH bool parse_error (const_utf8_string type = 0, const_utf8_string name = 0, bool skip = true); |
|
|
484 | |
|
|
485 | struct delayed_ref { |
|
|
486 | attachable *op; |
|
|
487 | object_ptr *ptr; |
|
|
488 | const_utf8_string ref; |
|
|
489 | }; |
|
|
490 | std::vector<delayed_ref> delrefs; |
|
|
491 | |
|
|
492 | void delayed_deref (attachable *op, object_ptr &ptr, const_utf8_string ref); |
|
|
493 | MTH void resolve_delayed_derefs (bool deref = true); |
|
|
494 | }; |
|
|
495 | |
|
|
496 | //TODO: remove |
|
|
497 | char *fgets (char *s, int n, object_thawer &thawer); |
|
|
498 | |
294 | |
499 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
295 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
500 | |
296 | |
501 | struct coroapi { |
297 | struct coroapi { |
502 | static struct CoroAPI *GCoroAPI; |
298 | static struct CoroAPI *GCoroAPI; |