Skip to content

Commit

Permalink
feat: add console command attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Oct 12, 2023
1 parent c25c4f0 commit 0f7d11f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using CounterStrikeSharp.API.Modules.Events;

namespace CounterStrikeSharp.API.Core.Attributes;

[AttributeUsage(AttributeTargets.Method)]
public class ConsoleCommandAttribute : Attribute
{
public string Command { get; }
public string Description { get; }

public ConsoleCommandAttribute(string command, string description = null)
{
Command = command;
Description = description;
}
}
20 changes: 20 additions & 0 deletions managed/CounterStrikeSharp.API/Core/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ public event Listeners.SourceEventHandler<Listeners.EntityArgs> OnEntityDeleted
remove => RemoveListener("OnEntityDeleted", value);
}

public void RegisterAllAttributes(object instance)
{
this.RegisterAttributeHandlers(instance);
this.RegisterConsoleCommandAttributeHandlers(instance);
}

/**
* Automatically registers all game event handlers that are decorated with the [GameEventHandler] attribute.
*/
Expand Down Expand Up @@ -349,6 +355,20 @@ public void RegisterAttributeHandlers(object instance)
}
}

public void RegisterConsoleCommandAttributeHandlers(object instance)
{
var eventHandlers = instance.GetType()
.GetMethods()
.Where(method => method.GetCustomAttribute<ConsoleCommandAttribute>() != null)
.ToArray();

foreach (var eventHandler in eventHandlers)
{
var commandInfo = eventHandler.GetCustomAttribute<ConsoleCommandAttribute>();
AddCommand(commandInfo.Command, commandInfo.Description, eventHandler.CreateDelegate<CommandInfo.CommandCallback>(instance));
}
}

public void Dispose()
{
Dispose(true);
Expand Down
2 changes: 1 addition & 1 deletion managed/CounterStrikeSharp.API/Core/PluginContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Load(bool hotReload = false)

Console.WriteLine($"Loading plugin: {pluginType.Name}");
_plugin = (BasePlugin)Activator.CreateInstance(pluginType)!;
_plugin.RegisterAttributeHandlers(_plugin);
_plugin.RegisterAllAttributes(_plugin);
_plugin.Load(hotReload);

Console.WriteLine($"Finished loading plugin: {Name}");
Expand Down
7 changes: 7 additions & 0 deletions managed/TestPlugin/TestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Events;

namespace TestPlugin
Expand Down Expand Up @@ -65,6 +66,12 @@ public void OnPlayerConnect(EventPlayerConnect @event)
Console.ResetColor();
}

[ConsoleCommand("cssharp_attribute", "This is a custom attribute event")]
public void OnCommand(int client, CommandInfo command)
{
Console.WriteLine("cssharp_attribute called!");
}

private void GenericEventHandler<T>(T @event) where T : GameEvent
{
Console.BackgroundColor = ConsoleColor.Blue;
Expand Down

0 comments on commit 0f7d11f

Please sign in to comment.