Skip to content

Commit

Permalink
fix: remove command to use command manager (#579)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Wilson <[email protected]>
  • Loading branch information
ianlucas and roflmuffin authored Oct 11, 2024
1 parent cdb7a6e commit a0fcb78
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions managed/CounterStrikeSharp.API/Core/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ public void Dispose()

public readonly Dictionary<Delegate, CallbackSubscriber> Handlers =
new Dictionary<Delegate, CallbackSubscriber>();

public readonly Dictionary<Delegate, CallbackSubscriber> CommandHandlers =
new Dictionary<Delegate, CallbackSubscriber>();

public readonly Dictionary<Delegate, CallbackSubscriber> CommandListeners =
new Dictionary<Delegate, CallbackSubscriber>();
Expand All @@ -132,6 +129,8 @@ public void Dispose()
internal readonly Dictionary<Delegate, EntityIO.EntityOutputCallback> EntitySingleOutputHooks =
new Dictionary<Delegate, EntityIO.EntityOutputCallback>();

public readonly List<CommandDefinition> CommandDefinitions = new List<CommandDefinition>();

public readonly List<Timer> Timers = new List<Timer>();

public delegate HookResult GameEventHandler<T>(T @event, GameEventInfo info) where T : GameEvent;
Expand Down Expand Up @@ -193,11 +192,13 @@ public void DeregisterEventHandler(string name, Delegate handler, bool post)
public void AddCommand(string name, string description, CommandInfo.CommandCallback handler)
{
var definition = new CommandDefinition(name, description, handler);
CommandDefinitions.Add(definition);
CommandManager.RegisterCommand(definition);
}

private void AddCommand(CommandDefinition definition)
{
CommandDefinitions.Add(definition);
CommandManager.RegisterCommand(definition);
}

Expand Down Expand Up @@ -229,14 +230,13 @@ public void AddCommandListener(string? name, CommandInfo.CommandListenerCallback
/// <param name="handler">The callback function to be invoked when the command is executed.</param>
public void RemoveCommand(string name, CommandInfo.CommandCallback handler)
{
if (CommandHandlers.ContainsKey(handler))
{
var subscriber = CommandHandlers[handler];
var definition = CommandDefinitions.FirstOrDefault(
definition => definition.Name == name && definition.Callback == handler);

NativeAPI.RemoveCommand(name, subscriber.GetInputArgument());

FunctionReference.Remove(subscriber.GetReferenceIdentifier());
CommandHandlers.Remove(handler);
if (definition != null)
{
CommandDefinitions.Remove(definition);
CommandManager.RemoveCommand(definition);
}
}

Expand Down Expand Up @@ -621,11 +621,6 @@ protected virtual void Dispose(bool disposing)
{
subscriber.Dispose();
}

foreach (var subscriber in CommandHandlers.Values)
{
subscriber.Dispose();
}

foreach (var subscriber in CommandListeners.Values)
{
Expand All @@ -642,6 +637,11 @@ protected virtual void Dispose(bool disposing)
subscriber.Dispose();
}

foreach (var definition in CommandDefinitions)
{
CommandManager.RemoveCommand(definition);
}

foreach (var timer in Timers)
{
timer.Kill();
Expand All @@ -650,4 +650,4 @@ protected virtual void Dispose(bool disposing)
_disposed = true;
}
}
}
}

0 comments on commit a0fcb78

Please sign in to comment.