From b21eba680b1930e7a042417427d89bd8d8bf397f Mon Sep 17 00:00:00 2001 From: HighTechRedNeck Date: Thu, 23 Jan 2025 16:53:42 -0500 Subject: [PATCH] Adds precaching all weapons loaded in zones. This is the only way I have been able to get custom weapons to load other than replacing a base weapon. --- src/client/component/fastfiles.hpp | 2 ++ src/client/component/weapon.cpp | 58 ++++++++++++++++++++++++++++++ src/client/component/weapon.hpp | 6 ++++ src/client/game/structs.hpp | 16 +++++++-- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/client/component/weapon.cpp create mode 100644 src/client/component/weapon.hpp diff --git a/src/client/component/fastfiles.hpp b/src/client/component/fastfiles.hpp index 5bd092d..37ea106 100644 --- a/src/client/component/fastfiles.hpp +++ b/src/client/component/fastfiles.hpp @@ -1,5 +1,7 @@ #pragma once +#include "game/game.hpp" + namespace fastfiles { void enum_assets(game::XAssetType type, const std::function& callback, bool include_override); diff --git a/src/client/component/weapon.cpp b/src/client/component/weapon.cpp new file mode 100644 index 0000000..548a8cb --- /dev/null +++ b/src/client/component/weapon.cpp @@ -0,0 +1,58 @@ +#include +#include "loader/component_loader.hpp" + +#include "fastfiles.hpp" + +#include "game/game.hpp" + +#include + +namespace weapon +{ + namespace + { + utils::hook::detour g_setup_level_weapon_def_hook; + void g_setup_level_weapon_def_stub() + { + // precache level weapons first + g_setup_level_weapon_def_hook.invoke(); + + std::vector weapons; + + // find all weapons in asset pools + fastfiles::enum_assets(game::ASSET_TYPE_WEAPON, [&weapons](game::XAssetHeader header) + { + weapons.push_back(header.weapon); + }, false); + + // sort weapons + std::sort(weapons.begin(), weapons.end(), [](game::WeaponCompleteDef* weapon1, game::WeaponCompleteDef* weapon2) + { + return std::string_view(weapon1->name) < + std::string_view(weapon2->name); + }); + + // precache items + for (std::size_t i = 0; i < weapons.size(); i++) + { + //console::debug("precaching weapon \"%s\"\n", weapons[i]->name); + game::G_GetWeaponForName(weapons[i]->name); + } + } + } + + class component final : public component_interface + { + public: + void post_unpack() override + { + if (!game::environment::is_sp()) + { + // precache all weapons that are loaded in zones + g_setup_level_weapon_def_hook.create(0x1403DA910, g_setup_level_weapon_def_stub); + } + } + }; +} + +REGISTER_COMPONENT(weapon::component) \ No newline at end of file diff --git a/src/client/component/weapon.hpp b/src/client/component/weapon.hpp new file mode 100644 index 0000000..38f682f --- /dev/null +++ b/src/client/component/weapon.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace weapon +{ + +} \ No newline at end of file diff --git a/src/client/game/structs.hpp b/src/client/game/structs.hpp index 5f80604..3326176 100644 --- a/src/client/game/structs.hpp +++ b/src/client/game/structs.hpp @@ -1955,6 +1955,16 @@ namespace game weapInventoryType_t inventoryType; }; // Incomplete + struct WeaponCompleteDef + { + union + { + const char* szInternalName; + const char* name; + }; + WeaponDef* weapDef; + }; + struct RawFile { const char* name; @@ -1998,9 +2008,9 @@ namespace game menuDef_t *menu; AnimationClass *animClass; LocalizeEntry *localize; - WeaponAttachment *attachment; - WeaponCompleteDef *weapon; - SndDriverGlobals *sndDriverGlobals; + WeaponAttachment *attachment;*/ + WeaponCompleteDef* weapon; + /*SndDriverGlobals* sndDriverGlobals; FxEffectDef *fx; FxImpactTable *impactFx; SurfaceFxTable *surfaceFx;*/