Skip to content

Commit

Permalink
Additional changes to a4844ca
Browse files Browse the repository at this point in the history
  • Loading branch information
poiru committed Oct 6, 2012
1 parent a4844ca commit 1b235db
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
6 changes: 2 additions & 4 deletions Library/MeasureScript.cpp
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions Library/MeasureScript.h
Expand Up @@ -47,8 +47,6 @@ class CMeasureScript : public CMeasure
int m_ValueType;

std::wstring m_StringValue;

std::wstring m_ScriptFile;
};

#endif
19 changes: 8 additions & 11 deletions Library/lua/LuaManager.cpp
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Library/lua/LuaManager.h
Expand Up @@ -34,7 +34,7 @@ class LuaManager

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);
Expand Down
21 changes: 11 additions & 10 deletions Library/lua/LuaScript.cpp
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -117,6 +117,7 @@ void LuaScript::Uninitialize()
{
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
m_Ref = LUA_NOREF;
m_File.clear();
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
7 changes: 5 additions & 2 deletions Library/lua/LuaScript.h
Expand Up @@ -27,11 +27,12 @@ class LuaScript
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);
Expand All @@ -40,6 +41,8 @@ class LuaScript

protected:
int m_Ref;

std::wstring m_File;
};

#endif
Expand Down

0 comments on commit 1b235db

Please sign in to comment.