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

split plugin lua macros to separate header #5161

Merged
merged 7 commits into from
Jan 1, 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 library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(MAIN_HEADERS
include/Module.h
include/MemAccess.h
include/ModuleFactory.h
include/PluginLua.h
include/PluginManager.h
include/PluginStatics.h
include/RemoteClient.h
Expand Down
3 changes: 2 additions & 1 deletion library/VTableInterpose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ distribution.
#include <vector>
#include <map>

#include "MemAccess.h"
#include "Core.h"
#include "DataFuncs.h"
#include "MemAccess.h"
#include "VersionInfo.h"
#include "VTableInterpose.h"

Expand Down
30 changes: 30 additions & 0 deletions library/include/DataDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,36 @@ namespace df
}

namespace enums {}

/**
* A trait to obtain return type and class identity from an invokable
* used for invoking C++ methods from Lua and for vtable interposes
* moved here from DataFuncs.h to minimize header dependencies
*/

template<typename T> struct return_type {};

template<typename RT, typename ...AT>
struct return_type<RT(*)(AT...)> {
using type = RT;
static const bool is_method = false;
};

template<typename RT, class CT, typename ...AT>
struct return_type<RT(CT::*)(AT...)> {
using type = RT;
using class_type = CT;
static const bool is_method = true;
};

template<typename RT, class CT, typename ...AT>
struct return_type<RT(CT::*)(AT...) const> {
using type = RT;
using class_type = CT;
static const bool is_method = true;
};


}

/*
Expand Down
44 changes: 7 additions & 37 deletions library/include/DataFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,6 @@ namespace df {
operator DFHack::color_ostream& () { return *out; }
};

template<class T> struct return_type {};

template<typename RT, typename ...AT>
struct return_type<RT (*)(AT...)>{
using type = RT;
static const bool is_method = false;
};

template<typename RT, class CT, typename ...AT>
struct return_type<RT (CT::*)(AT...)>{
using type = RT;
using class_type = CT;
static const bool is_method = true;
};

template<typename RT, class CT, typename ...AT>
struct return_type<RT (CT::*)(AT...) const>{
using type = RT;
using class_type = CT;
static const bool is_method = true;
};

// the std::is_enum_v alternative is because pushing is_primitive
// into all the enum identities would require changing codegen

Expand All @@ -77,11 +55,9 @@ namespace df {
return val;
}


template<typename RT, typename... AT, typename FT, typename... ET, std::size_t... I>
template<isPrimitive RT, typename... AT, typename FT, typename... ET, std::size_t... I>
requires std::is_invocable_r_v<RT, FT, ET..., AT...>
&& isPrimitive<RT>
void call_and_push_impl(lua_State* L, int base, std::index_sequence<I...>, FT fun, ET... extra)
void call_and_push_impl(lua_State* L, int base, std::index_sequence<I...>, FT fun, ET... extra)
{
if constexpr (std::is_same_v<RT, void>) {
std::invoke(fun, extra..., (get_from_lua_state<AT>(L, base+I))...);
Expand All @@ -94,19 +70,16 @@ namespace df {
}
}

template<typename RT, typename... AT, typename FT, typename... ET, typename indices = std::index_sequence_for<AT...> >
template<isPrimitive RT, typename... AT, typename FT, typename... ET, typename indices = std::index_sequence_for<AT...> >
requires std::is_invocable_r_v<RT, FT, ET..., AT...>
&& isPrimitive<RT>

void call_and_push(lua_State* L, int base, FT fun, ET... extra)
{
call_and_push_impl<RT, AT...>(L, base, indices{}, fun, extra...);
}

template<typename T> struct function_wrapper {};

template<typename RT, typename ...AT>
requires isPrimitive<RT>
template<isPrimitive RT, typename ...AT>
struct function_wrapper<RT(*)(DFHack::color_ostream&, AT...)> {
static const int num_args = sizeof...(AT);
static void execute(lua_State *L, int base, RT (fun)(DFHack::color_ostream& out, AT...)) {
Expand All @@ -115,17 +88,15 @@ namespace df {
}
};

template<typename RT, typename ...AT>
requires isPrimitive<RT>
template<isPrimitive RT, typename ...AT>
struct function_wrapper<RT(*)(AT...)> {
static const int num_args = sizeof...(AT);
static void execute(lua_State *L, int base, RT (fun)(AT...)) {
call_and_push<RT, AT...>(L, base, fun);
}
};

template<typename RT, class CT, typename ...AT>
requires isPrimitive<RT>
template<isPrimitive RT, class CT, typename ...AT>
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...)) {
Expand All @@ -134,8 +105,7 @@ namespace df {
};
};

template<typename RT, class CT, typename ...AT>
requires isPrimitive<RT>
template<isPrimitive RT, class CT, typename ...AT>
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) {
Expand Down
19 changes: 19 additions & 0 deletions library/include/PluginLua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Additional macros for calling Lua from a plugin
*/

#pragma once

#include "DataFuncs.h"

#define DFHACK_PLUGIN_LUA_COMMANDS \
DFhackCExport const DFHack::CommandReg plugin_lua_commands[] =
#define DFHACK_PLUGIN_LUA_FUNCTIONS \
DFhackCExport const DFHack::FunctionReg plugin_lua_functions[] =
#define DFHACK_PLUGIN_LUA_EVENTS \
DFhackCExport const DFHack::EventReg plugin_lua_events[] =

