From 7cab158af8f1438dd828b60f725901beddf64349 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 14 Mar 2021 19:09:24 +0100 Subject: [PATCH] try to fix some binding errors --- src/bind_eressea.c | 20 +++++++++++--------- src/bind_ship.c | 16 ++++++++++++++++ src/bind_unit.c | 21 ++++++++++++--------- src/bindings.c | 24 +++++++++++++----------- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/bind_eressea.c b/src/bind_eressea.c index ac04ce36b..c00a21163 100755 --- a/src/bind_eressea.c +++ b/src/bind_eressea.c @@ -51,16 +51,18 @@ int eressea_read_orders(const char * filename) { } int eressea_export_json(const char * filename, int flags) { - FILE *F = fopen(filename, "w"); - if (F) { - stream out = { 0 }; - int err; - fstream_init(&out, F); - err = json_export(&out, flags); - fstream_done(&out); - return err; + if (filename) { + FILE *F = fopen(filename, "w"); + if (F) { + stream out = { 0 }; + int err; + fstream_init(&out, F); + err = json_export(&out, flags); + fstream_done(&out); + return err; + } + perror(filename); } - perror(filename); return -1; } diff --git a/src/bind_ship.c b/src/bind_ship.c index bcd18c58f..891a991d9 100644 --- a/src/bind_ship.c +++ b/src/bind_ship.c @@ -125,6 +125,16 @@ static int tolua_ship_set_display(lua_State * L) static int tolua_ship_get_units(lua_State * L) { +#ifndef TOLUA_RELEASE +tolua_Error tolua_err; +if ( +!tolua_isusertype(L,1,"ship",0,&tolua_err) || +!tolua_isuserdata(L,2,0,&tolua_err) || +!tolua_isnoobj(L,3,&tolua_err) +) goto tolua_lerror; +else +#endif + { ship *sh = (ship *)tolua_tousertype(L, 1, NULL); unit **unit_ptr = (unit **)lua_newuserdata(L, sizeof(unit *)); unit *u = sh->region->units; @@ -138,6 +148,12 @@ static int tolua_ship_get_units(lua_State * L) lua_pushcclosure(L, tolua_unitlist_nexts, 1); return 1; + } +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(L, "#ferror in function 'export'.", &tolua_err); + return 0; +#endif } static int tolua_ship_create(lua_State * L) diff --git a/src/bind_unit.c b/src/bind_unit.c index 8f8aeefac..3b8b4c127 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -760,17 +760,20 @@ static int tolua_unit_clear_attribs(lua_State *L) { } static int tolua_unit_has_attrib(lua_State *L) { - unit *u = (unit *)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); - attrib * a = u->attribs; - while (a) { - if (strcmp(a->type->name, name) == 0) { - break; + unit *u = (unit *)tolua_tousertype(L, 1, NULL); + const char *name = tolua_tostring(L, 2, NULL); + if (u && name) { + attrib * a = u->attribs; + while (a) { + if (strcmp(a->type->name, name) == 0) { + break; + } + a = a->nexttype; } - a = a->nexttype; + lua_pushboolean(L, a != NULL); + return 1; } - lua_pushboolean(L, a != NULL); - return 1; + return 0; } static int tolua_unit_get_key(lua_State * L) diff --git a/src/bindings.c b/src/bindings.c index fdb2cd153..d420f92e7 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -293,17 +293,19 @@ static int tolua_create_curse(lua_State * L) ap = &target->attribs; } if (ap) { - const char *cname = tolua_tostring(L, 3, 0); - const curse_type *ctype = ct_find(cname); - if (ctype) { - float vigour = (float)tolua_tonumber(L, 4, 0); - int duration = (int)tolua_tonumber(L, 5, 0); - float effect = (float)tolua_tonumber(L, 6, 0); - int men = (int)tolua_tonumber(L, 7, 0); /* optional */ - curse *c = create_curse(u, ap, ctype, vigour, duration, effect, men); - if (c) { - tolua_pushboolean(L, true); - return 1; + const char *cname = tolua_tostring(L, 3, NULL); + if (cname) { + const curse_type *ctype = ct_find(cname); + if (ctype) { + float vigour = (float)tolua_tonumber(L, 4, 0); + int duration = (int)tolua_tonumber(L, 5, 0); + float effect = (float)tolua_tonumber(L, 6, 0); + int men = (int)tolua_tonumber(L, 7, 0); /* optional */ + curse *c = create_curse(u, ap, ctype, vigour, duration, effect, men); + if (c) { + tolua_pushboolean(L, true); + return 1; + } } } }