Program.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using Client.MirNetwork;
  2. using Launcher;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. using System.Windows.Forms;
  9. namespace Client
  10. {
  11. internal static class Program
  12. {
  13. public static CMain Form;
  14. public static AMain PForm;
  15. public static bool Restart;
  16. [STAThread]
  17. private static void Main(string[] args)
  18. {
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. string processName = Process.GetCurrentProcess().ProcessName;
  21. Process[] processes = Process.GetProcessesByName(processName);
  22. //如果该数组长度大于1,说明多次运行
  23. if (processes.Length > 5)
  24. {
  25. MessageBox.Show("多开数量不允许大于5!");
  26. Environment.Exit(1);
  27. }
  28. try
  29. {
  30. if (File.Exists("updates_rename.log"))
  31. {
  32. // MessageBox.Show("检测到未能正确运行 Start.exe ,请确认是否有杀毒软件阻止程序运行。\nStart.exe 在处理完文件复制工作后,启动客户端会被部分杀毒软件误判断为病毒特征而误报,请放心。\n如有误杀,请加入杀毒软件允许白名单,允许程序运行。");
  33. // MessageBox.Show("更新失败,更新时请关闭所有游戏窗口,\n关闭360和鲁大师,改成火绒安全软件。\n或者把 Start.exe 加入杀毒软件白名单中。");
  34. Process.Start(".\\Start.exe");
  35. // System.Environment.Exit(0);
  36. }
  37. else
  38. {
  39. Settings.Load();
  40. // 调用公共配置
  41. Settings.initConfig();
  42. Network.CheckUpdate();
  43. if (true)
  44. {
  45. Start();
  46. }
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. CMain.SaveError(ex.ToString());
  52. throw;
  53. }
  54. }
  55. internal static void Start()
  56. {
  57. try
  58. {
  59. if (UpdatePatcher()) return;
  60. if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully == true) { }
  61. // kill geargn
  62. Process[] processes = Process.GetProcessesByName("GearNT");
  63. for (int i = 0; i < processes.Length; i++)
  64. processes[i].Kill();
  65. //string name = Process.GetCurrentProcess().Parent().ProcessName;
  66. // File.AppendAllText("start.log", name);
  67. //if ((name == "explorer" || name == "devenv") && Process.GetCurrentProcess().ProcessName.StartsWith("Mir3"))
  68. //{
  69. Application.EnableVisualStyles();
  70. Application.SetCompatibleTextRenderingDefault(false);
  71. Application.Run(PForm = new Launcher.AMain());
  72. //if (Settings.P_Patcher)
  73. // Application.Run(PForm = new Launcher.AMain());
  74. //else
  75. // Application.Run(Form = new CMain());
  76. // Settings.Save();
  77. CMain.InputKeys.Save();
  78. if (Restart)
  79. {
  80. Application.Restart();
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. CMain.SaveError(ex.ToString());
  86. }
  87. }
  88. private static bool UpdatePatcher()
  89. {
  90. try
  91. {
  92. const string fromName = @".\AutoPatcher.gz", toName = @".\AutoPatcher.exe";
  93. if (!File.Exists(fromName)) return false;
  94. Process[] processes = Process.GetProcessesByName("AutoPatcher");
  95. if (processes.Length > 0)
  96. {
  97. string patcherPath = Application.StartupPath + @"\AutoPatcher.exe";
  98. for (int i = 0; i < processes.Length; i++)
  99. if (processes[i].MainModule.FileName == patcherPath)
  100. processes[i].Kill();
  101. Stopwatch stopwatch = Stopwatch.StartNew();
  102. bool wait = true;
  103. processes = Process.GetProcessesByName("AutoPatcher");
  104. while (wait)
  105. {
  106. wait = false;
  107. for (int i = 0; i < processes.Length; i++)
  108. if (processes[i].MainModule.FileName == patcherPath)
  109. {
  110. wait = true;
  111. }
  112. if (stopwatch.ElapsedMilliseconds <= 3000) continue;
  113. MessageBox.Show("关闭自动补丁程序失败");
  114. return true;
  115. }
  116. }
  117. if (File.Exists(toName)) File.Delete(toName);
  118. File.Move(fromName, toName);
  119. Process.Start(toName, "Auto");
  120. return true;
  121. }
  122. catch (Exception ex)
  123. {
  124. CMain.SaveError(ex.ToString());
  125. throw;
  126. }
  127. }
  128. public static class RuntimePolicyHelper
  129. {
  130. public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }
  131. static RuntimePolicyHelper()
  132. {
  133. ICLRRuntimeInfo clrRuntimeInfo =
  134. (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
  135. Guid.Empty,
  136. typeof(ICLRRuntimeInfo).GUID);
  137. try
  138. {
  139. clrRuntimeInfo.BindAsLegacyV2Runtime();
  140. LegacyV2RuntimeEnabledSuccessfully = true;
  141. }
  142. catch (COMException)
  143. {
  144. // This occurs with an HRESULT meaning
  145. // "A different runtime was already bound to the legacy CLR version 2 activation policy."
  146. LegacyV2RuntimeEnabledSuccessfully = false;
  147. }
  148. }
  149. [ComImport]
  150. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  151. [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
  152. private interface ICLRRuntimeInfo
  153. {
  154. void xGetVersionString();
  155. void xGetRuntimeDirectory();
  156. void xIsLoaded();
  157. void xIsLoadable();
  158. void xLoadErrorString();
  159. void xLoadLibrary();
  160. void xGetProcAddress();
  161. void xGetInterface();
  162. void xSetDefaultStartupFlags();
  163. void xGetDefaultStartupFlags();
  164. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  165. void BindAsLegacyV2Runtime();
  166. }
  167. }
  168. }
  169. public static class ProcessExtensions
  170. {
  171. private static string FindIndexedProcessName(int pid)
  172. {
  173. var processName = Process.GetProcessById(pid).ProcessName;
  174. var processesByName = Process.GetProcessesByName(processName);
  175. string processIndexdName = null;
  176. for (var index = 0; index < processesByName.Length; index++)
  177. {
  178. processIndexdName = index == 0 ? processName : processName + "#" + index;
  179. var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
  180. if ((int)processId.NextValue() == pid)
  181. {
  182. return processIndexdName;
  183. }
  184. }
  185. return processIndexdName;
  186. }
  187. private static Process FindPidFromIndexedProcessName(string indexedProcessName)
  188. {
  189. var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
  190. return Process.GetProcessById((int)parentId.NextValue());
  191. }
  192. public static Process Parent(this Process process)
  193. {
  194. return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
  195. }
  196. }
  197. }