mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Lua:
- Removed GetValue() (which has never worked) and deprecated GetStringValue(). The measure value should be returned on Update() now. - Some related tweaks and cosmetic changes
This commit is contained in:
parent
4389edb8c1
commit
fa67b07a62
@ -1,24 +1,50 @@
|
|||||||
|
/*
|
||||||
|
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
|
||||||
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "StdAfx.h"
|
#include "StdAfx.h"
|
||||||
#include "MeasureScript.h"
|
#include "MeasureScript.h"
|
||||||
#include "lua/LuaManager.h"
|
#include "lua/LuaManager.h"
|
||||||
#include "Litestep.h"
|
#include "Litestep.h"
|
||||||
#include "Rainmeter.h"
|
#include "Rainmeter.h"
|
||||||
|
|
||||||
const char* g_strInitFunction = "Initialize";
|
const char* g_InitializeFunctionName = "Initialize";
|
||||||
const char* g_strUpdateFunction = "Update";
|
const char* g_UpdateFunctionName = "Update";
|
||||||
const char* g_strGetValueFunction = "GetValue";
|
const char* g_GetStringFunctionName = "GetStringValue";
|
||||||
const char* g_strGetStringValueFunction = "GetStringValue";
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** CMeasureScript
|
||||||
|
**
|
||||||
|
** The constructor
|
||||||
|
**
|
||||||
|
*/
|
||||||
CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
||||||
m_pLuaScript(),
|
m_LuaScript(),
|
||||||
m_bUpdateDefined(false),
|
m_HasInitializeFunction(false),
|
||||||
m_bGetValueDefined(false),
|
m_HasUpdateFunction(false),
|
||||||
m_bGetStringValueDefined(false),
|
m_HasGetStringFunction(false)
|
||||||
m_bInitializeDefined(false)
|
|
||||||
{
|
{
|
||||||
LuaManager::Init();
|
LuaManager::Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** ~CMeasureScript
|
||||||
|
**
|
||||||
|
** The destructor
|
||||||
|
**
|
||||||
|
*/
|
||||||
CMeasureScript::~CMeasureScript()
|
CMeasureScript::~CMeasureScript()
|
||||||
{
|
{
|
||||||
DeleteLuaScript();
|
DeleteLuaScript();
|
||||||
@ -27,28 +53,39 @@ CMeasureScript::~CMeasureScript()
|
|||||||
|
|
||||||
void CMeasureScript::DeleteLuaScript()
|
void CMeasureScript::DeleteLuaScript()
|
||||||
{
|
{
|
||||||
if (m_pLuaScript)
|
if (m_LuaScript)
|
||||||
{
|
{
|
||||||
delete m_pLuaScript;
|
delete m_LuaScript;
|
||||||
m_pLuaScript = NULL;
|
m_LuaScript = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_bUpdateDefined = false;
|
m_HasInitializeFunction = false;
|
||||||
m_bGetValueDefined = false;
|
m_HasUpdateFunction = false;
|
||||||
m_bGetStringValueDefined = false;
|
m_HasGetStringFunction = false;
|
||||||
m_bInitializeDefined = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Initialize
|
||||||
|
**
|
||||||
|
** Initializes the measure.
|
||||||
|
**
|
||||||
|
*/
|
||||||
void CMeasureScript::Initialize()
|
void CMeasureScript::Initialize()
|
||||||
{
|
{
|
||||||
CMeasure::Initialize();
|
CMeasure::Initialize();
|
||||||
|
|
||||||
if (m_bInitializeDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
if (m_HasInitializeFunction)
|
||||||
{
|
{
|
||||||
m_pLuaScript->RunFunction(g_strInitFunction);
|
m_LuaScript->RunFunction(g_InitializeFunctionName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Update
|
||||||
|
**
|
||||||
|
** Updates the current disk free space value.
|
||||||
|
**
|
||||||
|
*/
|
||||||
bool CMeasureScript::Update()
|
bool CMeasureScript::Update()
|
||||||
{
|
{
|
||||||
if (!CMeasure::PreUpdate())
|
if (!CMeasure::PreUpdate())
|
||||||
@ -56,80 +93,39 @@ bool CMeasureScript::Update()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_bUpdateDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
if (m_HasUpdateFunction)
|
||||||
{
|
{
|
||||||
m_pLuaScript->RunFunction(g_strUpdateFunction);
|
bool ret = m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
|
||||||
|
|
||||||
|
if (!ret)
|
||||||
|
{
|
||||||
|
// Update() didn't return anything. For backwards compatibility, check for GetStringValue() first
|
||||||
|
if (m_HasGetStringFunction)
|
||||||
|
{
|
||||||
|
m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::wstring error = L"Script: Update() in measure [";
|
||||||
|
error += m_Name;
|
||||||
|
error += L"] is not returning a valid number or string.";
|
||||||
|
Log(LOG_WARNING, error.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return PostUpdate();
|
return PostUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
double CMeasureScript::GetValue()
|
|
||||||
{
|
|
||||||
if (m_bGetValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
|
||||||
{
|
|
||||||
return m_pLuaScript->RunFunctionDouble(g_strGetValueFunction);
|
|
||||||
}
|
|
||||||
|
|
||||||
return CMeasure::GetValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CMeasureScript::SetValue(double d)
|
|
||||||
{
|
|
||||||
m_Value = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** GetStringValue
|
** GetStringValue
|
||||||
**
|
**
|
||||||
** This method returns the value as text string. The actual value is
|
** Returns the time as string.
|
||||||
** get with GetValue() so we don't have to worry about m_Invert.
|
|
||||||
**
|
**
|
||||||
** autoScale If true, scale the value automatically to some sensible range.
|
|
||||||
** scale The scale to use if autoScale is false.
|
|
||||||
** decimals Number of decimals used in the value.
|
|
||||||
** percentual Return the value as % from the maximum value.
|
|
||||||
*/
|
*/
|
||||||
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
||||||
{
|
{
|
||||||
if (m_bGetStringValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
return m_StringValue.c_str();
|
||||||
{
|
|
||||||
m_strValue = m_pLuaScript->RunFunctionString(g_strGetStringValueFunction);
|
|
||||||
|
|
||||||
return CheckSubstitute(m_strValue.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
return CMeasure::GetStringValue(autoScale, scale, decimals, percentual);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void stackDump(lua_State *L)
|
|
||||||
{
|
|
||||||
int i = lua_gettop(L);
|
|
||||||
LuaManager::LuaLog(LOG_DEBUG, " ---------------- Stack Dump ----------------" );
|
|
||||||
while (i)
|
|
||||||
{
|
|
||||||
int t = lua_type(L, i);
|
|
||||||
switch (t)
|
|
||||||
{
|
|
||||||
case LUA_TSTRING:
|
|
||||||
LuaManager::LuaLog(LOG_DEBUG, "%d:`%s'", i, lua_tostring(L, i));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LUA_TBOOLEAN:
|
|
||||||
LuaManager::LuaLog(LOG_DEBUG, "%d: %s",i,lua_toboolean(L, i) ? "true" : "false");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LUA_TNUMBER:
|
|
||||||
LuaManager::LuaLog(LOG_DEBUG, "%d: %g", i, lua_tonumber(L, i));
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -156,16 +152,23 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
|
|
||||||
DeleteLuaScript();
|
DeleteLuaScript();
|
||||||
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str());
|
m_LuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str());
|
||||||
|
|
||||||
if (m_pLuaScript->IsInitialized())
|
if (m_LuaScript->IsInitialized())
|
||||||
{
|
{
|
||||||
m_bUpdateDefined = m_pLuaScript->FunctionExists(g_strUpdateFunction);
|
m_HasInitializeFunction = m_LuaScript->IsFunction(g_InitializeFunctionName);
|
||||||
m_bInitializeDefined = m_pLuaScript->FunctionExists(g_strInitFunction);
|
m_HasUpdateFunction = m_LuaScript->IsFunction(g_UpdateFunctionName);
|
||||||
m_bGetValueDefined = m_pLuaScript->FunctionExists(g_strGetValueFunction);
|
m_HasGetStringFunction = m_LuaScript->IsFunction(g_GetStringFunctionName); // For backwards compatbility
|
||||||
m_bGetStringValueDefined = m_pLuaScript->FunctionExists(g_strGetStringValueFunction);
|
|
||||||
|
|
||||||
m_pLuaScript->PushTable();
|
if (m_HasGetStringFunction)
|
||||||
|
{
|
||||||
|
std::wstring error = L"Script: GetStringValue() used in measure [";
|
||||||
|
error += m_Name;
|
||||||
|
error += L"] has been deprecated. Check manual to ensure future compatibility.";
|
||||||
|
Log(LOG_WARNING, error.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
m_LuaScript->PushTable();
|
||||||
|
|
||||||
// Push the variable name we want to put a value in.
|
// Push the variable name we want to put a value in.
|
||||||
lua_pushstring(L, "SELF");
|
lua_pushstring(L, "SELF");
|
||||||
@ -174,18 +177,12 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
// Bind the variable
|
// Bind the variable
|
||||||
lua_settable(L, -3);
|
lua_settable(L, -3);
|
||||||
|
|
||||||
// Push the variable name we want to put a value in.
|
|
||||||
lua_pushstring(L, "SKIN");
|
lua_pushstring(L, "SKIN");
|
||||||
// Push the value
|
|
||||||
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
|
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
|
||||||
// Bind the variable
|
|
||||||
lua_settable(L, -3);
|
lua_settable(L, -3);
|
||||||
|
|
||||||
// Push the variable name we want to put a value in.
|
|
||||||
lua_pushstring(L, "RAINMETER");
|
lua_pushstring(L, "RAINMETER");
|
||||||
// Push the value
|
|
||||||
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
|
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
|
||||||
// Bind the variable
|
|
||||||
lua_settable(L, -3);
|
lua_settable(L, -3);
|
||||||
|
|
||||||
// Look in the properties table for values to read from the section.
|
// Look in the properties table for values to read from the section.
|
||||||
@ -213,6 +210,7 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop PROPERTIES table
|
// Pop PROPERTIES table
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|
||||||
@ -227,7 +225,11 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: ScriptFile missing in %s.", m_ANSIName.c_str());
|
std::wstring error = L"Script: ScriptFile= is not valid in measure [";
|
||||||
|
error += m_Name;
|
||||||
|
error += L"].";
|
||||||
|
Log(LOG_WARNING, error.c_str());
|
||||||
|
|
||||||
DeleteLuaScript();
|
DeleteLuaScript();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,10 +237,10 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|||||||
void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter)
|
void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter)
|
||||||
{
|
{
|
||||||
// Get the Lua State
|
// Get the Lua State
|
||||||
lua_State* L = m_pLuaScript->GetState();
|
lua_State* L = m_LuaScript->GetState();
|
||||||
|
|
||||||
// Push the script table
|
// Push the script table
|
||||||
m_pLuaScript->PushTable();
|
m_LuaScript->PushTable();
|
||||||
|
|
||||||
// Push the function onto the stack
|
// Push the function onto the stack
|
||||||
lua_getfield(L, -1, p_strFunction);
|
lua_getfield(L, -1, p_strFunction);
|
||||||
@ -264,7 +266,7 @@ void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_p
|
|||||||
|
|
||||||
void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
|
void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
|
||||||
{
|
{
|
||||||
if (m_pLuaScript && m_pLuaScript->IsInitialized())
|
if (m_LuaScript && m_LuaScript->IsInitialized())
|
||||||
{
|
{
|
||||||
switch (p_eMouse)
|
switch (p_eMouse)
|
||||||
{
|
{
|
||||||
@ -306,3 +308,33 @@ void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void stackDump(lua_State *L)
|
||||||
|
{
|
||||||
|
int i = lua_gettop(L);
|
||||||
|
LuaManager::LuaLog(LOG_DEBUG, " ---------------- Stack Dump ----------------" );
|
||||||
|
while (i)
|
||||||
|
{
|
||||||
|
int t = lua_type(L, i);
|
||||||
|
switch (t)
|
||||||
|
{
|
||||||
|
case LUA_TSTRING:
|
||||||
|
LuaManager::LuaLog(LOG_DEBUG, "%d:`%s'", i, lua_tostring(L, i));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case LUA_TBOOLEAN:
|
||||||
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %s",i,lua_toboolean(L, i) ? "true" : "false");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case LUA_TNUMBER:
|
||||||
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %g", i, lua_tonumber(L, i));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
||||||
|
}
|
||||||
|
@ -1,5 +1,21 @@
|
|||||||
#ifndef MEASURESCRIPT_H
|
/*
|
||||||
#define MEASURESCRIPT_H
|
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
|
||||||
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __MEASURESCRIPT_H__
|
||||||
|
#define __MEASURESCRIPT_H__
|
||||||
|
|
||||||
#include "Measure.h"
|
#include "Measure.h"
|
||||||
#include "lua/LuaScript.h"
|
#include "lua/LuaScript.h"
|
||||||
@ -16,35 +32,22 @@ public:
|
|||||||
virtual bool Update();
|
virtual bool Update();
|
||||||
virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual);
|
virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual);
|
||||||
|
|
||||||
double GetValue();
|
|
||||||
void SetValue(double d);
|
|
||||||
|
|
||||||
void DeleteLuaScript();
|
void DeleteLuaScript();
|
||||||
|
|
||||||
void MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse);
|
void MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse);
|
||||||
void RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter);
|
void RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
LuaScript* m_pLuaScript;
|
LuaScript* m_LuaScript;
|
||||||
|
|
||||||
bool m_bUpdateDefined;
|
bool m_HasInitializeFunction;
|
||||||
bool m_bGetValueDefined;
|
bool m_HasUpdateFunction;
|
||||||
bool m_bGetStringValueDefined;
|
bool m_HasGetStringFunction;
|
||||||
bool m_bInitializeDefined;
|
|
||||||
|
|
||||||
std::wstring m_strValue;
|
std::wstring m_StringValue;
|
||||||
|
|
||||||
std::string m_ScriptFile;
|
std::string m_ScriptFile;
|
||||||
std::string m_TableName;
|
std::string m_TableName;
|
||||||
|
|
||||||
/*
|
|
||||||
Sqrat::Table* m_ScriptTable;
|
|
||||||
Sqrat::Function* m_UpdateFunc;
|
|
||||||
Sqrat::Function* m_GetValueFunc;
|
|
||||||
Sqrat::Function* m_GetStringValueFunc;
|
|
||||||
Sqrat::Function* m_InitFunc;
|
|
||||||
Sqrat::Function* m_UpdateTransitionFunc;
|
|
||||||
*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -19,35 +19,33 @@
|
|||||||
#include "../Rainmeter.h"
|
#include "../Rainmeter.h"
|
||||||
|
|
||||||
int LuaManager::c_RefCount = 0;
|
int LuaManager::c_RefCount = 0;
|
||||||
lua_State* LuaManager::c_pState = 0;
|
lua_State* LuaManager::c_State = 0;
|
||||||
|
|
||||||
void LuaManager::Init()
|
void LuaManager::Init()
|
||||||
{
|
{
|
||||||
if (c_pState == NULL)
|
if (c_State == NULL)
|
||||||
{
|
{
|
||||||
// Initialize Lua
|
// Initialize Lua
|
||||||
c_pState = lua_open();
|
c_State = lua_open();
|
||||||
|
|
||||||
// Load Lua base libraries
|
// Load Lua base libraries
|
||||||
luaL_openlibs(c_pState);
|
luaL_openlibs(c_State);
|
||||||
|
|
||||||
// Initialize tolua
|
// Initialize tolua
|
||||||
tolua_open(c_pState);
|
tolua_open(c_State);
|
||||||
|
|
||||||
// Register custom types and functions
|
// Register custom types and functions
|
||||||
RegisterTypes(c_pState);
|
tolua_module(c_State, NULL, 0);
|
||||||
|
tolua_beginmodule(c_State, NULL);
|
||||||
tolua_module(c_pState, NULL, 0);
|
RegisterGlobal(c_State);
|
||||||
tolua_beginmodule(c_pState, NULL);
|
RegisterMeasure(c_State);
|
||||||
RegisterGlobal(c_pState);
|
RegisterGroup(c_State);
|
||||||
RegisterMeasure(c_pState);
|
RegisterMeasure(c_State);
|
||||||
RegisterGroup(c_pState);
|
RegisterMeter(c_State);
|
||||||
RegisterMeasure(c_pState);
|
RegisterMeterWindow(c_State);
|
||||||
RegisterMeter(c_pState);
|
RegisterRainmeter(c_State);
|
||||||
RegisterMeterWindow(c_pState);
|
RegisterMeterString(c_State);
|
||||||
RegisterRainmeter(c_pState);
|
tolua_endmodule(c_State);
|
||||||
RegisterMeterString(c_pState);
|
|
||||||
tolua_endmodule(c_pState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
++c_RefCount;
|
++c_RefCount;
|
||||||
@ -60,17 +58,13 @@ void LuaManager::CleanUp()
|
|||||||
--c_RefCount;
|
--c_RefCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c_RefCount == 0 && c_pState != NULL)
|
if (c_RefCount == 0 && c_State != NULL)
|
||||||
{
|
{
|
||||||
lua_close(c_pState);
|
lua_close(c_State);
|
||||||
c_pState = NULL;
|
c_State = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaManager::RegisterTypes(lua_State* L)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void LuaManager::ReportErrors(lua_State* L)
|
void LuaManager::ReportErrors(lua_State* L)
|
||||||
{
|
{
|
||||||
LuaLog(LOG_ERROR, "Script: %s", lua_tostring(L, -1));
|
LuaLog(LOG_ERROR, "Script: %s", lua_tostring(L, -1));
|
||||||
|
@ -27,19 +27,16 @@ public:
|
|||||||
static void Init();
|
static void Init();
|
||||||
static void CleanUp();
|
static void CleanUp();
|
||||||
|
|
||||||
static lua_State* GetState() { return c_pState; }
|
static lua_State* GetState() { return c_State; }
|
||||||
|
|
||||||
static void ReportErrors(lua_State* L);
|
static void ReportErrors(lua_State* L);
|
||||||
static void LuaLog(int nLevel, const char* format, ... );
|
static void LuaLog(int nLevel, const char* format, ... );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static int c_RefCount;
|
static int c_RefCount;
|
||||||
|
static lua_State* c_State;
|
||||||
static lua_State* c_pState;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void RegisterTypes(lua_State* L);
|
|
||||||
|
|
||||||
static void RegisterGlobal(lua_State* L);
|
static void RegisterGlobal(lua_State* L);
|
||||||
static void RegisterRainmeter(lua_State* L);
|
static void RegisterRainmeter(lua_State* L);
|
||||||
static void RegisterGroup(lua_State* L);
|
static void RegisterGroup(lua_State* L);
|
||||||
|
@ -1,201 +1,220 @@
|
|||||||
|
/*
|
||||||
|
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
|
||||||
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "../StdAfx.h"
|
#include "../StdAfx.h"
|
||||||
#include "LuaScript.h"
|
#include "LuaScript.h"
|
||||||
#include "LuaManager.h"
|
#include "LuaManager.h"
|
||||||
#include "../Rainmeter.h"
|
#include "../Rainmeter.h"
|
||||||
|
|
||||||
LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile) : m_pState(p_pState), m_strFile(_strdup(p_strFile)),
|
/*
|
||||||
|
** LuaScript
|
||||||
|
**
|
||||||
|
** The constructor
|
||||||
|
**
|
||||||
|
*/
|
||||||
|
LuaScript::LuaScript(lua_State* state, const char* file) : m_State(state),
|
||||||
m_iRef(LUA_NOREF),
|
m_iRef(LUA_NOREF),
|
||||||
m_bInitialized(true)
|
m_Initialized(true)
|
||||||
{
|
{
|
||||||
int result = luaL_loadfile(m_pState, p_strFile);
|
int result = luaL_loadfile(m_State, file);
|
||||||
|
|
||||||
// If the file loaded okay.
|
// If the file loaded okay.
|
||||||
if (result == 0)
|
if (result == 0)
|
||||||
{
|
{
|
||||||
// Create the table this script will reside in
|
// Create the table this script will reside in
|
||||||
lua_newtable(m_pState);
|
lua_newtable(m_State);
|
||||||
|
|
||||||
// Create the metatable that will store the global table
|
// Create the metatable that will store the global table
|
||||||
lua_createtable(m_pState, 0, 1);
|
lua_createtable(m_State, 0, 1);
|
||||||
|
|
||||||
// Push the global teble
|
// Push the global teble
|
||||||
lua_pushvalue(m_pState, LUA_GLOBALSINDEX);
|
lua_pushvalue(m_State, LUA_GLOBALSINDEX);
|
||||||
|
|
||||||
// Set the __index of the table to be the global table
|
// Set the __index of the table to be the global table
|
||||||
lua_setfield(m_pState, -2, "__index");
|
lua_setfield(m_State, -2, "__index");
|
||||||
|
|
||||||
// Set the metatable for the script's table
|
// Set the metatable for the script's table
|
||||||
lua_setmetatable(m_pState, -2);
|
lua_setmetatable(m_State, -2);
|
||||||
|
|
||||||
// Put the table into the global table
|
// Put the table into the global table
|
||||||
m_iRef = luaL_ref(m_pState, LUA_GLOBALSINDEX);
|
m_iRef = luaL_ref(m_State, LUA_GLOBALSINDEX);
|
||||||
|
|
||||||
PushTable();
|
PushTable();
|
||||||
|
|
||||||
// Set the environment for the function to be run in to be the table that
|
// Set the environment for the function to be run in to be the table that
|
||||||
// has been created for the script/
|
// has been created for the script/
|
||||||
lua_setfenv(m_pState, -2);
|
lua_setfenv(m_State, -2);
|
||||||
|
|
||||||
// Execute the Lua script
|
// Execute the Lua script
|
||||||
result = lua_pcall(m_pState, 0, LUA_MULTRET, 0);
|
result = lua_pcall(m_State, 0, LUA_MULTRET, 0);
|
||||||
|
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
m_bInitialized = false;
|
m_Initialized = false;
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1));
|
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_State, -1));
|
||||||
lua_pop(m_pState, 1);
|
lua_pop(m_State, 1);
|
||||||
|
|
||||||
luaL_unref(m_pState, LUA_GLOBALSINDEX, m_iRef);
|
luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef);
|
||||||
m_iRef = LUA_NOREF;
|
m_iRef = LUA_NOREF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_bInitialized = false;
|
m_Initialized = false;
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_pState, -1));
|
LuaManager::LuaLog(LOG_ERROR, "Script: Could not run file: %s", lua_tostring(m_State, -1));
|
||||||
lua_pop(m_pState, 1);
|
lua_pop(m_State, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaScript::~LuaScript(void)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
PushTable();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Push the variable name we want to put a value in.
|
** ~LuaScript
|
||||||
lua_pushstring(m_pState, p_strName);
|
**
|
||||||
// Push the value
|
** The destructor
|
||||||
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);
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
LuaScript::~LuaScript()
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
double result = -1;
|
luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef);
|
||||||
|
|
||||||
if (m_bInitialized && p_strFuncName)
|
|
||||||
{
|
|
||||||
// Push our table onto the stack
|
|
||||||
PushTable();
|
|
||||||
|
|
||||||
// Push the function onto the stack
|
|
||||||
lua_getfield(m_pState, -1, p_strFuncName);
|
|
||||||
|
|
||||||
if (lua_pcall(m_pState, 0, 1, 0))
|
|
||||||
{
|
|
||||||
LuaManager::ReportErrors(m_pState);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!lua_isnumber(m_pState, -1))
|
|
||||||
{
|
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s' must return a number: %s", p_strFuncName, m_strFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result = lua_tonumber(m_pState, -1);
|
/*
|
||||||
}
|
** IsFunction
|
||||||
|
**
|
||||||
// Pop both the table and the return value off the stack.
|
** Checks if given function is defined in the script file.
|
||||||
lua_pop(m_pState, 2);
|
**
|
||||||
}
|
*/
|
||||||
|
bool LuaScript::IsFunction(const char* funcName)
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
|
|
||||||
{
|
|
||||||
std::wstring result;
|
|
||||||
|
|
||||||
if (m_bInitialized && p_strFuncName)
|
|
||||||
{
|
|
||||||
// Push our table onto the stack
|
|
||||||
PushTable();
|
|
||||||
|
|
||||||
// Push the function onto the stack
|
|
||||||
lua_getfield(m_pState, -1, p_strFuncName);
|
|
||||||
|
|
||||||
if (lua_pcall(m_pState, 0, 1, 0))
|
|
||||||
{
|
|
||||||
LuaManager::ReportErrors(m_pState);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!lua_isstring(m_pState, -1))
|
|
||||||
{
|
|
||||||
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s' must return a string: %s", p_strFuncName, m_strFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* str = lua_tostring(m_pState, -1);
|
|
||||||
result = ConvertToWide(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pop both the table and the return value off the stack.
|
|
||||||
lua_pop(m_pState, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LuaScript::RunFunction(const char* p_strFuncName)
|
|
||||||
{
|
|
||||||
if (m_bInitialized && p_strFuncName)
|
|
||||||
{
|
|
||||||
// Push our table onto the stack
|
|
||||||
PushTable();
|
|
||||||
|
|
||||||
// Push the function onto the stack
|
|
||||||
lua_getfield(m_pState,-1, p_strFuncName);
|
|
||||||
|
|
||||||
if (lua_pcall(m_pState, 0, 0, 0))
|
|
||||||
{
|
|
||||||
LuaManager::ReportErrors(m_pState);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pop(m_pState, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LuaScript::FunctionExists(const char* p_strFuncName)
|
|
||||||
{
|
{
|
||||||
bool bExists = false;
|
bool bExists = false;
|
||||||
|
|
||||||
if (m_bInitialized && p_strFuncName)
|
if (m_Initialized && funcName)
|
||||||
{
|
{
|
||||||
// Push our table onto the stack
|
// Push our table onto the stack
|
||||||
PushTable();
|
PushTable();
|
||||||
|
|
||||||
// Push the function onto the stack
|
// Push the function onto the stack
|
||||||
lua_getfield(m_pState, -1, p_strFuncName);
|
lua_getfield(m_State, -1, funcName);
|
||||||
|
|
||||||
if (lua_isfunction( m_pState, -1))
|
if (lua_isfunction(m_State, -1))
|
||||||
{
|
{
|
||||||
bExists = true;
|
bExists = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop both the table and the function off the stack.
|
// Pop both the table and the function off the stack.
|
||||||
lua_pop(m_pState, 2);
|
lua_pop(m_State, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bExists;
|
return bExists;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** RunFunction
|
||||||
|
**
|
||||||
|
** Runs given function in script file.
|
||||||
|
**
|
||||||
|
*/
|
||||||
|
void LuaScript::RunFunction(const char* funcName)
|
||||||
|
{
|
||||||
|
if (m_Initialized)
|
||||||
|
{
|
||||||
|
// Push our table onto the stack
|
||||||
|
PushTable();
|
||||||
|
|
||||||
|
// Push the function onto the stack
|
||||||
|
lua_getfield(m_State, -1, funcName);
|
||||||
|
|
||||||
|
if (lua_pcall(m_State, 0, 0, 0))
|
||||||
|
{
|
||||||
|
LuaManager::ReportErrors(m_State);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pop(m_State, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** RunFunctionWithReturn
|
||||||
|
**
|
||||||
|
** Runs given function in script file and stores the retruned number and/or string.
|
||||||
|
** Returns true if the executed function returns a valid value.
|
||||||
|
**
|
||||||
|
*/
|
||||||
|
bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
|
if (m_Initialized)
|
||||||
|
{
|
||||||
|
// Push our table onto the stack
|
||||||
|
PushTable();
|
||||||
|
|
||||||
|
// Push the function onto the stack
|
||||||
|
lua_getfield(m_State, -1, funcName);
|
||||||
|
|
||||||
|
if (lua_pcall(m_State, 0, 1, 0))
|
||||||
|
{
|
||||||
|
LuaManager::ReportErrors(m_State);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (lua_isstring(m_State, -1))
|
||||||
|
{
|
||||||
|
const char* str = lua_tostring(m_State, -1);
|
||||||
|
strValue = ConvertToWide(str);
|
||||||
|
|
||||||
|
// A number is a string and numerical string (e.g. "10") is a number, so check for it here
|
||||||
|
if (lua_isnumber(m_State, -1))
|
||||||
|
{
|
||||||
|
numValue = lua_tonumber(m_State, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pop(m_State, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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_State, p_strName);
|
||||||
|
// // Push the value
|
||||||
|
// tolua_pushusertype(m_State, p_pValue, p_strTypeName);
|
||||||
|
// // Bind the variable
|
||||||
|
// lua_settable(m_State, -3);
|
||||||
|
//
|
||||||
|
// // Pop our table off of the stack
|
||||||
|
// lua_pop(m_State, 1);
|
||||||
|
//
|
||||||
|
// */
|
||||||
|
//
|
||||||
|
// // Push the variable name we want to put a value in.
|
||||||
|
// lua_pushstring(m_State, "SKIN");
|
||||||
|
// // Push the value
|
||||||
|
// tolua_pushusertype(m_State, p_pValue, "CMeterWindow");
|
||||||
|
// // Bind the variable
|
||||||
|
// lua_settable(m_State, -3);
|
||||||
|
//
|
||||||
|
// //lua_pop(m_pLuaScript->GetState(), 1);
|
||||||
|
//}
|
||||||
|
//
|
@ -1,45 +1,46 @@
|
|||||||
#ifndef LUA_SCRIPT_H
|
/*
|
||||||
#define LUA_SCRIPT_H
|
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.
|
||||||
|
|
||||||
#include <stdio.h>
|
This program is distributed in the hope that it will be useful,
|
||||||
#include "string"
|
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
|
||||||
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __LUASCRIPT_H__
|
||||||
|
#define __LUASCRIPT_H__
|
||||||
|
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
|
|
||||||
class LuaScript
|
class LuaScript
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
LuaScript(lua_State* state, const char* file);
|
||||||
|
~LuaScript();
|
||||||
|
|
||||||
LuaScript(lua_State* p_pState, const char* p_strFile);
|
bool IsInitialized() { return m_Initialized; }
|
||||||
|
|
||||||
~LuaScript(void);
|
lua_State* GetState() { return m_State; }
|
||||||
|
void PushTable() { lua_rawgeti(m_State, LUA_GLOBALSINDEX, m_iRef); }
|
||||||
|
|
||||||
bool FunctionExists(const char* p_strFuncName);
|
bool IsFunction(const char* funcName);
|
||||||
|
void RunFunction(const char* funcName);
|
||||||
void RunFunction(const char* p_strFuncName);
|
bool RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue);
|
||||||
|
|
||||||
double RunFunctionDouble(const char* p_strFuncName);
|
|
||||||
|
|
||||||
std::wstring RunFunctionString(const char* p_strFuncName);
|
|
||||||
|
|
||||||
lua_State* GetState() { return m_pState; }
|
|
||||||
|
|
||||||
bool IsInitialized() { return m_bInitialized; }
|
|
||||||
|
|
||||||
void BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName);
|
|
||||||
|
|
||||||
void PushTable() { lua_rawgeti(m_pState, LUA_GLOBALSINDEX, m_iRef); }
|
|
||||||
|
|
||||||
static void ReportErrors(lua_State* L);
|
static void ReportErrors(lua_State* L);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
lua_State* m_State;
|
||||||
|
|
||||||
lua_State* m_pState;
|
|
||||||
|
|
||||||
char* m_strFile;
|
|
||||||
int m_iRef;
|
int m_iRef;
|
||||||
|
bool m_Initialized;
|
||||||
bool m_bInitialized;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -20,5 +20,3 @@ void LuaManager::RegisterGlobal(lua_State* L)
|
|||||||
lua_register(L, "print", Global_Log);
|
lua_register(L, "print", Global_Log);
|
||||||
luaL_register(L, "TO", TO_funcs); // For backwards compatibility
|
luaL_register(L, "TO", TO_funcs); // For backwards compatibility
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user