Program.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = 5;
  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. Options.LoadSettings();
  71. // ideal place to replace internal messages from translation list
  72. _adminMissingMessage = Options.TranslationList["adminMissingMsg"];
  73. _unsupportedMessage = Options.TranslationList["unsupportedMsg"];
  74. _confInvalidFormatMsg = Options.TranslationList["confInvalidFormatMsg"];
  75. _confInvalidVersionMsg = Options.TranslationList["confInvalidVersionMsg"];
  76. _confNotFoundMsg = Options.TranslationList["confNotFoundMsg"];
  77. _argInvalidMsg = Options.TranslationList["argInvalidMsg"];
  78. }
  79. catch (Exception ex)
  80. {
  81. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  82. }
  83. // checking for silent config argument
  84. if (switches.Length == 1)
  85. {
  86. string arg = switches[0].Trim();
  87. // UNSAFE mode switch (allows running on Windows Server 2008+)
  88. if (arg == "/unsafe")
  89. {
  90. UNSAFE_MODE = true;
  91. Application.Run(new MainForm());
  92. return;
  93. }
  94. if (arg == "/reset")
  95. {
  96. Utilities.ResetConfiguration(true);
  97. return;
  98. }
  99. if (arg.StartsWith("/"))
  100. {
  101. if (File.Exists(arg.Remove(0, 1)))
  102. {
  103. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  104. if (SilentOps.CurrentSilentConfig != null)
  105. {
  106. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  107. {
  108. SilentOps.ProcessSilentConfigGeneral();
  109. SilentOps.SilentUpdateOptionsGeneral();
  110. Options.SaveSettings();
  111. }
  112. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  113. {
  114. SilentOps.ProcessSilentConfigGeneral();
  115. SilentOps.ProcessSilentConfigWindows8();
  116. SilentOps.SilentUpdateOptionsGeneral();
  117. SilentOps.SilentUpdateOptions8();
  118. Options.SaveSettings();
  119. }
  120. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  121. {
  122. SilentOps.ProcessSilentConfigGeneral();
  123. SilentOps.ProcessSilentConfigWindows10();
  124. SilentOps.SilentUpdateOptionsGeneral();
  125. SilentOps.SilentUpdateOptions10();
  126. Options.SaveSettings();
  127. }
  128. else
  129. {
  130. MessageBox.Show(_confInvalidVersionMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  131. Environment.Exit(0);
  132. }
  133. }
  134. else
  135. {
  136. MessageBox.Show(_confInvalidFormatMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  137. Environment.Exit(0);
  138. }
  139. }
  140. else
  141. {
  142. MessageBox.Show(_confNotFoundMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  143. Environment.Exit(0);
  144. }
  145. }
  146. else
  147. {
  148. MessageBox.Show(_argInvalidMsg, "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  149. Environment.Exit(0);
  150. }
  151. }
  152. else
  153. {
  154. Application.Run(new MainForm());
  155. }
  156. }
  157. else
  158. {
  159. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  160. f.ShowDialog();
  161. Application.Exit();
  162. }
  163. }
  164. }
  165. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  166. {
  167. return EmbeddedAssembly.Get(args.Name);
  168. }
  169. }
  170. }