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-02-10 08:48:04 +00:00
|
|
|
LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile) : m_pState(p_pState), m_strFile(_strdup(p_strFile)),
|
|
|
|
m_iRef(LUA_NOREF),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_bInitialized(true)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
int result = luaL_loadfile(m_pState, p_strFile);
|
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
|
|
|
|
lua_newtable(m_pState);
|
|
|
|
|
|
|
|
// Create the metatable that will store the global table
|
2011-01-30 13:04:17 +00:00
|
|
|
lua_createtable(m_pState, 0, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the global teble
|
2011-01-30 13:04:17 +00:00
|
|
|
lua_pushvalue(m_pState, LUA_GLOBALSINDEX);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the __index of the table to be the global table
|
2011-01-30 13:04:17 +00:00
|
|
|
lua_setfield(m_pState, -2, "__index");
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the metatable for the script's table
|
|
|
|
lua_setmetatable(m_pState, -2);
|
|
|
|
|
|
|
|
// Put the table into the global table
|
2011-02-10 08:48:04 +00:00
|
|
|
m_iRef = luaL_ref(m_pState, 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-01-30 13:04:17 +00:00
|
|
|
lua_setfenv(m_pState, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Execute the Lua script
|
|
|
|
result = lua_pcall(m_pState, 0, LUA_MULTRET, 0);
|
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
if (result)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
m_bInitialized = false;
|
2011-01-31 07:08:43 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1));
|
2011-02-09 06:10:25 +00:00
|
|
|
lua_pop(m_pState, 1);
|
2011-02-10 08:48:04 +00:00
|
|
|
|
|
|
|
luaL_unref(m_pState, LUA_GLOBALSINDEX, m_iRef);
|
|
|
|
m_iRef = LUA_NOREF;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bInitialized = false;
|
2011-01-31 07:08:43 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1));
|
2011-02-09 06:10:25 +00:00
|
|
|
lua_pop(m_pState, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaScript::~LuaScript(void)
|
|
|
|
{
|
2011-02-10 08:48:04 +00:00
|
|
|
luaL_unref(m_pState, LUA_GLOBALSINDEX, m_iRef);
|
|
|
|
if (m_strFile) free(m_strFile);
|
2010-12-12 17:08:36 +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_pState, p_strName);
|
|
|
|
// Push the value
|
|
|
|
tolua_pushusertype(m_pState, p_pValue, p_strTypeName);
|
|
|
|
// Bind the variable
|
|
|
|
lua_settable(m_pState, -3);
|
|
|
|
|
|
|
|
// Pop our table off of the stack
|
|
|
|
lua_pop(m_pState, 1);
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Push the variable name we want to put a value in.
|
|
|
|
lua_pushstring(m_pState, "SKIN");
|
|
|
|
// Push the value
|
|
|
|
tolua_pushusertype(m_pState, p_pValue, "CMeterWindow");
|
|
|
|
// Bind the variable
|
|
|
|
lua_settable(m_pState, -3);
|
|
|
|
|
|
|
|
//lua_pop(m_pLuaScript->GetState(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
double LuaScript::RunFunctionDouble(const char* p_strFuncName)
|
|
|
|
{
|
2011-02-09 06:10:25 +00:00
|
|
|
double result = -1;
|
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
if (m_bInitialized && p_strFuncName)
|
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-01-30 13:04:17 +00:00
|
|
|
lua_getfield(m_pState, -1, p_strFuncName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
if(lua_pcall(m_pState, 0, 1, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
LuaManager::ReportErrors(m_pState);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!lua_isnumber(m_pState, -1))
|
|
|
|
{
|
2011-02-10 08:48:04 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s' must return a number: %s", p_strFuncName, m_strFile);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 06:10:25 +00:00
|
|
|
result = lua_tonumber(m_pState, -1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 06:10:25 +00:00
|
|
|
// Pop both the table and the return value off the stack.
|
|
|
|
lua_pop(m_pState, 2);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 06:10:25 +00:00
|
|
|
return result;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
|
|
|
|
{
|
2011-02-09 06:10:25 +00:00
|
|
|
std::wstring result;
|
|
|
|
|
2011-01-30 13:04:17 +00:00
|
|
|
if (m_bInitialized && p_strFuncName)
|
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-01-30 10:39:10 +00:00
|
|
|
lua_getfield(m_pState, -1, p_strFuncName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
if (lua_pcall(m_pState, 0, 1, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
LuaManager::ReportErrors(m_pState);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!lua_isstring(m_pState, -1))
|
|
|
|
{
|
2011-02-10 08:48:04 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s' must return a string: %s", p_strFuncName, m_strFile);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* str = lua_tostring(m_pState, -1);
|
2011-02-09 06:10:25 +00:00
|
|
|
result = ConvertToWide(str);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 06:10:25 +00:00
|
|
|
// Pop both the table and the return value off the stack.
|
|
|
|
lua_pop(m_pState, 2);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 06:10:25 +00:00
|
|
|
return result;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::RunFunction(const char* p_strFuncName)
|
|
|
|
{
|
2011-01-30 13:04:17 +00:00
|
|
|
if (m_bInitialized && p_strFuncName)
|
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
|
|
|
|
lua_getfield(m_pState,-1, p_strFuncName);
|
|
|
|
|
2011-01-30 13:04:17 +00:00
|
|
|
if (lua_pcall(m_pState, 0, 0, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
LuaManager::ReportErrors(m_pState);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(m_pState, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::FunctionExists(const char* p_strFuncName)
|
|
|
|
{
|
|
|
|
bool bExists = false;
|
|
|
|
|
2011-01-30 13:04:17 +00:00
|
|
|
if (m_bInitialized && p_strFuncName)
|
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-01-30 13:04:17 +00:00
|
|
|
lua_getfield(m_pState, -1, p_strFuncName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-02-09 06:10:25 +00:00
|
|
|
if (lua_isfunction( m_pState, -1))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
bExists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop both the table and the function off the stack.
|
|
|
|
lua_pop(m_pState, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bExists;
|
2011-01-30 13:04:17 +00:00
|
|
|
}
|