server/src/kernel/event.c

261 lines
5.6 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
#include <platform.h>
#include "event.h"
#include "attrib.h"
2010-08-08 10:06:34 +02:00
/* util includes */
2018-09-29 13:21:46 +02:00
#include <kernel/gamedata.h>
#include <util/log.h>
#include <util/strings.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
/* 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(1, sizeof(trigger));
if (!t) abort();
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;
static void init_handler(variant *var)
2011-03-07 08:02:35 +01:00
{
var->v = calloc(1, sizeof(handler_info));
2010-08-08 10:06:34 +02:00
}
static void free_handler(variant *var)
2011-03-07 08:02:35 +01:00
{
handler_info *hi = (handler_info *)var->v;
free_triggers(hi->triggers);
free(hi->event);
free(hi);
2010-08-08 10:06:34 +02:00
}
static void
write_handler(const variant *var, const void *owner, struct storage *store)
2010-08-08 10:06:34 +02:00
{
handler_info *hi = (handler_info *)var->v;
WRITE_TOK(store, hi->event);
write_triggers(store, hi->triggers);
2010-08-08 10:06:34 +02:00
}
static int read_handler(variant *var, 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 *)var->v;
2011-03-07 08:02:35 +01:00
READ_TOK(store, zText, sizeof(zText));
hi->event = str_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 = str_strdup(eventname);
2011-03-07 08:02:35 +01:00
}
assert(td);
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
**/