server/src/util/event.c

275 lines
6.4 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
2015-01-30 22:10:29 +01:00
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
2010-08-08 10:06:34 +02:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include "event.h"
/* util includes */
#include "attrib.h"
#include "gamedata.h"
2010-08-08 10:06:34 +02:00
#include "log.h"
#include "storage.h"
/* libc includes */
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
2011-03-07 08:02:35 +01:00
void write_triggers(struct storage *store, const trigger * t)
2010-08-08 10:06:34 +02:00
{
while (t) {
if (t->type->write) {
WRITE_TOK(store, t->type->name);
t->type->write(t, store);
}
t = t->next;
2010-08-08 10:06:34 +02:00
}
WRITE_TOK(store, "end");
2010-08-08 10:06:34 +02:00
}
int read_triggers(struct gamedata *data, trigger ** tp)
2010-08-08 10:06:34 +02:00
{
for (;;) {
trigger_type *ttype;
char zText[128];
2010-08-08 10:06:34 +02:00
READ_TOK(data->store, zText, sizeof(zText));
if (!strcmp(zText, "end"))
break;
ttype = tt_find(zText);
assert(ttype || !"unknown trigger-type");
*tp = t_new(ttype);
if (ttype->read) {
int i = ttype->read(*tp, data);
switch (i) {
case AT_READ_OK:
tp = &(*tp)->next;
break;
case AT_READ_FAIL:
t_free(*tp);
free(*tp);
*tp = NULL;
break;
default:
assert(!"invalid return value");
break;
}
}
2010-08-08 10:06:34 +02:00
}
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
trigger *t_new(trigger_type * ttype)
2010-08-08 10:06:34 +02:00
{
trigger *t = calloc(sizeof(trigger), 1);
t->type = ttype;
if (ttype->initialize)
ttype->initialize(t);
return t;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void t_free(trigger * t)
2010-08-08 10:06:34 +02:00
{
if (t->type->finalize)
t->type->finalize(t);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void free_triggers(trigger * triggers)
2010-08-08 10:06:34 +02:00
{
while (triggers) {
trigger *t = triggers;
triggers = t->next;
t_free(t);
2015-10-14 20:29:07 +02:00
free(t);
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int handle_triggers(trigger ** triggers, void *param)
2010-08-08 10:06:34 +02:00
{
trigger **tp = triggers;
while (*tp) {
trigger *t = *tp;
if (t->type->handle(t, param) != 0) {
*tp = t->next;
t_free(t);
free(t);
}
else
tp = &t->next;
}
return (*triggers != NULL);
2010-08-08 10:06:34 +02:00
}
/***
** at_eventhandler
**/
typedef struct handler_info {
char *event;
trigger *triggers;
2010-08-08 10:06:34 +02:00
} handler_info;
2011-03-07 08:02:35 +01:00
static void init_handler(attrib * a)
{
a->data.v = calloc(sizeof(handler_info), 1);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void free_handler(attrib * a)
{
handler_info *hi = (handler_info *)a->data.v;
free_triggers(hi->triggers);
free(hi->event);
free(hi);
2010-08-08 10:06:34 +02:00
}
static void
2011-03-07 08:02:35 +01:00
write_handler(const attrib * a, const void *owner, struct storage *store)
2010-08-08 10:06:34 +02:00
{
handler_info *hi = (handler_info *)a->data.v;
WRITE_TOK(store, hi->event);
write_triggers(store, hi->triggers);
2010-08-08 10:06:34 +02:00
}
static int read_handler(attrib * a, void *owner, gamedata *data)
2010-08-08 10:06:34 +02:00
{
struct storage *store = data->store;
char zText[128];
handler_info *hi = (handler_info *)a->data.v;
2011-03-07 08:02:35 +01:00
READ_TOK(store, zText, sizeof(zText));
hi->event = strdup(zText);
read_triggers(data, &hi->triggers);
if (hi->triggers != NULL) {
return AT_READ_OK;
}
return AT_READ_FAIL;
2010-08-08 10:06:34 +02:00
}
attrib_type at_eventhandler = {
"eventhandler",
init_handler,
free_handler,
NULL,
write_handler,
read_handler
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
struct trigger **get_triggers(struct attrib *ap, const char *eventname)
2010-08-08 10:06:34 +02:00
{
handler_info *td = NULL;
attrib *a = a_find(ap, &at_eventhandler);
while (a != NULL && a->type == &at_eventhandler) {
td = (handler_info *)a->data.v;
if (strcmp(td->event, eventname) == 0) {
return &td->triggers;
}
a = a->next;
2011-03-07 08:02:35 +01:00
}
return NULL;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void add_trigger(struct attrib **ap, const char *eventname, struct trigger *t)
2010-08-08 10:06:34 +02:00
{
trigger **tp;
handler_info *td = NULL;
attrib *a = a_find(*ap, &at_eventhandler);
assert(t->next == NULL);
while (a != NULL && a->type == &at_eventhandler) {
td = (handler_info *)a->data.v;
if (!strcmp(td->event, eventname)) {
break;
}
a = a->next;
}
if (a == NULL || a->type != &at_eventhandler) {
a = a_add(ap, a_new(&at_eventhandler));
td = (handler_info *)a->data.v;
td->event = strdup(eventname);
2011-03-07 08:02:35 +01:00
}
tp = &td->triggers;
while (*tp)
tp = &(*tp)->next;
*tp = t;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void handle_event(attrib * attribs, const char *eventname, void *data)
2010-08-08 10:06:34 +02:00
{
while (attribs) {
if (attribs->type == &at_eventhandler)
break;
attribs = attribs->nexttype;
}
while (attribs && attribs->type == &at_eventhandler) {
handler_info *tl = (handler_info *)attribs->data.v;
if (!strcmp(tl->event, eventname)) {
handler_info *tl = (handler_info *)attribs->data.v;
handle_triggers(&tl->triggers, data);
break;
}
attribs = attribs->next;
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
void t_add(struct trigger **tlist, struct trigger *t)
2010-08-08 10:06:34 +02:00
{
while (*tlist)
tlist = &(*tlist)->next;
*tlist = t;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static trigger_type *triggertypes;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
void tt_register(trigger_type * tt)
2010-08-08 10:06:34 +02:00
{
tt->next = triggertypes;
triggertypes = tt;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
trigger_type *tt_find(const char *name)
2010-08-08 10:06:34 +02:00
{
trigger_type *tt = triggertypes;
2016-11-23 19:19:04 +01:00
while (tt && strcmp(tt->name, name)!=0)
tt = tt->next;
return tt;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void
remove_triggers(struct attrib **ap, const char *eventname,
const trigger_type * tt)
2010-08-08 10:06:34 +02:00
{
trigger **tp = get_triggers(*ap, eventname);
if (tp == NULL)
return;
while (*tp) {
/* first, remove all gate-triggers */
trigger *t = *tp;
if (t->type == tt) {
*tp = t->next;
t_free(t);
free(t);
}
else
tp = &t->next;
}
2010-08-08 10:06:34 +02:00
}
/***
** default events
**/