Added initial UnregisterAllAttributes function

This commit is contained in:
B3none
2024-01-25 22:27:05 +00:00
parent cdcddbb5f3
commit 91eec5de37
3 changed files with 27 additions and 2 deletions

View File

@@ -387,14 +387,19 @@ namespace CounterStrikeSharp.API.Core
Timers.Add(timer);
return timer;
}
public void RegisterAllAttributes(object instance)
{
this.RegisterAttributeHandlers(instance);
this.RegisterConsoleCommandAttributeHandlers(instance);
this.RegisterEntityOutputAttributeHandlers(instance);
}
public void UnregisterAllAttributes(object instance)
{
this.UnregisterConsoleCommandAttributeHandlers(instance);
// this.RegisterAttributeHandlers(instance);
// this.RegisterEntityOutputAttributeHandlers(instance);
}
public void InitializeConfig(object instance, Type pluginType)
{
@@ -470,6 +475,23 @@ namespace CounterStrikeSharp.API.Core
}
}
public void UnregisterConsoleCommandAttributeHandlers(object instance)
{
var eventHandlers = instance.GetType()
.GetMethods()
.Where(method => method.GetCustomAttributes<ConsoleCommandAttribute>().Any())
.ToArray();
foreach (var eventHandler in eventHandlers)
{
var attributes = eventHandler.GetCustomAttributes<ConsoleCommandAttribute>();
foreach (var commandInfo in attributes)
{
RemoveCommand(commandInfo.Command, eventHandler.CreateDelegate<CommandInfo.CommandCallback>(instance));
}
}
}
public void RegisterEntityOutputAttributeHandlers(object instance)
{
var handlers = instance.GetType()

View File

@@ -59,6 +59,7 @@ namespace CounterStrikeSharp.API.Core
IStringLocalizer Localizer { get; set; }
void RegisterAllAttributes(object instance);
void UnregisterAllAttributes(object instance);
void InitializeConfig(object instance, Type pluginType);
}

View File

@@ -215,6 +215,8 @@ namespace CounterStrikeSharp.API.Core.Plugin
_logger.LogInformation("Unloading plugin {Name}", Plugin.ModuleName);
Plugin.UnregisterAllAttributes(Plugin);
Plugin.Unload(hotReload);
Plugin.Dispose();