From 29b0f8d4c9299a295c339e47b45ff24f4f40f539 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 7 Aug 2016 01:54:33 +0300 Subject: [PATCH] Add the possibility to have null entity instances intentionally. --- source/Core.cpp | 39 ++++++++++++++++++++++++++++++++++++ source/Core.hpp | 20 ++++++++++++++++++ source/Entity/Blip.cpp | 15 ++++++++++++++ source/Entity/Blip.hpp | 10 +++++++++ source/Entity/Checkpoint.cpp | 15 ++++++++++++++ source/Entity/Checkpoint.hpp | 10 +++++++++ source/Entity/Keybind.cpp | 15 ++++++++++++++ source/Entity/Keybind.hpp | 10 +++++++++ source/Entity/Object.cpp | 15 ++++++++++++++ source/Entity/Object.hpp | 10 +++++++++ source/Entity/Pickup.cpp | 15 ++++++++++++++ source/Entity/Pickup.hpp | 10 +++++++++ source/Entity/Player.cpp | 14 +++++++++++++ source/Entity/Player.hpp | 10 +++++++++ source/Entity/Vehicle.cpp | 16 ++++++++++++++- source/Entity/Vehicle.hpp | 10 +++++++++ 16 files changed, 233 insertions(+), 1 deletion(-) diff --git a/source/Core.cpp b/source/Core.cpp index d9a8935b..060ce5a5 100644 --- a/source/Core.cpp +++ b/source/Core.cpp @@ -254,6 +254,37 @@ bool Core::Initialize() NullTable() = Table(); NullObject() = Object(); NullFunction() = Function(); + // Create the null entity instances + { + // The Blip constructor validates the ID. So we must take this route + CBlip * nblip = new CBlip(0); + nblip->m_ID = -1; + m_NullBlip = Object(nblip); + // The Checkpoint constructor validates the ID. So we must take this route + CCheckpoint * ncheckpoint = new CCheckpoint(0); + ncheckpoint->m_ID = -1; + m_NullCheckpoint = Object(ncheckpoint); + // The Keybind constructor validates the ID. So we must take this route + CKeybind * nkeybind = new CKeybind(0); + nkeybind->m_ID = -1; + m_NullKeybind = Object(nkeybind); + // The Object constructor validates the ID. So we must take this route + CObject * nobject = new CObject(0); + nobject->m_ID = -1; + m_NullObject = Object(nobject); + // The Pickup constructor validates the ID. So we must take this route + CPickup * npickup = new CPickup(0); + npickup->m_ID = -1; + m_NullPickup = Object(npickup); + // The Player constructor validates the ID. So we must take this route + CPlayer * nplayer = new CPlayer(0); + nplayer->m_ID = -1; + m_NullPlayer = Object(nplayer); + // The Vehicle constructor validates the ID. So we must take this route + CVehicle * nvehicle = new CVehicle(0); + nvehicle->m_ID = -1; + m_NullVehicle = Object(nvehicle); + } LogDbg("Registering the standard libraries"); // Push the root table on the stack @@ -471,6 +502,14 @@ void Core::Terminate(bool shutdown) NullTable().Release(); NullObject().Release(); NullFunction().ReleaseGently(); + // Release null entity instances + m_NullBlip.Release(); + m_NullCheckpoint.Release(); + m_NullKeybind.Release(); + m_NullObject.Release(); + m_NullPickup.Release(); + m_NullPlayer.Release(); + m_NullVehicle.Release(); // Is there a VM to close? if (m_VM) { diff --git a/source/Core.hpp b/source/Core.hpp index 80ae5443..0b45ae28 100644 --- a/source/Core.hpp +++ b/source/Core.hpp @@ -534,6 +534,15 @@ private: bool m_Executed; // Whether the scripts were executed. bool m_Shutdown; // Whether the server currently shutting down. + // -------------------------------------------------------------------------------------------- + Object m_NullBlip; // Null Blips instance. + Object m_NullCheckpoint; // Null Checkpoints instance. + Object m_NullKeybind; // Null Key-instance pool. + Object m_NullObject; // Null Objects instance. + Object m_NullPickup; // Null Pickups instance. + Object m_NullPlayer; // Null Players instance. + Object m_NullVehicle; // Null Vehicles instance. + public: /* -------------------------------------------------------------------------------------------- @@ -808,6 +817,17 @@ public: const Players & GetPlayers() const { return m_Players; } const Vehicles & GetVehicles() const { return m_Vehicles; } + /* -------------------------------------------------------------------------------------------- + * Null instance retrievers. + */ + Object & GetNullBlip() { return m_NullBlip; } + Object & GetNullCheckpoint() { return m_NullCheckpoint; } + Object & GetNullKeybind() { return m_NullKeybind; } + Object & GetNullObject() { return m_NullObject; } + Object & GetNullPickup() { return m_NullPickup; } + Object & GetNullPlayer() { return m_NullPlayer; } + Object & GetNullVehicle() { return m_NullVehicle; } + /* -------------------------------------------------------------------------------------------- * Container cleaner. */ diff --git a/source/Entity/Blip.cpp b/source/Entity/Blip.cpp index e2f6cb9a..20e55e14 100644 --- a/source/Entity/Blip.cpp +++ b/source/Entity/Blip.cpp @@ -16,6 +16,19 @@ SQInteger CBlip::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CBlip::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullBlip().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CBlip::GetNull() +{ + return Core::Get().GetNullBlip(); +} + // ------------------------------------------------------------------------------------------------ CBlip::CBlip(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_BLIP_POOL)) @@ -350,6 +363,8 @@ void Register_CBlip(HSQUIRRELVM vm) (_SC("Create"), &Blip_Create) .StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Int32, const Color4 &, Int32, Int32, Object &) > (_SC("Create"), &Blip_Create) + // Raw Squirrel Methods + .SquirrelFunc(_SC("NullInst"), &CBlip::SqGetNull) ); } diff --git a/source/Entity/Blip.hpp b/source/Entity/Blip.hpp index 23e48a22..aa978cbe 100644 --- a/source/Entity/Blip.hpp +++ b/source/Entity/Blip.hpp @@ -95,6 +95,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */ diff --git a/source/Entity/Checkpoint.cpp b/source/Entity/Checkpoint.cpp index 16f66911..8753c9c5 100644 --- a/source/Entity/Checkpoint.cpp +++ b/source/Entity/Checkpoint.cpp @@ -29,6 +29,19 @@ SQInteger CCheckpoint::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CCheckpoint::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullCheckpoint().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CCheckpoint::GetNull() +{ + return Core::Get().GetNullCheckpoint(); +} + // ------------------------------------------------------------------------------------------------ CCheckpoint::CCheckpoint(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_CHECKPOINT_POOL)) @@ -546,6 +559,8 @@ void Register_CCheckpoint(HSQUIRRELVM vm) (_SC("Create"), &Checkpoint_Create) .StaticOverload< Object & (*)(Int32, bool, const Vector3 &, const Color4 &, Float32, Int32, Object &) > (_SC("Create"), &Checkpoint_Create) + // Raw Squirrel Methods + .SquirrelFunc(_SC("NullInst"), &CCheckpoint::SqGetNull) ); } diff --git a/source/Entity/Checkpoint.hpp b/source/Entity/Checkpoint.hpp index 58beb640..bdec6418 100644 --- a/source/Entity/Checkpoint.hpp +++ b/source/Entity/Checkpoint.hpp @@ -102,6 +102,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */ diff --git a/source/Entity/Keybind.cpp b/source/Entity/Keybind.cpp index f7b31db8..607c1c58 100644 --- a/source/Entity/Keybind.cpp +++ b/source/Entity/Keybind.cpp @@ -16,6 +16,19 @@ SQInteger CKeybind::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CKeybind::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullKeybind().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CKeybind::GetNull() +{ + return Core::Get().GetNullKeybind(); +} + // ------------------------------------------------------------------------------------------------ CKeybind::CKeybind(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_KEYBIND_POOL)) @@ -233,6 +246,8 @@ void Register_CKeybind(HSQUIRRELVM vm) (_SC("Create"), &Keybind_Create) .StaticOverload< Object & (*)(bool, Int32, Int32, Int32, Int32, Object &) > (_SC("Create"), &Keybind_Create) + // Raw Squirrel Methods + .SquirrelFunc(_SC("NullInst"), &CKeybind::SqGetNull) ); } diff --git a/source/Entity/Keybind.hpp b/source/Entity/Keybind.hpp index 5a694bd3..31a70a3e 100644 --- a/source/Entity/Keybind.hpp +++ b/source/Entity/Keybind.hpp @@ -95,6 +95,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */ diff --git a/source/Entity/Object.cpp b/source/Entity/Object.cpp index 4371b235..82775517 100644 --- a/source/Entity/Object.cpp +++ b/source/Entity/Object.cpp @@ -23,6 +23,19 @@ SQInteger CObject::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CObject::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullObject().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CObject::GetNull() +{ + return Core::Get().GetNullObject(); +} + // ------------------------------------------------------------------------------------------------ CObject::CObject(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_OBJECT_POOL)) @@ -927,6 +940,8 @@ void Register_CObject(HSQUIRRELVM vm) (_SC("Create"), &Object_Create) .StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Int32, Int32, Object &) > (_SC("Create"), &Object_Create) + // Raw Squirrel Methods + .SquirrelFunc(_SC("NullInst"), &CObject::SqGetNull) ); } diff --git a/source/Entity/Object.hpp b/source/Entity/Object.hpp index 592a93ae..a205772f 100644 --- a/source/Entity/Object.hpp +++ b/source/Entity/Object.hpp @@ -117,6 +117,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */ diff --git a/source/Entity/Pickup.cpp b/source/Entity/Pickup.cpp index e30cbe04..4b4c9c53 100644 --- a/source/Entity/Pickup.cpp +++ b/source/Entity/Pickup.cpp @@ -21,6 +21,19 @@ SQInteger CPickup::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CPickup::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullPickup().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CPickup::GetNull() +{ + return Core::Get().GetNullPickup(); +} + // ------------------------------------------------------------------------------------------------ CPickup::CPickup(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_PICKUP_POOL)) @@ -427,6 +440,8 @@ void Register_CPickup(HSQUIRRELVM vm) (_SC("Create"), &Pickup_Create) .StaticOverload< Object & (*)(Int32, Int32, Int32, const Vector3 &, Int32, bool, Int32, Object &) > (_SC("Create"), &Pickup_Create) + // Raw Squirrel Methods + .SquirrelFunc(_SC("NullInst"), &CPickup::SqGetNull) ); } diff --git a/source/Entity/Pickup.hpp b/source/Entity/Pickup.hpp index ea4d2dbb..bee1fa0a 100644 --- a/source/Entity/Pickup.hpp +++ b/source/Entity/Pickup.hpp @@ -98,6 +98,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */ diff --git a/source/Entity/Player.cpp b/source/Entity/Player.cpp index 89c7674a..80f5b9b8 100644 --- a/source/Entity/Player.cpp +++ b/source/Entity/Player.cpp @@ -37,6 +37,19 @@ SQInteger CPlayer::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CPlayer::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullPlayer().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CPlayer::GetNull() +{ + return Core::Get().GetNullPlayer(); +} + // ------------------------------------------------------------------------------------------------ CPlayer::CPlayer(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_PLAYER_POOL)) @@ -2357,6 +2370,7 @@ void Register_CPlayer(HSQUIRRELVM vm) .SquirrelFunc(_SC("AnnounceEx"), &CPlayer::AnnounceEx) .SquirrelFunc(_SC("Text"), &CPlayer::Announce) .SquirrelFunc(_SC("TextEx"), &CPlayer::AnnounceEx) + .SquirrelFunc(_SC("NullInst"), &CPlayer::SqGetNull) ); } diff --git a/source/Entity/Player.hpp b/source/Entity/Player.hpp index 9f5752c6..193b2b7d 100644 --- a/source/Entity/Player.hpp +++ b/source/Entity/Player.hpp @@ -174,6 +174,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */ diff --git a/source/Entity/Vehicle.cpp b/source/Entity/Vehicle.cpp index 4da70d2b..23a7c7b0 100644 --- a/source/Entity/Vehicle.cpp +++ b/source/Entity/Vehicle.cpp @@ -25,6 +25,19 @@ SQInteger CVehicle::Typename(HSQUIRRELVM vm) return 1; } +// ------------------------------------------------------------------------------------------------ +SQInteger CVehicle::SqGetNull(HSQUIRRELVM vm) +{ + sq_pushobject(vm, Core::Get().GetNullVehicle().GetObject()); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Object & CVehicle::GetNull() +{ + return Core::Get().GetNullVehicle(); +} + // ------------------------------------------------------------------------------------------------ CVehicle::CVehicle(Int32 id) : m_ID(VALID_ENTITYGETEX(id, SQMOD_VEHICLE_POOL)) @@ -1811,7 +1824,8 @@ void Register_CVehicle(HSQUIRRELVM vm) (_SC("Create"), &Vehicle_Create) .StaticOverload< Object & (*)(Int32, Int32, const Vector3 &, Float32, Int32, Int32, Int32, Object &) > (_SC("Create"), &Vehicle_Create) - + // Raw Squirrel Methods + .SquirrelFunc(_SC("NullInst"), &CVehicle::SqGetNull) ); } diff --git a/source/Entity/Vehicle.hpp b/source/Entity/Vehicle.hpp index b3507fb3..a787ebfe 100644 --- a/source/Entity/Vehicle.hpp +++ b/source/Entity/Vehicle.hpp @@ -113,6 +113,16 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static SQInteger SqGetNull(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated null entity instance. + */ + static Object & GetNull(); + /* -------------------------------------------------------------------------------------------- * Retrieve the identifier of the entity managed by this instance. */