Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add FakeConVar class #325

Merged
merged 9 commits into from
Feb 26, 2024
Merged

feat: add FakeConVar class #325

merged 9 commits into from
Feb 26, 2024

Conversation

roflmuffin
Copy link
Owner

@roflmuffin roflmuffin commented Feb 16, 2024

Proposed usage, in your main plugin class:

public FakeConVar<bool> TestConVar = new ("my_allow_bool", "Allow the thing", defaultValue: true);
public FakeConVar<int> IntConvar = new ("my_allow_int", "Allow the thing", defaultValue: 20, new RangeValidator<int>(0, 5));
public FakeConVar<string> StrConvar = new ("fake_convar_str", "Allow the thing"));

... later on in plugin load:

TestConVar.ValueChanged += (sender, value) =>
{
    Logger.LogInformation("TestConVar value changed to {Value}", value);
};

Full Example Plugin

public class WithFakeConvarsPlugin : BasePlugin
{
    public override string ModuleName => "Example: With Fake Convars";
    public override string ModuleVersion => "1.0.0";
    public override string ModuleAuthor => "CounterStrikeSharp & Contributors";
    public override string ModuleDescription => "A simple plugin that registers some console variables";
    
    // FakeConVar is a class that can be used to create custom console variables.
    // You can specify a name, description, default value, and custom validators.
    public FakeConVar<bool> BoolCvar = new("example_bool", "An example boolean cvar", true);
    
    // Range validator is an inbuilt validator that can be used to ensure that a value is within a certain range.
    public FakeConVar<int> ExampleIntCvar  = new("example_int", "An example integer cvar", 10, flags: ConVarFlags.FCVAR_NONE, new RangeValidator<int>(0, 100));
    
    public FakeConVar<float> ExampleFloatCvar = new("example_float", "An example float cvar", 10, flags: ConVarFlags.FCVAR_NONE, new RangeValidator<float>(5, 20));
    public FakeConVar<string> ExampleStringCvar = new("example_string", "An example string cvar", "default");

    // Replicated, Cheat & Protected flags are supported.
    public FakeConVar<float> ExamplePublicCvar = new("example_public_float", "An example public float cvar", 5,
        ConVarFlags.FCVAR_REPLICATED);
    
    // Can only be changed if sv_cheats is enabled.
    public FakeConVar<float> ExampleCheatCvar = new("example_cheat_float", "An example cheat float cvar", 5,
        ConVarFlags.FCVAR_CHEAT);
    
    // Protected cvars do not output their value when queried.
    public FakeConVar<float> ExampleProtectedCvar = new("example_protected_float", "An example cheat float cvar", 5,
        ConVarFlags.FCVAR_PROTECTED);
    
    // You can create your own custom validators by implementing the IValidator interface.
    public FakeConVar<int> ExampleEvenNumberCvar = new("example_even_number", "An example even number cvar", 0, flags: ConVarFlags.FCVAR_NONE, new EvenNumberValidator());

    public FakeConVar<int> RequiresRestartCvar = new("example_requires_restart", "A cvar that requires a restart when changed");
    
    public override void Load(bool hotReload)
    {
        // You can subscribe to the ValueChanged event to execute code when the value of a cvar changes.
        // In this example, we restart the game when the value of RequiresRestartCvar is greater than 5.
        RequiresRestartCvar.ValueChanged += (sender, value) =>
        {
            if (value > 5)
            {
                Server.ExecuteCommand("mp_restartgame 1");
            }
        };
        
        RegisterFakeConVars(typeof(ConVars));
    }
}

All FakeConvar fields on your plugin class will be automatically registered, but if you want to register some from another class you can do it using plugin.RegisterFakeConVars(myOtherClass), similar to event attributes.

@roflmuffin roflmuffin marked this pull request as ready for review February 25, 2024 04:51
@roflmuffin roflmuffin merged commit a5399dd into main Feb 26, 2024
4 checks passed
@roflmuffin roflmuffin deleted the feature/fake-convars branch February 26, 2024 06:55
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.

1 participant