Added alias functionality to the shell.

This commit is contained in:
That_One_Nerd 2023-03-29 08:26:42 -04:00
parent 0a92bc6b5a
commit a1d95170bd
5 changed files with 33 additions and 28 deletions

View File

@ -100,9 +100,6 @@ public static class BaseModule
});
}
[Command("cut")]
public static void CutFile(string source, string destination) => MoveFile(source, destination);
[Command("del")]
public static void Delete(string path)
{
@ -189,9 +186,6 @@ public static class BaseModule
[Command("echo")]
public static void Echo(string msg) => Write(msg);
[Command("exit")]
public static void ExitShell(int code = 0) => QuitShell(code);
[Command("explorer")]
public static void OpenExplorer(string path = ".") => Process.Start("explorer.exe", Path.GetFullPath(path));
@ -210,6 +204,7 @@ public static class BaseModule
DisplayWithPages(lines);
}
[Command("cut")]
[Command("move")]
public static void MoveFile(string source, string destination)
{
@ -287,6 +282,7 @@ public static class BaseModule
}
[Command("print")]
[Command("type")]
public static void Print(string file)
{
if (!File.Exists(file)) throw new($"No file exists at \"{file}\"");
@ -346,15 +342,13 @@ public static class BaseModule
});
}
[Command("exit")]
[Command("quit")]
public static void QuitShell(int code = 0)
{
Environment.Exit(code);
}
[Command("type")]
public static void Type(string file) => Print(file);
[Command("undo")]
public static void UndoCommand(int amount = 1)
{

View File

@ -1,6 +1,6 @@
namespace SrcMod.Shell.Modules.ObjectModels;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class CommandAttribute : Attribute
{
public readonly string NameId;

View File

@ -17,18 +17,26 @@ public class CommandInfo
RequiredParameters = 0;
}
public static CommandInfo? FromMethod(ModuleInfo parentModule, MethodInfo info)
public static CommandInfo[] FromMethod(ModuleInfo parentModule, MethodInfo info)
{
CommandAttribute? attribute = info.GetCustomAttribute<CommandAttribute>();
if (attribute is null) return null;
// This is a little redundant as we're duplicating a bunch of info,
// but honestly, it isn't too bad. Maybe there will be an improvement
// in the future, maybe not. But not for a while because this works.
if (info.ReturnType != typeof(void)) return null;
if (info.ReturnType != typeof(void)) return Array.Empty<CommandInfo>();
ParameterInfo[] param = info.GetParameters();
int required = 0;
while (required < param.Length && !param[required].IsOptional) required++;
return new()
List<CommandInfo> commands = new();
CommandAttribute[] attributes = info.GetCustomAttributes<CommandAttribute>().ToArray();
if (attributes.Length <= 0) return Array.Empty<CommandInfo>();
foreach (CommandAttribute attribute in attributes)
{
commands.Add(new()
{
Method = info,
Module = parentModule,
@ -36,7 +44,10 @@ public class CommandInfo
NameId = attribute.NameId,
Parameters = param,
RequiredParameters = required
};
});
}
return commands.ToArray();
}
public void Invoke(params string[] args)

View File

@ -18,7 +18,7 @@ public class ModuleInfo
NameIsPrefix = true;
}
public static ModuleInfo? FromModule(Type info)
public static ModuleInfo? FromType(Type info)
{
ModuleAttribute? attribute = info.GetCustomAttribute<ModuleAttribute>();
if (attribute is null) return null;
@ -37,9 +37,9 @@ public class ModuleInfo
List<CommandInfo> commands = new();
foreach (MethodInfo method in info.GetMethods())
{
CommandInfo? cmd = CommandInfo.FromMethod(module, method);
if (cmd is null) continue;
commands.Add(cmd);
CommandInfo[] cmds = CommandInfo.FromMethod(module, method);
if (cmds.Length <= 0) continue;
commands.AddRange(cmds);
}
module.Commands.AddRange(commands);

View File

@ -4,7 +4,7 @@ public class Shell
{
public const string Author = "That_One_Nerd";
public const string Name = "SrcMod";
public const string Version = "Alpha 0.1.0";
public const string Version = "Alpha 0.2.1";
public readonly string? ShellDirectory;
@ -62,7 +62,7 @@ public class Shell
}
foreach (Type t in possibleModules)
{
ModuleInfo? module = ModuleInfo.FromModule(t);
ModuleInfo? module = ModuleInfo.FromType(t);
if (module is not null)
{
LoadedModules.Add(module);