1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-19 12:07:13 +01:00

Improve the client script data event. Also fix a crash because the buffer type was not registered.

This commit is contained in:
Sandu Liviu Catalin 2016-06-20 18:02:44 +03:00
parent 29abf2e9c0
commit ea70a21437
3 changed files with 29 additions and 57 deletions

View File

@ -7,9 +7,6 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
extern Object CreateBufferWrapperMove(Buffer && b);
// ------------------------------------------------------------------------------------------------
void Core::EmitCustomEvent(Int32 group, Int32 header, Object & payload)
{
@ -1064,19 +1061,42 @@ void Core::EmitVehicleUpdate(Int32 vehicle_id, vcmpVehicleUpdate update_type)
// ------------------------------------------------------------------------------------------------
void Core::EmitClientScriptData(Int32 player_id, const uint8_t * data, size_t size)
{
PlayerInst & _player = m_Players.at(player_id);
// Don't even bother if there's no one listening
if (_player.mOnClientScriptData.IsNull() && mOnClientScriptData.IsNull())
{
return;
}
// Allocate a buffer with the received size
Buffer b(size);
// Replicate the data to the allocated buffer
b.Write(0, reinterpret_cast< Buffer::ConstPtr >(data), size);
// Wrap the buffer into a script object
const Object o = CreateBufferWrapperMove(std::move(b));
// Wrap the buffer into a script object
// Prepare an object for the obtained buffer
Object o;
// Attempt to create the requested buffer
try
{
// Remember the current stack size
const StackGuard sg;
// Create a protected instance of a buffer wrapper
AutoDelete< BufferWrapper > ad(new BufferWrapper(std::move(b)));
// Transform the pointer into a script object
PushVar< BufferWrapper * >(DefaultVM::Get(), ad.Get());
// The script took over the instance now
ad.Release();
// Get the object from the stack and store it
o = Var< Object >(DefaultVM::Get(), -1).value;
}
catch (const std::exception & e)
{
STHROWF("%s", e.what()); // Re-package
}
// Make sure the buffer cannot be null at this point
if (o.IsNull())
{
STHROWF("Unable to transform script data into buffer");
}
// Forward the event call
PlayerInst & _player = m_Players.at(player_id);
Emit(_player.mOnClientScriptData, o, size);
Emit(mOnClientScriptData, _player.mObj, o, size);
}

View File

@ -11,56 +11,6 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
extern void Register_BufferInterpreter(Table & bns);
// ------------------------------------------------------------------------------------------------
Object CreateBufferWrapperCopy(const Buffer & b)
{
// Attempt to create the requested buffer
try
{
// Remember the current stack size
const StackGuard sg;
// Transform the pointer into a script object
PushVar< const BufferWrapper & >(DefaultVM::Get(), BufferWrapper(b));
// Get the object from the stack and return it
return Var< Object >(DefaultVM::Get(), -1).value;
}
catch (const Sqrat::Exception & e)
{
throw e; // Re-throw
}
catch (const std::exception & e)
{
STHROWF("%s", e.what()); // Re-package
}
// Shouldn't really reach this point
return NullObject();
}
// ------------------------------------------------------------------------------------------------
Object CreateBufferWrapperMove(Buffer && b)
{
// Attempt to create the requested buffer
try
{
// Remember the current stack size
const StackGuard sg;
// Transform the pointer into a script object
PushVar< const BufferWrapper & >(DefaultVM::Get(), BufferWrapper(std::move(b)));
// Get the object from the stack and return it
return Var< Object >(DefaultVM::Get(), -1).value;
}
catch (const Sqrat::Exception & e)
{
throw e; // Re-throw
}
catch (const std::exception & e)
{
STHROWF("%s", e.what()); // Re-package
}
// Shouldn't really reach this point
return NullObject();
}
// ------------------------------------------------------------------------------------------------
Object BufferWrapper::Create(SzType n)
{

View File

@ -40,6 +40,7 @@ extern void Register_IO(HSQUIRRELVM vm);
extern void Register_Numeric(HSQUIRRELVM vm);
extern void Register_String(HSQUIRRELVM vm);
extern void Register_System(HSQUIRRELVM vm);
extern void Register_Utils(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_Constants(HSQUIRRELVM vm);
@ -80,6 +81,7 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_Numeric(vm);
Register_String(vm);
Register_System(vm);
Register_Utils(vm);
Register_Constants(vm);
Register_Log(vm);