Program.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. namespace Optimizer
  9. {
  10. static class Program
  11. {
  12. /* VERSION PROPERTIES */
  13. /* DO NOT LEAVE THEM EMPTY */
  14. internal readonly static float Major = 12;
  15. internal readonly static float Minor = 1;
  16. internal readonly static bool EXPERIMENTAL_BUILD = false;
  17. internal static string GetCurrentVersionTostring()
  18. {
  19. return Major.ToString() + "." + Minor.ToString();
  20. }
  21. internal static float GetCurrentVersion()
  22. {
  23. return float.Parse(GetCurrentVersionTostring());
  24. }
  25. /* END OF VERSION PROPERTIES */
  26. // Enables the corresponding Windows tab for Windows Server machines
  27. internal static bool UNSAFE_MODE = false;
  28. const string _jsonAssembly = @"Optimizer.Newtonsoft.Json.dll";
  29. internal static MainForm _MainForm;
  30. internal static SplashForm _SplashForm;
  31. static string _adminMissingMessage = "Optimizer needs to be run as administrator!\nApp will now close...";
  32. static string _unsupportedMessage = "Optimizer works in Windows 7 or higher!\nApp will now close...";
  33. //static string _renameAppMessage = "It's recommended to rename the app from '{0}' to 'Optimizer' for a better experience.\n\nApp will now close...";
  34. static string _confInvalidVersionMsg = "Windows version does not match!";
  35. static string _confInvalidFormatMsg = "Config file is in invalid format!";
  36. static string _confNotFoundMsg = "Config file does not exist!";
  37. static string _argInvalidMsg = "Invalid argument! Example: Optimizer.exe /silent.conf";
  38. static string _alreadyRunningMsg = "Optimizer is already running in the background!";
  39. const string MUTEX_GUID = @"{DEADMOON-0EFC7B8A-D1FC-467F-B4B1-0117C643FE19-OPTIMIZER}";
  40. internal static Mutex MUTEX;
  41. static bool _notRunning;
  42. [STAThread]
  43. static void Main(string[] switches)
  44. {
  45. EmbeddedAssembly.Load(_jsonAssembly, _jsonAssembly.Replace("Optimizer.", string.Empty));
  46. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  47. Application.EnableVisualStyles();
  48. Application.SetCompatibleTextRenderingDefault(false);
  49. // prompt to change filename to 'Optimizer' (skip if experimental build)
  50. // annoying?
  51. //if (!EXPERIMENTAL_BUILD && Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location) != "Optimizer")
  52. //{
  53. // MessageBox.Show(string.Format(_renameAppMessage, Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)), "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  54. // Environment.Exit(0);
  55. //}
  56. // single-instance mechanism
  57. using (MUTEX = new Mutex(true, MUTEX_GUID, out _notRunning))
  58. {
  59. if (!_notRunning)
  60. {
  61. MessageBox.Show(_alreadyRunningMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  62. Environment.Exit(0);
  63. }
  64. else
  65. {
  66. if (!Utilities.IsAdmin())
  67. {
  68. HelperForm f = new HelperForm(null, MessageType.Error, _adminMissingMessage);
  69. f.ShowDialog();
  70. Environment.Exit(0);
  71. }
  72. else
  73. {
  74. if (Utilities.IsCompatible())
  75. {
  76. Required.Deploy();
  77. // for backward compatibility (legacy)
  78. Options.LegacyCheck();
  79. // load settings, if there is no settings, load defaults
  80. try
  81. {
  82. // show FirstRunForm/Language Selector if app is running first time
  83. if (!File.Exists(Options.SettingsFile))
  84. {
  85. Options.LoadSettings();
  86. FirstRunForm frf = new FirstRunForm();
  87. frf.ShowDialog();
  88. }
  89. else
  90. {
  91. Options.LoadSettings();
  92. }
  93. // ideal place to replace internal messages from translation list
  94. _adminMissingMessage = Options.TranslationList["adminMissingMsg"];
  95. _unsupportedMessage = Options.TranslationList["unsupportedMsg"];
  96. _confInvalidFormatMsg = Options.TranslationList["confInvalidFormatMsg"];
  97. _confInvalidVersionMsg = Options.TranslationList["confInvalidVersionMsg"];
  98. _confNotFoundMsg = Options.TranslationList["confNotFoundMsg"];
  99. _argInvalidMsg = Options.TranslationList["argInvalidMsg"];
  100. _alreadyRunningMsg = Options.TranslationList["alreadyRunningMsg"];
  101. }
  102. catch (Exception ex)
  103. {
  104. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  105. }
  106. // checking for silent config argument
  107. if (switches.Length == 1)
  108. {
  109. string arg = switches[0].Trim();
  110. // UNSAFE mode switch (allows running on Windows Server 2008+)
  111. if (arg == "/unsafe")
  112. {
  113. UNSAFE_MODE = true;
  114. StartSplashForm();
  115. _MainForm = new MainForm(_SplashForm);
  116. _MainForm.Load += MainForm_Load;
  117. Application.Run(_MainForm);
  118. return;
  119. }
  120. // resets configuration
  121. if (arg == "/reset")
  122. {
  123. Utilities.ResetConfiguration(true);
  124. return;
  125. }
  126. // displays build info
  127. if (arg == "/version")
  128. {
  129. if (!EXPERIMENTAL_BUILD) MessageBox.Show($"Optimizer: {GetCurrentVersionTostring()}\n\nCoded by: deadmoon © ∞\n\nhttps://github.com/hellzerg/optimizer", "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  130. else MessageBox.Show("Optimizer: EXPERIMENTAL BUILD. PLEASE DELETE AFTER TESTING.\n\nCoded by: deadmoon © ∞\n\nhttps://github.com/hellzerg/optimizer", "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  131. Environment.Exit(0);
  132. }
  133. // instruct to restart in safe-mode
  134. if (arg == "/restart=safemode")
  135. {
  136. Utilities.RunCommand("bcdedit /set {current} safeboot Minimal");
  137. Thread.Sleep(500);
  138. Utilities.Reboot();
  139. Environment.Exit(0);
  140. }
  141. // instruct to restart normally
  142. if (arg == "/restart=normal")
  143. {
  144. Utilities.RunCommand("bcdedit /deletevalue {current} safeboot");
  145. Thread.Sleep(500);
  146. Utilities.Reboot();
  147. Environment.Exit(0);
  148. }
  149. // other options
  150. if (arg.StartsWith("/disable="))
  151. {
  152. string x = arg.Replace("/disable=", string.Empty);
  153. string[] opts = x.Split(',');
  154. bool disableIndicium = opts.Contains("indicium");
  155. bool disableUWPTool = opts.Contains("uwp");
  156. bool disableAppsTool = opts.Contains("apps");
  157. bool disableHostsEditor = opts.Contains("hosts");
  158. bool disableStartupTool = opts.Contains("startup");
  159. bool disableCleaner = opts.Contains("cleaner");
  160. bool disableIntegrator = opts.Contains("integrator");
  161. StartSplashForm();
  162. _MainForm = new MainForm(_SplashForm, disableIndicium, disableHostsEditor, disableAppsTool, disableUWPTool, disableStartupTool, disableCleaner, disableIntegrator);
  163. _MainForm.Load += MainForm_Load;
  164. Application.Run(_MainForm);
  165. return;
  166. }
  167. // disables Defender in SAFE MODE (for Windows 10 1903+ / works in Windows 11 as well)
  168. if (arg == "/disabledefender")
  169. {
  170. File.WriteAllText("DisableDefenderSafeMode.bat", Properties.Resources.DisableDefenderSafeMode1903Plus);
  171. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  172. Thread.Sleep(1000);
  173. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  174. Thread.Sleep(1000);
  175. File.Delete("DisableDefenderSafeMode.bat");
  176. MessageBox.Show("Windows Defender has been completely disabled successfully.", "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  177. return;
  178. }
  179. if (arg.StartsWith("/"))
  180. {
  181. if (File.Exists(arg.Remove(0, 1)))
  182. {
  183. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  184. if (SilentOps.CurrentSilentConfig != null)
  185. {
  186. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  187. {
  188. SilentOps.ProcessSilentConfigGeneral();
  189. SilentOps.SilentUpdateOptionsGeneral();
  190. Options.SaveSettings();
  191. }
  192. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  193. {
  194. SilentOps.ProcessSilentConfigGeneral();
  195. SilentOps.ProcessSilentConfigWindows8();
  196. SilentOps.SilentUpdateOptionsGeneral();
  197. SilentOps.SilentUpdateOptions8();
  198. Options.SaveSettings();
  199. }
  200. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  201. {
  202. SilentOps.ProcessSilentConfigGeneral();
  203. SilentOps.ProcessSilentConfigWindows10();
  204. SilentOps.SilentUpdateOptionsGeneral();
  205. SilentOps.SilentUpdateOptions10();
  206. Options.SaveSettings();
  207. }
  208. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 11 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows11)
  209. {
  210. SilentOps.ProcessSilentConfigGeneral();
  211. SilentOps.ProcessSilentConfigWindows10();
  212. SilentOps.ProcessSilentConfigWindows11();
  213. SilentOps.SilentUpdateOptionsGeneral();
  214. SilentOps.SilentUpdateOptions10();
  215. SilentOps.SilentUpdateOptions11();
  216. Options.SaveSettings();
  217. }
  218. else
  219. {
  220. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  221. Environment.Exit(0);
  222. }
  223. }
  224. else
  225. {
  226. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  227. Environment.Exit(0);
  228. }
  229. }
  230. else
  231. {
  232. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  233. Environment.Exit(0);
  234. }
  235. }
  236. else
  237. {
  238. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  239. Environment.Exit(0);
  240. }
  241. }
  242. else
  243. {
  244. StartSplashForm();
  245. _MainForm = new MainForm(_SplashForm);
  246. _MainForm.Load += MainForm_Load;
  247. Application.Run(_MainForm);
  248. }
  249. }
  250. else
  251. {
  252. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  253. f.ShowDialog();
  254. Environment.Exit(0);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. private static void StartSplashForm()
  261. {
  262. _SplashForm = new SplashForm();
  263. var splashThread = new Thread(new ThreadStart(
  264. () => Application.Run(_SplashForm)));
  265. splashThread.SetApartmentState(ApartmentState.STA);
  266. splashThread.Start();
  267. }
  268. private static void MainForm_Load(object sender, EventArgs e)
  269. {
  270. if (_SplashForm != null && !_SplashForm.Disposing && !_SplashForm.IsDisposed)
  271. _SplashForm.Invoke(new Action(() => _SplashForm.Close()));
  272. _MainForm.TopMost = true;
  273. _MainForm.Activate();
  274. _MainForm.TopMost = false;
  275. }
  276. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  277. {
  278. return EmbeddedAssembly.Get(args.Name);
  279. }
  280. }
  281. }