Program.cs 17 KB

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