From 850b415a3fc78a3f8405cc5dc23d642374ce50fd Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 21 Jul 2012 19:46:00 +0300 Subject: [PATCH] Script: Reverted Unicode change (99c67f7) --- Library/MeasureScript.cpp | 12 ++++-- Library/MeasureScript.h | 2 +- Library/lua/LuaManager.cpp | 16 +++++++- Library/lua/LuaScript.cpp | 71 ++++++---------------------------- Library/lua/LuaScript.h | 2 +- Library/lua/glue/LuaGlobal.cpp | 2 +- 6 files changed, 36 insertions(+), 69 deletions(-) diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index eca8a0a7..8a347175 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -106,14 +106,15 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) { m_MeterWindow->MakePathAbsolute(file); } + std::string scriptFile = ConvertToAscii(file.c_str()); if (!m_Initialized || - wcscmp(file.c_str(), m_ScriptFile.c_str()) != 0) + strcmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0) { DeleteLuaScript(); lua_State* L = LuaManager::GetState(); - m_ScriptFile = file; + m_ScriptFile = scriptFile; m_LuaScript = new LuaScript(m_ScriptFile.c_str()); if (m_LuaScript->IsInitialized()) @@ -156,7 +157,10 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) if (!wstrValue.empty()) { - LuaManager::PushWide(L, wstrValue.c_str()); + std::string strStrVal = ConvertToAscii(wstrValue.c_str()); + const char* strValue = strStrVal.c_str(); + + lua_pushstring(L, strValue); lua_setfield(L, -3, strKey); } } @@ -189,7 +193,7 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) */ void CMeasureScript::Command(const std::wstring& command) { - std::string str = ConvertToUTF8(command.c_str()); + std::string str = ConvertToAscii(command.c_str()); m_LuaScript->RunString(str.c_str()); } diff --git a/Library/MeasureScript.h b/Library/MeasureScript.h index 62aa1e43..7fba2cb1 100644 --- a/Library/MeasureScript.h +++ b/Library/MeasureScript.h @@ -48,7 +48,7 @@ private: std::wstring m_StringValue; - std::wstring m_ScriptFile; + std::string m_ScriptFile; }; #endif \ No newline at end of file diff --git a/Library/lua/LuaManager.cpp b/Library/lua/LuaManager.cpp index 7abd4777..332e8525 100644 --- a/Library/lua/LuaManager.cpp +++ b/Library/lua/LuaManager.cpp @@ -61,15 +61,27 @@ void LuaManager::ReportErrors(lua_State* L) 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 != ':') + { + if (*pos == '\\') + { + error = pos + 1; + } + ++pos; + } + LogWithArgs(LOG_ERROR, L"Script: %S", error); } void LuaManager::PushWide(lua_State* L, const WCHAR* str) { - lua_pushstring(L, ConvertToUTF8(str).c_str()); + lua_pushstring(L, ConvertToAscii(str).c_str()); } std::wstring LuaManager::ToWide(lua_State* L, int narg) { - return ConvertUTF8ToWide(lua_tostring(L, narg)); + return ConvertToWide(lua_tostring(L, narg)); } diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index bbb99246..92fcb82d 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -25,62 +25,15 @@ ** The constructor ** */ -LuaScript::LuaScript(const WCHAR* scriptFile) : +LuaScript::LuaScript(const char* file) : m_Ref(LUA_NOREF), - m_Initialized(false) + m_Initialized(true) { lua_State* L = LuaManager::GetState(); + int result = luaL_loadfile(L, file); - FILE* file = _wfopen(scriptFile, L"rb"); - if (!file) - { - return; - } - - fseek(file, 0, SEEK_END); - long fileSize = ftell(file); - if (fileSize < 3) - { - return; - } - - int load = 0; - std::string scriptName = ConvertToUTF8(wcsrchr(scriptFile, L'\\') + 1); - const char* scriptNameSz = scriptName.c_str(); - - BYTE* fileData = new BYTE[fileSize]; - fseek(file, 0, SEEK_SET); - fread(fileData, fileSize, 1, file); - - if (fileData[0] == 0xEF && fileData[1] == 0xBB && fileData[2] == 0xBF) - { - // Has UTF8 BOM, so assume that data is already in UTF8. - const char* utf8Data = (char*)fileData + 3; - int utf8Size = fileSize - 3; - - load = luaL_loadbuffer(L, utf8Data, utf8Size, scriptNameSz); - delete [] fileData; - } - else - { - // Convert the file contents first to WCHAR with respect to the current - // code page and then to UTF8 in order to preserve code page specific chars. - - int wideSize = MultiByteToWideChar(CP_ACP, 0, (char*)fileData, fileSize, NULL, 0); - WCHAR* wideData = new WCHAR[wideSize]; - MultiByteToWideChar(CP_ACP, 0, (char*)fileData, fileSize, wideData, wideSize); - delete [] fileData; - - int utf8Size = WideCharToMultiByte(CP_UTF8, 0, wideData, wideSize, NULL, 0, NULL, NULL); - char* utf8Data = new char[utf8Size]; - WideCharToMultiByte(CP_UTF8, 0, wideData, wideSize, utf8Data, utf8Size, NULL, NULL); - delete [] wideData; - - load = luaL_loadbuffer(L, utf8Data, utf8Size, scriptNameSz); - delete [] utf8Data; - } - - if (load == 0) + // If the file loaded okay. + if (result == 0) { // Create the table this script will reside in lua_newtable(L); @@ -107,12 +60,11 @@ LuaScript::LuaScript(const WCHAR* scriptFile) : lua_setfenv(L, -2); // Execute the Lua script - if (lua_pcall(L, 0, 0, 0) == 0) - { - m_Initialized = true; - } - else + result = lua_pcall(L, 0, 0, 0); + + if (result) { + m_Initialized = false; LuaManager::ReportErrors(L); luaL_unref(L, LUA_GLOBALSINDEX, m_Ref); @@ -121,10 +73,9 @@ LuaScript::LuaScript(const WCHAR* scriptFile) : } else { + m_Initialized = false; LuaManager::ReportErrors(L); } - - fclose(file); } /* @@ -223,7 +174,7 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std else if (type == LUA_TSTRING) { const char* str = lua_tostring(L, -1); - strValue = ConvertUTF8ToWide(str); + strValue = ConvertToWide(str); numValue = strtod(str, NULL); } diff --git a/Library/lua/LuaScript.h b/Library/lua/LuaScript.h index 5d9ff8ce..d87f792e 100644 --- a/Library/lua/LuaScript.h +++ b/Library/lua/LuaScript.h @@ -24,7 +24,7 @@ class LuaScript { public: - LuaScript(const WCHAR* scriptFile); + LuaScript(const char* file); ~LuaScript(); bool IsInitialized() { return m_Initialized; } diff --git a/Library/lua/glue/LuaGlobal.cpp b/Library/lua/glue/LuaGlobal.cpp index 94d43aa9..26f54d8b 100644 --- a/Library/lua/glue/LuaGlobal.cpp +++ b/Library/lua/glue/LuaGlobal.cpp @@ -53,7 +53,7 @@ static int Print(lua_State* L) lua_pop(L, 1); } - Log(LOG_DEBUG, ConvertUTF8ToWide(message.c_str()).c_str()); + Log(LOG_DEBUG, ConvertToWide(message.c_str()).c_str()); return 0; }