mirror of
https://github.com/1dot13/source.git
synced 2026-07-29 13:52:17 +02:00
************************************************************
* Merged Source Code from Development Trunk: Revision 4063 * ************************************************************ - Source Code is merged from: https://81.169.133.124/source/ja2/branches/Wanne/JA2%201.13%20MP - This will be the Source for the Beta 2011 Test git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@4064 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
#ifndef _LUA_FUNCTION_H_
|
||||
#define _LUA_FUNCTION_H_
|
||||
|
||||
#include <lua_state.h>
|
||||
#include <lua_table.h>
|
||||
|
||||
#include <stack>
|
||||
|
||||
class LuaFunction
|
||||
{
|
||||
public:
|
||||
LuaFunction(LuaState L, const char* function_name)
|
||||
: _function_name(function_name), _L(L()), _counter(0), m_is_function(false)
|
||||
{
|
||||
lua_getglobal(_L, function_name);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
LuaFunction& Param(T const& par);
|
||||
|
||||
template<typename TI, typename TV>
|
||||
LuaFunction& TParam(TI const index, TV const& par)
|
||||
{
|
||||
if(m_is_function || is_function(-2))
|
||||
{
|
||||
if(!_tables.empty())
|
||||
{
|
||||
LuaTable& tab = *_tables.top();
|
||||
tab.setValue(index, par);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
LuaFunction& TableOpen()
|
||||
{
|
||||
_tables.push(new LuaTable(_L));
|
||||
return *this;
|
||||
}
|
||||
LuaFunction& TableClose()
|
||||
{
|
||||
if(!_tables.empty())
|
||||
{
|
||||
LuaTable* tab = _tables.top();
|
||||
_tables.pop();
|
||||
if(tab) delete tab;
|
||||
_counter++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Call(int num_ret)
|
||||
{
|
||||
if(m_is_function || is_function())
|
||||
{
|
||||
if(_tables.empty() && num_ret >= 0)
|
||||
{
|
||||
if( lua_pcall(_L, _counter, num_ret, 0) != 0)
|
||||
{
|
||||
std::string err = lua_tostring(_L, -1);
|
||||
|
||||
std::wstring wstr;
|
||||
convert_string(_function_name, wstr);
|
||||
|
||||
std::wstringstream wss;
|
||||
wss << L"Execution of lua function '" << wstr << L"' failed : ";
|
||||
|
||||
convert_string(err, wstr);
|
||||
wss << wstr;
|
||||
|
||||
SGP_THROW(wss.str().c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool is_function(int pos=-1)
|
||||
{
|
||||
return m_is_function = lua_isfunction(_L, pos);
|
||||
}
|
||||
private:
|
||||
std::string _function_name;
|
||||
lua_State* _L;
|
||||
std::stack<LuaTable*> _tables;
|
||||
int _counter;
|
||||
bool m_is_function;
|
||||
};
|
||||
|
||||
|
||||
#endif // _LUA_FUNCTION_H_
|
||||
Reference in New Issue
Block a user