mirror of
https://github.com/minetest/minetest.git
synced 2025-02-03 11:51:07 +01:00
Move EnumString to separate file and add enum_to_string (#15714)
This commit is contained in:
@ -15,6 +15,7 @@
|
||||
#include "porting.h"
|
||||
#include "convert_json.h"
|
||||
#include "script/common/c_internal.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
void ModSpec::checkAndLog() const
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include <IGUIStaticText.h>
|
||||
#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"
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
#include "irrlichttypes_bloated.h"
|
||||
#include <string>
|
||||
#include "common/c_types.h"
|
||||
#include "util/enum_string.h"
|
||||
|
||||
#define HUD_DIR_LEFT_RIGHT 0
|
||||
#define HUD_DIR_RIGHT_LEFT 1
|
||||
|
@ -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
|
||||
|
@ -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<const f32, const video::SColor> 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);
|
||||
|
@ -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<const char *, 33> object_property_keys;
|
||||
@ -141,9 +142,6 @@ std::vector<ItemStack> 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);
|
||||
|
||||
|
@ -16,6 +16,7 @@ extern "C" {
|
||||
#include "constants.h"
|
||||
#include <set>
|
||||
#include <cmath>
|
||||
#include "common/c_types.h"
|
||||
|
||||
|
||||
#define CHECK_TYPE(index, name, type) do { \
|
||||
|
@ -15,7 +15,6 @@
|
||||
#include <string_view>
|
||||
|
||||
#include "irrlichttypes_bloated.h"
|
||||
#include "common/c_types.h"
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "porting.h"
|
||||
#include "settings.h"
|
||||
#include <algorithm> // std::find
|
||||
#include "common/c_types.h" // LuaError
|
||||
|
||||
std::string script_get_backtrace(lua_State *L)
|
||||
{
|
||||
|
@ -19,7 +19,7 @@ extern "C" {
|
||||
}
|
||||
|
||||
#include "config.h"
|
||||
#include "common/c_types.h"
|
||||
#include "util/enum_string.h"
|
||||
|
||||
|
||||
/*
|
||||
|
@ -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 <lauxlib.h>
|
||||
|
@ -1,27 +0,0 @@
|
||||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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},
|
||||
};
|
@ -9,15 +9,8 @@ extern "C" {
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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[];
|
||||
|
@ -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"
|
||||
|
@ -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 <cassert>
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "chatmessage.h"
|
||||
#include "sound.h"
|
||||
#include "translation.h"
|
||||
#include "script/common/c_types.h" // LuaError
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
@ -12,7 +12,6 @@
|
||||
#include "util/serialize.h"
|
||||
#include "util/numeric.h"
|
||||
#include "util/hex.h"
|
||||
#include "common/c_content.h"
|
||||
#include <json/json.h>
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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 <SColor.h>
|
||||
|
||||
#include <string>
|
||||
|
@ -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)
|
||||
|
31
src/util/enum_string.cpp
Normal file
31
src/util/enum_string.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2025 cx384
|
||||
|
||||
#include "util/enum_string.h"
|
||||
#include <cassert>
|
||||
|
||||
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;
|
||||
}
|
17
src/util/enum_string.h
Normal file
17
src/util/enum_string.h
Normal file
@ -0,0 +1,17 @@
|
||||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2025 cx384
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
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);
|
Reference in New Issue
Block a user