From 74237747c1886314baea00a082f48b789f984616 Mon Sep 17 00:00:00 2001 From: Kernell Date: Sun, 29 Nov 2015 18:47:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20Lua=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mta-mono/src/extra/CLuaArgument.cpp | 34 +-- mta-mono/src/extra/CLuaArguments.cpp | 352 ++++++++++++++------------- mta-mono/src/extra/CLuaArguments.h | 132 +++++----- 3 files changed, 264 insertions(+), 254 deletions(-) diff --git a/mta-mono/src/extra/CLuaArgument.cpp b/mta-mono/src/extra/CLuaArgument.cpp index 4fcd05e..b083451 100644 --- a/mta-mono/src/extra/CLuaArgument.cpp +++ b/mta-mono/src/extra/CLuaArgument.cpp @@ -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; diff --git a/mta-mono/src/extra/CLuaArguments.cpp b/mta-mono/src/extra/CLuaArguments.cpp index da18e83..e919ba4 100644 --- a/mta-mono/src/extra/CLuaArguments.cpp +++ b/mta-mono/src/extra/CLuaArguments.cpp @@ -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 - -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 + +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 (); } \ No newline at end of file diff --git a/mta-mono/src/extra/CLuaArguments.h b/mta-mono/src/extra/CLuaArguments.h index 5f9ddc4..b63aec0 100644 --- a/mta-mono/src/extra/CLuaArguments.h +++ b/mta-mono/src/extra/CLuaArguments.h @@ -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 -} - -#include -#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 +} + +#include +#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