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
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
void LuaManager::Init()
|
|
|
|
{
|
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
|
2011-07-06 10:21:18 +00:00
|
|
|
c_State = lua_open();
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-05 13:41:05 +00:00
|
|
|
// Load Lua base libraries
|
2011-07-06 10:21:18 +00:00
|
|
|
luaL_openlibs(c_State);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-07-05 13:41:05 +00:00
|
|
|
// Initialize tolua
|
2011-07-06 10:21:18 +00:00
|
|
|
tolua_open(c_State);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
// Register custom types and functions
|
2011-07-06 10:21:18 +00:00
|
|
|
tolua_module(c_State, NULL, 0);
|
|
|
|
tolua_beginmodule(c_State, NULL);
|
|
|
|
RegisterGlobal(c_State);
|
|
|
|
RegisterMeasure(c_State);
|
|
|
|
RegisterGroup(c_State);
|
|
|
|
RegisterMeasure(c_State);
|
|
|
|
RegisterMeter(c_State);
|
|
|
|
RegisterMeterWindow(c_State);
|
|
|
|
RegisterRainmeter(c_State);
|
|
|
|
RegisterMeterString(c_State);
|
|
|
|
tolua_endmodule(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
|
|
|
}
|
|
|
|
|
|
|
|
void LuaManager::CleanUp()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2011-07-05 13:41:05 +00:00
|
|
|
void LuaManager::ReportErrors(lua_State* L)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 10:39:10 +00:00
|
|
|
LuaLog(LOG_ERROR, "Script: %s", lua_tostring(L, -1));
|
2010-12-12 17:08:36 +00:00
|
|
|
lua_pop(L, 1);
|
2011-03-29 19:21:57 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
void LuaManager::LuaLog(int nLevel, const char* format, ... )
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-02-15 13:22:19 +00:00
|
|
|
char* buffer = new char[4096];
|
2010-12-15 22:03:36 +00:00
|
|
|
va_list args;
|
|
|
|
va_start( args, format );
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2010-12-15 22:03:36 +00:00
|
|
|
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler);
|
|
|
|
_CrtSetReportMode(_CRT_ASSERT, 0);
|
|
|
|
|
|
|
|
errno = 0;
|
2011-02-15 13:22:19 +00:00
|
|
|
_vsnprintf_s( buffer, 4096, _TRUNCATE, format, args );
|
2010-12-15 22:03:36 +00:00
|
|
|
if (errno != 0)
|
|
|
|
{
|
2011-02-15 13:22:19 +00:00
|
|
|
_snprintf_s(buffer, 4096, _TRUNCATE, "Script: LuaLog internal error: %s", format);
|
2010-12-15 22:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_set_invalid_parameter_handler(oldHandler);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
#ifndef _DEBUG
|
|
|
|
// Forcing output to the Debug Output window!
|
|
|
|
OutputDebugStringA(buffer);
|
|
|
|
OutputDebugStringA("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::wstring str = ConvertToWide(buffer);
|
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
LSLog(nLevel, L"Rainmeter", str.c_str());
|
2010-12-12 17:08:36 +00:00
|
|
|
va_end(args);
|
2011-02-15 13:22:19 +00:00
|
|
|
|
|
|
|
delete [] buffer;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|