Skip to content

Commit

Permalink
refactor: reuse command types
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Aug 11, 2024
1 parent 25987e9 commit 01037e7
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions lc-hax/Scripts/Static/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,28 @@
static class Chat {
internal static event Action<string>? OnExecuteCommandAttempt;

static IEnumerable<Type> CommandTypes { get; } =
Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ICommand).IsAssignableFrom(type));

static Dictionary<string, ICommand> Commands { get; } =
Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ICommand).IsAssignableFrom(type))
.Where(type => type.GetCustomAttribute<CommandAttribute>() is not null)
.ToDictionary(
type => type.GetCustomAttribute<CommandAttribute>().Syntax,
type => (ICommand)Activator.CreateInstance(type)
);
Chat.CommandTypes.Where(type => type.GetCustomAttribute<CommandAttribute>() is not null).ToDictionary(
type => type.GetCustomAttribute<CommandAttribute>().Syntax,
type => (ICommand)Activator.CreateInstance(type)
);

static Dictionary<string, ICommand> DebugCommands { get; } =
Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ICommand).IsAssignableFrom(type))
.Where(type => type.GetCustomAttribute<DebugCommandAttribute>() is not null)
.ToDictionary(
type => type.GetCustomAttribute<DebugCommandAttribute>().Syntax,
type => (ICommand)new DebugCommand((ICommand)Activator.CreateInstance(type))
);
Chat.CommandTypes.Where(type => type.GetCustomAttribute<DebugCommandAttribute>() is not null).ToDictionary(
type => type.GetCustomAttribute<DebugCommandAttribute>().Syntax,
type => (ICommand)new DebugCommand((ICommand)Activator.CreateInstance(type))
);

static Dictionary<string, ICommand> PrivilegeCommands { get; } =
Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ICommand).IsAssignableFrom(type))
.Where(type => type.GetCustomAttribute<PrivilegedCommandAttribute>() is not null)
.ToDictionary(
type => type.GetCustomAttribute<PrivilegedCommandAttribute>().Syntax,
type => (ICommand)new PrivilegedCommand((ICommand)Activator.CreateInstance(type))
);
Chat.CommandTypes.Where(type => type.GetCustomAttribute<PrivilegedCommandAttribute>() is not null).ToDictionary(
type => type.GetCustomAttribute<PrivilegedCommandAttribute>().Syntax,
type => (ICommand)new PrivilegedCommand((ICommand)Activator.CreateInstance(type))
);

internal static void Clear() {
Helper.HUDManager?.AddTextToChatOnServer(
Expand Down

0 comments on commit 01037e7

Please sign in to comment.