Additional changes to a4844ca

This commit is contained in:
Birunthan Mohanathas 2012-10-06 22:03:29 +03:00
parent a4844ca924
commit 1b235dbae8
6 changed files with 27 additions and 30 deletions

View File

@ -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);

View File

@ -47,8 +47,6 @@ private:
int m_ValueType;
std::wstring m_StringValue;
std::wstring m_ScriptFile;
};
#endif

View File

@ -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)

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -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