FormBotTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using CommonFroms.G2D;
  2. using CommonLang;
  3. using CommonLang.Property;
  4. using CommonLang.Xml;
  5. using Pomelo.DotNetClient;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using XmdsBattleClientBot;
  17. using XmdsBattleClientBot.Bot;
  18. using XmdsBattleClientWin32.Battle;
  19. using XmdsBotTest.Runner;
  20. namespace XmdsBotTest
  21. {
  22. public partial class FormBotTest : Form
  23. {
  24. private static bool first_start = true;
  25. private int lastIndex = 0;
  26. private long last_update_time;
  27. private int totalTestCount = 0;
  28. private BotConfig config;
  29. public FormBotTest(BotConfig cfg)
  30. {
  31. this.config = cfg;
  32. InitializeComponent();
  33. InitBotModules();
  34. }
  35. public BotListViewItem SelectedBotItem
  36. {
  37. get
  38. {
  39. if (list_Bots.SelectedItems.Count > 0)
  40. {
  41. return list_Bots.SelectedItems[0] as BotListViewItem;
  42. }
  43. return null;
  44. }
  45. }
  46. private void FormBotTest_Load(object sender, EventArgs e)
  47. {
  48. }
  49. private void FormBotTest_Shown(object sender, EventArgs e)
  50. {
  51. if (LaunchArgs.IsAuto && first_start)
  52. {
  53. first_start = false;
  54. StartBots(LaunchArgs.DefaultBotPrefix, LaunchArgs.DefaultBotCount);
  55. }
  56. }
  57. private void FormBotTest_FormClosing(object sender, FormClosingEventArgs e)
  58. {
  59. foreach (BotListViewItem item in list_Bots.Items)
  60. {
  61. try
  62. {
  63. item.Dispose();
  64. }
  65. catch (Exception err)
  66. {
  67. Console.WriteLine(err.Message);
  68. }
  69. }
  70. }
  71. private void btn_GC_Click(object sender, EventArgs e)
  72. {
  73. System.GC.Collect();
  74. }
  75. private void timer_Update_Tick(object sender, EventArgs e)
  76. {
  77. if (base.Visible)
  78. {
  79. long curTime = CommonLang.CUtils.CurrentTimeMS;
  80. if (last_update_time == 0)
  81. {
  82. last_update_time = curTime;
  83. }
  84. int intervalMS = (int)(curTime - last_update_time);
  85. last_update_time = curTime;
  86. intervalMS = Math.Min(intervalMS, timer_Update.Interval * 2);
  87. foreach (BotListViewItem item in list_Bots.Items)
  88. {
  89. item.Update(intervalMS);
  90. }
  91. }
  92. var selected = SelectedBotItem;
  93. if (text_Events.Tag != selected)
  94. {
  95. text_Events.Tag = selected;
  96. text_Events.Clear();
  97. }
  98. if (selected != null)
  99. {
  100. if (text_Events.TextLength < selected.Events.Length)
  101. {
  102. char[] copyto = new char[selected.Events.Length - text_Events.TextLength];
  103. selected.Events.CopyTo(text_Events.TextLength, copyto, 0, copyto.Length);
  104. text_Events.AppendText(new string(copyto));
  105. }
  106. if (text_Events.TextLength > selected.Events.Length)
  107. {
  108. text_Events.Clear();
  109. }
  110. }
  111. }
  112. private void timer_RefreshList_Tick(object sender, EventArgs e)
  113. {
  114. foreach (BotListViewItem item in list_Bots.Items)
  115. {
  116. item.Refresh();
  117. }
  118. lbl_NetStatus.Text = PomeloStatus.Status;
  119. }
  120. private void btn_AddBots_Click(object sender, EventArgs e)
  121. {
  122. StartBots();
  123. }
  124. private void btn_StopAll_Click(object sender, EventArgs e)
  125. {
  126. foreach (BotListViewItem item in list_Bots.Items)
  127. {
  128. try
  129. {
  130. item.Stop();
  131. }
  132. catch (Exception err)
  133. {
  134. Console.WriteLine(err.Message);
  135. }
  136. }
  137. }
  138. private void btn_ClearConsole_Click(object sender, EventArgs e)
  139. {
  140. text_Events.Clear();
  141. var selected = SelectedBotItem;
  142. if (selected != null)
  143. {
  144. selected.Events.Clear();
  145. }
  146. }
  147. private void btn_ClearAllConsole_Click(object sender, EventArgs e)
  148. {
  149. text_Events.Clear();
  150. foreach (BotListViewItem item in list_Bots.Items)
  151. {
  152. item.Events.Clear();
  153. }
  154. }
  155. private void list_Bots_SelectedIndexChanged(object sender, EventArgs e)
  156. {
  157. splitContainer2.Panel1.Controls.Clear();
  158. var selected = SelectedBotItem;
  159. if (selected != null && selected.BattleView != null)
  160. {
  161. splitContainer2.Panel1.Controls.Add(selected.BattleView);
  162. }
  163. }
  164. private void moduleItem_CheckedChanged(object sender, EventArgs e)
  165. {
  166. var item = sender as ToolStripMenuItem;
  167. var mt = item.Tag as Type;
  168. BotRunner.RunnerModule.SetModuleEnable(mt, item.Checked);
  169. SaveBotModules();
  170. }
  171. private void childItem_CheckedChanged(object sender ,EventArgs e) {
  172. var item = sender as ToolStripMenuItem;
  173. var mt = item.Tag as Type;
  174. string fullName = mt.Namespace + "." + mt.Name + "+Config";
  175. Type type = Type.GetType(fullName);
  176. ConfigBase obj = ReflectionUtil.CreateInstance(type) as ConfigBase;
  177. obj.popG2DPropertyDialog();
  178. AddBotConfig.SaveProp(mt.Name, type);
  179. }
  180. private void menu_BotItem_Opening(object sender, CancelEventArgs e)
  181. {
  182. var menu = sender as ContextMenuStrip;
  183. if (menu != null)
  184. {
  185. var mp = menu.PointToScreen(new Point(0, 0));
  186. var lp = list_Bots.PointToClient(mp);
  187. var item = list_Bots.GetItemAt(lp.X, lp.Y);
  188. if (item != null)
  189. {
  190. item.Selected = true;
  191. var selected = SelectedBotItem;
  192. if (selected == null)
  193. {
  194. e.Cancel = true;
  195. }
  196. }
  197. else
  198. {
  199. e.Cancel = true;
  200. }
  201. }
  202. }
  203. private void btn_BotReconnect_Click(object sender, EventArgs e)
  204. {
  205. var selected = SelectedBotItem;
  206. if (selected != null)
  207. {
  208. selected.Runner.reconnect();
  209. }
  210. }
  211. private void btn_BotStop_Click(object sender, EventArgs e)
  212. {
  213. var selected = SelectedBotItem;
  214. if (selected != null)
  215. {
  216. selected.Runner.Stop();
  217. totalTestCount = 0;
  218. }
  219. }
  220. //-------------------------------------------------------------------------------------------------------------
  221. #region OP
  222. private void StartBots(string name_prefix = null, int count = 1)
  223. {
  224. var add = AddBotConfig.TryLoadAddConfig();
  225. if (name_prefix != null)
  226. {
  227. add.name_format = name_prefix;
  228. add.count = count;
  229. }
  230. else
  231. {
  232. add.index = lastIndex;
  233. add = G2DPropertyDialog<AddBotConfig>.Show("add robot", add);
  234. }
  235. if (add != null)
  236. {
  237. totalTestCount += add.count;
  238. this.Text = "BotTest : " + add.name_format + " (" + totalTestCount + ")";
  239. try
  240. {
  241. if (add.name_format.Contains("{0}") && !string.IsNullOrEmpty(add.digit_format))
  242. {
  243. for (int i = 0; i < add.count; i++)
  244. {
  245. var id = add.index + i;
  246. var name = string.Format(add.name_format, id.ToString(add.digit_format));
  247. var item = new BotListViewItem(name, id, add, config);
  248. list_Bots.Items.Add(item);
  249. //item.Start();
  250. }
  251. }
  252. else
  253. {
  254. var name = add.name_format;
  255. var item = new BotListViewItem(name, lastIndex, add, config);
  256. list_Bots.Items.Add(item);
  257. item.Start();
  258. }
  259. }
  260. catch (Exception err)
  261. {
  262. MessageBox.Show(err.Message);
  263. }
  264. lastIndex = add.index + add.count;
  265. AddBotConfig.TrySaveAddConfig(add);
  266. }
  267. }
  268. private void InitBotModules()
  269. {
  270. try
  271. {
  272. var saved = XmlUtil.LoadXML(Application.StartupPath + "/bot_modules.xml");
  273. if (saved != null)
  274. {
  275. var cfg = XmlUtil.XmlToObject<BotModuleConfig>(saved);
  276. foreach (var me in cfg.Modules)
  277. {
  278. var mt = ReflectionUtil.GetType(me.Key);
  279. if (mt != null)
  280. {
  281. BotRunner.RunnerModule.SetModuleEnable(mt, me.Value);
  282. }
  283. }
  284. }
  285. }
  286. catch (Exception err)
  287. {
  288. MessageBox.Show(err.Message);
  289. }
  290. foreach (var mt in BotRunner.RunnerModule.GetSubTypes())
  291. {
  292. var enable = BotRunner.RunnerModule.GetModuleEnable(mt);
  293. var item = new ToolStripMenuItem();
  294. item.CheckOnClick = true;
  295. item.Size = new System.Drawing.Size(152, 22);
  296. BotRunner.RunnerModule _module = ReflectionUtil.CreateInstance(mt) as BotRunner.RunnerModule;
  297. item.Text = _module.module_name + "(" + mt.Name+ ")";
  298. item.Tag = mt;
  299. item.Checked = enable;
  300. item.CheckedChanged += moduleItem_CheckedChanged;
  301. if (mt.Name.EndsWith("Func"))
  302. {
  303. var childItem = new ToolStripMenuItem();
  304. childItem.Text = "修改配置";
  305. childItem.Tag = mt;
  306. childItem.CheckOnClick = true;
  307. childItem.CheckedChanged += childItem_CheckedChanged;
  308. item.DropDownItems.Add(childItem);
  309. func_module.DropDownItems.Add(item);
  310. }
  311. else if (mt.Name.EndsWith("Warn"))
  312. {
  313. var childItem = new ToolStripMenuItem();
  314. childItem.Text = "修改配置";
  315. childItem.Tag = mt;
  316. childItem.CheckOnClick = true;
  317. childItem.CheckedChanged += childItem_CheckedChanged;
  318. item.DropDownItems.Add(childItem);
  319. warn_module.DropDownItems.Add(item);
  320. }
  321. else if (mt.Name.EndsWith("Proto")) {
  322. var childItem = new ToolStripMenuItem();
  323. childItem.Text = "修改配置";
  324. childItem.Tag = mt;
  325. childItem.CheckOnClick = true;
  326. childItem.CheckedChanged += childItem_CheckedChanged;
  327. item.DropDownItems.Add(childItem);
  328. proto_module.DropDownItems.Add(item);
  329. }
  330. else
  331. {
  332. var childItem = new ToolStripMenuItem();
  333. childItem.Text = "修改配置";
  334. childItem.Tag = mt;
  335. childItem.CheckOnClick = true;
  336. childItem.CheckedChanged += childItem_CheckedChanged;
  337. item.DropDownItems.Add(childItem);
  338. group_Module.DropDownItems.Add(item);
  339. }
  340. }
  341. }
  342. private void SaveBotModules()
  343. {
  344. BotModuleConfig cfg = new BotModuleConfig();
  345. foreach (var mt in BotRunner.RunnerModule.GetSubTypes())
  346. {
  347. cfg.Modules.Add(mt.FullName, BotRunner.RunnerModule.GetModuleEnable(mt));
  348. }
  349. var save = XmlUtil.ObjectToXml(cfg);
  350. XmlUtil.SaveXML(Application.StartupPath + "/bot_modules.xml", save);
  351. }
  352. #endregion
  353. private void connect_Click(object sender, EventArgs e)
  354. {
  355. var selected = SelectedBotItem;
  356. if (selected != null)
  357. {
  358. selected.Runner.connect();
  359. }
  360. }
  361. private void tsi_leave_Click(object sender, EventArgs e)
  362. {
  363. var selected = SelectedBotItem;
  364. if (selected != null)
  365. {
  366. selected.Runner.leaveScene();
  367. }
  368. }
  369. }
  370. public class BotListViewItem : IBotListViewItem
  371. {
  372. public BotClient Client { get; private set; }
  373. public BotRunner Runner { get; private set; }
  374. public StringBuilder Events { get; private set; }
  375. public BattlePanelContainer BattleView { get; private set; }
  376. public BotListViewItem(string name, int index, AddBotConfig add, BotConfig cfg)
  377. : base(name)
  378. {
  379. var client = new BotClient(name, add.password, cfg, this);
  380. var bot = new BotRunner(client, index, add);
  381. this.Client = client;
  382. this.Runner = bot;
  383. this.Events = new StringBuilder();
  384. if (!cfg.NoBattleView)
  385. {
  386. this.BattleView = new BattlePanelContainer(client);
  387. this.BattleView.Dock = DockStyle.Fill;
  388. }
  389. this.Tag = bot;
  390. this.SubItems.Add(bot.NetState.ToString());
  391. this.SubItems.Add(bot.RoleName);
  392. this.SubItems.Add(bot.SceneName);
  393. this.SubItems.Add(bot.Status);
  394. }
  395. public override void Start()
  396. {
  397. Runner.Start();
  398. }
  399. public override void Stop()
  400. {
  401. Runner.Stop();
  402. }
  403. public override void Dispose()
  404. {
  405. Runner.Dispose();
  406. }
  407. public void Update(int intervalMS)
  408. {
  409. Client.Update(intervalMS);
  410. if (BattleView != null)
  411. {
  412. BattleView.UpdateBattle(intervalMS);
  413. }
  414. }
  415. private bool reconnect;
  416. private long disconnectTime;
  417. public void Refresh()
  418. {
  419. if (Events.Length > 65535)
  420. {
  421. Events.Clear();
  422. }
  423. using (var list = ListObjectPool<string>.AllocAutoRelease())
  424. {
  425. this.Runner.PopLogs(list);
  426. foreach (var e in list)
  427. {
  428. Events.AppendLine(e);
  429. }
  430. }
  431. string st = Runner.NetState.ToString() + ":" + Runner.Bot.Client.EntryServerElapsedMS;
  432. if (this.SubItems[1].Text != st)
  433. this.SubItems[1].Text = st;
  434. if (this.SubItems[2].Text != Runner.RoleName)
  435. this.SubItems[2].Text = Runner.RoleName;
  436. if (this.SubItems[3].Text != Runner.SceneName)
  437. this.SubItems[3].Text = Runner.SceneName;
  438. if (this.SubItems[4].Text != Runner.Status)
  439. this.SubItems[4].Text = Runner.Status;
  440. if (Runner.Status != "" && Runner.SceneName == "")
  441. {
  442. if (CUtils.CurrentTimeMS > disconnectTime + 30000)
  443. {
  444. if (reconnect)
  445. {
  446. //Console.WriteLine("AUTO HOOK RECONNECT...");
  447. Runner.reconnect();
  448. reconnect = false;
  449. }
  450. reconnect = !reconnect;
  451. disconnectTime = CUtils.CurrentTimeMS;
  452. }
  453. }
  454. else
  455. {
  456. reconnect = false;
  457. }
  458. }
  459. }
  460. }