From f0fbc0f1456b5f7660bb1410a62dc654988bd1d3 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 9 Jul 2011 09:23:28 +0000 Subject: [PATCH] - Added !CommandMeasure bang. Instead of '!PluginBang "MeasureName Arguments' use '!CommandMeasure "MeasureName" "Arguments"'. - Script: The !CommandMeasure argument must now be Lua code. For example: !CommandMeasure "MeasureLuaScript" "someVar = 'hello'" !CommandMeasure "MeasureLuaScript" "SomeFunc()" --- Library/MeasureScript.cpp | 14 ++------------ Library/MeterWindow.cpp | 38 +++++++++++++++++++++++++++++++++----- Library/MeterWindow.h | 2 +- Library/Rainmeter.cpp | 15 +++++++++++++++ Library/Rainmeter.h | 1 + Library/lua/LuaScript.cpp | 29 +++++++++++++++++++++++++++++ Library/lua/LuaScript.h | 3 +-- 7 files changed, 82 insertions(+), 20 deletions(-) diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 0bbc4114..bf9f4113 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -242,18 +242,8 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section) */ void CMeasureScript::ExecuteBang(const WCHAR* args) { - std::string function = ConvertToAscii(args); - - if (m_LuaScript->IsFunction(function.c_str())) - { - m_LuaScript->RunFunction(function.c_str()); - } - else - { - std::wstring error = L"Script: Function \""; - error += args; - error += L"\" does not exist."; - } + std::string str = ConvertToAscii(args); + m_LuaScript->RunString(str.c_str()); } static void stackDump(lua_State *L) diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index f4c04e4d..2c0f7be6 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -824,7 +824,7 @@ void CMeterWindow::RunBang(BANGCOMMAND bang, const WCHAR* arg) break; case BANG_MOVE: - pos = wcschr(arg, ' '); + pos = wcschr(arg, L' '); if (pos != NULL) { MoveWindow(_wtoi(arg), _wtoi(pos)); @@ -879,7 +879,7 @@ void CMeterWindow::RunBang(BANGCOMMAND bang, const WCHAR* arg) case BANG_LSHOOK: { - pos = wcsrchr(arg, ' '); + pos = wcsrchr(arg, L' '); if (pos != NULL) { #ifdef _WIN64 @@ -905,10 +905,10 @@ void CMeterWindow::RunBang(BANGCOMMAND bang, const WCHAR* arg) break; case BANG_MOVEMETER: - pos = wcschr(arg, ' '); + pos = wcschr(arg, L' '); if (pos != NULL) { - pos2 = wcschr(pos + 1, ' '); + pos2 = wcschr(pos + 1, L' '); if (pos2 != NULL) { MoveMeter(_wtoi(arg), _wtoi(pos), pos2 + 1); @@ -924,6 +924,34 @@ void CMeterWindow::RunBang(BANGCOMMAND bang, const WCHAR* arg) } break; + case BANG_COMMANDMEASURE: + { + std::wstring args = arg; + std::wstring measure; + std::wstring::size_type pos3; + + pos3 = args.find(L' '); + if (pos3 != std::wstring::npos) + { + measure = args.substr(0, pos3); + args.erase(0, ++pos3); + + CMeasure* m = GetMeasure(measure); + if (m) + { + m->ExecuteBang(args.c_str()); + return; + } + + LogWithArgs(LOG_WARNING, L"Unable to find [%s] for !CommandMeasure", measure.c_str()); + } + else + { + Log(LOG_ERROR, L"Unable to parse parameters for !CommandMeasure"); + } + } + break; + case BANG_PLUGIN: { std::wstring args = arg; @@ -970,7 +998,7 @@ void CMeterWindow::RunBang(BANGCOMMAND bang, const WCHAR* arg) break; case BANG_SETVARIABLE: - pos = wcschr(arg, ' '); + pos = wcschr(arg, L' '); if (pos != NULL) { std::wstring strVariable(arg, pos - arg); diff --git a/Library/MeterWindow.h b/Library/MeterWindow.h index cef539e5..37948339 100644 --- a/Library/MeterWindow.h +++ b/Library/MeterWindow.h @@ -19,7 +19,6 @@ #ifndef __METERWINDOW_H__ #define __METERWINDOW_H__ - #include #include #include @@ -120,6 +119,7 @@ enum BANGCOMMAND BANG_ENABLEMEASURE, BANG_DISABLEMEASURE, BANG_UPDATEMEASURE, + BANG_COMMANDMEASURE, BANG_SHOWBLUR, BANG_HIDEBLUR, BANG_TOGGLEBLUR, diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index a87e974b..ad33ff22 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -680,6 +680,17 @@ void RainmeterUpdateMeasure(HWND, const char* arg) BangWithArgs(BANG_UPDATEMEASURE, ConvertToWide(arg).c_str(), 1); } +/* +** RainmeterCommandMeasure +** +** Callback for the !RainmeterCommandMeasure bang +** +*/ +void RainmeterCommandMeasure(HWND, const char* arg) +{ + BangWithArgs(BANG_COMMANDMEASURE, ConvertToWide(arg).c_str(), 2); +} + /* ** RainmeterRefresh ** @@ -2871,6 +2882,10 @@ BOOL CRainmeter::ExecuteBang(const std::wstring& bang, const std::wstring& arg, { BangWithArgs(BANG_UPDATEMEASURE, arg.c_str(), 1); } + else if (_wcsicmp(name, L"CommandMeasure") == 0) + { + BangWithArgs(BANG_COMMANDMEASURE, arg.c_str(), 2); + } else if (_wcsicmp(name, L"ShowBlur") == 0) { BangWithArgs(BANG_SHOWBLUR, arg.c_str(), 0); diff --git a/Library/Rainmeter.h b/Library/Rainmeter.h index 857affba..720118d0 100644 --- a/Library/Rainmeter.h +++ b/Library/Rainmeter.h @@ -61,6 +61,7 @@ void RainmeterEnableMeasure(HWND, const char* arg); void RainmeterDisableMeasure(HWND, const char* arg); void RainmeterToggleMeasure(HWND, const char* arg); void RainmeterUpdateMeasure(HWND, const char* arg); +void RainmeterCommandMeasure(HWND, const char* arg); void RainmeterActivateConfig(HWND, const char* arg); void RainmeterDeactivateConfig(HWND, const char* arg); void RainmeterToggleConfig(HWND, const char* arg); diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index 86df0b03..a8d869ee 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -189,6 +189,35 @@ bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, st return ret; } +/* +** RunString +** +** Runs given string in the context of the script file. +** +*/ +void LuaScript::RunString(const char* str) +{ + if (m_Initialized) + { + // Load the string as a Lua chunk + if (luaL_loadstring(m_State, str)) + { + LuaManager::ReportErrors(m_State); + } + + // Push our table onto the stack + PushTable(); + + // Pop table and set the environment of the loaded chunk to it + lua_setfenv(m_State, -2); + + if (lua_pcall(m_State, 0, 0, 0)) + { + LuaManager::ReportErrors(m_State); + } + } +} + //void LuaScript::BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName) //{ // PushTable(); diff --git a/Library/lua/LuaScript.h b/Library/lua/LuaScript.h index d021b160..25c0ab53 100644 --- a/Library/lua/LuaScript.h +++ b/Library/lua/LuaScript.h @@ -33,8 +33,7 @@ public: bool IsFunction(const char* funcName); void RunFunction(const char* funcName); bool RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue); - - static void ReportErrors(lua_State* L); + void RunString(const char* str); protected: lua_State* m_State;