Skip to content

Commit

Permalink
Add enum_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
cx384 committed Jan 19, 2025
1 parent 59c3b71 commit 543713e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/client/content_cao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "client/shader.h"
#include "client/minimap.h"
#include <quaternion.h>
#include "common/c_content.h" // enum_to_string

class Settings;
struct ToolCapabilities;
Expand Down Expand Up @@ -597,7 +598,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
if (!m_prop.is_visible)
return;

infostream << "GenericCAO::addToScene(): " << es_ObjectVisual[m_prop.visual].str << std::endl;
infostream << "GenericCAO::addToScene(): " << enum_to_string(es_ObjectVisual, m_prop.visual) << std::endl;

m_material_type_param = 0.5f; // May cut off alpha < 128 depending on m_material_type

Expand Down Expand Up @@ -768,7 +769,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
break;
} default:
infostream << "GenericCAO::addToScene(): \""
<< es_ObjectVisual[m_prop.visual].str
<< enum_to_string(es_ObjectVisual, m_prop.visual)
<< "\" not supported"<<std::endl;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/object_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::string ObjectProperties::dump() const
os << ", physical=" << physical;
os << ", collideWithObjects=" << collideWithObjects;
os << ", collisionbox=" << collisionbox.MinEdge << "," << collisionbox.MaxEdge;
os << ", visual=" << es_ObjectVisual[visual].str;
os << ", visual=" << enum_to_string(es_ObjectVisual, visual);
os << ", mesh=" << mesh;
os << ", visual_size=" << visual_size;
os << ", textures=[";
Expand Down Expand Up @@ -152,7 +152,7 @@ void ObjectProperties::serialize(std::ostream &os) const

// Convert to string for compatibility
// New serialize versions should use the numeric enum value
os << serializeString16(es_ObjectVisual[visual].str);
os << serializeString16(enum_to_string(es_ObjectVisual, visual));

writeV3F32(os, visual_size);
writeU16(os, textures.size());
Expand Down
13 changes: 12 additions & 1 deletion src/script/common/c_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ void push_object_properties(lua_State *L, const ObjectProperties *prop)
lua_setfield(L, -2, "selectionbox");
push_pointability_type(L, prop->pointable);
lua_setfield(L, -2, "pointable");
lua_pushstring(L, es_ObjectVisual[prop->visual].str);
lua_pushstring(L, enum_to_string(es_ObjectVisual, prop->visual));
lua_setfield(L, -2, "visual");
lua_pushlstring(L, prop->mesh.c_str(), prop->mesh.size());
lua_setfield(L, -2, "mesh");
Expand Down Expand Up @@ -1346,6 +1346,17 @@ bool string_to_enum(const EnumString *spec, int &result,
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);
return p->str;
}

/******************************************************************************/
ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
{
Expand Down
3 changes: 3 additions & 0 deletions src/script/common/c_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,13 @@ std::vector<ItemStack> read_items (lua_State *L,
void push_simplesoundspec (lua_State *L,
const SoundSpec &spec);

// TODO move EnumString to its own file to not import script code everywhere
bool string_to_enum (const EnumString *spec,
int &result,
const std::string &str);

const char* enum_to_string (const EnumString *spec, int num);

bool read_noiseparams (lua_State *L, int index,
NoiseParams *np);
void push_noiseparams (lua_State *L, NoiseParams *np);
Expand Down

0 comments on commit 543713e

Please sign in to comment.