Script: Change Unicode script support to require UTF-16 LE files rather than UTF-8

This commit is contained in:
Birunthan Mohanathas 2013-08-07 17:09:25 +03:00
parent d0466c5588
commit 20393df751

View File

@ -55,21 +55,21 @@ bool LuaScript::Initialize(const std::wstring& scriptFile)
fclose(file);
file = nullptr;
// Has UTF8 BOM, so assume that data is already in UTF8.
char* scriptData = (char*)fileData;
int scriptDataSize = fileSize;
lua_State* L = GetState();
bool scriptLoaded = false;
// Treat the script as Unicode if it has the UTF-8 BOM.
m_Unicode = fileSize > 3 && fileData[0] == 0xEF && fileData[1] == 0xBB && fileData[2] == 0xBF;
// Treat the script as Unicode if it has the UTF-16 LE BOM.
m_Unicode = fileSize > 2 && fileData[0] == 0xFF && fileData[1] == 0xFE;
if (m_Unicode)
{
// Skip the BOM.
scriptData += 3;
scriptDataSize -= 3;
const std::string utf8Data =
StringUtil::NarrowUTF8((WCHAR*)(fileData + 2), (fileSize - 2) / sizeof(WCHAR));
scriptLoaded = luaL_loadbuffer(L, utf8Data.c_str(), utf8Data.length(), "") == 0;
}
else
{
scriptLoaded = luaL_loadbuffer(L, (char*)fileData, fileSize, "") == 0;
}
lua_State* L = GetState();
bool scriptLoaded = luaL_loadbuffer(L, scriptData, scriptDataSize, "") == 0;
delete [] fileData;