2012-03-25 04:06:05 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "DataIdentity.h"
|
|
|
|
#include "LuaWrapper.h"
|
|
|
|
|
|
|
|
namespace df {
|
2012-03-26 00:46:51 -06:00
|
|
|
// A very simple and stupid implementation of some stuff from boost
|
|
|
|
template<class U, class V> struct is_same_type { static const bool value = false; };
|
|
|
|
template<class T> struct is_same_type<T,T> { static const bool value = true; };
|
|
|
|
template<class T> struct return_type {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Workaround for a msvc bug suggested by:
|
|
|
|
*
|
|
|
|
* http://stackoverflow.com/questions/5110529/class-template-partial-specialization-parametrized-on-member-function-return-typ
|
|
|
|
*/
|
|
|
|
template<class T, bool isvoid = is_same_type<typename return_type<T>::type,void>::value>
|
|
|
|
struct function_wrapper {};
|
2012-03-25 04:06:05 -06:00
|
|
|
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
class DFHACK_EXPORT cur_lua_ostream_argument {
|
2012-04-05 09:55:59 -06:00
|
|
|
DFHack::color_ostream *out;
|
|
|
|
public:
|
|
|
|
cur_lua_ostream_argument(lua_State *state);
|
|
|
|
operator DFHack::color_ostream& () { return *out; }
|
|
|
|
};
|
|
|
|
|
2012-03-25 04:06:05 -06:00
|
|
|
/*
|
|
|
|
* Since templates can't match variable arg count,
|
|
|
|
* a separate specialization is needed for every
|
|
|
|
* supported count value...
|
|
|
|
*
|
|
|
|
* The FW_TARGS ugliness is needed because of
|
|
|
|
* commas not wrapped in ()
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define INVOKE_VOID(call) \
|
|
|
|
call; lua_pushnil(state);
|
|
|
|
#define INVOKE_RV(call) \
|
|
|
|
RT rv = call; df::identity_traits<RT>::get()->lua_read(state, UPVAL_METHOD_NAME, &rv);
|
|
|
|
#define LOAD_CLASS() \
|
|
|
|
CT *self = (CT*)DFHack::LuaWrapper::get_object_addr(state, base++, UPVAL_METHOD_NAME, "invoke");
|
|
|
|
#define LOAD_ARG(type) \
|
|
|
|
type v##type; df::identity_traits<type>::get()->lua_write(state, UPVAL_METHOD_NAME, &v##type, base++);
|
2012-04-05 01:59:39 -06:00
|
|
|
#define OSTREAM_ARG DFHack::color_ostream&
|
|
|
|
#define LOAD_OSTREAM(name) \
|
2012-04-05 09:55:59 -06:00
|
|
|
cur_lua_ostream_argument name(state);
|
2012-03-25 04:06:05 -06:00
|
|
|
|
2012-04-05 01:59:39 -06:00
|
|
|
#define INSTANTIATE_RETURN_TYPE(FArgs) \
|
2012-03-26 00:46:51 -06:00
|
|
|
template<FW_TARGSC class RT> struct return_type<RT (*) FArgs> { typedef RT type; }; \
|
2012-04-05 01:59:39 -06:00
|
|
|
template<FW_TARGSC class RT, class CT> struct return_type<RT (CT::*) FArgs> { typedef RT type; };
|
|
|
|
|
|
|
|
#define INSTANTIATE_WRAPPERS(Count, FArgs, Args, Loads) \
|
2012-03-26 00:46:51 -06:00
|
|
|
template<FW_TARGS> struct function_wrapper<void (*) FArgs, true> { \
|
2012-03-25 04:06:05 -06:00
|
|
|
static const bool is_method = false; \
|
|
|
|
static const int num_args = Count; \
|
|
|
|
static void execute(lua_State *state, int base, void (*cb) FArgs) { Loads; INVOKE_VOID(cb Args); } \
|
|
|
|
}; \
|
2012-03-26 00:46:51 -06:00
|
|
|
template<FW_TARGSC class RT> struct function_wrapper<RT (*) FArgs, false> { \
|
2012-03-25 04:06:05 -06:00
|
|
|
static const bool is_method = false; \
|
|
|
|
static const int num_args = Count; \
|
|
|
|
static void execute(lua_State *state, int base, RT (*cb) FArgs) { Loads; INVOKE_RV(cb Args); } \
|
|
|
|
}; \
|
2012-03-26 00:46:51 -06:00
|
|
|
template<FW_TARGSC class CT> struct function_wrapper<void (CT::*) FArgs, true> { \
|
2012-03-25 04:06:05 -06:00
|
|
|
static const bool is_method = true; \
|
|
|
|
static const int num_args = Count+1; \
|
|
|
|
static void execute(lua_State *state, int base, void (CT::*cb) FArgs) { \
|
|
|
|
LOAD_CLASS() Loads; INVOKE_VOID((self->*cb) Args); } \
|
|
|
|
}; \
|
2012-03-26 00:46:51 -06:00
|
|
|
template<FW_TARGSC class RT, class CT> struct function_wrapper<RT (CT::*) FArgs, false> { \
|
2012-03-25 04:06:05 -06:00
|
|
|
static const bool is_method = true; \
|
|
|
|
static const int num_args = Count+1; \
|
|
|
|
static void execute(lua_State *state, int base, RT (CT::*cb) FArgs) { \
|
|
|
|
LOAD_CLASS(); Loads; INVOKE_RV((self->*cb) Args); } \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FW_TARGSC
|
|
|
|
#define FW_TARGS
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_RETURN_TYPE(())
|
2012-03-25 04:06:05 -06:00
|
|
|
INSTANTIATE_WRAPPERS(0, (), (), ;)
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_WRAPPERS(0, (OSTREAM_ARG), (out), LOAD_OSTREAM(out);)
|
2012-03-25 04:06:05 -06:00
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#undef FW_TARGSC
|
|
|
|
#define FW_TARGSC FW_TARGS,
|
|
|
|
#define FW_TARGS class A1
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_RETURN_TYPE((A1))
|
2012-03-25 04:06:05 -06:00
|
|
|
INSTANTIATE_WRAPPERS(1, (A1), (vA1), LOAD_ARG(A1);)
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_WRAPPERS(1, (OSTREAM_ARG,A1), (out,vA1), LOAD_OSTREAM(out); LOAD_ARG(A1);)
|
2012-03-25 04:06:05 -06:00
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2))
|
2012-03-25 04:06:05 -06:00
|
|
|
INSTANTIATE_WRAPPERS(2, (A1,A2), (vA1,vA2), LOAD_ARG(A1); LOAD_ARG(A2);)
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_WRAPPERS(2, (OSTREAM_ARG,A1,A2), (out,vA1,vA2),
|
|
|
|
LOAD_OSTREAM(out); LOAD_ARG(A1); LOAD_ARG(A2);)
|
2012-03-25 04:06:05 -06:00
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2, class A3
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2,A3))
|
2012-03-25 04:06:05 -06:00
|
|
|
INSTANTIATE_WRAPPERS(3, (A1,A2,A3), (vA1,vA2,vA3), LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3);)
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_WRAPPERS(3, (OSTREAM_ARG,A1,A2,A3), (out,vA1,vA2,vA3),
|
|
|
|
LOAD_OSTREAM(out); LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3);)
|
2012-03-25 04:06:05 -06:00
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2, class A3, class A4
|
2012-04-05 01:59:39 -06:00
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2,A3,A4))
|
2012-03-25 04:06:05 -06:00
|
|
|
INSTANTIATE_WRAPPERS(4, (A1,A2,A3,A4), (vA1,vA2,vA3,vA4),
|
|
|
|
LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3); LOAD_ARG(A4);)
|
2012-04-11 09:42:05 -06:00
|
|
|
INSTANTIATE_WRAPPERS(4, (OSTREAM_ARG,A1,A2,A3,A4), (out,vA1,vA2,vA3,vA4),
|
|
|
|
LOAD_OSTREAM(out); LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3); LOAD_ARG(A4);)
|
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2, class A3, class A4, class A5
|
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2,A3,A4,A5))
|
|
|
|
INSTANTIATE_WRAPPERS(5, (A1,A2,A3,A4,A5), (vA1,vA2,vA3,vA4,vA5),
|
|
|
|
LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3); LOAD_ARG(A4); LOAD_ARG(A5);)
|
2012-05-12 10:54:26 -06:00
|
|
|
INSTANTIATE_WRAPPERS(5, (OSTREAM_ARG,A1,A2,A3,A4,A5), (out,vA1,vA2,vA3,vA4,vA5),
|
|
|
|
LOAD_OSTREAM(out); LOAD_ARG(A1); LOAD_ARG(A2);
|
|
|
|
LOAD_ARG(A3); LOAD_ARG(A4); LOAD_ARG(A5);)
|
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2, class A3, class A4, class A5, class A6
|
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2,A3,A4,A5,A6))
|
|
|
|
INSTANTIATE_WRAPPERS(6, (A1,A2,A3,A4,A5,A6), (vA1,vA2,vA3,vA4,vA5,vA6),
|
|
|
|
LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3);
|
|
|
|
LOAD_ARG(A4); LOAD_ARG(A5); LOAD_ARG(A6);)
|
|
|
|
INSTANTIATE_WRAPPERS(6, (OSTREAM_ARG,A1,A2,A3,A4,A5,A6), (out,vA1,vA2,vA3,vA4,vA5,vA6),
|
|
|
|
LOAD_OSTREAM(out); LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3);
|
|
|
|
LOAD_ARG(A4); LOAD_ARG(A5); LOAD_ARG(A6);)
|
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2, class A3, class A4, class A5, class A6, class A7
|
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2,A3,A4,A5,A6,A7))
|
2012-06-05 04:00:52 -06:00
|
|
|
INSTANTIATE_WRAPPERS(7, (A1,A2,A3,A4,A5,A6,A7), (vA1,vA2,vA3,vA4,vA5,vA6,vA7),
|
|
|
|
LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3);
|
|
|
|
LOAD_ARG(A4); LOAD_ARG(A5); LOAD_ARG(A6);
|
|
|
|
LOAD_ARG(A7);)
|
|
|
|
INSTANTIATE_WRAPPERS(7, (OSTREAM_ARG,A1,A2,A3,A4,A5,A6,A7), (out,vA1,vA2,vA3,vA4,vA5,vA6,vA7),
|
|
|
|
LOAD_OSTREAM(out); LOAD_ARG(A1); LOAD_ARG(A2); LOAD_ARG(A3);
|
|
|
|
LOAD_ARG(A4); LOAD_ARG(A5); LOAD_ARG(A6); LOAD_ARG(A7);)
|
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#define FW_TARGS class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8
|
|
|
|
INSTANTIATE_RETURN_TYPE((A1,A2,A3,A4,A5,A6,A7,A8))
|
2012-03-25 04:06:05 -06:00
|
|
|
#undef FW_TARGS
|
|
|
|
|
|
|
|
#undef FW_TARGSC
|
|
|
|
#undef INSTANTIATE_WRAPPERS
|
|
|
|
#undef INVOKE_VOID
|
|
|
|
#undef INVOKE_RV
|
|
|
|
#undef LOAD_CLASS
|
|
|
|
#undef LOAD_ARG
|
2012-04-05 01:59:39 -06:00
|
|
|
#undef OSTREAM_ARG
|
|
|
|
#undef LOAD_OSTREAM
|
2012-03-25 04:06:05 -06:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class function_identity : public function_identity_base {
|
|
|
|
T ptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef function_wrapper<T> wrapper;
|
|
|
|
|
2012-04-15 09:09:25 -06:00
|
|
|
function_identity(T ptr, bool vararg)
|
|
|
|
: function_identity_base(wrapper::num_args, vararg), ptr(ptr) {};
|
2012-03-25 04:06:05 -06:00
|
|
|
|
|
|
|
virtual void invoke(lua_State *state, int base) { wrapper::execute(state, base, ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
2012-04-15 09:09:25 -06:00
|
|
|
inline function_identity_base *wrap_function(T ptr, bool vararg = false) {
|
2012-03-25 11:12:20 -06:00
|
|
|
// bah, but didn't have any idea how to allocate statically
|
2012-04-15 09:09:25 -06:00
|
|
|
return new function_identity<T>(ptr, vararg);
|
2012-03-25 04:06:05 -06:00
|
|
|
}
|
|
|
|
}
|