Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions include/modlock/engine_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class MODLOCK_API EngineHost {
// Resource selection belongs to the plugin; the framework deduplicates them.
std::expected<void, std::string> Precache(std::vector<std::string> heroes,
std::vector<std::string> 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.
Expand Down
31 changes: 31 additions & 0 deletions include/modlock/gameinterop/game_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> heroes, std::vector<std::string> 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<std::string>& heroes() const { return heroes_; }

[[nodiscard]] const std::vector<std::string>& resources() const { return resources_; }

private:
std::vector<std::string> heroes_;
std::vector<std::string> 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
Expand Down Expand Up @@ -101,6 +129,9 @@ class MODLOCK_API GameRulesHooks {

// AddPrecache adds plugin-supplied resources before the next manifest build.
void AddPrecache(std::vector<std::string> heroes, std::vector<std::string> 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;
Expand Down
5 changes: 5 additions & 0 deletions src/engine_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ std::expected<gameinterop::StaminaObserver*, std::string> 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<void, std::string> EngineHost::Precache(std::vector<std::string> heroes,
std::vector<std::string> resources) {
// Add to the precache lists once the hooks exist.
Expand Down
56 changes: 37 additions & 19 deletions src/gameinterop/game_rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> g_hero_names;
std::vector<std::string> g_resource_names;
PrecacheList g_precache;
void* g_precache_global = nullptr;
HeroPrecacheFn g_precache_fn = nullptr;
GameRulesHooks::AddResourceFn g_add_resource = nullptr;
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -107,20 +106,39 @@ bool GameRulesHooks::RegisterResources(void* manifest, AddResourceFn add_resourc
return true;
}

bool PrecacheList::Add(std::vector<std::string> heroes, std::vector<std::string> resources) {
// Append each name once; any new name waits for the next build.
bool added = false;
const auto append = [&added](std::vector<std::string>& held, std::vector<std::string>& 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<std::string> heroes,
std::vector<std::string> 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
}

Expand Down Expand Up @@ -410,7 +428,7 @@ std::expected<GameRulesHooks, std::string> 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
Expand Down
41 changes: 41 additions & 0 deletions tests/precache_list_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <gtest/gtest.h>

#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<std::string>{"hero_inferno", "hero_haze"}));
EXPECT_EQ(list.resources(), (std::vector<std::string>{"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<std::string>{"models/b.vmdl"}));
}

} // namespace
} // namespace modlock::gameinterop
Loading