mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Script: Fixed ScriptFile with Unicode path and tweaks
This commit is contained in:
parent
d449e5dac6
commit
a4844ca924
@ -29,7 +29,6 @@ const char* g_GetStringFunctionName = "GetStringValue";
|
|||||||
**
|
**
|
||||||
*/
|
*/
|
||||||
CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
||||||
m_LuaScript(),
|
|
||||||
m_HasUpdateFunction(false),
|
m_HasUpdateFunction(false),
|
||||||
m_HasGetStringFunction(false),
|
m_HasGetStringFunction(false),
|
||||||
m_ValueType(LUA_TNIL)
|
m_ValueType(LUA_TNIL)
|
||||||
@ -43,19 +42,16 @@ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : C
|
|||||||
*/
|
*/
|
||||||
CMeasureScript::~CMeasureScript()
|
CMeasureScript::~CMeasureScript()
|
||||||
{
|
{
|
||||||
DeleteLuaScript();
|
UninitializeLuaScript();
|
||||||
LuaManager::Finalize();
|
LuaManager::Finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMeasureScript::DeleteLuaScript()
|
void CMeasureScript::UninitializeLuaScript()
|
||||||
{
|
{
|
||||||
delete m_LuaScript;
|
m_LuaScript.Uninitialize();
|
||||||
m_LuaScript = NULL;
|
|
||||||
|
|
||||||
m_HasUpdateFunction = false;
|
m_HasUpdateFunction = false;
|
||||||
m_HasGetStringFunction = false;
|
m_HasGetStringFunction = false;
|
||||||
|
|
||||||
m_ScriptFile.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -66,12 +62,12 @@ void CMeasureScript::UpdateValue()
|
|||||||
{
|
{
|
||||||
if (m_HasUpdateFunction)
|
if (m_HasUpdateFunction)
|
||||||
{
|
{
|
||||||
m_ValueType = m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
|
m_ValueType = m_LuaScript.RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
|
||||||
|
|
||||||
if (m_ValueType == LUA_TNIL && m_HasGetStringFunction)
|
if (m_ValueType == LUA_TNIL && m_HasGetStringFunction)
|
||||||
{
|
{
|
||||||
// For backwards compatbility
|
// For backwards compatbility
|
||||||
m_ValueType = m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
|
m_ValueType = m_LuaScript.RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,37 +94,34 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
|
|||||||
{
|
{
|
||||||
CMeasure::ReadOptions(parser, section);
|
CMeasure::ReadOptions(parser, section);
|
||||||
|
|
||||||
std::wstring file = parser.ReadString(section, L"ScriptFile", L"");
|
std::wstring scriptFile = parser.ReadString(section, L"ScriptFile", L"");
|
||||||
|
if (!scriptFile.empty())
|
||||||
if (!file.empty())
|
|
||||||
{
|
{
|
||||||
if (m_MeterWindow)
|
if (m_MeterWindow)
|
||||||
{
|
{
|
||||||
m_MeterWindow->MakePathAbsolute(file);
|
m_MeterWindow->MakePathAbsolute(scriptFile);
|
||||||
}
|
}
|
||||||
std::string scriptFile = ConvertToAscii(file.c_str());
|
|
||||||
|
|
||||||
if (!m_Initialized ||
|
if (!m_Initialized ||
|
||||||
strcmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0)
|
wcscmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0)
|
||||||
{
|
{
|
||||||
DeleteLuaScript();
|
UninitializeLuaScript();
|
||||||
|
|
||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
m_ScriptFile = scriptFile;
|
m_ScriptFile = scriptFile;
|
||||||
m_LuaScript = new LuaScript(m_ScriptFile.c_str());
|
|
||||||
|
|
||||||
if (m_LuaScript->IsInitialized())
|
if (m_LuaScript.Initialize(m_ScriptFile.c_str()))
|
||||||
{
|
{
|
||||||
bool hasInitializeFunction = m_LuaScript->IsFunction(g_InitializeFunctionName);
|
bool hasInitializeFunction = m_LuaScript.IsFunction(g_InitializeFunctionName);
|
||||||
m_HasUpdateFunction = m_LuaScript->IsFunction(g_UpdateFunctionName);
|
m_HasUpdateFunction = m_LuaScript.IsFunction(g_UpdateFunctionName);
|
||||||
m_HasGetStringFunction = m_LuaScript->IsFunction(g_GetStringFunctionName); // For backwards compatbility
|
m_HasGetStringFunction = m_LuaScript.IsFunction(g_GetStringFunctionName); // For backwards compatbility
|
||||||
|
|
||||||
if (m_HasGetStringFunction)
|
if (m_HasGetStringFunction)
|
||||||
{
|
{
|
||||||
LogWithArgs(LOG_WARNING, L"Script: Using deprecated GetStringValue() in [%s]", m_Name.c_str());
|
LogWithArgs(LOG_WARNING, L"Script: Using deprecated GetStringValue() in [%s]", m_Name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript->GetRef());
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript.GetRef());
|
||||||
|
|
||||||
*(CMeterWindow**)lua_newuserdata(L, sizeof(CMeterWindow*)) = m_MeterWindow;
|
*(CMeterWindow**)lua_newuserdata(L, sizeof(CMeterWindow*)) = m_MeterWindow;
|
||||||
lua_getglobal(L, "CMeterWindow");
|
lua_getglobal(L, "CMeterWindow");
|
||||||
@ -171,20 +164,17 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
|
|||||||
|
|
||||||
if (hasInitializeFunction)
|
if (hasInitializeFunction)
|
||||||
{
|
{
|
||||||
m_LuaScript->RunFunction(g_InitializeFunctionName);
|
m_LuaScript.RunFunction(g_InitializeFunctionName);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
// Valid script.
|
||||||
{
|
return;
|
||||||
DeleteLuaScript();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
LogWithArgs(LOG_ERROR, L"Script: File not valid in [%s]", m_Name.c_str());
|
||||||
LogWithArgs(LOG_ERROR, L"Script: File not valid in [%s]", m_Name.c_str());
|
UninitializeLuaScript();
|
||||||
DeleteLuaScript();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -194,7 +184,7 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
|
|||||||
void CMeasureScript::Command(const std::wstring& command)
|
void CMeasureScript::Command(const std::wstring& command)
|
||||||
{
|
{
|
||||||
std::string str = ConvertToAscii(command.c_str());
|
std::string str = ConvertToAscii(command.c_str());
|
||||||
m_LuaScript->RunString(str.c_str());
|
m_LuaScript.RunString(str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//static void stackDump(lua_State *L)
|
//static void stackDump(lua_State *L)
|
||||||
|
@ -32,14 +32,14 @@ public:
|
|||||||
virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual);
|
virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual);
|
||||||
virtual void Command(const std::wstring& command);
|
virtual void Command(const std::wstring& command);
|
||||||
|
|
||||||
void DeleteLuaScript();
|
void UninitializeLuaScript();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void ReadOptions(CConfigParser& parser, const WCHAR* section);
|
virtual void ReadOptions(CConfigParser& parser, const WCHAR* section);
|
||||||
virtual void UpdateValue();
|
virtual void UpdateValue();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LuaScript* m_LuaScript;
|
LuaScript m_LuaScript;
|
||||||
|
|
||||||
bool m_HasUpdateFunction;
|
bool m_HasUpdateFunction;
|
||||||
bool m_HasGetStringFunction;
|
bool m_HasGetStringFunction;
|
||||||
@ -48,7 +48,7 @@ private:
|
|||||||
|
|
||||||
std::wstring m_StringValue;
|
std::wstring m_StringValue;
|
||||||
|
|
||||||
std::string m_ScriptFile;
|
std::wstring m_ScriptFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -25,15 +25,45 @@
|
|||||||
** The constructor
|
** The constructor
|
||||||
**
|
**
|
||||||
*/
|
*/
|
||||||
LuaScript::LuaScript(const char* file) :
|
LuaScript::LuaScript() :
|
||||||
m_Ref(LUA_NOREF),
|
m_Ref(LUA_NOREF)
|
||||||
m_Initialized(true)
|
|
||||||
{
|
{
|
||||||
lua_State* L = LuaManager::GetState();
|
}
|
||||||
int result = luaL_loadfile(L, file);
|
|
||||||
|
|
||||||
// If the file loaded okay.
|
/*
|
||||||
if (result == 0)
|
** The destructor
|
||||||
|
**
|
||||||
|
*/
|
||||||
|
LuaScript::~LuaScript()
|
||||||
|
{
|
||||||
|
Uninitialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LuaScript::Initialize(const WCHAR* scriptFile)
|
||||||
|
{
|
||||||
|
assert(!IsInitialized());
|
||||||
|
|
||||||
|
lua_State* L = LuaManager::GetState();
|
||||||
|
|
||||||
|
// Load file into a buffer as luaL_loadfile does not support Unicode paths.
|
||||||
|
FILE* file = _wfopen(scriptFile, L"rb");
|
||||||
|
if (!file)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long fileSize = ftell(file);
|
||||||
|
|
||||||
|
char* fileData = new char[fileSize];
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
fread(fileData, fileSize, 1, file);
|
||||||
|
|
||||||
|
std::string scriptName = ConvertToUTF8(wcsrchr(scriptFile, L'\\') + 1);
|
||||||
|
int load = luaL_loadbuffer(L, fileData, fileSize, scriptName.c_str());
|
||||||
|
delete [] fileData;
|
||||||
|
|
||||||
|
if (load == 0)
|
||||||
{
|
{
|
||||||
// Create the table this script will reside in
|
// Create the table this script will reside in
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
@ -60,32 +90,34 @@ LuaScript::LuaScript(const char* file) :
|
|||||||
lua_setfenv(L, -2);
|
lua_setfenv(L, -2);
|
||||||
|
|
||||||
// Execute the Lua script
|
// Execute the Lua script
|
||||||
result = lua_pcall(L, 0, 0, 0);
|
int result = lua_pcall(L, 0, 0, 0);
|
||||||
|
if (result == 0)
|
||||||
if (result)
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
m_Initialized = false;
|
|
||||||
LuaManager::ReportErrors(L);
|
LuaManager::ReportErrors(L);
|
||||||
|
Uninitialize();
|
||||||
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
|
||||||
m_Ref = LUA_NOREF;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_Initialized = false;
|
|
||||||
LuaManager::ReportErrors(L);
|
LuaManager::ReportErrors(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
void LuaScript::Uninitialize()
|
||||||
** The destructor
|
|
||||||
**
|
|
||||||
*/
|
|
||||||
LuaScript::~LuaScript()
|
|
||||||
{
|
{
|
||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
|
||||||
|
if (m_Ref != LUA_NOREF)
|
||||||
|
{
|
||||||
|
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
||||||
|
m_Ref = LUA_NOREF;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -97,7 +129,7 @@ bool LuaScript::IsFunction(const char* funcName)
|
|||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
bool bExists = false;
|
bool bExists = false;
|
||||||
|
|
||||||
if (m_Initialized)
|
if (IsInitialized())
|
||||||
{
|
{
|
||||||
// Push our table onto the stack
|
// Push our table onto the stack
|
||||||
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
||||||
@ -125,7 +157,7 @@ void LuaScript::RunFunction(const char* funcName)
|
|||||||
{
|
{
|
||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
|
|
||||||
if (m_Initialized)
|
if (IsInitialized())
|
||||||
{
|
{
|
||||||
// Push our table onto the stack
|
// Push our table onto the stack
|
||||||
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
||||||
@ -151,7 +183,7 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std
|
|||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
int type = LUA_TNIL;
|
int type = LUA_TNIL;
|
||||||
|
|
||||||
if (m_Initialized)
|
if (IsInitialized())
|
||||||
{
|
{
|
||||||
// Push our table onto the stack
|
// Push our table onto the stack
|
||||||
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
||||||
@ -193,7 +225,7 @@ void LuaScript::RunString(const char* str)
|
|||||||
{
|
{
|
||||||
lua_State* L = LuaManager::GetState();
|
lua_State* L = LuaManager::GetState();
|
||||||
|
|
||||||
if (m_Initialized)
|
if (IsInitialized())
|
||||||
{
|
{
|
||||||
// Load the string as a Lua chunk
|
// Load the string as a Lua chunk
|
||||||
if (luaL_loadstring(L, str))
|
if (luaL_loadstring(L, str))
|
||||||
|
@ -24,10 +24,13 @@
|
|||||||
class LuaScript
|
class LuaScript
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LuaScript(const char* file);
|
LuaScript();
|
||||||
~LuaScript();
|
~LuaScript();
|
||||||
|
|
||||||
bool IsInitialized() { return m_Initialized; }
|
bool Initialize(const WCHAR* scriptFile);
|
||||||
|
void Uninitialize();
|
||||||
|
|
||||||
|
bool IsInitialized() { return m_Ref != LUA_NOREF; }
|
||||||
int GetRef() { return m_Ref; }
|
int GetRef() { return m_Ref; }
|
||||||
|
|
||||||
bool IsFunction(const char* funcName);
|
bool IsFunction(const char* funcName);
|
||||||
@ -37,7 +40,6 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
int m_Ref;
|
int m_Ref;
|
||||||
bool m_Initialized;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user