Program.cs 6.5 KB

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