StyleCop: everything else

This commit is contained in:
Raymond Lynch 2021-05-30 07:10:00 -04:00
parent f64c9b8321
commit 595fd3f1e4
134 changed files with 16346 additions and 6202 deletions

View file

@ -1,10 +1,23 @@
using System.Reflection;
namespace Dalamud.Game.Command {
namespace Dalamud.Game.Command
{
/// <summary>
/// This class describes a registered command.
/// </summary>
public sealed class CommandInfo {
public sealed class CommandInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="CommandInfo"/> class.
/// Create a new CommandInfo with the provided handler.
/// </summary>
/// <param name="handler">The method to call when the command is run.</param>
public CommandInfo(HandlerDelegate handler)
{
this.Handler = handler;
this.LoaderAssemblyName = Assembly.GetCallingAssembly()?.GetName()?.Name;
}
/// <summary>
/// The function to be executed when the command is dispatched.
/// </summary>
@ -13,29 +26,23 @@ namespace Dalamud.Game.Command {
public delegate void HandlerDelegate(string command, string arguments);
/// <summary>
/// A <see cref="HandlerDelegate"/> which will be called when the command is dispatched.
/// Gets a <see cref="HandlerDelegate"/> which will be called when the command is dispatched.
/// </summary>
public HandlerDelegate Handler { get; }
/// <summary>
/// The help message for this command.
/// Gets or sets the help message for this command.
/// </summary>
public string HelpMessage { get; set; } = string.Empty;
/// <summary>
/// If this command should be shown in the help output.
/// Gets or sets a value indicating whether if this command should be shown in the help output.
/// </summary>
public bool ShowInHelp { get; set; } = true;
/// <summary>
/// Create a new CommandInfo with the provided handler.
/// </summary>
/// <param name="handler"></param>
public CommandInfo(HandlerDelegate handler) {
Handler = handler;
LoaderAssemblyName = Assembly.GetCallingAssembly()?.GetName()?.Name;
}
/// <summary>
/// Gets or sets the name of the assembly responsible for this command.
/// </summary>
internal string LoaderAssemblyName { get; set; } = string.Empty;
}
}

View file

@ -2,45 +2,37 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Internal.Libc;
using Serilog;
namespace Dalamud.Game.Command {
namespace Dalamud.Game.Command
{
/// <summary>
/// This class manages registered in-game slash commands.
/// </summary>
public sealed class CommandManager {
public sealed class CommandManager
{
private readonly Dalamud dalamud;
private readonly Dictionary<string, CommandInfo> commandMap = new Dictionary<string, CommandInfo>();
/// <summary>
/// Read-only list of all registered commands.
/// </summary>
public ReadOnlyDictionary<string, CommandInfo> Commands =>
new ReadOnlyDictionary<string, CommandInfo>(this.commandMap);
private readonly Regex commandRegexEn =
new Regex(@"^The command (?<command>.+) does not exist\.$", RegexOptions.Compiled);
private readonly Regex commandRegexJp = new Regex(@"^そのコマンドはありません。: (?<command>.+)$", RegexOptions.Compiled);
private readonly Regex commandRegexDe =
new Regex(@"^„(?<command>.+)“ existiert nicht als Textkommando\.$", RegexOptions.Compiled);
private readonly Regex commandRegexFr =
new Regex(@"^La commande texte “(?<command>.+)” n'existe pas\.$",
RegexOptions.Compiled);
private readonly Dictionary<string, CommandInfo> commandMap = new();
private readonly Regex commandRegexEn = new(@"^The command (?<command>.+) does not exist\.$", RegexOptions.Compiled);
private readonly Regex commandRegexJp = new(@"^そのコマンドはありません。: (?<command>.+)$", RegexOptions.Compiled);
private readonly Regex commandRegexDe = new(@"^„(?<command>.+)“ existiert nicht als Textkommando\.$", RegexOptions.Compiled);
private readonly Regex commandRegexFr = new(@"^La commande texte “(?<command>.+)” n'existe pas\.$", RegexOptions.Compiled);
private readonly Regex currentLangCommandRegex;
public CommandManager(Dalamud dalamud, ClientLanguage language) {
/// <summary>
/// Initializes a new instance of the <see cref="CommandManager"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance.</param>
/// <param name="language">The client language requested.</param>
public CommandManager(Dalamud dalamud, ClientLanguage language)
{
this.dalamud = dalamud;
switch (language) {
switch (language)
{
case ClientLanguage.Japanese:
this.currentLangCommandRegex = this.commandRegexJp;
break;
@ -55,40 +47,42 @@ namespace Dalamud.Game.Command {
break;
}
dalamud.Framework.Gui.Chat.OnCheckMessageHandled += OnCheckMessageHandled;
dalamud.Framework.Gui.Chat.OnCheckMessageHandled += this.OnCheckMessageHandled;
}
private void OnCheckMessageHandled(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled) {
if (type == XivChatType.ErrorMessage && senderId == 0) {
var cmdMatch = this.currentLangCommandRegex.Match(message.TextValue).Groups["command"];
if (cmdMatch.Success) {
// Yes, it's a chat command.
var command = cmdMatch.Value;
if (ProcessCommand(command)) isHandled = true;
}
}
}
/// <summary>
/// Gets a read-only list of all registered commands.
/// </summary>
public ReadOnlyDictionary<string, CommandInfo> Commands => new(this.commandMap);
/// <summary>
/// Process a command in full.
/// </summary>
/// <param name="content">The full command string.</param>
/// <returns>True if the command was found and dispatched.</returns>
public bool ProcessCommand(string content) {
public bool ProcessCommand(string content)
{
string command;
string argument;
var separatorPosition = content.IndexOf(' ');
if (separatorPosition == -1 || separatorPosition + 1 >= content.Length) {
if (separatorPosition == -1 || separatorPosition + 1 >= content.Length)
{
// If no space was found or ends with the space. Process them as a no argument
if (separatorPosition + 1 >= content.Length) {
if (separatorPosition + 1 >= content.Length)
{
// Remove the trailing space
command = content.Substring(0, separatorPosition);
} else {
}
else
{
command = content;
}
argument = string.Empty;
} else {
}
else
{
// e.g.)
// /testcommand arg1
// => Total of 17 chars
@ -98,13 +92,13 @@ namespace Dalamud.Game.Command {
command = content.Substring(0, separatorPosition);
var argStart = separatorPosition + 1;
argument = content.Substring(argStart, content.Length - argStart);
argument = content[argStart..];
}
if (!this.commandMap.TryGetValue(command, out var handler)) // Commad was not found.
return false;
DispatchCommand(command, argument, handler);
this.DispatchCommand(command, argument, handler);
return true;
}
@ -114,12 +108,15 @@ namespace Dalamud.Game.Command {
/// <param name="command">The command to dispatch.</param>
/// <param name="argument">The provided arguments.</param>
/// <param name="info">A <see cref="CommandInfo"/> object describing this command.</param>
public void DispatchCommand(string command, string argument, CommandInfo info) {
try {
public void DispatchCommand(string command, string argument, CommandInfo info)
{
try
{
info.Handler(command, argument);
} catch (Exception ex) {
Log.Error(ex, "Error while dispatching command {CommandName} (Argument: {Argument})", command,
argument);
}
catch (Exception ex)
{
Log.Error(ex, "Error while dispatching command {CommandName} (Argument: {Argument})", command, argument);
}
}
@ -129,13 +126,17 @@ namespace Dalamud.Game.Command {
/// <param name="command">The command to register.</param>
/// <param name="info">A <see cref="CommandInfo"/> object describing the command.</param>
/// <returns>If adding was successful.</returns>
public bool AddHandler(string command, CommandInfo info) {
public bool AddHandler(string command, CommandInfo info)
{
if (info == null) throw new ArgumentNullException(nameof(info), "Command handler is null.");
try {
try
{
this.commandMap.Add(command, info);
return true;
} catch (ArgumentException) {
}
catch (ArgumentException)
{
Log.Error("Command {CommandName} is already registered.", command);
return false;
}
@ -146,8 +147,23 @@ namespace Dalamud.Game.Command {
/// </summary>
/// <param name="command">The command to remove.</param>
/// <returns>If the removal was successful.</returns>
public bool RemoveHandler(string command) {
public bool RemoveHandler(string command)
{
return this.commandMap.Remove(command);
}
private void OnCheckMessageHandled(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled)
{
if (type == XivChatType.ErrorMessage && senderId == 0)
{
var cmdMatch = this.currentLangCommandRegex.Match(message.TextValue).Groups["command"];
if (cmdMatch.Success)
{
// Yes, it's a chat command.
var command = cmdMatch.Value;
if (this.ProcessCommand(command)) isHandled = true;
}
}
}
}
}