25 #ifndef __I6ENGINE_MODULES_LUASCRIPTINGMANAGER_H__
26 #define __I6ENGINE_MODULES_LUASCRIPTINGMANAGER_H__
38 #include "i6engine/luabind/luabind.hpp"
42 class ScriptingFacade;
46 class LuaScriptingMailbox;
68 template<
typename Ret,
typename... args>
69 typename std::enable_if<std::is_void<Ret>::value, Ret>::type
callScript(
const std::string & file,
const std::string & func, args... B) {
70 _callScripts.push(std::bind([
this, file, func](args... A) {
71 ASSERT_THREAD_SAFETY_FUNCTION
72 if (!parseScript(file, false)) {
76 lua_getglobal(_luaState, func.c_str());
77 assert(lua_isfunction(_luaState, -1));
78 luabind::object o(luabind::from_stack(_luaState, -1));
79 luabind::call_function<Ret>(o, A...);
80 } catch (
const luabind::error & e) {
81 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
82 }
catch (
const std::exception & e) {
83 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
91 template<
typename Ret,
typename... args>
92 typename std::enable_if<!std::is_void<Ret>::value, std::shared_ptr<utils::Future<Ret>>>::type
callScript(
const std::string & file,
const std::string & func, args... B) {
93 std::shared_ptr<utils::Future<Ret>> ret = std::make_shared<utils::Future<Ret>>();
94 _callScripts.push(std::bind([
this, file, func, ret](args... A) {
95 ASSERT_THREAD_SAFETY_FUNCTION
96 if (!parseScript(file, false)) {
100 lua_getglobal(_luaState, func.c_str());
101 assert(lua_isfunction(_luaState, -1));
102 luabind::object o(luabind::from_stack(_luaState, -1));
103 ret->push(Ret(luabind::call_function<Ret>(o, A...)));
104 } catch (
const luabind::error & e) {
105 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
106 }
catch (
const std::exception & e) {
107 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
116 template<
typename Ret,
typename... args>
117 typename std::enable_if<std::is_void<Ret>::value, Ret>::type
callFunction(
const std::string & func, args... B) {
118 _callScripts.push(std::bind([
this, func](args... A) {
119 ASSERT_THREAD_SAFETY_FUNCTION
121 lua_getglobal(_luaState, func.c_str());
122 assert(lua_isfunction(_luaState, -1));
123 luabind::object o(luabind::from_stack(_luaState, -1));
124 luabind::call_function<Ret>(o, A...);
125 } catch (
const luabind::error & e) {
126 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
127 }
catch (
const std::exception & e) {
128 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
136 template<
typename Ret,
typename... args>
137 typename std::enable_if<!std::is_void<Ret>::value, std::shared_ptr<utils::Future<Ret>>>::type
callFunction(
const std::string & func, args... B) {
138 std::shared_ptr<utils::Future<Ret>> ret = std::make_shared<utils::Future<Ret>>();
139 _callScripts.push(std::bind([
this, func, ret](args... A) {
140 ASSERT_THREAD_SAFETY_FUNCTION
142 lua_getglobal(_luaState, func.c_str());
143 assert(lua_isfunction(_luaState, -1));
144 luabind::object o(luabind::from_stack(_luaState, -1));
145 ret->push(Ret(luabind::call_function<Ret>(o, A...)));
146 } catch (
const luabind::error & e) {
147 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
148 }
catch (
const std::exception & e) {
149 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
158 template<
typename Ret,
typename... args>
159 typename std::enable_if<std::is_void<Ret>::value, Ret>::type
callScriptWithCallback(
const std::string & file,
const std::string & func,
const std::function<
void(
void)> & callback, args... B) {
160 _callScripts.push(std::bind([
this, file, func, callback](args... A) {
161 ASSERT_THREAD_SAFETY_FUNCTION
162 if (!parseScript(file, false)) {
166 lua_getglobal(_luaState, func.c_str());
167 assert(lua_isfunction(_luaState, -1));
168 luabind::object o(luabind::from_stack(_luaState, -1));
169 luabind::call_function<Ret>(o, A...);
170 } catch (
const luabind::error & e) {
171 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
172 }
catch (
const std::exception & e) {
173 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
182 template<
typename Ret,
typename... args>
183 typename std::enable_if<!std::is_void<Ret>::value, std::shared_ptr<utils::Future<Ret>>>::type
callScriptWithCallback(
const std::string & file,
const std::string & func,
const std::function<
void(
void)> & callback, args... B) {
184 std::shared_ptr<utils::Future<Ret>> ret = std::make_shared<utils::Future<Ret>>();
185 _callScripts.push(std::bind([
this, file, func, callback, ret](args... A) {
186 ASSERT_THREAD_SAFETY_FUNCTION
187 if (!parseScript(file, false)) {
191 lua_getglobal(_luaState, func.c_str());
192 assert(lua_isfunction(_luaState, -1));
193 luabind::object o(luabind::from_stack(_luaState, -1));
194 ret->push(Ret(luabind::call_function<Ret>(o, A...)));
195 } catch (
const luabind::error & e) {
196 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
197 }
catch (
const std::exception & e) {
198 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"' in script '" << file <<
".lua': " << e.what() <<
": " << lua_tostring(_luaState, -1));
208 template<
typename Ret,
typename... args>
209 typename std::enable_if<std::is_void<Ret>::value, Ret>::type
callFunctionWithCallback(
const std::string & func,
const std::function<
void(
void)> & callback, args... B) {
210 _callScripts.push(std::bind([
this, func, callback](args... A) {
211 ASSERT_THREAD_SAFETY_FUNCTION
213 lua_getglobal(_luaState, func.c_str());
214 assert(lua_isfunction(_luaState, -1));
215 luabind::object o(luabind::from_stack(_luaState, -1));
216 luabind::call_function<Ret>(o, A...);
217 } catch (
const luabind::error & e) {
218 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
219 }
catch (
const std::exception & e) {
220 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
229 template<
typename Ret,
typename... args>
230 typename std::enable_if<!std::is_void<Ret>::value, std::shared_ptr<utils::Future<Ret>>>::type
callFunctionWithCallback(
const std::string & func,
const std::function<
void(
void)> & callback, args... B) {
231 std::shared_ptr<utils::Future<Ret>> ret = std::make_shared<utils::Future<Ret>>();
232 _callScripts.push(std::bind([
this, func, callback, ret](args... A) {
233 ASSERT_THREAD_SAFETY_FUNCTION
235 lua_getglobal(_luaState, func.c_str());
236 assert(lua_isfunction(_luaState, -1));
237 luabind::object o(luabind::from_stack(_luaState, -1));
238 ret->push(Ret(luabind::call_function<Ret>(o, A...)));
239 } catch (
const luabind::error & e) {
240 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
241 }
catch (
const std::exception & e) {
242 ISIXE_THROW_FAILURE(
"LuaScriptingManager",
"Error calling function '" << func <<
"': " << e.what() <<
": " << lua_tostring(_luaState, -1));
253 typename std::enable_if<std::is_pointer<T>::value>::type
setGlobalVariable(
const std::string & name, T value) {
254 _callScripts.push(std::bind([
this, name, value]() {
256 luabind::globals(_luaState)[name] = value;
263 void loadAllScripts();
266 lua_State * _luaState;
267 std::set<std::string> _parsedFiles;
268 std::string _scriptsPath;
279 bool parseScript(
const std::string & file,
bool completePath);
Handles the incoming messages on the scripting channel and calls the corresponding methods...
#define ASSERT_THREAD_SAFETY_FUNCTION
#define ISIXE_MODULES_API
std::enable_if<!std::is_void< Ret >::value, std::shared_ptr< utils::Future< Ret > > >::type callFunctionWithCallback(const std::string &func, const std::function< void(void)> &callback, args...B)
executes the given method with return type non-void and calls callback after scripts was executed ...
boost::shared_ptr< Message > Ptr
std::enable_if< std::is_pointer< T >::value >::type setGlobalVariable(const std::string &name, T value)
sets a global variable
std::enable_if< std::is_void< Ret >::value, Ret >::type callScript(const std::string &file, const std::string &func, args...B)
executes the given method in the given script with return type void
std::enable_if< std::is_void< Ret >::value, Ret >::type callFunctionWithCallback(const std::string &func, const std::function< void(void)> &callback, args...B)
executes the given method with return type void and calls callback after scripts was executed ...
std::enable_if<!std::is_void< Ret >::value, std::shared_ptr< utils::Future< Ret > > >::type callFunction(const std::string &func, args...B)
executes the given method with return type non-void
std::enable_if< std::is_void< Ret >::value, Ret >::type callScriptWithCallback(const std::string &file, const std::string &func, const std::function< void(void)> &callback, args...B)
executes the given method in the given script with return type void and calls callback after scripts ...
#define ASSERT_THREAD_SAFETY_HEADER
std::enable_if< std::is_void< Ret >::value, Ret >::type callFunction(const std::string &func, args...B)
executes the given method with return type void
#define ISIXE_THROW_FAILURE(module, message)
std::enable_if<!std::is_void< Ret >::value, std::shared_ptr< utils::Future< Ret > > >::type callScript(const std::string &file, const std::string &func, args...B)
executes the given method in the given script with return type non-void
std::enable_if<!std::is_void< Ret >::value, std::shared_ptr< utils::Future< Ret > > >::type callScriptWithCallback(const std::string &file, const std::string &func, const std::function< void(void)> &callback, args...B)
executes the given method in the given script with return type non-void and calls callback after scri...