Script: Reverted Unicode change (99c67f7)

This commit is contained in:
Birunthan Mohanathas 2012-07-21 19:46:00 +03:00
parent b66875d8ad
commit 850b415a3f
6 changed files with 36 additions and 69 deletions

View File

@ -106,14 +106,15 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
{ {
m_MeterWindow->MakePathAbsolute(file); m_MeterWindow->MakePathAbsolute(file);
} }
std::string scriptFile = ConvertToAscii(file.c_str());
if (!m_Initialized || if (!m_Initialized ||
wcscmp(file.c_str(), m_ScriptFile.c_str()) != 0) strcmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0)
{ {
DeleteLuaScript(); DeleteLuaScript();
lua_State* L = LuaManager::GetState(); lua_State* L = LuaManager::GetState();
m_ScriptFile = file; m_ScriptFile = scriptFile;
m_LuaScript = new LuaScript(m_ScriptFile.c_str()); m_LuaScript = new LuaScript(m_ScriptFile.c_str());
if (m_LuaScript->IsInitialized()) if (m_LuaScript->IsInitialized())
@ -156,7 +157,10 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
if (!wstrValue.empty()) 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); lua_setfield(L, -3, strKey);
} }
} }
@ -189,7 +193,7 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
*/ */
void CMeasureScript::Command(const std::wstring& command) 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()); m_LuaScript->RunString(str.c_str());
} }

View File

@ -48,7 +48,7 @@ private:
std::wstring m_StringValue; std::wstring m_StringValue;
std::wstring m_ScriptFile; std::string m_ScriptFile;
}; };
#endif #endif

View File

@ -61,15 +61,27 @@ void LuaManager::ReportErrors(lua_State* L)
const char* error = lua_tostring(L, -1); const char* error = lua_tostring(L, -1);
lua_pop(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); LogWithArgs(LOG_ERROR, L"Script: %S", error);
} }
void LuaManager::PushWide(lua_State* L, const WCHAR* str) 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) std::wstring LuaManager::ToWide(lua_State* L, int narg)
{ {
return ConvertUTF8ToWide(lua_tostring(L, narg)); return ConvertToWide(lua_tostring(L, narg));
} }

View File

@ -25,62 +25,15 @@
** The constructor ** The constructor
** **
*/ */
LuaScript::LuaScript(const WCHAR* scriptFile) : LuaScript::LuaScript(const char* file) :
m_Ref(LUA_NOREF), m_Ref(LUA_NOREF),
m_Initialized(false) m_Initialized(true)
{ {
lua_State* L = LuaManager::GetState(); lua_State* L = LuaManager::GetState();
int result = luaL_loadfile(L, file);
FILE* file = _wfopen(scriptFile, L"rb"); // If the file loaded okay.
if (!file) if (result == 0)
{
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)
{ {
// Create the table this script will reside in // Create the table this script will reside in
lua_newtable(L); lua_newtable(L);
@ -107,12 +60,11 @@ LuaScript::LuaScript(const WCHAR* scriptFile) :
lua_setfenv(L, -2); lua_setfenv(L, -2);
// Execute the Lua script // Execute the Lua script
if (lua_pcall(L, 0, 0, 0) == 0) result = lua_pcall(L, 0, 0, 0);
{
m_Initialized = true; if (result)
}
else
{ {
m_Initialized = false;
LuaManager::ReportErrors(L); LuaManager::ReportErrors(L);
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref); luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
@ -121,10 +73,9 @@ LuaScript::LuaScript(const WCHAR* scriptFile) :
} }
else else
{ {
m_Initialized = false;
LuaManager::ReportErrors(L); LuaManager::ReportErrors(L);
} }
fclose(file);
} }
/* /*
@ -223,7 +174,7 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std
else if (type == LUA_TSTRING) else if (type == LUA_TSTRING)
{ {
const char* str = lua_tostring(L, -1); const char* str = lua_tostring(L, -1);
strValue = ConvertUTF8ToWide(str); strValue = ConvertToWide(str);
numValue = strtod(str, NULL); numValue = strtod(str, NULL);
} }

View File

@ -24,7 +24,7 @@
class LuaScript class LuaScript
{ {
public: public:
LuaScript(const WCHAR* scriptFile); LuaScript(const char* file);
~LuaScript(); ~LuaScript();
bool IsInitialized() { return m_Initialized; } bool IsInitialized() { return m_Initialized; }

View File

@ -53,7 +53,7 @@ static int Print(lua_State* L)
lua_pop(L, 1); lua_pop(L, 1);
} }
Log(LOG_DEBUG, ConvertUTF8ToWide(message.c_str()).c_str()); Log(LOG_DEBUG, ConvertToWide(message.c_str()).c_str());
return 0; return 0;
} }