Fixed GetVehicleColor crash (Lua fails if use LUA_MULTRET or more than 10 results in lua_pcall)

This commit is contained in:
Kernell 2015-12-28 22:35:35 +03:00
parent 2007d6d5ab
commit d8acda6a8e
5 changed files with 40 additions and 21 deletions

View File

@ -93,7 +93,9 @@ MonoObject* CMonoFunctions::Vehicle::GetColor( DWORD pUserData )
for( int i = 0; i < 4; i++ ) for( int i = 0; i < 4; i++ )
{ {
args[ i ] = pMTALib->Color->New( pVehicleColor.GetRGBColor( i ) ); MonoObject* pObject = pMTALib->Color->New( pVehicleColor.GetRGBColor( i ) );
args[ i ] = pObject;
} }
return pMTALib->GetClass( "VehicleColor" )->New( args, 4 ); return pMTALib->GetClass( "VehicleColor" )->New( args, 4 );

View File

@ -52,6 +52,10 @@ extern "C"
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=nullptr; } } #define SAFE_DELETE(p) { if(p) { delete (p); (p)=nullptr; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=nullptr; } } #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=nullptr; } }
#define ASSERT(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
#define ASSERT_MSG(_Expression, _Message) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(_Message), _CRT_WIDE(__FILE__), __LINE__), 0) )
#include "extra/Vector2.h" #include "extra/Vector2.h"
#include "extra/Vector3.h" #include "extra/Vector3.h"

View File

@ -26,7 +26,6 @@ extern "C"
#include <lua.h> #include <lua.h>
} }
#include "CReferenceCounter.h"
#include "CLuaArguments.h" #include "CLuaArguments.h"
typedef vector< CLuaArgument > CLuaArgumentsVector; typedef vector< CLuaArgument > CLuaArgumentsVector;

View File

@ -18,7 +18,6 @@
#include "../StdInc.h" #include "../StdInc.h"
#include "CLuaArguments.h" #include "CLuaArguments.h"
#include <assert.h>
CLuaArguments::CLuaArguments( const CLuaArguments& Arguments ) CLuaArguments::CLuaArguments( const CLuaArguments& Arguments )
{ {
@ -76,15 +75,15 @@ void CLuaArguments::PushArguments( CLuaArguments& Arguments )
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 );
assert( luaVM ); ASSERT( luaVM );
lua_pushstring( luaVM, szFunction ); lua_pushstring( luaVM, szFunction );
lua_gettable( luaVM, LUA_GLOBALSINDEX ); lua_gettable( luaVM, LUA_GLOBALSINDEX );
this->PushArguments( luaVM ); this->PushArguments( luaVM );
int iret = lua_pcall( luaVM, m_Arguments.size(), iResults, iErrorFunc ); int iret = lua_pcall( luaVM, this->m_Arguments.size(), iResults, iErrorFunc );
return iret != LUA_ERRRUN && iret != LUA_ERRMEM; return iret != LUA_ERRRUN && iret != LUA_ERRMEM;
} }

View File

@ -3058,26 +3058,41 @@ bool CLuaFunctionDefinitions::GetVehicleColor( lua_State* pLuaVM, PVOID pUserDat
pLuaArguments.PushUserData( pUserData ); pLuaArguments.PushUserData( pUserData );
pLuaArguments.PushBoolean( true ); pLuaArguments.PushBoolean( true );
if( pLuaArguments.Call( pLuaVM, "getVehicleColor", 12 ) ) int iLuaTop = lua_gettop( pLuaVM );
if( pLuaArguments.Call( pLuaVM, "getVehicleColor", 9 ) )
{ {
SColor SColor color1, color2, color3, color4;
color1, color2, color3, color4;
color1.R = CLuaArgument( pLuaVM, -12 ).GetNumber< unsigned char >(); int iResults = lua_gettop( pLuaVM ) - iLuaTop;
color1.G = CLuaArgument( pLuaVM, -11 ).GetNumber< unsigned char >();
color1.B = CLuaArgument( pLuaVM, -10 ).GetNumber< unsigned char >();
color2.R = CLuaArgument( pLuaVM, -9 ).GetNumber< unsigned char >(); for( int iArgument = -iResults, i = 1; iArgument < 0; iArgument++, i++ )
color2.G = CLuaArgument( pLuaVM, -8 ).GetNumber< unsigned char >(); {
color2.B = CLuaArgument( pLuaVM, -7 ).GetNumber< unsigned char >(); ASSERT( lua_type( pLuaVM, iArgument ) == LUA_TNUMBER );
color3.R = CLuaArgument( pLuaVM, -6 ).GetNumber< unsigned char >(); unsigned char ucValue = static_cast< unsigned char >( lua_tonumber( pLuaVM, iArgument ) );
color3.G = CLuaArgument( pLuaVM, -5 ).GetNumber< unsigned char >();
color3.B = CLuaArgument( pLuaVM, -4 ).GetNumber< unsigned char >();
color4.R = CLuaArgument( pLuaVM, -3 ).GetNumber< unsigned char >(); switch( i )
color4.G = CLuaArgument( pLuaVM, -2 ).GetNumber< unsigned char >(); {
color4.B = CLuaArgument( pLuaVM, -1 ).GetNumber< unsigned char >(); case 1: color1.R = ucValue; break;
case 2: color1.G = ucValue; break;
case 3: color1.B = ucValue; break;
case 4: color2.R = ucValue; break;
case 5: color2.G = ucValue; break;
case 6: color2.B = ucValue; break;
case 7: color3.R = ucValue; break;
case 8: color3.G = ucValue; break;
case 9: color3.B = ucValue; break;
case 10: color4.R = ucValue; break;
case 11: color4.G = ucValue; break;
case 12: color4.B = ucValue; break;
}
}
color1.A = color2.A = color3.A = color4.A = 255;
color.SetRGBColors( color1, color2, color3, color4 ); color.SetRGBColors( color1, color2, color3, color4 );