From b99b275f445c9b3e32eea7768ea58fa294fbede7 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Thu, 7 Jul 2011 16:18:39 +0000 Subject: [PATCH] Script: - Added support for calling Lua functions with !PluginBang - Removed old (undocumented) implementation for handling mouse actions - Lua errors now contain the script file name only (instead of full path) --- Library/MeasureScript.cpp | 89 +++++++--------------------------- Library/MeasureScript.h | 4 +- Library/MeterWindow.cpp | 19 +------- Library/MeterWindow.h | 1 - Library/lua/LuaManager.cpp | 24 +++++---- Library/lua/LuaScript.cpp | 6 +-- Library/lua/glue/LuaGlobal.cpp | 3 +- 7 files changed, 39 insertions(+), 107 deletions(-) diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 496d332e..0bbc4114 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -234,107 +234,52 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section) } } -void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter) +/* +** ExecuteBang +** +** Sends a bang to the measure. +** +*/ +void CMeasureScript::ExecuteBang(const WCHAR* args) { - // Get the Lua State - lua_State* L = m_LuaScript->GetState(); + std::string function = ConvertToAscii(args); - // Push the script table - m_LuaScript->PushTable(); - - // Push the function onto the stack - lua_getfield(L, -1, p_strFunction); - - // Check if the function exists - if (!lua_isnil(L, -1)) + if (m_LuaScript->IsFunction(function.c_str())) { - // Push the Meter - tolua_pushusertype(L, (void*) p_pMeter, "CMeter"); - - if (lua_pcall(L, 1, 0, 0)) - { - LuaManager::ReportErrors(L); - } - - lua_pop(L, 1); + m_LuaScript->RunFunction(function.c_str()); } else { - lua_pop(L, 2); - } -} - -void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse) -{ - if (m_LuaScript && m_LuaScript->IsInitialized()) - { - switch (p_eMouse) - { - case MOUSE_LMB_DOWN: - RunFunctionWithMeter("LeftMouseDown", p_pMeter); - break; - - case MOUSE_LMB_UP: - RunFunctionWithMeter("LeftMouseUp", p_pMeter); - break; - - case MOUSE_LMB_DBLCLK: - RunFunctionWithMeter("LeftMouseDoubleClick", p_pMeter); - break; - - case MOUSE_RMB_DOWN: - RunFunctionWithMeter("RightMouseDown", p_pMeter); - break; - - case MOUSE_RMB_UP: - RunFunctionWithMeter("RightMouseUp", p_pMeter); - break; - - case MOUSE_RMB_DBLCLK: - RunFunctionWithMeter("RightMouseDoubleClick", p_pMeter); - break; - - case MOUSE_MMB_DOWN: - RunFunctionWithMeter("MiddleMouseDown", p_pMeter); - break; - - case MOUSE_MMB_UP: - RunFunctionWithMeter("MiddleMouseUp", p_pMeter); - break; - - case MOUSE_MMB_DBLCLK: - RunFunctionWithMeter("MiddleMouseDoubleClick", p_pMeter); - break; - } + std::wstring error = L"Script: Function \""; + error += args; + error += L"\" does not exist."; } } static void stackDump(lua_State *L) { - int i = lua_gettop(L); LuaManager::LuaLog(LOG_DEBUG, " ---------------- Stack Dump ----------------" ); - while (i) + for (int i = lua_gettop(L); i > 0; --i) { int t = lua_type(L, i); switch (t) { case LUA_TSTRING: - LuaManager::LuaLog(LOG_DEBUG, "%d:`%s'", i, lua_tostring(L, i)); + LuaManager::LuaLog(LOG_DEBUG, "%d:'%s'", i, lua_tostring(L, i)); break; case LUA_TBOOLEAN: - LuaManager::LuaLog(LOG_DEBUG, "%d: %s",i,lua_toboolean(L, i) ? "true" : "false"); + LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_toboolean(L, i) ? "true" : "false"); break; case LUA_TNUMBER: - LuaManager::LuaLog(LOG_DEBUG, "%d: %g", i, lua_tonumber(L, i)); + LuaManager::LuaLog(LOG_DEBUG, "%d: %g", i, lua_tonumber(L, i)); break; default: LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t)); break; } - i--; } LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" ); } diff --git a/Library/MeasureScript.h b/Library/MeasureScript.h index e2366b54..0955bd15 100644 --- a/Library/MeasureScript.h +++ b/Library/MeasureScript.h @@ -31,12 +31,10 @@ public: virtual void Initialize(); virtual bool Update(); virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual); + virtual void ExecuteBang(const WCHAR* args); void DeleteLuaScript(); - void MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse); - void RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter); - protected: LuaScript* m_LuaScript; diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index 4cbf07e7..36d72791 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -352,7 +352,6 @@ void CMeterWindow::Refresh(bool init, bool all) delete (*i); } m_Measures.clear(); - m_ScriptMeasures.clear(); std::list::iterator j = m_Meters.begin(); for ( ; j != m_Meters.end(); ++j) @@ -2140,17 +2139,9 @@ bool CMeterWindow::ReadSkin() m_Measures.push_back(measure); m_Parser.AddMeasure(measure); - CMeasureScript* measureScript = dynamic_cast(measure); - if (measureScript) + if (!m_HasNetMeasures && dynamic_cast(measure)) { - m_ScriptMeasures.push_back(measureScript); - } - else - { - if (!m_HasNetMeasures && dynamic_cast(measure)) - { - m_HasNetMeasures = true; - } + m_HasNetMeasures = true; } } } @@ -4442,12 +4433,6 @@ bool CMeterWindow::DoAction(int x, int y, MOUSE mouse, bool test) if ((*j)->HitTest(x, y)) { - std::list::iterator k = m_ScriptMeasures.begin(); - for ( ; k != m_ScriptMeasures.end(); ++k) - { - (*k)->MeterMouseEvent((*j), mouse); - } - switch (mouse) { case MOUSE_LMB_DOWN: diff --git a/Library/MeterWindow.h b/Library/MeterWindow.h index 6141c725..cef539e5 100644 --- a/Library/MeterWindow.h +++ b/Library/MeterWindow.h @@ -418,7 +418,6 @@ private: bool m_ResetRegion; // If true, the window region is recalculated during the next update std::list m_Measures; // All the measures - std::list m_ScriptMeasures;// All the measures std::list m_Meters; // All the meters const std::wstring m_SkinPath; // Path of the skin folder diff --git a/Library/lua/LuaManager.cpp b/Library/lua/LuaManager.cpp index ba1ccce5..205933e2 100644 --- a/Library/lua/LuaManager.cpp +++ b/Library/lua/LuaManager.cpp @@ -67,8 +67,21 @@ void LuaManager::CleanUp() void LuaManager::ReportErrors(lua_State* L) { - LuaLog(LOG_ERROR, "Script: %s", lua_tostring(L, -1)); + std::string error = lua_tostring(L, -1); lua_pop(L, 1); + + // Get rid of everything up to the filename + std::string::size_type pos = 4; // Skip the drive + pos = error.find_first_of(':', pos); + pos = error.find_last_of('\\', pos); + if (pos != std::string::npos) + { + error.erase(0, ++pos); + } + + std::wstring str = L"Script: "; + str += ConvertToWide(error.c_str()); + Log(LOG_ERROR, str.c_str()); } void LuaManager::LuaLog(int nLevel, const char* format, ... ) @@ -89,15 +102,8 @@ void LuaManager::LuaLog(int nLevel, const char* format, ... ) _set_invalid_parameter_handler(oldHandler); -#ifndef _DEBUG - // Forcing output to the Debug Output window! - OutputDebugStringA(buffer); - OutputDebugStringA("\n"); -#endif - std::wstring str = ConvertToWide(buffer); - - LSLog(nLevel, L"Rainmeter", str.c_str()); + Log(nLevel, str.c_str()); va_end(args); delete [] buffer; diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index 34d59d10..86df0b03 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -64,8 +64,7 @@ LuaScript::LuaScript(lua_State* state, const char* file) : m_State(state), if (result) { m_Initialized = false; - LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_State, -1)); - lua_pop(m_State, 1); + LuaManager::ReportErrors(m_State); luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef); m_iRef = LUA_NOREF; @@ -74,8 +73,7 @@ LuaScript::LuaScript(lua_State* state, const char* file) : m_State(state), else { m_Initialized = false; - LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_State, -1)); - lua_pop(m_State, 1); + LuaManager::ReportErrors(m_State); } } diff --git a/Library/lua/glue/LuaGlobal.cpp b/Library/lua/glue/LuaGlobal.cpp index e032d7e4..16306113 100644 --- a/Library/lua/glue/LuaGlobal.cpp +++ b/Library/lua/glue/LuaGlobal.cpp @@ -12,7 +12,8 @@ static int Global_Log(lua_State* L) static const luaL_reg TO_funcs[] = { - { "LuaLog", Global_Log }, { NULL, NULL } + { "LuaLog", Global_Log }, + { NULL, NULL } }; void LuaManager::RegisterGlobal(lua_State* L)