You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: add Convar<T> for real convar creation/deletion/access - #935
This PR adds Convar<T> which is a strongly typed variant of ConVar which provides better safety (no raw pointer accesses), as well as the ability to create real-convars that exist within the engine (and can be found using find foo in the console) amongst other things.
TODO:
Add BasePlugin based registration so that convars are automatically unloaded (similar to FakeConvar). Should the .Create static be restricted?
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome. Would be great to also have a list of known ConVars that utilize this to reduce mistake of accidentally using the wrong type with a ConVar. This however would be fitting in another PR I suppose.
This looks awesome. Would be great to also have a list of known ConVars that utilize this to reduce mistake of accidentally using the wrong type with a ConVar. This however would be fitting in another PR I suppose.
This looks awesome. Would be great to also have a list of known ConVars that utilize this to reduce mistake of accidentally using the wrong type with a ConVar. This however would be fitting in another PR I suppose.
Yes I know of that. But implement some of these at least into C# as statically typed to make use of the strength of static typing and static code analysis to reduce mistakes and catch erronous usage at compile time.
For example would it be suitable to do something similar to this?
public static class KnownConVars
{
public static ConVar<bool> SvCheats = new ConVar<bool>("sv_cheats");
public static ConVar<int> MpWarmupTime = new ConVar<int>("mp_warmuptime");
public static ConVar<float> MaxSpeed = new ConVar<float>("sv_maxspeed");
// Example: Add other ConVars here...
}
Filtering on an exact declared ConVar<> type skips owned values stored in a ConVarBase field or a subclass of ConVar<T>, so those public plugin fields are never deleted on unload. The loop already checks the runtime value and Owned; select every field assignable to ConVarBase instead.
Returned vector values bypass validation and expose storage
For vector and angle types this returns a NativeObject backed directly by the engine's value pointer. Mutating the returned object's components therefore changes the ConVar without going through SetAs, bypassing bounds enforcement and OnConVarChanged notifications; retaining it also exposes storage after deletion. Return a detached value copy (or an immutable view) so mutations must use the Value setter.
Created ConVar wrappers are leaked
src/scripting/natives/natives_convars.cpp:624
Each creation branch allocates a CConVar<T> with new, but this pointer is only kept in this local variable and is discarded when the native returns. DeleteConVar merely calls Invalidate(), so repeated plugin reloads or create/delete cycles permanently leak every allocated wrapper (and potentially its owned storage). Keep the created object in an ownership registry and destroy it during deletion/shutdown.
Add null checks for DefaultValue and value parameters in Create and setter methods.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This branch has not been deployed
No deployments
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
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.
This PR adds
Convar<T>which is a strongly typed variant of ConVar which provides better safety (no raw pointer accesses), as well as the ability to create real-convars that exist within the engine (and can be found usingfind fooin the console) amongst other things.TODO:
.Createstatic be restricted?