1 | /*****************************************************************************/ |
1 | /*****************************************************************************/ |
2 | /* CrossFire, A Multiplayer game for the X Window System */ |
2 | /* CrossFire, A Multiplayer game for the X Window System */ |
3 | /* */ |
|
|
4 | /*****************************************************************************/ |
3 | /*****************************************************************************/ |
5 | |
4 | |
6 | /* |
5 | /* |
7 | * This code is placed under the GNU General Public Licence (GPL) |
6 | * This code is placed under the GNU General Public Licence (GPL) |
8 | * |
7 | * |
… | |
… | |
29 | #include <XSUB.h> |
28 | #include <XSUB.h> |
30 | |
29 | |
31 | #undef save_long // clashes with libproto.h |
30 | #undef save_long // clashes with libproto.h |
32 | |
31 | |
33 | #define PLUGIN_NAME "perl" |
32 | #define PLUGIN_NAME "perl" |
34 | #define PLUGIN_VERSION "cfperl 0.2" |
33 | #define PLUGIN_VERSION "cfperl 0.3" |
35 | |
34 | |
36 | #ifndef __CEXTRACT__ |
35 | #ifndef __CEXTRACT__ |
37 | #include <plugin.h> |
36 | #include <plugin.h> |
38 | #endif |
37 | #endif |
39 | |
38 | |
… | |
… | |
78 | } CFPContext; |
77 | } CFPContext; |
79 | |
78 | |
80 | static HV *obj_cache; |
79 | static HV *obj_cache; |
81 | static PerlInterpreter *perl; |
80 | static PerlInterpreter *perl; |
82 | |
81 | |
|
|
82 | #define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value)))) |
83 | #define PUSHcfapi(type,ctype) PUSHs (sv_2mortal (newSVcfapi ((type), va_arg (args, ctype)))) |
83 | #define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype)) |
84 | #define PUSH_OB PUSHcfapi(CFAPI_POBJECT, object *) |
84 | #define PUSH_OB PUSHcfapi_va(POBJECT, object *) |
85 | #define PUSH_PL PUSHcfapi(CFAPI_PPLAYER, player *) |
85 | #define PUSH_PL PUSHcfapi_va(PPLAYER, player *) |
86 | #define PUSH_MAP PUSHcfapi(CFAPI_PMAP, mapstruct *) |
86 | #define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *) |
87 | #define PUSH_PV PUSHcfapi(CFAPI_STRING, const char *) |
87 | #define PUSH_PV PUSHcfapi_va(STRING, const char *) |
88 | #define PUSH_IV PUSHcfapi(CFAPI_INT, int) |
88 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
89 | |
89 | |
90 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
90 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
91 | |
91 | |
92 | // garbage collect some perl objects, if possible |
92 | // garbage collect some perl objects, if possible |
93 | // all objects no longer referenced and empty are |
93 | // all objects no longer referenced and empty are |
… | |
… | |
229 | case CFAPI_LONG: |
229 | case CFAPI_LONG: |
230 | sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */ |
230 | sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */ |
231 | break; |
231 | break; |
232 | |
232 | |
233 | case CFAPI_DOUBLE: |
233 | case CFAPI_DOUBLE: |
234 | sv = newSViv (*va_arg (args, double *)); |
234 | sv = newSVnv (*va_arg (args, double *)); |
235 | break; |
235 | break; |
236 | |
236 | |
237 | case CFAPI_STRING: |
237 | case CFAPI_STRING: |
238 | { |
238 | { |
239 | char *str = va_arg (args, char *); |
239 | char *str = va_arg (args, char *); |
… | |
… | |
557 | PUTBACK; |
557 | PUTBACK; |
558 | int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL); |
558 | int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL); |
559 | SPAGAIN; |
559 | SPAGAIN; |
560 | |
560 | |
561 | if (SvTRUE (ERRSV)) |
561 | if (SvTRUE (ERRSV)) |
562 | LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
562 | LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
563 | |
563 | |
564 | rv = count > 0 ? POPi : 0; |
564 | rv = count > 0 ? POPi : 0; |
565 | |
565 | |
566 | PUTBACK; |
566 | PUTBACK; |
567 | FREETMPS; |
567 | FREETMPS; |
… | |
… | |
572 | } |
572 | } |
573 | |
573 | |
574 | void * |
574 | void * |
575 | eventListener (int *type, ...) |
575 | eventListener (int *type, ...) |
576 | { |
576 | { |
577 | static int rv = 0; |
577 | static int rv; |
578 | va_list args; |
578 | va_list args; |
579 | char *buf; |
579 | int event_code; |
580 | CFPContext context; |
580 | object *who, *activator, *third, *event; |
|
|
581 | char *message, *extension, *options; |
581 | |
582 | |
582 | if (!perl) |
583 | if (!perl) |
583 | return; |
584 | return; |
584 | |
585 | |
585 | memset (&context, 0, sizeof (context)); |
|
|
586 | |
|
|
587 | va_start (args, type); |
586 | va_start (args, type); |
588 | |
|
|
589 | context.who = va_arg (args, object *); |
587 | who = va_arg (args, object *); |
590 | context.event_code = va_arg (args, int); |
588 | event_code = va_arg (args, int); |
591 | context.activator = va_arg (args, object *); |
589 | activator = va_arg (args, object *); |
592 | context.third = va_arg (args, object *); |
590 | third = va_arg (args, object *); |
593 | |
|
|
594 | buf = va_arg (args, char *); |
591 | message = va_arg (args, char *); |
595 | if (buf != 0) |
592 | va_arg (args, int); // fix yourself |
596 | strncpy (context.message, buf, sizeof (context.message)); |
593 | extension = va_arg (args, char *); |
597 | |
594 | options = va_arg (args, char *); |
598 | context.fix = va_arg (args, int); |
|
|
599 | strncpy (context.extension, va_arg (args, char *), sizeof (context.extension)); |
|
|
600 | strncpy (context.options, va_arg (args, char *), sizeof (context.options)); |
|
|
601 | context.event = va_arg (args, object *); |
595 | event = va_arg (args, object *); |
602 | va_end (args); |
596 | va_end (args); |
603 | |
597 | |
604 | { |
598 | { |
605 | dSP; |
599 | dSP; |
606 | |
600 | |
607 | ENTER; |
601 | ENTER; |
608 | SAVETMPS; |
602 | SAVETMPS; |
609 | |
603 | |
610 | PUSHMARK (SP); |
604 | PUSHMARK (SP); |
611 | |
|
|
612 | EXTEND (SP, 10); |
605 | EXTEND (SP, 10); |
613 | |
606 | |
614 | HV *hv = newHV (); |
607 | PUSHcfapi (STRING, extension); |
615 | #define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context.expr), 0) |
608 | PUSHs (sv_2mortal (newSViv (event_code))); |
616 | hv_context (CFAPI_POBJECT, ,who); |
|
|
617 | hv_context (CFAPI_POBJECT, ,activator); |
|
|
618 | hv_context (CFAPI_POBJECT, ,third); |
|
|
619 | hv_context (CFAPI_POBJECT, ,event); |
|
|
620 | hv_context (CFAPI_PMAP, ,map); |
|
|
621 | hv_context (CFAPI_STRING , ,message); |
|
|
622 | hv_context (CFAPI_INT ,&,fix); |
|
|
623 | hv_context (CFAPI_INT ,&,event_code); |
|
|
624 | hv_context (CFAPI_STRING , ,options); |
|
|
625 | hv_context (CFAPI_STRING , ,extension); |
|
|
626 | |
609 | |
627 | PUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
610 | PUSHcfapi (POBJECT, event); |
|
|
611 | PUSHcfapi (POBJECT, who); |
|
|
612 | |
|
|
613 | switch (event_code) |
|
|
614 | { |
|
|
615 | case EVENT_STOP: // $ob (e.g. arrow) |
|
|
616 | case EVENT_TIME: // $ob |
|
|
617 | case EVENT_TIMER: // $ob |
|
|
618 | break; |
|
|
619 | |
|
|
620 | case EVENT_APPLY: // $ob, $who |
|
|
621 | case EVENT_DROP: // $ob, $who |
|
|
622 | case EVENT_CLOSE: // $ob, $who |
|
|
623 | case EVENT_DEATH: // $ob[, $killer] |
|
|
624 | case EVENT_MOVE: // $ob, $enemy |
|
|
625 | case EVENT_THROW: // $ob, $thrower |
|
|
626 | PUSHcfapi (POBJECT, activator); |
|
|
627 | break; |
|
|
628 | |
|
|
629 | case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me) |
|
|
630 | PUSHcfapi (POBJECT, activator); |
|
|
631 | PUSHcfapi (POBJECT, third); |
|
|
632 | break; |
|
|
633 | |
|
|
634 | case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg] |
|
|
635 | PUSHcfapi (POBJECT, activator); |
|
|
636 | PUSHcfapi (POBJECT, third); |
|
|
637 | PUSHcfapi (POBJECT, message); |
|
|
638 | break; |
|
|
639 | |
|
|
640 | case EVENT_SAY: // $ob, $who, $msg |
|
|
641 | PUSHcfapi (POBJECT, activator); |
|
|
642 | PUSHcfapi (STRING, message); |
|
|
643 | break; |
|
|
644 | |
|
|
645 | default: |
|
|
646 | LOG (llevError, "perl plugin called for unsupported event type %d", event_code); |
|
|
647 | break; |
|
|
648 | } |
628 | |
649 | |
629 | PUTBACK; |
650 | PUTBACK; |
630 | int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); |
651 | int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); |
631 | SPAGAIN; |
652 | SPAGAIN; |
632 | |
653 | |
633 | if (SvTRUE (ERRSV)) |
654 | if (SvTRUE (ERRSV)) |
634 | LOG (llevError, "event '%d' callback evaluation error: %s", context.event_code, SvPV_nolen (ERRSV)); |
655 | LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
635 | |
656 | |
636 | context.returnvalue = count > 0 ? POPi : 0; |
657 | rv = count > 0 ? POPi : 0; |
637 | |
658 | |
638 | PUTBACK; |
659 | PUTBACK; |
639 | FREETMPS; |
660 | FREETMPS; |
640 | LEAVE; |
661 | LEAVE; |
641 | } |
662 | } |
642 | |
663 | |
643 | rv = context.returnvalue; |
|
|
644 | return &rv; |
664 | return &rv; |
645 | } |
665 | } |
646 | |
666 | |
647 | int |
667 | int |
648 | closePlugin () |
668 | closePlugin () |
… | |
… | |
1503 | { |
1523 | { |
1504 | int unused_type; |
1524 | int unused_type; |
1505 | RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); |
1525 | RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); |
1506 | } |
1526 | } |
1507 | |
1527 | |
|
|
1528 | # syntatic sugar for easier use in event callbacks. |
|
|
1529 | const char *options (object *op) |
|
|
1530 | CODE: |
|
|
1531 | RETVAL = op->name; |
|
|
1532 | OUTPUT: |
|
|
1533 | RETVAL |
|
|
1534 | |
1508 | const char *get_ob_key_value (object *op, const char *key) |
1535 | const char *get_ob_key_value (object *op, const char *key) |
1509 | |
1536 | |
1510 | bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1) |
1537 | bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1) |
1511 | |
1538 | |
1512 | object *get_nearest_player (object *ob) |
1539 | object *get_nearest_player (object *ob) |