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
|
|
|
*/
|
|
|
|
|
2010-12-13 09:00:02 +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() :
|
|
|
|
m_Ref(LUA_NOREF)
|
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-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
// Load file into a buffer as luaL_loadfile does not support Unicode paths.
|
2012-10-06 19:03:29 +00:00
|
|
|
FILE* file = _wfopen(scriptFile.c_str(), L"rb");
|
2012-10-06 18:43:07 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long fileSize = ftell(file);
|
|
|
|
|
|
|
|
char* fileData = new char[fileSize];
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
fread(fileData, fileSize, 1, file);
|
|
|
|
|
2013-03-17 15:56:52 +00:00
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
int load = luaL_loadbuffer(L, fileData, fileSize, "");
|
2012-10-06 18:43:07 +00:00
|
|
|
delete [] fileData;
|
|
|
|
|
|
|
|
if (load == 0)
|
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
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
LuaManager::ReportErrors(L, scriptFile);
|
2012-10-06 18:43:07 +00:00
|
|
|
Uninitialize();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
LuaManager::ReportErrors(L, 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
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::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
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::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
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
|
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
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
LuaManager::ReportErrors(L, 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
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::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
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
LuaManager::ReportErrors(L, 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);
|
|
|
|
strValue = StringUtil::Widen(str, (int)strLen);
|
2011-08-01 17:20:24 +00:00
|
|
|
numValue = strtod(str, NULL);
|
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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void LuaScript::RunString(const char* str)
|
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
if (IsInitialized())
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
|
|
|
// Load the string as a Lua chunk
|
2012-03-11 09:59:48 +00:00
|
|
|
if (luaL_loadstring(L, str))
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
LuaManager::ReportErrors(L, 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
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
LuaManager::ReportErrors(L, m_File);
|
2011-07-09 09:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|