Lua: Removed TableName.

This commit is contained in:
spx 2011-02-10 08:48:04 +00:00
parent bf6d081600
commit e99a1cc808
3 changed files with 78 additions and 83 deletions

View File

@ -142,26 +142,21 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
{
// Store the current values
std::string oldScriptFile = m_ScriptFile;
std::string oldTableName = m_TableName;
// Read common configs
CMeasure::ReadConfig(parser, section);
m_ScriptFile = ConvertToAscii(parser.ReadString(section, L"ScriptFile", L"").c_str());
m_TableName = ConvertToAscii(parser.ReadString(section, L"TableName", L"").c_str());
if (!m_ScriptFile.empty())
{
if (!m_TableName.empty())
{
if (!m_Initialized ||
oldScriptFile != m_ScriptFile ||
oldTableName != m_TableName)
oldScriptFile != m_ScriptFile)
{
lua_State* L = LuaManager::GetState();
DeleteLuaScript();
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str(), m_TableName.c_str());
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str());
if (m_pLuaScript->IsInitialized())
{
@ -231,12 +226,6 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
}
}
else
{
LuaManager::LuaLog(LOG_ERROR, "Script: TableName missing in %s.", m_ANSIName.c_str());
DeleteLuaScript();
}
}
else
{
LuaManager::LuaLog(LOG_ERROR, "Script: ScriptFile missing in %s.", m_ANSIName.c_str());
DeleteLuaScript();

View File

@ -3,7 +3,8 @@
#include "LuaManager.h"
#include "../Rainmeter.h"
LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_strTableName) : m_pState(p_pState), m_strTableName(_strdup(p_strTableName)),
LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile) : m_pState(p_pState), m_strFile(_strdup(p_strFile)),
m_iRef(LUA_NOREF),
m_bInitialized(true)
{
int result = luaL_loadfile(m_pState, p_strFile);
@ -27,9 +28,9 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s
lua_setmetatable(m_pState, -2);
// Put the table into the global table
lua_setglobal(m_pState, p_strTableName);
m_iRef = luaL_ref(m_pState, LUA_GLOBALSINDEX);
lua_getglobal(m_pState, p_strTableName);
PushTable();
// Set the environment for the function to be run in to be the table that
// has been created for the script/
@ -43,6 +44,9 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s
m_bInitialized = false;
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1));
lua_pop(m_pState, 1);
luaL_unref(m_pState, LUA_GLOBALSINDEX, m_iRef);
m_iRef = LUA_NOREF;
}
}
else
@ -55,7 +59,8 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s
LuaScript::~LuaScript(void)
{
if (m_strTableName) free(m_strTableName);
luaL_unref(m_pState, LUA_GLOBALSINDEX, m_iRef);
if (m_strFile) free(m_strFile);
}
void LuaScript::BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName)
@ -92,7 +97,7 @@ double LuaScript::RunFunctionDouble(const char* p_strFuncName)
if (m_bInitialized && p_strFuncName)
{
// Push our table onto the stack
lua_getglobal(m_pState, m_strTableName);
PushTable();
// Push the function onto the stack
lua_getfield(m_pState, -1, p_strFuncName);
@ -105,7 +110,7 @@ double LuaScript::RunFunctionDouble(const char* p_strFuncName)
{
if (!lua_isnumber(m_pState, -1))
{
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a number", m_strTableName, p_strFuncName);
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s' must return a number: %s", p_strFuncName, m_strFile);
}
result = lua_tonumber(m_pState, -1);
@ -125,7 +130,7 @@ std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
if (m_bInitialized && p_strFuncName)
{
// Push our table onto the stack
lua_getglobal(m_pState, m_strTableName);
PushTable();
// Push the function onto the stack
lua_getfield(m_pState, -1, p_strFuncName);
@ -138,7 +143,7 @@ std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
{
if (!lua_isstring(m_pState, -1))
{
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a string", m_strTableName, p_strFuncName);
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s' must return a string: %s", p_strFuncName, m_strFile);
}
const char* str = lua_tostring(m_pState, -1);
@ -157,7 +162,7 @@ void LuaScript::RunFunction(const char* p_strFuncName)
if (m_bInitialized && p_strFuncName)
{
// Push our table onto the stack
lua_getglobal(m_pState, m_strTableName);
PushTable();
// Push the function onto the stack
lua_getfield(m_pState,-1, p_strFuncName);
@ -178,7 +183,7 @@ bool LuaScript::FunctionExists(const char* p_strFuncName)
if (m_bInitialized && p_strFuncName)
{
// Push our table onto the stack
lua_getglobal(m_pState, m_strTableName);
PushTable();
// Push the function onto the stack
lua_getfield(m_pState, -1, p_strFuncName);

View File

@ -10,7 +10,7 @@ class LuaScript
{
public:
LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_strTableName);
LuaScript(lua_State* p_pState, const char* p_strFile);
~LuaScript(void);
@ -28,7 +28,7 @@ public:
void BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName);
void PushTable() { lua_getglobal(m_pState, m_strTableName); }
void PushTable() { lua_rawgeti(m_pState, LUA_GLOBALSINDEX, m_iRef); }
static void ReportErrors(lua_State * L);
@ -36,7 +36,8 @@ protected:
lua_State* m_pState;
char* m_strTableName;
char* m_strFile;
int m_iRef;
bool m_bInitialized;
};