Skip to content

feat: cache the game rules entity behind Utilities.GetGameRules - #1477

Merged
roflmuffin merged 3 commits into
roflmuffin:mainfrom
makadore:feat/cached-game-rules
Nov 5, 2026
Merged

roflmuffin merged 3 commits into
roflmuffin:mainfrom
makadore:feat/cached-game-rules

Conversation

@makadore

@makadore makadore commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

There is no accessor for the game rules, so plugins scan the entity list for cs_gamerules every time they need them; a GitHub code search on 28 October 2026 finds FindAllEntitiesByDesignerName<CCSGameRulesProxy> in 263 files, 55 of them inside OnTick. The core does the same for @aim targeting, with a private cache in Target.cs.

Utilities.GetGameRules() finds the proxy once and remembers its handle rather than the wrapper: a wrapper would validate itself by reading through its old pointer, while the handle is resolved through EntitySystem.GetEntityByHandle on every call, which compares the serial in the entity system's own identity table. After a map change the old handle no longer resolves and the next call scans again; during the change itself, in OnMapEnd and OnMapStart, the call returns null, and from the first tick of the new map it returns the new game rules. Before the entity system exists the call returns null and remembers nothing. Target.cs now uses it instead of its own copy, and @aim no longer dereferences a missing game rules object; @aim was checked with a player on the server before and after a map change, on a player and on nothing.

Measured on a Linux dedicated server, game build 1.41.9.3, with about 420 entities on the map: the scan costs 50 to 53 us per call, GetGameRules() 0.08 to 0.16 us, and both return the same entity. A map change on the same server was checked with a small probe: the proxy came back at another index with another serial, the old handle no longer resolved, and the accessor returned the same entity as a fresh scan.

Tests in the native test plugin: the result matches the scan, two calls return the same entity, and a call from Load() writes to the log whether it ran before the map (cold start, null) or on a map, and resolves once the map is up. That last test is what turned up #1475: an entity lookup that early used to break every later lookup in the process, so this sits on top of that fix.

@makadore
makadore requested a review from roflmuffin as a code owner October 30, 2026 13:24
Every plugin that needs the game rules scans the whole entity list for
cs_gamerules, and many do it on every tick or event. The scan costs about
60 us with 480 entities on a test server; a handle lookup costs a fraction
of a microsecond.

GetGameRules finds the proxy once and remembers its handle. The handle
stops resolving when the map changes, so the next call scans again, and a
call before the map has loaded returns null without remembering anything.
Target resolution for @aim used a private copy of the same cache and now
shares this one.
GetGameRules asked the entity system for its first entity and caught the NativeException it throws before the first map. The new native IsEntitySystemInitialized reports that state directly, so the accessor returns null early and no exception is used for control flow.

GameRulesMapChangeTests asks the accessor for the current map's rules, changes the level and checks that it hands out the new map's rules. It sits in the MapChange collection, which the assembly's collection orderer runs after every other collection, so the map change cannot disturb the rest of the smoke suite.
@makadore
makadore force-pushed the feat/cached-game-rules branch from 724ee46 to 583c4ae Compare November 1, 2026 16:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the shared game-rules accessor and its targeting integration.

No open issues 🎉

What changed in this PR

Adds Utilities.GetGameRules() as a shared, handle-based cache, reducing repeated entity scans and replacing the private cache used by @aim targeting. Builds on #1475’s early entity-lookup recovery fix.

Changes:

  • Adds a native initialization check and a cached accessor that resolves the proxy handle on each call.
  • Updates @aim targeting to use the shared accessor and handle missing game rules.
  • Adds native tests for scan equivalence, repeated calls, load-time access, and map changes.
File Description
src/​scripting/​natives/​natives_entities.yaml Declares the entity-system initialization native.
src/​scripting/​natives/​natives_entities.cpp Implements and registers the initialization check.
managed/​CounterStrikeSharp.Tests.Native/​NativeTestsPlugin.cs Probes game rules during plugin loading.
managed/​CounterStrikeSharp.Tests.Native/​MapChangeCollectionOrderer.cs Orders map-change tests after other collections.
managed/​CounterStrikeSharp.Tests.Native/​GameRulesTests.cs Tests accessor consistency and lifecycle behavior.
managed/​CounterStrikeSharp.API/​Utilities.cs Adds the handle-cached GetGameRules() accessor.
managed/​CounterStrikeSharp.API/​Modules/​Entities/​EntitySystem.cs Exposes entity-system initialization status.
managed/​CounterStrikeSharp.API/​Modules/​Commands/​Targeting/​Target.cs Uses the shared accessor and guards against missing rules.
managed/​CounterStrikeSharp.API/​Generated/​Natives/​API.cs Adds the generated initialization-check binding.

Review effort: Balanced

@roflmuffin
roflmuffin merged commit d0d812b into roflmuffin:main Nov 5, 2026
7 checks passed
@makadore
makadore deleted the feat/cached-game-rules branch November 5, 2026 07:19
M-archand pushed a commit to M-archand/CounterStrikeSharp that referenced this pull request Nov 6, 2026
markus-wa pushed a commit to markus-wa/CounterStrikeSharp that referenced this pull request Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants