File: plugin_template.c

package info (click to toggle)
crossfire 1.71.0%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 28,076 kB
  • sloc: ansic: 85,126; sh: 11,978; perl: 2,659; lex: 2,044; makefile: 1,271
file content (300 lines) | stat: -rw-r--r-- 9,933 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*****************************************************************************/
/* Template for version 2.0 plugins.                                         */
/* Contact: yann.chachkoff@myrealbox.com                                     */
/*****************************************************************************/
/* That code is placed under the GNU General Public Licence (GPL)            */
/* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints)     */
/*****************************************************************************/
/*  CrossFire, A Multiplayer game for X-windows                              */
/*                                                                           */
/*  Copyright (C) 2000 Mark Wedel                                            */
/*  Copyright (C) 1992 Frank Tore Johansen                                   */
/*                                                                           */
/*  This program is free software; you can redistribute it and/or modify     */
/*  it under the terms of the GNU General Public License as published by     */
/*  the Free Software Foundation; either version 2 of the License, or        */
/*  (at your option) any later version.                                      */
/*                                                                           */
/*  This program is distributed in the hope that it will be useful,          */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
/*  GNU General Public License for more details.                             */
/*                                                                           */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program; if not, write to the Free Software              */
/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
/*                                                                           */
/*****************************************************************************/

/* First let's include the header file needed                                */

#include <plugin_template.h>
#include <stdarg.h>
#ifndef __CEXTRACT__
#include <plugin_template_proto.h>
#endif

CFPContext *context_stack;

CFPContext *current_context;

static int current_command = -999;

void initContextStack(void) {
    current_context = NULL;
    context_stack = NULL;
}

void pushContext(CFPContext *context) {
    CFPContext *stack_context;

    if (current_context == NULL) {
        context_stack = context;
        context->down = NULL;
    } else {
        context->down = current_context;
    }
    current_context = context;
}

CFPContext *popContext(void) {
    CFPContext *oldcontext;

    if (current_context != NULL) {
        oldcontext = current_context;
        current_context = current_context->down;
        return oldcontext;
    } else
        return NULL;
}

CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
    cf_init_plugin(gethooksptr);

    cf_log(llevDebug, PLUGIN_VERSION " init\n");

    /* Place your initialization code here */
    return 0;
}

CF_PLUGIN void *getPluginProperty(int *type, ...) {
    va_list args;
    const char *propname;
    int i, size;
    command_array_struct *rtn_cmd;
    char *buf;

    va_start(args, type);
    propname = va_arg(args, const char *);

    if (!strcmp(propname, "command?")) {
        const char *cmdname;
        cmdname = va_arg(args, const char *);
        rtn_cmd = va_arg(args, command_array_struct *);
        va_end(args);

        /** Check if plugin handles custom command */
        return NULL;
    } else if (!strcmp(propname, "Identification")) {
        buf = va_arg(args, char *);
        size = va_arg(args, int);
        va_end(args);
        snprintf(buf, size, PLUGIN_NAME);
        return NULL;
    } else if (!strcmp(propname, "FullName")) {
        buf = va_arg(args, char *);
        size = va_arg(args, int);
        va_end(args);
        snprintf(buf, size, PLUGIN_VERSION);
        return NULL;
    }
    va_end(args);
    return NULL;
}

CF_PLUGIN int runPluginCommand(object *op, char *params) {
    return -1;
}

CF_PLUGIN int postInitPlugin(void) {
    cf_log(llevDebug, PLUGIN_VERSION" post init\n");
    initContextStack();
    /* Pick the global events you want to monitor from this plugin */
/*
    cf_system_register_global_event(EVENT_BORN, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_CLOCK, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_CRASH, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_PLAYER_DEATH, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_GKILL, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_LOGIN, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_LOGOUT, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_MAPENTER, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_MAPLEAVE, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_MAPRESET, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_TELL, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
    cf_system_register_global_event(EVENT_KICK, PLUGIN_NAME, globalEventListener);
*/
    return 0;
}

CF_PLUGIN void *globalEventListener(int *type, ...) {
    va_list args;
    static int rv = 0;
    CFPContext *context;
    context = malloc(sizeof(CFPContext));
    char *buf;
    player *pl;
    object *op;

    va_start(args, type);
    context->event_code = va_arg(args, int);
    printf("****** Global event listener called ***********\n");
    printf("- Event code: %d\n", context->event_code);

    context->message[0] = 0;

    context->who = NULL;
    context->activator = NULL;
    context->third = NULL;
    context->event = NULL;
    rv = context->returnvalue = 0;
    switch (context->event_code) {
    case EVENT_CRASH:
        printf("Unimplemented for now\n");
        break;

    case EVENT_BORN:
        context->activator = va_arg(args, object *);
        break;

    case EVENT_PLAYER_DEATH:
        context->who = va_arg(args, object *);
        context->activator = va_arg(args, object *);
        break;

    case EVENT_GKILL:
        context->who = va_arg(args, object *);
        context->activator = va_arg(args, object *);
        break;

    case EVENT_LOGIN:
        pl = va_arg(args, player *);
        context->activator = pl->ob;
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        break;

    case EVENT_LOGOUT:
        pl = va_arg(args, player *);
        context->activator = pl->ob;
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        break;

    case EVENT_REMOVE:
        context->activator = va_arg(args, object *);
        break;

    case EVENT_SHOUT:
        context->activator = va_arg(args, object *);
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        break;

    case EVENT_MUZZLE:
        context->activator = va_arg(args, object *);
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        break;

    case EVENT_KICK:
        context->activator = va_arg(args, object *);
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        break;

    case EVENT_MAPENTER:
        context->activator = va_arg(args, object *);
        break;

    case EVENT_MAPLEAVE:
        context->activator = va_arg(args, object *);
        break;

    case EVENT_CLOCK:
        break;

    case EVENT_MAPRESET:
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        break;

    case EVENT_TELL:
        context->activator = va_arg(args, object *);
        buf = va_arg(args, char *);
        if (buf != 0)
            strcpy(context->message, buf);
        context->third = va_arg(args, object *);
        break;
    }
    va_end(args);
    context->returnvalue = 0;

    pushContext(context);
    /* Put your plugin action(s) here */
    context = popContext();
    rv = context->returnvalue;
    free(context);
    cf_log(llevDebug, "*********** Execution complete ****************\n");

    return &rv;
}

CF_PLUGIN void *eventListener(int *type, ...) {
    static int rv = 0;
    va_list args;
    char *buf;
    CFPContext *context;

    context = malloc(sizeof(CFPContext));

    context->message[0] = 0;

    va_start(args, type);

    context->who = va_arg(args, object *);
    context->activator = va_arg(args, object *);
    context->third = va_arg(args, object *);
    buf = va_arg(args, char *);
    if (buf != 0)
        strcpy(context->message, buf);
    context->fix = va_arg(args, int);
    context->event = va_arg(args, object *);
    context->event_code = context->event->subtype;
    strncpy(context->options, context->event->name, sizeof(context->options));
    context->returnvalue = 0;
    va_arg(args, talk_info *); /* ignored for now */
    va_end(args);

    pushContext(context);
    /* Put your plugin action(s) here */
    context = popContext();
    rv = context->returnvalue;
    free(context);
    cf_log(llevDebug, "Execution complete");
    return &rv;
}

CF_PLUGIN int closePlugin(void) {
    cf_log(llevDebug, PLUGIN_VERSION " closing\n");
    return 0;
}