2
0

Program.cs 6.6 KB

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