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
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2010-12-13 09:00:02 +00:00
|
|
|
#include "../StdAfx.h"
|
2010-12-12 17:08:36 +00:00
|
|
|
#include "LuaScript.h"
|
|
|
|
#include "LuaManager.h"
|
|
|
|
#include "../Rainmeter.h"
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** LuaScript
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
LuaScript::LuaScript(lua_State* state, const char* file) : m_State(state),
|
2011-02-10 08:48:04 +00:00
|
|
|
m_iRef(LUA_NOREF),
|
2011-07-06 10:21:18 +00:00
|
|
|
m_Initialized(true)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
int result = luaL_loadfile(m_State, file);
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
// If the file loaded okay.
|
2011-01-30 10:39:10 +00:00
|
|
|
if (result == 0)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Create the table this script will reside in
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_newtable(m_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Create the metatable that will store the global table
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_createtable(m_State, 0, 1);
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
// Push the global teble
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_pushvalue(m_State, 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
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_setfield(m_State, -2, "__index");
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the metatable for the script's table
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_setmetatable(m_State, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Put the table into the global table
|
2011-07-06 10:21:18 +00:00
|
|
|
m_iRef = luaL_ref(m_State, LUA_GLOBALSINDEX);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
PushTable();
|
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/
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_setfenv(m_State, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Execute the Lua script
|
2011-07-06 10:21:18 +00:00
|
|
|
result = lua_pcall(m_State, 0, LUA_MULTRET, 0);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
if (result)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
m_Initialized = false;
|
2011-07-07 16:18:39 +00:00
|
|
|
LuaManager::ReportErrors(m_State);
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef);
|
2011-02-10 08:48:04 +00:00
|
|
|
m_iRef = LUA_NOREF;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
m_Initialized = false;
|
2011-07-07 16:18:39 +00:00
|
|
|
LuaManager::ReportErrors(m_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** ~LuaScript
|
|
|
|
**
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
LuaScript::~LuaScript()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** IsFunction
|
|
|
|
**
|
|
|
|
** Checks if given function is defined in the script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool LuaScript::IsFunction(const char* funcName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
bool bExists = false;
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2011-07-09 16:42:51 +00:00
|
|
|
if (m_Initialized)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2011-02-10 08:48:04 +00:00
|
|
|
PushTable();
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_getfield(m_State, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (lua_isfunction(m_State, -1))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
bExists = true;
|
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.
|
|
|
|
lua_pop(m_State, 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
|
|
|
/*
|
|
|
|
** RunFunction
|
|
|
|
**
|
|
|
|
** Runs given function in script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void LuaScript::RunFunction(const char* funcName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
if (m_Initialized)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2011-02-10 08:48:04 +00:00
|
|
|
PushTable();
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_getfield(m_State, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (lua_pcall(m_State, 0, 0, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
LuaManager::ReportErrors(m_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_pop(m_State, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** RunFunctionWithReturn
|
|
|
|
**
|
|
|
|
** Runs given function in script file and stores the retruned number and/or string.
|
|
|
|
** Returns true if the executed function returns a valid value.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (m_Initialized)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2011-02-10 08:48:04 +00:00
|
|
|
PushTable();
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_getfield(m_State, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (lua_pcall(m_State, 0, 1, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
LuaManager::ReportErrors(m_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-07-06 10:21:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lua_isstring(m_State, -1))
|
|
|
|
{
|
|
|
|
const char* str = lua_tostring(m_State, -1);
|
|
|
|
strValue = ConvertToWide(str);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
// A number is a string and numerical string (e.g. "10") is a number, so check for it here
|
|
|
|
if (lua_isnumber(m_State, -1))
|
|
|
|
{
|
|
|
|
numValue = lua_tonumber(m_State, -1);
|
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_pop(m_State, 2);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-09 09:23:28 +00:00
|
|
|
/*
|
|
|
|
** RunString
|
|
|
|
**
|
|
|
|
** Runs given string in the context of the script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void LuaScript::RunString(const char* str)
|
|
|
|
{
|
|
|
|
if (m_Initialized)
|
|
|
|
{
|
|
|
|
// Load the string as a Lua chunk
|
|
|
|
if (luaL_loadstring(m_State, str))
|
|
|
|
{
|
|
|
|
LuaManager::ReportErrors(m_State);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push our table onto the stack
|
|
|
|
PushTable();
|
|
|
|
|
|
|
|
// Pop table and set the environment of the loaded chunk to it
|
|
|
|
lua_setfenv(m_State, -2);
|
|
|
|
|
|
|
|
if (lua_pcall(m_State, 0, 0, 0))
|
|
|
|
{
|
|
|
|
LuaManager::ReportErrors(m_State);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
//void LuaScript::BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName)
|
|
|
|
//{
|
|
|
|
// PushTable();
|
|
|
|
//
|
|
|
|
// /*
|
|
|
|
// // Push the variable name we want to put a value in.
|
|
|
|
// lua_pushstring(m_State, p_strName);
|
|
|
|
// // Push the value
|
|
|
|
// tolua_pushusertype(m_State, p_pValue, p_strTypeName);
|
|
|
|
// // Bind the variable
|
|
|
|
// lua_settable(m_State, -3);
|
|
|
|
//
|
|
|
|
// // Pop our table off of the stack
|
|
|
|
// lua_pop(m_State, 1);
|
|
|
|
//
|
|
|
|
// */
|
|
|
|
//
|
|
|
|
// // Push the variable name we want to put a value in.
|
|
|
|
// lua_pushstring(m_State, "SKIN");
|
|
|
|
// // Push the value
|
|
|
|
// tolua_pushusertype(m_State, p_pValue, "CMeterWindow");
|
|
|
|
// // Bind the variable
|
|
|
|
// lua_settable(m_State, -3);
|
|
|
|
//
|
|
|
|
// //lua_pop(m_pLuaScript->GetState(), 1);
|
|
|
|
//}
|
2011-07-09 16:42:51 +00:00
|
|
|
//
|