From 67262e9ed7892c7ec1d35e2219e78247e1e811ff Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Sat, 14 Nov 2026 18:28:39 -0800 Subject: [PATCH] gameinterop: report precache names the loaded world lacks Precache adds names to the next session manifest, but a host had no way to tell whether the world it runs already holds them. A host that starts another mod on the map already loaded could not know whether that mod's precache needs a map reload, so it had to either reload on every switch or risk a world without the mod's models. PrecacheList now holds the hero and resource names, each once, and records whether a name arrived after the last manifest build. The manifest builder marks it built after registering every name, and EngineHost::PrecachePending reports it. Names stay for the life of the process, so once a world has loaded a name, asking for it again never makes the host reload. Cost: Add was already a linear scan of the held names per call; it now also sets a flag. PrecachePending reads one bool. Nothing runs per frame. Signed-off-by: Christian Stewart --- CMakeLists.txt | 3 ++ include/modlock/engine_host.h | 6 +++ include/modlock/gameinterop/game_rules.h | 31 +++++++++++++ src/engine_host.cc | 5 +++ src/gameinterop/game_rules.cc | 56 ++++++++++++++++-------- tests/precache_list_test.cc | 41 +++++++++++++++++ 6 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 tests/precache_list_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ef900b1..ae7a6fef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -558,6 +558,9 @@ if(BUILD_TESTING) target_include_directories(entity_rules_test PRIVATE src) target_link_libraries(entity_rules_test PRIVATE modlock::sdk gtest_main) add_test(NAME entity_rules_test COMMAND entity_rules_test) + add_executable(precache_list_test tests/precache_list_test.cc) + target_link_libraries(precache_list_test PRIVATE modlock::sdk gtest_main) + add_test(NAME precache_list_test COMMAND precache_list_test) add_executable(console_variables_test tests/console_variables_test.cc) target_link_libraries(console_variables_test PRIVATE modlock::sdk gtest_main) add_test(NAME console_variables_test COMMAND console_variables_test) diff --git a/include/modlock/engine_host.h b/include/modlock/engine_host.h index a510bd74..08fbe84a 100644 --- a/include/modlock/engine_host.h +++ b/include/modlock/engine_host.h @@ -91,6 +91,12 @@ class MODLOCK_API EngineHost { // Resource selection belongs to the plugin; the framework deduplicates them. std::expected Precache(std::vector heroes, std::vector resources); + // PrecachePending reports whether Precache named a hero or resource that + // the loaded world lacks: no session manifest has registered it since. + // Such a name reaches the game only when a map loads, so a host that needs + // it, such as one starting another mod, reloads the map; once registered, a + // name stays in every later manifest and never makes this true again. + [[nodiscard]] bool PrecachePending() const; // AdvertiseAddons offers comma-separated content addon names to every // connecting client, once per host, before Run starts the listen server. diff --git a/include/modlock/gameinterop/game_rules.h b/include/modlock/gameinterop/game_rules.h index 8955365e..88b2c720 100644 --- a/include/modlock/gameinterop/game_rules.h +++ b/include/modlock/gameinterop/game_rules.h @@ -71,6 +71,34 @@ class MODLOCK_API GamePause { size_t paused_offset_ = 0; }; +// PrecacheList holds the hero and resource names every session manifest +// build registers, each once, in the order first added. Names stay for the +// life of the list, so a world that registered a name keeps it in every later +// manifest. The list also tracks whether a name arrived after the last build: +// such a name reaches the game only with the next map load. It is not +// synchronized; the game thread both adds and builds. +class MODLOCK_API PrecacheList { + public: + // Add appends each name the list lacks and reports whether it added any. + bool Add(std::vector heroes, std::vector resources); + + // Built records that a manifest build registered every name held. + void Built() { pending_ = false; } + + // Pending reports whether Add appended a name since the last Built, so the + // loaded world lacks it until the map loads again. + [[nodiscard]] bool Pending() const { return pending_; } + + [[nodiscard]] const std::vector& heroes() const { return heroes_; } + + [[nodiscard]] const std::vector& resources() const { return resources_; } + + private: + std::vector heroes_; + std::vector resources_; + bool pending_ = false; +}; + // GameRulesHooks interposes CCitadelGameRules::BuildGameSessionManifest // through a native detour. The engine builds one session // manifest per map activation; the hook lets the original build it, then @@ -101,6 +129,9 @@ class MODLOCK_API GameRulesHooks { // AddPrecache adds plugin-supplied resources before the next manifest build. void AddPrecache(std::vector heroes, std::vector resources); + // PrecachePending reports whether AddPrecache named a hero or resource that + // no manifest build has registered yet; the next map load registers it. + [[nodiscard]] bool PrecachePending() const; GameRulesHooks(GameRulesHooks&&) noexcept; GameRulesHooks& operator=(GameRulesHooks&&) noexcept; diff --git a/src/engine_host.cc b/src/engine_host.cc index 8fd19559..0fa17bf5 100644 --- a/src/engine_host.cc +++ b/src/engine_host.cc @@ -481,6 +481,11 @@ std::expected EngineHost::Stamina() return &*impl_->stamina; } +bool EngineHost::PrecachePending() const { + // Without the hooks nothing was asked for, so nothing waits. + return impl_->rules && impl_->rules->PrecachePending(); +} + std::expected EngineHost::Precache(std::vector heroes, std::vector resources) { // Add to the precache lists once the hooks exist. diff --git a/src/gameinterop/game_rules.cc b/src/gameinterop/game_rules.cc index c6ca1246..6212b21a 100644 --- a/src/gameinterop/game_rules.cc +++ b/src/gameinterop/game_rules.cc @@ -24,8 +24,7 @@ using HeroPrecacheFn = void(__fastcall*)(void* global_set, const char* hero_name // Thunk state: one hook may exist at a time. The names and decoded targets // are set once at install; the manifest context is per call. -std::vector g_hero_names; -std::vector g_resource_names; +PrecacheList g_precache; void* g_precache_global = nullptr; HeroPrecacheFn g_precache_fn = nullptr; GameRulesHooks::AddResourceFn g_add_resource = nullptr; @@ -41,7 +40,7 @@ struct ResourceContext { void PrecacheIntoCurrentManifest(void** original_args) { // Nothing is added without heroes or the precache pair. - if (g_hero_names.empty()) { + if (g_precache.heroes().empty()) { return; } if (g_precache_fn == nullptr || g_precache_global == nullptr) { @@ -53,7 +52,7 @@ void PrecacheIntoCurrentManifest(void** original_args) { // Precache each hero into the session manifest. ResourceContext ctx{0, 0, original_args != nullptr ? *original_args : nullptr}; int count = 0; - for (const auto& name : g_hero_names) { + for (const auto& name : g_precache.heroes()) { // An unknown name fails inside the game's own lookup without touching // the manifest; the log line keeps the outcome visible for the session. g_precache_fn(g_precache_global, name.c_str(), &ctx); @@ -68,7 +67,8 @@ __int64 __fastcall ManifestBuildThunk(void* thisptr, void** original_args) { const __int64 result = g_hook.call<__int64>(thisptr, original_args); PrecacheIntoCurrentManifest(original_args); void* manifest = original_args ? *original_args : nullptr; - GameRulesHooks::RegisterResources(manifest, g_add_resource, g_resource_names); + GameRulesHooks::RegisterResources(manifest, g_add_resource, g_precache.resources()); + g_precache.Built(); return result; } @@ -81,8 +81,7 @@ struct GameRulesHooks::Impl { #if defined(_WIN32) // Remove the hook and forget what it registered. g_hook.reset(); - g_hero_names.clear(); - g_resource_names.clear(); + g_precache = {}; g_precache_global = nullptr; g_precache_fn = nullptr; g_add_resource = nullptr; @@ -107,20 +106,39 @@ bool GameRulesHooks::RegisterResources(void* manifest, AddResourceFn add_resourc return true; } +bool PrecacheList::Add(std::vector heroes, std::vector resources) { + // Append each name once; any new name waits for the next build. + bool added = false; + const auto append = [&added](std::vector& held, std::vector& names) { + for (auto& name : names) { + if (std::find(held.begin(), held.end(), name) != held.end()) { + continue; + } + held.push_back(std::move(name)); + added = true; + } + }; + append(heroes_, heroes); + append(resources_, resources); + pending_ = pending_ || added; + return added; +} + void GameRulesHooks::AddPrecache(std::vector heroes, std::vector resources) { #if defined(_WIN32) - for (auto& hero : heroes) { - if (std::find(g_hero_names.begin(), g_hero_names.end(), hero) == g_hero_names.end()) { - g_hero_names.push_back(std::move(hero)); - } - } - for (auto& resource : resources) { - if (std::find(g_resource_names.begin(), g_resource_names.end(), resource) == - g_resource_names.end()) { - g_resource_names.push_back(std::move(resource)); - } - } + g_precache.Add(std::move(heroes), std::move(resources)); +#else + (void)heroes; + (void)resources; +#endif +} + +bool GameRulesHooks::PrecachePending() const { +#if defined(_WIN32) + return g_precache.Pending(); +#else + return false; #endif } @@ -410,7 +428,7 @@ std::expected GameRulesHooks::Install( // Log where the hook landed. std::cout << "[modlock] game rules: manifest hook installed at +0x" << std::hex - << builder_addr - server.base() << std::dec << "; " << g_hero_names.size() + << builder_addr - server.base() << std::dec << "; " << g_precache.heroes().size() << " hero(es) registered\n"; return owner; #else diff --git a/tests/precache_list_test.cc b/tests/precache_list_test.cc new file mode 100644 index 00000000..434388d1 --- /dev/null +++ b/tests/precache_list_test.cc @@ -0,0 +1,41 @@ +#include + +#include "modlock/gameinterop/game_rules.h" + +namespace modlock::gameinterop { +namespace { + +TEST(PrecacheList, ANameWaitsForOneBuildThenStays) { + // A new list has nothing for a build to register. + PrecacheList list; + EXPECT_FALSE(list.Pending()); + + // New names wait for the next build, which registers them. + EXPECT_TRUE(list.Add({"hero_inferno"}, {"particles/a.vpcf"})); + EXPECT_TRUE(list.Pending()); + list.Built(); + EXPECT_FALSE(list.Pending()); + + // Names the world already registered do not wait again. + EXPECT_FALSE(list.Add({"hero_inferno"}, {"particles/a.vpcf"})); + EXPECT_FALSE(list.Pending()); + + // One new name among known ones waits, and only it is appended. + EXPECT_TRUE(list.Add({"hero_inferno", "hero_haze"}, {})); + EXPECT_TRUE(list.Pending()); + EXPECT_EQ(list.heroes(), (std::vector{"hero_inferno", "hero_haze"})); + EXPECT_EQ(list.resources(), (std::vector{"particles/a.vpcf"})); + + // A repeat before the build keeps the name waiting. + EXPECT_FALSE(list.Add({"hero_haze"}, {})); + EXPECT_TRUE(list.Pending()); +} + +TEST(PrecacheList, ADuplicateWithinOneCallIsAddedOnce) { + PrecacheList list; + EXPECT_TRUE(list.Add({}, {"models/b.vmdl", "models/b.vmdl"})); + EXPECT_EQ(list.resources(), (std::vector{"models/b.vmdl"})); +} + +} // namespace +} // namespace modlock::gameinterop