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
76 changes: 40 additions & 36 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2667,20 +2667,17 @@ static const LuaWrapper::FunctionReg dfhack_world_module[] = {
{ NULL, NULL }
};

#define WORLD_GAMEMODE_WRAPPER(func) \
static int world_gamemode_##func(lua_State *L) \
{ \
int gametype = luaL_optint(L, 1, -1); \
lua_pushboolean(L, World::func((df::game_type)gametype)); \
return 1;\
}
#define WORLD_GAMEMODE_FUNC(func) \
{#func, world_gamemode_##func}
using gamemode_func = auto (df::game_type t) -> bool;
template <gamemode_func gmf>
static int world_gamemode(lua_State* L)
{
int gametype = luaL_optint(L, 1, -1);
lua_pushboolean(L, gmf((df::game_type)gametype));
return 1;
}

WORLD_GAMEMODE_WRAPPER(isFortressMode);
WORLD_GAMEMODE_WRAPPER(isAdventureMode);
WORLD_GAMEMODE_WRAPPER(isArena);
WORLD_GAMEMODE_WRAPPER(isLegends);
#define WORLD_GAMEMODE_FUNC(func) \
{#func, world_gamemode<World::func>}

static const luaL_Reg dfhack_world_funcs[] = {
WORLD_GAMEMODE_FUNC(isFortressMode),
Expand Down Expand Up @@ -3789,32 +3786,39 @@ static int internal_diffscan(lua_State *L)
bool has_newv = !lua_isnil(L, 7);
bool has_diffv = !lua_isnil(L, 8);

#define LOOP(esz, etype) \
case esz: { \
etype *pold = (etype*)old_data; \
etype *pnew = (etype*)new_data; \
etype oldv = (etype)luaL_optint(L, 6, 0); \
etype newv = (etype)luaL_optint(L, 7, 0); \
etype diffv = (etype)luaL_optint(L, 8, 0); \
for (int i = start_idx; i < end_idx; i++) { \
if (pold[i] == pnew[i]) continue; \
if (has_oldv && pold[i] != oldv) continue; \
if (has_newv && pnew[i] != newv) continue; \
if (has_diffv && etype(pnew[i]-pold[i]) != diffv) continue; \
lua_pushinteger(L, i); return 1; \
} \
break; \
}
auto loop = [&]<typename etype>() -> std::optional<int>
{
etype* pold = (etype*)old_data;
etype* pnew = (etype*)new_data;
etype oldv = (etype)luaL_optint(L, 6, 0);
etype newv = (etype)luaL_optint(L, 7, 0);
etype diffv = (etype)luaL_optint(L, 8, 0);
for (int i = start_idx; i < end_idx; i++) {
if (pold[i] == pnew[i]) continue;
if (has_oldv && pold[i] != oldv) continue;
if (has_newv && pnew[i] != newv) continue;
if (has_diffv && etype(pnew[i] - pold[i]) != diffv) continue;
return i;
}
return std::nullopt;
};

std::optional<int> res;
switch (eltsize) {
LOOP(1, uint8_t);
LOOP(2, uint16_t);
LOOP(4, uint32_t);
default:
luaL_argerror(L, 5, "invalid element size");
case 1:
res = loop.operator()<uint8_t>(); break;
case 2:
res = loop.operator()<uint16_t>(); break;
case 4:
res = loop.operator()<uint32_t>(); break;
default:
luaL_argerror(L, 5, "invalid element size");
}
#undef LOOP
if (res)
lua_pushinteger(L,*res);
else
lua_pushnil(L);

lua_pushnil(L);
return 1;
}

Expand Down
6 changes: 3 additions & 3 deletions library/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ using namespace DFHack::LuaWrapper;

/* */

static int change_error(lua_State *state)
[[noreturn]] static int change_error(lua_State *state)
{
luaL_error(state, "Attempt to change a read-only table.\n");
return 0;
std::abort(); // should never be reached but makes gcc happy
}

/**
Expand Down Expand Up @@ -1642,7 +1642,7 @@ static void RenderType(lua_State *state, const compound_identity *node)
case IDTYPE_UNION: // TODO: change this to union-type? what relies on this?
lua_pushboolean(state, true);
lua_setfield(state, ftable, "_union");
// fall through
[[fallthrough]];
case IDTYPE_STRUCT:
lua_pushstring(state, "struct-type");
lua_setfield(state, ftable, "_kind");
Expand Down
1 change: 1 addition & 0 deletions library/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ int Plugin::lua_cmd_wrapper(lua_State *state)

int Plugin::lua_fun_wrapper(lua_State *state)
{
using DFHack::LuaWrapper::UPVAL_CONTAINER_ID;
auto cmd = (LuaFunction*)lua_touserdata(state, UPVAL_CONTAINER_ID);

RefAutoinc lock(cmd->owner->access);
Expand Down
2 changes: 1 addition & 1 deletion library/include/ColorText.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace DFHack
class DFHACK_EXPORT color_ostream : public std::ostream
{
public:
typedef DFHack::color_value color_value;
using color_value = DFHack::color_value;

private:
color_value cur_color;
Expand Down
8 changes: 4 additions & 4 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ distribution.
#include <vector>
#include <stdint.h>

#define DFH_MOD_SHIFT 1
#define DFH_MOD_CTRL 2
#define DFH_MOD_ALT 4

struct WINDOW;
struct lua_State;

Expand All @@ -58,6 +54,10 @@ namespace df

namespace DFHack
{
constexpr auto DFH_MOD_SHIFT = 1;
constexpr auto DFH_MOD_CTRL = 2;
constexpr auto DFH_MOD_ALT = 4;

class Process;
class Module;
class Materials;
Expand Down
6 changes: 3 additions & 3 deletions library/include/DataDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ distribution.
#include "BitArray.h"
#include "Export.h"

typedef struct lua_State lua_State;
struct lua_State;

/*
* Definitions of DFHack namespace structs used by generated headers.
Expand Down Expand Up @@ -352,9 +352,9 @@ namespace DFHack
};

#ifdef _MSC_VER
typedef void *virtual_ptr;
using virtual_ptr = void*;
#else
typedef virtual_class *virtual_ptr;
using virtual_ptr = virtual_class*;
#endif

class DFHACK_EXPORT VMethodInterposeLinkBase;
Expand Down
7 changes: 7 additions & 0 deletions library/include/DataFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace df {
T get_from_lua_state(lua_State* L, int idx)
{
using DFHack::LuaWrapper::field_error;
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
using Ptr = std::add_pointer_t<std::remove_reference_t<T>>;
Ptr ptr{};
df::identity_traits<Ptr>::get()->lua_write(L, UPVAL_METHOD_NAME, &ptr, idx);
Expand All @@ -69,6 +70,7 @@ namespace df {
template<std::semiregular T>
T get_from_lua_state(lua_State* L, int idx)
{
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
T val{};
df::identity_traits<T>::get()->lua_write(L, UPVAL_METHOD_NAME, &val, idx);
return val;
Expand All @@ -78,6 +80,7 @@ namespace df {
requires std::is_invocable_r_v<RT, FT, ET..., AT...>
void call_and_push_impl(lua_State* L, int base, std::index_sequence<I...>, FT fun, ET... extra)
{
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
if constexpr (std::is_same_v<RT, void>) {
std::invoke(fun, extra..., (get_from_lua_state<AT>(L, base+I))...);
lua_pushnil(L);
Expand Down Expand Up @@ -119,6 +122,7 @@ namespace df {
struct function_wrapper<RT(CT::*)(AT...)> {
static const int num_args = sizeof...(AT)+1;
static void execute(lua_State *L, int base, RT(CT::*mem_fun)(AT...)) {
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
CT *self = (CT*)DFHack::LuaWrapper::get_object_addr(L, base++, UPVAL_METHOD_NAME, "invoke");
call_and_push<RT, AT...>(L, base, mem_fun, self);
};
Expand All @@ -128,6 +132,7 @@ namespace df {
struct function_wrapper<RT(CT::*)(AT...) const> {
static const int num_args = sizeof...(AT)+1;
static void execute(lua_State *L, int base, RT(CT::*mem_fun)(AT...) const) {
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
CT *self = (CT*)DFHack::LuaWrapper::get_object_addr(L, base++, UPVAL_METHOD_NAME, "invoke");
call_and_push<RT, AT...>(L, base, mem_fun, self);
};
Expand All @@ -154,6 +159,7 @@ namespace df {
struct function_wrapper<RT(CT::*)(AT...) noexcept> {
static const int num_args = sizeof...(AT) + 1;
static void execute(lua_State* L, int base, RT(CT::* mem_fun)(AT...)) {
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
CT* self = (CT*)DFHack::LuaWrapper::get_object_addr(L, base++, UPVAL_METHOD_NAME, "invoke");
call_and_push<RT, AT...>(L, base, mem_fun, self);
};
Expand All @@ -163,6 +169,7 @@ namespace df {
struct function_wrapper<RT(CT::*)(AT...) const noexcept> {
static const int num_args = sizeof...(AT) + 1;
static void execute(lua_State* L, int base, RT(CT::* mem_fun)(AT...) const) {
using DFHack::LuaWrapper::UPVAL_METHOD_NAME;
CT* self = (CT*)DFHack::LuaWrapper::get_object_addr(L, base++, UPVAL_METHOD_NAME, "invoke");
call_and_push<RT, AT...>(L, base, mem_fun, self);
};
Expand Down
22 changes: 11 additions & 11 deletions library/include/DataIdentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ namespace df

class DFHACK_EXPORT stl_ptr_vector_identity : public ptr_container_identity {
public:
typedef std::vector<void*> container;
using container = std::vector<void*>;

/*
* This class assumes that std::vector<T*> is equivalent
Expand Down Expand Up @@ -486,7 +486,7 @@ namespace df
* in layout and behavior to BitArray<int> for any T.
*/

typedef BitArray<int> container;
using container = BitArray<int>;

bit_array_identity(const enum_identity *ienum = NULL)
: bit_container_identity(sizeof(container), &allocator_fn<container>, ienum)
Expand Down Expand Up @@ -516,7 +516,7 @@ namespace df

class DFHACK_EXPORT stl_bit_vector_identity : public bit_container_identity {
public:
typedef std::vector<bool> container;
using container = std::vector<bool>;

stl_bit_vector_identity(const enum_identity *ienum = NULL)
: bit_container_identity(sizeof(container), &df::allocator_fn<container>, ienum)
Expand Down Expand Up @@ -547,7 +547,7 @@ namespace df
template<class T>
class enum_list_attr_identity : public container_identity {
public:
typedef enum_list_attr<T> container;
using container = enum_list_attr<T>;

enum_list_attr_identity(const type_identity *item)
: container_identity(sizeof(container), NULL, item, NULL)
Expand Down Expand Up @@ -615,7 +615,7 @@ namespace df
template<typename T>
struct DFHACK_EXPORT identity_traits<std::shared_ptr<T>> {
static opaque_identity *get() {
typedef std::shared_ptr<T> type;
using type = std::shared_ptr<T>;
static std::string name = std::string("shared_ptr<") + typeid(T).name() + ">";
static opaque_identity identity(sizeof(type), allocator_noassign_fn<type>, name);
return &identity;
Expand Down Expand Up @@ -772,7 +772,7 @@ namespace df

template<class T>
inline const container_identity *identity_traits<std::vector<T> >::get() {
typedef std::vector<T> container;
using container = std::vector<T>;
static const stl_container_identity<container> identity("vector", identity_traits<T>::get());
return &identity;
}
Expand Down Expand Up @@ -800,28 +800,28 @@ namespace df
#ifdef BUILD_DFHACK_LIB
template<class T>
inline const container_identity *identity_traits<std::deque<T> >::get() {
typedef std::deque<T> container;
using container = std::deque<T>;
static const stl_container_identity<container> identity("deque", identity_traits<T>::get());
return &identity;
}

template<class T>
inline const container_identity *identity_traits<std::set<T> >::get() {
typedef std::set<T> container;
using container = std::set<T>;
static const ro_stl_container_identity<container> identity("set", identity_traits<T>::get());
return &identity;
}

template<class KT, class T>
inline const container_identity *identity_traits<std::map<KT, T>>::get() {
typedef std::map<KT, T> container;
using container = std::map<KT, T>;
static const ro_stl_assoc_container_identity<container> identity("map", identity_traits<KT>::get(), identity_traits<T>::get());
return &identity;
}

template<class KT, class T>
inline const container_identity *identity_traits<std::unordered_map<KT, T>>::get() {
typedef std::unordered_map<KT, T> container;
using container = std::unordered_map<KT, T>;
static const ro_stl_assoc_container_identity<container> identity("unordered_map", identity_traits<KT>::get(), identity_traits<T>::get());
return &identity;
}
Expand All @@ -834,7 +834,7 @@ namespace df

template<class T>
inline const container_identity *identity_traits<DfArray<T> >::get() {
typedef DfArray<T> container;
using container = DfArray<T>;
static const stl_container_identity<container> identity("DfArray", identity_traits<T>::get());
return &identity;
}
Expand Down
Loading