Program.cs 6.6 KB

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