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.

341 lines
16 KiB

9 months ago
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Net.Http;
  7. using System.ServiceProcess;
  8. using System.Threading;
  9. using amecs.Actions;
  10. using Ameliorated.ConsoleUtils;
  11. namespace amecs.Extra
  12. {
  13. public static partial class Extra
  14. {
  15. public static bool ShowMenu()
  16. {
  17. while (true)
  18. {
  19. Program.Frame.Clear();
  20. bool hibernation = new Reg.Value()
  21. {
  22. KeyName = @"HKLM\SYSTEM\CurrentControlSet\Control\Power",
  23. ValueName = "HibernateEnabled",
  24. Data = 1,
  25. }.IsEqual()
  26. &&
  27. new Reg.Value()
  28. {
  29. KeyName = @"HKLM\SYSTEM\CurrentControlSet\Control\Power",
  30. ValueName = "HiberFileType",
  31. Data = 2,
  32. }.IsEqual();
  33. bool notifications = new Reg.Value()
  34. {
  35. KeyName = "HKU\\" + Globals.UserSID + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\PushNotifications",
  36. ValueName = "ToastEnabled",
  37. Data = 1,
  38. }.IsEqual();
  39. bool notificationCenter = !new Reg.Value()
  40. {
  41. KeyName = "HKU\\" + Globals.UserSID + @"\SOFTWARE\Policies\Microsoft\Windows\Explorer",
  42. ValueName = "DisableNotificationCenter",
  43. Data = 1,
  44. }.IsEqual();
  45. bool vbsEnabled = new Reg.Value()
  46. {
  47. KeyName = @"HKCR\.vbs",
  48. ValueName = "",
  49. Data = "VBSFile",
  50. Type = Reg.RegistryValueType.REG_SZ
  51. }.IsEqual();
  52. bool ncsiEnabled = new Reg.Value()
  53. {
  54. KeyName = @"HKLM\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet",
  55. ValueName = "EnableActiveProbing",
  56. Data = 1,
  57. }.IsEqual();
  58. var mainMenu = new Ameliorated.ConsoleUtils.Menu()
  59. {
  60. Choices =
  61. {
  62. new Menu.MenuItem("Manage WSL", null) {IsEnabled = false, SecondaryText = "[Not Supported]", PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red},
  63. hibernation ?
  64. new Menu.MenuItem("Disable Hibernation", new Func<bool>(DisableHibernation)) :
  65. new Menu.MenuItem("Enable Hibernation", new Func<bool>(EnableHibernation)),
  66. notificationCenter ?
  67. new Menu.MenuItem("Disable Notification Center", new Func<bool>(DisableNotifCen)) :
  68. new Menu.MenuItem("Enable Notification Center", new Func<bool>(EnableNotifCen)),
  69. notifications ?
  70. new Menu.MenuItem("Disable Desktop Notifications", new Func<bool>(DisableNotifications)) :
  71. new Menu.MenuItem("Enable Desktop Notifications", new Func<bool>(EnableNotifications)),
  72. GetWSHItem(),
  73. vbsEnabled ?
  74. new Menu.MenuItem("Disable Visual Basic Script [VBS] (Legacy)", new Func<bool>(DisableVBS)) :
  75. new Menu.MenuItem("Enable Visual Basic Script [VBS] (Legacy)", new Func<bool>(EnableVBS)),
  76. ncsiEnabled ?
  77. new Menu.MenuItem("Disable NCSI Active Probing (Legacy)", new Func<bool>(DisableNCSI)) :
  78. new Menu.MenuItem("Enable NCSI Active Probing (Legacy)", new Func<bool>(EnableNCSI)),
  79. GetNVCPItem(),
  80. Menu.MenuItem.Blank,
  81. new Menu.MenuItem("Return to Menu", null),
  82. new Menu.MenuItem("Exit", new Func<bool>(Globals.Exit))
  83. },
  84. SelectionForeground = ConsoleColor.Green
  85. };
  86. Func<bool> result;
  87. try
  88. {
  89. mainMenu.Write();
  90. var res = mainMenu.Load();
  91. if (res == null)
  92. return true;
  93. result = (Func<bool>)res;
  94. } catch (Exception e)
  95. {
  96. Console.WriteLine(e);
  97. Console.ReadLine();
  98. return false;
  99. }
  100. try
  101. {
  102. result.Invoke();
  103. } catch (Exception e)
  104. {
  105. ConsoleTUI.ShowErrorBox("Error while running an action: " + e.ToString(), null);
  106. }
  107. }
  108. }
  109. private static Menu.MenuItem GetNVCPItem()
  110. {
  111. if (File.Exists(Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\NVIDIA Control Panel\nvcplui.exe")))
  112. return new Menu.MenuItem("Uninstall NVIDIA Control Panel", new Func<bool>(NVCP.Uninstall));
  113. if (Globals.WinVer > 19043)
  114. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[Not Supported]"};
  115. try
  116. {
  117. ManagementObjectSearcher searcher =
  118. new ManagementObjectSearcher("SELECT NAME FROM Win32_VideoController");
  119. bool foundGPU = false;
  120. foreach (ManagementObject mo in searcher.Get())
  121. {
  122. PropertyData Name = mo.Properties["Name"];
  123. if (Name.Value != null)
  124. {
  125. var name = ((string)Name.Value);
  126. if (name.Contains("NVIDIA") || name.Contains("GeForce") || name.Contains("GTX") || name.Contains("RTX"))
  127. {
  128. foundGPU = true;
  129. break;
  130. }
  131. }
  132. }
  133. if (!foundGPU)
  134. {
  135. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[No NVIDIA GPU]"};
  136. }
  137. } catch {}
  138. try
  139. {
  140. if (!ServiceController.GetServices().Any(x => x.ServiceName.Equals("NVDisplay.ContainerLocalSystem")))
  141. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[No NVIDIA Driver]"};
  142. } catch { }
  143. try
  144. {
  145. var dir = Directory.EnumerateDirectories(Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\WindowsApps")).First(x => x.Contains("NVIDIACorp.NVIDIAControlPanel"));
  146. if (File.Exists(Path.Combine(dir, "nvcplui.exe")))
  147. return new Menu.MenuItem("Install NVIDIA Control Panel", new Func<bool>(() => NVCP.Install(dir)));
  148. } catch { }
  149. if (!amecs.IsInternetAvailable())
  150. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[Internet Required]"};
  151. if (!amecs.InternetCheckConnection("https://store.rg-adguard.net", 1, 0))
  152. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[Server Unavailable]"};
  153. if (!amecs.InternetCheckConnection("https://git.ameliorated.info/", 1, 0))
  154. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[Git Unavailable]"};
  155. try
  156. {
  157. using (HttpClient client = new HttpClient())
  158. {
  159. using (HttpResponseMessage response = client.GetAsync("https://store.rg-adguard.net/").Result)
  160. {
  161. using (HttpContent content = response.Content)
  162. {
  163. string result = content.ReadAsStringAsync().Result;
  164. if (result.Contains("Cloudflare Ray ID"))
  165. return new Menu.MenuItem("Install NVIDIA Control Panel", null) {IsEnabled = false, PrimaryTextForeground = ConsoleColor.DarkGray, SecondaryTextForeground = ConsoleColor.Red, SecondaryText = "[Server Unavailable]"};
  166. }
  167. }
  168. }
  169. } catch { }
  170. return new Menu.MenuItem("Install NVIDIA Control Panel", new Func<bool>(NVCP.InstallFromNetwork));
  171. }
  172. private static Menu.MenuItem GetWSHItem()
  173. {
  174. if (new Reg.Value()
  175. {
  176. KeyName = Globals.UserHive + @"\SOFTWARE\Microsoft\Windows Script Host\Settings",
  177. ValueName = "Enabled",
  178. Data = 1,
  179. }.IsEqual())
  180. return new Menu.MenuItem("Disable Windows Script Host [WSH] (Legacy)", new Func<bool>(WSH.Disable));
  181. if (new Reg.Value()
  182. {
  183. KeyName = Globals.UserHive + @"\SOFTWARE\Microsoft\Windows Script Host\Settings",
  184. ValueName = "Enabled",
  185. Data = 0,
  186. }.IsEqual())
  187. return new Menu.MenuItem("Enable Windows Script Host [WSH] (Legacy)", new Func<bool>(WSH.Enable));
  188. return new Reg.Value()
  189. {
  190. KeyName = @"HKLM\SOFTWARE\Microsoft\Windows Script Host\Settings",
  191. ValueName = "Enabled",
  192. Data = 0,
  193. }.IsEqual() ? new Menu.MenuItem("Disable Windows Script Host [WSH] (Legacy)", new Func<bool>(WSH.Enable)) : new Menu.MenuItem("Disable Windows Script Host [WSH] (Legacy)", new Func<bool>(WSH.Disable));
  194. }
  195. private static bool EnableHibernation() =>amecs.RunBasicAction("Enabling hibernation","Enabled hibernation successfully",() =>
  196. {
  197. Thread.Sleep(1600);
  198. Process proc = new Process();
  199. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  200. proc.StartInfo.FileName = "powercfg.exe";
  201. proc.StartInfo.Arguments = "/HIBERNATE /TYPE FULL";
  202. proc.Start();
  203. proc.WaitForExit(20000);
  204. if (proc.ExitCode != 0)
  205. throw new Exception("powercfg exited with a non-zero exitcode.\r\nHibernation may not be supported by your hardware.");
  206. });
  207. private static bool DisableHibernation() =>amecs.RunBasicAction("Disabling hibernation","Disabled hibernation successfully",() =>
  208. {
  209. Thread.Sleep(1600);
  210. Process proc = new Process();
  211. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  212. proc.StartInfo.FileName = "powercfg.exe";
  213. proc.StartInfo.Arguments = "/HIBERNATE OFF";
  214. proc.Start();
  215. proc.WaitForExit(20000);
  216. if (proc.ExitCode != 0)
  217. throw new Exception("powercfg exited with a non-zero exitcode.");
  218. Thread.Sleep(1600);
  219. });
  220. private static bool EnableNotifCen() =>amecs.RunBasicAction("Enabling Notification Center","Notification Center enabled successfully",() =>
  221. {
  222. new Reg.Value()
  223. {
  224. KeyName = "HKU\\" + Globals.UserSID + @"\SOFTWARE\Policies\Microsoft\Windows\Explorer",
  225. ValueName = "DisableNotificationCenter",
  226. Operation = Reg.RegistryValueOperation.Delete
  227. }.Apply();
  228. Thread.Sleep(1600);
  229. }, true);
  230. private static bool DisableNotifCen() =>amecs.RunBasicAction("Disabling Notification Center","Notification Center disabled successfully",() =>
  231. {
  232. new Reg.Value()
  233. {
  234. KeyName = "HKU\\" + Globals.UserSID + @"\SOFTWARE\Policies\Microsoft\Windows\Explorer",
  235. ValueName = "DisableNotificationCenter",
  236. Data = 1,
  237. }.Apply();
  238. Thread.Sleep(1600);
  239. }, true);
  240. private static bool EnableNotifications() =>amecs.RunBasicAction("Enabling desktop notifications","Enabled desktop notifications successfully",() =>
  241. {
  242. new Reg.Value()
  243. {
  244. KeyName = "HKU\\" + Globals.UserSID + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\PushNotifications",
  245. ValueName = "ToastEnabled",
  246. Data = 1,
  247. }.Apply();
  248. Thread.Sleep(1600);
  249. }, true);
  250. private static bool DisableNotifications() =>amecs.RunBasicAction("Disabling desktop notifications","Disabled desktop notifications successfully",() =>
  251. {
  252. new Reg.Value()
  253. {
  254. KeyName = "HKU\\" + Globals.UserSID + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\PushNotifications",
  255. ValueName = "ToastEnabled",
  256. Data = 0,
  257. }.Apply();
  258. Thread.Sleep(1600);
  259. }, true);
  260. private static bool EnableVBS() =>amecs.RunBasicAction("Enabling Visual Basic Script","Enabled VBS successfully",() =>
  261. {
  262. new Reg.Value()
  263. {
  264. KeyName = @"HKCR\.vbs",
  265. ValueName = "",
  266. Data = "VBSFile",
  267. Type = Reg.RegistryValueType.REG_SZ
  268. }.Apply();
  269. Thread.Sleep(1600);
  270. });
  271. private static bool DisableVBS() =>amecs.RunBasicAction("Disabling Visual Basic Script","Disabled VBS successfully",() =>
  272. {
  273. new Reg.Value()
  274. {
  275. KeyName = @"HKCR\.vbs",
  276. ValueName = "",
  277. Data = "",
  278. Type = Reg.RegistryValueType.REG_SZ
  279. }.Apply();
  280. Thread.Sleep(1600);
  281. });
  282. private static bool EnableNCSI() =>amecs.RunBasicAction("Enabling NCSI Active Probing","Enabled NCSI Active Probing successfully",() =>
  283. {
  284. new Reg.Value()
  285. {
  286. KeyName = @"HKLM\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet",
  287. ValueName = "EnableActiveProbing",
  288. Data = 1,
  289. }.Apply();
  290. Thread.Sleep(1600);
  291. }, false, true);
  292. private static bool DisableNCSI() =>amecs.RunBasicAction("Disabling NCSI Active Probing","Disabled NCSI Active Probing successfully",() =>
  293. {
  294. new Reg.Value()
  295. {
  296. KeyName = @"HKLM\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet",
  297. ValueName = "EnableActiveProbing",
  298. Data = 0,
  299. }.Apply();
  300. Thread.Sleep(1600);
  301. }, false, true);
  302. }
  303. }