Program.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Diagnostics;
  2. using Launcher;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.CompilerServices;
  5. using Client.Resolution;
  6. namespace Client
  7. {
  8. internal static class Program
  9. {
  10. public static CMain Form;
  11. public static AMain PForm;
  12. public static bool Restart;
  13. public static bool Launch;
  14. [STAThread]
  15. private static void Main(string[] args)
  16. {
  17. if (args.Length > 0)
  18. {
  19. foreach (var arg in args)
  20. {
  21. if (arg.ToLower() == "-tc") Settings.UseTestConfig = true;
  22. }
  23. }
  24. #if DEBUG
  25. Settings.UseTestConfig = true;
  26. #endif
  27. try
  28. {
  29. if (UpdatePatcher()) return;
  30. if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully == true) { }
  31. Packet.IsServer = false;
  32. Settings.Load();
  33. Application.EnableVisualStyles();
  34. Application.SetCompatibleTextRenderingDefault(false);
  35. CheckResolutionSetting();
  36. Launch = false;
  37. if (Settings.P_Patcher)
  38. Application.Run(PForm = new AMain());
  39. else
  40. Launch = true;
  41. if (Launch)
  42. Application.Run(Form = new CMain());
  43. Settings.Save();
  44. if (Restart)
  45. {
  46. Application.Restart();
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. CMain.SaveError(ex.ToString());
  52. }
  53. }
  54. private static bool UpdatePatcher()
  55. {
  56. try
  57. {
  58. const string fromName = @".\AutoPatcher.gz", toName = @".\AutoPatcher.exe";
  59. if (!File.Exists(fromName)) return false;
  60. Process[] processes = Process.GetProcessesByName("AutoPatcher");
  61. if (processes.Length > 0)
  62. {
  63. string patcherPath = Application.StartupPath + @"\AutoPatcher.exe";
  64. for (int i = 0; i < processes.Length; i++)
  65. if (processes[i].MainModule.FileName == patcherPath)
  66. processes[i].Kill();
  67. Stopwatch stopwatch = Stopwatch.StartNew();
  68. bool wait = true;
  69. processes = Process.GetProcessesByName("AutoPatcher");
  70. while (wait)
  71. {
  72. wait = false;
  73. for (int i = 0; i < processes.Length; i++)
  74. if (processes[i].MainModule.FileName == patcherPath)
  75. {
  76. wait = true;
  77. }
  78. if (stopwatch.ElapsedMilliseconds <= 3000) continue;
  79. MessageBox.Show("Failed to close AutoPatcher during update.");
  80. return true;
  81. }
  82. }
  83. if (File.Exists(toName)) File.Delete(toName);
  84. File.Move(fromName, toName);
  85. Process.Start(toName, "Auto");
  86. return true;
  87. }
  88. catch (Exception ex)
  89. {
  90. CMain.SaveError(ex.ToString());
  91. throw;
  92. }
  93. }
  94. public static class RuntimePolicyHelper
  95. {
  96. public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }
  97. static RuntimePolicyHelper()
  98. {
  99. //ICLRRuntimeInfo clrRuntimeInfo =
  100. // (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
  101. // Guid.Empty,
  102. // typeof(ICLRRuntimeInfo).GUID);
  103. //try
  104. //{
  105. // clrRuntimeInfo.BindAsLegacyV2Runtime();
  106. // LegacyV2RuntimeEnabledSuccessfully = true;
  107. //}
  108. //catch (COMException)
  109. //{
  110. // // This occurs with an HRESULT meaning
  111. // // "A different runtime was already bound to the legacy CLR version 2 activation policy."
  112. // LegacyV2RuntimeEnabledSuccessfully = false;
  113. //}
  114. }
  115. [ComImport]
  116. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  117. [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
  118. private interface ICLRRuntimeInfo
  119. {
  120. void xGetVersionString();
  121. void xGetRuntimeDirectory();
  122. void xIsLoaded();
  123. void xIsLoadable();
  124. void xLoadErrorString();
  125. void xLoadLibrary();
  126. void xGetProcAddress();
  127. void xGetInterface();
  128. void xSetDefaultStartupFlags();
  129. void xGetDefaultStartupFlags();
  130. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  131. void BindAsLegacyV2Runtime();
  132. }
  133. }
  134. public static void CheckResolutionSetting()
  135. {
  136. var parsedOK = DisplayResolutions.GetDisplayResolutions();
  137. if (!parsedOK)
  138. {
  139. MessageBox.Show(GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.CouldNotGetDisplayResolutions), GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.GetDisplayResolutionIssue), MessageBoxButtons.OK, MessageBoxIcon.Error);
  140. Environment.Exit(0);
  141. }
  142. if (!DisplayResolutions.IsSupported(Settings.Resolution))
  143. {
  144. MessageBox.Show(GameLanguage.ClientTextMap.GetLocalization((ClientTextKeys.ClientNotSupportSettingResolution), Settings.Resolution),
  145. GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.InvalidClientResolution),
  146. MessageBoxButtons.OK,
  147. MessageBoxIcon.Error);
  148. Settings.Resolution = (int)eSupportedResolution.w1024h768;
  149. Settings.Save();
  150. }
  151. }
  152. }
  153. }