From cb4aa0ade3ec622d7ba676b1dc3bb63f81d74eb2 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Mon, 1 Aug 2011 09:08:11 +0000 Subject: [PATCH] Script: Fixed that returning "" in Update() displayed 0 due to r885. --- Library/Measure.cpp | 2 +- Library/MeasureScript.cpp | 21 +++++++++++++++------ Library/MeasureScript.h | 4 +++- Library/lua/LuaScript.cpp | 14 +++++--------- Library/lua/LuaScript.h | 2 +- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 9ae049bb..e2cc1d38 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -830,7 +830,7 @@ CMeasure* CMeasure::Create(const WCHAR* measure, CMeterWindow* meterWindow, cons { return new CMeasureCalc(meterWindow, name); } - else if (_wcsicmp(L"script", measure) == 0) + else if (_wcsicmp(L"Script", measure) == 0) { return new CMeasureScript(meterWindow, name); } diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 842aa74f..b2add2b7 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -34,7 +34,8 @@ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : C m_LuaScript(), m_HasInitializeFunction(false), m_HasUpdateFunction(false), - m_HasGetStringFunction(false) + m_HasGetStringFunction(false), + m_ValueType(LUA_TNIL) { LuaManager::Init(); } @@ -95,11 +96,19 @@ bool CMeasureScript::Update() return false; } - if (!(m_HasUpdateFunction && m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue)) && - !(m_HasGetStringFunction && m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue))) + if (m_HasUpdateFunction) { - m_Value = 0; - m_StringValue.clear(); + m_ValueType = m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue); + + if (m_ValueType == LUA_TSTRING) + { + m_Value = 0; + } + else if (m_ValueType == LUA_TNIL && m_HasGetStringFunction) + { + // For backwards compatbility + m_ValueType = m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue); + } } return PostUpdate(); @@ -113,7 +122,7 @@ bool CMeasureScript::Update() */ const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual) { - if (!m_StringValue.empty()) + if (m_ValueType == LUA_TSTRING) { return CheckSubstitute(m_StringValue.c_str()); } diff --git a/Library/MeasureScript.h b/Library/MeasureScript.h index 0c0792c8..8c18d805 100644 --- a/Library/MeasureScript.h +++ b/Library/MeasureScript.h @@ -37,11 +37,13 @@ public: protected: LuaScript* m_LuaScript; - + bool m_HasInitializeFunction; bool m_HasUpdateFunction; bool m_HasGetStringFunction; + int m_ValueType; + std::wstring m_StringValue; std::string m_ScriptFile; diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index 03334b89..b141ae92 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -147,12 +147,12 @@ void LuaScript::RunFunction(const char* funcName) ** RunFunctionWithReturn ** ** Runs given function in script file and stores the retruned number or string. -** Returns true if the executed function returns a valid value. +** Returns LUA_TNIL when no return. ** */ -bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue) +int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue) { - bool ret = false; + int ret = LUA_TNIL; if (m_Initialized) { @@ -172,17 +172,13 @@ bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, st if (lua_isnumber(m_State, -1)) { numValue = lua_tonumber(m_State, -1); - - strValue.clear(); - ret = true; + ret = LUA_TNUMBER; } else if (lua_isstring(m_State, -1)) { const char* str = lua_tostring(m_State, -1); strValue = ConvertToWide(str); - - numValue = 0; - ret = true; + ret = LUA_TSTRING; } lua_pop(m_State, 2); diff --git a/Library/lua/LuaScript.h b/Library/lua/LuaScript.h index 25c0ab53..9102a80f 100644 --- a/Library/lua/LuaScript.h +++ b/Library/lua/LuaScript.h @@ -32,7 +32,7 @@ public: bool IsFunction(const char* funcName); void RunFunction(const char* funcName); - bool RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue); + int RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue); void RunString(const char* str); protected: