Repository navigation
feat: cache the game rules entity behind Utilities.GetGameRules - #1477
Merged
Merged
Conversation
roflmuffin
reviewed
Nov 1, 2026
roflmuffin
reviewed
Nov 1, 2026
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
force-pushed
the
feat/cached-game-rules
branch
from
November 1, 2026 16:47
724ee46 to
583c4ae
Compare
There was a problem hiding this comment.
🟢 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
@aimtargeting 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
approved these changes
Nov 5, 2026
M-archand
pushed a commit
to M-archand/CounterStrikeSharp
that referenced
this pull request
Nov 6, 2026
…muffin#1477) (cherry picked from commit d0d812b)
This was referenced Nov 12, 2026
markus-wa
pushed a commit
to markus-wa/CounterStrikeSharp
that referenced
this pull request
Nov 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is no accessor for the game rules, so plugins scan the entity list for
cs_gamerulesevery time they need them; a GitHub code search on 28 October 2026 findsFindAllEntitiesByDesignerName<CCSGameRulesProxy>in 263 files, 55 of them insideOnTick. The core does the same for@aimtargeting, with a private cache inTarget.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 throughEntitySystem.GetEntityByHandleon 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, inOnMapEndandOnMapStart, 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.csnow uses it instead of its own copy, and@aimno longer dereferences a missing game rules object;@aimwas 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.