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"
|
|
|
|
|
|
|
|
#include "lua_measure.h"
|
|
|
|
#include "lua_meter.h"
|
|
|
|
#include "lua_group.h"
|
|
|
|
#include "lua_meterwindow.h"
|
|
|
|
#include "lua_rainmeter.h"
|
|
|
|
#include "lua_meterstring.h"
|
|
|
|
#include "lua_meterimage.h"
|
|
|
|
|
|
|
|
#include "lua_rainmeter_ext.h"
|
|
|
|
|
2011-01-30 15:44:48 +00:00
|
|
|
int LuaManager::c_RefCount = 0;
|
|
|
|
lua_State* LuaManager::c_pState = 0;
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
void LuaManager::Init()
|
|
|
|
{
|
2011-01-30 15:44:48 +00:00
|
|
|
if (c_pState == 0)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
// initialize Lua
|
2011-01-30 15:44:48 +00:00
|
|
|
c_pState = lua_open();
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
//load Lua base libraries
|
2011-01-30 15:44:48 +00:00
|
|
|
luaL_openlibs(c_pState);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:44:48 +00:00
|
|
|
tolua_measure_open(c_pState);
|
|
|
|
tolua_group_open(c_pState);
|
|
|
|
tolua_meter_open(c_pState);
|
|
|
|
tolua_meterwindow_open(c_pState);
|
|
|
|
tolua_rainmeter_open(c_pState);
|
|
|
|
tolua_meter_string_open(c_pState);
|
|
|
|
//tolua_meter_image_open(c_pState);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:44:48 +00:00
|
|
|
luaopen_rainmeter_ext(c_pState);
|
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-01-30 15:44:48 +00:00
|
|
|
if (c_RefCount == 0 && c_pState != 0)
|
2011-01-30 15:39:14 +00:00
|
|
|
{
|
2011-01-30 15:44:48 +00:00
|
|
|
lua_close(c_pState);
|
|
|
|
c_pState = 0;
|
2011-01-30 15:39:14 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaManager::ReportErrors(lua_State * L)
|
|
|
|
{
|
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-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
|
|
|
}
|