From d668242901d67bc3bd27577ee59225adc395e6e6 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 13 Apr 2018 20:31:18 +0300 Subject: [PATCH] Implement an unrestricted pool of custom weapon names and fall back to it when looking for weapon names. --- source/Misc/Register.cpp | 2 ++ source/Misc/Weapon.cpp | 52 ++++++++++++++++++++++++++++++++++++---- source/Misc/Weapon.hpp | 15 ++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/source/Misc/Register.cpp b/source/Misc/Register.cpp index 49664063..30fb573b 100644 --- a/source/Misc/Register.cpp +++ b/source/Misc/Register.cpp @@ -141,6 +141,8 @@ void Register_Misc(HSQUIRRELVM vm) .Func(_SC("IsAutomobileValid"), &IsAutomobileValid) .Func(_SC("GetWeaponName"), &GetWeaponName) .FmtFunc(_SC("SetWeaponName"), &SetWeaponName) + .Func(_SC("GetCustomWeaponNamePoolSize"), &GetCustomWeaponNamePoolSize) + .Func(_SC("ClearCustomWeaponNamePool"), &ClearCustomWeaponNamePool) .FmtFunc(_SC("GetWeaponID"), &GetWeaponID) .Func(_SC("IsWeaponValid"), &IsWeaponValid) .Func(_SC("WeaponToModel"), &WeaponToModel) diff --git a/source/Misc/Weapon.cpp b/source/Misc/Weapon.cpp index cb153837..a10d0267 100644 --- a/source/Misc/Weapon.cpp +++ b/source/Misc/Weapon.cpp @@ -5,6 +5,7 @@ // ------------------------------------------------------------------------------------------------ #include #include +#include // ------------------------------------------------------------------------------------------------ namespace SqMod { @@ -48,20 +49,63 @@ static String CS_Weapon_Names[] = { /* 68 */ "", /* 69 */ "", /* 70 */ "Suicide", /* 71 */ "" }; +/// Fall back for custom weapon names. +static std::unordered_map CS_Custom_Weapon_Names{}; + +// ------------------------------------------------------------------------------------------------ +static inline bool IsCustomWeapon(Uint32 id) +{ + return (id > 70); +} // ------------------------------------------------------------------------------------------------ CCStr GetWeaponName(Uint32 id) { - return (id > 70) ? _SC("") : CS_Weapon_Names[id].c_str(); + // Can we consider this a custom weapon ID? + if (IsCustomWeapon(id)) + { + // Attempt to look for the specified identifier + auto it = CS_Custom_Weapon_Names.find(id); + // If we found anything than return the associated name + if ((it != CS_Custom_Weapon_Names.end())) + { + return it->second.c_str(); + } + } + else + { + return CS_Weapon_Names[id].c_str(); // Use the standard weapon name + } + // Fall back to an empty name + return _SC(""); } // ------------------------------------------------------------------------------------------------ void SetWeaponName(Uint32 id, const StackStrF & name) { - if (id <= 70) - { + // Can we consider this a custom weapon ID? + if (IsCustomWeapon(id)) + { + // Attempt to insert or update the name into the custom weapon table + CS_Custom_Weapon_Names[id] = String(name.mPtr, name.mLen); + } + else + { + // Attempt to insert or update the name into the standard weapon table CS_Weapon_Names[id].assign(name.mPtr); - } + } +} + +// ------------------------------------------------------------------------------------------------ +Uint32 GetCustomWeaponNamePoolSize() +{ + return static_cast< Uint32 >(CS_Custom_Weapon_Names.size()); +} + +// ------------------------------------------------------------------------------------------------ +void ClearCustomWeaponNamePool() +{ + CS_Custom_Weapon_Names.clear(); } // ------------------------------------------------------------------------------------------------ diff --git a/source/Misc/Weapon.hpp b/source/Misc/Weapon.hpp index a1be3d87..97457131 100644 --- a/source/Misc/Weapon.hpp +++ b/source/Misc/Weapon.hpp @@ -17,6 +17,21 @@ CSStr GetWeaponName(Uint32 id); */ void SetWeaponName(Uint32 id, const StackStrF & name); +/* ------------------------------------------------------------------------------------------------ + * Retrieve the total number of identifiers in the pool of custom weapon names. +*/ +Uint32 GetCustomWeaponNamePoolSize(); + +/* ------------------------------------------------------------------------------------------------ + * Clear all identifiersand associated names from the pool of custom weapon names. +*/ +void ClearCustomWeaponNamePool(); + +/* ------------------------------------------------------------------------------------------------ + * Modify the name associated with a weapon identifier. +*/ +void SetWeaponName(Uint32 id, const StackStrF & name); + /* ------------------------------------------------------------------------------------------------ * Convert a weapon name to a weapon identifier. */