From 0cb5facd7a892128385f1313621cb5696b1c43fe Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 6 Jan 2012 01:29:26 -0200 Subject: [PATCH] lua binder improvments --- modules/core_lib/core_lib.otmod | 1 + modules/core_lib/ext/string.lua | 8 -- modules/core_lib/util.lua | 8 ++ src/framework/core/configmanager.cpp | 22 +++++ src/framework/core/configmanager.h | 7 +- src/framework/luafunctions.cpp | 23 +++++- src/framework/luascript/luabinder.h | 39 ++++++--- src/framework/luascript/luainterface.h | 80 +++++++++---------- src/otclient/luafunctions.cpp | 10 +-- .../generate_lua_bindings.lua | 2 +- 10 files changed, 132 insertions(+), 68 deletions(-) diff --git a/modules/core_lib/core_lib.otmod b/modules/core_lib/core_lib.otmod index 0107de4e..9eea1d17 100644 --- a/modules/core_lib/core_lib.otmod +++ b/modules/core_lib/core_lib.otmod @@ -17,3 +17,4 @@ Module require 'dispatcher' require 'widget' require 'effects' + require 'settings' diff --git a/modules/core_lib/ext/string.lua b/modules/core_lib/ext/string.lua index b3bb10b6..5b20b8f6 100644 --- a/modules/core_lib/ext/string.lua +++ b/modules/core_lib/ext/string.lua @@ -16,11 +16,3 @@ end function string:trim() return self:match('^%s*(.*%S)') or '' end - -function toboolean(str) - str = str:trim() - if str == '1' or str == 'true' then - return true - end - return false -end diff --git a/modules/core_lib/util.lua b/modules/core_lib/util.lua index a2787709..23cfd059 100644 --- a/modules/core_lib/util.lua +++ b/modules/core_lib/util.lua @@ -68,3 +68,11 @@ function resolvepath(filePath, depth) return filePath end end + +function toboolean(str) + str = str:trim():lower() + if str == '1' or str == 'true' then + return true + end + return false +end diff --git a/src/framework/core/configmanager.cpp b/src/framework/core/configmanager.cpp index e2038103..252c02be 100644 --- a/src/framework/core/configmanager.cpp +++ b/src/framework/core/configmanager.cpp @@ -54,3 +54,25 @@ bool ConfigManager::save() } return doc->save(m_fileName); } + +bool ConfigManager::exists(const std::string& key) +{ + return m_confsMap.find(key) != m_confsMap.end(); +} + +void ConfigManager::set(const std::string& key, const std::string& value) +{ + m_confsMap[key] = value; +} + +std::string ConfigManager::get(const std::string& key) +{ + return m_confsMap[key]; +} + +void ConfigManager::remove(const std::string& key) +{ + auto it = m_confsMap.find(key); + if(it != m_confsMap.end()) + m_confsMap.erase(it); +} diff --git a/src/framework/core/configmanager.h b/src/framework/core/configmanager.h index da5a21d2..cca797c2 100644 --- a/src/framework/core/configmanager.h +++ b/src/framework/core/configmanager.h @@ -31,9 +31,10 @@ public: bool load(const std::string& file); bool save(); - bool exists(const std::string& key) { return m_confsMap.find(key) != m_confsMap.end(); } - void set(const std::string& key, const std::string& value) { m_confsMap[key] = value; } - std::string get(const std::string& key) { return m_confsMap[key]; } + bool exists(const std::string& key); + void set(const std::string& key, const std::string& value); + std::string get(const std::string& key); + void remove(const std::string& key); private: std::string m_fileName; diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index fed39515..ca878208 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -31,11 +31,29 @@ #include #include +class LuaRect : public LuaObject, public Rect { +public: + LuaRect() : TRect() { } +}; + void Application::registerLuaFunctions() { + // globals + g_lua.registerStaticClass("Rect"); + g_lua.bindClassStaticFunction("Rect", "create", []{ return std::shared_ptr(new LuaRect); }); + g_lua.bindClassMemberFunction("Rect", "resize", (void (Rect::*)(int,int)) &Rect::resize); + + /* + g_lua.bindGlobalFunction("torect", [](const std::string& str) { return Fw::unsafeCast(str); }); + g_lua.bindGlobalFunction("topoint", [](const std::string& str) { return Fw::unsafeCast(str); }); + g_lua.bindGlobalFunction("tocolor", [](const std::string& str) { return Fw::unsafeCast(str); }); + g_lua.bindGlobalFunction("tosize", [](const std::string& str) { return Fw::unsafeCast(str); }); + g_lua.bindGlobalFunction("toboolean", [](const std::string& str) { return Fw::unsafeCast(str); }); + */ + // UIWidget g_lua.registerClass(); - g_lua.bindClassStaticFunction("create", &UIWidget::create); + g_lua.bindClassStaticFunction("create", []{ return UIWidgetPtr(new UIWidget); } ); g_lua.bindClassMemberFunction("destroy", &UIWidget::destroy); g_lua.bindClassMemberFunction("setVisible", &UIWidget::setVisible); g_lua.bindClassMemberFunction("setEnabled", &UIWidget::setEnabled); @@ -219,7 +237,7 @@ void Application::registerLuaFunctions() // UIFrameCounter g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &UIWidget::create); - g_lua.bindClassMemberFunction("getFrameCount", &UIFrameCounter::getFrameCount); + g_lua.bindClassMemberFunction("getFrameCount", &UIFrameCounter::getFrameCount); // Protocol g_lua.registerClass(); @@ -230,6 +248,7 @@ void Application::registerLuaFunctions() g_lua.bindClassStaticFunction("g_configs", "get", std::bind(&ConfigManager::get, &g_configs, _1)); g_lua.bindClassStaticFunction("g_configs", "exists", std::bind(&ConfigManager::exists, &g_configs, _1)); + g_lua.registerStaticClass("g_window"); g_lua.bindClassStaticFunction("g_window", "show", std::bind(&PlatformWindow::show, &g_window)); g_lua.bindClassStaticFunction("g_window", "hide", std::bind(&PlatformWindow::hide, &g_window)); diff --git a/src/framework/luascript/luabinder.h b/src/framework/luascript/luabinder.h index 94538ed3..16202800 100644 --- a/src/framework/luascript/luabinder.h +++ b/src/framework/luascript/luabinder.h @@ -113,6 +113,27 @@ namespace luabinder Tuple>(f); } + /// Specialization for lambdas + template + struct bind_lambda_fun; + + template + struct bind_lambda_fun { + static LuaCppFunction call(const Lambda& f) { + typedef typename std::tuple::type...> Tuple; + return bind_fun_specializer::type, + decltype(f), + Tuple>(f); + + } + }; + + template + typename std::enable_if::value, LuaCppFunction>::type bind_fun(const Lambda& f) { + typedef decltype(&Lambda::operator()) F; + return bind_lambda_fun::call(f); + } + /// Bind a customized functions template<> inline @@ -207,29 +228,29 @@ namespace luabinder } /// Bind member functions - template - LuaCppFunction bind_mem_fun(Ret (Obj::*f)(Args...)) { + template + LuaCppFunction bind_mem_fun(Ret (FC::*f)(Args...)) { auto mf = std::mem_fn(f); - typedef typename std::tuple, typename remove_const_ref::type...> Tuple; + typedef typename std::tuple, typename remove_const_ref::type...> Tuple; return bind_fun_specializer::type, decltype(mf), Tuple>(mf); } - template - LuaCppFunction bind_mem_fun(Ret (Obj::*f)(Args...) const) { + template + LuaCppFunction bind_mem_fun(Ret (FC::*f)(Args...) const) { auto mf = std::mem_fn(f); - typedef typename std::tuple, typename remove_const_ref::type...> Tuple; + typedef typename std::tuple, typename remove_const_ref::type...> Tuple; return bind_fun_specializer::type, decltype(mf), Tuple>(mf); } /// Bind customized member functions - template - LuaCppFunction bind_mem_fun(int (Obj::*f)(LuaInterface*)) { + template + LuaCppFunction bind_mem_fun(int (C::*f)(LuaInterface*)) { auto mf = std::mem_fn(f); return [=](LuaInterface* lua) { - auto obj = lua->castValue>(1); + auto obj = lua->castValue>(1); lua->remove(1); return mf(obj, lua); }; diff --git a/src/framework/luascript/luainterface.h b/src/framework/luascript/luainterface.h index 63eca66d..58aa4f68 100644 --- a/src/framework/luascript/luainterface.h +++ b/src/framework/luascript/luainterface.h @@ -90,25 +90,25 @@ public: template void bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function); - template - void bindClassMemberFunction(const std::string& functionName, F C::*function); - template - void bindClassMemberFunction(const std::string& className, const std::string& functionName, F C::*function); + template + void bindClassMemberFunction(const std::string& functionName, F FC::*function); + template + void bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function); - template - void bindClassMemberField(const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction); - template - void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction); + template + void bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction); + template + void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction); - template - void bindClassMemberGetField(const std::string& fieldName, F C::*getFunction); - template - void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F C::*getFunction); + template + void bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction); + template + void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction); - template - void bindClassMemberSetField(const std::string& fieldName, F C::*setFunction); - template - void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F C::*setFunction); + template + void bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction); + template + void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction); template void bindGlobalFunction(const std::string& functionName, const F& function); @@ -336,40 +336,40 @@ void LuaInterface::bindClassStaticFunction(const std::string& className, const s registerClassStaticFunction(className, functionName, luabinder::bind_fun(function)); } -template -void LuaInterface::bindClassMemberFunction(const std::string& functionName, F C::*function) { - registerClassMemberFunction(functionName, luabinder::bind_mem_fun(function)); +template +void LuaInterface::bindClassMemberFunction(const std::string& functionName, F FC::*function) { + registerClassMemberFunction(functionName, luabinder::bind_mem_fun(function)); } -template -void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F C::*function) { - registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun(function)); +template +void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function) { + registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun(function)); } -template -void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction) { - registerClassMemberField(fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction)); +template +void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) { + registerClassMemberField(fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction)); } -template -void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction) { - registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction)); +template +void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) { + registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction)); } -template -void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F C::*getFunction) { - registerClassMemberField(fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction()); +template +void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction) { + registerClassMemberField(fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction()); } -template -void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F C::*getFunction) { - registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction()); +template +void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction) { + registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction()); } -template -void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F C::*setFunction) { - registerClassMemberField(fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction)); +template +void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction) { + registerClassMemberField(fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction)); } -template -void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F C::*setFunction) { - registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction)); +template +void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction) { + registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction)); } template diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index b6b11a58..460b71d9 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -35,8 +35,8 @@ void OTClient::registerLuaFunctions() g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &ProtocolLogin::create); - g_lua.bindClassMemberFunction("login", &ProtocolLogin::login); - g_lua.bindClassMemberFunction("cancelLogin", &ProtocolLogin::cancelLogin); + g_lua.bindClassMemberFunction("login", &ProtocolLogin::login); + g_lua.bindClassMemberFunction("cancelLogin", &ProtocolLogin::cancelLogin); g_lua.registerClass(); @@ -53,9 +53,9 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("asLocalPlayer", &Thing::asLocalPlayer); g_lua.registerClass(); - g_lua.bindClassMemberFunction("getName", &Creature::getName); - g_lua.bindClassMemberFunction("setOutfit", &Creature::setOutfit); - g_lua.bindClassMemberFunction("getOutfit", &Creature::getOutfit); + g_lua.bindClassMemberFunction("getName", &Creature::getName); + g_lua.bindClassMemberFunction("setOutfit", &Creature::setOutfit); + g_lua.bindClassMemberFunction("getOutfit", &Creature::getOutfit); g_lua.registerClass(); g_lua.registerClass(); diff --git a/tools/lua-binding-generator/generate_lua_bindings.lua b/tools/lua-binding-generator/generate_lua_bindings.lua index bc9682b7..b1b03c33 100755 --- a/tools/lua-binding-generator/generate_lua_bindings.lua +++ b/tools/lua-binding-generator/generate_lua_bindings.lua @@ -56,7 +56,7 @@ for line in io.lines(cppclassheader) do bindline = bindline .. '>();' print(bindline) - bindline = 'g_lua.bindClassStaticFunction<' .. cppclassname .. '>("create", &' .. cppclassname .. '::create);' + bindline = 'g_lua.bindClassStaticFunction<' .. cppclassname .. '>("create", []{ return ' .. cppclassname .. 'Ptr(new ' .. cppclassname .. '); });' print(bindline) end elseif classfound then