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"
|
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
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2012-03-09 10:28:25 +00:00
|
|
|
LuaScript::LuaScript(const char* file) :
|
|
|
|
m_Ref(LUA_NOREF),
|
2011-07-06 10:21:18 +00:00
|
|
|
m_Initialized(true)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
int result = luaL_loadfile(L, 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
|
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-03-11 09:59:48 +00:00
|
|
|
result = lua_pcall(L, 0, 0, 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;
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
2012-03-09 10:28:25 +00:00
|
|
|
m_Ref = LUA_NOREF;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
m_Initialized = false;
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
LuaScript::~LuaScript()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
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
|
|
|
|
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
|
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_isfunction(L, -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.
|
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();
|
|
|
|
|
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
|
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-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
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
|
|
|
|
|
|
|
if (m_Initialized)
|
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-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
|
|
|
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
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
const char* str = lua_tostring(L, -1);
|
2011-09-03 18:19:43 +00:00
|
|
|
strValue = ConvertToWide(str);
|
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();
|
|
|
|
|
2011-07-09 09:23:28 +00:00
|
|
|
if (m_Initialized)
|
|
|
|
{
|
|
|
|
// 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-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
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-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2011-07-09 09:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|