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.13 by root, Tue Feb 7 03:06:01 2006 UTC vs.
Revision 1.14 by root, Tue Feb 7 23:29:55 2006 UTC

68 int returnvalue; 68 int returnvalue;
69} CFPContext; 69} CFPContext;
70 70
71//static int current_command = -999; 71//static int current_command = -999;
72 72
73static HV *obj_cache;
73static PerlInterpreter *perl; 74static PerlInterpreter *perl;
74 75
75////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 76//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77
78// garbage collect some perl objects, if possible
79// all objects no longer referenced and empty are
80// eligible for destruction.
81void
82clean_obj_cache ()
83{
84 int todo = 10;
85 do
86 {
87 I32 klen;
88 char *key;
89 HE *he = hv_iternext (obj_cache);
90
91 if (he)
92 {
93 SV *sv = SvRV (hv_iterval (obj_cache, he));
94
95 // emopty and unreferened? nuke it
96 if (SvREFCNT (sv) == 1 && !HvFILL ((HV *)sv))
97 {
98 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
99 todo++;
100 }
101 }
102 else
103 break;
104 }
105 while (--todo);
106}
76 107
77static SV * 108static SV *
78newSVptr (void *ptr, const char *klass) 109newSVptr (void *ptr, const char *klass)
79{ 110{
80 if (!ptr) 111 if (!ptr)
81 return &PL_sv_undef; 112 return &PL_sv_undef;
82 113
83 HV *hv = newHV (); 114 HV *hv = newHV ();
84 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 115 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
85 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 116 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
117}
118
119static SV *
120newSVptr_cached (void *ptr, const char *klass)
121{
122 SV *sv, **he;
123
124 if (!ptr)
125 return &PL_sv_undef;
126
127 he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0);
128
129 if (he)
130 sv = *he;
131 else
132 {
133 sv = newSVptr (ptr, klass);
134 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
135 }
136
137 return newSVsv (sv);
86} 138}
87 139
88static void 140static void
89clearSVptr (SV *sv) 141clearSVptr (SV *sv)
90{ 142{
146 sv = &PL_sv_undef; 198 sv = &PL_sv_undef;
147 else 199 else
148 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 200 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE))
149 { 201 {
150 case MAP: 202 case MAP:
151 sv = newSVptr (obj, "cf::object::map"); 203 sv = newSVptr_cached (obj, "cf::object::map");
152 break; 204 break;
153 205
154 case PLAYER: 206 case PLAYER:
155 sv = newSVptr (obj, "cf::object::player"); 207 sv = newSVptr_cached (obj, "cf::object::player");
156 break; 208 break;
157 209
158 default: 210 default:
159 sv = newSVptr (obj, "cf::object"); 211 sv = newSVptr_cached (obj, "cf::object");
160 break; 212 break;
161 } 213 }
162 } 214 }
163 break; 215 break;
164 216
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 strcpy (context.message, buf);
440 break; 494 break;
441 495
442 case EVENT_REMOVE:
443 context.activator = va_arg (args, object *);
444 break;
445
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 strcpy (context.message, buf);
465 break; 503 break;
466 504
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;
474
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 strcpy (context.message, buf);
482 break; 516 break;
483
484 case EVENT_TELL:
485 break;
486 } 517 }
487 518
488 va_end (args); 519 va_end (args);
489 520
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
490 inject_event (&context); 532 inject_event (&context);
491 533
492 rv = context.returnvalue; 534 rv = context.returnvalue;
493 535
494 return &rv; 536 return &rv;
495} 537}
846 const_iv (P_NO_CLERIC) 888 const_iv (P_NO_CLERIC)
847 const_iv (P_NEED_UPDATE) 889 const_iv (P_NEED_UPDATE)
848 const_iv (P_NO_ERROR) 890 const_iv (P_NO_ERROR)
849 const_iv (P_OUT_OF_MAP) 891 const_iv (P_OUT_OF_MAP)
850 const_iv (P_NEW_MAP) 892 const_iv (P_NEW_MAP)
893
894 const_iv (UP_OBJ_INSERT)
895 const_iv (UP_OBJ_REMOVE)
896 const_iv (UP_OBJ_CHANGE)
897 const_iv (UP_OBJ_FACE)
898
899 const_iv (INS_NO_MERGE)
900 const_iv (INS_ABOVE_FLOOR_ONLY)
901 const_iv (INS_NO_WALK_ON)
902 const_iv (INS_ON_TOP)
903 const_iv (INS_BELOW_ORIGINATOR)
904 const_iv (INS_MAP_LOAD)
905
906 const_iv (WILL_APPLY_HANDLE)
907 const_iv (WILL_APPLY_TREASURE)
908 const_iv (WILL_APPLY_EARTHWALL)
909 const_iv (WILL_APPLY_DOOR)
910 const_iv (WILL_APPLY_FOOD)
851 }; 911 };
852 912
853 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 913 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
854 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 914 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
855 915
869 const_event (TIME) 929 const_event (TIME)
870 const_event (THROW) 930 const_event (THROW)
871 const_event (TRIGGER) 931 const_event (TRIGGER)
872 const_event (CLOSE) 932 const_event (CLOSE)
873 const_event (TIMER) 933 const_event (TIMER)
934 const_event (MOVE)
874 935
875 const_event (BORN) 936 const_event (BORN)
876 const_event (CLOCK) 937 const_event (CLOCK)
877 const_event (CRASH) 938 const_event (CRASH)
878 const_event (PLAYER_DEATH) 939 const_event (PLAYER_DEATH)
885 const_event (REMOVE) 946 const_event (REMOVE)
886 const_event (SHOUT) 947 const_event (SHOUT)
887 const_event (TELL) 948 const_event (TELL)
888 const_event (MUZZLE) 949 const_event (MUZZLE)
889 const_event (KICK) 950 const_event (KICK)
951 //const_event (FREE_OB)
890 }; 952 };
891 953
892 AV *av = get_av ("cf::EVENT", 1); 954 AV *av = get_av ("cf::EVENT", 1);
893 955
894 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) 956 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; )
1094 break; 1156 break;
1095 default: 1157 default:
1096 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1158 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1097 } 1159 }
1098 1160
1099SV * 1161void
1100inv (object *obj) 1162inv (object *obj)
1101 PROTOTYPE: $ 1163 PROTOTYPE: $
1102 PPCODE: 1164 PPCODE:
1103{ 1165{
1104 object *o; 1166 object *o;
1105 for (o = obj->inv; o; o = o->below) 1167 for (o = obj->inv; o; o = o->below)
1106 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1168 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1107} 1169}
1108 1170
1109int cf_object_get_resistance (object *op, int rtype) 1171int cf_object_get_resistance (object *op, int rtype)
1110 ALIAS: resistance = 0 1172 ALIAS: resistance = 0
1111 1173
1294object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) 1356object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny)
1295 C_ARGS: str, map, nx, ny 1357 C_ARGS: str, map, nx, ny
1296 1358
1297#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny) 1359#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny)
1298 1360
1299SV * 1361void
1300at (mapstruct *obj, unsigned int x, unsigned int y) 1362at (mapstruct *obj, unsigned int x, unsigned int y)
1301 PROTOTYPE: $$$ 1363 PROTOTYPE: $$$
1302 INIT: 1364 INIT:
1303 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF; 1365 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_EMPTY;
1304 PPCODE: 1366 PPCODE:
1305{ 1367{
1306 object *o; 1368 object *o;
1307 for (o = GET_MAP_OB (obj, x, y); o; o = o->above) 1369 for (o = GET_MAP_OB (obj, x, y); o; o = o->above)
1308 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1370 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1309} 1371}
1310 1372
1311SV * 1373SV *
1312bot_at (mapstruct *obj, unsigned int x, unsigned int y) 1374bot_at (mapstruct *obj, unsigned int x, unsigned int y)
1313 PROTOTYPE: $$$ 1375 PROTOTYPE: $$$

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines