… | |
… | |
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 | |
55 | extern sint64 *levels; // the experience table |
43 | extern sint64 *levels; // the experience table |
56 | |
44 | |
57 | typedef object object_ornull; |
45 | typedef object object_ornull; |
… | |
… | |
61 | #define newSVval64 newSVnv |
49 | #define newSVval64 newSVnv |
62 | #define SvVAL64 SvNV |
50 | #define SvVAL64 SvNV |
63 | |
51 | |
64 | static f_plug_api gethook = cfapi_get_hooks; |
52 | static f_plug_api gethook = cfapi_get_hooks; |
65 | static f_plug_api object_set_property = cfapi_object_set_property; |
53 | static f_plug_api object_set_property = cfapi_object_set_property; |
66 | static f_plug_api map_get_map = cfapi_map_get_map; |
|
|
67 | static f_plug_api object_insert = cfapi_object_insert; |
54 | static f_plug_api object_insert = cfapi_object_insert; |
|
|
55 | |
|
|
56 | static bool perl_booted; |
68 | |
57 | |
69 | /* this is a stupid way to do things, and awkward to use for plug-in authors */ |
58 | /* this is a stupid way to do things, and awkward to use for plug-in authors */ |
70 | typedef struct |
59 | typedef struct |
71 | { |
60 | { |
72 | object* who; |
61 | object* who; |
… | |
… | |
117 | return &PL_sv_undef; |
106 | return &PL_sv_undef; |
118 | |
107 | |
119 | if (!obj->self) |
108 | if (!obj->self) |
120 | obj->self = newSVptr (obj, klass); |
109 | obj->self = newSVptr (obj, klass); |
121 | |
110 | |
122 | return newSVsv (static_cast<SV *>(obj->self)); |
111 | return newSVsv (obj->self); |
123 | } |
112 | } |
124 | |
113 | |
125 | static void |
114 | static void |
126 | SVptr_cache_set (void *ptr, SV *sv) |
115 | SVptr_cache_set (void *ptr, SV *sv) |
127 | { |
116 | { |
… | |
… | |
316 | void attachable_base::clear () |
305 | void attachable_base::clear () |
317 | { |
306 | { |
318 | if (self) |
307 | if (self) |
319 | { |
308 | { |
320 | if (cb) |
309 | if (cb) |
321 | if (SvROK (*av_fetch ((AV *)cb, EVENT_OBJECT_DESTROY, 1))) |
310 | if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1))) |
322 | INVOKE_OBJECT (DESTROY, static_cast<object *>(this)); |
311 | INVOKE_OBJECT (DESTROY, static_cast<object *>(this)); |
323 | else if (SvROK (*av_fetch ((AV *)cb, EVENT_MAP_DESTROY, 1))) |
312 | else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1))) |
324 | INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this)); |
313 | INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this)); |
325 | |
314 | |
326 | // disconnect Perl from C, to avoid crashes |
315 | // disconnect Perl from C, to avoid crashes |
327 | sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); |
316 | sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); |
328 | |
317 | |
… | |
… | |
415 | void reattach (attachable<subclass> *obj) |
404 | void reattach (attachable<subclass> *obj) |
416 | { |
405 | { |
417 | obj->optimise (); |
406 | obj->optimise (); |
418 | |
407 | |
419 | if (obj->self) |
408 | if (obj->self) |
420 | reattach (subclass::get_dt (), (subclass *)obj); |
409 | reattach ((data_type) cftype<subclass>::dt, (subclass *)obj); |
421 | } |
410 | } |
422 | |
411 | |
|
|
412 | #include "kw_hash.h" |
|
|
413 | |
423 | object_freezer::object_freezer (const char *filename) |
414 | object_freezer::object_freezer () |
|
|
415 | : dynbuf (128 * 1024, 64 * 1024) |
424 | { |
416 | { |
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 (); |
417 | av = newAV (); |
436 | } |
418 | } |
437 | |
419 | |
438 | object_freezer::~object_freezer () |
420 | object_freezer::~object_freezer () |
439 | { |
421 | { |
440 | if (fp) |
422 | 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 | } |
423 | } |
458 | |
424 | |
459 | void object_freezer::put (attachable_base *ext) |
425 | void object_freezer::put (attachable_base *ext) |
460 | { |
426 | { |
461 | ext->optimise (); |
427 | ext->optimise (); |
462 | |
428 | |
463 | if (ext->self) |
429 | if (ext->self) |
464 | { |
430 | { |
465 | int idx = AvFILLp ((AV *)av) + 1; |
431 | int idx = AvFILLp ((AV *)av) + 1; |
466 | av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self)); |
432 | av_store (av, idx, SvREFCNT_inc (ext->self)); |
467 | fprintf (fp, "oid %d\n", idx); |
433 | |
|
|
434 | add ((void *)"oid ", 4); |
|
|
435 | add ((sint32)idx); |
|
|
436 | add ('\n'); |
468 | } |
437 | } |
|
|
438 | } |
|
|
439 | |
|
|
440 | bool object_freezer::save (const char *filename) |
|
|
441 | { |
|
|
442 | dSP; |
|
|
443 | ENTER; |
|
|
444 | SAVETMPS; |
|
|
445 | PUSHMARK (SP); |
|
|
446 | EXTEND (SP, 3); |
|
|
447 | PUSHs (sv_2mortal (newSVpv (filename, 0))); |
|
|
448 | PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ())))); |
|
|
449 | PUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
450 | PUTBACK; |
|
|
451 | call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); |
|
|
452 | FREETMPS; |
|
|
453 | LEAVE; |
|
|
454 | } |
|
|
455 | |
|
|
456 | int fprintf (object_freezer &freezer, const char *format, ...) |
|
|
457 | { |
|
|
458 | va_list ap; |
|
|
459 | |
|
|
460 | va_start (ap, format); |
|
|
461 | |
|
|
462 | int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap); |
|
|
463 | |
|
|
464 | if (len >= 0) |
|
|
465 | freezer.alloc (len); |
|
|
466 | |
|
|
467 | va_end (ap); |
|
|
468 | } |
|
|
469 | |
|
|
470 | int fputs (const char *s, object_freezer &freezer) |
|
|
471 | { |
|
|
472 | freezer.add (s); |
469 | } |
473 | } |
470 | |
474 | |
471 | object_thawer::object_thawer (const char *filename) |
475 | object_thawer::object_thawer (const char *filename) |
472 | { |
476 | { |
473 | av = 0; |
477 | av = 0; |
… | |
… | |
481 | { |
485 | { |
482 | LOG (llevError, "object_thawer: unable to open '%s': %s.\n", filename, strerror (errno)); |
486 | LOG (llevError, "object_thawer: unable to open '%s': %s.\n", filename, strerror (errno)); |
483 | return; |
487 | return; |
484 | } |
488 | } |
485 | |
489 | |
486 | if (perl) |
490 | if (perl_booted) |
487 | { |
491 | { |
488 | dSP; |
492 | dSP; |
489 | ENTER; |
493 | ENTER; |
490 | SAVETMPS; |
494 | SAVETMPS; |
491 | PUSHMARK (SP); |
495 | PUSHMARK (SP); |
… | |
… | |
495 | if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) |
499 | if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) |
496 | { |
500 | { |
497 | SPAGAIN; |
501 | SPAGAIN; |
498 | SV *sv = POPs; |
502 | SV *sv = POPs; |
499 | if (SvROK (sv)) |
503 | if (SvROK (sv)) |
500 | av = SvREFCNT_inc (SvRV (sv)); |
504 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
501 | } |
505 | } |
502 | |
506 | |
503 | FREETMPS; |
507 | FREETMPS; |
504 | LEAVE; |
508 | LEAVE; |
505 | } |
509 | } |
… | |
… | |
520 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
524 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
521 | return; |
525 | return; |
522 | } |
526 | } |
523 | |
527 | |
524 | ext->self = *svp; *svp = &PL_sv_undef; |
528 | ext->self = *svp; *svp = &PL_sv_undef; |
525 | sv_magic (SvRV ((SV *)ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); |
529 | sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); |
526 | |
530 | |
527 | reattach (type, obj); |
531 | reattach (type, obj); |
528 | } |
532 | } |
529 | |
533 | |
530 | object_thawer::~object_thawer () |
534 | object_thawer::~object_thawer () |
531 | { |
535 | { |
532 | if (fp) fclose (fp); |
536 | if (fp) fclose (fp); |
533 | if (av) SvREFCNT_dec ((AV *)av); |
537 | if (av) SvREFCNT_dec ((AV *)av); |
534 | } |
538 | } |
535 | |
539 | |
|
|
540 | token object_thawer::get_token () |
|
|
541 | { |
|
|
542 | #if 0 |
|
|
543 | for (;;) |
|
|
544 | { |
|
|
545 | if (!fgets (line, sizeof (line), fp)) |
|
|
546 | return token (KW_eof); |
|
|
547 | |
|
|
548 | unsigned char *p = (unsigned char *)line; |
|
|
549 | |
|
|
550 | while (*p > ' ') |
|
|
551 | p++; |
|
|
552 | |
|
|
553 | int len = p - (unsigned char *)line; |
|
|
554 | |
|
|
555 | while ((*p - 1) < ' ') |
|
|
556 | p++; |
|
|
557 | |
|
|
558 | if (*p) |
|
|
559 | { |
|
|
560 | char *v = p; |
|
|
561 | |
|
|
562 | while (*p && *p != '\n') |
|
|
563 | p++; |
|
|
564 | |
|
|
565 | *p = 0; |
|
|
566 | |
|
|
567 | return token (k, v); |
|
|
568 | } |
|
|
569 | else |
|
|
570 | return token (k); |
|
|
571 | } |
|
|
572 | #endif |
|
|
573 | } |
|
|
574 | |
536 | ///////////////////////////////////////////////////////////////////////////// |
575 | ///////////////////////////////////////////////////////////////////////////// |
537 | |
576 | |
538 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
577 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
539 | { |
578 | { |
540 | return 0; |
579 | return 0; |
… | |
… | |
637 | } |
676 | } |
638 | |
677 | |
639 | void |
678 | void |
640 | cfperl_init () |
679 | cfperl_init () |
641 | { |
680 | { |
|
|
681 | PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0); |
|
|
682 | perl = perl_alloc (); |
|
|
683 | perl_construct (perl); |
|
|
684 | |
|
|
685 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
|
|
686 | |
642 | char *argv[] = { |
687 | char *argv[] = { |
643 | "", |
688 | "", |
644 | "-e" |
689 | "-e" |
645 | "BEGIN {" |
|
|
646 | " cf->bootstrap;" |
690 | "cf->bootstrap;" |
647 | " unshift @INC, cf::datadir ();" |
691 | "unshift @INC, cf::datadir ();" |
648 | "}" |
|
|
649 | "" |
|
|
650 | "use cf;" |
|
|
651 | }; |
692 | }; |
652 | |
|
|
653 | perl = perl_alloc (); |
|
|
654 | perl_construct (perl); |
|
|
655 | |
|
|
656 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
|
|
657 | |
693 | |
658 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
694 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
659 | { |
695 | { |
660 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
696 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
661 | |
|
|
662 | exit (EXIT_FAILURE); |
697 | exit (EXIT_FAILURE); |
663 | //perl_destruct (perl); |
|
|
664 | //perl_free (perl); |
|
|
665 | //perl = 0; |
|
|
666 | //return; |
|
|
667 | } |
698 | } |
668 | |
699 | |
669 | obj_cache = newHV (); |
700 | obj_cache = newHV (); |
|
|
701 | } |
|
|
702 | |
|
|
703 | void cfperl_boot () |
|
|
704 | { |
|
|
705 | perl_booted = true; |
|
|
706 | |
|
|
707 | eval_pv ("require cf", 1); |
670 | } |
708 | } |
671 | |
709 | |
672 | void cfperl_main () |
710 | void cfperl_main () |
673 | { |
711 | { |
674 | dSP; |
712 | dSP; |
… | |
… | |
746 | dt = (data_type) va_arg (ap, int); |
784 | dt = (data_type) va_arg (ap, int); |
747 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
785 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
748 | op = va_arg (ap, object *); |
786 | op = va_arg (ap, object *); |
749 | |
787 | |
750 | if (op->cb) |
788 | if (op->cb) |
751 | gather_callbacks (callbacks, (AV *)op->cb, event); |
789 | gather_callbacks (callbacks, op->cb, event); |
752 | |
790 | |
753 | if (op->type) |
791 | if (op->type) |
754 | { |
792 | { |
755 | if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) |
793 | if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) |
756 | { |
794 | { |
… | |
… | |
777 | dt = (data_type) va_arg (ap, int); |
815 | dt = (data_type) va_arg (ap, int); |
778 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
816 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
779 | pl = va_arg (ap, player *); |
817 | pl = va_arg (ap, player *); |
780 | |
818 | |
781 | if (pl->cb) |
819 | if (pl->cb) |
782 | gather_callbacks (callbacks, (AV *)pl->cb, event); |
820 | gather_callbacks (callbacks, pl->cb, event); |
783 | |
821 | |
784 | gather_callbacks (callbacks, cb_player, event); |
822 | gather_callbacks (callbacks, cb_player, event); |
785 | break; |
823 | break; |
786 | |
824 | |
787 | case KLASS_MAP: |
825 | case KLASS_MAP: |
788 | dt = (data_type) va_arg (ap, int); |
826 | dt = (data_type) va_arg (ap, int); |
789 | assert (("first argument must be of type object", dt == DT_MAP)); |
827 | assert (("first argument must be of type object", dt == DT_MAP)); |
790 | map = va_arg (ap, mapstruct *); |
828 | map = va_arg (ap, mapstruct *); |
791 | |
829 | |
792 | if (map->cb) |
830 | if (map->cb) |
793 | gather_callbacks (callbacks, (AV *)map->cb, event); |
831 | gather_callbacks (callbacks, map->cb, event); |
794 | |
832 | |
795 | gather_callbacks (callbacks, cb_map, event); |
833 | gather_callbacks (callbacks, cb_map, event); |
796 | break; |
834 | break; |
797 | |
835 | |
798 | default: |
836 | default: |
… | |
… | |
1250 | |
1288 | |
1251 | const_iv (SK_EXP_ADD_SKILL) |
1289 | const_iv (SK_EXP_ADD_SKILL) |
1252 | const_iv (SK_EXP_TOTAL) |
1290 | const_iv (SK_EXP_TOTAL) |
1253 | const_iv (SK_EXP_NONE) |
1291 | const_iv (SK_EXP_NONE) |
1254 | const_iv (SK_SUBTRACT_SKILL_EXP) |
1292 | const_iv (SK_SUBTRACT_SKILL_EXP) |
|
|
1293 | const_iv (SK_EXP_SKILL_ONLY) |
1255 | |
1294 | |
1256 | const_iv (SK_LOCKPICKING) |
1295 | const_iv (SK_LOCKPICKING) |
1257 | const_iv (SK_HIDING) |
1296 | const_iv (SK_HIDING) |
1258 | const_iv (SK_SMITHERY) |
1297 | const_iv (SK_SMITHERY) |
1259 | const_iv (SK_BOWYER) |
1298 | const_iv (SK_BOWYER) |
… | |
… | |
1839 | |
1878 | |
1840 | void cf_object_drop (object *op, object *author) |
1879 | void cf_object_drop (object *op, object *author) |
1841 | |
1880 | |
1842 | void cf_object_take (object *op, object *author) |
1881 | void cf_object_take (object *op, object *author) |
1843 | |
1882 | |
1844 | void cf_object_say (object *op, char *msg) |
|
|
1845 | |
|
|
1846 | void cf_object_speak (object *op, char *msg) |
|
|
1847 | |
|
|
1848 | object *cf_object_insert_object (object *op, object *container) |
1883 | object *cf_object_insert_object (object *op, object *container) |
1849 | |
1884 | |
1850 | const char *cf_object_get_msg (object *ob) |
1885 | const char *cf_object_get_msg (object *ob) |
1851 | ALIAS: msg = 0 |
1886 | ALIAS: msg = 0 |
1852 | |
1887 | |
… | |
… | |
2316 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2351 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2317 | } |
2352 | } |
2318 | OUTPUT: |
2353 | OUTPUT: |
2319 | RETVAL |
2354 | RETVAL |
2320 | |
2355 | |
2321 | # "deserialise" perl map data into the map # TODO# compatibility cruft, remove |
|
|
2322 | void |
|
|
2323 | _set_obs (mapstruct *map, SV *sv) |
|
|
2324 | CODE: |
|
|
2325 | { |
|
|
2326 | object *o; |
|
|
2327 | AV *av; |
|
|
2328 | int x, y; |
|
|
2329 | AV *obs = (AV *)SvRV (sv); |
|
|
2330 | |
|
|
2331 | for (y = 0; y < MAP_HEIGHT (map); y++) |
|
|
2332 | for (x = 0; x < MAP_WIDTH (map); x++) |
|
|
2333 | { |
|
|
2334 | sv = *av_fetch (obs, x + y * MAP_HEIGHT (map), 1); |
|
|
2335 | |
|
|
2336 | if (!SvROK (sv)) |
|
|
2337 | continue; |
|
|
2338 | |
|
|
2339 | av = (AV *)SvRV (sv); |
|
|
2340 | |
|
|
2341 | for (o = GET_MAP_OB (map, x, y); o; o = o->above) |
|
|
2342 | { |
|
|
2343 | sv = av_shift (av); |
|
|
2344 | |
|
|
2345 | if (SvROK (sv)) |
|
|
2346 | { |
|
|
2347 | sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0); |
|
|
2348 | SVptr_cache_set (o, sv); |
|
|
2349 | } |
|
|
2350 | } |
|
|
2351 | } |
|
|
2352 | } |
|
|
2353 | |
|
|
2354 | |
2356 | |
2355 | MODULE = cf PACKAGE = cf::arch |
2357 | MODULE = cf PACKAGE = cf::arch |
|
|
2358 | |
|
|
2359 | archetype *find (const char *name) |
|
|
2360 | CODE: |
|
|
2361 | RETVAL = find_archetype (name); |
|
|
2362 | OUTPUT: |
|
|
2363 | RETVAL |
2356 | |
2364 | |
2357 | archetype *first() |
2365 | archetype *first() |
2358 | PROTOTYPE: |
2366 | PROTOTYPE: |
2359 | CODE: |
2367 | CODE: |
2360 | RETVAL = first_archetype; |
2368 | RETVAL = first_archetype; |