2
0

Program.cs 12 KB

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