Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
9.2 KiB

9 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Runtime.InteropServices;
  7. using System.Threading.Tasks;
  8. using Ameliorated.ConsoleUtils;
  9. namespace amecs
  10. {
  11. public class amecs
  12. {
  13. public static bool RunBasicAction(string status, string result, Action action, bool logoff = false, bool restart = false)
  14. {
  15. ConsoleTUI.OpenFrame.WriteCentered(status);
  16. try
  17. {
  18. using (new ConsoleUtils.LoadingIndicator(true))
  19. {
  20. action.Invoke();
  21. }
  22. } catch (Exception e)
  23. {
  24. Console.WriteLine();
  25. ConsoleTUI.OpenFrame.Close("Error: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " });
  26. return false;
  27. }
  28. Console.WriteLine();
  29. if (logoff)
  30. {
  31. if ((int?)ConsoleTUI.OpenFrame.Close(result, ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt()
  32. {
  33. TextForeground = ConsoleColor.Yellow,
  34. Text = "Logoff to apply changes? (Y/N): "
  35. }) == 0) amecs.RestartWindows(true);
  36. return true;
  37. }
  38. if (restart)
  39. {
  40. if ((int?)ConsoleTUI.OpenFrame.Close(result, ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt()
  41. {
  42. TextForeground = ConsoleColor.Yellow,
  43. Text = "Restart to apply changes? (Y/N): "
  44. }) == 0) amecs.RestartWindows(false);
  45. return true;
  46. }
  47. ConsoleTUI.OpenFrame.Close(result, ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  48. return true;
  49. }
  50. public static bool RunBasicAction(string status, string result, Action<int> action, ChoicePrompt prompt = null, bool logoff = false, bool restart = false)
  51. {
  52. int choice = -1;
  53. if (prompt != null)
  54. {
  55. var choiceRes = prompt.Start();
  56. if (!choiceRes.HasValue)
  57. return true;
  58. choice = choiceRes.Value;
  59. }
  60. ConsoleTUI.OpenFrame.WriteCentered(status);
  61. try
  62. {
  63. using (new ConsoleUtils.LoadingIndicator(true))
  64. {
  65. action.Invoke(choice);
  66. }
  67. } catch (Exception e)
  68. {
  69. Console.WriteLine();
  70. ConsoleTUI.OpenFrame.Close("Error: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " });
  71. return false;
  72. }
  73. Console.WriteLine();
  74. if (logoff)
  75. {
  76. if ((int?)ConsoleTUI.OpenFrame.Close(result, ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt()
  77. {
  78. TextForeground = ConsoleColor.Yellow,
  79. Text = "Logoff to apply changes? (Y/N): "
  80. }) == 0) amecs.RestartWindows(true);
  81. return true;
  82. }
  83. if (restart)
  84. {
  85. if ((int?)ConsoleTUI.OpenFrame.Close(result, ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt()
  86. {
  87. TextForeground = ConsoleColor.Yellow,
  88. Text = "Restart to apply changes? (Y/N): "
  89. }) == 0) amecs.RestartWindows(false);
  90. return true;
  91. }
  92. ConsoleTUI.OpenFrame.Close(result, ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  93. return true;
  94. }
  95. [Flags]
  96. enum ExitWindows : uint
  97. {
  98. // ONE of the following five:
  99. LogOff = 0x00,
  100. ShutDown = 0x01,
  101. Reboot = 0x02,
  102. PowerOff = 0x08,
  103. RestartApps = 0x40,
  104. // plus AT MOST ONE of the following two:
  105. Force = 0x04,
  106. ForceIfHung = 0x10,
  107. }
  108. [Flags]
  109. enum ShutdownReason : uint
  110. {
  111. MajorApplication = 0x00040000,
  112. MajorHardware = 0x00010000,
  113. MajorLegacyApi = 0x00070000,
  114. MajorOperatingSystem = 0x00020000,
  115. MajorOther = 0x00000000,
  116. MajorPower = 0x00060000,
  117. MajorSoftware = 0x00030000,
  118. MajorSystem = 0x00050000,
  119. MinorBlueScreen = 0x0000000F,
  120. MinorCordUnplugged = 0x0000000b,
  121. MinorDisk = 0x00000007,
  122. MinorEnvironment = 0x0000000c,
  123. MinorHardwareDriver = 0x0000000d,
  124. MinorHotfix = 0x00000011,
  125. MinorHung = 0x00000005,
  126. MinorInstallation = 0x00000002,
  127. MinorMaintenance = 0x00000001,
  128. MinorMMC = 0x00000019,
  129. MinorNetworkConnectivity = 0x00000014,
  130. MinorNetworkCard = 0x00000009,
  131. MinorOther = 0x00000000,
  132. MinorOtherDriver = 0x0000000e,
  133. MinorPowerSupply = 0x0000000a,
  134. MinorProcessor = 0x00000008,
  135. MinorReconfig = 0x00000004,
  136. MinorSecurity = 0x00000013,
  137. MinorSecurityFix = 0x00000012,
  138. MinorSecurityFixUninstall = 0x00000018,
  139. MinorServicePack = 0x00000010,
  140. MinorServicePackUninstall = 0x00000016,
  141. MinorTermSrv = 0x00000020,
  142. MinorUnstable = 0x00000006,
  143. MinorUpgrade = 0x00000003,
  144. MinorWMI = 0x00000015,
  145. FlagUserDefined = 0x40000000,
  146. FlagPlanned = 0x80000000
  147. }
  148. [DllImport("user32.dll")]
  149. static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
  150. [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  151. private static extern bool InitiateSystemShutdownEx(
  152. string lpMachineName,
  153. string lpMessage,
  154. uint dwTimeout,
  155. bool bForceAppsClosed,
  156. bool bRebootAfterShutdown,
  157. ShutdownReason dwReason);
  158. public static void RestartWindows(bool logoff, bool cmd = false)
  159. {
  160. if (cmd)
  161. {
  162. var psi = new ProcessStartInfo("shutdown","/r /t 0");
  163. psi.CreateNoWindow = true;
  164. psi.UseShellExecute = false;
  165. Process.Start(psi);
  166. Environment.Exit(0);
  167. }
  168. if (!logoff)
  169. {
  170. NSudo.GetShutdownPrivilege();
  171. InitiateSystemShutdownEx(null, null, 0, false, true, ShutdownReason.MinorInstallation);
  172. } else
  173. ExitWindowsEx((uint)ExitWindows.LogOff | (uint)ExitWindows.ForceIfHung, (uint)ShutdownReason.MinorInstallation);
  174. Environment.Exit(0);
  175. }
  176. public static object ShowDefaultMenu(List<Menu.MenuItem> list)
  177. {
  178. list.AddRange(new [] {
  179. Menu.MenuItem.BlankStatic,
  180. new Menu.MenuItem("Next Page", null) { IsNextButton = true },
  181. new Menu.MenuItem("Previous Page", null) { IsPreviousButton = true },
  182. new Menu.MenuItem("Return to Menu", new Func<bool>(() => true)) { IsStatic = true },
  183. new Menu.MenuItem("Exit", new Func<bool>(Globals.Exit)) { IsStatic = true },
  184. });
  185. var mainMenu = new Menu()
  186. {
  187. Choices = list,
  188. SelectionForeground = ConsoleColor.Green
  189. };
  190. mainMenu.Write();
  191. return mainMenu.Load();
  192. }
  193. [DllImport("wininet.dll", SetLastError = true)]
  194. public static extern bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);
  195. [DllImport("wininet.dll", SetLastError=true)]
  196. private extern static bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);
  197. public static bool IsInternetAvailable()
  198. {
  199. try
  200. {
  201. try
  202. {
  203. if (!InternetCheckConnection("http://archlinux.org", 1, 0))
  204. {
  205. if (!InternetCheckConnection("http://google.com", 1, 0))
  206. return false;
  207. }
  208. return true;
  209. }
  210. catch
  211. {
  212. var request = (HttpWebRequest)WebRequest.Create("http://google.com");
  213. request.KeepAlive = false;
  214. request.Timeout = 5000;
  215. using (var response = (HttpWebResponse)request.GetResponse())
  216. return true;
  217. }
  218. }
  219. catch
  220. {
  221. return false;
  222. }
  223. }
  224. }
  225. }