From 724a41ac8537a3bfd78fc97cd8905f9a6129a8e7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 30 Apr 2017 18:41:30 +0200 Subject: [PATCH] refactor: pass function name to callback, calculate it in call_spell instead. --- src/helpers.c | 13 +------------ src/kernel/callbacks.h | 2 +- src/kernel/xmlreader.c | 2 +- src/magic.c | 15 ++++++++++++++- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/helpers.c b/src/helpers.c index 429c5d190..4e020cefe 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -157,23 +157,12 @@ static void push_param(lua_State * L, char c, spllprm * param) } /** callback to use lua for spell functions */ -static int lua_callspell(castorder * co) +static int lua_callspell(castorder * co, const char *fname) { lua_State *L = (lua_State *)global.vm_state; - const char *fname = co->sp->sname; unit *caster = co_get_caster(co); region * r = co_get_region(co); int result = -1; - const char *hashpos = strchr(fname, '#'); - char fbuf[64]; - - if (hashpos != NULL) { - ptrdiff_t len = hashpos - fname; - assert(len < (ptrdiff_t) sizeof(fbuf)); - memcpy(fbuf, fname, len); - fbuf[len] = '\0'; - fname = fbuf; - } lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { diff --git a/src/kernel/callbacks.h b/src/kernel/callbacks.h index 99db5e390..71f3900c4 100644 --- a/src/kernel/callbacks.h +++ b/src/kernel/callbacks.h @@ -26,7 +26,7 @@ extern "C" { struct castorder; extern struct callbacks { - int (*cast_spell)(struct castorder *co); + int (*cast_spell)(struct castorder *co, const char *fname); } callbacks; #ifdef __cplusplus diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 60c80dade..c0595b4f5 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1399,7 +1399,7 @@ static int parse_spells(xmlDocPtr doc) if (result->nodesetval->nodeNr == 0) { cast = get_function(sp->sname); if (!cast) { - log_error("no spell cast function registered for '%s'\n", sp->sname); + log_info("no spell cast function registered for '%s'\n", sp->sname); } } else { diff --git a/src/magic.c b/src/magic.c index 8db533923..d7bc63ca2 100644 --- a/src/magic.c +++ b/src/magic.c @@ -3006,11 +3006,24 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan int cast_spell(struct castorder *co) { + const char *fname = co->sp->sname; + const char *hashpos = strchr(fname, '#'); + char fbuf[64]; + const spell *sp = co->sp; if (sp->cast_fun) { return sp->cast_fun(co); } - return callbacks.cast_spell(co); + + if (hashpos != NULL) { + ptrdiff_t len = hashpos - fname; + assert(len < (ptrdiff_t) sizeof(fbuf)); + memcpy(fbuf, fname, len); + fbuf[len] = '\0'; + fname = fbuf; + } + + return callbacks.cast_spell(co, fname); } static critbit_tree cb_spellbooks;