mirror of
				https://github.com/chibicitiberiu/rainmeter-studio.git
				synced 2024-02-24 04:33:31 +00:00 
			
		
		
		
	Fixed line endings and applied gitignore
This commit is contained in:
		@@ -1,103 +1,103 @@
 | 
			
		||||
/*
 | 
			
		||||
  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"
 | 
			
		||||
 | 
			
		||||
int LuaManager::c_RefCount = 0;
 | 
			
		||||
lua_State* LuaManager::c_State = 0;
 | 
			
		||||
 | 
			
		||||
std::vector<bool> LuaManager::c_UnicodeStateStack;
 | 
			
		||||
 | 
			
		||||
void LuaManager::Initialize()
 | 
			
		||||
{
 | 
			
		||||
	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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	++c_RefCount;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::Finalize()
 | 
			
		||||
{
 | 
			
		||||
	if (c_RefCount > 0)
 | 
			
		||||
	{
 | 
			
		||||
		--c_RefCount;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (c_RefCount == 0 && c_State != nullptr)
 | 
			
		||||
	{
 | 
			
		||||
		lua_close(c_State);
 | 
			
		||||
		c_State = nullptr;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::ReportErrors(const std::wstring& file)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	const char* error = lua_tostring(L, -1);
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
 | 
			
		||||
	// Skip "[string ...]".
 | 
			
		||||
	const char* pos = strchr(error, ':');
 | 
			
		||||
	if (pos)
 | 
			
		||||
	{
 | 
			
		||||
		error = pos;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	std::wstring str(file, file.find_last_of(L'\\') + 1);
 | 
			
		||||
	str += IsUnicodeState() ? StringUtil::WidenUTF8(error) : StringUtil::Widen(error);
 | 
			
		||||
	LogErrorF(L"Script: %s", str.c_str());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::PushWide(const WCHAR* str)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	const std::string narrowStr = IsUnicodeState() ?
 | 
			
		||||
		StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
 | 
			
		||||
	lua_pushlstring(L, narrowStr.c_str(), narrowStr.length());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::PushWide(const std::wstring& str)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	const std::string narrowStr = IsUnicodeState() ?
 | 
			
		||||
		StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
 | 
			
		||||
	lua_pushlstring(L, narrowStr.c_str(), narrowStr.length());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::wstring LuaManager::ToWide(int narg)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	size_t strLen = 0;
 | 
			
		||||
	const char* str = lua_tolstring(L, narg, &strLen);
 | 
			
		||||
	return IsUnicodeState() ?
 | 
			
		||||
		StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen);
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
  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"
 | 
			
		||||
 | 
			
		||||
int LuaManager::c_RefCount = 0;
 | 
			
		||||
lua_State* LuaManager::c_State = 0;
 | 
			
		||||
 | 
			
		||||
std::vector<bool> LuaManager::c_UnicodeStateStack;
 | 
			
		||||
 | 
			
		||||
void LuaManager::Initialize()
 | 
			
		||||
{
 | 
			
		||||
	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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	++c_RefCount;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::Finalize()
 | 
			
		||||
{
 | 
			
		||||
	if (c_RefCount > 0)
 | 
			
		||||
	{
 | 
			
		||||
		--c_RefCount;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (c_RefCount == 0 && c_State != nullptr)
 | 
			
		||||
	{
 | 
			
		||||
		lua_close(c_State);
 | 
			
		||||
		c_State = nullptr;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::ReportErrors(const std::wstring& file)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	const char* error = lua_tostring(L, -1);
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
 | 
			
		||||
	// Skip "[string ...]".
 | 
			
		||||
	const char* pos = strchr(error, ':');
 | 
			
		||||
	if (pos)
 | 
			
		||||
	{
 | 
			
		||||
		error = pos;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	std::wstring str(file, file.find_last_of(L'\\') + 1);
 | 
			
		||||
	str += IsUnicodeState() ? StringUtil::WidenUTF8(error) : StringUtil::Widen(error);
 | 
			
		||||
	LogErrorF(L"Script: %s", str.c_str());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::PushWide(const WCHAR* str)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	const std::string narrowStr = IsUnicodeState() ?
 | 
			
		||||
		StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
 | 
			
		||||
	lua_pushlstring(L, narrowStr.c_str(), narrowStr.length());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::PushWide(const std::wstring& str)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	const std::string narrowStr = IsUnicodeState() ?
 | 
			
		||||
		StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
 | 
			
		||||
	lua_pushlstring(L, narrowStr.c_str(), narrowStr.length());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::wstring LuaManager::ToWide(int narg)
 | 
			
		||||
{
 | 
			
		||||
	lua_State* L = c_State;
 | 
			
		||||
	size_t strLen = 0;
 | 
			
		||||
	const char* str = lua_tolstring(L, narg, &strLen);
 | 
			
		||||
	return IsUnicodeState() ?
 | 
			
		||||
		StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,72 +1,72 @@
 | 
			
		||||
/*
 | 
			
		||||
  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.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#ifndef __LUAMANAGER_H__
 | 
			
		||||
#define __LUAMANAGER_H__
 | 
			
		||||
 | 
			
		||||
extern "C"
 | 
			
		||||
{
 | 
			
		||||
#include "lua.h"
 | 
			
		||||
#include "lualib.h"
 | 
			
		||||
#include "lauxlib.h"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
class LuaManager
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	class ScopedLuaState
 | 
			
		||||
	{
 | 
			
		||||
	public:
 | 
			
		||||
		ScopedLuaState(bool unicode) { LuaManager::c_UnicodeStateStack.push_back(unicode); }
 | 
			
		||||
		~ScopedLuaState() { LuaManager::c_UnicodeStateStack.pop_back(); }
 | 
			
		||||
		operator lua_State*() { return LuaManager::c_State; }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	static void Initialize();
 | 
			
		||||
	static void Finalize();
 | 
			
		||||
 | 
			
		||||
	static ScopedLuaState GetState(bool unicode) { return ScopedLuaState(unicode); }
 | 
			
		||||
 | 
			
		||||
	static bool IsUnicodeState() { return c_UnicodeStateStack.back(); }
 | 
			
		||||
 | 
			
		||||
	static void ReportErrors(const std::wstring& file);
 | 
			
		||||
 | 
			
		||||
	static void PushWide(const WCHAR* str);
 | 
			
		||||
	static void PushWide(const std::wstring& str);
 | 
			
		||||
	static std::wstring ToWide(int narg);
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	static int c_RefCount;
 | 
			
		||||
	static lua_State* c_State;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	static void RegisterGlobal(lua_State* L);
 | 
			
		||||
	static void RegisterMeasure(lua_State* L);
 | 
			
		||||
	static void RegisterMeter(lua_State* L);
 | 
			
		||||
	static void RegisterMeterWindow(lua_State* L);
 | 
			
		||||
	static void RegisterMeterString(lua_State* L);
 | 
			
		||||
 | 
			
		||||
	// If the back of the vector is |true|, Lua strings converted to/from as if they were encoded
 | 
			
		||||
	// in UTF-8. Otherwise Lua strings are treated as if they are encoded in the default system
 | 
			
		||||
	// encoding.
 | 
			
		||||
	static std::vector<bool> c_UnicodeStateStack;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
/*
 | 
			
		||||
  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.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#ifndef __LUAMANAGER_H__
 | 
			
		||||
#define __LUAMANAGER_H__
 | 
			
		||||
 | 
			
		||||
extern "C"
 | 
			
		||||
{
 | 
			
		||||
#include "lua.h"
 | 
			
		||||
#include "lualib.h"
 | 
			
		||||
#include "lauxlib.h"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
class LuaManager
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	class ScopedLuaState
 | 
			
		||||
	{
 | 
			
		||||
	public:
 | 
			
		||||
		ScopedLuaState(bool unicode) { LuaManager::c_UnicodeStateStack.push_back(unicode); }
 | 
			
		||||
		~ScopedLuaState() { LuaManager::c_UnicodeStateStack.pop_back(); }
 | 
			
		||||
		operator lua_State*() { return LuaManager::c_State; }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	static void Initialize();
 | 
			
		||||
	static void Finalize();
 | 
			
		||||
 | 
			
		||||
	static ScopedLuaState GetState(bool unicode) { return ScopedLuaState(unicode); }
 | 
			
		||||
 | 
			
		||||
	static bool IsUnicodeState() { return c_UnicodeStateStack.back(); }
 | 
			
		||||
 | 
			
		||||
	static void ReportErrors(const std::wstring& file);
 | 
			
		||||
 | 
			
		||||
	static void PushWide(const WCHAR* str);
 | 
			
		||||
	static void PushWide(const std::wstring& str);
 | 
			
		||||
	static std::wstring ToWide(int narg);
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	static int c_RefCount;
 | 
			
		||||
	static lua_State* c_State;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	static void RegisterGlobal(lua_State* L);
 | 
			
		||||
	static void RegisterMeasure(lua_State* L);
 | 
			
		||||
	static void RegisterMeter(lua_State* L);
 | 
			
		||||
	static void RegisterMeterWindow(lua_State* L);
 | 
			
		||||
	static void RegisterMeterString(lua_State* L);
 | 
			
		||||
 | 
			
		||||
	// If the back of the vector is |true|, Lua strings converted to/from as if they were encoded
 | 
			
		||||
	// in UTF-8. Otherwise Lua strings are treated as if they are encoded in the default system
 | 
			
		||||
	// encoding.
 | 
			
		||||
	static std::vector<bool> c_UnicodeStateStack;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,15 @@
 | 
			
		||||
lbaselib.c
 | 
			
		||||
- Commented out collectgarbage, getfenv, print, and setfenv functions in base_funcs array
 | 
			
		||||
- Commented out luaL_register for coroutine library
 | 
			
		||||
 | 
			
		||||
liolib.c
 | 
			
		||||
- Commented out popen in iolib array
 | 
			
		||||
- Commented out createstdfile calls for stdin/stdout/stderr in luaopen_io
 | 
			
		||||
- Commented out popen related calls in luaopen_io
 | 
			
		||||
 | 
			
		||||
linit.c
 | 
			
		||||
- Commented out debug and package libraries
 | 
			
		||||
 | 
			
		||||
loslib.c
 | 
			
		||||
- Commented out exit function in syslib array
 | 
			
		||||
lbaselib.c
 | 
			
		||||
- Commented out collectgarbage, getfenv, print, and setfenv functions in base_funcs array
 | 
			
		||||
- Commented out luaL_register for coroutine library
 | 
			
		||||
 | 
			
		||||
liolib.c
 | 
			
		||||
- Commented out popen in iolib array
 | 
			
		||||
- Commented out createstdfile calls for stdin/stdout/stderr in luaopen_io
 | 
			
		||||
- Commented out popen related calls in luaopen_io
 | 
			
		||||
 | 
			
		||||
linit.c
 | 
			
		||||
- Commented out debug and package libraries
 | 
			
		||||
 | 
			
		||||
loslib.c
 | 
			
		||||
- Commented out exit function in syslib array
 | 
			
		||||
- Changed os_setlocale() to noop and to always return nil
 | 
			
		||||
@@ -1,261 +1,261 @@
 | 
			
		||||
/*
 | 
			
		||||
  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 "LuaScript.h"
 | 
			
		||||
#include "LuaManager.h"
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** The constructor
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
LuaScript::LuaScript() :
 | 
			
		||||
	m_Ref(LUA_NOREF),
 | 
			
		||||
	m_Unicode(false)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** The destructor
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
LuaScript::~LuaScript()
 | 
			
		||||
{
 | 
			
		||||
	Uninitialize();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool LuaScript::Initialize(const std::wstring& scriptFile)
 | 
			
		||||
{
 | 
			
		||||
	assert(!IsInitialized());
 | 
			
		||||
 | 
			
		||||
	FILE* file = _wfopen(scriptFile.c_str(), L"rb");
 | 
			
		||||
	if (!file) return false;
 | 
			
		||||
 | 
			
		||||
	fseek(file, 0, SEEK_END);
 | 
			
		||||
	const long fileSize = ftell(file);
 | 
			
		||||
	BYTE* fileData = new BYTE[fileSize];
 | 
			
		||||
	fseek(file, 0, SEEK_SET);
 | 
			
		||||
	fread(fileData, fileSize, 1, file);
 | 
			
		||||
	fclose(file);
 | 
			
		||||
	file = nullptr;
 | 
			
		||||
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
	bool scriptLoaded = false;
 | 
			
		||||
 | 
			
		||||
	// Treat the script as Unicode if it has the UTF-16 LE BOM.
 | 
			
		||||
	m_Unicode = fileSize > 2 && fileData[0] == 0xFF && fileData[1] == 0xFE;
 | 
			
		||||
	if (m_Unicode)
 | 
			
		||||
	{
 | 
			
		||||
		const std::string utf8Data = 
 | 
			
		||||
			StringUtil::NarrowUTF8((WCHAR*)(fileData + 2), (fileSize - 2) / sizeof(WCHAR));
 | 
			
		||||
		scriptLoaded = luaL_loadbuffer(L, utf8Data.c_str(), utf8Data.length(), "") == 0;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		scriptLoaded = luaL_loadbuffer(L, (char*)fileData, fileSize, "") == 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	delete [] fileData;
 | 
			
		||||
 | 
			
		||||
	if (scriptLoaded)
 | 
			
		||||
	{
 | 
			
		||||
		// Create the table this script will reside in
 | 
			
		||||
		lua_newtable(L);
 | 
			
		||||
 | 
			
		||||
		// Create the metatable that will store the global table
 | 
			
		||||
		lua_createtable(L, 0, 1);
 | 
			
		||||
 | 
			
		||||
		// Push the global teble
 | 
			
		||||
		lua_pushvalue(L, LUA_GLOBALSINDEX);
 | 
			
		||||
 | 
			
		||||
		// Set the __index of the table to be the global table
 | 
			
		||||
		lua_setfield(L, -2, "__index");
 | 
			
		||||
 | 
			
		||||
		// Set the metatable for the script's table
 | 
			
		||||
		lua_setmetatable(L, -2);
 | 
			
		||||
 | 
			
		||||
		// Put the table into the global table
 | 
			
		||||
		m_Ref = luaL_ref(L, LUA_GLOBALSINDEX);
 | 
			
		||||
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Set the environment for the function to be run in to be the table that
 | 
			
		||||
		// has been created for the script/
 | 
			
		||||
		lua_setfenv(L, -2);
 | 
			
		||||
 | 
			
		||||
		// Execute the Lua script
 | 
			
		||||
		int result = lua_pcall(L, 0, 0, 0);
 | 
			
		||||
		if (result == 0)
 | 
			
		||||
		{
 | 
			
		||||
			m_File = scriptFile;
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(scriptFile);
 | 
			
		||||
			Uninitialize();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		LuaManager::ReportErrors(scriptFile);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaScript::Uninitialize()
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
 | 
			
		||||
	if (m_Ref != LUA_NOREF)
 | 
			
		||||
	{
 | 
			
		||||
		luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
		m_Ref = LUA_NOREF;
 | 
			
		||||
		m_File.clear();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Checks if given function is defined in the script file.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
bool LuaScript::IsFunction(const char* funcName)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
	bool bExists = false;
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Push the function onto the stack
 | 
			
		||||
		lua_getfield(L, -1, funcName);
 | 
			
		||||
 | 
			
		||||
		bExists = lua_isfunction(L, -1);
 | 
			
		||||
 | 
			
		||||
		// Pop both the table and the function off the stack.
 | 
			
		||||
		lua_pop(L, 2);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bExists;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Runs given function in script file.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
void LuaScript::RunFunction(const char* funcName)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Push the function onto the stack
 | 
			
		||||
		lua_getfield(L, -1, funcName);
 | 
			
		||||
 | 
			
		||||
		if (lua_pcall(L, 0, 0, 0))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		lua_pop(L, 1);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Runs given function in script file and stores the retruned number or string.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
	int type = LUA_TNIL;
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Push the function onto the stack
 | 
			
		||||
		lua_getfield(L, -1, funcName);
 | 
			
		||||
 | 
			
		||||
		if (lua_pcall(L, 0, 1, 0))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
			lua_pop(L, 1);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			type = lua_type(L, -1);
 | 
			
		||||
			if (type == LUA_TNUMBER)
 | 
			
		||||
			{
 | 
			
		||||
				numValue = lua_tonumber(L, -1);
 | 
			
		||||
			}
 | 
			
		||||
			else if (type == LUA_TSTRING)
 | 
			
		||||
			{
 | 
			
		||||
				size_t strLen = 0;
 | 
			
		||||
				const char* str = lua_tolstring(L, -1, &strLen);
 | 
			
		||||
				strValue = m_Unicode ?
 | 
			
		||||
					StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen);
 | 
			
		||||
				numValue = strtod(str, nullptr);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			lua_pop(L, 2);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return type;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Runs given string in the context of the script file.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
void LuaScript::RunString(const std::wstring& str)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		const std::string narrowStr = m_Unicode ?
 | 
			
		||||
			StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
 | 
			
		||||
 | 
			
		||||
		// Load the string as a Lua chunk
 | 
			
		||||
		if (luaL_loadstring(L, narrowStr.c_str()))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Pop table and set the environment of the loaded chunk to it
 | 
			
		||||
		lua_setfenv(L, -2);
 | 
			
		||||
 | 
			
		||||
		if (lua_pcall(L, 0, 0, 0))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
  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 "LuaScript.h"
 | 
			
		||||
#include "LuaManager.h"
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** The constructor
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
LuaScript::LuaScript() :
 | 
			
		||||
	m_Ref(LUA_NOREF),
 | 
			
		||||
	m_Unicode(false)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** The destructor
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
LuaScript::~LuaScript()
 | 
			
		||||
{
 | 
			
		||||
	Uninitialize();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool LuaScript::Initialize(const std::wstring& scriptFile)
 | 
			
		||||
{
 | 
			
		||||
	assert(!IsInitialized());
 | 
			
		||||
 | 
			
		||||
	FILE* file = _wfopen(scriptFile.c_str(), L"rb");
 | 
			
		||||
	if (!file) return false;
 | 
			
		||||
 | 
			
		||||
	fseek(file, 0, SEEK_END);
 | 
			
		||||
	const long fileSize = ftell(file);
 | 
			
		||||
	BYTE* fileData = new BYTE[fileSize];
 | 
			
		||||
	fseek(file, 0, SEEK_SET);
 | 
			
		||||
	fread(fileData, fileSize, 1, file);
 | 
			
		||||
	fclose(file);
 | 
			
		||||
	file = nullptr;
 | 
			
		||||
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
	bool scriptLoaded = false;
 | 
			
		||||
 | 
			
		||||
	// Treat the script as Unicode if it has the UTF-16 LE BOM.
 | 
			
		||||
	m_Unicode = fileSize > 2 && fileData[0] == 0xFF && fileData[1] == 0xFE;
 | 
			
		||||
	if (m_Unicode)
 | 
			
		||||
	{
 | 
			
		||||
		const std::string utf8Data = 
 | 
			
		||||
			StringUtil::NarrowUTF8((WCHAR*)(fileData + 2), (fileSize - 2) / sizeof(WCHAR));
 | 
			
		||||
		scriptLoaded = luaL_loadbuffer(L, utf8Data.c_str(), utf8Data.length(), "") == 0;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		scriptLoaded = luaL_loadbuffer(L, (char*)fileData, fileSize, "") == 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	delete [] fileData;
 | 
			
		||||
 | 
			
		||||
	if (scriptLoaded)
 | 
			
		||||
	{
 | 
			
		||||
		// Create the table this script will reside in
 | 
			
		||||
		lua_newtable(L);
 | 
			
		||||
 | 
			
		||||
		// Create the metatable that will store the global table
 | 
			
		||||
		lua_createtable(L, 0, 1);
 | 
			
		||||
 | 
			
		||||
		// Push the global teble
 | 
			
		||||
		lua_pushvalue(L, LUA_GLOBALSINDEX);
 | 
			
		||||
 | 
			
		||||
		// Set the __index of the table to be the global table
 | 
			
		||||
		lua_setfield(L, -2, "__index");
 | 
			
		||||
 | 
			
		||||
		// Set the metatable for the script's table
 | 
			
		||||
		lua_setmetatable(L, -2);
 | 
			
		||||
 | 
			
		||||
		// Put the table into the global table
 | 
			
		||||
		m_Ref = luaL_ref(L, LUA_GLOBALSINDEX);
 | 
			
		||||
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Set the environment for the function to be run in to be the table that
 | 
			
		||||
		// has been created for the script/
 | 
			
		||||
		lua_setfenv(L, -2);
 | 
			
		||||
 | 
			
		||||
		// Execute the Lua script
 | 
			
		||||
		int result = lua_pcall(L, 0, 0, 0);
 | 
			
		||||
		if (result == 0)
 | 
			
		||||
		{
 | 
			
		||||
			m_File = scriptFile;
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(scriptFile);
 | 
			
		||||
			Uninitialize();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		LuaManager::ReportErrors(scriptFile);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaScript::Uninitialize()
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
 | 
			
		||||
	if (m_Ref != LUA_NOREF)
 | 
			
		||||
	{
 | 
			
		||||
		luaL_unref(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
		m_Ref = LUA_NOREF;
 | 
			
		||||
		m_File.clear();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Checks if given function is defined in the script file.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
bool LuaScript::IsFunction(const char* funcName)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
	bool bExists = false;
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Push the function onto the stack
 | 
			
		||||
		lua_getfield(L, -1, funcName);
 | 
			
		||||
 | 
			
		||||
		bExists = lua_isfunction(L, -1);
 | 
			
		||||
 | 
			
		||||
		// Pop both the table and the function off the stack.
 | 
			
		||||
		lua_pop(L, 2);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bExists;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Runs given function in script file.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
void LuaScript::RunFunction(const char* funcName)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Push the function onto the stack
 | 
			
		||||
		lua_getfield(L, -1, funcName);
 | 
			
		||||
 | 
			
		||||
		if (lua_pcall(L, 0, 0, 0))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		lua_pop(L, 1);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Runs given function in script file and stores the retruned number or string.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
	int type = LUA_TNIL;
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Push the function onto the stack
 | 
			
		||||
		lua_getfield(L, -1, funcName);
 | 
			
		||||
 | 
			
		||||
		if (lua_pcall(L, 0, 1, 0))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
			lua_pop(L, 1);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			type = lua_type(L, -1);
 | 
			
		||||
			if (type == LUA_TNUMBER)
 | 
			
		||||
			{
 | 
			
		||||
				numValue = lua_tonumber(L, -1);
 | 
			
		||||
			}
 | 
			
		||||
			else if (type == LUA_TSTRING)
 | 
			
		||||
			{
 | 
			
		||||
				size_t strLen = 0;
 | 
			
		||||
				const char* str = lua_tolstring(L, -1, &strLen);
 | 
			
		||||
				strValue = m_Unicode ?
 | 
			
		||||
					StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen);
 | 
			
		||||
				numValue = strtod(str, nullptr);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			lua_pop(L, 2);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return type;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** Runs given string in the context of the script file.
 | 
			
		||||
**
 | 
			
		||||
*/
 | 
			
		||||
