From a1d95170bd0107b8c91f20e0a64598c5ecfc915c Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 08:26:42 -0400 Subject: [PATCH 1/3] Added alias functionality to the shell. --- SrcMod/Shell/Modules/BaseModule.cs | 12 ++----- .../Modules/ObjectModels/CommandAttribute.cs | 2 +- .../Shell/Modules/ObjectModels/CommandInfo.cs | 35 ++++++++++++------- .../Shell/Modules/ObjectModels/ModuleInfo.cs | 8 ++--- SrcMod/Shell/Shell.cs | 4 +-- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 41d45ec..9e18192 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -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) { diff --git a/SrcMod/Shell/Modules/ObjectModels/CommandAttribute.cs b/SrcMod/Shell/Modules/ObjectModels/CommandAttribute.cs index 9d39d52..8ea8fff 100644 --- a/SrcMod/Shell/Modules/ObjectModels/CommandAttribute.cs +++ b/SrcMod/Shell/Modules/ObjectModels/CommandAttribute.cs @@ -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; diff --git a/SrcMod/Shell/Modules/ObjectModels/CommandInfo.cs b/SrcMod/Shell/Modules/ObjectModels/CommandInfo.cs index 7dee2d0..8505f35 100644 --- a/SrcMod/Shell/Modules/ObjectModels/CommandInfo.cs +++ b/SrcMod/Shell/Modules/ObjectModels/CommandInfo.cs @@ -17,26 +17,37 @@ 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(); - 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(); ParameterInfo[] param = info.GetParameters(); int required = 0; while (required < param.Length && !param[required].IsOptional) required++; - return new() + List commands = new(); + + CommandAttribute[] attributes = info.GetCustomAttributes().ToArray(); + if (attributes.Length <= 0) return Array.Empty(); + + foreach (CommandAttribute attribute in attributes) { - Method = info, - Module = parentModule, - Name = info.Name, - NameId = attribute.NameId, - Parameters = param, - RequiredParameters = required - }; + commands.Add(new() + { + Method = info, + Module = parentModule, + Name = info.Name, + NameId = attribute.NameId, + Parameters = param, + RequiredParameters = required + }); + } + + return commands.ToArray(); } public void Invoke(params string[] args) diff --git a/SrcMod/Shell/Modules/ObjectModels/ModuleInfo.cs b/SrcMod/Shell/Modules/ObjectModels/ModuleInfo.cs index 1200c83..ce9b58f 100644 --- a/SrcMod/Shell/Modules/ObjectModels/ModuleInfo.cs +++ b/SrcMod/Shell/Modules/ObjectModels/ModuleInfo.cs @@ -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(); if (attribute is null) return null; @@ -37,9 +37,9 @@ public class ModuleInfo List 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); diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 1d9464d..1a1479d 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -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); From 8170a86247b72222003951175e06161dd74b9834 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 08:49:11 -0400 Subject: [PATCH 2/3] Fixed a loading bar issue (#19) --- SrcMod/Shell/Modules/BaseModule.cs | 15 +++++++++++++++ SrcMod/Shell/Shell.cs | 2 +- SrcMod/Shell/Tools.cs | 19 ++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 9e18192..2b6ad17 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -342,6 +342,21 @@ public static class BaseModule }); } + [Command("testing")] + public static void Testing() + { + LoadingBarStart(); + int count = 0; + for (float f = 0; f <= 1; f += 0.01f) + { + LoadingBarSet(f); + count++; + if (count % 10 == 0) Write("wowie!"); + Thread.Sleep(15); + } + LoadingBarEnd(); + } + [Command("exit")] [Command("quit")] public static void QuitShell(int code = 0) diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 1a1479d..0ea44b3 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -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.2.1"; + public const string Version = "Alpha 0.2.2"; public readonly string? ShellDirectory; diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 0bbeb15..3fea015 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -7,6 +7,8 @@ public static class Tools private static int loadingPosition = -1; private static int lastLoadingBufferSize = 0; private static int lastLoadingValue = -1; + private static float loadingBarValue = 0; + private static ConsoleColor loadingBarColor = Console.ForegroundColor; public static bool LoadingBarEnabled { get; private set; } @@ -101,6 +103,9 @@ public static class Tools Int2 oldPos = (Console.CursorLeft, Console.CursorTop); + loadingBarValue = value; + loadingBarColor = color ?? Console.ForegroundColor; + // Erase last bar. Console.SetCursorPosition(0, loadingPosition); Console.Write(new string(' ', lastLoadingBufferSize)); @@ -119,14 +124,20 @@ public static class Tools Write(right, newLine: false); if (oldPos.y == Console.CursorTop) oldPos.y++; + while (oldPos.y >= Console.BufferHeight) + { + Console.WriteLine(); + oldPos.y--; + loadingPosition--; + } Console.SetCursorPosition(oldPos.x, oldPos.y); } public static void LoadingBarStart(float value = 0, int? position = null, ConsoleColor? color = null) { if (loadingPosition != -1) throw new("The loading bar has already been enabled."); loadingPosition = position ?? Console.CursorTop; - LoadingBarSet(value, color); LoadingBarEnabled = true; + LoadingBarSet(value, color); } public static void Write(object? message, ConsoleColor? col = null, bool newLine = true) @@ -138,6 +149,12 @@ public static class Tools else Console.Write(message); Console.ForegroundColor = prevCol; + + if (newLine && LoadingBarEnabled && Console.CursorTop >= Console.BufferHeight - 1) + { + loadingPosition--; + LoadingBarSet(loadingBarValue, loadingBarColor); + } } public static bool ValidateUnsafe() From 0ebecf42342074e6cecffe965c2de2e51b9411d4 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 09:08:32 -0400 Subject: [PATCH 3/3] Forgot to remove the testing command. --- SrcMod/Shell/Modules/BaseModule.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 2b6ad17..9e18192 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -342,21 +342,6 @@ public static class BaseModule }); } - [Command("testing")] - public static void Testing() - { - LoadingBarStart(); - int count = 0; - for (float f = 0; f <= 1; f += 0.01f) - { - LoadingBarSet(f); - count++; - if (count % 10 == 0) Write("wowie!"); - Thread.Sleep(15); - } - LoadingBarEnd(); - } - [Command("exit")] [Command("quit")] public static void QuitShell(int code = 0)