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,98 +142,87 @@ 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)
{
if (!m_Initialized ||
oldScriptFile != m_ScriptFile ||
oldTableName != m_TableName)
lua_State* L = LuaManager::GetState();
DeleteLuaScript();
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str());
if (m_pLuaScript->IsInitialized())
{
lua_State* L = LuaManager::GetState();
m_bUpdateDefined = m_pLuaScript->FunctionExists(g_strUpdateFunction);
m_bInitializeDefined = m_pLuaScript->FunctionExists(g_strInitFunction);
m_bGetValueDefined = m_pLuaScript->FunctionExists(g_strGetValueFunction);
m_bGetStringValueDefined = m_pLuaScript->FunctionExists(g_strGetStringValueFunction);
DeleteLuaScript();
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str(), m_TableName.c_str());
m_pLuaScript->PushTable();
if (m_pLuaScript->IsInitialized())
// Push the variable name we want to put a value in.
lua_pushstring(L, "SELF");
// Push the value
tolua_pushusertype(L, this, "CMeasure");
// Bind the variable
lua_settable(L, -3);
// Push the variable name we want to put a value in.
lua_pushstring(L, "SKIN");
// Push the value
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
// Bind the variable
lua_settable(L, -3);
// Push the variable name we want to put a value in.
lua_pushstring(L, "RAINMETER");
// Push the value
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
// Bind the variable
lua_settable(L, -3);
// Look i nthe properties table for values to read from the section.
lua_getfield(L, -1, "PROPERTIES");
if (lua_isnil(L, -1) == 0)
{
m_bUpdateDefined = m_pLuaScript->FunctionExists(g_strUpdateFunction);
m_bInitializeDefined = m_pLuaScript->FunctionExists(g_strInitFunction);
m_bGetValueDefined = m_pLuaScript->FunctionExists(g_strGetValueFunction);
m_bGetStringValueDefined = m_pLuaScript->FunctionExists(g_strGetStringValueFunction);
lua_pushnil(L);
m_pLuaScript->PushTable();
// Push the variable name we want to put a value in.
lua_pushstring(L, "SELF");
// Push the value
tolua_pushusertype(L, this, "CMeasure");
// Bind the variable
lua_settable(L, -3);
// Push the variable name we want to put a value in.
lua_pushstring(L, "SKIN");
// Push the value
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
// Bind the variable
lua_settable(L, -3);
// Push the variable name we want to put a value in.
lua_pushstring(L, "RAINMETER");
// Push the value
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
// Bind the variable
lua_settable(L, -3);
// Look i nthe properties table for values to read from the section.
lua_getfield(L, -1, "PROPERTIES");
if (lua_isnil(L, -1) == 0)
while(lua_next(L, -2))
{
lua_pushnil(L);
lua_pop(L, 1);
while(lua_next(L, -2))
const char* strKey = lua_tostring(L, -1);
std::wstring wstrKey = ConvertToWide(strKey);
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
if (!wstrValue.empty())
{
lua_pop(L, 1);
std::string strStrVal = ConvertToAscii(wstrValue.c_str());
const char* strValue = strStrVal.c_str();
const char* strKey = lua_tostring(L, -1);
std::wstring wstrKey = ConvertToWide(strKey);
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
if (!wstrValue.empty())
{
std::string strStrVal = ConvertToAscii(wstrValue.c_str());
const char* strValue = strStrVal.c_str();
lua_pushstring(L, strValue);
lua_setfield(L, -3, strKey);
}
lua_pushstring(L, strValue);
lua_setfield(L, -3, strKey);
}
}
// Pop PROPERTIES table
lua_pop(L, 1);
}
// Pop PROPERTIES table
lua_pop(L, 1);
// Pop the table
lua_pop(L, 1);
}
else
{
DeleteLuaScript();
}
// Pop the table
lua_pop(L, 1);
}
else
{
DeleteLuaScript();
}
}
else
{
LuaManager::LuaLog(LOG_ERROR, "Script: TableName missing in %s.", m_ANSIName.c_str());
DeleteLuaScript();
}
}
else

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;
};