From d4378a74d3c593c9dd4dfbba30120049e8128102 Mon Sep 17 00:00:00 2001 From: cx384 Date: Fri, 15 Nov 2024 11:37:17 +0100 Subject: [PATCH] Fix register_ore ore_type error handling --- src/mapgen/mg_ore.h | 3 +-- src/script/lua_api/l_mapgen.cpp | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mapgen/mg_ore.h b/src/mapgen/mg_ore.h index 435933046..eed13ebfc 100644 --- a/src/mapgen/mg_ore.h +++ b/src/mapgen/mg_ore.h @@ -172,9 +172,8 @@ public: return new OreVein; case ORE_STRATUM: return new OreStratum; - default: - return nullptr; } + return nullptr; } void clear(); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index c8b412c73..89323b2f6 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -1323,20 +1323,20 @@ int ModApiMapgen::l_register_ore(lua_State *L) BiomeManager *bmgr = emerge->getWritableBiomeManager(); OreManager *oremgr = emerge->getWritableOreManager(); - enum OreType oretype = (OreType)getenumfield(L, index, - "ore_type", es_OreType, ORE_SCATTER); - Ore *ore = oremgr->create(oretype); - if (!ore) { - errorstream << "register_ore: ore_type " << oretype << " not implemented\n"; - return 0; + int oretype_int; + std::string oretype_string = getstringfield_default(L, index, "ore_type", "nil"); + if (!string_to_enum(es_OreType, oretype_int, oretype_string)) { + throw LuaError("register_ore: unknown oretype \"" + oretype_string + "\""); } + enum OreType oretype = (OreType) oretype_int; + std::unique_ptr ore(oremgr->create(oretype)); ore->name = getstringfield_default(L, index, "name", ""); ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0); ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1); ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1); ore->clust_size = getintfield_default(L, index, "clust_size", 0); - ore->noise = NULL; + ore->noise = nullptr; ore->flags = 0; //// Get noise_threshold @@ -1368,12 +1368,11 @@ int ModApiMapgen::l_register_ore(lua_State *L) if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) { errorstream << "register_ore: clust_scarcity and clust_num_ores" "must be greater than 0" << std::endl; - delete ore; return 0; } //// Get flags - getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL); + getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, nullptr); //// Get biomes associated with this decoration (if any) lua_getfield(L, index, "biomes"); @@ -1394,7 +1393,7 @@ int ModApiMapgen::l_register_ore(lua_State *L) //// Get type-specific parameters switch (oretype) { case ORE_SHEET: { - OreSheet *oresheet = (OreSheet *)ore; + OreSheet *oresheet = (OreSheet *)ore.get(); oresheet->column_height_min = getintfield_default(L, index, "column_height_min", 1); @@ -1406,7 +1405,7 @@ int ModApiMapgen::l_register_ore(lua_State *L) break; } case ORE_PUFF: { - OrePuff *orepuff = (OrePuff *)ore; + OrePuff *orepuff = (OrePuff *)ore.get(); lua_getfield(L, index, "np_puff_top"); read_noiseparams(L, -1, &orepuff->np_puff_top); @@ -1419,7 +1418,7 @@ int ModApiMapgen::l_register_ore(lua_State *L) break; } case ORE_VEIN: { - OreVein *orevein = (OreVein *)ore; + OreVein *orevein = (OreVein *)ore.get(); orevein->random_factor = getfloatfield_default(L, index, "random_factor", 1.f); @@ -1427,7 +1426,7 @@ int ModApiMapgen::l_register_ore(lua_State *L) break; } case ORE_STRATUM: { - OreStratum *orestratum = (OreStratum *)ore; + OreStratum *orestratum = (OreStratum *)ore.get(); lua_getfield(L, index, "np_stratum_thickness"); if (read_noiseparams(L, -1, &orestratum->np_stratum_thickness)) @@ -1443,9 +1442,8 @@ int ModApiMapgen::l_register_ore(lua_State *L) break; } - ObjDefHandle handle = oremgr->add(ore); + ObjDefHandle handle = oremgr->add(ore.get()); if (handle == OBJDEF_INVALID_HANDLE) { - delete ore; return 0; } @@ -1454,7 +1452,10 @@ int ModApiMapgen::l_register_ore(lua_State *L) size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames); ore->m_nnlistsizes.push_back(nnames); - ndef->pendNodeResolve(ore); + ndef->pendNodeResolve(ore.get()); + + // We passed ownership of the ore object to oremgr earlier. + ore.release(); lua_pushinteger(L, handle); return 1;