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.12 by root, Tue Feb 7 03:01:29 2006 UTC vs.
Revision 1.16 by root, Wed Feb 8 04:32:19 2006 UTC

28#include <perl.h> 28#include <perl.h>
29#include <XSUB.h> 29#include <XSUB.h>
30 30
31#undef save_long // clashes with libproto.h 31#undef save_long // clashes with libproto.h
32 32
33#define PLUGIN_NAME "cfperl" 33#define PLUGIN_NAME "perl"
34#define PLUGIN_VERSION "cfperl 0.0" 34#define PLUGIN_VERSION "cfperl 0.0"
35 35
36#ifndef __CEXTRACT__ 36#ifndef __CEXTRACT__
37#include <plugin.h> 37#include <plugin.h>
38#endif 38#endif
62 object* activator; 62 object* activator;
63 object* third; 63 object* third;
64 char message[1024]; 64 char message[1024];
65 int fix; 65 int fix;
66 int event_code; 66 int event_code;
67 char options[1024]; 67 char extension[1024]; // name field, should invoke specific perl extension
68 char options[1024]; // slaying field of event_connectors
68 int returnvalue; 69 int returnvalue;
69} CFPContext; 70} CFPContext;
70 71
71//static int current_command = -999; 72//static int current_command = -999;
72 73
74static HV *obj_cache;
73static PerlInterpreter *perl; 75static PerlInterpreter *perl;
74 76
75////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 77//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78
79// garbage collect some perl objects, if possible
80// all objects no longer referenced and empty are
81// eligible for destruction.
82void
83clean_obj_cache ()
84{
85 return; // killing neko-san gives me an unreferenced scalar
86 int todo = 10;
87 do
88 {
89 I32 klen;
90 char *key;
91 HE *he = hv_iternext (obj_cache);
92
93 if (he)
94 {
95 SV *sv = hv_iterval (obj_cache, he);
96
97 // emopty and unreferened? nuke it
98 if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv))))
99 {
100 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
101 todo++;
102 }
103 }
104 else
105 break;
106 }
107 while (--todo);
108}
76 109
77static SV * 110static SV *
78newSVptr (void *ptr, const char *klass) 111newSVptr (void *ptr, const char *klass)
79{ 112{
80 if (!ptr) 113 if (!ptr)
81 return &PL_sv_undef; 114 return &PL_sv_undef;
82 115
83 HV *hv = newHV (); 116 HV *hv = newHV ();
84 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 117 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
85 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 118 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
119}
120
121static SV *
122newSVptr_cached (void *ptr, const char *klass)
123{
124 SV *sv, **he;
125
126 if (!ptr)
127 return &PL_sv_undef;
128
129 he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0);
130
131 if (he)
132 sv = *he;
133 else
134 {
135 sv = newSVptr (ptr, klass);
136 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
137 }
138
139 return newSVsv (sv);
86} 140}
87 141
88static void 142static void
89clearSVptr (SV *sv) 143clearSVptr (SV *sv)
90{ 144{
146 sv = &PL_sv_undef; 200 sv = &PL_sv_undef;
147 else 201 else
148 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 202 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE))
149 { 203 {
150 case MAP: 204 case MAP:
151 sv = newSVptr (obj, "cf::object::map"); 205 sv = newSVptr_cached (obj, "cf::object::map");
152 break; 206 break;
153 207
154 case PLAYER: 208 case PLAYER:
155 sv = newSVptr (obj, "cf::object::player"); 209 sv = newSVptr_cached (obj, "cf::object::player");
156 break; 210 break;
157 211
158 default: 212 default:
159 sv = newSVptr (obj, "cf::object"); 213 sv = newSVptr_cached (obj, "cf::object");
160 break; 214 break;
161 } 215 }
162 } 216 }
163 break; 217 break;
164 218
192} 246}
193 247
194///////////////////////////////////////////////////////////////////////////// 248/////////////////////////////////////////////////////////////////////////////
195 249
196void 250void
197inject_event (CFPContext *context) 251inject_event (const char *func, CFPContext *context)
198{ 252{
199 dSP; 253 dSP;
200 254
201 ENTER; 255 ENTER;
202 SAVETMPS; 256 SAVETMPS;
203 257
204 PUSHMARK (SP); 258 PUSHMARK (SP);
205
206 EXTEND (SP, 2);
207 //PUSHs (sv_2mortal (newSViv (type)));
208 259
209 HV *hv = newHV (); 260 HV *hv = newHV ();
210#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0) 261#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0)
211 hv_context (CFAPI_POBJECT, ,who); 262 hv_context (CFAPI_POBJECT, ,who);
212 hv_context (CFAPI_POBJECT, ,activator); 263 hv_context (CFAPI_POBJECT, ,activator);
213 hv_context (CFAPI_POBJECT, ,third); 264 hv_context (CFAPI_POBJECT, ,third);
214 hv_context (CFAPI_STRING , ,message); 265 hv_context (CFAPI_STRING , ,message);
215 hv_context (CFAPI_INT ,&,fix); 266 hv_context (CFAPI_INT ,&,fix);
216 hv_context (CFAPI_INT ,&,event_code); 267 hv_context (CFAPI_INT ,&,event_code);
217 hv_context (CFAPI_STRING , ,options); 268 hv_context (CFAPI_STRING , ,options);
269 hv_context (CFAPI_STRING , ,extension);
218 270
219 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 271 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
220 272
221 PUTBACK; 273 PUTBACK;
222 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); 274 int count = call_pv (func, G_SCALAR | G_EVAL);
223 SPAGAIN; 275 SPAGAIN;
224 276
225 if (SvTRUE (ERRSV)) 277 if (SvTRUE (ERRSV))
226 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV)); 278 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
227 279
357 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener); 409 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
358 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); 410 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
359 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); 411 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener);
360 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); 412 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
361 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); 413 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener);
414 registerGlobalEvent (NULL, EVENT_FREE_OB, PLUGIN_NAME, globalEventListener);
362 415
363 char *argv[] = { 416 char *argv[] = {
364 "", 417 "",
365 "-e" 418 "-e"
366 "BEGIN {" 419 "BEGIN {"
380 433
381 perl_destruct (perl); 434 perl_destruct (perl);
382 perl_free (perl); 435 perl_free (perl);
383 perl = 0; 436 perl = 0;
384 } 437 }
438 else
439 {
440 obj_cache = newHV ();
441 }
385 442
386 return 0; 443 return 0;
387} 444}
388 445
389void * 446void *
408 { 465 {
409 case EVENT_CRASH: 466 case EVENT_CRASH:
410 printf ("Unimplemented for now\n"); 467 printf ("Unimplemented for now\n");
411 break; 468 break;
412 469
470 case EVENT_MAPENTER:
471 case EVENT_MAPLEAVE:
472 case EVENT_FREE_OB:
413 case EVENT_BORN: 473 case EVENT_BORN:
474 case EVENT_REMOVE:
414 context.activator = va_arg (args, object *); 475 context.activator = va_arg (args, object *);
415 break; 476 break;
416 477
417 case EVENT_PLAYER_DEATH: 478 case EVENT_PLAYER_DEATH:
418 context.who = va_arg (args, object *); 479 context.who = va_arg (args, object *);
422 context.who = va_arg (args, object *); 483 context.who = va_arg (args, object *);
423 context.activator = va_arg (args, object *); 484 context.activator = va_arg (args, object *);
424 break; 485 break;
425 486
426 case EVENT_LOGIN: 487 case EVENT_LOGIN:
427 pl = va_arg (args, player *);
428 context.activator = pl->ob;
429 buf = va_arg (args, char *);
430 if (buf != 0)
431 strcpy (context.message, buf);
432 break;
433
434 case EVENT_LOGOUT: 488 case EVENT_LOGOUT:
435 pl = va_arg (args, player *); 489 pl = va_arg (args, player *);
436 context.activator = pl->ob; 490 context.activator = pl->ob;
437 buf = va_arg (args, char *); 491 buf = va_arg (args, char *);
438 if (buf != 0) 492 if (buf != 0)
439 strcpy (context.message, buf); 493 strncpy (context.message, buf, sizeof (context.message));
440 break;
441
442 case EVENT_REMOVE:
443 context.activator = va_arg (args, object *);
444 break; 494 break;
445 495
446 case EVENT_SHOUT: 496 case EVENT_SHOUT:
447 context.activator = va_arg (args, object *);
448 buf = va_arg (args, char *);
449 if (buf != 0)
450 strcpy (context.message, buf);
451 break;
452
453 case EVENT_MUZZLE: 497 case EVENT_MUZZLE:
454 context.activator = va_arg (args, object *);
455 buf = va_arg (args, char *);
456 if (buf != 0)
457 strcpy (context.message, buf);
458 break;
459
460 case EVENT_KICK: 498 case EVENT_KICK:
461 context.activator = va_arg (args, object *); 499 context.activator = va_arg (args, object *);
462 buf = va_arg (args, char *); 500 buf = va_arg (args, char *);
463 if (buf != 0) 501 if (buf != 0)
464 strcpy (context.message, buf); 502 strncpy (context.message, buf, sizeof (context.message));
465 break;
466
467 case EVENT_MAPENTER:
468 context.activator = va_arg (args, object *);
469 break;
470
471 case EVENT_MAPLEAVE:
472 context.activator = va_arg (args, object *);
473 break; 503 break;
474 504
475 case EVENT_CLOCK: 505 case EVENT_CLOCK:
506 clean_obj_cache ();
507 break;
508
509 case EVENT_TELL:
476 break; 510 break;
477 511
478 case EVENT_MAPRESET: 512 case EVENT_MAPRESET:
479 buf = va_arg (args, char *); 513 buf = va_arg (args, char *);
480 if (buf != 0) 514 if (buf != 0)
481 strcpy (context.message, buf); 515 strncpy (context.message, buf, sizeof (context.message));
482 break;
483
484 case EVENT_TELL:
485 break; 516 break;
486 } 517 }
487 518
488 va_end (args); 519 va_end (args);
489 520
490 inject_event (&context); 521 if (context.event_code == EVENT_FREE_OB)
522 {
523 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (object *), 0);
524
525 if (sv)
526 {
527 clearSVptr (sv);
528 SvREFCNT_dec (sv);
529 }
530 }
531 else
532 inject_event ("cf::inject_global_event", &context);
491 533
492 rv = context.returnvalue; 534 rv = context.returnvalue;
493 535
494 return &rv; 536 return &rv;
495} 537}
511 553
512 context.who = va_arg (args, object *); 554 context.who = va_arg (args, object *);
513 context.event_code = va_arg (args, int); 555 context.event_code = va_arg (args, int);
514 context.activator = va_arg (args, object *); 556 context.activator = va_arg (args, object *);
515 context.third = va_arg (args, object *); 557 context.third = va_arg (args, object *);
558
516 buf = va_arg (args, char *); 559 buf = va_arg (args, char *);
517
518 if (buf != 0) 560 if (buf != 0)
519 strcpy (context.message, buf); 561 strncpy (context.message, buf, sizeof (context.message));
520 562
521 context.fix = va_arg (args, int); 563 context.fix = va_arg (args, int);
564 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
522 strcpy (context.options, va_arg (args, char *)); 565 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
523 context.returnvalue = 0; 566 context.returnvalue = 0;
524 va_end (args); 567 va_end (args);
525 568
526 inject_event (&context); 569 inject_event ("cf::inject_event", &context);
527 570
528 rv = context.returnvalue; 571 rv = context.returnvalue;
529 return &rv; 572 return &rv;
530} 573}
531 574
846 const_iv (P_NO_CLERIC) 889 const_iv (P_NO_CLERIC)
847 const_iv (P_NEED_UPDATE) 890 const_iv (P_NEED_UPDATE)
848 const_iv (P_NO_ERROR) 891 const_iv (P_NO_ERROR)
849 const_iv (P_OUT_OF_MAP) 892 const_iv (P_OUT_OF_MAP)
850 const_iv (P_NEW_MAP) 893 const_iv (P_NEW_MAP)
894
895 const_iv (UP_OBJ_INSERT)
896 const_iv (UP_OBJ_REMOVE)
897 const_iv (UP_OBJ_CHANGE)
898 const_iv (UP_OBJ_FACE)
899
900 const_iv (INS_NO_MERGE)
901 const_iv (INS_ABOVE_FLOOR_ONLY)
902 const_iv (INS_NO_WALK_ON)
903 const_iv (INS_ON_TOP)
904 const_iv (INS_BELOW_ORIGINATOR)
905 const_iv (INS_MAP_LOAD)
906
907 const_iv (WILL_APPLY_HANDLE)
908 const_iv (WILL_APPLY_TREASURE)
909 const_iv (WILL_APPLY_EARTHWALL)
910 const_iv (WILL_APPLY_DOOR)
911 const_iv (WILL_APPLY_FOOD)
851 }; 912 };
852 913
853 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 914 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
854 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 915 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
855 916
869 const_event (TIME) 930 const_event (TIME)
870 const_event (THROW) 931 const_event (THROW)
871 const_event (TRIGGER) 932 const_event (TRIGGER)
872 const_event (CLOSE) 933 const_event (CLOSE)
873 const_event (TIMER) 934 const_event (TIMER)
935 const_event (MOVE)
874 936
875 const_event (BORN) 937 const_event (BORN)
876 const_event (CLOCK) 938 const_event (CLOCK)
877 const_event (CRASH) 939 const_event (CRASH)
878 const_event (PLAYER_DEATH) 940 const_event (PLAYER_DEATH)
885 const_event (REMOVE) 947 const_event (REMOVE)
886 const_event (SHOUT) 948 const_event (SHOUT)
887 const_event (TELL) 949 const_event (TELL)
888 const_event (MUZZLE) 950 const_event (MUZZLE)
889 const_event (KICK) 951 const_event (KICK)
952 //const_event (FREE_OB)
890 }; 953 };
891 954
892 AV *av = get_av ("cf::EVENT", 1); 955 AV *av = get_av ("cf::EVENT", 1);
893 956
894 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) 957 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; )
1094 break; 1157 break;
1095 default: 1158 default:
1096 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1159 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1097 } 1160 }
1098 1161
1099SV * 1162void
1100inv (object *obj) 1163inv (object *obj)
1101 PROTOTYPE: $ 1164 PROTOTYPE: $
1102 PPCODE: 1165 PPCODE:
1103{ 1166{
1104 object *o; 1167 object *o;
1105 for (o = obj->inv; o; o = o->below) 1168 for (o = obj->inv; o; o = o->below)
1106 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1169 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1107} 1170}
1108 1171
1109int cf_object_get_resistance (object *op, int rtype) 1172int cf_object_get_resistance (object *op, int rtype)
1110 ALIAS: resistance = 0 1173 ALIAS: resistance = 0
1111 1174
1112int cf_object_get_flag (object *op, int flag) 1175int cf_object_get_flag (object *op, int flag)
1113 ALIAS: flag = 0 1176 ALIAS: flag = 0
1114 1177
1115void cf_object_set_flag (object *op, int flag, int value) 1178void cf_object_set_flag (object *op, int flag, int value)
1116 1179
1117void cf_object_move (object *op, object *originator, int dir) 1180void cf_object_move (object *op, int dir, object *originator)
1118 1181
1119void cf_object_apply (object *op, object *author, int flags) 1182void cf_object_apply (object *op, object *author, int flags = 0)
1120 1183
1121void cf_object_apply_below (object *op) 1184void cf_object_apply_below (object *op)
1122 1185
1123void cf_object_remove (object *op) 1186void cf_object_remove (object *op)
1124 1187
1205void cf_fix_object (object *pl) 1268void cf_fix_object (object *pl)
1206 ALIAS: fix = 0 1269 ALIAS: fix = 0
1207 1270
1208object *cf_insert_ob_in_ob (object *ob, object *where) 1271object *cf_insert_ob_in_ob (object *ob, object *where)
1209 1272
1273object *get_nearest_player (object *ob)
1274 ALIAS: nearest_player = 0
1275
1276void rangevector (object *ob, object *other, int flags = 0)
1277 PROTOTYPE: $$;$
1278 PPCODE:
1279{
1280 rv_vector rv;
1281 get_rangevector (ob, other, &rv, flags);
1282 EXTEND (SP, 5);
1283 PUSHs (newSVuv (rv.distance));
1284 PUSHs (newSViv (rv.distance_x));
1285 PUSHs (newSViv (rv.distance_y));
1286 PUSHs (newSViv (rv.direction));
1287 PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
1288}
1289
1290bool on_same_map_as (object *ob, object *other)
1291 CODE:
1292 RETVAL = on_same_map (ob, other);
1293 OUTPUT: RETVAL
1294
1210 1295
1211MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1296MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1212 1297
1213player *player (object *op) 1298player *player (object *op)
1214 CODE: 1299 CODE:
1245# nonstandard 1330# nonstandard
1246object *ob (player *pl) 1331object *ob (player *pl)
1247 CODE: 1332 CODE:
1248 RETVAL = pl->ob; 1333 RETVAL = pl->ob;
1249 OUTPUT: RETVAL 1334 OUTPUT: RETVAL
1335
1336player *first ()
1337 CODE:
1338 RETVAL = first_player;
1339 OUTPUT: RETVAL
1340
1341player *next (player *pl)
1342 CODE:
1343 RETVAL = pl->next;
1344 OUTPUT: RETVAL
1345
1346void
1347list ()
1348 PPCODE:
1349{
1350 player *pl;
1351 for (pl = first_player; pl; pl = pl->next)
1352 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1353}
1250 1354
1251 1355
1252MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 1356MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
1253 1357
1254SV * 1358SV *
1294object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) 1398object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny)
1295 C_ARGS: str, map, nx, ny 1399 C_ARGS: str, map, nx, ny
1296 1400
1297#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny) 1401#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny)
1298 1402
1299SV * 1403void
1300at (mapstruct *obj, unsigned int x, unsigned int y) 1404at (mapstruct *obj, unsigned int x, unsigned int y)
1301 PROTOTYPE: $$$ 1405 PROTOTYPE: $$$
1302 INIT: 1406 INIT:
1303 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF; 1407 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_EMPTY;
1304 PPCODE: 1408 PPCODE:
1305{ 1409{
1306 object *o; 1410 object *o;
1307 for (o = GET_MAP_OB (obj, x, y); o; o = o->above) 1411 for (o = GET_MAP_OB (obj, x, y); o; o = o->above)
1308 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1412 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1309} 1413}
1310 1414
1311SV * 1415SV *
1312bot_at (mapstruct *obj, unsigned int x, unsigned int y) 1416bot_at (mapstruct *obj, unsigned int x, unsigned int y)
1313 PROTOTYPE: $$$ 1417 PROTOTYPE: $$$

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines