FormLauncher.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using DeepCore;
  2. using DeepCore.Log;
  3. using DeepCore.MPQ;
  4. using DeepCore.MPQ.Updater;
  5. using DeepCore.Xml;
  6. using DeepEditor.Common.G2D;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. namespace DeepMMO.Client.BotTest
  13. {
  14. public partial class FormLauncher : Form
  15. {
  16. private static MPQFileSystem FileSystem { get; set; }
  17. private BotConfig bot_config;
  18. private BotConfigHistory bot_config_history = new BotConfigHistory();
  19. public static BotConfig LastConfig { get; private set; }
  20. public BotConfig Config
  21. {
  22. get { return bot_config; }
  23. set { SetConfig(bot_config); }
  24. }
  25. private Action<FormLauncher, BotConfig> m_OnStart;
  26. public event Action<FormLauncher, BotConfig> OnStart { add { m_OnStart += value; } remove { m_OnStart -= value; } }
  27. public FormLauncher()
  28. {
  29. this.bot_config = BotFactory.Instance.CreateBotConfig();
  30. InitializeComponent();
  31. }
  32. public void SetConfig(BotConfig value)
  33. {
  34. bot_config = value;
  35. var desc = this.prop_Config.SetSelectedObject(bot_config);
  36. desc.AppendOptionals(bot_config_history.List);
  37. bot_config_history.List = desc.GetOptionalsMap();
  38. this.prop_Config.Refresh();
  39. }
  40. private void FormLauncher_Load(object sender, EventArgs e)
  41. {
  42. try
  43. {
  44. var saved = XmlUtil.LoadXML(Application.StartupPath + "/bot_config.save");
  45. if (saved != null)
  46. {
  47. this.bot_config = XmlUtil.XmlToObject(BotFactory.Instance.CreateBotConfig().GetType(), saved) as BotConfig;
  48. }
  49. var saved_history = XmlUtil.LoadXML(Application.StartupPath + "/bot_config_history.save");
  50. if (saved_history != null)
  51. {
  52. this.bot_config_history = XmlUtil.XmlToObject<BotConfigHistory>(saved_history);
  53. }
  54. }
  55. catch (Exception err)
  56. {
  57. MessageBox.Show("加载配置失败: " + err.Message);
  58. this.Show();
  59. return;
  60. }
  61. }
  62. private void FormLauncher_Shown(object sender, EventArgs e)
  63. {
  64. this.SetConfig(bot_config);
  65. }
  66. private void btn_Start_Click(object sender, EventArgs e)
  67. {
  68. Start();
  69. }
  70. public void Start()
  71. {
  72. var config = XmlUtil.CloneObject(Config);
  73. try
  74. {
  75. if (BotLauncher.IsAuto == false)
  76. {
  77. var save = XmlUtil.ObjectToXml(Config);
  78. XmlUtil.SaveXML(Application.StartupPath + "/bot_config.save", save);
  79. }
  80. var desc = this.prop_Config.SelectedDescriptorObject;
  81. this.prop_Config.AppendCurrentToHistory();
  82. try
  83. {
  84. bot_config_history.List = desc.GetOptionalsMap();
  85. }
  86. catch { }
  87. if (BotLauncher.IsAuto == false)
  88. {
  89. var save_history = XmlUtil.ObjectToXml(bot_config_history);
  90. XmlUtil.SaveXML(Application.StartupPath + "/bot_config_history.save", save_history);
  91. }
  92. this.Hide();
  93. BotFactory.Instance.Init(config);
  94. }
  95. catch (Exception err)
  96. {
  97. MessageBox.Show("初始化失败: " + err.Message);
  98. this.Show();
  99. return;
  100. }
  101. LastConfig = config;
  102. if (m_OnStart != null) { m_OnStart.Invoke(this, config); }
  103. }
  104. }
  105. }