Skip to content

Commit

Permalink
implement df-other-vectors-type for lua
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLubar committed May 9, 2020
1 parent 7cf9624 commit 6c20fe4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions library/LuaTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,28 @@ void struct_identity::build_metatable(lua_State *state)
SetPtrMethods(state, base+1, base+2);
}

void other_vectors_identity::build_metatable(lua_State *state)
{
int base = lua_gettop(state);
MakeFieldMetatable(state, this, meta_struct_index, meta_struct_newindex);

EnableMetaField(state, base+2, "_enum");

LookupInTable(state, index_enum, &DFHACK_TYPEID_TABLE_TOKEN);
lua_setfield(state, base+1, "_enum");

auto keys = &index_enum->getKeys()[-index_enum->getFirstItem()];

for (int64_t i = 0; i < index_enum->getLastItem(); i++)
{
lua_getfield(state, base+2, keys[i]);
lua_rawseti(state, base+2, int(i));
}

SetStructMethod(state, base+1, base+2, meta_struct_field_reference, "_field");
SetPtrMethods(state, base+1, base+2);
}

void global_identity::build_metatable(lua_State *state)
{
int base = lua_gettop(state);
Expand Down
18 changes: 18 additions & 0 deletions library/include/DataDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,23 @@ namespace DFHack
virtual identity_type type() { return IDTYPE_UNION; }
};

class DFHACK_EXPORT other_vectors_identity : public struct_identity {
enum_identity *index_enum;

public:
other_vectors_identity(size_t size, TAllocateFn alloc,
compound_identity *scope_parent, const char *dfhack_name,
struct_identity *parent, const struct_field_info *fields,
enum_identity *index_enum) :
struct_identity(size, alloc, scope_parent, dfhack_name, parent, fields),
index_enum(index_enum)
{}

enum_identity *getIndexEnum() { return index_enum; }

virtual void build_metatable(lua_State *state);
};

#ifdef _MSC_VER
typedef void *virtual_ptr;
#else
Expand Down Expand Up @@ -465,6 +482,7 @@ namespace df
using DFHack::global_identity;
using DFHack::struct_identity;
using DFHack::union_identity;
using DFHack::other_vectors_identity;
using DFHack::struct_field_info;
using DFHack::struct_field_info_extra;
using DFHack::bitfield_item_info;
Expand Down

0 comments on commit 6c20fe4

Please sign in to comment.