Skip to content

Commit

Permalink
Move delegate creation to commandinfo generator to make constructor m…
Browse files Browse the repository at this point in the history
…ore readable
  • Loading branch information
karashiiro committed Jun 9, 2020
1 parent f45e12c commit d01e246
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions PluginCommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Dalamud.Game.Command;
using Dalamud.Plugin;
using DalamudPluginProjectTemplate.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Dalamud.Game.Command;
using Dalamud.Plugin;
using DalamudPluginProjectTemplate.Attributes;
using static Dalamud.Game.Command.CommandInfo;
// ReSharper disable ForCanBeConvertedToForeach

Expand All @@ -14,17 +14,17 @@ public class PluginCommandManager<THost> : IDisposable
{
private readonly DalamudPluginInterface pluginInterface;
private readonly (string, CommandInfo)[] pluginCommands;
private readonly THost host;

public PluginCommandManager(THost host, DalamudPluginInterface pluginInterface)
{
this.pluginInterface = pluginInterface;
this.host = host;

#region Command Registration
this.pluginCommands = host.GetType().GetMethods()
.Where(method => method.GetCustomAttribute<CommandAttribute>() != null)
.SelectMany(method => GetCommandInfoTuple((HandlerDelegate)Delegate.CreateDelegate(typeof(HandlerDelegate), host, method)))
.SelectMany(GetCommandInfoTuple)
.ToArray();
#endregion

AddComandHandlers();
}
Expand All @@ -51,13 +51,10 @@ private void RemoveCommandHandlers()
}
}

public void Dispose()
private IEnumerable<(string, CommandInfo)> GetCommandInfoTuple(MethodInfo method)
{
RemoveCommandHandlers();
}
var handlerDelegate = (HandlerDelegate)Delegate.CreateDelegate(typeof(HandlerDelegate), this.host, method);

private static IEnumerable<(string, CommandInfo)> GetCommandInfoTuple(HandlerDelegate handlerDelegate)
{
var command = handlerDelegate.Method.GetCustomAttribute<CommandAttribute>();
var aliases = handlerDelegate.Method.GetCustomAttribute<AliasesAttribute>();
var helpMessage = handlerDelegate.Method.GetCustomAttribute<HelpMessageAttribute>();
Expand All @@ -82,5 +79,10 @@ public void Dispose()

return commandInfoTuples;
}

public void Dispose()
{
RemoveCommandHandlers();
}
}
}

0 comments on commit d01e246

Please sign in to comment.