void LuaScript::RunString(const std::wstring& str)
 | 
			
		||||
{
 | 
			
		||||
	auto L = GetState();
 | 
			
		||||
 | 
			
		||||
	if (IsInitialized())
 | 
			
		||||
	{
 | 
			
		||||
		const std::string narrowStr = m_Unicode ?
 | 
			
		||||
			StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str);
 | 
			
		||||
 | 
			
		||||
		// Load the string as a Lua chunk
 | 
			
		||||
		if (luaL_loadstring(L, narrowStr.c_str()))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Push our table onto the stack
 | 
			
		||||
		lua_rawgeti(L, LUA_GLOBALSINDEX, m_Ref);
 | 
			
		||||
 | 
			
		||||
		// Pop table and set the environment of the loaded chunk to it
 | 
			
		||||
		lua_setfenv(L, -2);
 | 
			
		||||
 | 
			
		||||
		if (lua_pcall(L, 0, 0, 0))
 | 
			
		||||
		{
 | 
			
		||||
			LuaManager::ReportErrors(m_File);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,52 +1,52 @@
 | 
			
		||||
/*
 | 
			
		||||
  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.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#ifndef __LUASCRIPT_H__
 | 
			
		||||
#define __LUASCRIPT_H__
 | 
			
		||||
 | 
			
		||||
#include "LuaManager.h"
 | 
			
		||||
 | 
			
		||||
class LuaScript
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	LuaScript();
 | 
			
		||||
	~LuaScript();
 | 
			
		||||
 | 
			
		||||
	bool Initialize(const std::wstring& scriptFile);
 | 
			
		||||
	void Uninitialize();
 | 
			
		||||
	bool IsInitialized() { return m_Ref != LUA_NOREF; }
 | 
			
		||||
 | 
			
		||||
	const std::wstring& GetFile() { return m_File; }
 | 
			
		||||
	int GetRef() { return m_Ref; }
 | 
			
		||||
	bool IsUnicode() const { return m_Unicode; }
 | 
			
		||||
 | 
			
		||||
	LuaManager::ScopedLuaState GetState() { return LuaManager::GetState(m_Unicode); }
 | 
			
		||||
 | 
			
		||||
	bool IsFunction(const char* funcName);
 | 
			
		||||
	void RunFunction(const char* funcName);
 | 
			
		||||
	int RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue);
 | 
			
		||||
	void RunString(const std::wstring& str);
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	std::wstring m_File;
 | 
			
		||||
	int m_Ref;
 | 
			
		||||
	bool m_Unicode;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
  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.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#ifndef __LUASCRIPT_H__
 | 
			
		||||
#define __LUASCRIPT_H__
 | 
			
		||||
 | 
			
		||||
#include "LuaManager.h"
 | 
			
		||||
 | 
			
		||||
class LuaScript
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	LuaScript();
 | 
			
		||||
	~LuaScript();
 | 
			
		||||
 | 
			
		||||
	bool Initialize(const std::wstring& scriptFile);
 | 
			
		||||
	void Uninitialize();
 | 
			
		||||
	bool IsInitialized() { return m_Ref != LUA_NOREF; }
 | 
			
		||||
 | 
			
		||||
	const std::wstring& GetFile() { return m_File; }
 | 
			
		||||
	int GetRef() { return m_Ref; }
 | 
			
		||||
	bool IsUnicode() const { return m_Unicode; }
 | 
			
		||||
 | 
			
		||||
	LuaManager::ScopedLuaState GetState() { return LuaManager::GetState(m_Unicode); }
 | 
			
		||||
 | 
			
		||||
	bool IsFunction(const char* funcName);
 | 
			
		||||
	void RunFunction(const char* funcName);
 | 
			
		||||
	int RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue);
 | 
			
		||||
	void RunString(const std::wstring& str);
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	std::wstring m_File;
 | 
			
		||||
	int m_Ref;
 | 
			
		||||
	bool m_Unicode;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,81 +1,81 @@
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Logger.h"
 | 
			
		||||
#include "../../../Common/StringUtil.h"
 | 
			
		||||
 | 
			
		||||
static int Print(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	// Modified version of luaB_print()
 | 
			
		||||
	std::string message;
 | 
			
		||||
 | 
			
		||||
	int n = lua_gettop(L);		// Number of arguments
 | 
			
		||||
	lua_getglobal(L, "tostring");
 | 
			
		||||
 | 
			
		||||
	for (int i = 1; i <= n; ++i)
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushvalue(L, -1);	// Function to be called
 | 
			
		||||
		lua_pushvalue(L, i);	// Value to print
 | 
			
		||||
		lua_call(L, 1, 1);
 | 
			
		||||
 | 
			
		||||
		// Get result
 | 
			
		||||
		const char* s = lua_tostring(L, -1);
 | 
			
		||||
		if (s == nullptr)
 | 
			
		||||
		{
 | 
			
		||||
			return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (i > 1)
 | 
			
		||||
		{
 | 
			
		||||
			// About dialog List View doesn't like tabs, just use 4 spaces instead
 | 
			
		||||
			message += "    ";
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		message += s;
 | 
			
		||||
 | 
			
		||||
		// Pop result
 | 
			
		||||
		lua_pop(L, 1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	LogDebug(LuaManager::IsUnicodeState() ?
 | 
			
		||||
		StringUtil::WidenUTF8(message).c_str() : StringUtil::Widen(message).c_str());
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int tolua_cast(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	// Simply push first argument onto stack.
 | 
			
		||||
	lua_pushvalue(L, 1);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterGlobal(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	lua_register(L, "print", Print);
 | 
			
		||||
 | 
			
		||||
	// Register tolua.cast() for backwards compatibility.
 | 
			
		||||
	const luaL_Reg toluaFuncs[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "cast", tolua_cast },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "tolua", toluaFuncs);
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Logger.h"
 | 
			
		||||
#include "../../../Common/StringUtil.h"
 | 
			
		||||
 | 
			
		||||
static int Print(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	// Modified version of luaB_print()
 | 
			
		||||
	std::string message;
 | 
			
		||||
 | 
			
		||||
	int n = lua_gettop(L);		// Number of arguments
 | 
			
		||||
	lua_getglobal(L, "tostring");
 | 
			
		||||
 | 
			
		||||
	for (int i = 1; i <= n; ++i)
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushvalue(L, -1);	// Function to be called
 | 
			
		||||
		lua_pushvalue(L, i);	// Value to print
 | 
			
		||||
		lua_call(L, 1, 1);
 | 
			
		||||
 | 
			
		||||
		// Get result
 | 
			
		||||
		const char* s = lua_tostring(L, -1);
 | 
			
		||||
		if (s == nullptr)
 | 
			
		||||
		{
 | 
			
		||||
			return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (i > 1)
 | 
			
		||||
		{
 | 
			
		||||
			// About dialog List View doesn't like tabs, just use 4 spaces instead
 | 
			
		||||
			message += "    ";
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		message += s;
 | 
			
		||||
 | 
			
		||||
		// Pop result
 | 
			
		||||
		lua_pop(L, 1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	LogDebug(LuaManager::IsUnicodeState() ?
 | 
			
		||||
		StringUtil::WidenUTF8(message).c_str() : StringUtil::Widen(message).c_str());
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int tolua_cast(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	// Simply push first argument onto stack.
 | 
			
		||||
	lua_pushvalue(L, 1);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterGlobal(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	lua_register(L, "print", Print);
 | 
			
		||||
 | 
			
		||||
	// Register tolua.cast() for backwards compatibility.
 | 
			
		||||
	const luaL_Reg toluaFuncs[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "cast", tolua_cast },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "tolua", toluaFuncs);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,158 +1,158 @@
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Measure.h"
 | 
			
		||||
#include "../../MeterWindow.h"
 | 
			
		||||
 | 
			
		||||
#define DECLARE_SELF(L) \
 | 
			
		||||
	void* selfData = lua_touserdata(L, 1); \
 | 
			
		||||
	if (!selfData) return 0; \
 | 
			
		||||
	Measure* self = *(Measure**)selfData;
 | 
			
		||||
 | 
			
		||||
static int GetName(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	LuaManager::PushWide(self->GetName());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetOption(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	MeterWindow* meterWindow = self->GetMeterWindow();
 | 
			
		||||
	ConfigParser& parser = meterWindow->GetParser();
 | 
			
		||||
 | 
			
		||||
	const std::wstring section = LuaManager::ToWide(2);
 | 
			
		||||
	const std::wstring defValue = LuaManager::ToWide(3);
 | 
			
		||||
	const std::wstring& value =
 | 
			
		||||
		parser.ReadString(self->GetName(), section.c_str(), defValue.c_str());
 | 
			
		||||
	LuaManager::PushWide(value);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetNumberOption(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	MeterWindow* meterWindow = self->GetMeterWindow();
 | 
			
		||||
	ConfigParser& parser = meterWindow->GetParser();
 | 
			
		||||
 | 
			
		||||
	std::wstring strTmp = LuaManager::ToWide(2);
 | 
			
		||||
	double value = parser.ReadFloat(self->GetName(), strTmp.c_str(), lua_tonumber(L, 3));
 | 
			
		||||
 | 
			
		||||
	lua_pushnumber(L, value);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Disable(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Disable();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Enable(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Enable();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetRelativeValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetRelativeValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetValueRange(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetValueRange());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMinValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetMinValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMaxValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetMaxValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetStringValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
 | 
			
		||||
	int top = lua_gettop(L);
 | 
			
		||||
	AUTOSCALE autoScale = (top > 1) ? (AUTOSCALE)(int)lua_tonumber(L, 2) : AUTOSCALE_OFF;
 | 
			
		||||
	double scale = (top > 2) ? lua_tonumber(L, 3) : 1.0;
 | 
			
		||||
	int decimals = (int)lua_tonumber(L, 4);
 | 
			
		||||
	bool percentual = lua_toboolean(L, 5) != 0;
 | 
			
		||||
 | 
			
		||||
	const WCHAR* val = self->GetStringOrFormattedValue(autoScale, scale, decimals, percentual);
 | 
			
		||||
	LuaManager::PushWide(val);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterMeasure(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	const luaL_Reg functions[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "GetName", GetName },
 | 
			
		||||
		{ "GetOption", GetOption },
 | 
			
		||||
		{ "GetNumberOption", GetNumberOption },
 | 
			
		||||
		{ "Disable", Disable },
 | 
			
		||||
		{ "Enable", Enable },
 | 
			
		||||
		{ "GetValue", GetValue },
 | 
			
		||||
		{ "GetRelativeValue", GetRelativeValue },
 | 
			
		||||
		{ "GetValueRange", GetValueRange },
 | 
			
		||||
		{ "GetMinValue", GetMinValue },
 | 
			
		||||
		{ "GetMaxValue", GetMaxValue },
 | 
			
		||||
		{ "GetStringValue", GetStringValue },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "Measure", functions);
 | 
			
		||||
	lua_pushvalue(L, -1);
 | 
			
		||||
	lua_setfield(L, -2, "__index");
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Measure.h"
 | 
			
		||||
#include "../../MeterWindow.h"
 | 
			
		||||
 | 
			
		||||
#define DECLARE_SELF(L) \
 | 
			
		||||
	void* selfData = lua_touserdata(L, 1); \
 | 
			
		||||
	if (!selfData) return 0; \
 | 
			
		||||
	Measure* self = *(Measure**)selfData;
 | 
			
		||||
 | 
			
		||||
static int GetName(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	LuaManager::PushWide(self->GetName());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetOption(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	MeterWindow* meterWindow = self->GetMeterWindow();
 | 
			
		||||
	ConfigParser& parser = meterWindow->GetParser();
 | 
			
		||||
 | 
			
		||||
	const std::wstring section = LuaManager::ToWide(2);
 | 
			
		||||
	const std::wstring defValue = LuaManager::ToWide(3);
 | 
			
		||||
	const std::wstring& value =
 | 
			
		||||
		parser.ReadString(self->GetName(), section.c_str(), defValue.c_str());
 | 
			
		||||
	LuaManager::PushWide(value);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetNumberOption(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	MeterWindow* meterWindow = self->GetMeterWindow();
 | 
			
		||||
	ConfigParser& parser = meterWindow->GetParser();
 | 
			
		||||
 | 
			
		||||
	std::wstring strTmp = LuaManager::ToWide(2);
 | 
			
		||||
	double value = parser.ReadFloat(self->GetName(), strTmp.c_str(), lua_tonumber(L, 3));
 | 
			
		||||
 | 
			
		||||
	lua_pushnumber(L, value);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Disable(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Disable();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Enable(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Enable();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetRelativeValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetRelativeValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetValueRange(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetValueRange());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMinValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetMinValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMaxValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetMaxValue());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetStringValue(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
 | 
			
		||||
	int top = lua_gettop(L);
 | 
			
		||||
	AUTOSCALE autoScale = (top > 1) ? (AUTOSCALE)(int)lua_tonumber(L, 2) : AUTOSCALE_OFF;
 | 
			
		||||
	double scale = (top > 2) ? lua_tonumber(L, 3) : 1.0;
 | 
			
		||||
	int decimals = (int)lua_tonumber(L, 4);
 | 
			
		||||
	bool percentual = lua_toboolean(L, 5) != 0;
 | 
			
		||||
 | 
			
		||||
	const WCHAR* val = self->GetStringOrFormattedValue(autoScale, scale, decimals, percentual);
 | 
			
		||||
	LuaManager::PushWide(val);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterMeasure(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	const luaL_Reg functions[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "GetName", GetName },
 | 
			
		||||
		{ "GetOption", GetOption },
 | 
			
		||||
		{ "GetNumberOption", GetNumberOption },
 | 
			
		||||
		{ "Disable", Disable },
 | 
			
		||||
		{ "Enable", Enable },
 | 
			
		||||
		{ "GetValue", GetValue },
 | 
			
		||||
		{ "GetRelativeValue", GetRelativeValue },
 | 
			
		||||
		{ "GetValueRange", GetValueRange },
 | 
			
		||||
		{ "GetMinValue", GetMinValue },
 | 
			
		||||
		{ "GetMaxValue", GetMaxValue },
 | 
			
		||||
		{ "GetStringValue", GetStringValue },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "Measure", functions);
 | 
			
		||||
	lua_pushvalue(L, -1);
 | 
			
		||||
	lua_setfield(L, -2, "__index");
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,175 +1,175 @@
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Meter.h"
 | 
			
		||||
#include "../../MeterString.h"
 | 
			
		||||
 | 
			
		||||
#define DECLARE_SELF(L) \
 | 
			
		||||
	void* selfData = lua_touserdata(L, 1); \
 | 
			
		||||
	if (!selfData) return 0; \
 | 
			
		||||
	Meter* self = *(Meter**)selfData;
 | 
			
		||||
 | 
			
		||||
static int GetName(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	LuaManager::PushWide(self->GetName());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetOption(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	MeterWindow* meterWindow = self->GetMeterWindow();
 | 
			
		||||
	ConfigParser& parser = meterWindow->GetParser();
 | 
			
		||||
 | 
			
		||||
	const std::wstring section = LuaManager::ToWide(2);
 | 
			
		||||
	const std::wstring defValue = LuaManager::ToWide(3);
 | 
			
		||||
	const std::wstring& value =
 | 
			
		||||
		parser.ReadString(self->GetName(), section.c_str(), defValue.c_str());
 | 
			
		||||
	LuaManager::PushWide(value);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetW(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetW());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetH(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetH());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetX(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const bool abs = lua_toboolean(L, 2) != 0;
 | 
			
		||||
	lua_pushnumber(L, self->GetX(abs));
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetY(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const bool abs = lua_toboolean(L, 2) != 0;
 | 
			
		||||
	lua_pushnumber(L, self->GetY(abs));
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetW(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int w = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetW(w);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetH(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int h = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetH(h);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetX(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int x = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetX(x);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetY(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int y = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetY(y);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Hide(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Hide();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Show(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Show();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetText(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	if (self->GetTypeID() == TypeID<MeterString>())
 | 
			
		||||
	{
 | 
			
		||||
		MeterString* string = (MeterString*)self;
 | 
			
		||||
		std::wstring str = LuaManager::ToWide(2);
 | 
			
		||||
		string->SetText(str.c_str());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterMeter(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	const luaL_Reg functions[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "GetName", GetName },
 | 
			
		||||
		{ "GetOption", GetOption },
 | 
			
		||||
		{ "GetW", GetW },
 | 
			
		||||
		{ "GetH", GetH },
 | 
			
		||||
		{ "GetX", GetX },
 | 
			
		||||
		{ "GetY", GetY },
 | 
			
		||||
		{ "SetW", SetW },
 | 
			
		||||
		{ "SetH", SetH },
 | 
			
		||||
		{ "SetX", SetX },
 | 
			
		||||
		{ "SetY", SetY },
 | 
			
		||||
		{ "Hide", Hide },
 | 
			
		||||
		{ "Show", Show },
 | 
			
		||||
		{ "SetText", SetText },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "Meter", functions);
 | 
			
		||||
	lua_pushvalue(L, -1);
 | 
			
		||||
	lua_setfield(L, -2, "__index");
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Meter.h"
 | 
			
		||||
#include "../../MeterString.h"
 | 
			
		||||
 | 
			
		||||
#define DECLARE_SELF(L) \
 | 
			
		||||
	void* selfData = lua_touserdata(L, 1); \
 | 
			
		||||
	if (!selfData) return 0; \
 | 
			
		||||
	Meter* self = *(Meter**)selfData;
 | 
			
		||||
 | 
			
		||||
static int GetName(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	LuaManager::PushWide(self->GetName());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetOption(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	MeterWindow* meterWindow = self->GetMeterWindow();
 | 
			
		||||
	ConfigParser& parser = meterWindow->GetParser();
 | 
			
		||||
 | 
			
		||||
	const std::wstring section = LuaManager::ToWide(2);
 | 
			
		||||
	const std::wstring defValue = LuaManager::ToWide(3);
 | 
			
		||||
	const std::wstring& value =
 | 
			
		||||
		parser.ReadString(self->GetName(), section.c_str(), defValue.c_str());
 | 
			
		||||
	LuaManager::PushWide(value);
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetW(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetW());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetH(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetH());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetX(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const bool abs = lua_toboolean(L, 2) != 0;
 | 
			
		||||
	lua_pushnumber(L, self->GetX(abs));
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetY(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const bool abs = lua_toboolean(L, 2) != 0;
 | 
			
		||||
	lua_pushnumber(L, self->GetY(abs));
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetW(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int w = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetW(w);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetH(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int h = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetH(h);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetX(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int x = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetX(x);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetY(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int y = (int)lua_tonumber(L, 2);
 | 
			
		||||
	self->SetY(y);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Hide(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Hide();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int Show(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	self->Show();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int SetText(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	if (self->GetTypeID() == TypeID<MeterString>())
 | 
			
		||||
	{
 | 
			
		||||
		MeterString* string = (MeterString*)self;
 | 
			
		||||
		std::wstring str = LuaManager::ToWide(2);
 | 
			
		||||
		string->SetText(str.c_str());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterMeter(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	const luaL_Reg functions[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "GetName", GetName },
 | 
			
		||||
		{ "GetOption", GetOption },
 | 
			
		||||
		{ "GetW", GetW },
 | 
			
		||||
		{ "GetH", GetH },
 | 
			
		||||
		{ "GetX", GetX },
 | 
			
		||||
		{ "GetY", GetY },
 | 
			
		||||
		{ "SetW", SetW },
 | 
			
		||||
		{ "SetH", SetH },
 | 
			
		||||
		{ "SetX", SetX },
 | 
			
		||||
		{ "SetY", SetY },
 | 
			
		||||
		{ "Hide", Hide },
 | 
			
		||||
		{ "Show", Show },
 | 
			
		||||
		{ "SetText", SetText },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "Meter", functions);
 | 
			
		||||
	lua_pushvalue(L, -1);
 | 
			
		||||
	lua_setfield(L, -2, "__index");
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,240 +1,240 @@
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Rainmeter.h"
 | 
			
		||||
#include "../../MeterWindow.h"
 | 
			
		||||
#include "../../MeterString.h"
 | 
			
		||||
 | 
			
		||||
#define DECLARE_SELF(L) \
 | 
			
		||||
	void* selfData = lua_touserdata(L, 1); \
 | 
			
		||||
	if (!selfData) return 0; \
 | 
			
		||||
	MeterWindow* self = *(MeterWindow**)selfData;
 | 
			
		||||
 | 
			
		||||
static int Bang(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	ConfigParser& parser = self->GetParser();
 | 
			
		||||
 | 
			
		||||
	std::wstring bang = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	int top = lua_gettop(L);
 | 
			
		||||
	if (top == 2)	// 1 argument
 | 
			
		||||
	{
 | 
			
		||||
		parser.ReplaceVariables(bang);
 | 
			
		||||
		Rainmeter::GetInstance().ExecuteCommand(bang.c_str(), self);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		const WCHAR* bangSz = bang.c_str();
 | 
			
		||||
		if (*bangSz == L'!')
 | 
			
		||||
		{
 | 
			
		||||
			++bangSz;	// Skip "!"
 | 
			
		||||
			std::vector<std::wstring> args;
 | 
			
		||||
			for (int i = 3; i <= top; ++i)
 | 
			
		||||
			{
 | 
			
		||||
				std::wstring tmpSz = LuaManager::ToWide(i);
 | 
			
		||||
				parser.ReplaceVariables(tmpSz);
 | 
			
		||||
				args.push_back(tmpSz);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			Rainmeter::GetInstance().ExecuteBang(bangSz, args, self);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMeter(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const std::wstring meterName = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	Meter* meter = self->GetMeter(meterName);
 | 
			
		||||
	if (meter)
 | 
			
		||||
	{
 | 
			
		||||
		*(Meter**)lua_newuserdata(L, sizeof(Meter*)) = meter;
 | 
			
		||||
		lua_getglobal(L, "Meter");
 | 
			
		||||
		lua_setmetatable(L, -2);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushnil(L);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMeasure(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const std::wstring measureName = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	Measure* measure = self->GetMeasure(measureName);
 | 
			
		||||
	if (measure)
 | 
			
		||||
	{
 | 
			
		||||
		*(Measure**)lua_newuserdata(L, sizeof(Measure*)) = measure;
 | 
			
		||||
		lua_getglobal(L, "Measure");
 | 
			
		||||
		lua_setmetatable(L, -2);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushnil(L);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetVariable(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
 | 
			
		||||
	const std::wstring name = LuaManager::ToWide(2);
 | 
			
		||||
	const std::wstring* value = self->GetParser().GetVariable(name);
 | 
			
		||||
	if (value)
 | 
			
		||||
	{
 | 
			
		||||
		LuaManager::PushWide(*value);
 | 
			
		||||
	}
 | 
			
		||||
	else if (lua_gettop(L) >= 3)
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushvalue(L, 3);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushnil(L);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int ReplaceVariables(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	std::wstring strTmp = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	self->GetParser().ReplaceVariables(strTmp);
 | 
			
		||||
	self->GetParser().ReplaceMeasures(strTmp);
 | 
			
		||||
	LuaManager::PushWide(strTmp);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int ParseFormula(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	std::wstring strTmp = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	double result;
 | 
			
		||||
	if (!self->GetParser().ParseFormula(strTmp, &result))
 | 
			
		||||
	{
 | 
			
		||||
		result = lua_tonumber(L, 2);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	lua_pushnumber(L, result);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int MoveWindow(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int x = (int)lua_tonumber(L, 2);
 | 
			
		||||
	int y = (int)lua_tonumber(L, 3);
 | 
			
		||||
	self->MoveWindow(x, y);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int FadeWindow(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int from = (int)lua_tonumber(L, 2);
 | 
			
		||||
	int to = (int)lua_tonumber(L, 3);
 | 
			
		||||
	self->FadeWindow(from, to);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetW(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetW());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetH(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetH());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetX(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetX());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetY(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetY());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int MakePathAbsolute(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	std::wstring path = LuaManager::ToWide(2);
 | 
			
		||||
	self->MakePathAbsolute(path);
 | 
			
		||||
	LuaManager::PushWide(path);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterMeterWindow(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	const luaL_Reg functions[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "Bang", Bang },
 | 
			
		||||
		{ "GetMeter", GetMeter },
 | 
			
		||||
		{ "GetMeasure", GetMeasure },
 | 
			
		||||
		{ "GetVariable", GetVariable },
 | 
			
		||||
		{ "ReplaceVariables", ReplaceVariables },
 | 
			
		||||
		{ "ParseFormula", ParseFormula },
 | 
			
		||||
		{ "MoveWindow", MoveWindow },
 | 
			
		||||
		{ "FadeWindow", FadeWindow },
 | 
			
		||||
		{ "GetW", GetW },
 | 
			
		||||
		{ "GetH", GetH },
 | 
			
		||||
		{ "GetX", GetX },
 | 
			
		||||
		{ "GetY", GetY },
 | 
			
		||||
		{ "MakePathAbsolute", MakePathAbsolute },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "MeterWindow", functions);
 | 
			
		||||
	lua_pushvalue(L, -1);
 | 
			
		||||
	lua_setfield(L, -2, "__index");
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
  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 "../LuaManager.h"
 | 
			
		||||
#include "../../Rainmeter.h"
 | 
			
		||||
#include "../../MeterWindow.h"
 | 
			
		||||
#include "../../MeterString.h"
 | 
			
		||||
 | 
			
		||||
#define DECLARE_SELF(L) \
 | 
			
		||||
	void* selfData = lua_touserdata(L, 1); \
 | 
			
		||||
	if (!selfData) return 0; \
 | 
			
		||||
	MeterWindow* self = *(MeterWindow**)selfData;
 | 
			
		||||
 | 
			
		||||
static int Bang(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	ConfigParser& parser = self->GetParser();
 | 
			
		||||
 | 
			
		||||
	std::wstring bang = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	int top = lua_gettop(L);
 | 
			
		||||
	if (top == 2)	// 1 argument
 | 
			
		||||
	{
 | 
			
		||||
		parser.ReplaceVariables(bang);
 | 
			
		||||
		Rainmeter::GetInstance().ExecuteCommand(bang.c_str(), self);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		const WCHAR* bangSz = bang.c_str();
 | 
			
		||||
		if (*bangSz == L'!')
 | 
			
		||||
		{
 | 
			
		||||
			++bangSz;	// Skip "!"
 | 
			
		||||
			std::vector<std::wstring> args;
 | 
			
		||||
			for (int i = 3; i <= top; ++i)
 | 
			
		||||
			{
 | 
			
		||||
				std::wstring tmpSz = LuaManager::ToWide(i);
 | 
			
		||||
				parser.ReplaceVariables(tmpSz);
 | 
			
		||||
				args.push_back(tmpSz);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			Rainmeter::GetInstance().ExecuteBang(bangSz, args, self);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMeter(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const std::wstring meterName = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	Meter* meter = self->GetMeter(meterName);
 | 
			
		||||
	if (meter)
 | 
			
		||||
	{
 | 
			
		||||
		*(Meter**)lua_newuserdata(L, sizeof(Meter*)) = meter;
 | 
			
		||||
		lua_getglobal(L, "Meter");
 | 
			
		||||
		lua_setmetatable(L, -2);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushnil(L);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetMeasure(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	const std::wstring measureName = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	Measure* measure = self->GetMeasure(measureName);
 | 
			
		||||
	if (measure)
 | 
			
		||||
	{
 | 
			
		||||
		*(Measure**)lua_newuserdata(L, sizeof(Measure*)) = measure;
 | 
			
		||||
		lua_getglobal(L, "Measure");
 | 
			
		||||
		lua_setmetatable(L, -2);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushnil(L);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetVariable(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
 | 
			
		||||
	const std::wstring name = LuaManager::ToWide(2);
 | 
			
		||||
	const std::wstring* value = self->GetParser().GetVariable(name);
 | 
			
		||||
	if (value)
 | 
			
		||||
	{
 | 
			
		||||
		LuaManager::PushWide(*value);
 | 
			
		||||
	}
 | 
			
		||||
	else if (lua_gettop(L) >= 3)
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushvalue(L, 3);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		lua_pushnil(L);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int ReplaceVariables(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	std::wstring strTmp = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	self->GetParser().ReplaceVariables(strTmp);
 | 
			
		||||
	self->GetParser().ReplaceMeasures(strTmp);
 | 
			
		||||
	LuaManager::PushWide(strTmp);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int ParseFormula(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	std::wstring strTmp = LuaManager::ToWide(2);
 | 
			
		||||
 | 
			
		||||
	double result;
 | 
			
		||||
	if (!self->GetParser().ParseFormula(strTmp, &result))
 | 
			
		||||
	{
 | 
			
		||||
		result = lua_tonumber(L, 2);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	lua_pushnumber(L, result);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int MoveWindow(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int x = (int)lua_tonumber(L, 2);
 | 
			
		||||
	int y = (int)lua_tonumber(L, 3);
 | 
			
		||||
	self->MoveWindow(x, y);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int FadeWindow(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	int from = (int)lua_tonumber(L, 2);
 | 
			
		||||
	int to = (int)lua_tonumber(L, 3);
 | 
			
		||||
	self->FadeWindow(from, to);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetW(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetW());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetH(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetH());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetX(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetX());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int GetY(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	lua_pushnumber(L, self->GetY());
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int MakePathAbsolute(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	DECLARE_SELF(L)
 | 
			
		||||
	std::wstring path = LuaManager::ToWide(2);
 | 
			
		||||
	self->MakePathAbsolute(path);
 | 
			
		||||
	LuaManager::PushWide(path);
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaManager::RegisterMeterWindow(lua_State* L)
 | 
			
		||||
{
 | 
			
		||||
	const luaL_Reg functions[] =
 | 
			
		||||
	{
 | 
			
		||||
		{ "Bang", Bang },
 | 
			
		||||
		{ "GetMeter", GetMeter },
 | 
			
		||||
		{ "GetMeasure", GetMeasure },
 | 
			
		||||
		{ "GetVariable", GetVariable },
 | 
			
		||||
		{ "ReplaceVariables", ReplaceVariables },
 | 
			
		||||
		{ "ParseFormula", ParseFormula },
 | 
			
		||||
		{ "MoveWindow", MoveWindow },
 | 
			
		||||
		{ "FadeWindow", FadeWindow },
 | 
			
		||||
		{ "GetW", GetW },
 | 
			
		||||
		{ "GetH", GetH },
 | 
			
		||||
		{ "GetX", GetX },
 | 
			
		||||
		{ "GetY", GetY },
 | 
			
		||||
		{ "MakePathAbsolute", MakePathAbsolute },
 | 
			
		||||
		{ nullptr, nullptr }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	luaL_register(L, "MeterWindow", functions);
 | 
			
		||||
	lua_pushvalue(L, -1);
 | 
			
		||||
	lua_setfield(L, -2, "__index");
 | 
			
		||||
	lua_pop(L, 1);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user