2011-07-06 10:21:18 +00:00
|
|
|
/*
|
2012-01-23 06:36:15 +00:00
|
|
|
Copyright (C) 2010 Matt King, Birunthan Mohanathas
|
|
|
|
|
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-13 09:00:02 +00:00
|
|
|
#include "../StdAfx.h"
|
2010-12-12 17:08:36 +00:00
|
|
|
#include "LuaScript.h"
|
|
|
|
#include "LuaManager.h"
|
|
|
|
#include "../Rainmeter.h"
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2012-07-13 11:36:59 +00:00
|
|
|
LuaScript::LuaScript(const WCHAR* scriptFile) :
|
2012-03-09 10:28:25 +00:00
|
|
|
m_Ref(LUA_NOREF),
|
2012-07-13 11:36:59 +00:00
|
|
|
m_Initialized(false)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2012-07-13 11:36:59 +00:00
|
|
|
FILE* file = _wfopen(scriptFile, L"rb");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long fileSize = ftell(file);
|
|
|
|
if (fileSize < 3)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int load = 0;
|
|
|
|
std::string scriptName = ConvertToUTF8(wcsrchr(scriptFile, L'\\') + 1);
|
|
|
|
const char* scriptNameSz = scriptName.c_str();
|
|
|
|
|
|
|
|
BYTE* fileData = new BYTE[fileSize];
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
fread(fileData, fileSize, 1, file);
|
|
|
|
|
|
|
|
if (fileData[0] == 0xEF && fileData[1] == 0xBB && fileData[2] == 0xBF)
|
|
|
|
{
|
|
|
|
// Has UTF8 BOM, so assume that data is already in UTF8.
|
|
|
|
const char* utf8Data = (char*)fileData + 3;
|
|
|
|
int utf8Size = fileSize - 3;
|
|
|
|
|
|
|
|
load = luaL_loadbuffer(L, utf8Data, utf8Size, scriptNameSz);
|
|
|
|
delete [] fileData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Convert the file contents first to WCHAR with respect to the current
|
|
|
|
// code page and then to UTF8 in order to preserve code page specific chars.
|
|
|
|
|
|
|
|
int wideSize = MultiByteToWideChar(CP_ACP, 0, (char*)fileData, fileSize, NULL, 0);
|
|
|
|
WCHAR* wideData = new WCHAR[wideSize];
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, (char*)fileData, fileSize, wideData, wideSize);
|
|
|
|
delete [] fileData;
|
|
|
|
|
|
|
|
int utf8Size = WideCharToMultiByte(CP_UTF8, 0, wideData, wideSize, NULL, 0, NULL, NULL);
|
|
|
|
char* utf8Data = new char[utf8Size];
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wideData, wideSize, utf8Data, utf8Size, NULL, NULL);
|
|
|
|
delete [] wideData;
|
|
|
|
|
|
|
|
load = luaL_loadbuffer(L, utf8Data, utf8Size, scriptNameSz);
|
|
|
|
delete [] utf8Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (load == 0)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Create the table this script will reside in
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_newtable(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Create the metatable that will store the global table
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_createtable(L, 0, 1);
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
// Push the global teble
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
// Set the __index of the table to be the global table
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setfield(L, -2, "__index");
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the metatable for the script's table
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setmetatable(L, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Put the table into the global table
|
2012-03-11 09:59:48 +00:00
|
|
|
m_Ref = luaL_ref(L, LUA_GLOBALSINDEX);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Set the environment for the function to be run in to be the table that
|
|
|
|
// has been created for the script/
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setfenv(L, -2);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Execute the Lua script
|
2012-07-13 11:36:59 +00:00
|
|
|
if (lua_pcall(L, 0, 0, 0) == 0)
|
|
|
|
{
|
|
|
|
m_Initialized = true;
|
|
|
|
}
|
|
|
|
else
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2011-02-10 08:48:04 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
2012-03-09 10:28:25 +00:00
|
|
|
m_Ref = LUA_NOREF;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2012-07-13 11:36:59 +00:00
|
|
|
|
|
|
|
fclose(file);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
LuaScript::~LuaScript()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** Checks if given function is defined in the script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool LuaScript::IsFunction(const char* funcName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
2011-07-06 10:21:18 +00:00
|
|
|
bool bExists = false;
|
2011-02-09 06:10:25 +00:00
|
|
|
|
2011-07-09 16:42:51 +00:00
|
|
|
if (m_Initialized)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_getfield(L, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_isfunction(L, -1))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
bExists = true;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
// Pop both the table and the function off the stack.
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 2);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
return bExists;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
|
|
|
** Runs given function in script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void LuaScript::RunFunction(const char* funcName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (m_Initialized)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_getfield(L, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_pcall(L, 0, 0, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
/*
|
2011-07-31 08:58:43 +00:00
|
|
|
** Runs given function in script file and stores the retruned number or string.
|
2011-07-06 10:21:18 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-08-01 09:08:11 +00:00
|
|
|
int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
2011-08-01 17:08:51 +00:00
|
|
|
int type = LUA_TNIL;
|
2011-07-06 10:21:18 +00:00
|
|
|
|
|
|
|
if (m_Initialized)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
// Push the function onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_getfield(L, -1, funcName);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_pcall(L, 0, 1, 0))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
|
|
|
lua_pop(L, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-07-06 10:21:18 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
type = lua_type(L, -1);
|
2011-08-01 17:08:51 +00:00
|
|
|
if (type == LUA_TNUMBER)
|
2011-07-31 08:58:43 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
numValue = lua_tonumber(L, -1);
|
2011-07-31 08:58:43 +00:00
|
|
|
}
|
2011-08-01 17:08:51 +00:00
|
|
|
else if (type == LUA_TSTRING)
|
2011-07-06 10:21:18 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
const char* str = lua_tostring(L, -1);
|
2012-07-13 11:36:59 +00:00
|
|
|
strValue = ConvertUTF8ToWide(str);
|
2011-08-01 17:20:24 +00:00
|
|
|
numValue = strtod(str, NULL);
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_pop(L, 2);
|
2011-07-25 12:52:46 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 17:08:51 +00:00
|
|
|
return type;
|
2011-07-06 10:21:18 +00:00
|
|
|
}
|
|
|
|
|
2011-07-09 09:23:28 +00:00
|
|
|
/*
|
|
|
|
** Runs given string in the context of the script file.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void LuaScript::RunString(const char* str)
|
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
|
|
|
|
2011-07-09 09:23:28 +00:00
|
|
|
if (m_Initialized)
|
|
|
|
{
|
|
|
|
// Load the string as a Lua chunk
|
2012-03-11 09:59:48 +00:00
|
|
|
if (luaL_loadstring(L, str))
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2011-07-09 09:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push our table onto the stack
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
|
2011-07-09 09:23:28 +00:00
|
|
|
|
|
|
|
// Pop table and set the environment of the loaded chunk to it
|
2012-03-11 09:59:48 +00:00
|
|
|
lua_setfenv(L, -2);
|
2011-07-09 09:23:28 +00:00
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
if (lua_pcall(L, 0, 0, 0))
|
2011-07-09 09:23:28 +00:00
|
|
|
{
|
2012-03-11 09:59:48 +00:00
|
|
|
LuaManager::ReportErrors(L);
|
2011-07-09 09:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|