Program.cs 7.7 KB

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