Program.cs 7.5 KB

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