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

Comparing deliantra/server/plugins/cfperl/cfperl.xs (file contents):
Revision 1.40 by root, Sun Mar 26 15:52:03 2006 UTC vs.
Revision 1.44 by root, Fri Mar 31 20:21:53 2006 UTC

65typedef struct 65typedef struct
66{ 66{
67 object* who; 67 object* who;
68 object* activator; 68 object* activator;
69 object* third; 69 object* third;
70 object* event;
70 mapstruct* map; 71 mapstruct* map;
71 char message[1024]; 72 char message[1024];
72 int fix; // seems to be python-only, and should not be part of the API 73 int fix; // seems to be python-only, and should not be part of the API
73 int event_code; 74 int event_code;
74 char extension[1024]; // name field, should invoke specific perl extension 75 char extension[1024]; // name field, should invoke specific perl extension
76 int returnvalue; 77 int returnvalue;
77} CFPContext; 78} CFPContext;
78 79
79static HV *obj_cache; 80static HV *obj_cache;
80static PerlInterpreter *perl; 81static PerlInterpreter *perl;
82
83#define PUSHcfapi(type,ctype) PUSHs (sv_2mortal (newSVcfapi ((type), va_arg (args, ctype))))
84#define PUSH_OB PUSHcfapi(CFAPI_POBJECT, object *)
85#define PUSH_PL PUSHcfapi(CFAPI_PPLAYER, player *)
86#define PUSH_MAP PUSHcfapi(CFAPI_PMAP, mapstruct *)
87#define PUSH_PV PUSHcfapi(CFAPI_STRING, const char *)
88#define PUSH_IV PUSHcfapi(CFAPI_INT, int)
81 89
82////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 90//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83 91
84// garbage collect some perl objects, if possible 92// garbage collect some perl objects, if possible
85// all objects no longer referenced and empty are 93// all objects no longer referenced and empty are
296 ENTER; 304 ENTER;
297 SAVETMPS; 305 SAVETMPS;
298 306
299 PUSHMARK (SP); 307 PUSHMARK (SP);
300 308
309 EXTEND (SP, 10);
310
301 HV *hv = newHV (); 311 HV *hv = newHV ();
302#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0) 312#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0)
303 hv_context (CFAPI_POBJECT, ,who); 313 hv_context (CFAPI_POBJECT, ,who);
304 hv_context (CFAPI_POBJECT, ,activator); 314 hv_context (CFAPI_POBJECT, ,activator);
305 hv_context (CFAPI_POBJECT, ,third); 315 hv_context (CFAPI_POBJECT, ,third);
316 hv_context (CFAPI_POBJECT, ,event);
306 hv_context (CFAPI_PMAP, ,map); 317 hv_context (CFAPI_PMAP, ,map);
307 hv_context (CFAPI_STRING , ,message); 318 hv_context (CFAPI_STRING , ,message);
308 hv_context (CFAPI_INT ,&,fix); 319 hv_context (CFAPI_INT ,&,fix);
309 hv_context (CFAPI_INT ,&,event_code); 320 hv_context (CFAPI_INT ,&,event_code);
310 hv_context (CFAPI_STRING , ,options); 321 hv_context (CFAPI_STRING , ,options);
311 hv_context (CFAPI_STRING , ,extension); 322 hv_context (CFAPI_STRING , ,extension);
312 323
313 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 324 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
314 325
315 PUTBACK; 326 PUTBACK;
316 int count = call_pv (func, G_SCALAR | G_EVAL); 327 int count = call_pv (func, G_SCALAR | G_EVAL);
317 SPAGAIN; 328 SPAGAIN;
318 329
494 505
495void * 506void *
496globalEventListener (int *type, ...) 507globalEventListener (int *type, ...)
497{ 508{
498 va_list args; 509 va_list args;
499 static int rv = 0; 510 static int rv;
500 CFPContext context; 511 int event_code;
501 char *buf;
502 player *pl;
503 object *op;
504 512
505 if (!perl) 513 if (!perl)
506 return; 514 return;
507 515
508 memset (&context, 0, sizeof (context));
509
510 va_start (args, type); 516 va_start (args, type);
511 context.event_code = va_arg (args, int); 517 event_code = va_arg (args, int);
512 518
513 switch (context.event_code) 519 if (event_code == EVENT_FREE_OB)
514 { 520 {
515 case EVENT_CRASH: 521 player *pl;
516 printf ("Unimplemented for now\n"); 522 object *op;
517 break; 523 SV *sv;
518 524
519 case EVENT_PLAYER_LOAD:
520 case EVENT_PLAYER_SAVE:
521 context.who = va_arg (args, object *); 525 op = va_arg (args, object *);
522 buf = va_arg (args, char *);
523 if (buf != 0)
524 strncpy (context.message, buf, sizeof (context.message));
525 break;
526
527 case EVENT_MAPLOAD:
528 case EVENT_MAPOUT:
529 case EVENT_MAPIN:
530 case EVENT_MAPCLEAN:
531 context.map = va_arg (args, mapstruct *);
532 break;
533
534 case EVENT_MAPENTER:
535 case EVENT_MAPLEAVE:
536 case EVENT_FREE_OB:
537 case EVENT_BORN:
538 case EVENT_REMOVE:
539 context.activator = va_arg (args, object *);
540 break;
541
542 case EVENT_PLAYER_DEATH:
543 context.who = va_arg (args, object *);
544 break;
545
546 case EVENT_GKILL:
547 context.who = va_arg (args, object *);
548 context.activator = va_arg (args, object *);
549 break;
550
551 case EVENT_LOGIN:
552 case EVENT_LOGOUT:
553 pl = va_arg (args, player *);
554 context.activator = pl->ob;
555 buf = va_arg (args, char *);
556 if (buf != 0)
557 strncpy (context.message, buf, sizeof (context.message));
558 break;
559
560 case EVENT_SHOUT:
561 case EVENT_MUZZLE:
562 case EVENT_KICK:
563 context.activator = va_arg (args, object *);
564 buf = va_arg (args, char *);
565 if (buf != 0)
566 strncpy (context.message, buf, sizeof (context.message));
567 break;
568
569 case EVENT_CLOCK:
570 clean_obj_cache ();
571 break;
572
573 case EVENT_TELL:
574 break;
575
576 case EVENT_MAPRESET:
577 /* stupid, should be the map itself, not "message"??? */
578 buf = va_arg (args, char *);
579 if (buf != 0)
580 strncpy (context.message, buf, sizeof (context.message));
581 break;
582 }
583
584 va_end (args);
585
586 if (context.event_code == EVENT_FREE_OB)
587 {
588 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (void *), 0); 526 sv = hv_delete (obj_cache, (char *)&op, sizeof (void *), 0);
589 527
590 if (sv) 528 if (sv)
591 clearSVptr (sv); 529 clearSVptr (sv);
530
531 rv = 0;
592 } 532 }
593 else 533 else
594 inject_event ("cf::inject_global_event", &context); 534 {
595 535 dSP;
596 rv = context.returnvalue; 536
537 ENTER;
538 SAVETMPS;
539
540 PUSHMARK (SP);
541
542 EXTEND (SP, 10);
543 PUSHs (sv_2mortal (newSViv (event_code)));
544
545 switch (event_code)
546 {
547 case EVENT_CRASH:
548 break;
549
550 case EVENT_PLAYER_LOAD:
551 case EVENT_PLAYER_SAVE:
552 PUSH_OB;
553 PUSH_PV;
554 break;
555
556 case EVENT_MAPLOAD:
557 case EVENT_MAPOUT:
558 case EVENT_MAPIN:
559 case EVENT_MAPCLEAN:
560 case EVENT_MAPRESET:
561 PUSH_MAP;
562 break;
563
564 case EVENT_MAPENTER:
565 case EVENT_MAPLEAVE:
566 case EVENT_BORN:
567 case EVENT_REMOVE:
568 case EVENT_PLAYER_DEATH:
569 PUSH_OB;
570 break;
571
572 case EVENT_GKILL:
573 PUSH_OB;
574 PUSH_OB;
575 break;
576
577 case EVENT_LOGIN:
578 case EVENT_LOGOUT:
579 PUSH_PL;
580 PUSH_PV;
581 break;
582
583 case EVENT_SHOUT:
584 case EVENT_MUZZLE:
585 case EVENT_KICK:
586 PUSH_OB;
587 PUSH_PV;
588 break;
589
590 case EVENT_CLOCK:
591 clean_obj_cache ();
592 break;
593
594 case EVENT_TELL:
595 break;
596 }
597
598 va_end (args);
599
600 PUTBACK;
601 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL);
602 SPAGAIN;
603
604 if (SvTRUE (ERRSV))
605 LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
606
607 rv = count > 0 ? POPi : 0;
608
609 PUTBACK;
610 FREETMPS;
611 LEAVE;
612 }
597 613
598 return &rv; 614 return &rv;
599} 615}
600 616
601void * 617void *
623 strncpy (context.message, buf, sizeof (context.message)); 639 strncpy (context.message, buf, sizeof (context.message));
624 640
625 context.fix = va_arg (args, int); 641 context.fix = va_arg (args, int);
626 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension)); 642 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
627 strncpy (context.options, va_arg (args, char *), sizeof (context.options)); 643 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
644 context.event = va_arg (args, object *);
645 va_end (args);
646
628 context.returnvalue = 0; 647 context.returnvalue = 0;
629 va_end (args);
630 648
631 inject_event ("cf::inject_event", &context); 649 inject_event ("cf::inject_event", &context);
632 650
633 rv = context.returnvalue; 651 rv = context.returnvalue;
634 return &rv; 652 return &rv;
1618 player *pl; 1636 player *pl;
1619 for (pl = first_player; pl; pl = pl->next) 1637 for (pl = first_player; pl; pl = pl->next)
1620 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl)); 1638 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1621} 1639}
1622 1640
1641bool
1642peaceful (player *pl, bool new_setting = 0)
1643 PROTOTYPE: $;$
1644 CODE:
1645 RETVAL = pl->peaceful;
1646 if (items > 1)
1647 pl->peaceful = new_setting;
1648 OUTPUT:
1649 RETVAL
1650
1623living * 1651living *
1624orig_stats (player *pl) 1652orig_stats (player *pl)
1625 CODE: 1653 CODE:
1626 RETVAL = &pl->orig_stats; 1654 RETVAL = &pl->orig_stats;
1627 OUTPUT: RETVAL 1655 OUTPUT: RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines