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
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
** CMeasureScript
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2011-02-15 16:26:54 +00:00
|
|
|
CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
2011-07-06 10:21:18 +00:00
|
|
|
m_LuaScript(),
|
|
|
|
m_HasInitializeFunction(false),
|
|
|
|
m_HasUpdateFunction(false),
|
|
|
|
m_HasGetStringFunction(false)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
LuaManager::Init();
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** ~CMeasureScript
|
|
|
|
**
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
2010-12-12 17:08:36 +00:00
|
|
|
CMeasureScript::~CMeasureScript()
|
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
DeleteLuaScript();
|
|
|
|
LuaManager::CleanUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMeasureScript::DeleteLuaScript()
|
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
if (m_LuaScript)
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
delete m_LuaScript;
|
|
|
|
m_LuaScript = NULL;
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
m_HasInitializeFunction = false;
|
|
|
|
m_HasUpdateFunction = false;
|
|
|
|
m_HasGetStringFunction = false;
|
2011-07-27 14:18:02 +00:00
|
|
|
|
|
|
|
m_ScriptFile.clear();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** Initialize
|
|
|
|
**
|
|
|
|
** Initializes the measure.
|
|
|
|
**
|
|
|
|
*/
|
2010-12-12 17:08:36 +00:00
|
|
|
void CMeasureScript::Initialize()
|
|
|
|
{
|
|
|
|
CMeasure::Initialize();
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (m_HasInitializeFunction)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
m_LuaScript->RunFunction(g_InitializeFunctionName);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** Update
|
|
|
|
**
|
|
|
|
** Updates the current disk free space value.
|
|
|
|
**
|
|
|
|
*/
|
2010-12-12 17:08:36 +00:00
|
|
|
bool CMeasureScript::Update()
|
|
|
|
{
|
|
|
|
if (!CMeasure::PreUpdate())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-25 17:59:43 +00:00
|
|
|
if (!(m_HasUpdateFunction && m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue)) &&
|
|
|
|
!(m_HasGetStringFunction && m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue)))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-25 17:59:43 +00:00
|
|
|
if (!m_StringValue.empty())
|
2011-07-06 10:21:18 +00:00
|
|
|
{
|
2011-07-25 17:59:43 +00:00
|
|
|
m_Value = 0;
|
|
|
|
m_StringValue.clear();
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
return PostUpdate();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GetStringValue
|
|
|
|
**
|
2011-07-06 10:21:18 +00:00
|
|
|
** Returns the time as string.
|
2011-03-29 19:21:57 +00:00
|
|
|
**
|
2010-12-12 17:08:36 +00:00
|
|
|
*/
|
2011-01-19 15:31:45 +00:00
|
|
|
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 20:12:05 +00:00
|
|
|
return CheckSubstitute(m_StringValue.c_str());
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Reads the measure specific configs.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
// Read common configs
|
2010-12-12 17:08:36 +00:00
|
|
|
CMeasure::ReadConfig(parser, section);
|
|
|
|
|
2011-07-09 16:42:51 +00:00
|
|
|
std::wstring file = parser.ReadString(section, L"ScriptFile", L"");
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-09 16:42:51 +00:00
|
|
|
if (!file.empty())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-27 14:18:02 +00:00
|
|
|
std::string scriptFile = ConvertToAscii(m_MeterWindow->MakePathAbsolute(file).c_str());
|
2011-07-09 16:42:51 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
if (!m_Initialized ||
|
2011-07-27 14:18:02 +00:00
|
|
|
scriptFile != m_ScriptFile)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-02-10 08:48:04 +00:00
|
|
|
DeleteLuaScript();
|
2011-07-27 14:18:02 +00:00
|
|
|
|
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
m_ScriptFile = scriptFile;
|
2011-07-06 10:21:18 +00:00
|
|
|
m_LuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str());
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (m_LuaScript->IsInitialized())
|
2011-02-10 08:48:04 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
m_HasInitializeFunction = m_LuaScript->IsFunction(g_InitializeFunctionName);
|
|
|
|
m_HasUpdateFunction = m_LuaScript->IsFunction(g_UpdateFunctionName);
|
|
|
|
m_HasGetStringFunction = m_LuaScript->IsFunction(g_GetStringFunctionName); // For backwards compatbility
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
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();
|
2011-02-10 08:48:04 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
lua_pushstring(L, "SKIN");
|
|
|
|
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
// Look in the properties table for values to read from the section.
|
2011-02-10 08:48:04 +00:00
|
|
|
lua_getfield(L, -1, "PROPERTIES");
|
|
|
|
if (lua_isnil(L, -1) == 0)
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2011-02-10 08:48:04 +00:00
|
|
|
lua_pushnil(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
while (lua_next(L, -2))
|
2011-02-10 08:48:04 +00:00
|
|
|
{
|
|
|
|
lua_pop(L, 1);
|
|
|
|
const char* strKey = lua_tostring(L, -1);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
std::wstring wstrKey = ConvertToWide(strKey);
|
|
|
|
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
if (!wstrValue.empty())
|
|
|
|
{
|
|
|
|
std::string strStrVal = ConvertToAscii(wstrValue.c_str());
|
|
|
|
const char* strValue = strStrVal.c_str();
|
2011-01-30 15:39:14 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
lua_pushstring(L, strValue);
|
|
|
|
lua_setfield(L, -3, strKey);
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
2011-07-06 10:21:18 +00:00
|
|
|
|
2011-02-10 08:48:04 +00:00
|
|
|
// Pop PROPERTIES table
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
// Pop the table
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DeleteLuaScript();
|
2011-01-30 10:39:10 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
std::wstring error = L"Script: ScriptFile= is not valid in measure [";
|
|
|
|
error += m_Name;
|
|
|
|
error += L"].";
|
|
|
|
Log(LOG_WARNING, error.c_str());
|
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
DeleteLuaScript();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
|
2011-07-07 16:18:39 +00:00
|
|
|
/*
|
|
|
|
** ExecuteBang
|
|
|
|
**
|
|
|
|
** Sends a bang to the measure.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasureScript::ExecuteBang(const WCHAR* args)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-09 09:23:28 +00:00
|
|
|
std::string str = ConvertToAscii(args);
|
|
|
|
m_LuaScript->RunString(str.c_str());
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stackDump(lua_State *L)
|
|
|
|
{
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, " ---------------- Stack Dump ----------------" );
|
2011-07-07 16:18:39 +00:00
|
|
|
for (int i = lua_gettop(L); i > 0; --i)
|
2011-07-06 10:21:18 +00:00
|
|
|
{
|
|
|
|
int t = lua_type(L, i);
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case LUA_TSTRING:
|
2011-07-07 16:18:39 +00:00
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d:'%s'", i, lua_tostring(L, i));
|
2011-07-06 10:21:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TBOOLEAN:
|
2011-07-07 16:18:39 +00:00
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_toboolean(L, i) ? "true" : "false");
|
2011-07-06 10:21:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TNUMBER:
|
2011-07-07 16:18:39 +00:00
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %g", i, lua_tonumber(L, i));
|
2011-07-06 10:21:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
|
|
|
}
|