#define DFHACK_LUA_COMMAND(name) { #name, name }
#define DFHACK_LUA_FUNCTION(name) { #name, df::wrap_function(name,true) }
#define DFHACK_LUA_EVENT(name) { #name, &name##_event }
#define DFHACK_LUA_END { NULL, NULL }
14 changes: 0 additions & 14 deletions library/include/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ distribution.
#include <vector>

#include "Core.h"
#include "DataFuncs.h"

typedef struct lua_State lua_State;

Expand Down Expand Up @@ -324,19 +323,6 @@ extern "C" { \
DFhackDataExport bool plugin_is_enabled = false; \
bool &varname = plugin_is_enabled;

#define DFHACK_PLUGIN_LUA_COMMANDS \
DFhackCExport const DFHack::CommandReg plugin_lua_commands[] =
#define DFHACK_PLUGIN_LUA_FUNCTIONS \
DFhackCExport const DFHack::FunctionReg plugin_lua_functions[] =
#define DFHACK_PLUGIN_LUA_EVENTS \
DFhackCExport const DFHack::EventReg plugin_lua_events[] =

#define DFHACK_LUA_COMMAND(name) { #name, name }
#define DFHACK_LUA_FUNCTION(name) { #name, df::wrap_function(name,true) }
#define DFHACK_LUA_EVENT(name) { #name, &name##_event }
#define DFHACK_LUA_END { NULL, NULL }


#define REQUIRE_GLOBAL_NO_USE(global_name) \
static int VARIABLE_IS_NOT_USED CONCAT_TOKENS(required_globals_, __LINE__) = \
(plugin_globals->push_back(#global_name), 0);
Expand Down
3 changes: 2 additions & 1 deletion library/include/VTableInterpose.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ distribution.

#pragma once

#include "DataFuncs.h"
#include "DataDefs.h"
#include "DataIdentity.h"

namespace DFHack
{
Expand Down
4 changes: 0 additions & 4 deletions plugins/Plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ macro(dfhack_plugin)
endif()
target_link_libraries(${PLUGIN_NAME} dfhack dfhack-version ${PLUGIN_LINK_LIBRARIES})

# since PluginManager currently uses Lua headers, even when Lua is not used
get_target_property(lua_INCLUDES lua INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(${PLUGIN_NAME} PRIVATE ${lua_INCLUDES})

if(UNIX)
set(PLUGIN_COMPILE_FLAGS "${PLUGIN_COMPILE_FLAGS} ${PLUGIN_COMPILE_FLAGS_GCC}")
else()
Expand Down
1 change: 1 addition & 0 deletions plugins/aquifer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"

#include "modules/Maps.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/autobutcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Persistence.h"
#include "modules/Units.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/autochop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"

#include "modules/Burrows.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/autoclothing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Debug.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Items.h"
#include "modules/Materials.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/autonestbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Buildings.h"
#include "modules/Gui.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/blueprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"

#include "modules/Buildings.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/buildingplan/buildingplan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Burrows.h"
#include "modules/World.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/burrow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"

#include "modules/Burrows.h"
Expand Down
6 changes: 2 additions & 4 deletions plugins/cxxrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ Updated: Dec. 21 2017
#include <stdio.h>
#include <stdlib.h>

#include "Error.h"
#include "DataFuncs.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include "PluginManager.h"
#include "PluginLua.h"

using namespace DFHack;
DFHACK_PLUGIN("cxxrandom");
Expand Down
2 changes: 2 additions & 0 deletions plugins/design.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "df/graphic_viewportst.h"
#include "df/world.h"
#include "modules/Gui.h"
Expand Down
3 changes: 0 additions & 3 deletions plugins/devel/eventExample.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "DataDefs.h"
#include "VTableInterpose.h"

#include "modules/EventManager.h"

Expand Down
1 change: 1 addition & 0 deletions plugins/dig-now.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"

#include "modules/Buildings.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/dig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "LuaTools.h"
#include "MemAccess.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/EventManager.h"
#include "modules/Gui.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/dwarfvet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Persistence.h"
#include "modules/Units.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/eventful.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "VTableInterpose.h"

#include "modules/EventManager.h"
Expand Down
3 changes: 3 additions & 0 deletions plugins/examples/skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "Debug.h"
#include "MemAccess.h"
#include "PluginManager.h"
// this include is only required if the plugin is going to bind to Lua
// events, functions, or commands
// #include "PluginLua.h"

#include "modules/Gui.h"
#include "modules/Persistence.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/fix-occupancy.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Buildings.h"
#include "modules/Maps.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/hotkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

DFHACK_PLUGIN("hotkeys");

Expand Down
1 change: 1 addition & 0 deletions plugins/infinite-sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/EventManager.h"
#include "modules/Maps.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/liquids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Export.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"

#include "modules/Gui.h"
Expand Down
1 change: 1 addition & 0 deletions plugins/logistics.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"

#include "modules/Buildings.h"
#include "modules/Job.h"
Expand Down
Loading
Loading