2011-07-05 13:41:05 +00:00
|
|
|
/*
|
2012-01-23 06:36:15 +00:00
|
|
|
Copyright (C) 2010 Matt King, Birunthan Mohanathas
|
|
|
|
|
2011-07-05 13:41:05 +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-05 13:41:05 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-13 09:00:02 +00:00
|
|
|
#include "../StdAfx.h"
|
2010-12-12 17:08:36 +00:00
|
|
|
#include "LuaManager.h"
|
|
|
|
#include "../Rainmeter.h"
|
|
|
|
|
2011-01-30 15:44:48 +00:00
|
|
|
int LuaManager::c_RefCount = 0;
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_State* LuaManager::c_State = 0;
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2012-03-02 10:04:08 +00:00
|
|
|
void LuaManager::Initialize()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
if (c_State == NULL)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-07-05 13:41:05 +00:00
|
|
|
// Initialize Lua
|
2012-03-10 07:03:19 +00:00
|
|
|
c_State = luaL_newstate();
|
|
|
|
|
2012-07-10 11:30:45 +00:00
|
|
|
luaL_openlibs(c_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-05 13:41:05 +00:00
|
|
|
// Register custom types and functions
|
2011-07-06 10:21:18 +00:00
|
|
|
RegisterGlobal(c_State);
|
|
|
|
RegisterMeasure(c_State);
|
|
|
|
RegisterMeter(c_State);
|
|
|
|
RegisterMeterWindow(c_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
|
2011-01-30 15:44:48 +00:00
|
|
|
++c_RefCount;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 10:04:08 +00:00
|
|
|
void LuaManager::Finalize()
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 15:44:48 +00:00
|
|
|
if (c_RefCount > 0)
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2011-01-30 15:44:48 +00:00
|
|
|
--c_RefCount;
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:21:18 +00:00
|
|
|
if (c_RefCount == 0 && c_State != NULL)
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2011-07-06 10:21:18 +00:00
|
|
|
lua_close(c_State);
|
|
|
|
c_State = NULL;
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
void LuaManager::ReportErrors(lua_State* L, const std::wstring& file)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2012-05-29 16:43:24 +00:00
|
|
|
const char* error = lua_tostring(L, -1);
|
2010-12-12 17:08:36 +00:00
|
|
|
lua_pop(L, 1);
|
2011-07-07 16:18:39 +00:00
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
// Skip "[string ...]".
|
|
|
|
const char* pos = strchr(error, ':');
|
|
|
|
if (pos)
|
2012-07-21 16:46:00 +00:00
|
|
|
{
|
2012-10-06 19:03:29 +00:00
|
|
|
error = pos;
|
2012-07-21 16:46:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 19:03:29 +00:00
|
|
|
std::wstring str(file, file.find_last_of(L'\\') + 1);
|
|
|
|
str += ConvertToWide(error);
|
|
|
|
LogWithArgs(LOG_ERROR, L"Script: %s", str.c_str());
|
2011-03-29 19:21:57 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
void LuaManager::PushWide(lua_State* L, const WCHAR* str)
|
|
|
|
{
|
2012-07-21 16:46:00 +00:00
|
|
|
lua_pushstring(L, ConvertToAscii(str).c_str());
|
2011-09-03 16:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring LuaManager::ToWide(lua_State* L, int narg)
|
|
|
|
{
|
2012-07-21 16:46:00 +00:00
|
|
|
return ConvertToWide(lua_tostring(L, narg));
|
2011-09-03 16:45:29 +00:00
|
|
|
}
|