2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "event.h"
|
2018-09-29 11:37:17 +02:00
|
|
|
#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>
|
2018-09-29 11:37:17 +02:00
|
|
|
#include <util/log.h>
|
|
|
|
#include <util/strings.h>
|
2017-12-28 18:29:40 +01:00
|
|
|
|
|
|
|
#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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
WRITE_TOK(store, "end");
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-02-13 15:25:07 +01:00
|
|
|
int read_triggers(struct gamedata *data, trigger ** tp)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
for (;;) {
|
|
|
|
trigger_type *ttype;
|
|
|
|
char zText[128];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-02-13 15:25:07 +01:00
|
|
|
READ_TOK(data->store, zText, sizeof(zText));
|
2015-01-30 20:37:14 +01:00
|
|
|
if (!strcmp(zText, "end"))
|
|
|
|
break;
|
|
|
|
ttype = tt_find(zText);
|
|
|
|
assert(ttype || !"unknown trigger-type");
|
|
|
|
*tp = t_new(ttype);
|
|
|
|
if (ttype->read) {
|
2016-02-13 15:25:07 +01:00
|
|
|
int i = ttype->read(*tp, data);
|
2015-01-30 20:37:14 +01:00
|
|
|
switch (i) {
|
|
|
|
case AT_READ_OK:
|
|
|
|
tp = &(*tp)->next;
|
|
|
|
break;
|
|
|
|
case AT_READ_FAIL:
|
|
|
|
t_free(*tp);
|
2016-03-10 13:27:08 +01:00
|
|
|
free(*tp);
|
2015-01-30 20:37:14 +01:00
|
|
|
*tp = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(!"invalid return value");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01: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
|
|
|
{
|
2018-12-15 19:38:40 +01:00
|
|
|
trigger *t = calloc(1, sizeof(trigger));
|
|
|
|
if (!t) abort();
|
2015-01-30 20:37:14 +01:00
|
|
|
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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
while (triggers) {
|
|
|
|
trigger *t = triggers;
|
|
|
|
triggers = t->next;
|
|
|
|
t_free(t);
|
2015-10-14 20:29:07 +02:00
|
|
|
free(t);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
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
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
trigger **tp = triggers;
|
|
|
|
while (*tp) {
|
|
|
|
trigger *t = *tp;
|
|
|
|
if (t->type->handle(t, param) != 0) {
|
|
|
|
*tp = t->next;
|
|
|
|
t_free(t);
|
2016-03-10 13:27:08 +01:00
|
|
|
free(t);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
tp = &t->next;
|
|
|
|
}
|
|
|
|
return (*triggers != NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
** at_eventhandler
|
|
|
|
**/
|
|
|
|
|
|
|
|
typedef struct handler_info {
|
2015-01-30 20:37:14 +01:00
|
|
|
char *event;
|
|
|
|
trigger *triggers;
|
2010-08-08 10:06:34 +02:00
|
|
|
} handler_info;
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static void init_handler(variant *var)
|
2011-03-07 08:02:35 +01:00
|
|
|
{
|
2018-12-15 19:38:40 +01:00
|
|
|
var->v = calloc(1, sizeof(handler_info));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static void free_handler(variant *var)
|
2011-03-07 08:02:35 +01:00
|
|
|
{
|
2018-02-09 21:20:43 +01:00
|
|
|
handler_info *hi = (handler_info *)var->v;
|
2015-01-30 20:37:14 +01:00
|
|
|
free_triggers(hi->triggers);
|
|
|
|
free(hi->event);
|
|
|
|
free(hi);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-02-09 21:20:43 +01:00
|
|
|
write_handler(const variant *var, const void *owner, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2018-02-09 21:20:43 +01:00
|
|
|
handler_info *hi = (handler_info *)var->v;
|
2015-01-30 20:37:14 +01:00
|
|
|
WRITE_TOK(store, hi->event);
|
|
|
|
write_triggers(store, hi->triggers);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static int read_handler(variant *var, void *owner, gamedata *data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2016-02-13 13:42:02 +01:00
|
|
|
struct storage *store = data->store;
|
2015-01-30 20:37:14 +01:00
|
|
|
char zText[128];
|
2018-02-09 21:20:43 +01:00
|
|
|
handler_info *hi = (handler_info *)var->v;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
READ_TOK(store, zText, sizeof(zText));
|
2017-12-28 18:29:40 +01:00
|
|
|
hi->event = str_strdup(zText);
|
2016-02-13 15:25:07 +01:00
|
|
|
read_triggers(data, &hi->triggers);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (hi->triggers != NULL) {
|
|
|
|
return AT_READ_OK;
|
|
|
|
}
|
|
|
|
return AT_READ_FAIL;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
attrib_type at_eventhandler = {
|
2015-01-30 20:37:14 +01:00
|
|
|
"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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
}
|
2015-01-30 20:37:14 +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
|
|
|
{
|
2015-01-30 20:37:14 +01: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;
|
2017-12-28 18:29:40 +01:00
|
|
|
td->event = str_strdup(eventname);
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2018-12-15 19:38:40 +01:00
|
|
|
assert(td);
|
2015-01-30 20:37:14 +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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
trigger_type *tt = triggertypes;
|
2016-11-23 19:19:04 +01:00
|
|
|
while (tt && strcmp(tt->name, name)!=0)
|
2015-01-30 20:37:14 +01:00
|
|
|
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,
|
2015-01-30 20:37:14 +01:00
|
|
|
const trigger_type * tt)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01: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);
|
2016-03-10 13:27:08 +01:00
|
|
|
free(t);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
tp = &t->next;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
** default events
|
|
|
|
**/
|