Crossfire Server, Branch 1.12  R12190
plugin_template.c
Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /* Template for version 2.0 plugins.                                         */
00003 /* Contact: yann.chachkoff@myrealbox.com                                     */
00004 /*****************************************************************************/
00005 /* That code is placed under the GNU General Public Licence (GPL)            */
00006 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints)     */
00007 /*****************************************************************************/
00008 /*  CrossFire, A Multiplayer game for X-windows                              */
00009 /*                                                                           */
00010 /*  Copyright (C) 2000 Mark Wedel                                            */
00011 /*  Copyright (C) 1992 Frank Tore Johansen                                   */
00012 /*                                                                           */
00013 /*  This program is free software; you can redistribute it and/or modify     */
00014 /*  it under the terms of the GNU General Public License as published by     */
00015 /*  the Free Software Foundation; either version 2 of the License, or        */
00016 /*  (at your option) any later version.                                      */
00017 /*                                                                           */
00018 /*  This program is distributed in the hope that it will be useful,          */
00019 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
00020 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
00021 /*  GNU General Public License for more details.                             */
00022 /*                                                                           */
00023 /*  You should have received a copy of the GNU General Public License        */
00024 /*  along with this program; if not, write to the Free Software              */
00025 /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00026 /*                                                                           */
00027 /*****************************************************************************/
00028 
00029 /* First let's include the header file needed                                */
00030 
00031 #include <plugin_template.h>
00032 #include <stdarg.h>
00033 #ifndef __CEXTRACT__
00034 #include <plugin_template_proto.h>
00035 #endif
00036 
00037 CFPContext *context_stack;
00038 
00039 CFPContext *current_context;
00040 
00041 static int current_command = -999;
00042 
00043 void initContextStack(void) {
00044     current_context = NULL;
00045     context_stack = NULL;
00046 }
00047 
00048 void pushContext(CFPContext *context) {
00049     CFPContext *stack_context;
00050 
00051     if (current_context == NULL) {
00052         context_stack = context;
00053         context->down = NULL;
00054     } else {
00055         context->down = current_context;
00056     }
00057     current_context = context;
00058 }
00059 
00060 CFPContext *popContext(void) {
00061     CFPContext *oldcontext;
00062 
00063     if (current_context != NULL) {
00064         oldcontext = current_context;
00065         current_context = current_context->down;
00066         return oldcontext;
00067     } else
00068         return NULL;
00069 }
00070 
00071 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
00072     cf_init_plugin(gethooksptr);
00073 
00074     cf_log(llevDebug, PLUGIN_VERSION " init\n");
00075 
00076     /* Place your initialization code here */
00077     return 0;
00078 }
00079 
00080 CF_PLUGIN void *getPluginProperty(int *type, ...) {
00081     va_list args;
00082     const char *propname;
00083     int i, size;
00084     command_array_struct *rtn_cmd;
00085     char *buf;
00086 
00087     va_start(args, type);
00088     propname = va_arg(args, const char *);
00089 
00090     if (!strcmp(propname, "command?")) {
00091         const char *cmdname;
00092         cmdname = va_arg(args, const char *);
00093         rtn_cmd = va_arg(args, command_array_struct *);
00094         va_end(args);
00095 
00097         return NULL;
00098     } else if (!strcmp(propname, "Identification")) {
00099         buf = va_arg(args, char *);
00100         size = va_arg(args, int);
00101         va_end(args);
00102         snprintf(buf, size, PLUGIN_NAME);
00103         return NULL;
00104     } else if (!strcmp(propname, "FullName")) {
00105         buf = va_arg(args, char *);
00106         size = va_arg(args, int);
00107         va_end(args);
00108         snprintf(buf, size, PLUGIN_VERSION);
00109         return NULL;
00110     }
00111     va_end(args);
00112     return NULL;
00113 }
00114 
00115 CF_PLUGIN int runPluginCommand(object *op, char *params) {
00116     return -1;
00117 }
00118 
00119 CF_PLUGIN int postInitPlugin(void) {
00120     cf_log(llevDebug, PLUGIN_VERSION" post init\n");
00121     initContextStack();
00122     /* Pick the global events you want to monitor from this plugin */
00123 /*
00124     cf_system_register_global_event(EVENT_BORN, PLUGIN_NAME, globalEventListener);
00125     cf_system_register_global_event(EVENT_CLOCK, PLUGIN_NAME, globalEventListener);
00126     cf_system_register_global_event(EVENT_CRASH, PLUGIN_NAME, globalEventListener);
00127     cf_system_register_global_event(EVENT_PLAYER_DEATH, PLUGIN_NAME, globalEventListener);
00128     cf_system_register_global_event(EVENT_GKILL, PLUGIN_NAME, globalEventListener);
00129     cf_system_register_global_event(EVENT_LOGIN, PLUGIN_NAME, globalEventListener);
00130     cf_system_register_global_event(EVENT_LOGOUT, PLUGIN_NAME, globalEventListener);
00131     cf_system_register_global_event(EVENT_MAPENTER, PLUGIN_NAME, globalEventListener);
00132     cf_system_register_global_event(EVENT_MAPLEAVE, PLUGIN_NAME, globalEventListener);
00133     cf_system_register_global_event(EVENT_MAPRESET, PLUGIN_NAME, globalEventListener);
00134     cf_system_register_global_event(EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
00135     cf_system_register_global_event(EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
00136     cf_system_register_global_event(EVENT_TELL, PLUGIN_NAME, globalEventListener);
00137     cf_system_register_global_event(EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
00138     cf_system_register_global_event(EVENT_KICK, PLUGIN_NAME, globalEventListener);
00139 */
00140     return 0;
00141 }
00142 
00143 CF_PLUGIN void *globalEventListener(int *type, ...) {
00144     va_list args;
00145     static int rv = 0;
00146     CFPContext *context;
00147     context = malloc(sizeof(CFPContext));
00148     char *buf;
00149     player *pl;
00150     object *op;
00151 
00152     va_start(args, type);
00153     context->event_code = va_arg(args, int);
00154     printf("****** Global event listener called ***********\n");
00155     printf("- Event code: %d\n", context->event_code);
00156 
00157     context->message[0] = 0;
00158 
00159     context->who = NULL;
00160     context->activator = NULL;
00161     context->third = NULL;
00162     context->event = NULL;
00163     rv = context->returnvalue = 0;
00164     switch (context->event_code) {
00165     case EVENT_CRASH:
00166         printf("Unimplemented for now\n");
00167         break;
00168 
00169     case EVENT_BORN:
00170         context->activator = va_arg(args, object *);
00171         break;
00172 
00173     case EVENT_PLAYER_DEATH:
00174         context->who = va_arg(args, object *);
00175         break;
00176 
00177     case EVENT_GKILL:
00178         context->who = va_arg(args, object *);
00179         context->activator = va_arg(args, object *);
00180         break;
00181 
00182     case EVENT_LOGIN:
00183         pl = va_arg(args, player *);
00184         context->activator = pl->ob;
00185         buf = va_arg(args, char *);
00186         if (buf != 0)
00187             strcpy(context->message, buf);
00188         break;
00189 
00190     case EVENT_LOGOUT:
00191         pl = va_arg(args, player *);
00192         context->activator = pl->ob;
00193         buf = va_arg(args, char *);
00194         if (buf != 0)
00195             strcpy(context->message, buf);
00196         break;
00197 
00198     case EVENT_REMOVE:
00199         context->activator = va_arg(args, object *);
00200         break;
00201 
00202     case EVENT_SHOUT:
00203         context->activator = va_arg(args, object *);
00204         buf = va_arg(args, char *);
00205         if (buf != 0)
00206             strcpy(context->message, buf);
00207         break;
00208 
00209     case EVENT_MUZZLE:
00210         context->activator = va_arg(args, object *);
00211         buf = va_arg(args, char *);
00212         if (buf != 0)
00213             strcpy(context->message, buf);
00214         break;
00215 
00216     case EVENT_KICK:
00217         context->activator = va_arg(args, object *);
00218         buf = va_arg(args, char *);
00219         if (buf != 0)
00220             strcpy(context->message, buf);
00221         break;
00222 
00223     case EVENT_MAPENTER:
00224         context->activator = va_arg(args, object *);
00225         break;
00226 
00227     case EVENT_MAPLEAVE:
00228         context->activator = va_arg(args, object *);
00229         break;
00230 
00231     case EVENT_CLOCK:
00232         break;
00233 
00234     case EVENT_MAPRESET:
00235         buf = va_arg(args, char *);
00236         if (buf != 0)
00237             strcpy(context->message, buf);
00238         break;
00239 
00240     case EVENT_TELL:
00241         context->activator = va_arg(args, object *);
00242         buf = va_arg(args, char *);
00243         if (buf != 0)
00244             strcpy(context->message, buf);
00245         context->third = va_arg(args, object *);
00246         break;
00247     }
00248     va_end(args);
00249     context->returnvalue = 0;
00250 
00251     pushContext(context);
00252     /* Put your plugin action(s) here */
00253     context = popContext();
00254     rv = context->returnvalue;
00255     free(context);
00256     cf_log(llevDebug, "*********** Execution complete ****************\n");
00257 
00258     return &rv;
00259 }
00260 
00261 CF_PLUGIN void *eventListener(int *type, ...) {
00262     static int rv = 0;
00263     va_list args;
00264     char *buf;
00265     CFPContext *context;
00266 
00267     context = malloc(sizeof(CFPContext));
00268 
00269     context->message[0] = 0;
00270 
00271     va_start(args, type);
00272 
00273     context->who = va_arg(args, object *);
00274     context->activator = va_arg(args, object *);
00275     context->third = va_arg(args, object *);
00276     buf = va_arg(args, char *);
00277     if (buf != 0)
00278         strcpy(context->message, buf);
00279     context->fix = va_arg(args, int);
00280     context->event = va_arg(args, object *);
00281     context->event_code = context->event->subtype;
00282     strncpy(context->options, context->event->name, sizeof(context->options));
00283     context->returnvalue = 0;
00284     va_end(args);
00285 
00286     pushContext(context);
00287     /* Put your plugin action(s) here */
00288     context = popContext();
00289     rv = context->returnvalue;
00290     free(context);
00291     cf_log(llevDebug, "Execution complete");
00292     return &rv;
00293 }
00294 
00295 CF_PLUGIN int closePlugin(void) {
00296     cf_log(llevDebug, PLUGIN_VERSION " closing\n");
00297     return 0;
00298 }