Skip to content
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
33 changes: 33 additions & 0 deletions library/DataDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ const std::string compound_identity::getFullName() const
return getName();
}

bool compound_identity::is_equivalent(const compound_identity* other) const
{
if (this->byte_size() != other->byte_size() || strcmp(this->getName(), other->getName()) != 0)
return false;

if (this->scope_parent != other->scope_parent &&
!(this->scope_parent && other->scope_parent && this->scope_parent->is_equivalent(other->scope_parent)))
return false;

return true;
}

static std::mutex *known_mutex = NULL;

void compound_identity::Init(Core *core)
Expand Down Expand Up @@ -239,6 +251,27 @@ bool struct_identity::is_subclass(const struct_identity *actual) const
return false;
}

bool struct_identity::is_equivalent(const struct_identity* other) const
{
if (!static_cast<const compound_identity*>(this)->is_equivalent(static_cast<const compound_identity*>(other)))
return false;

if (this->parent != other->parent &&
!(this->parent && other->parent && this->parent->is_equivalent(other->parent)))
return false;

const struct_field_info* f0 = this->fields;
const struct_field_info* f1 = other->fields;

for (; f0->mode != struct_field_info::Mode::END && f1->mode != struct_field_info::Mode::END; f0++, f1++)
{
if (f0->mode != f1->mode || f0->offset != f1->offset || f0->count != f1->count || strcmp(f0->name, f1->name) != 0)
return false;
}

return true;
}

const std::string pointer_identity::getFullName() const
{
return (target ? target->getFullName() : std::string("void")) + "*";
Expand Down
6 changes: 5 additions & 1 deletion library/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ bool LuaWrapper::is_type_compatible(lua_State *state, const type_identity *type1
auto b1 = (struct_identity*)type1;
auto b2 = (struct_identity*)type2;

if (b1->is_equivalent(b2)) return true;

return (!exact_equal && b1->is_subclass(b2));
}

Expand Down Expand Up @@ -373,7 +375,9 @@ void *LuaWrapper::get_object_internal(lua_State *state, const type_identity *typ
if (!LookupTypeInfo(state, in_method)) // metatable -> type?
return NULL;

if (type && lua_touserdata(state, -1) != type)
type_identity* othertype = static_cast<type_identity*>(lua_touserdata(state, -1));

if (type && othertype != type)
{
/*
* If valid but different type, do an intelligent comparison.
Expand Down
2 changes: 1 addition & 1 deletion library/include/CoordTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace DFHack
};

template <typename T, T initializer = DFHack::coord_default_initializer<T>, typename U = DFHack::Coord2d<T, initializer>>
static inline const struct_identity coord2d_identity{sizeof(U), &df::allocator_fn<U>, nullptr, "coord", nullptr, coord2d_fields<T,initializer>};
static inline const struct_identity coord2d_identity{sizeof(U), &df::allocator_fn<U>, nullptr, "coord2d", nullptr, coord2d_fields<T,initializer>};

template <typename T, T initializer = coord_default_initializer<T>>
struct Coord3d
Expand Down
9 changes: 8 additions & 1 deletion library/include/DataDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ namespace DFHack
static std::vector<const compound_identity*>* top_scope;

const char *dfhack_name;
const compound_identity *const scope_parent;

static void ensure_compound_identity_init();

protected:
const compound_identity *const scope_parent;

compound_identity(size_t size, TAllocateFn alloc,
const compound_identity *scope_parent, const char *dfhack_name);

Expand All @@ -163,6 +164,9 @@ namespace DFHack
static const std::vector<const compound_identity*> &getTopScope() { return *top_scope; }

static void Init(Core *core);

bool is_equivalent(const compound_identity* other) const;

};

// Bitfields
Expand Down Expand Up @@ -297,6 +301,7 @@ namespace DFHack
const struct_field_info *fields;

static void ensure_struct_identity_init();
struct_identity* parent;

protected:
virtual void doInit(Core *core) const override;
Expand All @@ -317,6 +322,8 @@ namespace DFHack
bool is_subclass(const struct_identity *subtype) const;

virtual void build_metatable(lua_State *state) const;

bool is_equivalent(const struct_identity* other) const;
};

class DFHACK_EXPORT global_identity : public struct_identity {
Expand Down
Loading