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.

This commit is contained in:
HighTechRedNeck 2025-01-23 16:53:42 -05:00
parent e243afacb6
commit b21eba680b
4 changed files with 79 additions and 3 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "game/game.hpp"
namespace fastfiles namespace fastfiles
{ {
void enum_assets(game::XAssetType type, const std::function<void(game::XAssetHeader)>& callback, bool include_override); void enum_assets(game::XAssetType type, const std::function<void(game::XAssetHeader)>& callback, bool include_override);

View File

@ -0,0 +1,58 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "fastfiles.hpp"
#include "game/game.hpp"
#include <utils/hook.hpp>
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<void>();
std::vector<game::WeaponCompleteDef*> 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)

View File

@ -0,0 +1,6 @@
#pragma once
namespace weapon
{
}

View File

@ -1955,6 +1955,16 @@ namespace game
weapInventoryType_t inventoryType; weapInventoryType_t inventoryType;
}; // Incomplete }; // Incomplete
struct WeaponCompleteDef
{
union
{
const char* szInternalName;
const char* name;
};
WeaponDef* weapDef;
};
struct RawFile struct RawFile
{ {
const char* name; const char* name;
@ -1998,9 +2008,9 @@ namespace game
menuDef_t *menu; menuDef_t *menu;
AnimationClass *animClass; AnimationClass *animClass;
LocalizeEntry *localize; LocalizeEntry *localize;
WeaponAttachment *attachment; WeaponAttachment *attachment;*/
WeaponCompleteDef *weapon; WeaponCompleteDef* weapon;
SndDriverGlobals *sndDriverGlobals; /*SndDriverGlobals* sndDriverGlobals;
FxEffectDef *fx; FxEffectDef *fx;
FxImpactTable *impactFx; FxImpactTable *impactFx;
SurfaceFxTable *surfaceFx;*/ SurfaceFxTable *surfaceFx;*/