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.17 by root, Wed Feb 8 05:22:41 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; //D // 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 return sv; //D
136 sv = newSVptr (ptr, klass);
137 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
138 }
139
140 return newSVsv (sv);
86} 141}
87 142
88static void 143static void
89clearSVptr (SV *sv) 144clearSVptr (SV *sv)
90{ 145{
146 sv = &PL_sv_undef; 201 sv = &PL_sv_undef;
147 else 202 else
148 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 203 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE))
149 { 204 {
150 case MAP: 205 case MAP:
151 sv = newSVptr (obj, "cf::object::map"); 206 sv = newSVptr_cached (obj, "cf::object::map");
152 break; 207 break;
153 208
154 case PLAYER: 209 case PLAYER:
155 sv = newSVptr (obj, "cf::object::player"); 210 sv = newSVptr_cached (obj, "cf::object::player");
156 break; 211 break;
157 212
158 default: 213 default:
159 sv = newSVptr (obj, "cf::object"); 214 sv = newSVptr_cached (obj, "cf::object");
160 break; 215 break;
161 } 216 }
162 } 217 }
163 break; 218 break;
164 219
192} 247}
193 248
194///////////////////////////////////////////////////////////////////////////// 249/////////////////////////////////////////////////////////////////////////////
195 250
196void 251void
197inject_event (CFPContext *context) 252inject_event (const char *func, CFPContext *context)
198{ 253{
199 dSP; 254 dSP;
200 255
201 ENTER; 256 ENTER;
202 SAVETMPS; 257 SAVETMPS;
203 258
204 PUSHMARK (SP); 259 PUSHMARK (SP);
205
206 EXTEND (SP, 2);
207 //PUSHs (sv_2mortal (newSViv (type)));
208 260
209 HV *hv = newHV (); 261 HV *hv = newHV ();
210#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0) 262#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); 263 hv_context (CFAPI_POBJECT, ,who);
212 hv_context (CFAPI_POBJECT, ,activator); 264 hv_context (CFAPI_POBJECT, ,activator);
213 hv_context (CFAPI_POBJECT, ,third); 265 hv_context (CFAPI_POBJECT, ,third);
214 hv_context (CFAPI_STRING , ,message); 266 hv_context (CFAPI_STRING , ,message);
215 hv_context (CFAPI_INT ,&,fix); 267 hv_context (CFAPI_INT ,&,fix);
216 hv_context (CFAPI_INT ,&,event_code); 268 hv_context (CFAPI_INT ,&,event_code);
217 hv_context (CFAPI_STRING , ,options); 269 hv_context (CFAPI_STRING , ,options);
270 hv_context (CFAPI_STRING , ,extension);
218 271
219 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 272 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
220 273
221 PUTBACK; 274 PUTBACK;
222 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); 275 int count = call_pv (func, G_SCALAR | G_EVAL);
223 SPAGAIN; 276 SPAGAIN;
224 277
225 if (SvTRUE (ERRSV)) 278 if (SvTRUE (ERRSV))
226 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV)); 279 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
227 280
357 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener); 410 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
358 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); 411 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
359 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); 412 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener);
360 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); 413 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
361 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); 414 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener);
415 registerGlobalEvent (NULL, EVENT_FREE_OB, PLUGIN_NAME, globalEventListener);
362 416
363 char *argv[] = { 417 char *argv[] = {
364 "", 418 "",
365 "-e" 419 "-e"
366 "BEGIN {" 420 "BEGIN {"
380 434
381 perl_destruct (perl); 435 perl_destruct (perl);
382 perl_free (perl); 436 perl_free (perl);
383 perl = 0; 437 perl = 0;
384 } 438 }
439 else
440 {
441 obj_cache = newHV ();
442 }
385 443
386 return 0; 444 return 0;
387} 445}
388 446
389void * 447void *
408 { 466 {
409 case EVENT_CRASH: 467 case EVENT_CRASH:
410 printf ("Unimplemented for now\n"); 468 printf ("Unimplemented for now\n");
411 break; 469 break;
412 470
471 case EVENT_MAPENTER:
472 case EVENT_MAPLEAVE:
473 case EVENT_FREE_OB:
413 case EVENT_BORN: 474 case EVENT_BORN:
475 case EVENT_REMOVE:
414 context.activator = va_arg (args, object *); 476 context.activator = va_arg (args, object *);
415 break; 477 break;
416 478
417 case EVENT_PLAYER_DEATH: 479 case EVENT_PLAYER_DEATH:
418 context.who = va_arg (args, object *); 480 context.who = va_arg (args, object *);
422 context.who = va_arg (args, object *); 484 context.who = va_arg (args, object *);
423 context.activator = va_arg (args, object *); 485 context.activator = va_arg (args, object *);
424 break; 486 break;
425 487
426 case EVENT_LOGIN: 488 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: 489 case EVENT_LOGOUT:
435 pl = va_arg (args, player *); 490 pl = va_arg (args, player *);
436 context.activator = pl->ob; 491 context.activator = pl->ob;
437 buf = va_arg (args, char *); 492 buf = va_arg (args, char *);
438 if (buf != 0) 493 if (buf != 0)
439 strcpy (context.message, buf); 494 strncpy (context.message, buf, sizeof (context.message));
440 break;
441
442 case EVENT_REMOVE:
443 context.activator = va_arg (args, object *);
444 break; 495 break;
445 496
446 case EVENT_SHOUT: 497 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: 498 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: 499 case EVENT_KICK:
461 context.activator = va_arg (args, object *); 500 context.activator = va_arg (args, object *);
462 buf = va_arg (args, char *); 501 buf = va_arg (args, char *);
463 if (buf != 0) 502 if (buf != 0)
464 strcpy (context.message, buf); 503 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; 504 break;
474 505
475 case EVENT_CLOCK: 506 case EVENT_CLOCK:
507 clean_obj_cache ();
508 break;
509
510 case EVENT_TELL:
476 break; 511 break;
477 512
478 case EVENT_MAPRESET: 513 case EVENT_MAPRESET:
479 buf = va_arg (args, char *); 514 buf = va_arg (args, char *);
480 if (buf != 0) 515 if (buf != 0)
481 strcpy (context.message, buf); 516 strncpy (context.message, buf, sizeof (context.message));
482 break;
483
484 case EVENT_TELL:
485 break; 517 break;
486 } 518 }
487 519
488 va_end (args); 520 va_end (args);
489 521
490 inject_event (&context); 522 if (context.event_code == EVENT_FREE_OB)
523 {
524 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (object *), 0);
525
526 if (sv)
527 {
528 clearSVptr (sv);
529 SvREFCNT_dec (sv);
530 }
531 }
532 else
533 inject_event ("cf::inject_global_event", &context);
491 534
492 rv = context.returnvalue; 535 rv = context.returnvalue;
493 536
494 return &rv; 537 return &rv;
495} 538}
511 554
512 context.who = va_arg (args, object *); 555 context.who = va_arg (args, object *);
513 context.event_code = va_arg (args, int); 556 context.event_code = va_arg (args, int);
514 context.activator = va_arg (args, object *); 557 context.activator = va_arg (args, object *);
515 context.third = va_arg (args, object *); 558 context.third = va_arg (args, object *);
559
516 buf = va_arg (args, char *); 560 buf = va_arg (args, char *);
517
518 if (buf != 0) 561 if (buf != 0)
519 strcpy (context.message, buf); 562 strncpy (context.message, buf, sizeof (context.message));
520 563
521 context.fix = va_arg (args, int); 564 context.fix = va_arg (args, int);
565 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
522 strcpy (context.options, va_arg (args, char *)); 566 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
523 context.returnvalue = 0; 567 context.returnvalue = 0;
524 va_end (args); 568 va_end (args);
525 569
526 inject_event (&context); 570 inject_event ("cf::inject_event", &context);
527 571
528 rv = context.returnvalue; 572 rv = context.returnvalue;
529 return &rv; 573 return &rv;
530} 574}
531 575
846 const_iv (P_NO_CLERIC) 890 const_iv (P_NO_CLERIC)
847 const_iv (P_NEED_UPDATE) 891 const_iv (P_NEED_UPDATE)
848 const_iv (P_NO_ERROR) 892 const_iv (P_NO_ERROR)
849 const_iv (P_OUT_OF_MAP) 893 const_iv (P_OUT_OF_MAP)
850 const_iv (P_NEW_MAP) 894 const_iv (P_NEW_MAP)
895
896 const_iv (UP_OBJ_INSERT)
897 const_iv (UP_OBJ_REMOVE)
898 const_iv (UP_OBJ_CHANGE)
899 const_iv (UP_OBJ_FACE)
900
901 const_iv (INS_NO_MERGE)
902 const_iv (INS_ABOVE_FLOOR_ONLY)
903 const_iv (INS_NO_WALK_ON)
904 const_iv (INS_ON_TOP)
905 const_iv (INS_BELOW_ORIGINATOR)
906 const_iv (INS_MAP_LOAD)
907
908 const_iv (WILL_APPLY_HANDLE)
909 const_iv (WILL_APPLY_TREASURE)
910 const_iv (WILL_APPLY_EARTHWALL)
911 const_iv (WILL_APPLY_DOOR)
912 const_iv (WILL_APPLY_FOOD)
851 }; 913 };
852 914
853 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 915 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
854 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 916 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
855 917
869 const_event (TIME) 931 const_event (TIME)
870 const_event (THROW) 932 const_event (THROW)
871 const_event (TRIGGER) 933 const_event (TRIGGER)
872 const_event (CLOSE) 934 const_event (CLOSE)
873 const_event (TIMER) 935 const_event (TIMER)
936 const_event (MOVE)
874 937
875 const_event (BORN) 938 const_event (BORN)
876 const_event (CLOCK) 939 const_event (CLOCK)
877 const_event (CRASH) 940 const_event (CRASH)
878 const_event (PLAYER_DEATH) 941 const_event (PLAYER_DEATH)
885 const_event (REMOVE) 948 const_event (REMOVE)
886 const_event (SHOUT) 949 const_event (SHOUT)
887 const_event (TELL) 950 const_event (TELL)
888 const_event (MUZZLE) 951 const_event (MUZZLE)
889 const_event (KICK) 952 const_event (KICK)
953 //const_event (FREE_OB)
890 }; 954 };
891 955
892 AV *av = get_av ("cf::EVENT", 1); 956 AV *av = get_av ("cf::EVENT", 1);
893 957
894 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) 958 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; )
1094 break; 1158 break;
1095 default: 1159 default:
1096 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1160 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1097 } 1161 }
1098 1162
1099SV * 1163void
1100inv (object *obj) 1164inv (object *obj)
1101 PROTOTYPE: $ 1165 PROTOTYPE: $
1102 PPCODE: 1166 PPCODE:
1103{ 1167{
1104 object *o; 1168 object *o;
1105 for (o = obj->inv; o; o = o->below) 1169 for (o = obj->inv; o; o = o->below)
1106 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1170 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1107} 1171}
1108 1172
1109int cf_object_get_resistance (object *op, int rtype) 1173int cf_object_get_resistance (object *op, int rtype)
1110 ALIAS: resistance = 0 1174 ALIAS: resistance = 0
1111 1175
1112int cf_object_get_flag (object *op, int flag) 1176int cf_object_get_flag (object *op, int flag)
1113 ALIAS: flag = 0 1177 ALIAS: flag = 0
1114 1178
1115void cf_object_set_flag (object *op, int flag, int value) 1179void cf_object_set_flag (object *op, int flag, int value)
1116 1180
1117void cf_object_move (object *op, object *originator, int dir) 1181void cf_object_move (object *op, int dir, object *originator)
1118 1182
1119void cf_object_apply (object *op, object *author, int flags) 1183void cf_object_apply (object *op, object *author, int flags = 0)
1120 1184
1121void cf_object_apply_below (object *op) 1185void cf_object_apply_below (object *op)
1122 1186
1123void cf_object_remove (object *op) 1187void cf_object_remove (object *op)
1124 1188
1205void cf_fix_object (object *pl) 1269void cf_fix_object (object *pl)
1206 ALIAS: fix = 0 1270 ALIAS: fix = 0
1207 1271
1208object *cf_insert_ob_in_ob (object *ob, object *where) 1272object *cf_insert_ob_in_ob (object *ob, object *where)
1209 1273
1274object *get_nearest_player (object *ob)
1275 ALIAS: nearest_player = 0
1276
1277void rangevector (object *ob, object *other, int flags = 0)
1278 PROTOTYPE: $$;$
1279 PPCODE:
1280{
1281 rv_vector rv;
1282 get_rangevector (ob, other, &rv, flags);
1283 EXTEND (SP, 5);
1284 PUSHs (newSVuv (rv.distance));
1285 PUSHs (newSViv (rv.distance_x));
1286 PUSHs (newSViv (rv.distance_y));
1287 PUSHs (newSViv (rv.direction));
1288 PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
1289}
1290
1291bool on_same_map_as (object *ob, object *other)
1292 CODE:
1293 RETVAL = on_same_map (ob, other);
1294 OUTPUT: RETVAL
1295
1210 1296
1211MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1297MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1212 1298
1213player *player (object *op) 1299player *player (object *op)
1214 CODE: 1300 CODE:
1245# nonstandard 1331# nonstandard
1246object *ob (player *pl) 1332object *ob (player *pl)
1247 CODE: 1333 CODE:
1248 RETVAL = pl->ob; 1334 RETVAL = pl->ob;
1249 OUTPUT: RETVAL 1335 OUTPUT: RETVAL
1336
1337player *first ()
1338 CODE:
1339 RETVAL = first_player;
1340 OUTPUT: RETVAL
1341
1342player *next (player *pl)
1343 CODE:
1344 RETVAL = pl->next;
1345 OUTPUT: RETVAL
1346
1347void
1348list ()
1349 PPCODE:
1350{
1351 player *pl;
1352 for (pl = first_player; pl; pl = pl->next)
1353 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1354}
1250 1355
1251 1356
1252MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 1357MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
1253 1358
1254SV * 1359SV *
1294object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) 1399object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny)
1295 C_ARGS: str, map, nx, ny 1400 C_ARGS: str, map, nx, ny
1296 1401
1297#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny) 1402#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny)
1298 1403
1299SV * 1404void
1300at (mapstruct *obj, unsigned int x, unsigned int y) 1405at (mapstruct *obj, unsigned int x, unsigned int y)
1301 PROTOTYPE: $$$ 1406 PROTOTYPE: $$$
1302 INIT: 1407 INIT:
1303 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF; 1408 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_EMPTY;
1304 PPCODE: 1409 PPCODE:
1305{ 1410{
1306 object *o; 1411 object *o;
1307 for (o = GET_MAP_OB (obj, x, y); o; o = o->above) 1412 for (o = GET_MAP_OB (obj, x, y); o; o = o->above)
1308 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1413 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1309} 1414}
1310 1415
1311SV * 1416SV *
1312bot_at (mapstruct *obj, unsigned int x, unsigned int y) 1417bot_at (mapstruct *obj, unsigned int x, unsigned int y)
1313 PROTOTYPE: $$$ 1418 PROTOTYPE: $$$

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines