Crossfire Server, Branches 1.12  R18729
plugin_template.c
Go to the documentation of this file.
1 /*****************************************************************************/
2 /* Template for version 2.0 plugins. */
3 /* Contact: yann.chachkoff@myrealbox.com */
4 /*****************************************************************************/
5 /* That code is placed under the GNU General Public Licence (GPL) */
6 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints) */
7 /*****************************************************************************/
8 /* CrossFire, A Multiplayer game for X-windows */
9 /* */
10 /* Copyright (C) 2000 Mark Wedel */
11 /* Copyright (C) 1992 Frank Tore Johansen */
12 /* */
13 /* This program is free software; you can redistribute it and/or modify */
14 /* it under the terms of the GNU General Public License as published by */
15 /* the Free Software Foundation; either version 2 of the License, or */
16 /* (at your option) any later version. */
17 /* */
18 /* This program is distributed in the hope that it will be useful, */
19 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
20 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
21 /* GNU General Public License for more details. */
22 /* */
23 /* You should have received a copy of the GNU General Public License */
24 /* along with this program; if not, write to the Free Software */
25 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* */
27 /*****************************************************************************/
28 
29 /* First let's include the header file needed */
30 
31 #include <plugin_template.h>
32 #include <stdarg.h>
33 #ifndef __CEXTRACT__
34 #include <plugin_template_proto.h>
35 #endif
36 
38 
40 
41 static int current_command = -999;
42 
43 void initContextStack(void) {
44  current_context = NULL;
45  context_stack = NULL;
46 }
47 
48 void pushContext(CFPContext *context) {
49  CFPContext *stack_context;
50 
51  if (current_context == NULL) {
52  context_stack = context;
53  context->down = NULL;
54  } else {
55  context->down = current_context;
56  }
57  current_context = context;
58 }
59 
61  CFPContext *oldcontext;
62 
63  if (current_context != NULL) {
64  oldcontext = current_context;
65  current_context = current_context->down;
66  return oldcontext;
67  } else
68  return NULL;
69 }
70 
71 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
72  cf_init_plugin(gethooksptr);
73 
74  cf_log(llevDebug, PLUGIN_VERSION " init\n");
75 
76  /* Place your initialization code here */
77  return 0;
78 }
79 
80 CF_PLUGIN void *getPluginProperty(int *type, ...) {
81  va_list args;
82  const char *propname;
83  int i, size;
84  command_array_struct *rtn_cmd;
85  char *buf;
86 
87  va_start(args, type);
88  propname = va_arg(args, const char *);
89 
90  if (!strcmp(propname, "command?")) {
91  const char *cmdname;
92  cmdname = va_arg(args, const char *);
93  rtn_cmd = va_arg(args, command_array_struct *);
94  va_end(args);
95 
97  return NULL;
98  } else if (!strcmp(propname, "Identification")) {
99  buf = va_arg(args, char *);
100  size = va_arg(args, int);
101  va_end(args);
102  snprintf(buf, size, PLUGIN_NAME);
103  return NULL;
104  } else if (!strcmp(propname, "FullName")) {
105  buf = va_arg(args, char *);
106  size = va_arg(args, int);
107  va_end(args);
108  snprintf(buf, size, PLUGIN_VERSION);
109  return NULL;
110  }
111  va_end(args);
112  return NULL;
113 }
114 
115 CF_PLUGIN int runPluginCommand(object *op, char *params) {
116  return -1;
117 }
118 
120  cf_log(llevDebug, PLUGIN_VERSION" post init\n");
122  /* Pick the global events you want to monitor from this plugin */
123 /*
124  cf_system_register_global_event(EVENT_BORN, PLUGIN_NAME, globalEventListener);
125  cf_system_register_global_event(EVENT_CLOCK, PLUGIN_NAME, globalEventListener);
126  cf_system_register_global_event(EVENT_CRASH, PLUGIN_NAME, globalEventListener);
127  cf_system_register_global_event(EVENT_PLAYER_DEATH, PLUGIN_NAME, globalEventListener);
128  cf_system_register_global_event(EVENT_GKILL, PLUGIN_NAME, globalEventListener);
129  cf_system_register_global_event(EVENT_LOGIN, PLUGIN_NAME, globalEventListener);
130  cf_system_register_global_event(EVENT_LOGOUT, PLUGIN_NAME, globalEventListener);
131  cf_system_register_global_event(EVENT_MAPENTER, PLUGIN_NAME, globalEventListener);
132  cf_system_register_global_event(EVENT_MAPLEAVE, PLUGIN_NAME, globalEventListener);
133  cf_system_register_global_event(EVENT_MAPRESET, PLUGIN_NAME, globalEventListener);
134  cf_system_register_global_event(EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
135  cf_system_register_global_event(EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
136  cf_system_register_global_event(EVENT_TELL, PLUGIN_NAME, globalEventListener);
137  cf_system_register_global_event(EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
138  cf_system_register_global_event(EVENT_KICK, PLUGIN_NAME, globalEventListener);
139 */
140  return 0;
141 }
142 
143 CF_PLUGIN void *globalEventListener(int *type, ...) {
144  va_list args;
145  static int rv = 0;
146  CFPContext *context;
147  context = malloc(sizeof(CFPContext));
148  char *buf;
149  player *pl;
150  object *op;
151 
152  va_start(args, type);
153  context->event_code = va_arg(args, int);
154  printf("****** Global event listener called ***********\n");
155  printf("- Event code: %d\n", context->event_code);
156 
157  context->message[0] = 0;
158 
159  context->who = NULL;
160  context->activator = NULL;
161  context->third = NULL;
162  context->event = NULL;
163  rv = context->returnvalue = 0;
164  switch (context->event_code) {
165  case EVENT_CRASH:
166  printf("Unimplemented for now\n");
167  break;
168 
169  case EVENT_BORN:
170  context->activator = va_arg(args, object *);
171  break;
172 
173  case EVENT_PLAYER_DEATH:
174  context->who = va_arg(args, object *);
175  break;
176 
177  case EVENT_GKILL:
178  context->who = va_arg(args, object *);
179  context->activator = va_arg(args, object *);
180  break;
181 
182  case EVENT_LOGIN:
183  pl = va_arg(args, player *);
184  context->activator = pl->ob;
185  buf = va_arg(args, char *);
186  if (buf != 0)
187  strcpy(context->message, buf);
188  break;
189 
190  case EVENT_LOGOUT:
191  pl = va_arg(args, player *);
192  context->activator = pl->ob;
193  buf = va_arg(args, char *);
194  if (buf != 0)
195  strcpy(context->message, buf);
196  break;
197 
198  case EVENT_REMOVE:
199  context->activator = va_arg(args, object *);
200  break;
201 
202  case EVENT_SHOUT:
203  context->activator = va_arg(args, object *);
204  buf = va_arg(args, char *);
205  if (buf != 0)
206  strcpy(context->message, buf);
207  break;
208 
209  case EVENT_MUZZLE:
210  context->activator = va_arg(args, object *);
211  buf = va_arg(args, char *);
212  if (buf != 0)
213  strcpy(context->message, buf);
214  break;
215 
216  case EVENT_KICK:
217  context->activator = va_arg(args, object *);
218  buf = va_arg(args, char *);
219  if (buf != 0)
220  strcpy(context->message, buf);
221  break;
222 
223  case EVENT_MAPENTER:
224  context->activator = va_arg(args, object *);
225  break;
226 
227  case EVENT_MAPLEAVE:
228  context->activator = va_arg(args, object *);
229  break;
230 
231  case EVENT_CLOCK:
232  break;
233 
234  case EVENT_MAPRESET:
235  buf = va_arg(args, char *);
236  if (buf != 0)
237  strcpy(context->message, buf);
238  break;
239 
240  case EVENT_TELL:
241  context->activator = va_arg(args, object *);
242  buf = va_arg(args, char *);
243  if (buf != 0)
244  strcpy(context->message, buf);
245  context->third = va_arg(args, object *);
246  break;
247  }
248  va_end(args);
249  context->returnvalue = 0;
250 
251  pushContext(context);
252  /* Put your plugin action(s) here */
253  context = popContext();
254  rv = context->returnvalue;
255  free(context);
256  cf_log(llevDebug, "*********** Execution complete ****************\n");
257 
258  return &rv;
259 }
260 
261 CF_PLUGIN void *eventListener(int *type, ...) {
262  static int rv = 0;
263  va_list args;
264  char *buf;
265  CFPContext *context;
266 
267  context = malloc(sizeof(CFPContext));
268 
269  context->message[0] = 0;
270 
271  va_start(args, type);
272 
273  context->who = va_arg(args, object *);
274  context->activator = va_arg(args, object *);
275  context->third = va_arg(args, object *);
276  buf = va_arg(args, char *);
277  if (buf != 0)
278  strcpy(context->message, buf);
279  context->fix = va_arg(args, int);
280  context->event = va_arg(args, object *);
281  context->event_code = context->event->subtype;
282  strncpy(context->options, context->event->name, sizeof(context->options));
283  context->returnvalue = 0;
284  va_end(args);
285 
286  pushContext(context);
287  /* Put your plugin action(s) here */
288  context = popContext();
289  rv = context->returnvalue;
290  free(context);
291  cf_log(llevDebug, "Execution complete");
292  return &rv;
293 }
294 
296  cf_log(llevDebug, PLUGIN_VERSION " closing\n");
297  return 0;
298 }
Definition: player.h:146
PyObject * third
Definition: cfpython.h:120
#define EVENT_REMOVE
Definition: plugin.h:89
#define EVENT_SHOUT
Definition: plugin.h:90
CF_PLUGIN void * eventListener(int *type,...)
CFPContext * current_context
int event_code
Definition: cfpython.h:124
#define EVENT_KICK
Definition: plugin.h:93
#define EVENT_LOGIN
Definition: plugin.h:84
PyObject * activator
Definition: cfpython.h:119
#define EVENT_MAPLEAVE
Definition: plugin.h:87
char options[1024]
Definition: cfpython.h:126
#define EVENT_LOGOUT
Definition: plugin.h:85
void *(* f_plug_api)(int *type,...)
Definition: plugin.h:121
char message[1024]
Definition: cfpython.h:122
#define EVENT_TELL
Definition: plugin.h:91
#define EVENT_CRASH
Definition: plugin.h:81
CF_PLUGIN int postInitPlugin(void)
CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr)
CF_PLUGIN int runPluginCommand(object *op, char *params)
void cf_log(LogLevel logLevel, const char *format,...)
#define EVENT_MAPENTER
Definition: plugin.h:86
int cf_init_plugin(f_plug_api getHooks)
PyObject * event
Definition: cfpython.h:121
#define PLUGIN_NAME
Definition: cfanim.h:32
CFPContext * context_stack
CF_PLUGIN int closePlugin(void)
static int current_command
void pushContext(CFPContext *context)
PyObject * who
Definition: cfpython.h:118
#define EVENT_MAPRESET
Definition: plugin.h:88
#define EVENT_GKILL
Definition: plugin.h:83
object * ob
Definition: player.h:207
#define EVENT_CLOCK
Definition: plugin.h:80
CF_PLUGIN void * globalEventListener(int *type,...)
int snprintf(char *dest, int max, const char *format,...)
Definition: porting.c:498
#define CF_PLUGIN
Definition: plugin_common.h:35
CFPContext * popContext(void)
void initContextStack(void)
CF_PLUGIN void * getPluginProperty(int *type,...)
struct _cfpcontext * down
Definition: cfpython.h:117
#define EVENT_PLAYER_DEATH
Definition: plugin.h:82
int returnvalue
Definition: cfpython.h:127
#define EVENT_BORN
Definition: plugin.h:79
#define EVENT_MUZZLE
Definition: plugin.h:92
#define PLUGIN_VERSION
Definition: cfanim.h:33