Program.cs 14 KB

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