From 1b235dbae8264b763986f6345b9383e0a2332e41 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 6 Oct 2012 22:03:29 +0300 Subject: [PATCH] Additional changes to a4844ca --- Library/MeasureScript.cpp | 6 ++---- Library/MeasureScript.h | 2 -- Library/lua/LuaManager.cpp | 19 ++++++++----------- Library/lua/LuaManager.h | 2 +- Library/lua/LuaScript.cpp | 21 +++++++++++---------- Library/lua/LuaScript.h | 7 +++++-- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 44088795..70b0797b 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -103,14 +103,12 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) } if (!m_Initialized || - wcscmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0) + wcscmp(scriptFile.c_str(), m_LuaScript.GetFile().c_str()) != 0) { UninitializeLuaScript(); lua_State* L = LuaManager::GetState(); - m_ScriptFile = scriptFile; - - if (m_LuaScript.Initialize(m_ScriptFile.c_str())) + if (m_LuaScript.Initialize(scriptFile)) { bool hasInitializeFunction = m_LuaScript.IsFunction(g_InitializeFunctionName); m_HasUpdateFunction = m_LuaScript.IsFunction(g_UpdateFunctionName); diff --git a/Library/MeasureScript.h b/Library/MeasureScript.h index 48079bb5..069d943f 100644 --- a/Library/MeasureScript.h +++ b/Library/MeasureScript.h @@ -47,8 +47,6 @@ private: int m_ValueType; std::wstring m_StringValue; - - std::wstring m_ScriptFile; }; #endif \ No newline at end of file diff --git a/Library/lua/LuaManager.cpp b/Library/lua/LuaManager.cpp index 332e8525..f130e384 100644 --- a/Library/lua/LuaManager.cpp +++ b/Library/lua/LuaManager.cpp @@ -56,24 +56,21 @@ void LuaManager::Finalize() } } -void LuaManager::ReportErrors(lua_State* L) +void LuaManager::ReportErrors(lua_State* L, const std::wstring& file) { const char* error = lua_tostring(L, -1); lua_pop(L, 1); - const char* pos = error + 4; // Skip the drive - - // Get rid of everything up to the filename - while (*pos != ':') + // Skip "[string ...]". + const char* pos = strchr(error, ':'); + if (pos) { - if (*pos == '\\') - { - error = pos + 1; - } - ++pos; + error = pos; } - LogWithArgs(LOG_ERROR, L"Script: %S", error); + std::wstring str(file, file.find_last_of(L'\\') + 1); + str += ConvertToWide(error); + LogWithArgs(LOG_ERROR, L"Script: %s", str.c_str()); } void LuaManager::PushWide(lua_State* L, const WCHAR* str) diff --git a/Library/lua/LuaManager.h b/Library/lua/LuaManager.h index 94fcac9d..33d0a5f5 100644 --- a/Library/lua/LuaManager.h +++ b/Library/lua/LuaManager.h @@ -34,7 +34,7 @@ public: static lua_State* GetState() { return c_State; } - static void ReportErrors(lua_State* L); + static void ReportErrors(lua_State* L, const std::wstring& file); static void PushWide(lua_State* L, const WCHAR* str); static std::wstring ToWide(lua_State* L, int narg); diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index b4170b92..53ba6be5 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -39,14 +39,14 @@ LuaScript::~LuaScript() Uninitialize(); } -bool LuaScript::Initialize(const WCHAR* scriptFile) +bool LuaScript::Initialize(const std::wstring& 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"); + FILE* file = _wfopen(scriptFile.c_str(), L"rb"); if (!file) { return false; @@ -59,8 +59,7 @@ bool LuaScript::Initialize(const WCHAR* scriptFile) 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()); + int load = luaL_loadbuffer(L, fileData, fileSize, ""); delete [] fileData; if (load == 0) @@ -93,17 +92,18 @@ bool LuaScript::Initialize(const WCHAR* scriptFile) int result = lua_pcall(L, 0, 0, 0); if (result == 0) { + m_File = scriptFile; return true; } else { - LuaManager::ReportErrors(L); + LuaManager::ReportErrors(L, scriptFile); Uninitialize(); } } else { - LuaManager::ReportErrors(L); + LuaManager::ReportErrors(L, scriptFile); } return false; @@ -117,6 +117,7 @@ void LuaScript::Uninitialize() { luaL_unref(L, LUA_GLOBALSINDEX, m_Ref); m_Ref = LUA_NOREF; + m_File.clear(); } } @@ -167,7 +168,7 @@ void LuaScript::RunFunction(const char* funcName) if (lua_pcall(L, 0, 0, 0)) { - LuaManager::ReportErrors(L); + LuaManager::ReportErrors(L, m_File); } lua_pop(L, 1); @@ -193,7 +194,7 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std if (lua_pcall(L, 0, 1, 0)) { - LuaManager::ReportErrors(L); + LuaManager::ReportErrors(L, m_File); lua_pop(L, 1); } else @@ -230,7 +231,7 @@ void LuaScript::RunString(const char* str) // Load the string as a Lua chunk if (luaL_loadstring(L, str)) { - LuaManager::ReportErrors(L); + LuaManager::ReportErrors(L, m_File); } // Push our table onto the stack @@ -241,7 +242,7 @@ void LuaScript::RunString(const char* str) if (lua_pcall(L, 0, 0, 0)) { - LuaManager::ReportErrors(L); + LuaManager::ReportErrors(L, m_File); } } } diff --git a/Library/lua/LuaScript.h b/Library/lua/LuaScript.h index d18ae08a..c963fd0a 100644 --- a/Library/lua/LuaScript.h +++ b/Library/lua/LuaScript.h @@ -27,11 +27,12 @@ public: LuaScript(); ~LuaScript(); - bool Initialize(const WCHAR* scriptFile); + bool Initialize(const std::wstring& scriptFile); void Uninitialize(); - bool IsInitialized() { return m_Ref != LUA_NOREF; } + int GetRef() { return m_Ref; } + const std::wstring& GetFile() { return m_File; } bool IsFunction(const char* funcName); void RunFunction(const char* funcName); @@ -40,6 +41,8 @@ public: protected: int m_Ref; + + std::wstring m_File; }; #endif