Program.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // Enter current version here
  13. internal readonly static float Major = 10;
  14. internal readonly static float Minor = 2;
  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. 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 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. Application.Run(new MainForm());
  113. return;
  114. }
  115. // resets configuration
  116. if (arg == "/reset")
  117. {
  118. Utilities.ResetConfiguration(true);
  119. return;
  120. }
  121. // disables Defender in SAFE MODE (for Windows 10 1903+)
  122. if (arg == "/disabledefender")
  123. {
  124. File.WriteAllText("DisableDefenderSafeMode.bat", Properties.Resources.DisableDefenderSafeMode1903Plus);
  125. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  126. System.Threading.Thread.Sleep(1000);
  127. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  128. System.Threading.Thread.Sleep(1000);
  129. File.Delete("DisableDefenderSafeMode.bat");
  130. return;
  131. }
  132. if (arg.StartsWith("/"))
  133. {
  134. if (File.Exists(arg.Remove(0, 1)))
  135. {
  136. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  137. if (SilentOps.CurrentSilentConfig != null)
  138. {
  139. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  140. {
  141. SilentOps.ProcessSilentConfigGeneral();
  142. SilentOps.SilentUpdateOptionsGeneral();
  143. Options.SaveSettings();
  144. }
  145. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  146. {
  147. SilentOps.ProcessSilentConfigGeneral();
  148. SilentOps.ProcessSilentConfigWindows8();
  149. SilentOps.SilentUpdateOptionsGeneral();
  150. SilentOps.SilentUpdateOptions8();
  151. Options.SaveSettings();
  152. }
  153. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  154. {
  155. SilentOps.ProcessSilentConfigGeneral();
  156. SilentOps.ProcessSilentConfigWindows10();
  157. SilentOps.SilentUpdateOptionsGeneral();
  158. SilentOps.SilentUpdateOptions10();
  159. Options.SaveSettings();
  160. }
  161. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 11 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows11)
  162. {
  163. SilentOps.ProcessSilentConfigGeneral();
  164. SilentOps.ProcessSilentConfigWindows10();
  165. SilentOps.ProcessSilentConfigWindows11();
  166. SilentOps.SilentUpdateOptionsGeneral();
  167. SilentOps.SilentUpdateOptions10();
  168. SilentOps.SilentUpdateOptions11();
  169. Options.SaveSettings();
  170. }
  171. else
  172. {
  173. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  174. Environment.Exit(0);
  175. }
  176. }
  177. else
  178. {
  179. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  180. Environment.Exit(0);
  181. }
  182. }
  183. else
  184. {
  185. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  186. Environment.Exit(0);
  187. }
  188. }
  189. else
  190. {
  191. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  192. Environment.Exit(0);
  193. }
  194. }
  195. else
  196. {
  197. Application.Run(new MainForm());
  198. }
  199. }
  200. else
  201. {
  202. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  203. f.ShowDialog();
  204. Application.Exit();
  205. }
  206. }
  207. }
  208. }
  209. }
  210. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  211. {
  212. return EmbeddedAssembly.Get(args.Name);
  213. }
  214. }
  215. }