2023-11-28 11:41:25 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2023-11-13 12:10:02 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using AscNet.Logging;
|
2023-11-28 11:41:25 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2023-11-13 12:10:02 +00:00
|
|
|
|
|
|
|
|
|
namespace AscNet.GameServer.Commands
|
|
|
|
|
{
|
|
|
|
|
public abstract class Command
|
|
|
|
|
{
|
|
|
|
|
protected Session session;
|
|
|
|
|
protected string[] args;
|
|
|
|
|
|
|
|
|
|
public abstract string Help { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Make sure to handle me well...
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="session"></param>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
|
|
public Command(Session session, string[] args, bool validate = true)
|
|
|
|
|
{
|
|
|
|
|
this.session = session;
|
|
|
|
|
this.args = args;
|
|
|
|
|
|
|
|
|
|
string? ret = Validate();
|
|
|
|
|
if (ret is not null && validate)
|
|
|
|
|
throw new ArgumentException(ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string? Validate()
|
|
|
|
|
{
|
|
|
|
|
List<PropertyInfo> argsProperties = GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute(typeof(ArgumentAttribute)) is not null).ToList();
|
2023-11-28 11:41:25 +00:00
|
|
|
|
if (argsProperties.Where(x => (((ArgumentAttribute)x.GetCustomAttribute(typeof(ArgumentAttribute))!).Flags & ArgumentFlags.Optional) != ArgumentFlags.Optional).Count() > args.Length)
|
2023-11-13 12:10:02 +00:00
|
|
|
|
return "Invalid args length!";
|
|
|
|
|
|
|
|
|
|
foreach (var argProp in argsProperties)
|
|
|
|
|
{
|
|
|
|
|
ArgumentAttribute attr = (ArgumentAttribute)argProp.GetCustomAttribute(typeof(ArgumentAttribute))!;
|
2023-11-28 11:41:25 +00:00
|
|
|
|
if (attr.Position + 1 > args.Length && (attr.Flags & ArgumentFlags.Optional) != ArgumentFlags.Optional)
|
|
|
|
|
return $"Argument {argProp.Name} is required!";
|
|
|
|
|
else if (attr.Position + 1 > args.Length)
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-11-13 12:10:02 +00:00
|
|
|
|
if (!attr.Pattern.IsMatch(args[attr.Position]))
|
|
|
|
|
return $"Argument {argProp.Name} is invalid!";
|
|
|
|
|
|
|
|
|
|
argProp.SetValue(this, args[attr.Position]);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void Execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Property)]
|
|
|
|
|
public class ArgumentAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
public int Position { get; }
|
2023-12-02 09:24:16 +00:00
|
|
|
|
public Regex Pattern { get; set; }
|
2023-11-13 12:10:02 +00:00
|
|
|
|
public string? Description { get; }
|
2023-11-28 11:41:25 +00:00
|
|
|
|
public ArgumentFlags Flags { get; }
|
2023-11-13 12:10:02 +00:00
|
|
|
|
|
2023-11-28 11:41:25 +00:00
|
|
|
|
public ArgumentAttribute(int position, string pattern, string? description = null, ArgumentFlags flags = ArgumentFlags.None)
|
2023-11-13 12:10:02 +00:00
|
|
|
|
{
|
|
|
|
|
Position = position;
|
2023-12-02 09:24:16 +00:00
|
|
|
|
|
|
|
|
|
if ((flags & ArgumentFlags.IgnoreCase) != ArgumentFlags.IgnoreCase)
|
|
|
|
|
Pattern = new(pattern);
|
|
|
|
|
else
|
|
|
|
|
Pattern = new(pattern, RegexOptions.IgnoreCase);
|
|
|
|
|
|
2023-11-13 12:10:02 +00:00
|
|
|
|
Description = description;
|
2023-11-28 11:41:25 +00:00
|
|
|
|
Flags = flags;
|
2023-11-13 12:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 11:41:25 +00:00
|
|
|
|
public enum ArgumentFlags
|
|
|
|
|
{
|
|
|
|
|
None = 0,
|
2023-12-02 09:24:16 +00:00
|
|
|
|
Optional = 1,
|
|
|
|
|
IgnoreCase = 2
|
2023-11-28 11:41:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 12:10:02 +00:00
|
|
|
|
[AttributeUsage(AttributeTargets.Class)]
|
|
|
|
|
public class CommandNameAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
|
|
|
|
public CommandNameAttribute(string name)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CommandName("help")]
|
|
|
|
|
internal class HelpCommand : Command
|
|
|
|
|
{
|
|
|
|
|
public HelpCommand(Session session, string[] args, bool validate = true) : base(session, args, validate) { }
|
|
|
|
|
|
|
|
|
|
public override string Help => "Show this help.";
|
|
|
|
|
|
|
|
|
|
public override void Execute()
|
|
|
|
|
{
|
|
|
|
|
string helpText = string.Empty;
|
|
|
|
|
|
|
|
|
|
foreach (var command in CommandFactory.commands.Keys)
|
|
|
|
|
{
|
|
|
|
|
Command? cmd = CommandFactory.CreateCommand(command, session, args, false);
|
|
|
|
|
if (cmd is not null)
|
2023-11-27 12:45:34 +00:00
|
|
|
|
{
|
|
|
|
|
List<PropertyInfo> argsProperties = cmd.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute(typeof(ArgumentAttribute)) is not null).ToList();
|
|
|
|
|
|
2023-11-28 11:41:25 +00:00
|
|
|
|
helpText += $"{command} {string.Join(" ", argsProperties.Select(x => (((ArgumentAttribute)x.GetCustomAttribute(typeof(ArgumentAttribute))!).Flags & ArgumentFlags.Optional) == ArgumentFlags.Optional ? $"[{x.Name}]" : $"<{x.Name}>"))}\n{cmd.Help}\n";
|
2023-11-27 12:45:34 +00:00
|
|
|
|
foreach (var argProp in argsProperties)
|
|
|
|
|
{
|
|
|
|
|
ArgumentAttribute attr = (ArgumentAttribute)argProp.GetCustomAttribute(typeof(ArgumentAttribute))!;
|
2023-11-27 13:00:58 +00:00
|
|
|
|
helpText += string.Format($"└{argProp.Name} \"{attr.Pattern}\"{{0}}\n", string.IsNullOrEmpty(attr.Description) ? string.Empty : $", {attr.Description}");
|
2023-11-27 12:45:34 +00:00
|
|
|
|
}
|
2023-11-27 13:00:58 +00:00
|
|
|
|
helpText += '\n';
|
2023-11-27 12:45:34 +00:00
|
|
|
|
}
|
2023-11-13 12:10:02 +00:00
|
|
|
|
}
|
2023-11-27 12:45:34 +00:00
|
|
|
|
|
|
|
|
|
throw new CommandMessageCallbackException(helpText);
|
2023-11-13 12:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class CommandFactory
|
|
|
|
|
{
|
|
|
|
|
public static readonly Dictionary<string, Type> commands = new();
|
|
|
|
|
|
|
|
|
|
internal static readonly Logger log = new(typeof(CommandFactory), LogLevel.DEBUG, LogLevel.DEBUG);
|
|
|
|
|
|
|
|
|
|
public static void LoadCommands()
|
|
|
|
|
{
|
|
|
|
|
log.LogLevelColor[LogLevel.INFO] = ConsoleColor.White;
|
|
|
|
|
log.Info("Loading commands...");
|
|
|
|
|
|
|
|
|
|
IEnumerable<Type> classes = from t in Assembly.GetExecutingAssembly().GetTypes()
|
|
|
|
|
where t.IsClass && t.GetCustomAttribute<CommandNameAttribute>() is not null
|
|
|
|
|
select t;
|
|
|
|
|
|
|
|
|
|
foreach (var command in classes)
|
|
|
|
|
{
|
|
|
|
|
CommandNameAttribute nameAttr = command.GetCustomAttribute<CommandNameAttribute>()!;
|
|
|
|
|
commands.Add(nameAttr.Name, command);
|
|
|
|
|
#if DEBUG
|
|
|
|
|
log.Info($"Loaded {nameAttr.Name} command");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info("Finished loading commands");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Command? CreateCommand(string name, Session session, string[] args, bool validate = true)
|
|
|
|
|
{
|
|
|
|
|
Type? command = commands.GetValueOrDefault(name);
|
|
|
|
|
if (command is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return (Command)Activator.CreateInstance(command, new object[] { session, args, validate })!;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-27 12:45:34 +00:00
|
|
|
|
|
|
|
|
|
public class CommandMessageCallbackException : Exception
|
|
|
|
|
{
|
|
|
|
|
public CommandMessageCallbackException(string message)
|
|
|
|
|
: base(message) { }
|
|
|
|
|
}
|
2023-11-13 12:10:02 +00:00
|
|
|
|
}
|