Добавлена поддержка 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(); m_pArray = new CLuaArguments();
lua_pushnil( luaVM ); lua_pushnil( luaVM );
uiArgument--; uiArgument--;
while( lua_next( luaVM, uiArgument ) != 0 ) while( lua_next( luaVM, uiArgument ) != 0 )
{ {
CLuaArgument pKey( luaVM, -2 ); CLuaArgument pKey( luaVM, -2 );
CLuaArgument pValue( luaVM, -1 ); CLuaArgument pValue( luaVM, -1 );
if( pKey.GetType() == LUA_TSTRING ) if( pKey.GetType() == LUA_TSTRING )
{ {
m_pTable[ string( pKey.GetString() ) ] = &pValue; m_pTable[ string( pKey.GetString() ) ] = &pValue;
} }
m_pArray->PushArgument( pValue ); m_pArray->PushArgument( pValue );
lua_pop( luaVM, 1 ); lua_pop( luaVM, 1 );
} }
break; break;

View File

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