AssistantForm.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Mir2Assistant.Common.Functions;
  2. using Mir2Assistant.Common.Models;
  3. using Mir2Assistant.Common.TabForms;
  4. using Mir2Assistant.Common.Utils;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices;
  9. namespace Mir2Assistant
  10. {
  11. public partial class AssistantForm : Form
  12. {
  13. public MirGameInstanceModel? gameInstance { get; set; }
  14. private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
  15. public CharacterStatusModel CharacterStatus = new CharacterStatusModel();
  16. public AssistantForm()
  17. {
  18. InitializeComponent();
  19. }
  20. public AssistantForm(MirGameInstanceModel instance)
  21. {
  22. gameInstance = instance;
  23. InitializeComponent();
  24. }
  25. private void AssistantForm_Load(object sender, EventArgs e)
  26. {
  27. gameInstance!.MemoryUtils = new MemoryUtils(gameInstance);
  28. var configPath = Path.Combine(Directory.GetCurrentDirectory(), "config", $"{gameInstance.mirVer}.ini");
  29. if (!File.Exists(configPath))
  30. {
  31. MessageBox.Show($"当前版本{gameInstance.mirVer},暂无该版本的配置文件");
  32. return;
  33. }
  34. var cfgStr = File.ReadAllText(configPath, System.Text.Encoding.GetEncoding("gb2312"));
  35. foreach (var cfg in cfgStr.Split('\n'))
  36. {
  37. if (cfg?.Contains("=") ?? false)
  38. {
  39. gameInstance.MirConfig.Add(cfg.Split('=')[0].Trim(), Convert.ToInt32(cfg.Split('=')[1].Trim(), 16));
  40. }
  41. }
  42. //加载tab页
  43. string currentDirectory = Directory.GetCurrentDirectory();
  44. string[] dllFiles = Directory.GetFiles(currentDirectory, "*.dll");
  45. var tabForms = new List<Form>();
  46. foreach (string dllPath in dllFiles)
  47. {
  48. try
  49. {
  50. var assembly = Assembly.LoadFrom(dllPath);
  51. var forms = assembly.GetTypes().Where(o => o.IsClass && o.IsAssignableTo(typeof(ITabForm))).OrderBy(o => o.GetCustomAttribute<OrderAttribute>()?.Order);
  52. foreach (var form in forms)
  53. {
  54. var fm = (Form)assembly.CreateInstance(form.FullName!)!;
  55. ((ITabForm)fm).GameInstance = gameInstance;
  56. fm.FormBorderStyle = FormBorderStyle.None;
  57. fm.TopLevel = false;
  58. fm.ControlBox = false;
  59. fm.Dock = DockStyle.Fill;
  60. fm.Show();
  61. tabForms.Add(fm);
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. Console.WriteLine("Failed to load " + dllPath + ": " + ex.Message);
  67. }
  68. }
  69. foreach (var fm in tabForms.OrderBy(o => (o.GetType()?.GetCustomAttribute<OrderAttribute>()?.Order ?? 9999)).ToList())
  70. {
  71. var tp = new TabPage();
  72. tp.Text = ((ITabForm)fm).Title;
  73. fm.Parent = tp;
  74. tabControl.TabPages.Add(tp);
  75. }
  76. //读状态
  77. Task.Run(() =>
  78. {
  79. try
  80. {
  81. while (gameInstance.LibIpdl > 0)
  82. {
  83. Task.Delay(500).Wait();
  84. if (cancellationTokenSource.Token.IsCancellationRequested)
  85. {
  86. cancellationTokenSource.Token.ThrowIfCancellationRequested();
  87. break;
  88. }
  89. CharacterStatusFunction.GetInfo(gameInstance);
  90. if (gameInstance.CharacterStatus!.MaxMP > 0 && gameInstance.CharacterStatus.MaxHP > 0)
  91. {
  92. if (gameInstance.Skills.Count == 0)
  93. {
  94. this.Invoke(() => SendMirCall.Send(gameInstance, 9001, [gameInstance.MirConfig["写屏CALL地址"], this.Handle]));
  95. SkillFunction.ReadSkills(gameInstance);
  96. }
  97. MonsterFunction.ReadMonster(gameInstance);
  98. }
  99. else //已小退
  100. {
  101. gameInstance.Skills.Clear();
  102. gameInstance.Monsters.Clear();
  103. this.Invoke(() => SendMirCall.Send(gameInstance, 9002, new nint[] { }));
  104. }
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. if (ex.Message.EndsWith("is not running."))
  110. {
  111. //游戏退出了
  112. this.Invoke(() =>
  113. {
  114. DllInject.Unhook(gameInstance);
  115. this.Dispose();
  116. });
  117. }
  118. //将异常抛给主线程
  119. //this.BeginInvoke(new EventHandler(delegate { throw ex; }));
  120. }
  121. });
  122. }
  123. private void AssistantForm_FormClosing(object sender, FormClosingEventArgs e)
  124. {
  125. e.Cancel = true;
  126. this.Hide();
  127. }
  128. protected override void WndProc(ref Message m)
  129. {
  130. const int WM_COPYDATA = 0x004A;
  131. if (m.Msg == WM_COPYDATA)
  132. {
  133. // Extract the string from the COPYDATASTRUCT
  134. COPYDATASTRUCT cds = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT))!;
  135. string? msg = Marshal.PtrToStringAnsi(cds.lpData);
  136. var flag = m.WParam;
  137. gameInstance!.InvokeSysMsg(flag, msg);
  138. }
  139. base.WndProc(ref m);
  140. }
  141. // Define the COPYDATASTRUCT for C#
  142. private struct COPYDATASTRUCT
  143. {
  144. public IntPtr dwData;
  145. public int cbData;
  146. public IntPtr lpData;
  147. }
  148. }
  149. }