From 7a367b10adc072312eb7f83303012a1bcf7b0d55 Mon Sep 17 00:00:00 2001 From: mjn-mixael Date: Sat, 11 Nov 2023 11:30:07 -0600 Subject: [PATCH 1/2] Add ability to remove an enum item from a list --- code/scripting/api/objs/LuaEnum.cpp | 33 ++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/code/scripting/api/objs/LuaEnum.cpp b/code/scripting/api/objs/LuaEnum.cpp index a583acd42a6..0c1d1faf85d 100644 --- a/code/scripting/api/objs/LuaEnum.cpp +++ b/code/scripting/api/objs/LuaEnum.cpp @@ -39,7 +39,7 @@ ADE_INDEXER(l_LuaEnum, "number Index", "Array of enum items", "string", "enum it return ADE_RETURN_NIL; idx--; //Convert from lua 1 based index - if ((idx < 0) || (idx >= (int)lua_enum.getEnum()->list.size())) + if ((idx < 0) || (idx >= static_cast(lua_enum.getEnum()->list.size()))) return ADE_RETURN_NIL; return ade_set_args(L, "s", lua_enum.getEnum()->list[idx].c_str()); @@ -51,7 +51,7 @@ ADE_FUNC(__len, l_LuaEnum, nullptr, "The number of Lua enum items", "number", "T if (!ade_get_args(L, "o", l_LuaEnum.Get(&lua_enum))) return ade_set_error(L, "i", 0); - return ade_set_args(L, "i", (int)lua_enum.getEnum()->list.size()); + return ade_set_args(L, "i", static_cast(lua_enum.getEnum()->list.size())); } ADE_FUNC(addEnumItem, @@ -66,7 +66,7 @@ ADE_FUNC(addEnumItem, if (!ade_get_args(L, "os", l_LuaEnum.Get(&lua_enum) , &item_name)) return ADE_RETURN_FALSE; - for (int i = 0; i < (int)lua_enum.getEnum()->list.size(); i++) { + for (size_t i = 0; i < lua_enum.getEnum()->list.size(); i++) { if (!stricmp(item_name, lua_enum.getEnum()->list[i].c_str())) { return ADE_RETURN_FALSE; } @@ -77,5 +77,32 @@ ADE_FUNC(addEnumItem, return ADE_RETURN_TRUE; } +ADE_FUNC(removeEnumItem, + l_LuaEnum, + "string itemname", + "Removes an enum item with the given string.", + "boolean", + "Returns true if successful, false otherwise") +{ + lua_enum_h lua_enum; + const char* item_name; + if (!ade_get_args(L, "os", l_LuaEnum.Get(&lua_enum), &item_name)) + return ADE_RETURN_FALSE; + + size_t i; + + for (i = 0; i < lua_enum.getEnum()->list.size(); i++) { + if (stricmp(item_name, lua_enum.getEnum()->list[i].c_str())) { + return ADE_RETURN_FALSE; + } else { + break; + } + } + + lua_enum.getEnum()->list.erase(lua_enum.getEnum()->list.begin() + i); + + return ADE_RETURN_TRUE; +} + } // namespace api } // namespace scripting From b628b665cf7dfa5a80332264fb98a24983239d7b Mon Sep 17 00:00:00 2001 From: mjn-mixael Date: Sat, 11 Nov 2023 11:51:45 -0600 Subject: [PATCH 2/2] Do this using brain --- code/scripting/api/objs/LuaEnum.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/code/scripting/api/objs/LuaEnum.cpp b/code/scripting/api/objs/LuaEnum.cpp index 0c1d1faf85d..285c6371023 100644 --- a/code/scripting/api/objs/LuaEnum.cpp +++ b/code/scripting/api/objs/LuaEnum.cpp @@ -92,16 +92,15 @@ ADE_FUNC(removeEnumItem, size_t i; for (i = 0; i < lua_enum.getEnum()->list.size(); i++) { - if (stricmp(item_name, lua_enum.getEnum()->list[i].c_str())) { - return ADE_RETURN_FALSE; - } else { - break; + if (!stricmp(item_name, lua_enum.getEnum()->list[i].c_str())) { + lua_enum.getEnum()->list.erase(lua_enum.getEnum()->list.begin() + i); + return ADE_RETURN_TRUE; } } - lua_enum.getEnum()->list.erase(lua_enum.getEnum()->list.begin() + i); + - return ADE_RETURN_TRUE; + return ADE_RETURN_FALSE; } } // namespace api