Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. namespace Optimizer
  9. {
  10. static class Program
  11. {
  12. /* VERSION PROPERTIES */
  13. /* DO NOT LEAVE THEM EMPTY */
  14. // Enter current version here
  15. internal readonly static float Major = 10;
  16. internal readonly static float Minor = 1;
  17. internal readonly static bool EXPERIMENTAL_BUILD = false;
  18. internal static string GetCurrentVersionTostring()
  19. {
  20. return Major.ToString() + "." + Minor.ToString();
  21. }
  22. internal static float GetCurrentVersion()
  23. {
  24. return float.Parse(GetCurrentVersionTostring());
  25. }
  26. /* END OF VERSION PROPERTIES */
  27. // Enables the corresponding Windows tab for Windows Server machines
  28. internal static bool UNSAFE_MODE = false;
  29. const string _jsonAssembly = @"Optimizer.Newtonsoft.Json.dll";
  30. internal static MainForm MainForm;
  31. static string _adminMissingMessage = "Optimizer needs to be run as administrator!\nApp will now close...";
  32. static string _unsupportedMessage = "Optimizer works in Windows 7 or higher!\nApp will now close...";
  33. //static string _renameAppMessage = "It's recommended to rename the app from '{0}' to 'Optimizer' for a better experience.\n\nApp will now close...";
  34. static string _confInvalidVersionMsg = "Windows version does not match!";
  35. static string _confInvalidFormatMsg = "Config file is in invalid format!";
  36. static string _confNotFoundMsg = "Config file does not exist!";
  37. static string _argInvalidMsg = "Invalid argument! Example: Optimizer.exe /silent.conf";
  38. static string _alreadyRunningMsg = "Optimizer is already running in the background!";
  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. if (!Utilities.IsAdmin())
  54. {
  55. HelperForm f = new HelperForm(null, MessageType.Error, _adminMissingMessage);
  56. f.ShowDialog();
  57. Application.Exit();
  58. }
  59. else
  60. {
  61. if (Utilities.IsCompatible())
  62. {
  63. // single-instance mechanism
  64. //int processCount = Process.GetProcesses().Count(p => p.ProcessName.ToLowerInvariant().Contains("optimizer"));
  65. //if (processCount > 1)
  66. //{
  67. // MessageBox.Show(_alreadyRunningMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  68. // Environment.Exit(0);
  69. //}
  70. Required.Deploy();
  71. // for backward compatibility (legacy)
  72. Options.LegacyCheck();
  73. // load settings, if there is no settings, load defaults
  74. try
  75. {
  76. // show FirstRunForm if app is running first time
  77. if (!File.Exists(Options.SettingsFile))
  78. {
  79. Options.LoadSettings();
  80. FirstRunForm frf = new FirstRunForm();
  81. frf.ShowDialog();
  82. }
  83. else
  84. {
  85. Options.LoadSettings();
  86. }
  87. // ideal place to replace internal messages from translation list
  88. _adminMissingMessage = Options.TranslationList["adminMissingMsg"];
  89. _unsupportedMessage = Options.TranslationList["unsupportedMsg"];
  90. _confInvalidFormatMsg = Options.TranslationList["confInvalidFormatMsg"];
  91. _confInvalidVersionMsg = Options.TranslationList["confInvalidVersionMsg"];
  92. _confNotFoundMsg = Options.TranslationList["confNotFoundMsg"];
  93. _argInvalidMsg = Options.TranslationList["argInvalidMsg"];
  94. _alreadyRunningMsg = Options.TranslationList["alreadyRunningMsg"];
  95. }
  96. catch (Exception ex)
  97. {
  98. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  99. }
  100. // checking for silent config argument
  101. if (switches.Length == 1)
  102. {
  103. string arg = switches[0].Trim();
  104. // UNSAFE mode switch (allows running on Windows Server 2008+)
  105. if (arg == "/unsafe")
  106. {
  107. UNSAFE_MODE = true;
  108. Application.Run(new MainForm());
  109. return;
  110. }
  111. // resets configuration
  112. if (arg == "/reset")
  113. {
  114. Utilities.ResetConfiguration(true);
  115. return;
  116. }
  117. // disables Defender in SAFE MODE (for Windows 10 1903+)
  118. if (arg == "/disabledefender")
  119. {
  120. File.WriteAllText("DisableDefenderSafeMode.bat", Properties.Resources.DisableDefenderSafeMode1903Plus);
  121. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  122. System.Threading.Thread.Sleep(1000);
  123. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  124. System.Threading.Thread.Sleep(1000);
  125. File.Delete("DisableDefenderSafeMode.bat");
  126. return;
  127. }
  128. if (arg.StartsWith("/"))
  129. {
  130. if (File.Exists(arg.Remove(0, 1)))
  131. {
  132. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  133. if (SilentOps.CurrentSilentConfig != null)
  134. {
  135. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  136. {
  137. SilentOps.ProcessSilentConfigGeneral();
  138. SilentOps.SilentUpdateOptionsGeneral();
  139. Options.SaveSettings();
  140. }
  141. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  142. {
  143. SilentOps.ProcessSilentConfigGeneral();
  144. SilentOps.ProcessSilentConfigWindows8();
  145. SilentOps.SilentUpdateOptionsGeneral();
  146. SilentOps.SilentUpdateOptions8();
  147. Options.SaveSettings();
  148. }
  149. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  150. {
  151. SilentOps.ProcessSilentConfigGeneral();
  152. SilentOps.ProcessSilentConfigWindows10();
  153. SilentOps.SilentUpdateOptionsGeneral();
  154. SilentOps.SilentUpdateOptions10();
  155. Options.SaveSettings();
  156. }
  157. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 11 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows11)
  158. {
  159. SilentOps.ProcessSilentConfigGeneral();
  160. SilentOps.ProcessSilentConfigWindows10();
  161. SilentOps.ProcessSilentConfigWindows11();
  162. SilentOps.SilentUpdateOptionsGeneral();
  163. SilentOps.SilentUpdateOptions10();
  164. SilentOps.SilentUpdateOptions11();
  165. Options.SaveSettings();
  166. }
  167. else
  168. {
  169. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  170. Environment.Exit(0);
  171. }
  172. }
  173. else
  174. {
  175. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  176. Environment.Exit(0);
  177. }
  178. }
  179. else
  180. {
  181. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  182. Environment.Exit(0);
  183. }
  184. }
  185. else
  186. {
  187. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  188. Environment.Exit(0);
  189. }
  190. }
  191. else
  192. {
  193. Application.Run(new MainForm());
  194. }
  195. }
  196. else
  197. {
  198. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  199. f.ShowDialog();
  200. Application.Exit();
  201. }
  202. }
  203. }
  204. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  205. {
  206. return EmbeddedAssembly.Get(args.Name);
  207. }
  208. }
  209. }