Program.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Windows.Forms;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Threading;
  6. namespace Optimizer
  7. {
  8. static class Program
  9. {
  10. /* VERSION PROPERTIES */
  11. /* DO NOT LEAVE THEM EMPTY */
  12. // Enter current version here
  13. internal readonly static float Major = 7;
  14. internal readonly static float Minor = 2;
  15. internal readonly static bool EXPERIMENTAL_BUILD = false;
  16. internal static string GetCurrentVersionTostring()
  17. {
  18. return Major.ToString() + "." + Minor.ToString();
  19. }
  20. internal static float GetCurrentVersion()
  21. {
  22. return float.Parse(GetCurrentVersionTostring());
  23. }
  24. /* END OF VERSION PROPERTIES */
  25. // Enables the corresponding Windows tab for Windows Server machines
  26. internal static bool UNSAFE_MODE = false;
  27. const string _jsonAssembly = @"Optimizer.Newtonsoft.Json.dll";
  28. internal static MainForm MainForm;
  29. readonly static string _adminMissingMessage = "Optimizer needs to be run as administrator!\nApp will now close...";
  30. readonly static string _unsupportedMessage = "Optimizer works in Windows 7 or higher!\nApp will now close...";
  31. [STAThread]
  32. static void Main(string[] switches)
  33. {
  34. EmbeddedAssembly.Load(_jsonAssembly, _jsonAssembly.Replace("Optimizer.", string.Empty));
  35. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  36. // check if another instance is running
  37. // problem? prevents auto-patching...
  38. //if (System.Diagnostics.Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1)
  39. //{
  40. // MessageBox.Show("Optimizer is already running in the background!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  41. // return;
  42. //}
  43. Application.EnableVisualStyles();
  44. Application.SetCompatibleTextRenderingDefault(false);
  45. if (!Utilities.IsAdmin())
  46. {
  47. HelperForm f = new HelperForm(null, MessageType.Error, _adminMissingMessage);
  48. f.ShowDialog();
  49. Application.Exit();
  50. }
  51. else
  52. {
  53. if (Utilities.IsCompatible())
  54. {
  55. if (!Directory.Exists(Required.CoreFolder))
  56. {
  57. Required.Deploy();
  58. }
  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. }
  72. catch (Exception ex)
  73. {
  74. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  75. }
  76. // checking for silent config argument
  77. if (switches.Length == 1)
  78. {
  79. string arg = switches[0].Trim();
  80. // UNSAFE mode switch (allows running on Windows Server 2008+)
  81. if (arg == "/unsafe")
  82. {
  83. UNSAFE_MODE = true;
  84. Application.Run(new MainForm());
  85. return;
  86. }
  87. if (arg.StartsWith("/"))
  88. {
  89. if (File.Exists(arg.Remove(0, 1)))
  90. {
  91. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  92. if (SilentOps.CurrentSilentConfig != null)
  93. {
  94. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  95. {
  96. SilentOps.ProcessSilentConfigGeneral();
  97. SilentOps.SilentUpdateOptionsGeneral();
  98. Options.SaveSettings();
  99. }
  100. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  101. {
  102. SilentOps.ProcessSilentConfigGeneral();
  103. SilentOps.ProcessSilentConfigWindows8();
  104. SilentOps.SilentUpdateOptionsGeneral();
  105. SilentOps.SilentUpdateOptions8();
  106. Options.SaveSettings();
  107. }
  108. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  109. {
  110. SilentOps.ProcessSilentConfigGeneral();
  111. SilentOps.ProcessSilentConfigWindows10();
  112. SilentOps.SilentUpdateOptionsGeneral();
  113. SilentOps.SilentUpdateOptions10();
  114. Options.SaveSettings();
  115. }
  116. else
  117. {
  118. MessageBox.Show("Windows version does not match!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  119. Environment.Exit(0);
  120. }
  121. }
  122. else
  123. {
  124. MessageBox.Show("Config file is in invalid format!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  125. Environment.Exit(0);
  126. }
  127. }
  128. else
  129. {
  130. MessageBox.Show("Config file does not exist!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  131. Environment.Exit(0);
  132. }
  133. }
  134. else
  135. {
  136. MessageBox.Show("Invalid argument. Example: optimizer.exe /silent.conf", "Invalid argument", MessageBoxButtons.OK, MessageBoxIcon.Information);
  137. Environment.Exit(0);
  138. }
  139. }
  140. else
  141. {
  142. Application.Run(new MainForm());
  143. }
  144. }
  145. else
  146. {
  147. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  148. f.ShowDialog();
  149. Application.Exit();
  150. }
  151. }
  152. }
  153. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  154. {
  155. return EmbeddedAssembly.Get(args.Name);
  156. }
  157. }
  158. }