rainmeter-studio/Library/lua/LuaManager.cpp

104 lines
2.7 KiB
C++
Raw Normal View History

/*
Copyright (C) 2010 Matt King, Birunthan Mohanathas
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "../StdAfx.h"
#include "../../Common/StringUtil.h"
#include "LuaManager.h"
#include "../Logger.h"
2011-01-30 15:44:48 +00:00
int LuaManager::c_RefCount = 0;
lua_State* LuaManager::c_State = 0;
std::vector<bool> LuaManager::c_UnicodeStateStack;
2012-03-02 10:04:08 +00:00
void LuaManager::Initialize()
{
2013-05-31 14:28:39 +00:00
if (c_State == nullptr)
{
// Initialize Lua
c_State = luaL_newstate();
luaL_openlibs(c_State);
// Register custom types and functions
RegisterGlobal(c_State);
RegisterMeasure(c_State);
RegisterMeter(c_State);
RegisterMeterWindow(c_State);
}
2011-01-30 15:44:48 +00:00
++c_RefCount;
}
2012-03-02 10:04:08 +00:00
void LuaManager::Finalize()
{
2011-01-30 15:44:48 +00:00
if (c_RefCount > 0)
{
2011-01-30 15:44:48 +00:00
--c_RefCount;
}
2013-05-31 14:28:39 +00:00
if (c_RefCount == 0 && c_State != nullptr)
{
lua_close(c_State);
2013-05-31 14:28:39 +00:00
c_State = nullptr;
}
}
void LuaManager::ReportErrors(const std::wstring& file)
{
lua_State* L = c_State;
2012-05-29 16:43:24 +00:00
const char* error = lua_tostring(L, -1);
lua_pop(L, 1);
2012-10-06 19:03:29 +00:00
// Skip "[string ...]".
const char* pos = strchr(error, ':');
if (pos)
{
2012-10-06 19:03:29 +00:00
error = pos;
}
2012-10-06 19:03:29 +00:00
std::wstring str(file, file.find_last_of(L'\\') + 1);
str += IsUnicodeState() ? StringUtil::WidenUTF8(error) : StringUtil::Widen(error);
2013-05-30 14:19:42 +00:00
LogErrorF(L"Script: %s", str.c_str());
2011-03-29 19:21:57 +00:00
}
void LuaManager::PushWide(const WCHAR* str)
{
lua_State* L = c_State;
const std::string narrowStr = IsUnicodeState() ?
StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
2013-01-27 10:17:38 +00:00
lua_pushlstring(L, narrowStr.c_str(), narrowStr.length());
}
void LuaManager::PushWide(const std::wstring& str)
2013-01-27 10:17:38 +00:00
{
lua_State* L = c_State;
const std::string narrowStr = IsUnicodeState() ?
StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
2013-01-27 10:17:38 +00:00
lua_pushlstring(L, narrowStr.c_str(), narrowStr.length());
}
std::wstring LuaManager::ToWide(int narg)
{
lua_State* L = c_State;
2013-01-27 10:14:37 +00:00
size_t strLen = 0;
const char* str = lua_tolstring(L, narg, &strLen);
return IsUnicodeState() ?
StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen);
}