diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index 10fdeb30..d1ecf089 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -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); } diff --git a/source/Library/Utils/BufferWrapper.cpp b/source/Library/Utils/BufferWrapper.cpp index 4ba7b954..5c4b607b 100644 --- a/source/Library/Utils/BufferWrapper.cpp +++ b/source/Library/Utils/BufferWrapper.cpp @@ -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) { diff --git a/source/Register.cpp b/source/Register.cpp index 1b40c402..f31dd3db 100644 --- a/source/Register.cpp +++ b/source/Register.cpp @@ -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);