forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureScript.cpp
288 lines (251 loc) · 7.41 KB
/
MeasureScript.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* Copyright (C) 2010 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "MeasureScript.h"
#include "lua/LuaHelper.h"
#include "Util.h"
#include "Rainmeter.h"
const char* g_InitializeFunctionName = "Initialize";
const char* g_UpdateFunctionName = "Update";
const char* g_GetStringFunctionName = "GetStringValue";
MeasureScript::MeasureScript(Skin* skin, const WCHAR* name) : Measure(skin, name),
m_HasUpdateFunction(false),
m_HasGetStringFunction(false),
m_ValueType(LUA_TNIL)
{
}
MeasureScript::~MeasureScript()
{
UninitializeLuaScript();
}
void MeasureScript::UninitializeLuaScript()
{
m_LuaScript.Uninitialize();
m_HasUpdateFunction = false;
m_HasGetStringFunction = false;
}
void MeasureScript::Initialize()
{
Measure::Initialize();
if (m_LuaScript.IsFunction(g_InitializeFunctionName))
{
m_LuaScript.RunFunction(g_InitializeFunctionName);
}
}
/*
** Runs the function "Update()" in the script.
**
*/
void MeasureScript::UpdateValue()
{
if (m_HasUpdateFunction)
{
m_ValueType = m_LuaScript.RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
if (m_ValueType == LUA_TNIL && m_HasGetStringFunction)
{
// For backwards compatbility
m_ValueType = m_LuaScript.RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
}
}
}
/*
** Returns the value as a string.
**
*/
const WCHAR* MeasureScript::GetStringValue()
{
return (m_ValueType == LUA_TSTRING) ? CheckSubstitute(m_StringValue.c_str()) : nullptr;
}
/*
** Read the options specified in the ini file.
**
*/
void MeasureScript::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
Measure::ReadOptions(parser, section);
std::wstring scriptFile = parser.ReadString(section, L"ScriptFile", L"");
if (!scriptFile.empty())
{
if (m_Skin)
{
m_Skin->MakePathAbsolute(scriptFile);
}
if (!m_Initialized ||
wcscmp(scriptFile.c_str(), m_LuaScript.GetFile().c_str()) != 0)
{
UninitializeLuaScript();
if (m_LuaScript.Initialize(scriptFile))
{
bool hasInitializeFunction = m_LuaScript.IsFunction(g_InitializeFunctionName);
m_HasUpdateFunction = m_LuaScript.IsFunction(g_UpdateFunctionName);
auto L = m_LuaScript.GetState();
lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript.GetRef());
*(Skin**)lua_newuserdata(L, sizeof(Skin*)) = m_Skin;
lua_getglobal(L, "MeterWindow");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "SKIN");
*(Measure**)lua_newuserdata(L, sizeof(Measure*)) = this;
lua_getglobal(L, "Measure");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "SELF");
if (!m_LuaScript.IsUnicode())
{
// For backwards compatibility.
m_HasGetStringFunction = m_LuaScript.IsFunction(g_GetStringFunctionName);
if (m_HasGetStringFunction)
{
LogWarningF(this, L"Script: Using deprecated GetStringValue()");
}
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))
{
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);
}
}
}
// Pop PROPERTIES table.
lua_pop(L, 1);
}
// Pop our table.
lua_pop(L, 1);
if (m_Initialized)
{
// If the measure is already initialized and the script has changed, we need to
// manually call Initialize().
Initialize();
}
// Valid script.
return;
}
}
else if (m_LuaScript.IsInitialized())
{
// Already initialized.
return;
}
}
// Disabled measures do not log any messages.
// In this case, temporarily enable the measure to log the error message.
bool isDisabled = m_Disabled;
m_Disabled = false;
LogErrorF(this, L"Script: File not valid");
m_Disabled = isDisabled;
UninitializeLuaScript();
}
/*
** Executes a custom bang.
**
*/
void MeasureScript::Command(const std::wstring& command)
{
m_LuaScript.RunString(command);
}
bool MeasureScript::CommandWithReturn(const std::wstring& command, std::wstring& strValue, void* delayedLogEntry)
{
// Scripts need to be initialized so that any variables declared in
// the Initialize() function in the lua script file are accessible.
// Return "0" so that no errors are thrown for forumlas.
if (!m_Initialized)
{
strValue = L"0";
return true;
}
WCHAR errMsg[MAX_LINE_LENGTH];
size_t sPos = command.find_first_of(L'(');
if (sPos != std::wstring::npos)
{
// Function call
size_t ePos = command.find_last_of(L')');
if (ePos == std::wstring::npos ||
sPos > ePos ||
command.size() < 3)
{
_snwprintf_s(errMsg, _TRUNCATE, L"Invalid function call: %s", command.c_str());
if (delayedLogEntry)
{
std::wstring source = m_Skin->GetSkinPath();
source += L" - [";
source += GetOriginalName();
source += L']';
// Since scripts can accept single brackets as input, the nested variable parser
// can send incomplete section variable to the script, so store a delayed message
// in case the "actual" section variable is invalid. If the "final" variable the
// parser finds is a valid variable, this error message will not be logged.
// See: |ConfigParser::ParseVariables|
auto* log = (Logger::Entry*)delayedLogEntry;
*log = { Logger::Level::Error, L"", source.c_str(), errMsg };
}
else
{
LogErrorF(this, errMsg);
}
return false;
}
std::wstring funcName = command.substr(0, sPos);
auto args = ConfigParser::Tokenize2(
command.substr(sPos + 1, ePos - sPos - 1),
L',',
PairedPunctuation::BothQuotes);
if (!m_LuaScript.RunCustomFunction(funcName, args, strValue))
{
if (!strValue.empty())
{
LogErrorF(this, L"%s", strValue.c_str());
}
return false;
}
}
else
{
if (!m_LuaScript.GetLuaVariable(command, strValue))
{
LogErrorF(this, L"%s", strValue.c_str());
return false;
}
}
return true;
}
//static void stackDump(lua_State *L)
//{
// LuaHelper::LuaLogger::Debug(" ---------------- Stack Dump ----------------" );
// for (int i = lua_gettop(L); i > 0; --i)
// {
// int t = lua_type(L, i);
// switch (t)
// {
// case LUA_TSTRING:
// LuaHelper::LuaLogger::Debug("%d:'%s'", i, lua_tostring(L, i));
// break;
//
// case LUA_TBOOLEAN:
// LuaHelper::LuaLogger::Debug("%d: %s", i, lua_toboolean(L, i) ? "true" : "false");
// break;
//
// case LUA_TNUMBER:
// LuaHelper::LuaLogger::Debug("%d: %g", i, lua_tonumber(L, i));
// break;
//
// default:
// LuaHelper::LuaLogger::Debug("%d: %s", i, lua_typename(L, t));
// break;
// }
// }
// LuaHelper::LuaLogger::Debug("--------------- Stack Dump Finished ---------------" );
//}