Skip to content

Commit

Permalink
Changed LuaScript.cpp to use FileUtil::ReadFullFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkort committed Nov 25, 2015
1 parent 329fe60 commit 28dfc41
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions Library/lua/LuaScript.cpp
Expand Up @@ -7,6 +7,7 @@

#include "StdAfx.h"
#include "../../Common/StringUtil.h"
#include "../../Common/FileUtil.h"
#include "LuaScript.h"
#include "LuaManager.h"

Expand All @@ -25,16 +26,12 @@ bool LuaScript::Initialize(const std::wstring& scriptFile)
{
assert(!IsInitialized());

FILE* file = _wfopen(scriptFile.c_str(), L"rb");
if (!file) return false;

fseek(file, 0, SEEK_END);
const long fileSize = ftell(file);
BYTE* fileData = new BYTE[fileSize];
fseek(file, 0, SEEK_SET);
fread(fileData, fileSize, 1, file);
fclose(file);
file = nullptr;
size_t fileSize = 0;
auto fileData = FileUtil::ReadFullFile(scriptFile, &fileSize);
if (!fileData)
{
return false;
}

auto L = GetState();
bool scriptLoaded = false;
Expand All @@ -44,16 +41,14 @@ bool LuaScript::Initialize(const std::wstring& scriptFile)
if (m_Unicode)
{
const std::string utf8Data =
StringUtil::NarrowUTF8((WCHAR*)(fileData + 2), (fileSize - 2) / sizeof(WCHAR));
StringUtil::NarrowUTF8((WCHAR*)(fileData.get() + 2), (fileSize - 2) / sizeof(WCHAR));
scriptLoaded = luaL_loadbuffer(L, utf8Data.c_str(), utf8Data.length(), "") == 0;
}
else
{
scriptLoaded = luaL_loadbuffer(L, (char*)fileData, fileSize, "") == 0;
scriptLoaded = luaL_loadbuffer(L, (char*)fileData.get(), fileSize, "") == 0;
}

delete [] fileData;

if (scriptLoaded)
{
// Create the table this script will reside in
Expand Down

0 comments on commit 28dfc41

Please sign in to comment.