8#ifndef fflua_luabridge_h
9#define fflua_luabridge_h
11#ifndef DOXYGEN_SHOULD_IGNORE_THIS
20#define PDK_FRAMEWORK_LUAFRIENDLY_CPP
23#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wdocumentation"
27#include "LuaBridge/LuaBridge.h"
28#include "LuaBridge/Vector.h"
29#include "LuaBridge/Map.h"
31#if LUABRIDGE_MAJOR_VERSION >= 3
35using RefCountedPtr = std::shared_ptr<T>;
38static std::shared_ptr<T> makeLuaSharedPtr(T *p) {
return std::shared_ptr<T>(p, [](T* p){
delete p; }); }
40#include "LuaBridge/RefCountedPtr.h"
42static luabridge::RefCountedPtr<T> makeLuaSharedPtr(T *p) {
return luabridge::RefCountedPtr<T>(p); }
45#if LUABRIDGE_MAJOR_VERSION >= 3
47#define LB3_PARM(C) , C
56#pragma GCC diagnostic pop
59#if LUABRIDGE_MAJOR_VERSION < 3
65 typename std::enable_if<std::is_enum<T>::value,
void>::type
66 push(lua_State* L, T value) {
67 lua_pushnumber(L,
static_cast<std::size_t
>(value));
71 typename std::enable_if<std::is_enum<T>::value, T>::type
72 get(lua_State* L,
int index) {
73 return static_cast<T
>(lua_tointeger(L, index));
81constexpr char LUARUN_BASE_STATE_KEY[] =
"___LuaRun_BaseStateKey$$$___";
84inline lua_State* _GetBaseLuaState(lua_State *L)
86 lua_pushstring(L, LUARUN_BASE_STATE_KEY);
87 lua_gettable(L, LUA_REGISTRYINDEX);
88 lua_State* baseState = lua_tothread(L, -1);
97inline lua_State* _CreateLuaState()
99 lua_State* L = luaL_newstate();
101 lua_pushstring(L, LUARUN_BASE_STATE_KEY);
103 lua_settable(L, LUA_REGISTRYINDEX);
119template<
class FC,
typename OPT>
120FC* _OneOptionalParamLuaConstructor(
void* ptr, lua_State *L)
122 const int numArgs = lua_gettop(L);
123 if (numArgs <= 2)
return new(ptr) FC();
124 OPT optParam = luabridge::Stack<OPT>::get(L, 2) LB3(.value());
125 return new(ptr) FC(optParam);
138template <class C, typename RT, RT (C::*func)() const>
139int _CFunctionProxyGetter(lua_State* L)
141 const C* instance = luabridge::Stack<const C*>::get(L, 1) LB3(.value());
142 RT result = (instance->*func)();
143 luabridge::Stack<RT>::push(L, result) LB3(.throw_on_error());
155template <class C, typename RT,
void (C::*func)(RT)>
156int _CFunctionProxySetter(lua_State* L)
158 C* instance = luabridge::Stack<C*>::get(L, 1) LB3(.value());
159 luabridge::LuaRef ref = luabridge::Stack<luabridge::LuaRef>::get(L, 2) LB3(.value());
160 if constexpr(std::is_same<RT, const
char *>::value)
166 if (! ref.isString())
168 (instance->*func)(lua_isnil(L, 2) ?
"" : luaL_checkstring(L, 2));
172 (instance->*func)(ref.cast<RT>() LB3(.value()));
176template <
class C,
typename RT,
typename PT,
typename F,
typename I>
177int _CFunctionOneOptionalParameterImpl(lua_State *L, F func, I instance, PT default_value)
179 const int numArgs = lua_gettop(L);
182 PT param = (numArgs < 2) ? default_value : luabridge::Stack<PT>::get(L, 2) LB3(.value());
183 if constexpr(std::is_void<RT>::value)
184 ((*instance).*func)(param);
187 luabridge::Stack<RT>::push(L, ((*instance).*func)(param)) LB3(.throw_on_error());
193template <
class C,
typename RT,
typename PT, RT (C::*func)(PT)>
194int _CFunctionOneOptionalParameter(lua_State *L, PT default_value)
196 C* instance = luabridge::Stack<C*>::get(L, 1) LB3(.value());
197 return _CFunctionOneOptionalParameterImpl<C, RT, PT, decltype(func), C*>(L, func, instance, default_value);
200template <class C, typename RT, typename PT, RT (C::*func)(PT) const>
201int _CFunctionOneOptionalParameter(lua_State *L, PT default_value)
203 const C* instance = luabridge::Stack<const C*>::get(L, 1) LB3(.value());
204 return _CFunctionOneOptionalParameterImpl<C, RT, PT, decltype(func), const C*>(L, func, instance, default_value);
207template <class C, typename RT, typename PT, RT (C::*func)(PT, lua_State*)>
208int _CFunctionOneOptionalParameterWithState(lua_State* L, PT default_value)
210 const int numArgs = lua_gettop(L);
211 assert(numArgs >= 1);
212 C* instance = luabridge::Stack<C*>::get(L, 1) LB3(.value());
213 PT param = (numArgs < 2) ? default_value : luabridge::Stack<PT>::get(L, 2) LB3(.value());
214 luabridge::Stack<RT>::push(L, ((*instance).*func)(param, L)) LB3(.throw_on_error());
221extern lua_CFunction LuaRun_ErrorFunction;