Program.cs 7.1 KB

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