Program.cs 6.8 KB

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