From d97ff8e6e62f132a340bd7f70e8184db61f6cc0e Mon Sep 17 00:00:00 2001 From: MowFord <131182600+MowFord@users.noreply.github.com> Date: Thu, 9 May 2024 14:04:25 -0500 Subject: [PATCH 1/2] Cleanly fail the GetFirstID lookup --- src/map/lua/luautils.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/map/lua/luautils.cpp b/src/map/lua/luautils.cpp index 052791ccc9a..b5ae85e2045 100644 --- a/src/map/lua/luautils.cpp +++ b/src/map/lua/luautils.cpp @@ -901,9 +901,18 @@ namespace luautils // Update GetFirstID to use this new lookup // clang-format off - lua.set_function("GetFirstID", [&](std::string const& name) -> uint32 + lua.set_function("GetFirstID", [&](std::string const& name) -> std::optional { - return lookup[name].front(); + if (lookup.find(name) != lookup.end()) + { + return lookup[name].front(); + } + else + { + ShowWarning(fmt::format("GetFirstID({}) in zone {}: Returning nil", name, zoneName)); + + return std::nullopt; + } }); std::unordered_map idLuaTables; @@ -926,7 +935,7 @@ namespace luautils // If we have no entries, bail out and return nil if (lookup.find(name) == lookup.end()) { - ShowWarning(fmt::format("GetTableOfIDs({}): Returning nil", name)); + ShowWarning(fmt::format("GetTableOfIDs({}) in zone {}: Returning nil", name, zoneName)); return sol::lua_nil; } @@ -934,7 +943,7 @@ namespace luautils if (entriesVec.empty()) { - ShowWarning(fmt::format("GetTableOfIDs({}): Returning empty table", name)); + ShowWarning(fmt::format("GetTableOfIDs({}) in zone {}: Returning empty table", name, zoneName)); return table; } @@ -964,7 +973,7 @@ namespace luautils if (table.empty()) { - ShowWarning(fmt::format("Tried to look do ID lookup for {}, but found nothing.", name)); + ShowWarning(fmt::format("GetTableOfIDs({}) in zone {}: Tried to look do ID lookup, but found nothing.", name, zoneName)); } // Add to cache From 6d5667889cee50b21895c12dbaaffd07866a6f77 Mon Sep 17 00:00:00 2001 From: MowFord <131182600+MowFord@users.noreply.github.com> Date: Fri, 10 May 2024 08:22:36 -0500 Subject: [PATCH 2/2] Adjust PopulateIDLookups to throw errors on bad inputs --- src/map/lua/luautils.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/map/lua/luautils.cpp b/src/map/lua/luautils.cpp index b5ae85e2045..23accee1c3f 100644 --- a/src/map/lua/luautils.cpp +++ b/src/map/lua/luautils.cpp @@ -909,7 +909,7 @@ namespace luautils } else { - ShowWarning(fmt::format("GetFirstID({}) in zone {}: Returning nil", name, zoneName)); + ShowError(fmt::format("GetFirstID({}) in zone {}: Returning nil", name, zoneName)); return std::nullopt; } @@ -935,7 +935,7 @@ namespace luautils // If we have no entries, bail out and return nil if (lookup.find(name) == lookup.end()) { - ShowWarning(fmt::format("GetTableOfIDs({}) in zone {}: Returning nil", name, zoneName)); + ShowError(fmt::format("GetTableOfIDs({}) in zone {}: Returning nil", name, zoneName)); return sol::lua_nil; } @@ -943,7 +943,7 @@ namespace luautils if (entriesVec.empty()) { - ShowWarning(fmt::format("GetTableOfIDs({}) in zone {}: Returning empty table", name, zoneName)); + ShowError(fmt::format("GetTableOfIDs({}) in zone {}: Returning empty table", name, zoneName)); return table; } @@ -973,7 +973,7 @@ namespace luautils if (table.empty()) { - ShowWarning(fmt::format("GetTableOfIDs({}) in zone {}: Tried to look do ID lookup, but found nothing.", name, zoneName)); + ShowError(fmt::format("GetTableOfIDs({}) in zone {}: Tried to look do ID lookup, but found nothing.", name, zoneName)); } // Add to cache