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.45 by root, Fri Mar 31 20:59:17 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
284 va_end (args); 292 va_end (args);
285 293
286 return sv; 294 return sv;
287} 295}
288 296
289/////////////////////////////////////////////////////////////////////////////
290
291void
292inject_event (const char *func, CFPContext *context)
293{
294 dSP;
295
296 ENTER;
297 SAVETMPS;
298
299 PUSHMARK (SP);
300
301 HV *hv = newHV ();
302#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);
304 hv_context (CFAPI_POBJECT, ,activator);
305 hv_context (CFAPI_POBJECT, ,third);
306 hv_context (CFAPI_PMAP, ,map);
307 hv_context (CFAPI_STRING , ,message);
308 hv_context (CFAPI_INT ,&,fix);
309 hv_context (CFAPI_INT ,&,event_code);
310 hv_context (CFAPI_STRING , ,options);
311 hv_context (CFAPI_STRING , ,extension);
312
313 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
314
315 PUTBACK;
316 int count = call_pv (func, G_SCALAR | G_EVAL);
317 SPAGAIN;
318
319 if (SvTRUE (ERRSV))
320 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
321
322 context->returnvalue = count > 0 ? POPi : 0;
323
324 PUTBACK;
325 FREETMPS;
326 LEAVE;
327}
328
329///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
330 298
331int 299int
332initPlugin (const char *iversion, f_plug_api gethooksptr) 300initPlugin (const char *iversion, f_plug_api gethooksptr)
333{ 301{
494 462
495void * 463void *
496globalEventListener (int *type, ...) 464globalEventListener (int *type, ...)
497{ 465{
498 va_list args; 466 va_list args;
499 static int rv = 0; 467 static int rv;
500 CFPContext context; 468 int event_code;
501 char *buf;
502 player *pl;
503 object *op;
504 469
505 if (!perl) 470 if (!perl)
506 return; 471 return;
507 472
508 memset (&context, 0, sizeof (context));
509
510 va_start (args, type); 473 va_start (args, type);
511 context.event_code = va_arg (args, int); 474 event_code = va_arg (args, int);
512 475
513 switch (context.event_code) 476 if (event_code == EVENT_FREE_OB)
514 { 477 {
515 case EVENT_CRASH: 478 player *pl;
516 printf ("Unimplemented for now\n"); 479 object *op;
517 break; 480 SV *sv;
518 481
519 case EVENT_PLAYER_LOAD:
520 case EVENT_PLAYER_SAVE:
521 context.who = va_arg (args, object *); 482 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); 483 sv = hv_delete (obj_cache, (char *)&op, sizeof (void *), 0);
589 484
590 if (sv) 485 if (sv)
591 clearSVptr (sv); 486 clearSVptr (sv);
487
488 rv = 0;
592 } 489 }
593 else 490 else
594 inject_event ("cf::inject_global_event", &context); 491 {
595 492 dSP;
596 rv = context.returnvalue; 493
494 ENTER;
495 SAVETMPS;
496
497 PUSHMARK (SP);
498
499 EXTEND (SP, 10);
500 PUSHs (sv_2mortal (newSViv (event_code)));
501
502 switch (event_code)
503 {
504 case EVENT_CRASH:
505 break;
506
507 case EVENT_PLAYER_LOAD:
508 case EVENT_PLAYER_SAVE:
509 PUSH_OB;
510 PUSH_PV;
511 break;
512
513 case EVENT_MAPLOAD:
514 case EVENT_MAPOUT:
515 case EVENT_MAPIN:
516 case EVENT_MAPCLEAN:
517 case EVENT_MAPRESET:
518 PUSH_MAP;
519 break;
520
521 case EVENT_MAPENTER:
522 case EVENT_MAPLEAVE:
523 case EVENT_BORN:
524 case EVENT_REMOVE:
525 case EVENT_PLAYER_DEATH:
526 PUSH_OB;
527 break;
528
529 case EVENT_GKILL:
530 PUSH_OB;
531 PUSH_OB;
532 break;
533
534 case EVENT_LOGIN:
535 case EVENT_LOGOUT:
536 PUSH_PL;
537 PUSH_PV;
538 break;
539
540 case EVENT_SHOUT:
541 case EVENT_MUZZLE:
542 case EVENT_KICK:
543 PUSH_OB;
544 PUSH_PV;
545 break;
546
547 case EVENT_CLOCK:
548 clean_obj_cache ();
549 break;
550
551 case EVENT_TELL:
552 break;
553 }
554
555 va_end (args);
556
557 PUTBACK;
558 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL);
559 SPAGAIN;
560
561 if (SvTRUE (ERRSV))
562 LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
563
564 rv = count > 0 ? POPi : 0;
565
566 PUTBACK;
567 FREETMPS;
568 LEAVE;
569 }
597 570
598 return &rv; 571 return &rv;
599} 572}
600 573
601void * 574void *
623 strncpy (context.message, buf, sizeof (context.message)); 596 strncpy (context.message, buf, sizeof (context.message));
624 597
625 context.fix = va_arg (args, int); 598 context.fix = va_arg (args, int);
626 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension)); 599 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
627 strncpy (context.options, va_arg (args, char *), sizeof (context.options)); 600 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
628 context.returnvalue = 0; 601 context.event = va_arg (args, object *);
629 va_end (args); 602 va_end (args);
630 603
631 inject_event ("cf::inject_event", &context); 604 {
605 dSP;
606
607 ENTER;
608 SAVETMPS;
609
610 PUSHMARK (SP);
611
612 EXTEND (SP, 10);
613
614 HV *hv = newHV ();
615#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context.expr), 0)
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
627 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
628
629 PUTBACK;
630 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL);
631 SPAGAIN;
632
633 if (SvTRUE (ERRSV))
634 LOG (llevError, "event '%d' callback evaluation error: %s", context.event_code, SvPV_nolen (ERRSV));
635
636 context.returnvalue = count > 0 ? POPi : 0;
637
638 PUTBACK;
639 FREETMPS;
640 LEAVE;
641 }
632 642
633 rv = context.returnvalue; 643 rv = context.returnvalue;
634 return &rv; 644 return &rv;
635} 645}
636 646
1618 player *pl; 1628 player *pl;
1619 for (pl = first_player; pl; pl = pl->next) 1629 for (pl = first_player; pl; pl = pl->next)
1620 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl)); 1630 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1621} 1631}
1622 1632
1633bool
1634peaceful (player *pl, bool new_setting = 0)
1635 PROTOTYPE: $;$
1636 CODE:
1637 RETVAL = pl->peaceful;
1638 if (items > 1)
1639 pl->peaceful = new_setting;
1640 OUTPUT:
1641 RETVAL
1642
1623living * 1643living *
1624orig_stats (player *pl) 1644orig_stats (player *pl)
1625 CODE: 1645 CODE:
1626 RETVAL = &pl->orig_stats; 1646 RETVAL = &pl->orig_stats;
1627 OUTPUT: RETVAL 1647 OUTPUT: RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines