2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
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
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-07-06 10:21:18 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-12 19:27:56 +00:00
|
|
|
#include "StdAfx.h"
|
2010-12-12 17:08:36 +00:00
|
|
|
#include "MeasureScript.h"
|
|
|
|
#include "lua/LuaManager.h"
|
|
|
|
#include "Litestep.h"
|
|
|
|
#include "Rainmeter.h"
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
const char* g_InitializeFunctionName = "Initialize";
|
|
|
|
const char* g_UpdateFunctionName = "Update";
|
|
|
|
const char* g_GetStringFunctionName = "GetStringValue";
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureScript::MeasureScript(MeterWindow* meterWindow, const WCHAR* name) : Measure(meterWindow, name),
|
2011-07-06 10:21:18 +00:00
|
|
|
m_HasUpdateFunction(false),
|
2011-08-01 09:08:11 +00:00
|
|
|
m_HasGetStringFunction(false),
|
|
|
|
m_ValueType(LUA_TNIL)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-02 10:04:08 +00:00
|
|
|
LuaManager::Initialize();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureScript::~MeasureScript()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
UninitializeLuaScript();
|
2012-03-02 10:04:08 +00:00
|
|
|
LuaManager::Finalize();
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureScript::UninitializeLuaScript()
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
m_LuaScript.Uninitialize();
|
2011-01-30 15:39:14 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
m_HasUpdateFunction = false;
|
|
|
|
m_HasGetStringFunction = false;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 10:58:48 +00:00
|
|
|
void MeasureScript::Initialize()
|
|
|
|
{
|
|
|
|
Measure::Initialize();
|
|
|
|
|
|
|
|
if (m_LuaScript.IsFunction(g_InitializeFunctionName))
|
|
|
|
{
|
|
|
|
m_LuaScript.RunFunction(g_InitializeFunctionName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
2012-10-07 06:16:09 +00:00
|
|
|
** Runs the function "Update()" in the script.
|
2011-07-06 10:21:18 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureScript::UpdateValue()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-08-01 09:08:11 +00:00
|
|
|
if (m_HasUpdateFunction)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
m_ValueType = m_LuaScript.RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
|
2011-08-01 09:08:11 +00:00
|
|
|
|
2011-08-01 17:08:51 +00:00
|
|
|
if (m_ValueType == LUA_TNIL && m_HasGetStringFunction)
|
2011-08-01 09:08:11 +00:00
|
|
|
{
|
|
|
|
// For backwards compatbility
|
2012-10-06 18:43:07 +00:00
|
|
|
m_ValueType = m_LuaScript.RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
|
2011-08-01 09:08:11 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-07 06:16:09 +00:00
|
|
|
** Returns the value as a string.
|
2011-03-29 19:21:57 +00:00
|
|
|
**
|
2010-12-12 17:08:36 +00:00
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
const WCHAR* MeasureScript::GetStringValue()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
return (m_ValueType == LUA_TSTRING) ? CheckSubstitute(m_StringValue.c_str()) : nullptr;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-06-01 13:06:36 +00:00
|
|
|
** Read the options specified in the ini file.
|
2010-12-12 17:08:36 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureScript::ReadOptions(ConfigParser& parser, const WCHAR* section)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure::ReadOptions(parser, section);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-10-06 18:43:07 +00:00
|
|
|
std::wstring scriptFile = parser.ReadString(section, L"ScriptFile", L"");
|
|
|
|
if (!scriptFile.empty())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-11-28 14:13:20 +00:00
|
|
|
if (m_MeterWindow)
|
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
m_MeterWindow->MakePathAbsolute(scriptFile);
|
2011-11-28 14:13:20 +00:00
|
|
|
}
|
2011-07-09 16:42:51 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
if (!m_Initialized ||
|
2012-10-06 19:03:29 +00:00
|
|
|
wcscmp(scriptFile.c_str(), m_LuaScript.GetFile().c_str()) != 0)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
UninitializeLuaScript();
|
2011-07-27 14:18:02 +00:00
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
if (m_LuaScript.Initialize(scriptFile))
|
2011-02-10 08:48:04 +00:00
|
|
|
{
|
2012-10-06 18:43:07 +00:00
|
|
|
bool hasInitializeFunction = m_LuaScript.IsFunction(g_InitializeFunctionName);
|
|
|
|
m_HasUpdateFunction = m_LuaScript.IsFunction(g_UpdateFunctionName);
|
2011-07-06 10:21:18 +00:00
|
|
|
|
2013-08-07 14:48:13 +00:00
|
|
|
auto L = m_LuaScript.GetState();
|
2012-10-06 18:43:07 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript.GetRef());
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
*(MeterWindow**)lua_newuserdata(L, sizeof(MeterWindow*)) = m_MeterWindow;
|
|
|
|
lua_getglobal(L, "MeterWindow");
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_setfield(L, -2, "SKIN");
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
*(Measure**)lua_newuserdata(L, sizeof(Measure*)) = this;
|
|
|
|
lua_getglobal(L, "Measure");
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_setfield(L, -2, "SELF");
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2013-08-06 17:43:57 +00:00
|
|
|
if (!m_LuaScript.IsUnicode())
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
// For backwards compatibility.
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2013-08-06 17:43:57 +00:00
|
|
|
m_HasGetStringFunction = m_LuaScript.IsFunction(g_GetStringFunctionName);
|
|
|
|
if (m_HasGetStringFunction)
|
|
|
|
{
|
|
|
|
LogWarningF(this, L"Script: Using deprecated GetStringValue()");
|
|
|
|
}
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2013-08-06 17:43:57 +00:00
|
|
|
lua_getfield(L, -1, "PROPERTIES");
|
|
|
|
if (lua_isnil(L, -1) == 0)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
|
|
|
// Look in the table for values to read from the section
|
|
|
|
while (lua_next(L, -2))
|
2011-02-10 08:48:04 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
const char* strKey = lua_tostring(L, -1);
|
|
|
|
const std::wstring wstrKey = StringUtil::Widen(strKey);
|
|
|
|
const std::wstring& wstrValue =
|
|
|
|
parser.ReadString(section, wstrKey.c_str(), L"");
|
|
|
|
if (!wstrValue.empty())
|
|
|
|
{
|
|
|
|
const std::string strStrVal = StringUtil::Narrow(wstrValue);
|
|
|
|
lua_pushstring(L, strStrVal.c_str());
|
|
|
|
lua_setfield(L, -3, strKey);
|
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2013-08-06 18:58:41 +00:00
|
|
|
|
|
|
|
// Pop PROPERTIES table.
|
|
|
|
lua_pop(L, 1);
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
2011-07-06 10:21:18 +00:00
|
|
|
|
2013-08-06 18:58:41 +00:00
|
|
|
// Pop our table.
|
|
|
|
lua_pop(L, 1);
|
2012-07-18 14:41:28 +00:00
|
|
|
|
2013-06-23 10:58:48 +00:00
|
|
|
if (m_Initialized)
|
2012-07-18 14:41:28 +00:00
|
|
|
{
|
2013-06-23 10:58:48 +00:00
|
|
|
// If the measure is already initialized and the script has changed, we need to
|
|
|
|
// manually call Initialize().
|
|
|
|
Initialize();
|
2012-07-18 14:41:28 +00:00
|
|
|
}
|
2012-10-06 18:43:07 +00:00
|
|
|
|
|
|
|
// Valid script.
|
|
|
|
return;
|
2011-01-30 10:39:10 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2012-10-07 19:42:20 +00:00
|
|
|
else if (m_LuaScript.IsInitialized())
|
|
|
|
{
|
|
|
|
// Already initialized.
|
|
|
|
return;
|
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2012-10-06 18:43:07 +00:00
|
|
|
|
2013-06-29 16:59:18 +00:00
|
|
|
LogErrorF(this, L"Script: File not valid");
|
2012-10-06 18:43:07 +00:00
|
|
|
UninitializeLuaScript();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
|
2011-07-07 16:18:39 +00:00
|
|
|
/*
|
2012-02-01 15:55:29 +00:00
|
|
|
** Executes a custom bang.
|
2011-07-07 16:18:39 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureScript::Command(const std::wstring& command)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2013-08-06 17:43:57 +00:00
|
|
|
m_LuaScript.RunString(command);
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
2011-09-09 16:31:55 +00:00
|
|
|
//static void stackDump(lua_State *L)
|
|
|
|
//{
|
2013-05-29 19:05:41 +00:00
|
|
|
// LuaManager::LuaLogger::Debug(" ---------------- Stack Dump ----------------" );
|
2011-09-09 16:31:55 +00:00
|
|
|
// for (int i = lua_gettop(L); i > 0; --i)
|
|
|
|
// {
|
|
|
|
// int t = lua_type(L, i);
|
|
|
|
// switch (t)
|
|
|
|
// {
|
|
|
|
// case LUA_TSTRING:
|
2013-05-29 19:05:41 +00:00
|
|
|
// LuaManager::LuaLogger::Debug("%d:'%s'", i, lua_tostring(L, i));
|
2011-09-09 16:31:55 +00:00
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case LUA_TBOOLEAN:
|
2013-05-29 19:05:41 +00:00
|
|
|
// LuaManager::LuaLogger::Debug("%d: %s", i, lua_toboolean(L, i) ? "true" : "false");
|
2011-09-09 16:31:55 +00:00
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case LUA_TNUMBER:
|
2013-05-29 19:05:41 +00:00
|
|
|
// LuaManager::LuaLogger::Debug("%d: %g", i, lua_tonumber(L, i));
|
2011-09-09 16:31:55 +00:00
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// default:
|
2013-05-29 19:05:41 +00:00
|
|
|
// LuaManager::LuaLogger::Debug("%d: %s", i, lua_typename(L, t));
|
2011-09-09 16:31:55 +00:00
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
2013-05-29 19:05:41 +00:00
|
|
|
// LuaManager::LuaLogger::Debug("--------------- Stack Dump Finished ---------------" );
|
2011-09-09 16:31:55 +00:00
|
|
|
//}
|