123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using Client.MirNetwork;
- using Launcher;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace Client
- {
- internal static class Program
- {
- public static CMain Form;
- public static AMain PForm;
- public static bool Restart;
- [STAThread]
- private static void Main(string[] args)
- {
- Application.SetCompatibleTextRenderingDefault(false);
- string processName = Process.GetCurrentProcess().ProcessName;
- Process[] processes = Process.GetProcessesByName(processName);
- //如果该数组长度大于1,说明多次运行
- if (processes.Length > 5)
- {
- MessageBox.Show("多开数量不允许大于5!");
- Environment.Exit(1);
- }
- try
- {
- if (File.Exists("updates_rename.log"))
- {
- // MessageBox.Show("检测到未能正确运行 Start.exe ,请确认是否有杀毒软件阻止程序运行。\nStart.exe 在处理完文件复制工作后,启动客户端会被部分杀毒软件误判断为病毒特征而误报,请放心。\n如有误杀,请加入杀毒软件允许白名单,允许程序运行。");
- // MessageBox.Show("更新失败,更新时请关闭所有游戏窗口,\n关闭360和鲁大师,改成火绒安全软件。\n或者把 Start.exe 加入杀毒软件白名单中。");
- Process.Start(".\\Start.exe");
- // System.Environment.Exit(0);
- }
- else
- {
- Settings.Load();
- // 调用公共配置
- Settings.initConfig();
- Network.CheckUpdate();
- if (true)
- {
- Start();
- }
- }
- }
- catch (Exception ex)
- {
- CMain.SaveError(ex.ToString());
- throw;
- }
- }
- internal static void Start()
- {
- try
- {
- if (UpdatePatcher()) return;
- if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully == true) { }
- // kill geargn
- Process[] processes = Process.GetProcessesByName("GearNT");
- for (int i = 0; i < processes.Length; i++)
- processes[i].Kill();
- //string name = Process.GetCurrentProcess().Parent().ProcessName;
- // File.AppendAllText("start.log", name);
- //if ((name == "explorer" || name == "devenv") && Process.GetCurrentProcess().ProcessName.StartsWith("Mir3"))
- //{
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(PForm = new Launcher.AMain());
- //if (Settings.P_Patcher)
- // Application.Run(PForm = new Launcher.AMain());
- //else
- // Application.Run(Form = new CMain());
- // Settings.Save();
- CMain.InputKeys.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("关闭自动补丁程序失败");
- 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 class ProcessExtensions
- {
- private static string FindIndexedProcessName(int pid)
- {
- var processName = Process.GetProcessById(pid).ProcessName;
- var processesByName = Process.GetProcessesByName(processName);
- string processIndexdName = null;
- for (var index = 0; index < processesByName.Length; index++)
- {
- processIndexdName = index == 0 ? processName : processName + "#" + index;
- var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
- if ((int)processId.NextValue() == pid)
- {
- return processIndexdName;
- }
- }
- return processIndexdName;
- }
- private static Process FindPidFromIndexedProcessName(string indexedProcessName)
- {
- var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
- return Process.GetProcessById((int)parentId.NextValue());
- }
- public static Process Parent(this Process process)
- {
- return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
- }
- }
- }
|