diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 8a347175..44088795 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -29,7 +29,6 @@ const char* g_GetStringFunctionName = "GetStringValue"; ** */ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name), - m_LuaScript(), m_HasUpdateFunction(false), m_HasGetStringFunction(false), m_ValueType(LUA_TNIL) @@ -43,19 +42,16 @@ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : C */ CMeasureScript::~CMeasureScript() { - DeleteLuaScript(); + UninitializeLuaScript(); LuaManager::Finalize(); } -void CMeasureScript::DeleteLuaScript() +void CMeasureScript::UninitializeLuaScript() { - delete m_LuaScript; - m_LuaScript = NULL; + m_LuaScript.Uninitialize(); m_HasUpdateFunction = false; m_HasGetStringFunction = false; - - m_ScriptFile.clear(); } /* @@ -66,12 +62,12 @@ void CMeasureScript::UpdateValue() { if (m_HasUpdateFunction) { - m_ValueType = m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue); + m_ValueType = m_LuaScript.RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue); if (m_ValueType == LUA_TNIL && m_HasGetStringFunction) { // For backwards compatbility - m_ValueType = m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue); + m_ValueType = m_LuaScript.RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue); } } } @@ -98,37 +94,34 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) { CMeasure::ReadOptions(parser, section); - std::wstring file = parser.ReadString(section, L"ScriptFile", L""); - - if (!file.empty()) + std::wstring scriptFile = parser.ReadString(section, L"ScriptFile", L""); + if (!scriptFile.empty()) { if (m_MeterWindow) { - m_MeterWindow->MakePathAbsolute(file); + m_MeterWindow->MakePathAbsolute(scriptFile); } - std::string scriptFile = ConvertToAscii(file.c_str()); if (!m_Initialized || - strcmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0) + wcscmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0) { - DeleteLuaScript(); + UninitializeLuaScript(); lua_State* L = LuaManager::GetState(); m_ScriptFile = scriptFile; - m_LuaScript = new LuaScript(m_ScriptFile.c_str()); - if (m_LuaScript->IsInitialized()) + if (m_LuaScript.Initialize(m_ScriptFile.c_str())) { - bool hasInitializeFunction = m_LuaScript->IsFunction(g_InitializeFunctionName); - m_HasUpdateFunction = m_LuaScript->IsFunction(g_UpdateFunctionName); - m_HasGetStringFunction = m_LuaScript->IsFunction(g_GetStringFunctionName); // For backwards compatbility + bool hasInitializeFunction = m_LuaScript.IsFunction(g_InitializeFunctionName); + m_HasUpdateFunction = m_LuaScript.IsFunction(g_UpdateFunctionName); + m_HasGetStringFunction = m_LuaScript.IsFunction(g_GetStringFunctionName); // For backwards compatbility if (m_HasGetStringFunction) { LogWithArgs(LOG_WARNING, L"Script: Using deprecated GetStringValue() in [%s]", m_Name.c_str()); } - lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript->GetRef()); + lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript.GetRef()); *(CMeterWindow**)lua_newuserdata(L, sizeof(CMeterWindow*)) = m_MeterWindow; lua_getglobal(L, "CMeterWindow"); @@ -171,20 +164,17 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) if (hasInitializeFunction) { - m_LuaScript->RunFunction(g_InitializeFunctionName); + m_LuaScript.RunFunction(g_InitializeFunctionName); } - } - else - { - DeleteLuaScript(); + + // Valid script. + return; } } } - else - { - LogWithArgs(LOG_ERROR, L"Script: File not valid in [%s]", m_Name.c_str()); - DeleteLuaScript(); - } + + LogWithArgs(LOG_ERROR, L"Script: File not valid in [%s]", m_Name.c_str()); + UninitializeLuaScript(); } /* @@ -194,7 +184,7 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) void CMeasureScript::Command(const std::wstring& command) { std::string str = ConvertToAscii(command.c_str()); - m_LuaScript->RunString(str.c_str()); + m_LuaScript.RunString(str.c_str()); } //static void stackDump(lua_State *L) diff --git a/Library/MeasureScript.h b/Library/MeasureScript.h index 7fba2cb1..48079bb5 100644 --- a/Library/MeasureScript.h +++ b/Library/MeasureScript.h @@ -32,14 +32,14 @@ public: virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual); virtual void Command(const std::wstring& command); - void DeleteLuaScript(); + void UninitializeLuaScript(); protected: virtual void ReadOptions(CConfigParser& parser, const WCHAR* section); virtual void UpdateValue(); private: - LuaScript* m_LuaScript; + LuaScript m_LuaScript; bool m_HasUpdateFunction; bool m_HasGetStringFunction; @@ -48,7 +48,7 @@ private: std::wstring m_StringValue; - std::string m_ScriptFile; + std::wstring m_ScriptFile; }; #endif \ No newline at end of file diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index 92fcb82d..b4170b92 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -25,15 +25,45 @@ ** The constructor ** */ -LuaScript::LuaScript(const char* file) : - m_Ref(LUA_NOREF), - m_Initialized(true) +LuaScript::LuaScript() : + m_Ref(LUA_NOREF) { - lua_State* L = LuaManager::GetState(); - int result = luaL_loadfile(L, file); +} - // If the file loaded okay. - if (result == 0) +/* +** The destructor +** +*/ +LuaScript::~LuaScript() +{ + Uninitialize(); +} + +bool LuaScript::Initialize(const WCHAR* scriptFile) +{ + assert(!IsInitialized()); + + lua_State* L = LuaManager::GetState(); + + // Load file into a buffer as luaL_loadfile does not support Unicode paths. + FILE* file = _wfopen(scriptFile, L"rb"); + if (!file) + { + return false; + } + + fseek(file, 0, SEEK_END); + long fileSize = ftell(file); + + char* fileData = new char[fileSize]; + fseek(file, 0, SEEK_SET); + fread(fileData, fileSize, 1, file); + + std::string scriptName = ConvertToUTF8(wcsrchr(scriptFile, L'\\') + 1); + int load = luaL_loadbuffer(L, fileData, fileSize, scriptName.c_str()); + delete [] fileData; + + if (load == 0) { // Create the table this script will reside in lua_newtable(L); @@ -60,32 +90,34 @@ LuaScript::LuaScript(const char* file) : lua_setfenv(L, -2); // Execute the Lua script - result = lua_pcall(L, 0, 0, 0); - - if (result) + int result = lua_pcall(L, 0, 0, 0); + if (result == 0) + { + return true; + } + else { - m_Initialized = false; LuaManager::ReportErrors(L); - - luaL_unref(L, LUA_GLOBALSINDEX, m_Ref); - m_Ref = LUA_NOREF; + Uninitialize(); } } else { - m_Initialized = false; LuaManager::ReportErrors(L); } + + return false; } -/* -** The destructor -** -*/ -LuaScript::~LuaScript() +void LuaScript::Uninitialize() { lua_State* L = LuaManager::GetState(); - luaL_unref(L, LUA_GLOBALSINDEX, m_Ref); + + if (m_Ref != LUA_NOREF) + { + luaL_unref(L, LUA_GLOBALSINDEX, m_Ref); + m_Ref = LUA_NOREF; + } } /* @@ -97,7 +129,7 @@ bool LuaScript::IsFunction(const char* funcName) lua_State* L = LuaManager::GetState(); bool bExists = false; - if (m_Initialized) + if (IsInitialized()) { // Push our table onto the stack lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref); @@ -125,7 +157,7 @@ void LuaScript::RunFunction(const char* funcName) { lua_State* L = LuaManager::GetState(); - if (m_Initialized) + if (IsInitialized()) { // Push our table onto the stack lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref); @@ -151,7 +183,7 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std lua_State* L = LuaManager::GetState(); int type = LUA_TNIL; - if (m_Initialized) + if (IsInitialized()) { // Push our table onto the stack lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref); @@ -193,7 +225,7 @@ void LuaScript::RunString(const char* str) { lua_State* L = LuaManager::GetState(); - if (m_Initialized) + if (IsInitialized()) { // Load the string as a Lua chunk if (luaL_loadstring(L, str)) diff --git a/Library/lua/LuaScript.h b/Library/lua/LuaScript.h index d87f792e..d18ae08a 100644 --- a/Library/lua/LuaScript.h +++ b/Library/lua/LuaScript.h @@ -24,10 +24,13 @@ class LuaScript { public: - LuaScript(const char* file); + LuaScript(); ~LuaScript(); - - bool IsInitialized() { return m_Initialized; } + + bool Initialize(const WCHAR* scriptFile); + void Uninitialize(); + + bool IsInitialized() { return m_Ref != LUA_NOREF; } int GetRef() { return m_Ref; } bool IsFunction(const char* funcName); @@ -37,7 +40,6 @@ public: protected: int m_Ref; - bool m_Initialized; }; #endif