Program.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 = 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. [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. if (arg == "/reset")
  113. {
  114. Utilities.ResetConfiguration(true);
  115. return;
  116. }
  117. if (arg.StartsWith("/"))
  118. {
  119. if (File.Exists(arg.Remove(0, 1)))
  120. {
  121. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  122. if (SilentOps.CurrentSilentConfig != null)
  123. {
  124. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  125. {
  126. SilentOps.ProcessSilentConfigGeneral();
  127. SilentOps.SilentUpdateOptionsGeneral();
  128. Options.SaveSettings();
  129. }
  130. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  131. {
  132. SilentOps.ProcessSilentConfigGeneral();
  133. SilentOps.ProcessSilentConfigWindows8();
  134. SilentOps.SilentUpdateOptionsGeneral();
  135. SilentOps.SilentUpdateOptions8();
  136. Options.SaveSettings();
  137. }
  138. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  139. {
  140. SilentOps.ProcessSilentConfigGeneral();
  141. SilentOps.ProcessSilentConfigWindows10();
  142. SilentOps.SilentUpdateOptionsGeneral();
  143. SilentOps.SilentUpdateOptions10();
  144. Options.SaveSettings();
  145. }
  146. else
  147. {
  148. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  149. Environment.Exit(0);
  150. }
  151. }
  152. else
  153. {
  154. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  155. Environment.Exit(0);
  156. }
  157. }
  158. else
  159. {
  160. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  161. Environment.Exit(0);
  162. }
  163. }
  164. else
  165. {
  166. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  167. Environment.Exit(0);
  168. }
  169. }
  170. else
  171. {
  172. Application.Run(new MainForm());
  173. }
  174. }
  175. else
  176. {
  177. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  178. f.ShowDialog();
  179. Application.Exit();
  180. }
  181. }
  182. }
  183. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  184. {
  185. return EmbeddedAssembly.Get(args.Name);
  186. }
  187. }
  188. }