Fix register_ore ore_type error handling

This commit is contained in:
cx384 2024-11-15 11:37:17 +01:00 committed by GitHub
parent 0c3117f9b3
commit d4378a74d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 18 deletions

@ -172,9 +172,8 @@ public:
return new OreVein; return new OreVein;
case ORE_STRATUM: case ORE_STRATUM:
return new OreStratum; return new OreStratum;
default:
return nullptr;
} }
return nullptr;
} }
void clear(); void clear();

@ -1323,20 +1323,20 @@ int ModApiMapgen::l_register_ore(lua_State *L)
BiomeManager *bmgr = emerge->getWritableBiomeManager(); BiomeManager *bmgr = emerge->getWritableBiomeManager();
OreManager *oremgr = emerge->getWritableOreManager(); OreManager *oremgr = emerge->getWritableOreManager();
enum OreType oretype = (OreType)getenumfield(L, index, int oretype_int;
"ore_type", es_OreType, ORE_SCATTER); std::string oretype_string = getstringfield_default(L, index, "ore_type", "nil");
Ore *ore = oremgr->create(oretype); if (!string_to_enum(es_OreType, oretype_int, oretype_string)) {
if (!ore) { throw LuaError("register_ore: unknown oretype \"" + oretype_string + "\"");
errorstream << "register_ore: ore_type " << oretype << " not implemented\n";
return 0;
} }
enum OreType oretype = (OreType) oretype_int;
std::unique_ptr<Ore> ore(oremgr->create(oretype));
ore->name = getstringfield_default(L, index, "name", ""); ore->name = getstringfield_default(L, index, "name", "");
ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0); ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1); ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1); ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
ore->clust_size = getintfield_default(L, index, "clust_size", 0); ore->clust_size = getintfield_default(L, index, "clust_size", 0);
ore->noise = NULL; ore->noise = nullptr;
ore->flags = 0; ore->flags = 0;
//// Get noise_threshold //// 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) { if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
errorstream << "register_ore: clust_scarcity and clust_num_ores" errorstream << "register_ore: clust_scarcity and clust_num_ores"
"must be greater than 0" << std::endl; "must be greater than 0" << std::endl;
delete ore;
return 0; return 0;
} }
//// Get flags //// 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) //// Get biomes associated with this decoration (if any)
lua_getfield(L, index, "biomes"); lua_getfield(L, index, "biomes");
@ -1394,7 +1393,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
//// Get type-specific parameters //// Get type-specific parameters
switch (oretype) { switch (oretype) {
case ORE_SHEET: { case ORE_SHEET: {
OreSheet *oresheet = (OreSheet *)ore; OreSheet *oresheet = (OreSheet *)ore.get();
oresheet->column_height_min = getintfield_default(L, index, oresheet->column_height_min = getintfield_default(L, index,
"column_height_min", 1); "column_height_min", 1);
@ -1406,7 +1405,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
break; break;
} }
case ORE_PUFF: { case ORE_PUFF: {
OrePuff *orepuff = (OrePuff *)ore; OrePuff *orepuff = (OrePuff *)ore.get();
lua_getfield(L, index, "np_puff_top"); lua_getfield(L, index, "np_puff_top");
read_noiseparams(L, -1, &orepuff->np_puff_top); read_noiseparams(L, -1, &orepuff->np_puff_top);
@ -1419,7 +1418,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
break; break;
} }
case ORE_VEIN: { case ORE_VEIN: {
OreVein *orevein = (OreVein *)ore; OreVein *orevein = (OreVein *)ore.get();
orevein->random_factor = getfloatfield_default(L, index, orevein->random_factor = getfloatfield_default(L, index,
"random_factor", 1.f); "random_factor", 1.f);
@ -1427,7 +1426,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
break; break;
} }
case ORE_STRATUM: { case ORE_STRATUM: {
OreStratum *orestratum = (OreStratum *)ore; OreStratum *orestratum = (OreStratum *)ore.get();
lua_getfield(L, index, "np_stratum_thickness"); lua_getfield(L, index, "np_stratum_thickness");
if (read_noiseparams(L, -1, &orestratum->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; break;
} }
ObjDefHandle handle = oremgr->add(ore); ObjDefHandle handle = oremgr->add(ore.get());
if (handle == OBJDEF_INVALID_HANDLE) { if (handle == OBJDEF_INVALID_HANDLE) {
delete ore;
return 0; return 0;
} }
@ -1454,7 +1452,10 @@ int ModApiMapgen::l_register_ore(lua_State *L)
size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames); size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
ore->m_nnlistsizes.push_back(nnames); 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); lua_pushinteger(L, handle);
return 1; return 1;