Program.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = 3;
  19. internal readonly static float Minor = 1;
  20. /* END OF VERSION PROPERTIES */
  21. internal static MainForm main;
  22. private readonly static string noadminmsg = "Optimizer needs to be run as administrator!\nApp will now close...";
  23. private readonly static string unsupportedmsg = "Optimizer works in Windows 7 or higher!\nApp will now close...";
  24. internal static string GetCurrentVersionToString()
  25. {
  26. return Major.ToString() + "." + Minor.ToString();
  27. }
  28. internal static float GetCurrentVersion()
  29. {
  30. return float.Parse(GetCurrentVersionToString());
  31. }
  32. private static bool IsAdmin()
  33. {
  34. var identity = WindowsIdentity.GetCurrent();
  35. var principal = new WindowsPrincipal(identity);
  36. return principal.IsInRole(WindowsBuiltInRole.Administrator);
  37. }
  38. private static bool IsSevenOrHigher()
  39. {
  40. string os = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", "");
  41. bool legit;
  42. if ((os.Contains("XP")) || (os.Contains("Vista")) || os.Contains("Server 2003"))
  43. {
  44. legit = false;
  45. }
  46. else
  47. {
  48. legit = true;
  49. }
  50. return legit;
  51. }
  52. [STAThread]
  53. static void Main()
  54. {
  55. Application.EnableVisualStyles();
  56. Application.SetCompatibleTextRenderingDefault(false);
  57. if (IsAdmin() == false)
  58. {
  59. MessagerForm f = new MessagerForm(null, MessagerType.Error, noadminmsg);
  60. f.ShowDialog();
  61. Application.Exit();
  62. }
  63. else
  64. {
  65. if (IsSevenOrHigher() == true)
  66. {
  67. string resource = "Optimizer.Newtonsoft.Json.dll";
  68. EmbeddedAssembly.Load(resource, "Newtonsoft.Json.dll");
  69. AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  70. if (!Directory.Exists(Required.CoreFolder))
  71. {
  72. Required.Deploy();
  73. }
  74. // for backward compatibility
  75. if (File.Exists(Options.SettingsFile))
  76. {
  77. if (File.ReadAllText(Options.SettingsFile).Contains("FirstRun"))
  78. {
  79. File.Delete(Options.SettingsFile);
  80. }
  81. }
  82. // load settings, if there is no settings, load defaults
  83. Options.LoadSettings();
  84. Application.Run(new MainForm());
  85. }
  86. else
  87. {
  88. MessagerForm f = new MessagerForm(null, MessagerType.Error, unsupportedmsg);
  89. f.ShowDialog();
  90. Application.Exit();
  91. }
  92. }
  93. }
  94. static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  95. {
  96. return EmbeddedAssembly.Get(args.Name);
  97. }
  98. }
  99. }