1 | /* |
1 | /* |
2 | * static char *rcsid_plugins_c = |
2 | * static char *rcsid_plugins_c = |
3 | * "$Id: plugins.C,v 1.2 2006/08/15 17:10:46 root Exp $"; |
3 | * "$Id: plugins.C,v 1.6 2006/08/26 23:36:34 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /*****************************************************************************/ |
6 | /*****************************************************************************/ |
7 | /* CrossFire, A Multiplayer game for X-windows */ |
7 | /* CrossFire, A Multiplayer game for X-windows */ |
8 | /* */ |
8 | /* */ |
… | |
… | |
127 | |
127 | |
128 | /*****************************************************************************/ |
128 | /*****************************************************************************/ |
129 | /* NEW PLUGIN STUFF STARTS HERE */ |
129 | /* NEW PLUGIN STUFF STARTS HERE */ |
130 | /*****************************************************************************/ |
130 | /*****************************************************************************/ |
131 | |
131 | |
132 | static void plugin_load_original_map(mapstruct *map) |
|
|
133 | { |
|
|
134 | execute_global_event (EVENT_MAPLOAD, map); |
|
|
135 | } |
|
|
136 | |
|
|
137 | static void plugin_load_temporary_map(mapstruct *map) |
|
|
138 | { |
|
|
139 | execute_global_event (EVENT_MAPIN, map); |
|
|
140 | } |
|
|
141 | |
|
|
142 | static void plugin_clean_temporary_map(mapstruct *map) |
|
|
143 | { |
|
|
144 | execute_global_event (EVENT_MAPCLEAN, map); |
|
|
145 | } |
|
|
146 | |
|
|
147 | static void plugin_object_free(object *ob) |
|
|
148 | { |
|
|
149 | execute_global_event (EVENT_FREE_OB, ob); |
|
|
150 | } |
|
|
151 | |
|
|
152 | #ifdef WIN32 |
132 | #ifdef WIN32 |
153 | static const char *plugins_dlerror(void) |
133 | static const char *plugins_dlerror(void) |
154 | { |
134 | { |
155 | static char buf[256]; |
135 | static char buf[256]; |
156 | DWORD err; |
136 | DWORD err; |
… | |
… | |
268 | object *env = object_get_env_recursive(tmp); |
248 | object *env = object_get_env_recursive(tmp); |
269 | LOG(llevError, "The requested plugin doesn't exist: %s at %d/%d in map %s\n", tmp->title, env->x, env->y, env->map->name); |
249 | LOG(llevError, "The requested plugin doesn't exist: %s at %d/%d in map %s\n", tmp->title, env->x, env->y, env->map->name); |
270 | send_removed_object(tmp); |
250 | send_removed_object(tmp); |
271 | remove_ob(tmp); |
251 | remove_ob(tmp); |
272 | free_object(tmp); |
252 | free_object(tmp); |
273 | } else { |
253 | } else if (plugin->eventfunc) { |
274 | int rvt = 0; |
254 | int rvt = 0; |
275 | int *rv; |
255 | int *rv; |
276 | |
256 | |
277 | rv = (int*) plugin->eventfunc(&rvt, op, eventcode, activator, third, message, fix, tmp->slaying, tmp->name, tmp); |
257 | rv = (int*) plugin->eventfunc(&rvt, op, eventcode, activator, third, message, fix, tmp->slaying, tmp->name, tmp); |
278 | return *rv; |
258 | return *rv; |
… | |
… | |
309 | if (cp->gevent[eventcode] != NULL) |
289 | if (cp->gevent[eventcode] != NULL) |
310 | cp->gevent[eventcode](&rt, eventcode); |
290 | cp->gevent[eventcode](&rt, eventcode); |
311 | } |
291 | } |
312 | break; |
292 | break; |
313 | |
293 | |
314 | case EVENT_PLAYER_LOAD: |
|
|
315 | case EVENT_PLAYER_SAVE: |
|
|
316 | /* object, filename */ |
|
|
317 | op = va_arg(args, object*); |
|
|
318 | buf = va_arg(args, char*); |
|
|
319 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
320 | if (cp->gevent[eventcode] != NULL) |
|
|
321 | cp->gevent[eventcode](&rt, eventcode, op, buf); |
|
|
322 | } |
|
|
323 | break; |
|
|
324 | |
|
|
325 | case EVENT_MAPLOAD: |
|
|
326 | case EVENT_MAPOUT: |
|
|
327 | case EVENT_MAPIN: |
|
|
328 | case EVENT_MAPCLEAN: |
|
|
329 | /* map argument */ |
|
|
330 | map = va_arg(args, mapstruct*); |
|
|
331 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
332 | if (cp->gevent[eventcode] != NULL) |
|
|
333 | cp->gevent[eventcode](&rt, eventcode, map); |
|
|
334 | } |
|
|
335 | break; |
|
|
336 | |
|
|
337 | case EVENT_REMOVE: |
|
|
338 | case EVENT_MAPENTER: |
|
|
339 | case EVENT_MAPLEAVE: |
|
|
340 | case EVENT_BORN: |
|
|
341 | case EVENT_PLAYER_DEATH: |
|
|
342 | case EVENT_FREE_OB: |
|
|
343 | /* op argument */ |
|
|
344 | op = va_arg(args, object*); |
|
|
345 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
346 | if (cp->gevent[eventcode] != NULL) |
|
|
347 | cp->gevent[eventcode](&rt, eventcode, op); |
|
|
348 | } |
|
|
349 | break; |
|
|
350 | |
|
|
351 | case EVENT_GKILL: |
|
|
352 | /*GKILL: op, hitter*/ |
|
|
353 | op = va_arg(args, object*); |
|
|
354 | op2 = va_arg(args, object*); |
|
|
355 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
356 | if (cp->gevent[eventcode] != NULL) |
|
|
357 | cp->gevent[eventcode](&rt, eventcode, op, op2); |
|
|
358 | } |
|
|
359 | break; |
|
|
360 | |
|
|
361 | case EVENT_LOGIN: |
|
|
362 | /*LOGIN: pl, pl->socket.host*/ |
|
|
363 | pl = va_arg(args, player*); |
|
|
364 | buf = va_arg(args, char*); |
|
|
365 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
366 | if (cp->gevent[eventcode] != NULL) |
|
|
367 | cp->gevent[eventcode](&rt, eventcode, pl, buf); |
|
|
368 | } |
|
|
369 | break; |
|
|
370 | |
|
|
371 | case EVENT_LOGOUT: |
|
|
372 | /*LOGOUT: pl, pl->socket.host*/ |
|
|
373 | pl = va_arg(args, player*); |
|
|
374 | buf = va_arg(args, char*); |
|
|
375 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
376 | if (cp->gevent[eventcode] != NULL) |
|
|
377 | cp->gevent[eventcode](&rt, eventcode, pl, buf); |
|
|
378 | } |
|
|
379 | break; |
|
|
380 | |
|
|
381 | case EVENT_MAPRESET: |
|
|
382 | /*MAPRESET: map->path*/ |
|
|
383 | buf = va_arg(args, char*); |
|
|
384 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
385 | if (cp->gevent[eventcode] != NULL) |
|
|
386 | cp->gevent[eventcode](&rt, eventcode, buf); |
|
|
387 | } |
|
|
388 | break; |
|
|
389 | |
|
|
390 | case EVENT_SHOUT: |
294 | case EVENT_SHOUT: |
391 | /*SHOUT: op, parms, priority*/ |
295 | /*SHOUT: op, parms, priority*/ |
392 | op = va_arg(args, object*); |
296 | op = va_arg(args, object*); |
393 | buf = va_arg(args, char*); |
297 | buf = va_arg(args, char*); |
394 | i = va_arg(args, int); |
298 | i = va_arg(args, int); |
… | |
… | |
415 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
319 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
416 | if (cp->gevent[eventcode] != NULL) |
320 | if (cp->gevent[eventcode] != NULL) |
417 | cp->gevent[eventcode](&rt, eventcode, op, buf); |
321 | cp->gevent[eventcode](&rt, eventcode, op, buf); |
418 | } |
322 | } |
419 | break; |
323 | break; |
420 | |
|
|
421 | case EVENT_FIND_UNARMED_SKILL: |
|
|
422 | op = va_arg(args, object*); |
|
|
423 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
424 | if (cp->gevent[eventcode] != NULL) |
|
|
425 | if ((tmpretval = (int*) cp->gevent[eventcode](&rt, eventcode, op)) && *tmpretval > 0) { |
|
|
426 | retval = *tmpretval; |
|
|
427 | } |
|
|
428 | } |
|
|
429 | break; |
|
|
430 | |
|
|
431 | case EVENT_PLAYER_USE_SKILL: |
|
|
432 | case EVENT_MONSTER_USE_SKILL: |
|
|
433 | op = va_arg(args, object*); |
|
|
434 | op2 = va_arg(args, object*); |
|
|
435 | op3 = va_arg(args, object*); |
|
|
436 | fix = va_arg(args, int); |
|
|
437 | buf = va_arg(args, char*); |
|
|
438 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
439 | if (cp->gevent[eventcode] != NULL) |
|
|
440 | if ((tmpretval = (int*) cp->gevent[eventcode](&rt, eventcode, op, op2, op3, fix, buf)) && *tmpretval != 0) { |
|
|
441 | retval = *tmpretval; |
|
|
442 | } |
|
|
443 | } |
|
|
444 | break; |
|
|
445 | |
|
|
446 | case EVENT_CAST_SPELL: |
|
|
447 | op = va_arg(args, object*); |
|
|
448 | op2 = va_arg(args, object*); |
|
|
449 | op3 = va_arg(args, object*); |
|
|
450 | fix = va_arg(args, int); |
|
|
451 | buf = va_arg(args, char*); |
|
|
452 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
453 | if (cp->gevent[eventcode] != NULL) |
|
|
454 | if ((tmpretval = (int*) cp->gevent[eventcode](&rt, eventcode, op, op2, op3, fix, buf)) && *tmpretval != 0) { |
|
|
455 | retval = *tmpretval; |
|
|
456 | break; /* The first who did something wins! */ |
|
|
457 | } |
|
|
458 | } |
|
|
459 | break; |
|
|
460 | |
|
|
461 | case EVENT_EXTCMD: |
|
|
462 | /*KICK: op, parms*/ |
|
|
463 | pl = va_arg (args, player *); |
|
|
464 | buf = va_arg (args, char *); |
|
|
465 | i = va_arg (args, int); |
|
|
466 | for (cp = plugins_list; cp != NULL; cp = cp->next) { |
|
|
467 | if (cp->gevent[eventcode] != NULL) |
|
|
468 | cp->gevent[eventcode](&rt, eventcode, pl, buf, i, buf); |
|
|
469 | } |
|
|
470 | break; |
|
|
471 | } |
324 | } |
472 | va_end(args); |
325 | va_end(args); |
473 | |
326 | |
474 | return retval; |
327 | return retval; |
475 | } |
328 | } |
476 | |
329 | |
477 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr); |
330 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr); |
478 | extern "C" void *cfperl_getPluginProperty (int *type, ...); |
331 | extern "C" void *cfperl_getPluginProperty (int *type, ...); |
479 | extern "C" int cfperl_postInitPlugin (); |
332 | extern "C" int cfperl_postInitPlugin (); |
480 | extern "C" void *cfperl_eventListener (int *type, ...); |
|
|
481 | extern "C" int cfperl_closePlugin (); |
333 | extern "C" int cfperl_closePlugin (); |
482 | |
334 | |
|
|
335 | // this is a temporary hack till the old plugin stuff is removed |
483 | int plugins_init_perl() |
336 | int plugins_init_perl() |
484 | { |
337 | { |
485 | f_plug_init initfunc; |
338 | f_plug_init initfunc; |
486 | f_plug_api propfunc; |
339 | f_plug_api propfunc; |
487 | f_plug_api eventfunc; |
|
|
488 | f_plug_postinit postfunc; |
340 | f_plug_postinit postfunc; |
489 | f_plug_postinit closefunc; |
341 | f_plug_postinit closefunc; |
490 | int i; |
342 | int i; |
491 | crossfire_plugin* cp; |
343 | crossfire_plugin* cp; |
492 | crossfire_plugin* ccp; |
344 | crossfire_plugin* ccp; |
493 | |
345 | |
494 | load_original_map_callback = plugin_load_original_map; |
|
|
495 | load_temporary_map_callback = plugin_load_temporary_map; |
|
|
496 | clean_temporary_map_callback = plugin_clean_temporary_map; |
|
|
497 | object_free_callback = plugin_object_free; |
|
|
498 | |
|
|
499 | initfunc = (f_plug_init)cfperl_initPlugin; |
346 | initfunc = (f_plug_init)cfperl_initPlugin; |
500 | propfunc = (f_plug_api)cfperl_getPluginProperty; |
347 | propfunc = (f_plug_api)cfperl_getPluginProperty; |
501 | eventfunc = (f_plug_api)cfperl_eventListener; |
|
|
502 | postfunc = (f_plug_postinit)cfperl_postInitPlugin; |
348 | postfunc = (f_plug_postinit)cfperl_postInitPlugin; |
503 | closefunc = (f_plug_postinit)cfperl_closePlugin; |
349 | closefunc = (f_plug_postinit)cfperl_closePlugin; |
504 | i = initfunc("2.0", cfapi_get_hooks); |
350 | i = initfunc("2.0", cfapi_get_hooks); |
505 | cp = (crossfire_plugin*) malloc(sizeof(crossfire_plugin)); |
351 | cp = (crossfire_plugin*) malloc(sizeof(crossfire_plugin)); |
506 | for (i = 0; i < NR_EVENTS; i++) |
352 | for (i = 0; i < NR_EVENTS; i++) |
507 | cp->gevent[i] = NULL; |
353 | cp->gevent[i] = NULL; |
508 | cp->eventfunc = eventfunc; |
354 | cp->eventfunc = 0; |
509 | cp->propfunc = propfunc; |
355 | cp->propfunc = propfunc; |
510 | cp->closefunc = closefunc; |
356 | cp->closefunc = closefunc; |
511 | cp->libptr = 0; |
357 | cp->libptr = 0; |
512 | strcpy(cp->id, (const char*) propfunc(&i, "Identification")); |
358 | strcpy(cp->id, (const char*) propfunc(&i, "Identification")); |
513 | strcpy(cp->fullname, (const char*) propfunc(&i, "FullName")); |
359 | strcpy(cp->fullname, (const char*) propfunc(&i, "FullName")); |
… | |
… | |
535 | f_plug_postinit postfunc; |
381 | f_plug_postinit postfunc; |
536 | f_plug_postinit closefunc; |
382 | f_plug_postinit closefunc; |
537 | int i; |
383 | int i; |
538 | crossfire_plugin* cp; |
384 | crossfire_plugin* cp; |
539 | crossfire_plugin* ccp; |
385 | crossfire_plugin* ccp; |
540 | |
|
|
541 | load_original_map_callback = plugin_load_original_map; |
|
|
542 | load_temporary_map_callback = plugin_load_temporary_map; |
|
|
543 | clean_temporary_map_callback = plugin_clean_temporary_map; |
|
|
544 | object_free_callback = plugin_object_free; |
|
|
545 | |
386 | |
546 | /* Open the plugin lib and load the required functions */ |
387 | /* Open the plugin lib and load the required functions */ |
547 | ptr = plugins_dlopen(libfile); |
388 | ptr = plugins_dlopen(libfile); |
548 | if (ptr == NULL) { |
389 | if (ptr == NULL) { |
549 | LOG(llevError, "Error trying to load %s: %s\n", libfile, plugins_dlerror()); |
390 | LOG(llevError, "Error trying to load %s: %s\n", libfile, plugins_dlerror()); |