diff --git a/src/content/mods.cpp b/src/content/mods.cpp index 1c100f5a11563..5a9281f7d166c 100644 --- a/src/content/mods.cpp +++ b/src/content/mods.cpp @@ -15,6 +15,7 @@ #include "porting.h" #include "convert_json.h" #include "script/common/c_internal.h" +#include "exceptions.h" void ModSpec::checkAndLog() const { diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp index 980f0188cfcf1..e9e3a72d81e8f 100644 --- a/src/content/subgames.cpp +++ b/src/content/subgames.cpp @@ -12,6 +12,7 @@ #include "defaultsettings.h" // for set_default_settings #include "map_settings_manager.h" #include "util/string.h" +#include "exceptions.h" // The maximum number of identical world names allowed #define MAX_WORLD_NAMES 100 diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp index 21b529d0beeca..9402b477057c7 100644 --- a/src/gui/guiEngine.cpp +++ b/src/gui/guiEngine.cpp @@ -27,6 +27,7 @@ #include #include "client/imagefilters.h" #include "util/tracy_wrapper.h" +#include "script/common/c_types.h" // LuaError #if USE_SOUND #include "client/sound/sound_openal.h" diff --git a/src/hud.h b/src/hud.h index 214d1451248cd..2b02e998836f8 100644 --- a/src/hud.h +++ b/src/hud.h @@ -7,7 +7,7 @@ #include "irrlichttypes_bloated.h" #include -#include "common/c_types.h" +#include "util/enum_string.h" #define HUD_DIR_LEFT_RIGHT 0 #define HUD_DIR_RIGHT_LEFT 1 diff --git a/src/script/common/CMakeLists.txt b/src/script/common/CMakeLists.txt index 3e84b46c76a7a..f771861d7083e 100644 --- a/src/script/common/CMakeLists.txt +++ b/src/script/common/CMakeLists.txt @@ -1,7 +1,6 @@ set(common_SCRIPT_COMMON_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/c_content.cpp ${CMAKE_CURRENT_SOURCE_DIR}/c_converter.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/c_types.cpp ${CMAKE_CURRENT_SOURCE_DIR}/c_internal.cpp ${CMAKE_CURRENT_SOURCE_DIR}/c_packer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/helper.cpp diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index f6340ff291ef1..146609185441f 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -32,6 +32,23 @@ struct EnumString es_TileAnimationType[] = {0, nullptr}, }; +struct EnumString es_ItemType[] = +{ + {ITEM_NONE, "none"}, + {ITEM_NODE, "node"}, + {ITEM_CRAFT, "craft"}, + {ITEM_TOOL, "tool"}, + {0, NULL}, +}; + +struct EnumString es_TouchInteractionMode[] = +{ + {LONG_DIG_SHORT_PLACE, "long_dig_short_place"}, + {SHORT_DIG_LONG_PLACE, "short_dig_long_place"}, + {TouchInteractionMode_USER, "user"}, + {0, NULL}, +}; + /******************************************************************************/ void read_item_definition(lua_State* L, int index, const ItemDefinition &default_def, ItemDefinition &def) @@ -175,7 +192,7 @@ void push_item_definition(lua_State *L, const ItemDefinition &i) void push_item_definition_full(lua_State *L, const ItemDefinition &i) { - std::string type(es_ItemType[(int)i.type].str); + std::string type(enum_to_string(es_ItemType, i.type)); lua_newtable(L); lua_pushstring(L, i.name.c_str()); @@ -233,11 +250,11 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i) lua_createtable(L, 0, 3); const TouchInteraction &inter = i.touch_interaction; - lua_pushstring(L, es_TouchInteractionMode[inter.pointed_nothing].str); + lua_pushstring(L, enum_to_string(es_TouchInteractionMode, inter.pointed_nothing)); lua_setfield(L, -2,"pointed_nothing"); - lua_pushstring(L, es_TouchInteractionMode[inter.pointed_node].str); + lua_pushstring(L, enum_to_string(es_TouchInteractionMode, inter.pointed_node)); lua_setfield(L, -2,"pointed_node"); - lua_pushstring(L, es_TouchInteractionMode[inter.pointed_object].str); + lua_pushstring(L, enum_to_string(es_TouchInteractionMode, inter.pointed_object)); lua_setfield(L, -2,"pointed_object"); lua_setfield(L, -2, "touch_interaction"); } @@ -955,10 +972,10 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index) void push_content_features(lua_State *L, const ContentFeatures &c) { - std::string paramtype(ScriptApiNode::es_ContentParamType[(int)c.param_type].str); - std::string paramtype2(ScriptApiNode::es_ContentParamType2[(int)c.param_type_2].str); - std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str); - std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str); + std::string paramtype(enum_to_string(ScriptApiNode::es_ContentParamType, c.param_type)); + std::string paramtype2(enum_to_string(ScriptApiNode::es_ContentParamType2, c.param_type_2)); + std::string drawtype(enum_to_string(ScriptApiNode::es_DrawType, c.drawtype)); + std::string liquid_type(enum_to_string(ScriptApiNode::es_LiquidType, c.liquid_type)); /* Missing "tiles" because I don't see a usecase (at least not yet). */ @@ -1325,21 +1342,6 @@ int getenumfield(lua_State *L, int table, return result; } -/******************************************************************************/ -bool string_to_enum(const EnumString *spec, int &result, - const std::string &str) -{ - const EnumString *esp = spec; - while(esp->str){ - if (!strcmp(str.c_str(), esp->str)) { - result = esp->num; - return true; - } - esp++; - } - return false; -} - /******************************************************************************/ ItemStack read_item(lua_State* L, int index, IItemDefManager *idef) { @@ -1456,7 +1458,7 @@ void push_wear_bar_params(lua_State *L, const WearBarParams ¶ms) { lua_newtable(L); - setstringfield(L, -1, "blend", WearBarParams::es_BlendMode[params.blend].str); + setstringfield(L, -1, "blend", enum_to_string(WearBarParams::es_BlendMode, params.blend)); lua_newtable(L); for (const std::pair item: params.colorStops) { @@ -2312,7 +2314,7 @@ void push_hud_element(lua_State *L, HudElement *elem) { lua_newtable(L); - lua_pushstring(L, es_HudElementType[(u8)elem->type].str); + lua_pushstring(L, enum_to_string(es_HudElementType, elem->type)); lua_setfield(L, -2, "type"); push_v2f(L, elem->pos); diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 80e85f93f80e5..011aa355eccfb 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -25,7 +25,6 @@ extern "C" { #include "itemgroup.h" #include "itemdef.h" #include "util/pointabilities.h" -#include "c_types.h" // We do an explicit path include because by default c_content.h include src/client/hud.h // prior to the src/hud.h, which is not good on server only build #include "../../hud.h" @@ -58,6 +57,8 @@ struct collisionMoveResult; namespace treegen { struct TreeDef; } extern struct EnumString es_TileAnimationType[]; +extern struct EnumString es_ItemType[]; +extern struct EnumString es_TouchInteractionMode[]; extern const std::array object_property_keys; @@ -141,9 +142,6 @@ std::vector read_items(lua_State *L, int index, IGameDef* gdef); void push_simplesoundspec(lua_State *L, const SoundSpec &spec); -bool string_to_enum(const EnumString *spec, - int &result, const std::string &str); - bool read_noiseparams(lua_State *L, int index, NoiseParams *np); void push_noiseparams(lua_State *L, NoiseParams *np); diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 4e9175f533cf0..138063d54b2be 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -16,6 +16,7 @@ extern "C" { #include "constants.h" #include #include +#include "common/c_types.h" #define CHECK_TYPE(index, name, type) do { \ diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index 947fcd8035fda..39f6d3f7a6b38 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -15,7 +15,6 @@ #include #include "irrlichttypes_bloated.h" -#include "common/c_types.h" extern "C" { #include diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp index eb74bfa8e865a..7d489fa7680c7 100644 --- a/src/script/common/c_internal.cpp +++ b/src/script/common/c_internal.cpp @@ -10,6 +10,7 @@ #include "porting.h" #include "settings.h" #include // std::find +#include "common/c_types.h" // LuaError std::string script_get_backtrace(lua_State *L) { diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h index ce948801b619b..e5b33f9eb1b1c 100644 --- a/src/script/common/c_internal.h +++ b/src/script/common/c_internal.h @@ -19,7 +19,7 @@ extern "C" { } #include "config.h" -#include "common/c_types.h" +#include "util/enum_string.h" /* diff --git a/src/script/common/c_packer.cpp b/src/script/common/c_packer.cpp index 0a4045e1b370f..7083e3f4a7786 100644 --- a/src/script/common/c_packer.cpp +++ b/src/script/common/c_packer.cpp @@ -13,6 +13,7 @@ #include "log.h" #include "debug.h" #include "threading/mutex_auto_lock.h" +#include "common/c_types.h" // LuaError extern "C" { #include diff --git a/src/script/common/c_types.cpp b/src/script/common/c_types.cpp deleted file mode 100644 index 57d6c42444e05..0000000000000 --- a/src/script/common/c_types.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Luanti -// SPDX-License-Identifier: LGPL-2.1-or-later -// Copyright (C) 2013 celeron55, Perttu Ahola - -#include - -#include "common/c_types.h" -#include "common/c_internal.h" -#include "itemdef.h" - - -struct EnumString es_ItemType[] = - { - {ITEM_NONE, "none"}, - {ITEM_NODE, "node"}, - {ITEM_CRAFT, "craft"}, - {ITEM_TOOL, "tool"}, - {0, NULL}, - }; - -struct EnumString es_TouchInteractionMode[] = - { - {LONG_DIG_SHORT_PLACE, "long_dig_short_place"}, - {SHORT_DIG_LONG_PLACE, "short_dig_long_place"}, - {TouchInteractionMode_USER, "user"}, - {0, NULL}, - }; diff --git a/src/script/common/c_types.h b/src/script/common/c_types.h index 6560524b8a9ea..160b29eb5721d 100644 --- a/src/script/common/c_types.h +++ b/src/script/common/c_types.h @@ -9,15 +9,8 @@ extern "C" { } #include - #include "exceptions.h" -struct EnumString -{ - int num; - const char *str; -}; - class StackUnroller { private: @@ -41,7 +34,3 @@ class LuaError : public ModError public: LuaError(const std::string &s) : ModError(s) {} }; - - -extern EnumString es_ItemType[]; -extern EnumString es_TouchInteractionMode[]; diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index 135e9acfdbb9a..b532e9cd9907c 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -18,7 +18,6 @@ extern "C" { } #include "irrlichttypes.h" -#include "common/c_types.h" #include "common/c_internal.h" #include "debug.h" #include "config.h" diff --git a/src/script/cpp_api/s_internal.h b/src/script/cpp_api/s_internal.h index 3379d15c6a512..dc5823df1f51a 100644 --- a/src/script/cpp_api/s_internal.h +++ b/src/script/cpp_api/s_internal.h @@ -15,6 +15,7 @@ #include "common/c_internal.h" #include "cpp_api/s_base.h" #include "threading/mutex_auto_lock.h" +#include "common/c_types.h" #ifdef SCRIPTAPI_LOCK_DEBUG #include diff --git a/src/server.h b/src/server.h index 407d43d1b2de7..ff2f8dbcf5b99 100644 --- a/src/server.h +++ b/src/server.h @@ -23,6 +23,7 @@ #include "chatmessage.h" #include "sound.h" #include "translation.h" +#include "script/common/c_types.h" // LuaError #include #include #include diff --git a/src/tool.cpp b/src/tool.cpp index 43b3ebe25c140..7c942c0b4c051 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -12,7 +12,6 @@ #include "util/serialize.h" #include "util/numeric.h" #include "util/hex.h" -#include "common/c_content.h" #include @@ -235,7 +234,7 @@ void WearBarParams::serializeJson(std::ostream &os) const color_stops[ftos(item.first)] = encodeHexColorString(item.second); } root["color_stops"] = color_stops; - root["blend"] = WearBarParams::es_BlendMode[blend].str; + root["blend"] = enum_to_string(WearBarParams::es_BlendMode, blend); fastWriteJson(root, os); } diff --git a/src/tool.h b/src/tool.h index 62b374d91872f..2e0fdfe2077b5 100644 --- a/src/tool.h +++ b/src/tool.h @@ -7,7 +7,7 @@ #include "irrlichttypes.h" #include "itemgroup.h" #include "json-forwards.h" -#include "common/c_types.h" +#include "util/enum_string.h" #include #include diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index ec88a33c2bafc..7d5ac48fb094d 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -18,4 +18,5 @@ set(util_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp ${CMAKE_CURRENT_SOURCE_DIR}/png.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/enum_string.cpp PARENT_SCOPE) diff --git a/src/util/enum_string.cpp b/src/util/enum_string.cpp new file mode 100644 index 0000000000000..5b35053d80d97 --- /dev/null +++ b/src/util/enum_string.cpp @@ -0,0 +1,31 @@ +// Luanti +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2025 cx384 + +#include "util/enum_string.h" +#include + +bool string_to_enum(const EnumString *spec, int &result, std::string_view str) +{ + const EnumString *esp = spec; + while (esp->str) { + if (str == esp->str) { + assert(esp->num >= 0); + result = esp->num; + return true; + } + esp++; + } + return false; +} + +const char *enum_to_string(const EnumString *spec, int num) +{ + if (num < 0) + return nullptr; + // assume array order matches enum order + auto *p = &spec[num]; + assert(p->num == num); + assert(p->str); + return p->str; +} diff --git a/src/util/enum_string.h b/src/util/enum_string.h new file mode 100644 index 0000000000000..c4f84ee09d4e7 --- /dev/null +++ b/src/util/enum_string.h @@ -0,0 +1,17 @@ +// Luanti +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2025 cx384 + +#pragma once + +#include + +struct EnumString +{ + int num; + const char *str; +}; + +bool string_to_enum(const EnumString *spec, int &result, std::string_view str); + +const char *enum_to_string(const EnumString *spec, int num);