The print() function in Lua scripts now outputs to Rainmeter log (an alternative to the TO.LuaLog() function).

This commit is contained in:
Birunthan Mohanathas 2011-01-30 13:04:17 +00:00
parent 251beb9e3a
commit a69e2e91d2
6 changed files with 25 additions and 29 deletions

View File

@ -17,7 +17,7 @@ lua_State* LuaManager::m_pState = 0;
void LuaManager::Init() void LuaManager::Init()
{ {
if(m_pState == 0) if (m_pState == 0)
{ {
// initialize Lua // initialize Lua
m_pState = lua_open(); m_pState = lua_open();

View File

@ -3,21 +3,19 @@
#include "../Litestep.h" #include "../Litestep.h"
void push_wstring (lua_State* L, void* value, const char* type) void push_wstring(lua_State* L, void* value, const char* type)
{ {
std::string str2 = std::string( ConvertToAscii((const wchar_t*)value) ); std::string str2 = std::string(ConvertToAscii((const wchar_t*)value));
lua_pushstring(L,str2.c_str()); lua_pushstring(L,str2.c_str());
} }
void push_wchar(lua_State* L, void* value, const char* type)
void push_wchar (lua_State* L, void* value, const char* type)
{ {
std::string str2 = ConvertToAscii( (WCHAR*) value ); std::string str2 = ConvertToAscii((WCHAR*)value);
lua_pushstring(L,str2.c_str()); lua_pushstring(L,str2.c_str());
} }
std::wstring to_wstring(lua_State* L, int arg, void* type)
std::wstring to_wstring (lua_State* L, int arg, void* type)
{ {
// We have a static wstring here so we can keep a copy of the string // We have a static wstring here so we can keep a copy of the string
// passed in alive while its being passed around. // passed in alive while its being passed around.
@ -25,7 +23,7 @@ std::wstring to_wstring (lua_State* L, int arg, void* type)
// Rainmeter isn't threaded. // Rainmeter isn't threaded.
static std::wstring str2 = std::wstring(L""); static std::wstring str2 = std::wstring(L"");
str2 = ConvertToWide( lua_tostring(L,arg) ); str2 = ConvertToWide(lua_tostring(L,arg));
return str2; return str2;
} }
@ -38,17 +36,17 @@ const wchar_t* to_wchar (lua_State* L, int arg, void* type)
// Rainmeter isn't threaded. // Rainmeter isn't threaded.
static std::wstring str = std::wstring(L""); static std::wstring str = std::wstring(L"");
str = ConvertToWide( lua_tostring(L,arg) ); str = ConvertToWide(lua_tostring(L,arg));
return str.c_str(); return str.c_str();
} }
int is_wstring (lua_State* L, int lo, const char* type, int def, tolua_Error* err) int is_wstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{ {
return is_wchar(L,lo,type,def,err); return is_wchar(L,lo,type,def,err);
} }
int is_wchar (lua_State* L, int lo, const char* type, int def, tolua_Error* err) int is_wchar(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{ {
if (def && lua_gettop(L)<abs(lo)) if (def && lua_gettop(L)<abs(lo))
return 1; return 1;

View File

@ -17,4 +17,3 @@ const wchar_t* to_wchar (lua_State* L, int arg, void* type);
int is_wchar (lua_State* L, int lo, const char* type, int def, tolua_Error* err); int is_wchar (lua_State* L, int lo, const char* type, int def, tolua_Error* err);
#endif #endif

View File

@ -15,13 +15,13 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s
lua_newtable(m_pState); lua_newtable(m_pState);
// Create the metatable that will store the global table // Create the metatable that will store the global table
lua_createtable(m_pState,0,1); lua_createtable(m_pState, 0, 1);
// Push the global teble // Push the global teble
lua_pushvalue(m_pState,LUA_GLOBALSINDEX); lua_pushvalue(m_pState, LUA_GLOBALSINDEX);
// Set the __index of the table to be the global table // Set the __index of the table to be the global table
lua_setfield(m_pState,-2, "__index"); lua_setfield(m_pState, -2, "__index");
// Set the metatable for the script's table // Set the metatable for the script's table
lua_setmetatable(m_pState, -2); lua_setmetatable(m_pState, -2);
@ -33,7 +33,7 @@ LuaScript::LuaScript(lua_State* p_pState, const char* p_strFile, const char* p_s
// Set the environment for the function to be run in to be the table that // Set the environment for the function to be run in to be the table that
// has been created for the script/ // has been created for the script/
lua_setfenv(m_pState,-2); lua_setfenv(m_pState, -2);
// Execute the Lua script // Execute the Lua script
result = lua_pcall(m_pState, 0, LUA_MULTRET, 0); result = lua_pcall(m_pState, 0, LUA_MULTRET, 0);
@ -93,7 +93,7 @@ double LuaScript::RunFunctionDouble(const char* p_strFuncName)
lua_getglobal(m_pState, m_strTableName); lua_getglobal(m_pState, m_strTableName);
// Push the function onto the stack // Push the function onto the stack
lua_getfield(m_pState,-1, p_strFuncName); lua_getfield(m_pState, -1, p_strFuncName);
if(lua_pcall(m_pState, 0, 1, 0)) if(lua_pcall(m_pState, 0, 1, 0))
{ {
@ -121,7 +121,7 @@ double LuaScript::RunFunctionDouble(const char* p_strFuncName)
std::wstring LuaScript::RunFunctionString(const char* p_strFuncName) std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
{ {
if( m_bInitialized && p_strFuncName ) if (m_bInitialized && p_strFuncName)
{ {
// Push our table onto the stack // Push our table onto the stack
lua_getglobal(m_pState, m_strTableName); lua_getglobal(m_pState, m_strTableName);
@ -137,7 +137,7 @@ std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
{ {
if (!lua_isstring(m_pState, -1)) if (!lua_isstring(m_pState, -1))
{ {
LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a string",m_strTableName, p_strFuncName); LuaManager::LuaLog(LOG_ERROR, "Script: Function '%s:%s' must return a string", m_strTableName, p_strFuncName);
} }
const char* str = lua_tostring(m_pState, -1); const char* str = lua_tostring(m_pState, -1);
@ -155,7 +155,7 @@ std::wstring LuaScript::RunFunctionString(const char* p_strFuncName)
void LuaScript::RunFunction(const char* p_strFuncName) void LuaScript::RunFunction(const char* p_strFuncName)
{ {
if( m_bInitialized && p_strFuncName ) if (m_bInitialized && p_strFuncName)
{ {
// Push our table onto the stack // Push our table onto the stack
lua_getglobal(m_pState, m_strTableName); lua_getglobal(m_pState, m_strTableName);
@ -163,7 +163,7 @@ void LuaScript::RunFunction(const char* p_strFuncName)
// Push the function onto the stack // Push the function onto the stack
lua_getfield(m_pState,-1, p_strFuncName); lua_getfield(m_pState,-1, p_strFuncName);
if( lua_pcall(m_pState, 0, 0, 0) ) if (lua_pcall(m_pState, 0, 0, 0))
{ {
LuaManager::ReportErrors(m_pState); LuaManager::ReportErrors(m_pState);
} }
@ -176,15 +176,15 @@ bool LuaScript::FunctionExists(const char* p_strFuncName)
{ {
bool bExists = false; bool bExists = false;
if( m_bInitialized && p_strFuncName ) if (m_bInitialized && p_strFuncName)
{ {
// Push our table onto the stack // Push our table onto the stack
lua_getglobal(m_pState, m_strTableName); lua_getglobal(m_pState, m_strTableName);
// Push the function onto the stack // Push the function onto the stack
lua_getfield(m_pState,-1, p_strFuncName); lua_getfield(m_pState, -1, p_strFuncName);
if( lua_isfunction( m_pState, lua_gettop(m_pState) ) ) if (lua_isfunction( m_pState, lua_gettop(m_pState)))
{ {
bExists = true; bExists = true;
} }
@ -194,6 +194,4 @@ bool LuaScript::FunctionExists(const char* p_strFuncName)
} }
return bExists; return bExists;
} }

View File

@ -28,7 +28,7 @@ public:
void BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName); void BindVariable(const char* p_strName, void* p_pValue, const char* p_strTypeName);
void PushTable() { lua_getglobal(m_pState, m_strTableName); } void PushTable() { lua_getglobal(m_pState, m_strTableName); }
static void ReportErrors(lua_State * L); static void ReportErrors(lua_State * L);

View File

@ -114,6 +114,7 @@ static const luaL_reg rainmeter_ext_funcs[] =
TOLUA_API int luaopen_rainmeter_ext (lua_State* L) TOLUA_API int luaopen_rainmeter_ext (lua_State* L)
{ {
lua_register(L, "print", staticLuaLog);
luaL_register(L,"TO", rainmeter_ext_funcs); luaL_register(L,"TO", rainmeter_ext_funcs);
return 1; return 1;
} }