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

Comparing deliantra/server/server/cfperl.xs (file contents):
Revision 1.31 by root, Wed Aug 30 12:08:15 2006 UTC vs.
Revision 1.41 by root, Sun Sep 3 22:45:57 2006 UTC

21 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 23 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24*/ 24*/
25 25
26#include <EXTERN.h>
27#include <perl.h>
28#include <XSUB.h>
29
30#undef save_long // clashes with libproto.h
31
32#define PLUGIN_NAME "perl" 26#define PLUGIN_NAME "perl"
33#define PLUGIN_VERSION "cfperl 0.5" 27#define PLUGIN_VERSION "cfperl 0.5"
34
35#ifndef __CEXTRACT__
36#include <plugin.h>
37#endif
38
39#undef MODULEAPI
40#ifdef WIN32
41#else
42#define MODULEAPI
43#endif
44 28
45#include <plugin_common.h> 29#include <plugin_common.h>
46#include <sounds.h> 30#include <sounds.h>
47#include <cstdarg> 31#include <cstdarg>
48#include <sproto.h> 32#include <sproto.h>
49 33
50#include "cfperl.h" 34#include "cfperl.h"
51#include "shstr.h" 35#include "shstr.h"
52 36
37#include <EXTERN.h>
38#include <perl.h>
39#include <XSUB.h>
40
53#include "perlxsi.c" 41#include "perlxsi.c"
54 42
55extern sint64 *levels; // the experience table 43extern sint64 *levels; // the experience table
56 44
57typedef object object_ornull; 45typedef object object_ornull;
61#define newSVval64 newSVnv 49#define newSVval64 newSVnv
62#define SvVAL64 SvNV 50#define SvVAL64 SvNV
63 51
64static f_plug_api gethook = cfapi_get_hooks; 52static f_plug_api gethook = cfapi_get_hooks;
65static f_plug_api object_set_property = cfapi_object_set_property; 53static f_plug_api object_set_property = cfapi_object_set_property;
66static f_plug_api map_get_map = cfapi_map_get_map;
67static f_plug_api object_insert = cfapi_object_insert; 54static f_plug_api object_insert = cfapi_object_insert;
68 55
69/* this is a stupid way to do things, and awkward to use for plug-in authors */ 56/* this is a stupid way to do things, and awkward to use for plug-in authors */
70typedef struct 57typedef struct
71{ 58{
117 return &PL_sv_undef; 104 return &PL_sv_undef;
118 105
119 if (!obj->self) 106 if (!obj->self)
120 obj->self = newSVptr (obj, klass); 107 obj->self = newSVptr (obj, klass);
121 108
122 return newSVsv (static_cast<SV *>(obj->self)); 109 return newSVsv (obj->self);
123} 110}
124 111
125static void 112static void
126SVptr_cache_set (void *ptr, SV *sv) 113SVptr_cache_set (void *ptr, SV *sv)
127{ 114{
316void attachable_base::clear () 303void attachable_base::clear ()
317{ 304{
318 if (self) 305 if (self)
319 { 306 {
320 if (cb) 307 if (cb)
321 if (SvROK (*av_fetch ((AV *)cb, EVENT_OBJECT_DESTROY, 1))) 308 if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1)))
322 INVOKE_OBJECT (DESTROY, static_cast<object *>(this)); 309 INVOKE_OBJECT (DESTROY, static_cast<object *>(this));
323 else if (SvROK (*av_fetch ((AV *)cb, EVENT_MAP_DESTROY, 1))) 310 else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1)))
324 INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this)); 311 INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this));
325 312
326 // disconnect Perl from C, to avoid crashes 313 // disconnect Perl from C, to avoid crashes
327 sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); 314 sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext);
328 315
337 { 324 {
338 SvREFCNT_dec (cb); 325 SvREFCNT_dec (cb);
339 cb = 0; 326 cb = 0;
340 } 327 }
341 328
342 if (attach)
343 {
344 free_string (attach);
345 attach = 0; 329 attach = 0;
346 }
347} 330}
348 331
349void attachable_base::optimise () 332void attachable_base::optimise ()
350{ 333{
351 if (!self) 334 if (!self)
370 PUSHMARK (SP); 353 PUSHMARK (SP);
371 EXTEND (SP, 2); 354 EXTEND (SP, 2);
372 PUSHs (sv_2mortal (newSVdt (type, obj))); 355 PUSHs (sv_2mortal (newSVdt (type, obj)));
373 PUSHs (sv_2mortal (newSVpv (attach, 0))); 356 PUSHs (sv_2mortal (newSVpv (attach, 0)));
374 357
375 free_string (attach);
376 attach = 0; 358 attach = 0;
377 359
378 PUTBACK; 360 PUTBACK;
379 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); 361 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
380 FREETMPS; 362 FREETMPS;
415void reattach (attachable<subclass> *obj) 397void reattach (attachable<subclass> *obj)
416{ 398{
417 obj->optimise (); 399 obj->optimise ();
418 400
419 if (obj->self) 401 if (obj->self)
420 reattach (subclass::get_dt (), (subclass *)obj); 402 reattach ((data_type) cftype<subclass>::dt, (subclass *)obj);
421} 403}
422 404
405#include "kw_hash.h"
406
423object_freezer::object_freezer (const char *filename) 407object_freezer::object_freezer ()
408: dynbuf (128 * 1024, 64 * 1024)
424{ 409{
425 this->filename = (SV *)newSVpv (filename, 0);
426
427 char filename2 [4096];
428 snprintf (filename2, 4096, "%s~", filename);
429
430 fp = fopen (filename2, "w");
431
432 if (!fp)
433 LOG (llevError, "cannot open file '%s' for writing: %s\n", filename2, strerror (errno));
434
435 av = (AV *)newAV (); 410 av = newAV ();
436} 411}
437 412
438object_freezer::~object_freezer () 413object_freezer::~object_freezer ()
439{ 414{
440 if (fp) 415 SvREFCNT_dec (av);
441 {
442 fclose (fp);
443
444 dSP;
445 ENTER;
446 SAVETMPS;
447 PUSHMARK (SP);
448 XPUSHs (sv_2mortal ((SV *)filename));
449 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
450 PUTBACK;
451 call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL);
452 FREETMPS;
453 LEAVE;
454 }
455 else
456 SvREFCNT_dec ((SV *)filename);
457} 416}
458 417
459void object_freezer::put (attachable_base *ext) 418void object_freezer::put (attachable_base *ext)
460{ 419{
461 ext->optimise (); 420 ext->optimise ();
462 421
463 if (ext->self) 422 if (ext->self)
464 { 423 {
465 int idx = AvFILLp ((AV *)av) + 1; 424 int idx = AvFILLp ((AV *)av) + 1;
466 av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self)); 425 av_store (av, idx, SvREFCNT_inc (ext->self));
467 fprintf (fp, "oid %d\n", idx); 426
427 add ((void *)"oid ", 4);
428 add ((sint32)idx);
429 add ('\n');
468 } 430 }
469} 431}
470 432
433bool object_freezer::save (const char *filename)
434{
435 dSP;
436 ENTER;
437 SAVETMPS;
438 PUSHMARK (SP);
439 EXTEND (SP, 3);
440 PUSHs (sv_2mortal (newSVpv (filename, 0)));
441 PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ()))));
442 PUSHs (sv_2mortal (newRV_inc ((SV *)av)));
443 PUTBACK;
444 call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL);
445 FREETMPS;
446 LEAVE;
447}
448
449int fprintf (object_freezer &freezer, const char *format, ...)
450{
451 va_list ap;
452
453 va_start (ap, format);
454
455 int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap);
456
457 if (len >= 0)
458 freezer.alloc (len);
459
460 va_end (ap);
461}
462
463int fputs (const char *s, object_freezer &freezer)
464{
465 freezer.add (s);
466}
467
471object_thawer::object_thawer (const char *filename) 468object_thawer::object_thawer (const char *filename)
472{ 469{
470 static const char eof[] = "\n\n\n\0\0\0";
471
473 av = 0; 472 av = 0;
474 fp = 0; 473 text = 0;
475 474
476 if (!filename) 475 if (filename)
477 return;
478
479 fp = fopen (filename, "r");
480 if (!fp)
481 {
482 LOG (llevError, "object_thawer: unable to open '%s': %s.\n", filename, strerror (errno));
483 return;
484 }
485
486 if (perl)
487 { 476 {
488 dSP; 477 dSP;
489 ENTER; 478 ENTER;
490 SAVETMPS; 479 SAVETMPS;
491 PUSHMARK (SP); 480 PUSHMARK (SP);
492 XPUSHs (sv_2mortal (newSVpv (filename, 0))); 481 XPUSHs (sv_2mortal (newSVpv (filename, 0)));
493 PUTBACK; 482 PUTBACK;
494 483
495 if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) 484 if (2 == call_pv ("cf::object_thawer_load", G_ARRAY | G_EVAL))
496 { 485 {
497 SPAGAIN; 486 SPAGAIN;
487
488 // second value - perl objects
489 {
498 SV *sv = POPs; 490 SV *sv = POPs;
499 if (SvROK (sv)) 491 if (SvROK (sv))
500 av = SvREFCNT_inc (SvRV (sv)); 492 av = (AV *)SvREFCNT_inc (SvRV (sv));
493 }
494
495 // first value - text part, pad with 3 zeroes
496 {
497 SV *sv = POPs;
498 STRLEN len;
499 char *sv_ = SvPVbyte (sv, len);
500 text = newSV (len + sizeof (eof));
501 SvCUR_set (text, len);
502 memcpy (SvPVX (text), sv_, len);
503 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
504 }
501 } 505 }
502 506
507 PUTBACK;
503 FREETMPS; 508 FREETMPS;
504 LEAVE; 509 LEAVE;
505 } 510 }
506}
507 511
508// compatibility support, should be removed when no longer needed 512 if (!text)
509int fprintf (object_freezer &freezer, const char *format, ...) 513 text = newSVpvn (eof, sizeof (eof));
510{
511 va_list ap;
512 514
513 va_start (ap, format); 515 line = SvPVX (text);
514 vfprintf (freezer.fp, format, ap);
515 va_end (ap);
516}
517
518int fputs (const char *s, object_freezer &freezer)
519{
520 fputs (s, freezer.fp);
521} 516}
522 517
523void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) 518void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid)
524{ 519{
525 if (!av || oid < 0) // this is actually an error of sorts 520 if (!av || oid < 0) // this is actually an error of sorts
535 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 530 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
536 return; 531 return;
537 } 532 }
538 533
539 ext->self = *svp; *svp = &PL_sv_undef; 534 ext->self = *svp; *svp = &PL_sv_undef;
540 sv_magic (SvRV ((SV *)ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); 535 sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0);
541 536
542 reattach (type, obj); 537 reattach (type, obj);
543} 538}
544 539
545object_thawer::~object_thawer () 540object_thawer::~object_thawer ()
546{ 541{
547 if (fp) fclose (fp); 542 if (text) SvREFCNT_dec (text);
548 if (av) SvREFCNT_dec ((AV *)av); 543 if (av) SvREFCNT_dec (av);
544}
545
546char *fgets (char *s, int n, object_thawer &thawer)
547{
548 char *p = thawer.line;
549 char *q = s;
550
551 while (--n)
552 {
553 if (!*p)
554 break;
555
556 *q++ = *p;
557
558 if (*p++ == '\n')
559 break;
560 }
561
562 *q = 0;
563 thawer.line = p;
564
565 return s == q ? 0 : s;
566}
567
568keyword object_thawer::get_kv ()
569{
570 for (;;)
571 {
572 char *p = line;
573
574 if (!*p)
575 return KW_EOF;
576
577 // parse keyword
578 while (*p > ' ')
579 p++;
580
581 int klen = p - line;
582
583 if (*p++ != '\n')
584 {
585 // parse value
586 while (*p <= ' ' && *p != '\n') // skip 0x01 .. 0x20
587 ++p;
588
589 last_value = p;
590
591 while (*p != '\n')
592 p++;
593
594 *p++ = 0;
595 }
596 else
597 last_value = 0;
598
599 line [klen] = 0;
600 keyword_idx *kw = kw_lex::match (line, klen);
601
602 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D
603
604 if (kw)
605 {
606 line = p;
607 return kw->index;
608 }
609 else if (!*line || *line == '#')
610 {
611 // empty/comment line
612 line = p;
613 }
614 else
615 return KW_ERROR;
616 }
617}
618
619void object_thawer::get (shstr &sh) const
620{
621 if (last_value)
622 sh = last_value;
623 else
624 {
625 sh = "<value missing>";
626 LOG (llevError, "keyword requires value: <%s>\n", line);//TODO: add filename
627 }
628}
629
630void object_thawer::get_ml (keyword kend, shstr &sh)
631{
632 char kw[128];
633
634 // multi-line strings are delimited by "\nendXXX\n"
635 kw [0] = '\n';
636 strcpy (kw + 1, keyword_str [kend]);
637
638 char *end = strstr (line, kw);
639
640 if (!end)
641 {
642 sh = 0;
643 return;
644 }
645
646 *end = 0;
647 sh = line;
648
649 line = end + keyword_len [kend] + 1;
650
651 while (*line++ != '\n')
652 ;
653}
654
655sint32 object_thawer::get_sint32 () const
656{
657 char *p = last_value;
658
659 if (!p)
660 return 0;
661
662 sint32 val = 0;
663 bool negate;
664
665 if (*p == '-')
666 {
667 negate = true;
668 ++p;
669 }
670 else
671 negate = false;
672
673 do
674 {
675 val *= 10;
676 val += *p++ - '0';
677 }
678 while (*p);
679
680 return negate ? -val : val;
681}
682
683sint64 object_thawer::get_sint64 () const
684{
685 return last_value ? atoll (last_value) : 0;
686}
687
688double object_thawer::get_double () const
689{
690 return last_value ? atof (last_value) : 0;
549} 691}
550 692
551///////////////////////////////////////////////////////////////////////////// 693/////////////////////////////////////////////////////////////////////////////
552 694
553extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) 695extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr)
652} 794}
653 795
654void 796void
655cfperl_init () 797cfperl_init ()
656{ 798{
799 PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0);
800 perl = perl_alloc ();
801 perl_construct (perl);
802
803 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
804
657 char *argv[] = { 805 char *argv[] = {
658 "", 806 "",
659 "-e" 807 "-e"
660 "BEGIN {"
661 " cf->bootstrap;" 808 "cf->bootstrap;"
662 " unshift @INC, cf::datadir ();" 809 "unshift @INC, cf::datadir ();"
663 "}"
664 ""
665 "use cf;" 810 "require cf;"
666 }; 811 };
667
668 perl = perl_alloc ();
669 perl_construct (perl);
670
671 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
672 812
673 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) 813 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl))
674 { 814 {
675 printf ("unable to initialize perl-interpreter, aborting.\n"); 815 printf ("unable to initialize perl-interpreter, aborting.\n");
676
677 exit (EXIT_FAILURE); 816 exit (EXIT_FAILURE);
678 //perl_destruct (perl);
679 //perl_free (perl);
680 //perl = 0;
681 //return;
682 } 817 }
683 818
684 obj_cache = newHV (); 819 obj_cache = newHV ();
685} 820}
686 821
761 dt = (data_type) va_arg (ap, int); 896 dt = (data_type) va_arg (ap, int);
762 assert (("first argument must be of type object", dt == DT_OBJECT)); 897 assert (("first argument must be of type object", dt == DT_OBJECT));
763 op = va_arg (ap, object *); 898 op = va_arg (ap, object *);
764 899
765 if (op->cb) 900 if (op->cb)
766 gather_callbacks (callbacks, (AV *)op->cb, event); 901 gather_callbacks (callbacks, op->cb, event);
767 902
768 if (op->type) 903 if (op->type)
769 { 904 {
770 if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) 905 if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type))
771 { 906 {
792 dt = (data_type) va_arg (ap, int); 927 dt = (data_type) va_arg (ap, int);
793 assert (("first argument must be of type player", dt == DT_PLAYER)); 928 assert (("first argument must be of type player", dt == DT_PLAYER));
794 pl = va_arg (ap, player *); 929 pl = va_arg (ap, player *);
795 930
796 if (pl->cb) 931 if (pl->cb)
797 gather_callbacks (callbacks, (AV *)pl->cb, event); 932 gather_callbacks (callbacks, pl->cb, event);
798 933
799 gather_callbacks (callbacks, cb_player, event); 934 gather_callbacks (callbacks, cb_player, event);
800 break; 935 break;
801 936
802 case KLASS_MAP: 937 case KLASS_MAP:
803 dt = (data_type) va_arg (ap, int); 938 dt = (data_type) va_arg (ap, int);
804 assert (("first argument must be of type object", dt == DT_MAP)); 939 assert (("first argument must be of type object", dt == DT_MAP));
805 map = va_arg (ap, mapstruct *); 940 map = va_arg (ap, mapstruct *);
806 941
807 if (map->cb) 942 if (map->cb)
808 gather_callbacks (callbacks, (AV *)map->cb, event); 943 gather_callbacks (callbacks, map->cb, event);
809 944
810 gather_callbacks (callbacks, cb_map, event); 945 gather_callbacks (callbacks, cb_map, event);
811 break; 946 break;
812 947
813 default: 948 default:
1265 1400
1266 const_iv (SK_EXP_ADD_SKILL) 1401 const_iv (SK_EXP_ADD_SKILL)
1267 const_iv (SK_EXP_TOTAL) 1402 const_iv (SK_EXP_TOTAL)
1268 const_iv (SK_EXP_NONE) 1403 const_iv (SK_EXP_NONE)
1269 const_iv (SK_SUBTRACT_SKILL_EXP) 1404 const_iv (SK_SUBTRACT_SKILL_EXP)
1405 const_iv (SK_EXP_SKILL_ONLY)
1270 1406
1271 const_iv (SK_LOCKPICKING) 1407 const_iv (SK_LOCKPICKING)
1272 const_iv (SK_HIDING) 1408 const_iv (SK_HIDING)
1273 const_iv (SK_SMITHERY) 1409 const_iv (SK_SMITHERY)
1274 const_iv (SK_BOWYER) 1410 const_iv (SK_BOWYER)
1854 1990
1855void cf_object_drop (object *op, object *author) 1991void cf_object_drop (object *op, object *author)
1856 1992
1857void cf_object_take (object *op, object *author) 1993void cf_object_take (object *op, object *author)
1858 1994
1859void cf_object_say (object *op, char *msg)
1860
1861void cf_object_speak (object *op, char *msg)
1862
1863object *cf_object_insert_object (object *op, object *container) 1995object *cf_object_insert_object (object *op, object *container)
1864 1996
1865const char *cf_object_get_msg (object *ob) 1997const char *cf_object_get_msg (object *ob)
1866 ALIAS: msg = 0 1998 ALIAS: msg = 0
1867 1999
2333 OUTPUT: 2465 OUTPUT:
2334 RETVAL 2466 RETVAL
2335 2467
2336 2468
2337MODULE = cf PACKAGE = cf::arch 2469MODULE = cf PACKAGE = cf::arch
2470
2471archetype *find (const char *name)
2472 CODE:
2473 RETVAL = find_archetype (name);
2474 OUTPUT:
2475 RETVAL
2338 2476
2339archetype *first() 2477archetype *first()
2340 PROTOTYPE: 2478 PROTOTYPE:
2341 CODE: 2479 CODE:
2342 RETVAL = first_archetype; 2480 RETVAL = first_archetype;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines