Добавлена поддержка Lua функций

This commit is contained in:
Kernell 2015-11-29 18:47:49 +03:00
parent d8197802d8
commit 74237747c1
3 changed files with 264 additions and 254 deletions

View File

@ -272,23 +272,23 @@ void CLuaArgument::Read ( lua_State* luaVM, signed int uiArgument )
m_pArray = new CLuaArguments();
lua_pushnil( luaVM );
uiArgument--;
while( lua_next( luaVM, uiArgument ) != 0 )
{
CLuaArgument pKey( luaVM, -2 );
CLuaArgument pValue( luaVM, -1 );
if( pKey.GetType() == LUA_TSTRING )
{
m_pTable[ string( pKey.GetString() ) ] = &pValue;
}
m_pArray->PushArgument( pValue );
lua_pop( luaVM, 1 );
lua_pushnil( luaVM );
uiArgument--;
while( lua_next( luaVM, uiArgument ) != 0 )
{
CLuaArgument pKey( luaVM, -2 );
CLuaArgument pValue( luaVM, -1 );
if( pKey.GetType() == LUA_TSTRING )
{
m_pTable[ string( pKey.GetString() ) ] = &pValue;
}
m_pArray->PushArgument( pValue );
lua_pop( luaVM, 1 );
}
break;

View File

@ -1,173 +1,181 @@
/*********************************************************
*
* Multi Theft Auto: San Andreas - Deathmatch
*
* Squirrel 3, External lua add-on module
*
* Copyright © 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is © 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
* PROVIDED WITH THIS PACKAGE.
*
*********************************************************/
#include "CLuaArguments.h"
#include <assert.h>
CLuaArguments::CLuaArguments ( const CLuaArguments& Arguments )
{
// Copy all the arguments
vector < CLuaArgument* > ::const_iterator iter = Arguments.m_Arguments.begin ();
for ( ; iter != Arguments.m_Arguments.end (); iter++ )
{
CLuaArgument* pArgument = new CLuaArgument ( **iter );
m_Arguments.push_back ( pArgument );
}
}
const CLuaArguments& CLuaArguments::operator = ( const CLuaArguments& Arguments )
{
// Clear our previous list if any
DeleteArguments ();
// Copy all the arguments
vector < CLuaArgument* > ::const_iterator iter = Arguments.m_Arguments.begin ();
for ( ; iter != Arguments.m_Arguments.end (); iter++ )
{
CLuaArgument* pArgument = new CLuaArgument ( **iter );
m_Arguments.push_back ( pArgument );
}
// Return the given reference allowing for chaining
return Arguments;
}
void CLuaArguments::ReadArguments ( lua_State* luaVM, unsigned int uiIndexBegin )
{
// Delete the previous arguments if any
DeleteArguments ();
// Start reading arguments until there are none left
while ( lua_type ( luaVM, uiIndexBegin ) != LUA_TNONE )
{
// Create an argument, let it read out the argument and add it to our vector
CLuaArgument* pArgument = new CLuaArgument ( luaVM, uiIndexBegin++ );
m_Arguments.push_back ( pArgument );
}
}
void CLuaArguments::PushArguments ( lua_State* luaVM ) const
{
// Push all our arguments
vector < CLuaArgument* > ::const_iterator iter = m_Arguments.begin ();
for ( ; iter != m_Arguments.end (); iter++ )
{
(*iter)->Push ( luaVM );
}
}
void CLuaArguments::PushArguments ( CLuaArguments& Arguments )
{
vector < CLuaArgument* > ::const_iterator iter = Arguments.IterBegin ();
for ( ; iter != Arguments.IterEnd (); iter++ )
{
CLuaArgument* pArgument = new CLuaArgument ( **iter );
m_Arguments.push_back ( pArgument );
}
}
bool CLuaArguments::Call ( lua_State* luaVM, const char* szFunction, int iResults, int iErrorFunc ) const
{
assert ( szFunction );
// Add the function name to the stack and get the event from the table
assert ( luaVM );
lua_pushstring ( luaVM, szFunction );
lua_gettable ( luaVM, LUA_GLOBALSINDEX );
// Push our arguments onto the stack
PushArguments ( luaVM );
int iret = lua_pcall ( luaVM, m_Arguments.size(), iResults, iErrorFunc );
if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM )
{
const char* szRes = lua_tostring( luaVM, -1 );
return false; // the function call failed
}
return true;
}
CLuaArgument* CLuaArguments::PushNil ( void )
{
CLuaArgument* pArgument = new CLuaArgument;
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushBoolean ( bool bBool )
{
CLuaArgument* pArgument = new CLuaArgument ( bBool );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushNumber ( double dNumber )
{
CLuaArgument* pArgument = new CLuaArgument ( dNumber );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushString ( const char* szString )
{
CLuaArgument* pArgument = new CLuaArgument ( szString );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushUserData ( void* pUserData )
{
CLuaArgument* pArgument = new CLuaArgument ( pUserData );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushArgument ( const CLuaArgument & argument )
{
CLuaArgument* pArgument = new CLuaArgument (argument); // create a copy
m_Arguments.push_back ( pArgument );
return pArgument;
}
void CLuaArguments::DeleteArguments ( void )
{
// Delete each item
vector < CLuaArgument* > ::iterator iter = m_Arguments.begin ();
for ( ; iter != m_Arguments.end (); iter++ )
{
delete *iter;
}
// Clear the vector
m_Arguments.clear ();
/*********************************************************
*
* Multi Theft Auto: San Andreas - Deathmatch
*
* Squirrel 3, External lua add-on module
*
* Copyright © 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is © 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
* PROVIDED WITH THIS PACKAGE.
*
*********************************************************/
#include "CLuaArguments.h"
#include <assert.h>
CLuaArguments::CLuaArguments ( const CLuaArguments& Arguments )
{
// Copy all the arguments
vector < CLuaArgument* > ::const_iterator iter = Arguments.m_Arguments.begin ();
for ( ; iter != Arguments.m_Arguments.end (); iter++ )
{
CLuaArgument* pArgument = new CLuaArgument ( **iter );
m_Arguments.push_back ( pArgument );
}
}
const CLuaArguments& CLuaArguments::operator = ( const CLuaArguments& Arguments )
{
// Clear our previous list if any
DeleteArguments ();
// Copy all the arguments
vector < CLuaArgument* > ::const_iterator iter = Arguments.m_Arguments.begin ();
for ( ; iter != Arguments.m_Arguments.end (); iter++ )
{
CLuaArgument* pArgument = new CLuaArgument ( **iter );
m_Arguments.push_back ( pArgument );
}
// Return the given reference allowing for chaining
return Arguments;
}
void CLuaArguments::ReadArguments ( lua_State* luaVM, unsigned int uiIndexBegin )
{
// Delete the previous arguments if any
DeleteArguments ();
// Start reading arguments until there are none left
while ( lua_type ( luaVM, uiIndexBegin ) != LUA_TNONE )
{
// Create an argument, let it read out the argument and add it to our vector
CLuaArgument* pArgument = new CLuaArgument ( luaVM, uiIndexBegin++ );
m_Arguments.push_back ( pArgument );
}
}
void CLuaArguments::PushArguments ( lua_State* luaVM ) const
{
// Push all our arguments
vector < CLuaArgument* > ::const_iterator iter = m_Arguments.begin ();
for ( ; iter != m_Arguments.end (); iter++ )
{
(*iter)->Push ( luaVM );
}
}
void CLuaArguments::PushArguments ( CLuaArguments& Arguments )
{
vector < CLuaArgument* > ::const_iterator iter = Arguments.IterBegin ();
for ( ; iter != Arguments.IterEnd (); iter++ )
{
CLuaArgument* pArgument = new CLuaArgument ( **iter );
m_Arguments.push_back ( pArgument );
}
}
bool CLuaArguments::Call ( lua_State* luaVM, const char* szFunction, int iResults, int iErrorFunc ) const
{
assert ( szFunction );
// Add the function name to the stack and get the event from the table
assert ( luaVM );
lua_pushstring ( luaVM, szFunction );
lua_gettable ( luaVM, LUA_GLOBALSINDEX );
// Push our arguments onto the stack
PushArguments ( luaVM );
int iret = lua_pcall ( luaVM, m_Arguments.size(), iResults, iErrorFunc );
if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM )
{
const char* szRes = lua_tostring( luaVM, -1 );
return false; // the function call failed
}
return true;
}
CLuaArgument* CLuaArguments::PushNil ( void )
{
CLuaArgument* pArgument = new CLuaArgument;
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushBoolean ( bool bBool )
{
CLuaArgument* pArgument = new CLuaArgument ( bBool );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushNumber ( double dNumber )
{
CLuaArgument* pArgument = new CLuaArgument ( dNumber );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushString ( const char* szString )
{
CLuaArgument* pArgument = new CLuaArgument ( szString );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushUserData ( void* pUserData )
{
CLuaArgument* pArgument = new CLuaArgument ( pUserData );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushFunction( lua_CFunction iFunction )
{
CLuaArgument* pArgument = new CLuaArgument ( iFunction );
m_Arguments.push_back ( pArgument );
return pArgument;
}
CLuaArgument* CLuaArguments::PushArgument ( const CLuaArgument & argument )
{
CLuaArgument* pArgument = new CLuaArgument (argument); // create a copy
m_Arguments.push_back ( pArgument );
return pArgument;
}
void CLuaArguments::DeleteArguments ( void )
{
// Delete each item
vector < CLuaArgument* > ::iterator iter = m_Arguments.begin ();
for ( ; iter != m_Arguments.end (); iter++ )
{
delete *iter;
}
// Clear the vector
m_Arguments.clear ();
}

View File

@ -1,65 +1,67 @@
/*********************************************************
*
* Multi Theft Auto: San Andreas - Deathmatch
*
* Squirrel 3, External lua add-on module
*
* Copyright © 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is © 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
* PROVIDED WITH THIS PACKAGE.
*
*********************************************************/
class CLuaArguments;
#ifndef __CLUAARGUMENTS_H
#define __CLUAARGUMENTS_H
extern "C"
{
#include <lua.h>
}
#include <vector>
#include "CLuaArgument.h"
using namespace std;
class CLuaArguments
{
public:
inline CLuaArguments ( void ) {};
CLuaArguments ( const CLuaArguments& Arguments );
inline ~CLuaArguments ( void ) { DeleteArguments (); };
const CLuaArguments& operator = ( const CLuaArguments& Arguments );
void ReadArguments ( lua_State* luaVM, unsigned int uiIndexBegin = 1 );
void PushArguments ( lua_State* luaVM ) const;
void PushArguments ( CLuaArguments& Arguments );
bool Call ( lua_State* luaVM, const char* szFunction, int iResults = 0, int iErrorFunc = 0 ) const;
CLuaArgument* PushNil ( void );
CLuaArgument* PushBoolean ( bool bBool );
CLuaArgument* PushNumber ( double dNumber );
CLuaArgument* PushString ( const char* szString );
CLuaArgument* PushUserData ( void* pUserData );
CLuaArgument* PushArgument ( const CLuaArgument& argument );
void DeleteArguments ( void );
inline unsigned int Count ( void ) const { return static_cast < unsigned int > ( m_Arguments.size () ); };
inline vector < CLuaArgument* > ::const_iterator IterBegin ( void ) { return m_Arguments.begin (); };
inline vector < CLuaArgument* > ::const_iterator IterEnd ( void ) { return m_Arguments.end (); };
private:
vector < CLuaArgument* > m_Arguments;
};
#endif
/*********************************************************
*
* Multi Theft Auto: San Andreas - Deathmatch
*
* Squirrel 3, External lua add-on module
*
* Copyright © 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is © 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
* PROVIDED WITH THIS PACKAGE.
*
*********************************************************/
class CLuaArguments;
#ifndef __CLUAARGUMENTS_H
#define __CLUAARGUMENTS_H
extern "C"
{
#include <lua.h>
}
#include <vector>
#include "CLuaArgument.h"
using namespace std;
class CLuaArguments
{
public:
inline CLuaArguments ( void ) {};
CLuaArguments ( const CLuaArguments& Arguments );
inline ~CLuaArguments ( void ) { DeleteArguments (); };
const CLuaArguments& operator = ( const CLuaArguments& Arguments );
void ReadArguments ( lua_State* luaVM, unsigned int uiIndexBegin = 1 );
void PushArguments ( lua_State* luaVM ) const;
void PushArguments ( CLuaArguments& Arguments );
bool Call ( lua_State* luaVM, const char* szFunction, int iResults = 0, int iErrorFunc = 0 ) const;
CLuaArgument* PushNil ( void );
CLuaArgument* PushBoolean ( bool bBool );
CLuaArgument* PushNumber ( double dNumber );
CLuaArgument* PushString ( const char* szString );
CLuaArgument* PushUserData ( void* pUserData );
CLuaArgument* PushFunction ( lua_CFunction iFunction );
CLuaArgument* PushArgument ( const CLuaArgument& argument );
void DeleteArguments ( void );
inline vector < CLuaArgument* > GetArguments ( void ) { return m_Arguments; }
inline unsigned int Count ( void ) const { return static_cast < unsigned int > ( m_Arguments.size () ); };
inline vector < CLuaArgument* > ::const_iterator IterBegin ( void ) { return m_Arguments.begin (); };
inline vector < CLuaArgument* > ::const_iterator IterEnd ( void ) { return m_Arguments.end (); };
private:
vector < CLuaArgument* > m_Arguments;
};
#endif