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-01-29 00:11:01 +00:00
|
|
|
LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_strTableName) : m_pState(p_pState), m_strTableName(_strdup(p_strTableName)),
|
|
|
|
m_bInitialized(true)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
int result = luaL_loadfile(m_pState, p_strFile);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
lua_setglobal(m_pState, p_strTableName);
|
|
|
|
|
|
|
|
lua_getglobal(m_pState, p_strTableName);
|
|
|
|
|
|
|
|
// 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));
|
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));
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaScript::~LuaScript(void)
|
|
|
|
{
|
2010-12-13 11:47:03 +00:00
|
|
|
if (m_strTableName) free(m_strTableName);
|
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-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
|
|
|
|
lua_getglobal(m_pState, m_strTableName);
|
|
|
|
|
|
|
|
// 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-01-30 10:39:10 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a number", m_strTableName, p_strFuncName);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double d = lua_tonumber(m_pState, -1);
|
|
|
|
|
|
|
|
lua_pop(m_pState, 1);
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(m_pState, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring LuaScript::RunFunctionString(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
|
|
|
|
lua_getglobal(m_pState, m_strTableName);
|
|
|
|
|
|
|
|
// 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-01-30 13:04:17 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a string", m_strTableName, p_strFuncName);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* str = lua_tostring(m_pState, -1);
|
|
|
|
|
|
|
|
lua_pop(m_pState, 1);
|
|
|
|
|
|
|
|
return ConvertToWide(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(m_pState, 1);
|
|
|
|
}
|
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
return L"";
|
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
|
|
|
|
lua_getglobal(m_pState, m_strTableName);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
lua_getglobal(m_pState, m_strTableName);
|
|
|
|
|
|
|
|
// 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 13:04:17 +00:00
|
|
|
if (lua_isfunction( m_pState, lua_gettop(m_pState)))
|
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
|
|
|
}
|