using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ShiftOS.Main.Apps; namespace ShiftOS.Main.Terminal { public static class TerminalBackend { // The line below gets all the terminal commands in... well... the entire ShiftOS.Engine public static IEnumerable instances = Assembly.GetExecutingAssembly().GetTypes() .Where(t => t.IsSubclassOf(typeof(TerminalCommand)) && t.GetConstructor(Type.EmptyTypes) != null) .Select(t => Activator.CreateInstance(t) as TerminalCommand); public static List trm = new List(); public static int trmTopID = 0; public static Stack commandBuffer = new Stack(); /// /// Runs a terminal command. /// /// /// The rich text box that the text will be written to. public static void RunCommand(string command, int TermID) { string name; try { name = command.Split(' ')[0]; } catch { name = command; } var theParams = new string[command.Split(' ').Length - 1]; Array.Copy(command.Split(' '), 1, theParams, 0, command.Split(' ').Length - 1); bool complete = false; foreach (TerminalCommand instance in instances) { if (instance.Name.ToLower() == name.ToLower()) { instance.TermID = TermID; // Add a new line! Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText("\n"); instance.Run(theParams); complete = true; commandBuffer.Push(command); return; } if (instance.Name.ToLower() == name.ToLower() && instance.Unlocked == false) { Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"\n sbash: invalid command: {command.Split(' ').First()}"); return; } } if (command.Length == 0) return; if (!complete) { Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"\n sbash: invalid command: {command.Split(' ').First()}"); return; } } } }