FormLauncher.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using CommonFroms.G2D;
  2. using CommonLang.IO;
  3. using CommonLang.Log;
  4. using CommonLang.Xml;
  5. using CommonUI_Unity3D.Impl;
  6. using MPQ.FileSystem;
  7. using MPQ.Resource;
  8. using MPQ.Updater;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Windows.Forms;
  20. using XmdsBattleClientBot;
  21. using XmdsBattleClientBot.Bot;
  22. namespace XmdsBattleClientWin32
  23. {
  24. public partial class FormLauncher : Form
  25. {
  26. public static BotConfig LastConfig { get; private set; }
  27. private static MPQFileSystem FileSystem { get; set; }
  28. private BotConfig Config = new BotConfig();
  29. private Action<FormLauncher, BotConfig> m_OnStart;
  30. public event Action<FormLauncher, BotConfig> OnStart { add { m_OnStart += value; } remove { m_OnStart -= value; } }
  31. public FormLauncher()
  32. {
  33. InitializeComponent();
  34. ListLogger.EnableConsole = false;
  35. }
  36. private void FormLauncher_Load(object sender, EventArgs e)
  37. {
  38. try
  39. {
  40. var saved = XmlUtil.LoadXML(Application.StartupPath + "/bot_config.xml");
  41. if (saved != null)
  42. {
  43. Config = XmlUtil.XmlToObject<BotConfig>(saved);
  44. }
  45. }
  46. catch (Exception err)
  47. {
  48. MessageBox.Show("加载配置失败: " + err.Message);
  49. this.Show();
  50. return;
  51. }
  52. this.prop_Config.SetSelectedObject(Config);
  53. }
  54. private void btn_Start_Click(object sender, EventArgs e)
  55. {
  56. Start();
  57. }
  58. public void Start()
  59. {
  60. var config = XmlUtil.CloneObject(Config);
  61. try
  62. {
  63. var save = XmlUtil.ObjectToXml(Config);
  64. XmlUtil.SaveXML(Application.StartupPath + "/bot_config.xml", save);
  65. if (config.UseMPQ)
  66. {
  67. var task = new DownloadTask(config);
  68. var download = new G2DProgressDialog(task);
  69. download.ShowDialog();
  70. task.InitMPQ(config);
  71. }
  72. this.Hide();
  73. if (config.UseMPQ)
  74. {
  75. config.DataRoot = "mpq:///GameEditor/data";
  76. config.LuaRoot = "mpq:///ui_edit/lua";
  77. }
  78. else if (!Directory.Exists(config.DataRoot))
  79. {
  80. MessageBox.Show("资源地址不存在: " + config.DataRoot);
  81. return;
  82. }
  83. BotClientManager.Init(config);
  84. }
  85. catch (Exception err)
  86. {
  87. MessageBox.Show("初始化失败: " + err);
  88. this.Show();
  89. return;
  90. }
  91. LastConfig = config;
  92. if (m_OnStart != null) { m_OnStart.Invoke(this, config); }
  93. }
  94. private class DownloadTask : IProgress, MPQUpdaterListener
  95. {
  96. private MPQUpdater updater;
  97. private bool done = false;
  98. public DownloadTask(BotConfig cfg)
  99. {
  100. //http://192.168.4.169/sjcs/res_develop/updates_png/update_version.txt
  101. var suffix = "updates_pvr/update_version.txt";
  102. MPQUpdater.ZIP_EXT = ".z";
  103. updater = new MPQUpdater();
  104. updater.Init(
  105. new string[] { cfg.ResourcesURL },
  106. suffix,
  107. new DirectoryInfo(Application.StartupPath + "/remote_mpq"),
  108. new DirectoryInfo(Application.StartupPath + "/local_mpq"),
  109. false, this);
  110. updater.Start();
  111. this.Title = cfg.ResourcesURL + suffix;
  112. }
  113. public void onEvent(MPQUpdater updater, MPQUpdaterEvent e)
  114. {
  115. if (e.EventType == MPQUpdaterEvent.TYPE_COMPLETE)
  116. {
  117. this.done = true;
  118. }
  119. }
  120. public override void Run()
  121. {
  122. while (!done)
  123. {
  124. updater.Update();
  125. switch (updater.CurrentStatus)
  126. {
  127. case MPQUpdater.Status.Downloading:
  128. this.Text = updater.CurrentStatus + " : " + ""; //updater.CurrentDownloadFile;
  129. this.Maximum = (int)((updater.TotalDownloadBytes) / 1024);
  130. this.Value = (int)((updater.CurrentDownloadBytes) / 1024);
  131. break;
  132. case MPQUpdater.Status.Unzipping:
  133. this.Text = updater.CurrentStatus + " : " + "";// updater.CurrentUnzipFile;
  134. this.Maximum = (int)((updater.TotalUnzipBytes) / 1024);
  135. this.Value = (int)((updater.CurrentUnzipBytes) / 1024);
  136. break;
  137. default:
  138. this.Text = updater.CurrentStatus.ToString();
  139. break;
  140. }
  141. Thread.Sleep(1);
  142. }
  143. updater.Dispose();
  144. this.IsDone = true;
  145. }
  146. public bool InitMPQ(BotConfig cfg)
  147. {
  148. if (done)
  149. {
  150. if (FormLauncher.FileSystem != null) { FormLauncher.FileSystem.Dispose(); }
  151. FormLauncher.FileSystem = new MPQFileSystem();
  152. FormLauncher.FileSystem.init(updater);
  153. new MPQResourceLoader(Application.StartupPath, FormLauncher.FileSystem);
  154. return true;
  155. }
  156. return done;
  157. }
  158. }
  159. }
  160. }