diff options
Diffstat (limited to 'ShiftOS.WinForms/Commands.cs')
| -rw-r--r-- | ShiftOS.WinForms/Commands.cs | 615 |
1 files changed, 394 insertions, 221 deletions
diff --git a/ShiftOS.WinForms/Commands.cs b/ShiftOS.WinForms/Commands.cs index 2d297f5..23b8f19 100644 --- a/ShiftOS.WinForms/Commands.cs +++ b/ShiftOS.WinForms/Commands.cs @@ -22,279 +22,452 @@ * SOFTWARE. */ +#define DEVEL + using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; -using ShiftOS.Engine; +using System.Windows.Forms; +using ShiftOS.Engine.Properties; using System.IO; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Threading; using Newtonsoft.Json; +using System.IO.Compression; -/// <summary> -/// Coherence commands. -/// </summary> -namespace ShiftOS.WinForms +using ShiftOS.Objects; +using ShiftOS.Engine.Scripting; +using ShiftOS.Objects.ShiftFS; + +namespace ShiftOS.Engine { - [Namespace("trm")] - public static class TerminalExtensions + [TutorialLock] + public static class TerminalCommands + { + [Command("clear", description = "{DESC_CLEAR}")] + public static bool Clear() + { + AppearanceManager.ConsoleOut.Clear(); + return true; + } + } + + public static class ShiftOSCommands { - [Command("exit")] - public static bool StopRemoting() + + [Command("setsfxenabled", description = "{DESC_SETSFXENABLED}")] + [RequiresArgument("value")] + public static bool SetSfxEnabled(Dictionary<string, object> args) { - if(TerminalBackend.IsForwardingConsoleWrites == true) + try + { + bool value = Convert.ToBoolean(args["value"].ToString()); + SaveSystem.CurrentSave.SoundEnabled = value; + SaveSystem.SaveGame(); + } + catch { - ServerManager.SendMessage("trm_handshake_stop", $@"{{ - guid: ""{TerminalBackend.ForwardGUID}"" -}}"); - Console.WriteLine("Goodbye!"); + Console.WriteLine("{ERR_BADBOOL}"); + } + return true; + } + + + + [Command("setmusicenabled", description = "{DESC_SETMUSICENABLED}")] + [RequiresArgument("value")] + public static bool SetMusicEnabled(Dictionary<string, object> args) + { + try + { + bool value = Convert.ToBoolean(args["value"].ToString()); + SaveSystem.CurrentSave.MusicEnabled = value; + SaveSystem.SaveGame(); + } + catch + { + Console.WriteLine("{ERR_BADBOOL}"); + } + return true; + } + + + + [Command("setvolume", description ="{DESC_SETVOLUME}")] + [RequiresArgument("value")] + public static bool SetSfxVolume(Dictionary<string, object> args) + { + int value = int.Parse(args["value"].ToString()); + if(value >= 0 && value <= 100) + { + SaveSystem.CurrentSave.MusicVolume = value; + SaveSystem.SaveGame(); } else { + Console.WriteLine("{ERR_BADPERCENT}"); + } + return true; + } + + [RemoteLock] + [Command("shutdown", description = "{DESC_SHUTDOWN}")] + public static bool Shutdown() + { + SaveSystem.SaveGame(); + AppearanceManager.Exit(); + return true; + } + + [Command("lang", description = "{DESC_LANG}")] + [RequiresArgument("language")] + public static bool SetLanguage(Dictionary<string, object> userArgs) + { + try + { + string lang = ""; + + lang = (string)userArgs["language"]; + + if (Localization.GetAllLanguages().Contains(lang)) + { + SaveSystem.CurrentSave.Language = lang; + SaveSystem.SaveGame(); + Console.WriteLine("{RES_LANGUAGE_CHANGED}"); + return true; + } + + throw new Exception("{ERR_NOLANG}"); + } + catch + { return false; } + } + [Command("commands", "", "{DESC_COMMANDS}")] + public static bool Commands() + { + var sb = new StringBuilder(); + sb.AppendLine("{GEN_COMMANDS}"); + sb.AppendLine("================="); + sb.AppendLine(); + //print all unique namespaces. + foreach (var n in TerminalBackend.Commands.Where(x => !(x is TerminalBackend.WinOpenCommand) && Shiftorium.UpgradeInstalled(x.Dependencies) && x.CommandInfo.hide == false).OrderBy(x => x.CommandInfo.name)) + { + sb.Append(n.CommandInfo.name); + if (!string.IsNullOrWhiteSpace(n.CommandInfo.description)) + if (Shiftorium.UpgradeInstalled("help_description")) + sb.Append(" - " + n.CommandInfo.description); + sb.AppendLine(); + } + + Console.WriteLine(sb.ToString()); + + return true; + } + + [Command("help", description = "{DESC_HELP}")] + public static bool Help() + { + Commands(); + WindowCommands.Programs(); return true; } - [Command("setpass", true)] - [RequiresArgument("pass")] - public static bool setPass(Dictionary<string, object> args) + [MultiplayerOnly] + [Command("save", description = "{DESC_SAVE}")] + public static bool Save() { - SaveSystem.CurrentSave.Password = args["pass"] as string; + SaveSystem.SaveGame(); return true; } - [Command("remote", "username:,sysname:,password:", "Allows you to control a remote system on the multi-user domain given a username, password and system name.")] - [RequiresArgument("username")] - [RequiresArgument("sysname")] - [RequiresArgument("password")] - public static bool RemoteControl(Dictionary<string, object> args) + [MultiplayerOnly] + [Command("status", description = "{DESC_STATUS}")] + public static bool Status() { - ServerManager.SendMessage("trm_handshake_request", JsonConvert.SerializeObject(args)); + string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); + + string cp = SaveSystem.CurrentSave.Codepoints.ToString(); + string installed = SaveSystem.CurrentSave.CountUpgrades().ToString(); + string available = Shiftorium.GetAvailable().Length.ToString(); + + Console.WriteLine(Localization.Parse("{COM_STATUS}", new Dictionary<string, string> + { + ["%cp"] = cp, + ["%version"] = version, + ["%installed"] = installed, + ["%available"] = available + })); + Console.WriteLine("{GEN_OBJECTIVES}"); + try + { + if (Story.CurrentObjectives.Count > 0) + { + foreach (var obj in Story.CurrentObjectives) + { + Console.WriteLine(obj.Name); + Console.WriteLine("-------------------------------"); + Console.WriteLine(); + Console.WriteLine(obj.Description); + } + } + else + { + Console.WriteLine("{RES_NOOBJECTIVES}"); + } + } + catch + { + Console.WriteLine("{RES_NOOBJECTIVES}"); + } return true; } } - - [Namespace("coherence")] - [RequiresUpgrade("kernel_coherence")] - public static class CoherenceCommands + [MultiplayerOnly] + public static class ShiftoriumCommands { - /// <summary> - /// Sets the window position. - /// </summary> - /// <returns>The window position.</returns> - /// <param name="hWnd">H window.</param> - /// <param name="hWndInsertAfter">H window insert after.</param> - /// <param name="X">X.</param> - /// <param name="Y">Y.</param> - /// <param name="cx">Cx.</param> - /// <param name="cy">Cy.</param> - /// <param name="uFlags">U flags.</param> - [DllImport("user32.dll")] - static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); - - /// <summary> - /// The HWN d TOPMOS. - /// </summary> - static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); - - /// <summary> - /// The SW p SHOWWINDO. - /// </summary> - const UInt32 SWP_SHOWWINDOW = 0x0040; + [Command("buy", description = "{DESC_BUY}")] + [RequiresArgument("upgrade")] + public static bool BuyUpgrade(Dictionary<string, object> userArgs) + { + try + { + string upgrade = ""; - - [DllImport("user32.dll")] - /// <summary> - /// Gets the window rect. - /// </summary> - /// <returns>The window rect.</returns> - /// <param name="hWnd">H window.</param> - /// <param name="lpRect">Lp rect.</param> - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); - - /// <summary> - /// REC. - /// </summary> - [StructLayout(LayoutKind.Sequential)] - public struct RECT + upgrade = (string)userArgs["upgrade"]; + + var upg = Shiftorium.GetAvailable().FirstOrDefault(x => x.ID == upgrade); + if(upg != null) + { + if (Shiftorium.Buy(upg.ID, upg.Cost) == true) + Console.WriteLine("{RES_UPGRADEINSTALLED}"); + else + Console.WriteLine("{ERR_NOTENOUGHCODEPOINTS}"); + } + else + { + Console.WriteLine("{ERR_NOUPGRADE}"); + } + + } + catch + { + Console.WriteLine("{ERR_GENERAL}"); + } + return true; + } + + [RequiresUpgrade("shiftorium_bulk_buy")] + [Command("bulkbuy", description = "{DESC_BULKBUY}")] + [RequiresArgument("upgrades")] + public static bool BuyBulk(Dictionary<string, object> args) { - public int Left; // x position of upper-left corner - public int Top; // y position of upper-left corner - public int Right; // x position of lower-right corner - public int Bottom; // y position of lower-right corner + if (args.ContainsKey("upgrades")) + { + string[] upgrade_list = (args["upgrades"] as string).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); + foreach (var upg in upgrade_list) + { + var dict = new Dictionary<string, object>(); + dict.Add("upgrade", upg); + BuyUpgrade(dict); + } + } + return true; + } + + + [Command("upgradeinfo", description ="{DESC_UPGRADEINFO}")] + [RequiresArgument("upgrade")] + public static bool ViewInfo(Dictionary<string, object> userArgs) + { + try + { + string upgrade = ""; + + upgrade = (string)userArgs["upgrade"]; + + foreach (var upg in Shiftorium.GetDefaults()) + { + if (upg.ID == upgrade) + { + Console.WriteLine(Localization.Parse("{COM_UPGRADEINFO}", new Dictionary<string, string> + { + ["%id"] = upg.ID, + ["%category"] = upg.Category, + ["%name"] = upg.Name, + ["%cost"] = upg.Cost.ToString(), + ["%description"] = upg.Description + })); + + return true; + } + } + + throw new Exception("{ERR_NOUPGRADE}"); + } + catch + { + return false; + } } - [Command("launch", "process: \"C:\\path\\to\\process\" - The process path to launch.", "Launch a process inside kernel coherence.")] - [RequiresArgument("process")] - /// <summary> - /// Launchs the app. - /// </summary> - /// <returns>The app.</returns> - /// <param name="args">Arguments.</param> - public static bool LaunchApp(Dictionary<string, object> args) + [Command("upgradecategories", description = "{DESC_UPGRADECATEGORIES}")] + public static bool ListCategories() { - string process = args["process"].ToString(); - var prc = Process.Start(process); - StartCoherence(prc); + foreach(var cat in Shiftorium.GetCategories()) + { + Console.WriteLine(Localization.Parse("{SHFM_CATEGORY}", new Dictionary<string, string> + { + ["%name"] = cat, + ["%available"] = Shiftorium.GetAvailable().Where(x=>x.Category==cat).Count().ToString() + })); + } return true; } - /// <summary> - /// Starts the coherence. - /// </summary> - /// <returns>The coherence.</returns> - /// <param name="prc">Prc.</param> - private static void StartCoherence(Process prc) + [Command("upgrades", description ="{DESC_UPGRADES}")] + public static bool ListAll(Dictionary<string, object> args) { - RECT rct = new RECT(); - + try + { + bool showOnlyInCategory = false; + + string cat = "Other"; - while (!GetWindowRect(prc.MainWindowHandle, ref rct)) + if (args.ContainsKey("cat")) { + showOnlyInCategory = true; + cat = args["cat"].ToString(); } + Dictionary<string, ulong> upgrades = new Dictionary<string, ulong>(); + int maxLength = 5; - - AppearanceManager.Invoke(new Action(() => + IEnumerable<ShiftoriumUpgrade> upglist = Shiftorium.GetAvailable(); + if (showOnlyInCategory) { - IShiftOSWindow coherenceWindow = new Applications.CoherenceOverlay(prc.MainWindowHandle, rct); + if (Shiftorium.IsCategoryEmptied(cat)) + { + ConsoleEx.Bold = true; + ConsoleEx.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("{SHFM_QUERYERROR}"); + Console.WriteLine(); + ConsoleEx.Bold = false; + ConsoleEx.ForegroundColor = ConsoleColor.Gray; + Console.WriteLine("{ERR_EMPTYCATEGORY}"); + return true; + } + upglist = Shiftorium.GetAvailable().Where(x => x.Category == cat); + } - AppearanceManager.SetupWindow(coherenceWindow); - SetWindowPos(prc.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); - //MakeExternalWindowBorderless(prc.MainWindowHandle); - })); - + if(upglist.Count() == 0) + { + ConsoleEx.Bold = true; + ConsoleEx.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("{SHFM_NOUPGRADES}"); + Console.WriteLine(); + ConsoleEx.Bold = false; + ConsoleEx.ForegroundColor = ConsoleColor.Gray; + Console.WriteLine("{ERR_NOMOREUPGRADES}"); + return true; + + } + foreach (var upg in upglist) + { + if (upg.ID.Length > maxLength) + { + maxLength = upg.ID.Length; + } + + upgrades.Add(upg.ID, upg.Cost); + } + + foreach (var upg in upgrades) + { + Console.WriteLine(Localization.Parse("{SHFM_UPGRADE}", new Dictionary<string, string> + { + ["%id"] = upg.Key, + ["%cost"] = upg.Value.ToString() + })); + } + return true; + } + catch (Exception e) + { + CrashHandler.Start(e); + return false; + } } + } - /// <summary> - /// The W s BORDE. - /// </summary> - const int WS_BORDER = 8388608; - - /// <summary> - /// The W s DLGFRAM. - /// </summary> - const int WS_DLGFRAME = 4194304; - - /// <summary> - /// The W s CAPTIO. - /// </summary> - const int WS_CAPTION = WS_BORDER | WS_DLGFRAME; - - /// <summary> - /// The W s SYSMEN. - /// </summary> - const int WS_SYSMENU = 524288; - - /// <summary> - /// The W s THICKFRAM. - /// </summary> - const int WS_THICKFRAME = 262144; - - /// <summary> - /// The W s MINIMIZ. - /// </summary> - const int WS_MINIMIZE = 536870912; - - /// <summary> - /// The W s MAXIMIZEBO. - /// </summary> - const int WS_MAXIMIZEBOX = 65536; - - /// <summary> - /// The GW l STYL. - /// </summary> - const int GWL_STYLE = -16; - - /// <summary> - /// The GW l EXSTYL. - /// </summary> - const int GWL_EXSTYLE = -20; - - /// <summary> - /// The W s E x DLGMODALFRAM. - /// </summary> - const int WS_EX_DLGMODALFRAME = 0x1; - - /// <summary> - /// The SW p NOMOV. - /// </summary> - const int SWP_NOMOVE = 0x2; - - /// <summary> - /// The SW p NOSIZ. - /// </summary> - const int SWP_NOSIZE = 0x1; - - /// <summary> - /// The SW p FRAMECHANGE. - /// </summary> - const int SWP_FRAMECHANGED = 0x20; - - /// <summary> - /// The M f BYPOSITIO. - /// </summary> - const uint MF_BYPOSITION = 0x400; - - /// <summary> - /// The M f REMOV. - /// </summary> - const uint MF_REMOVE = 0x1000; - - /// <summary> - /// Gets the window long. - /// </summary> - /// <returns>The window long.</returns> - /// <param name="hWnd">H window.</param> - /// <param name="nIndex">N index.</param> - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern int GetWindowLong(IntPtr hWnd, int nIndex); - - /// <summary> - /// Sets the window long. - /// </summary> - /// <returns>The window long.</returns> - /// <param name="hWnd">H window.</param> - /// <param name="nIndex">N index.</param> - /// <param name="dwNewLong">Dw new long.</param> - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); - - /// <summary> - /// Sets the window position. - /// </summary> - /// <returns>The window position.</returns> - /// <param name="hWnd">H window.</param> - /// <param name="hWndInsertAfter">H window insert after.</param> - /// <param name="X">X.</param> - /// <param name="Y">Y.</param> - /// <param name="cx">Cx.</param> - /// <param name="cy">Cy.</param> - /// <param name="uFlags">U flags.</param> - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); - public static void MakeExternalWindowBorderless(IntPtr MainWindowHandle) + public static class WindowCommands + { + [RemoteLock] + [Command("processes", description = "{DESC_PROCESSES}")] + public static bool List() + { + Console.WriteLine("{GEN_CURRENTPROCESSES}"); + foreach (var app in AppearanceManager.OpenForms) + { + //Windows are displayed the order in which they were opened. + Console.WriteLine($"{AppearanceManager.OpenForms.IndexOf(app)}\t{app.Text}"); + } + return true; + } + + [Command("programs", description = "{DESC_PROGRAMS}")] + public static bool Programs() { - int Style = 0; - Style = GetWindowLong(MainWindowHandle, GWL_STYLE); - Style = Style & ~WS_CAPTION; - Style = Style & ~WS_SYSMENU; - Style = Style & ~WS_THICKFRAME; - Style = Style & ~WS_MINIMIZE; - Style = Style & ~WS_MAXIMIZEBOX; - SetWindowLong(MainWindowHandle, GWL_STYLE, Style); - Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE); - SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME); - SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); + Console.WriteLine("{GEN_PROGRAMS}"); + Console.WriteLine("==============="); + Console.WriteLine(); + foreach(var cmd in TerminalBackend.Commands.Where(x=>x is TerminalBackend.WinOpenCommand && Shiftorium.UpgradeInstalled(x.Dependencies)).OrderBy(x => x.CommandInfo.name)) + { + Console.Write(" - " + cmd.CommandInfo.name); + if (!string.IsNullOrWhiteSpace(cmd.CommandInfo.description)) + if (Shiftorium.UpgradeInstalled("help_description")) + Console.Write(": " + cmd.CommandInfo.description); + Console.WriteLine(); + } + return true; } + + [RemoteLock] + [Command("close", usage = "{win:integer32}", description ="{DESC_CLOSE}")] + [RequiresArgument("win")] + [RequiresUpgrade("close_command")] + public static bool CloseWindow(Dictionary<string, object> args) + { + int winNum = -1; + if (args.ContainsKey("win")) + winNum = Convert.ToInt32(args["win"].ToString()); + string err = null; + + if (winNum < 0 || winNum >= AppearanceManager.OpenForms.Count) + err = Localization.Parse("{ERR_BADWINID}", new Dictionary<string, string> + { + ["%max"] = (AppearanceManager.OpenForms.Count - 1).ToString() + }); + + if (string.IsNullOrEmpty(err)) + { + Console.WriteLine("{RES_WINDOWCLOSED}"); + AppearanceManager.Close(AppearanceManager.OpenForms[winNum].ParentWindow); + } + else + { + Console.WriteLine(err); + } + + return true; + } + } } |
