2011-07-06 10:21:18 +00:00
|
|
|
/*
|
2012-01-23 06:36:15 +00:00
|
|
|
Copyright (C) 2010 Matt King, Birunthan Mohanathas
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-07-06 10:21:18 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-05 11:00:50 +00:00
|
|
|
#include "StdAfx.h"
|
2013-05-30 16:51:05 +00:00
|
|
|
#include "../../Common/StringUtil.h"
|
2010-12-12 17:08:36 +00:00
|
|
|
#include "LuaScript.h"
|
|
|
|
#include "LuaManager.h"
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2012-10-06 18:43:07 +00:00
|
|
|
LuaScript::LuaScript() :
|
2013-08-06 17:43:57 +00:00
|
|
|
m_Ref(LUA_NOREF),
|
|
|
|
m_Unicode(false)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
LuaScript::~LuaScript()
|
|
|
|
{
|
|
|
|
Uninitialize();
|
|
|
|
}
|
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
bool LuaScript::Initialize(const std::wstring& scriptFile)
|
2012-10-06 18:43:07 +00:00
|
|
|
{
|
|
|
|
assert(!IsInitialized());
|
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
FILE* file = _wfopen(scriptFile.c_str(), L"rb");
|
2013-08-06 17:43:57 +00:00
|
|
|
if (!file) return false;
|
2012-10-06 18:43:07 +00:00
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
2013-08-06 17:43:57 +00:00
|
|
|
const long fileSize = ftell(file);
|
|
|
|
BYTE* fileData = new BYTE[fileSize];
|
2012-10-06 18:43:07 +00:00
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
fread(fileData, fileSize, 1, file);
|
2013-03-17 15:56:52 +00:00
|
|
|
fclose(file);
|
2013-05-31 14:28:39 +00:00
|
|
|
file = nullptr;
|
2013-03-17 15:56:52 +00:00
|
|
|
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = GetState();
|
2013-08-07 14:09:25 +00:00
|
|
|
bool scriptLoaded = false;
|
2013-08-06 17:43:57 +00:00
|
|
|
|
2013-08-07 14:09:25 +00:00
|
|
|
// Treat the script as Unicode if it has the UTF-16 LE BOM.
|
|
|
|
m_Unicode = fileSize > 2 && fileData[0] == 0xFF && fileData[1] == 0xFE;
|
2013-08-06 17:43:57 +00:00
|
|
|
if (m_Unicode)
|
|
|
|
{
|
2013-08-07 14:09:25 +00:00
|
|
|
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;
|
2013-08-06 17:43:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
delete [] fileData;
|
|
|
|
|
2013-08-06 17:43:57 +00:00
|
|
|
if (scriptLoaded)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Create the table this script will reside in
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_newtable(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Create the metatable that will store the global table
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_createtable(L, 0, 1);
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
// Push the global teble
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
// Set the __index of the table to be the global table
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setfield(L, -2, "__index");
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the metatable for the script's table
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setmetatable(L, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Put the table into the global table
|
2012-03-11 09:59:48 +00:00
|
|
|
m_Ref = luaL_ref(L, LUA_GLOBALSINDEX);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the environment for the function to be run in to be the table that
|
|
|
|
// has been created for the script/
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setfenv(L, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Execute the Lua script
|
2012-10-06 18:43:07 +00:00
|
|
|
int result = lua_pcall(L, 0, 0, 0);
|
|
|
|
if (result == 0)
|
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
m_File = scriptFile;
|
2012-10-06 18:43:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::ReportErrors(scriptFile);
|
2012-10-06 18:43:07 +00:00
|
|
|
Uninitialize();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::ReportErrors(scriptFile);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2012-10-06 18:43:07 +00:00
|
|
|
|
|
|
|
return false;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
void LuaScript::Uninitialize()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = GetState();
|
2012-10-06 18:43:07 +00:00
|
|
|
|
|
|
|
if (m_Ref != LUA_NOREF)
|
|
|
|
{
|
|
|
|
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
|
|
|
m_Ref = LUA_NOREF;
|
2012-10-06 19:03:29 +00:00
|
|
|
m_File.clear();
|
2012-10-06 18:43:07 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** Checks if given function is defined in the script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool LuaScript::IsFunction(const char* funcName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = GetState();
|
2011-07-06 10:21:18 +00:00
|
|
|
bool bExists = false;
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
if (IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_getfield(L, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2013-01-29 06:59:08 +00:00
|
|
|
bExists = lua_isfunction(L, -1);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
// Pop both the table and the function off the stack.
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 2);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
return bExists;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** Runs given function in script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void LuaScript::RunFunction(const char* funcName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = GetState();
|
2012-03-11 09:59:48 +00:00
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
if (IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_getfield(L, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_pcall(L, 0, 0, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::ReportErrors(m_File);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
2011-07-31 08:58:43 +00:00
|
|
|
** Runs given function in script file and stores the retruned number or string.
|
2011-07-06 10:21:18 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-08-01 09:08:11 +00:00
|
|
|
int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = GetState();
|
2011-08-01 17:08:51 +00:00
|
|
|
int type = LUA_TNIL;
|
2011-07-06 10:21:18 +00:00
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
if (IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_getfield(L, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_pcall(L, 0, 1, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::ReportErrors(m_File);
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-07-06 10:21:18 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
type = lua_type(L, -1);
|
2011-08-01 17:08:51 +00:00
|
|
|
if (type == LUA_TNUMBER)
|
2011-07-31 08:58:43 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
numValue = lua_tonumber(L, -1);
|
2011-07-31 08:58:43 +00:00
|
|
|
}
|
2011-08-01 17:08:51 +00:00
|
|
|
else if (type == LUA_TSTRING)
|
2011-07-06 10:21:18 +00:00
|
|
|
{
|
2013-01-27 10:14:37 +00:00
|
|
|
size_t strLen = 0;
|
|
|
|
const char* str = lua_tolstring(L, -1, &strLen);
|
2013-08-06 17:43:57 +00:00
|
|
|
strValue = m_Unicode ?
|
|
|
|
StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen);
|
2013-05-31 14:28:39 +00:00
|
|
|
numValue = strtod(str, nullptr);
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 2);
|
2011-07-25 12:52:46 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 17:08:51 +00:00
|
|
|
return type;
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
2011-07-09 09:23:28 +00:00
|
|
|
/*
|
|
|
|
** Runs given string in the context of the script file.
|
|
|
|
**
|
|
|
|
*/
|
2013-08-06 17:43:57 +00:00
|
|
|
void LuaScript::RunString(const std::wstring& str)
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = GetState();
|
2012-03-11 09:59:48 +00:00
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
if (IsInitialized())
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
const std::string narrowStr = m_Unicode ?
|
|
|
|
StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
|
|
|
|
|
2011-07-09 09:23:28 +00:00
|
|
|
// Load the string as a Lua chunk
|
2013-08-06 17:43:57 +00:00
|
|
|
if (luaL_loadstring(L, narrowStr.c_str()))
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::ReportErrors(m_File);
|
2011-07-09 09:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2011-07-09 09:23:28 +00:00
|
|
|
|
|
|
|
// Pop table and set the environment of the loaded chunk to it
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setfenv(L, -2);
|
2011-07-09 09:23:28 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_pcall(L, 0, 0, 0))
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::ReportErrors(m_File);
|
2011-07-09 09:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|