Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Windows.Forms;
  5. namespace Optimizer
  6. {
  7. static class Program
  8. {
  9. /* VERSION PROPERTIES */
  10. /* DO NOT LEAVE THEM EMPTY */
  11. // Enter current version here
  12. internal readonly static float Major = 9;
  13. internal readonly static float Minor = 8;
  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. [STAThread]
  36. static void Main(string[] switches)
  37. {
  38. EmbeddedAssembly.Load(_jsonAssembly, _jsonAssembly.Replace("Optimizer.", string.Empty));
  39. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  40. Application.EnableVisualStyles();
  41. Application.SetCompatibleTextRenderingDefault(false);
  42. // check if another instance is running
  43. // what's the problem? prevents auto-patching...
  44. //if (System.Diagnostics.Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1)
  45. //{
  46. // MessageBox.Show("Optimizer is already running in the background!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  47. // return;
  48. //}
  49. // prompt to change filename to 'Optimizer' (skip if experimental build)
  50. // annoying?
  51. //if (!EXPERIMENTAL_BUILD && Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location) != "Optimizer")
  52. //{
  53. // MessageBox.Show(string.Format(_renameAppMessage, Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)), "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  54. // Environment.Exit(0);
  55. //}
  56. if (!Utilities.IsAdmin())
  57. {
  58. HelperForm f = new HelperForm(null, MessageType.Error, _adminMissingMessage);
  59. f.ShowDialog();
  60. Application.Exit();
  61. }
  62. else
  63. {
  64. if (Utilities.IsCompatible())
  65. {
  66. Required.Deploy();
  67. // for backward compatibility (legacy)
  68. if (File.Exists(Options.SettingsFile))
  69. {
  70. if (File.ReadAllText(Options.SettingsFile).Contains("FirstRun"))
  71. {
  72. File.Delete(Options.SettingsFile);
  73. }
  74. }
  75. // load settings, if there is no settings, load defaults
  76. try
  77. {
  78. // show FirstRunForm if app is running first time
  79. if (!File.Exists(Options.SettingsFile))
  80. {
  81. Options.LoadSettings();
  82. FirstRunForm frf = new FirstRunForm();
  83. frf.ShowDialog();
  84. }
  85. else
  86. {
  87. Options.LoadSettings();
  88. }
  89. // ideal place to replace internal messages from translation list
  90. _adminMissingMessage = Options.TranslationList["adminMissingMsg"];
  91. _unsupportedMessage = Options.TranslationList["unsupportedMsg"];
  92. _confInvalidFormatMsg = Options.TranslationList["confInvalidFormatMsg"];
  93. _confInvalidVersionMsg = Options.TranslationList["confInvalidVersionMsg"];
  94. _confNotFoundMsg = Options.TranslationList["confNotFoundMsg"];
  95. _argInvalidMsg = Options.TranslationList["argInvalidMsg"];
  96. }
  97. catch (Exception ex)
  98. {
  99. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  100. }
  101. // checking for silent config argument
  102. if (switches.Length == 1)
  103. {
  104. string arg = switches[0].Trim();
  105. // UNSAFE mode switch (allows running on Windows Server 2008+)
  106. if (arg == "/unsafe")
  107. {
  108. UNSAFE_MODE = true;
  109. Application.Run(new MainForm());
  110. return;
  111. }
  112. // resets configuration
  113. if (arg == "/reset")
  114. {
  115. Utilities.ResetConfiguration(true);
  116. return;
  117. }
  118. // disables Defender in SAFE MODE (for Windows 10 1903+)
  119. if (arg == "/disabledefender")
  120. {
  121. File.WriteAllText("DisableDefenderSafeMode.bat", Properties.Resources.DisableDefenderSafeMode1903Plus);
  122. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  123. System.Threading.Thread.Sleep(1000);
  124. Utilities.RunBatchFile("DisableDefenderSafeMode.bat");
  125. System.Threading.Thread.Sleep(1000);
  126. File.Delete("DisableDefenderSafeMode.bat");
  127. return;
  128. }
  129. if (arg.StartsWith("/"))
  130. {
  131. if (File.Exists(arg.Remove(0, 1)))
  132. {
  133. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  134. if (SilentOps.CurrentSilentConfig != null)
  135. {
  136. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  137. {
  138. SilentOps.ProcessSilentConfigGeneral();
  139. SilentOps.SilentUpdateOptionsGeneral();
  140. Options.SaveSettings();
  141. }
  142. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  143. {
  144. SilentOps.ProcessSilentConfigGeneral();
  145. SilentOps.ProcessSilentConfigWindows8();
  146. SilentOps.SilentUpdateOptionsGeneral();
  147. SilentOps.SilentUpdateOptions8();
  148. Options.SaveSettings();
  149. }
  150. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  151. {
  152. SilentOps.ProcessSilentConfigGeneral();
  153. SilentOps.ProcessSilentConfigWindows10();
  154. SilentOps.SilentUpdateOptionsGeneral();
  155. SilentOps.SilentUpdateOptions10();
  156. Options.SaveSettings();
  157. }
  158. else
  159. {
  160. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  161. Environment.Exit(0);
  162. }
  163. }
  164. else
  165. {
  166. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  167. Environment.Exit(0);
  168. }
  169. }
  170. else
  171. {
  172. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  173. Environment.Exit(0);
  174. }
  175. }
  176. else
  177. {
  178. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  179. Environment.Exit(0);
  180. }
  181. }
  182. else
  183. {
  184. Application.Run(new MainForm());
  185. }
  186. }
  187. else
  188. {
  189. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  190. f.ShowDialog();
  191. Application.Exit();
  192. }
  193. }
  194. }
  195. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  196. {
  197. return EmbeddedAssembly.Get(args.Name);
  198. }
  199. }
  200. }