Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move EnumString to separate file and add enum_to_string #15714

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/content/mods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "porting.h"
#include "convert_json.h"
#include "script/common/c_internal.h"
#include "exceptions.h"

void ModSpec::checkAndLog() const
{
Expand Down
1 change: 1 addition & 0 deletions src/content/subgames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/gui/guiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/script/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
52 changes: 27 additions & 25 deletions src/script/common/c_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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). */

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1456,7 +1458,7 @@ void push_wear_bar_params(lua_State *L,
const WearBarParams &params)
{
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) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/script/common/c_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/script/common/c_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand Down
1 change: 0 additions & 1 deletion src/script/common/c_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <string_view>

#include "irrlichttypes_bloated.h"
#include "common/c_types.h"

extern "C" {
#include <lua.h>
Expand Down
1 change: 1 addition & 0 deletions src/script/common/c_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/script/common/c_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern "C" {
}

#include "config.h"
#include "common/c_types.h"
#include "util/enum_string.h"


/*
Expand Down
1 change: 1 addition & 0 deletions src/script/common/c_packer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
27 changes: 0 additions & 27 deletions src/script/common/c_types.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions src/script/common/c_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ extern "C" {
}

#include <iostream>

#include "exceptions.h"

struct EnumString
{
int num;
const char *str;
};

class StackUnroller
{
private:
Expand All @@ -41,7 +34,3 @@ class LuaError : public ModError
public:
LuaError(const std::string &s) : ModError(s) {}
};


extern EnumString es_ItemType[];
extern EnumString es_TouchInteractionMode[];
1 change: 0 additions & 1 deletion src/script/cpp_api/s_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/script/cpp_api/s_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 1 addition & 2 deletions src/tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>


Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 31 additions & 0 deletions src/util/enum_string.cpp
Original file line number Diff line number Diff line change
@@ -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;
cx384 marked this conversation as resolved.
Show resolved Hide resolved
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;
cx384 marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 17 additions & 0 deletions src/util/enum_string.h
Original file line number Diff line number Diff line change
@@ -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);
Loading