ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/cfperl.h
(Generate patch)

Comparing deliantra/server/include/cfperl.h (file contents):
Revision 1.117 by root, Fri Apr 2 03:41:24 2010 UTC vs.
Revision 1.127 by root, Sun Nov 11 04:29:11 2012 UTC

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,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,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// 23//
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
29#include <cstdarg>
30#include <cstdio>
31#include <bitset>
32 28
33using namespace std; 29using namespace std;
34 30
35#include <EXTERN.h> 31#include <EXTERN.h>
36#include <perl.h> 32#include <perl.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
87inline int call_pvsv (const char *ob, I32 flags) { return call_pv (ob, flags); } 83inline int call_pvsv (const char *ob, I32 flags) { return call_pv (ob, flags); }
88inline int call_pvsv (SV *ob, I32 flags) { return call_sv (ob, flags); } 84inline 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
98void cfperl_init (); 97void cfperl_init ();
99void cfperl_main (); 98void cfperl_main ();
102void cfperl_cleanup (int make_core); 101void cfperl_cleanup (int make_core);
103void cfperl_make_book (object *book, int level); 102void cfperl_make_book (object *book, int level);
104void cfperl_send_msg (client *ns, int color, const_utf8_string type, const_utf8_string msg); 103void cfperl_send_msg (client *ns, int color, const_utf8_string type, const_utf8_string msg);
105int cfperl_can_merge (object *ob1, object *ob2); 104int cfperl_can_merge (object *ob1, object *ob2);
106void cfperl_mapscript_activate (object *ob, int state, object *activator, object *originator = 0); 105void cfperl_mapscript_activate (object *ob, int state, object *activator, object *originator = 0);
106void cfperl_ix (client *ns, faceidx idx, int pri, uint32 size, uint32 fofs);
107 107
108bool is_match_expr (const_utf8_string expr); 108bool is_match_expr (const_utf8_string expr);
109// applies the match expression and returns true if it matches 109// applies the match expression and returns true if it matches
110bool match (const_utf8_string expr, object *ob, object *self = 0, object *source = 0, object *originator = 0); 110bool match (const_utf8_string expr, object *ob, object *self = 0, object *source = 0, object *originator = 0);
111// same as above, but returns the first object found, or 0 111// same as above, but returns the first object found, or 0
130#endif 130#endif
131 131
132extern tstamp runtime; // virtual server time, excluding time jumps and lag 132extern tstamp runtime; // virtual server time, excluding time jumps and lag
133extern tstamp NOW; // real time of current server tick 133extern tstamp NOW; // real time of current server tick
134 134
135noinline utf8_string cfSvPVutf8_nolen (SV *sv);
136
135enum event_klass 137enum event_klass
136{ 138{
137 KLASS_NONE, 139 KLASS_NONE,
138 KLASS_GLOBAL, 140 KLASS_GLOBAL,
139 KLASS_ATTACHABLE, 141 KLASS_ATTACHABLE,
214 MTH void destroy (); 216 MTH void destroy ();
215 217
216 // return wether an object was destroyed already 218 // return wether an object was destroyed already
217 MTH bool destroyed () const { return attachable_flags & F_DESTROYED; } 219 MTH bool destroyed () const { return attachable_flags & F_DESTROYED; }
218 220
221 // destruct and free the memory for this object
222 virtual void do_delete ();
223
219 virtual void gather_callbacks (AV *&callbacks, event_type event) const; 224 virtual void gather_callbacks (AV *&callbacks, event_type event) const;
220 225
221#if 0 226#if 0
222private: 227private:
223 static refcounted *rc_first; 228 static refcounted *rc_first;
286 291
287extern struct global gbl_ev; 292extern struct global gbl_ev;
288 293
289////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 294//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
290 295
291// a little dirty hack, maybe unify with something else at a later time 296#include "freezethaw.h"
292struct 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
315INTERFACE_CLASS(object_freezer)
316struct 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
429INTERFACE_CLASS(object_thawer)
430struct 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
497char *fgets (char *s, int n, object_thawer &thawer);
498 297
499////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 298//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
500 299
501struct coroapi { 300struct coroapi {
502 static struct CoroAPI *GCoroAPI; 301 static struct CoroAPI *GCoroAPI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines