1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Allow current client data buffer to be retrieved anywhere during the callback.

This commit is contained in:
Sandu Liviu Catalin 2021-07-23 17:31:05 +03:00
parent 0641de7920
commit 7b7f974e42
3 changed files with 33 additions and 3 deletions

View File

@ -181,6 +181,7 @@ Core::Core() noexcept
, m_LockUnloadSignal(false) , m_LockUnloadSignal(false)
, m_EmptyInit(false) , m_EmptyInit(false)
, m_Verbosity(1) , m_Verbosity(1)
, m_ClientData()
, m_NullBlip() , m_NullBlip()
, m_NullCheckpoint() , m_NullCheckpoint()
, m_NullKeyBind() , m_NullKeyBind()
@ -560,6 +561,8 @@ void Core::Terminate(bool shutdown)
NullObject().Release(); NullObject().Release();
NullLightObj().Release(); NullLightObj().Release();
NullFunction().Release(); NullFunction().Release();
// Release client data buffer, if any
m_ClientData.Release();
// Release null entity instances // Release null entity instances
m_NullBlip.Release(); m_NullBlip.Release();
m_NullCheckpoint.Release(); m_NullCheckpoint.Release();
@ -2846,6 +2849,12 @@ static bool SqDelVehicle(int32_t id, int32_t header, LightObj & payload)
return Core::Get().DelVehicle(id, header, payload); return Core::Get().DelVehicle(id, header, payload);
} }
// ------------------------------------------------------------------------------------------------
static LightObj & SqGetClientDataBuffer()
{
return Core::Get().GetClientDataBuffer();
}
// ================================================================================================ // ================================================================================================
void Register_Core(HSQUIRRELVM vm) void Register_Core(HSQUIRRELVM vm)
{ {
@ -2894,6 +2903,7 @@ void Register_Core(HSQUIRRELVM vm)
.Func(_SC("DestroyObject"), &SqDelObject) .Func(_SC("DestroyObject"), &SqDelObject)
.Func(_SC("DestroyPickup"), &SqDelPickup) .Func(_SC("DestroyPickup"), &SqDelPickup)
.Func(_SC("DestroyVehicle"), &SqDelVehicle) .Func(_SC("DestroyVehicle"), &SqDelVehicle)
.Func(_SC("ClientDataBuffer"), &SqGetClientDataBuffer)
.Func(_SC("OnPreLoad"), &SqGetPreLoadEvent) .Func(_SC("OnPreLoad"), &SqGetPreLoadEvent)
.Func(_SC("OnPostLoad"), &SqGetPostLoadEvent) .Func(_SC("OnPostLoad"), &SqGetPostLoadEvent)
.Func(_SC("OnUnload"), &SqGetUnloadEvent) .Func(_SC("OnUnload"), &SqGetUnloadEvent)

View File

@ -104,6 +104,9 @@ private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
int32_t m_Verbosity; // Restrict the amount of outputted information. int32_t m_Verbosity; // Restrict the amount of outputted information.
// --------------------------------------------------------------------------------------------
LightObj m_ClientData; // Currently processed client data buffer.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
LightObj m_NullBlip; // Null Blips instance. LightObj m_NullBlip; // Null Blips instance.
LightObj m_NullCheckpoint; // Null Checkpoints instance. LightObj m_NullCheckpoint; // Null Checkpoints instance.
@ -383,6 +386,14 @@ public:
return (!m_IncomingNameBuffer) ? _SC("") : m_IncomingNameBuffer; return (!m_IncomingNameBuffer) ? _SC("") : m_IncomingNameBuffer;
} }
/* --------------------------------------------------------------------------------------------
* Retrieve the current client data buffer, if any.
*/
SQMOD_NODISCARD LightObj & GetClientDataBuffer()
{
return m_ClientData;
}
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieves a line of code from a certain source. * Retrieves a line of code from a certain source.
*/ */

View File

@ -2213,18 +2213,25 @@ void Core::EmitClientScriptData(int32_t player_id, const uint8_t * data, size_t
{ {
SQMOD_CO_EV_TRACEBACK("[TRACE<] Core::ClientScriptData(%d, [byte stream], %" PRINT_SZ_FMT ")", player_id, size) SQMOD_CO_EV_TRACEBACK("[TRACE<] Core::ClientScriptData(%d, [byte stream], %" PRINT_SZ_FMT ")", player_id, size)
PlayerInst & _player = m_Players.at(static_cast< size_t >(player_id)); PlayerInst & _player = m_Players.at(static_cast< size_t >(player_id));
#ifndef VCMP_ENABLE_OFFICIAL
// Don't even bother if there's no one listening // Don't even bother if there's no one listening
if (!(_player.mOnClientScriptData.first->IsEmpty()) || !(mOnClientScriptData.first->IsEmpty())) if (!(_player.mOnClientScriptData.first->IsEmpty()) || !(mOnClientScriptData.first->IsEmpty()))
{ {
#endif
// Allocate a buffer with the received size // Allocate a buffer with the received size
Buffer b(static_cast< Buffer::SzType >(size)); Buffer b(static_cast< Buffer::SzType >(size));
// Replicate the data to the allocated buffer // Replicate the data to the allocated buffer
b.Write(0, reinterpret_cast< Buffer::ConstPtr >(data), static_cast< Buffer::SzType >(size)); b.Write(0, reinterpret_cast< Buffer::ConstPtr >(data), static_cast< Buffer::SzType >(size));
// Prepare an object for the obtained buffer // Prepare an object for the obtained buffer
LightObj o(SqTypeIdentity< SqBuffer >{}, m_VM, std::move(b)); m_ClientData = LightObj(SqTypeIdentity< SqBuffer >{}, m_VM, std::move(b));
#ifdef VCMP_ENABLE_OFFICIAL
// Don't even bother if there's no one listening
if (!(_player.mOnClientScriptData.first->IsEmpty()) || !(mOnClientScriptData.first->IsEmpty()))
{
#endif
// Forward the event call // Forward the event call
(*_player.mOnClientScriptData.first)(o, size); (*_player.mOnClientScriptData.first)(m_ClientData, size);
(*mOnClientScriptData.first)(_player.mObj, o, size); (*mOnClientScriptData.first)(_player.mObj, m_ClientData, size);
} }
SQMOD_CO_EV_TRACEBACK("[TRACE>] Core::ClientScriptData") SQMOD_CO_EV_TRACEBACK("[TRACE>] Core::ClientScriptData")
#ifdef VCMP_ENABLE_OFFICIAL #ifdef VCMP_ENABLE_OFFICIAL
@ -2234,6 +2241,8 @@ void Core::EmitClientScriptData(int32_t player_id, const uint8_t * data, size_t
ExecuteLegacyEvent(m_VM, _SC("onClientScriptData"), _player.mLgObj); ExecuteLegacyEvent(m_VM, _SC("onClientScriptData"), _player.mLgObj);
} }
#endif #endif
// Discard the buffer instance, if any
m_ClientData.Release();
} }
#undef NULL_SQOBJ_ // don't need this anymore #undef NULL_SQOBJ_ // don't need this anymore