Program.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 = 11;
  14. internal readonly static float Minor = 7;
  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 (!Program.EXPERIMENTAL_BUILD) MessageBox.Show($"Optimizer: {Program.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. // other options
  133. if (arg.StartsWith("/disable="))
  134. {
  135. string x = arg.Replace("/disable=", string.Empty);
  136. string[] opts = x.Split(',');
  137. bool disableIndicium = opts.Contains("indicium");
  138. bool disableUWPTool = opts.Contains("uwp");
  139. bool disableAppsTool = opts.Contains("apps");
  140. bool disableHostsEditor = opts.Contains("hosts");
  141. bool disableStartupTool = opts.Contains("startup");
  142. bool disableCleaner = opts.Contains("cleaner");
  143. bool disableIntegrator = opts.Contains("integrator");
  144. StartSplashForm();
  145. _MainForm = new MainForm(_SplashForm, disableIndicium, disableHostsEditor, disableAppsTool, disableUWPTool, disableStartupTool, disableCleaner, disableIntegrator);
  146. _MainForm.Load += MainForm_Load;
  147. Application.Run(_MainForm);
  148. return;
  149. }
  150. // disables Defender in SAFE MODE (for Windows 10 1903+ / works in Windows 11 as well)
  151. if (arg == "/disabledefender")
  152. {
  153. File.WriteAllText("DisableDefenderSafeMode.bat", Properties.Resources.DisableDefenderSafeMode1903Plus);
  154. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  155. Thread.Sleep(1000);
  156. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  157. Thread.Sleep(1000);
  158. File.Delete("DisableDefenderSafeMode.bat");
  159. MessageBox.Show("Windows Defender has been completely disabled successfully.", "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  160. return;
  161. }
  162. if (arg.StartsWith("/"))
  163. {
  164. if (File.Exists(arg.Remove(0, 1)))
  165. {
  166. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  167. if (SilentOps.CurrentSilentConfig != null)
  168. {
  169. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  170. {
  171. SilentOps.ProcessSilentConfigGeneral();
  172. SilentOps.SilentUpdateOptionsGeneral();
  173. Options.SaveSettings();
  174. }
  175. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  176. {
  177. SilentOps.ProcessSilentConfigGeneral();
  178. SilentOps.ProcessSilentConfigWindows8();
  179. SilentOps.SilentUpdateOptionsGeneral();
  180. SilentOps.SilentUpdateOptions8();
  181. Options.SaveSettings();
  182. }
  183. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  184. {
  185. SilentOps.ProcessSilentConfigGeneral();
  186. SilentOps.ProcessSilentConfigWindows10();
  187. SilentOps.SilentUpdateOptionsGeneral();
  188. SilentOps.SilentUpdateOptions10();
  189. Options.SaveSettings();
  190. }
  191. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 11 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows11)
  192. {
  193. SilentOps.ProcessSilentConfigGeneral();
  194. SilentOps.ProcessSilentConfigWindows10();
  195. SilentOps.ProcessSilentConfigWindows11();
  196. SilentOps.SilentUpdateOptionsGeneral();
  197. SilentOps.SilentUpdateOptions10();
  198. SilentOps.SilentUpdateOptions11();
  199. Options.SaveSettings();
  200. }
  201. else
  202. {
  203. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  204. Environment.Exit(0);
  205. }
  206. }
  207. else
  208. {
  209. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  210. Environment.Exit(0);
  211. }
  212. }
  213. else
  214. {
  215. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  216. Environment.Exit(0);
  217. }
  218. }
  219. else
  220. {
  221. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  222. Environment.Exit(0);
  223. }
  224. }
  225. else
  226. {
  227. StartSplashForm();
  228. _MainForm = new MainForm(_SplashForm);
  229. _MainForm.Load += MainForm_Load;
  230. Application.Run(_MainForm);
  231. }
  232. }
  233. else
  234. {
  235. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  236. f.ShowDialog();
  237. Environment.Exit(0);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. private static void StartSplashForm()
  244. {
  245. _SplashForm = new SplashForm();
  246. var splashThread = new Thread(new ThreadStart(
  247. () => Application.Run(_SplashForm)));
  248. splashThread.SetApartmentState(ApartmentState.STA);
  249. splashThread.Start();
  250. }
  251. private static void MainForm_Load(object sender, EventArgs e)
  252. {
  253. if (_SplashForm != null && !_SplashForm.Disposing && !_SplashForm.IsDisposed)
  254. _SplashForm.Invoke(new Action(() => _SplashForm.Close()));
  255. _MainForm.TopMost = true;
  256. _MainForm.Activate();
  257. _MainForm.TopMost = false;
  258. }
  259. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  260. {
  261. return EmbeddedAssembly.Get(args.Name);
  262. }
  263. }
  264. }