refactor: pass function name to callback, calculate it in call_spell instead.

This commit is contained in:
Enno Rehling 2017-04-30 18:41:30 +02:00
parent bd9968c266
commit 724a41ac85
4 changed files with 17 additions and 15 deletions

View File

@ -157,23 +157,12 @@ static void push_param(lua_State * L, char c, spllprm * param)
} }
/** callback to use lua for spell functions */ /** 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; lua_State *L = (lua_State *)global.vm_state;
const char *fname = co->sp->sname;
unit *caster = co_get_caster(co); unit *caster = co_get_caster(co);
region * r = co_get_region(co); region * r = co_get_region(co);
int result = -1; 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); lua_getglobal(L, fname);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {

View File

@ -26,7 +26,7 @@ extern "C" {
struct castorder; struct castorder;
extern struct callbacks { extern struct callbacks {
int (*cast_spell)(struct castorder *co); int (*cast_spell)(struct castorder *co, const char *fname);
} callbacks; } callbacks;
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1399,7 +1399,7 @@ static int parse_spells(xmlDocPtr doc)
if (result->nodesetval->nodeNr == 0) { if (result->nodesetval->nodeNr == 0) {
cast = get_function(sp->sname); cast = get_function(sp->sname);
if (!cast) { 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 { else {

View File

@ -3006,11 +3006,24 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan
int cast_spell(struct castorder *co) 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; const spell *sp = co->sp;
if (sp->cast_fun) { if (sp->cast_fun) {
return sp->cast_fun(co); 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; static critbit_tree cb_spellbooks;