Program.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 = 8;
  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 _confInvalidVersionMsg = "Windows version does not match!";
  31. static string _confInvalidFormatMsg = "Config file is in invalid format!";
  32. static string _confNotFoundMsg = "Config file does not exist!";
  33. static string _argInvalidMsg = "Invalid argument! Example: Optimizer.exe /silent.conf";
  34. [STAThread]
  35. static void Main(string[] switches)
  36. {
  37. EmbeddedAssembly.Load(_jsonAssembly, _jsonAssembly.Replace("Optimizer.", string.Empty));
  38. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  39. // check if another instance is running
  40. // what's the problem? prevents auto-patching...
  41. //if (System.Diagnostics.Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1)
  42. //{
  43. // MessageBox.Show("Optimizer is already running in the background!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  44. // return;
  45. //}
  46. Application.EnableVisualStyles();
  47. Application.SetCompatibleTextRenderingDefault(false);
  48. if (!Utilities.IsAdmin())
  49. {
  50. HelperForm f = new HelperForm(null, MessageType.Error, _adminMissingMessage);
  51. f.ShowDialog();
  52. Application.Exit();
  53. }
  54. else
  55. {
  56. if (Utilities.IsCompatible())
  57. {
  58. Required.Deploy();
  59. // for backward compatibility (legacy)
  60. if (File.Exists(Options.SettingsFile))
  61. {
  62. if (File.ReadAllText(Options.SettingsFile).Contains("FirstRun"))
  63. {
  64. File.Delete(Options.SettingsFile);
  65. }
  66. }
  67. // load settings, if there is no settings, load defaults
  68. try
  69. {
  70. // show FirstRunForm if app is running first time
  71. if (!File.Exists(Options.SettingsFile))
  72. {
  73. Options.LoadSettings();
  74. FirstRunForm frf = new FirstRunForm();
  75. frf.ShowDialog();
  76. }
  77. else
  78. {
  79. Options.LoadSettings();
  80. }
  81. // ideal place to replace internal messages from translation list
  82. _adminMissingMessage = Options.TranslationList["adminMissingMsg"];
  83. _unsupportedMessage = Options.TranslationList["unsupportedMsg"];
  84. _confInvalidFormatMsg = Options.TranslationList["confInvalidFormatMsg"];
  85. _confInvalidVersionMsg = Options.TranslationList["confInvalidVersionMsg"];
  86. _confNotFoundMsg = Options.TranslationList["confNotFoundMsg"];
  87. _argInvalidMsg = Options.TranslationList["argInvalidMsg"];
  88. }
  89. catch (Exception ex)
  90. {
  91. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  92. }
  93. // checking for silent config argument
  94. if (switches.Length == 1)
  95. {
  96. string arg = switches[0].Trim();
  97. // UNSAFE mode switch (allows running on Windows Server 2008+)
  98. if (arg == "/unsafe")
  99. {
  100. UNSAFE_MODE = true;
  101. Application.Run(new MainForm());
  102. return;
  103. }
  104. if (arg == "/reset")
  105. {
  106. Utilities.ResetConfiguration(true);
  107. return;
  108. }
  109. if (arg.StartsWith("/"))
  110. {
  111. if (File.Exists(arg.Remove(0, 1)))
  112. {
  113. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  114. if (SilentOps.CurrentSilentConfig != null)
  115. {
  116. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  117. {
  118. SilentOps.ProcessSilentConfigGeneral();
  119. SilentOps.SilentUpdateOptionsGeneral();
  120. Options.SaveSettings();
  121. }
  122. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  123. {
  124. SilentOps.ProcessSilentConfigGeneral();
  125. SilentOps.ProcessSilentConfigWindows8();
  126. SilentOps.SilentUpdateOptionsGeneral();
  127. SilentOps.SilentUpdateOptions8();
  128. Options.SaveSettings();
  129. }
  130. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  131. {
  132. SilentOps.ProcessSilentConfigGeneral();
  133. SilentOps.ProcessSilentConfigWindows10();
  134. SilentOps.SilentUpdateOptionsGeneral();
  135. SilentOps.SilentUpdateOptions10();
  136. Options.SaveSettings();
  137. }
  138. else
  139. {
  140. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  141. Environment.Exit(0);
  142. }
  143. }
  144. else
  145. {
  146. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  147. Environment.Exit(0);
  148. }
  149. }
  150. else
  151. {
  152. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  153. Environment.Exit(0);
  154. }
  155. }
  156. else
  157. {
  158. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  159. Environment.Exit(0);
  160. }
  161. }
  162. else
  163. {
  164. Application.Run(new MainForm());
  165. }
  166. }
  167. else
  168. {
  169. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  170. f.ShowDialog();
  171. Application.Exit();
  172. }
  173. }
  174. }
  175. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  176. {
  177. return EmbeddedAssembly.Get(args.Name);
  178. }
  179. }
  180. }