Program.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = 7;
  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. 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. // 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. if (!Directory.Exists(Required.CoreFolder))
  55. {
  56. Required.Deploy();
  57. }
  58. // for backward compatibility (legacy)
  59. if (File.Exists(Options.SettingsFile))
  60. {
  61. if (File.ReadAllText(Options.SettingsFile).Contains("FirstRun"))
  62. {
  63. File.Delete(Options.SettingsFile);
  64. }
  65. }
  66. // load settings, if there is no settings, load defaults
  67. try
  68. {
  69. Options.LoadSettings();
  70. }
  71. catch (Exception ex)
  72. {
  73. ErrorLogger.LogError("Program.Main", ex.Message, ex.StackTrace);
  74. }
  75. // checking for silent config argument
  76. if (switches.Length == 1)
  77. {
  78. string arg = switches[0].Trim();
  79. // UNSAFE mode switch (allows running on Windows Server 2008+)
  80. if (arg == "/unsafe")
  81. {
  82. UNSAFE_MODE = true;
  83. Application.Run(new MainForm());
  84. return;
  85. }
  86. if (arg.StartsWith("/"))
  87. {
  88. if (File.Exists(arg.Remove(0, 1)))
  89. {
  90. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  91. if (SilentOps.CurrentSilentConfig != null)
  92. {
  93. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  94. {
  95. SilentOps.ProcessSilentConfigGeneral();
  96. SilentOps.SilentUpdateOptionsGeneral();
  97. Options.SaveSettings();
  98. }
  99. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  100. {
  101. SilentOps.ProcessSilentConfigGeneral();
  102. SilentOps.ProcessSilentConfigWindows8();
  103. SilentOps.SilentUpdateOptionsGeneral();
  104. SilentOps.SilentUpdateOptions8();
  105. Options.SaveSettings();
  106. }
  107. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  108. {
  109. SilentOps.ProcessSilentConfigGeneral();
  110. SilentOps.ProcessSilentConfigWindows10();
  111. SilentOps.SilentUpdateOptionsGeneral();
  112. SilentOps.SilentUpdateOptions10();
  113. Options.SaveSettings();
  114. }
  115. else
  116. {
  117. MessageBox.Show("Windows version does not match!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  118. Environment.Exit(0);
  119. }
  120. }
  121. else
  122. {
  123. MessageBox.Show("Config file is in invalid format!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  124. Environment.Exit(0);
  125. }
  126. }
  127. else
  128. {
  129. MessageBox.Show("Config file does not exist!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  130. Environment.Exit(0);
  131. }
  132. }
  133. else
  134. {
  135. MessageBox.Show("Invalid argument. Example: optimizer.exe /silent.conf", "Invalid argument", MessageBoxButtons.OK, MessageBoxIcon.Information);
  136. Environment.Exit(0);
  137. }
  138. }
  139. else
  140. {
  141. Application.Run(new MainForm());
  142. }
  143. }
  144. else
  145. {
  146. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  147. f.ShowDialog();
  148. Application.Exit();
  149. }
  150. }
  151. }
  152. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  153. {
  154. return EmbeddedAssembly.Get(args.Name);
  155. }
  156. }
  157. }