Program.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = 4;
  13. internal readonly static float Minor = 9;
  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. const string _jsonAssembly = @"Optimizer.Newtonsoft.Json.dll";
  24. internal static MainForm MainForm;
  25. readonly static string _adminMissingMessage = "Optimizer needs to be run as administrator!\nApp will now close...";
  26. readonly static string _unsupportedMessage = "Optimizer works in Windows 7 or higher!\nApp will now close...";
  27. [STAThread]
  28. static void Main(string[] switches)
  29. {
  30. EmbeddedAssembly.Load(_jsonAssembly, _jsonAssembly.Replace("Optimizer.", string.Empty));
  31. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  32. Application.EnableVisualStyles();
  33. Application.SetCompatibleTextRenderingDefault(false);
  34. if (!Utilities.IsAdmin())
  35. {
  36. HelperForm f = new HelperForm(null, MessageType.Error, _adminMissingMessage);
  37. f.ShowDialog();
  38. Application.Exit();
  39. }
  40. else
  41. {
  42. if (Utilities.IsCompatible())
  43. {
  44. if (!Directory.Exists(Required.CoreFolder))
  45. {
  46. Required.Deploy();
  47. }
  48. // for backward compatibility
  49. if (File.Exists(Options.SettingsFile))
  50. {
  51. if (File.ReadAllText(Options.SettingsFile).Contains("FirstRun"))
  52. {
  53. File.Delete(Options.SettingsFile);
  54. }
  55. }
  56. // load settings, if there is no settings, load defaults
  57. Options.LoadSettings();
  58. // checking for silent config argument
  59. if (switches.Length == 1)
  60. {
  61. string arg = switches[0].Trim();
  62. if (arg.StartsWith("/"))
  63. {
  64. if (File.Exists(arg.Remove(0, 1)))
  65. {
  66. SilentOps.GetSilentConfig(arg.Remove(0, 1));
  67. if (SilentOps.CurrentSilentConfig != null)
  68. {
  69. if (SilentOps.CurrentSilentConfig.WindowsVersion == 7 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows7)
  70. {
  71. SilentOps.ProcessSilentConfigGeneral();
  72. SilentOps.SilentUpdateOptionsGeneral();
  73. Options.SaveSettings();
  74. }
  75. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 8 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows8)
  76. {
  77. SilentOps.ProcessSilentConfigGeneral();
  78. SilentOps.ProcessSilentConfigWindows8();
  79. SilentOps.SilentUpdateOptions8();
  80. Options.SaveSettings();
  81. }
  82. else if (SilentOps.CurrentSilentConfig.WindowsVersion == 10 && Utilities.CurrentWindowsVersion == WindowsVersion.Windows10)
  83. {
  84. SilentOps.ProcessSilentConfigGeneral();
  85. SilentOps.ProcessSilentConfigWindows10();
  86. SilentOps.SilentUpdateOptions10();
  87. Options.SaveSettings();
  88. }
  89. else
  90. {
  91. MessageBox.Show("Windows version does not match!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  92. Environment.Exit(0);
  93. }
  94. }
  95. else
  96. {
  97. MessageBox.Show("Config file is in invalid format!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  98. Environment.Exit(0);
  99. }
  100. }
  101. else
  102. {
  103. MessageBox.Show("Config file does not exist!", "Invalid config file", MessageBoxButtons.OK, MessageBoxIcon.Information);
  104. Environment.Exit(0);
  105. }
  106. }
  107. else
  108. {
  109. MessageBox.Show("Invalid argument. Example: optimizer.exe /silent.conf", "Invalid argument", MessageBoxButtons.OK, MessageBoxIcon.Information);
  110. Environment.Exit(0);
  111. }
  112. }
  113. else
  114. {
  115. Application.Run(new MainForm());
  116. }
  117. }
  118. else
  119. {
  120. HelperForm f = new HelperForm(null, MessageType.Error, _unsupportedMessage);
  121. f.ShowDialog();
  122. Application.Exit();
  123. }
  124. }
  125. }
  126. private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  127. {
  128. return EmbeddedAssembly.Get(args.Name);
  129. }
  130. }
  131. }