| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System.Diagnostics;
- using Launcher;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using Client.Resolution;
- namespace Client
- {
- internal static class Program
- {
- public static CMain Form;
- public static AMain PForm;
- public static bool Restart;
- public static bool Launch;
- [STAThread]
- private static void Main(string[] args)
- {
- if (args.Length > 0)
- {
- foreach (var arg in args)
- {
- if (arg.ToLower() == "-tc") Settings.UseTestConfig = true;
- }
- }
- #if DEBUG
- Settings.UseTestConfig = true;
- #endif
- try
- {
- if (UpdatePatcher()) return;
- if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully == true) { }
- Packet.IsServer = false;
- Settings.Load();
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- CheckResolutionSetting();
- Launch = false;
- if (Settings.P_Patcher)
- Application.Run(PForm = new AMain());
- else
- Launch = true;
- if (Launch)
- Application.Run(Form = new CMain());
- Settings.Save();
- if (Restart)
- {
- Application.Restart();
- }
- }
- catch (Exception ex)
- {
- CMain.SaveError(ex.ToString());
- }
- }
- private static bool UpdatePatcher()
- {
- try
- {
- const string fromName = @".\AutoPatcher.gz", toName = @".\AutoPatcher.exe";
- if (!File.Exists(fromName)) return false;
- Process[] processes = Process.GetProcessesByName("AutoPatcher");
- if (processes.Length > 0)
- {
- string patcherPath = Application.StartupPath + @"\AutoPatcher.exe";
- for (int i = 0; i < processes.Length; i++)
- if (processes[i].MainModule.FileName == patcherPath)
- processes[i].Kill();
- Stopwatch stopwatch = Stopwatch.StartNew();
- bool wait = true;
- processes = Process.GetProcessesByName("AutoPatcher");
- while (wait)
- {
- wait = false;
- for (int i = 0; i < processes.Length; i++)
- if (processes[i].MainModule.FileName == patcherPath)
- {
- wait = true;
- }
- if (stopwatch.ElapsedMilliseconds <= 3000) continue;
- MessageBox.Show("Failed to close AutoPatcher during update.");
- return true;
- }
- }
- if (File.Exists(toName)) File.Delete(toName);
- File.Move(fromName, toName);
- Process.Start(toName, "Auto");
- return true;
- }
- catch (Exception ex)
- {
- CMain.SaveError(ex.ToString());
-
- throw;
- }
- }
- public static class RuntimePolicyHelper
- {
- public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }
- static RuntimePolicyHelper()
- {
- //ICLRRuntimeInfo clrRuntimeInfo =
- // (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
- // Guid.Empty,
- // typeof(ICLRRuntimeInfo).GUID);
- //try
- //{
- // clrRuntimeInfo.BindAsLegacyV2Runtime();
- // LegacyV2RuntimeEnabledSuccessfully = true;
- //}
- //catch (COMException)
- //{
- // // This occurs with an HRESULT meaning
- // // "A different runtime was already bound to the legacy CLR version 2 activation policy."
- // LegacyV2RuntimeEnabledSuccessfully = false;
- //}
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
- private interface ICLRRuntimeInfo
- {
- void xGetVersionString();
- void xGetRuntimeDirectory();
- void xIsLoaded();
- void xIsLoadable();
- void xLoadErrorString();
- void xLoadLibrary();
- void xGetProcAddress();
- void xGetInterface();
- void xSetDefaultStartupFlags();
- void xGetDefaultStartupFlags();
- [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
- void BindAsLegacyV2Runtime();
- }
- }
- public static void CheckResolutionSetting()
- {
- var parsedOK = DisplayResolutions.GetDisplayResolutions();
- if (!parsedOK)
- {
- MessageBox.Show(GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.CouldNotGetDisplayResolutions), GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.GetDisplayResolutionIssue), MessageBoxButtons.OK, MessageBoxIcon.Error);
- Environment.Exit(0);
- }
- if (!DisplayResolutions.IsSupported(Settings.Resolution))
- {
- MessageBox.Show(GameLanguage.ClientTextMap.GetLocalization((ClientTextKeys.ClientNotSupportSettingResolution), Settings.Resolution),
- GameLanguage.ClientTextMap.GetLocalization(ClientTextKeys.InvalidClientResolution),
- MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- Settings.Resolution = (int)eSupportedResolution.w1024h768;
- Settings.Save();
- }
- }
- }
- }
|