Program.cs 15 KB

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