AddBotConfig.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using DeepCore;
  2. using DeepCore.Reflection;
  3. using DeepCore.Xml;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using DeepMMO.Client.BotTest.Runner;
  12. namespace DeepMMO.Client.BotTest
  13. {
  14. public class AddBotConfig
  15. {
  16. //-------------------------------------------------------------------------------------
  17. [Desc("前缀", "Add")]
  18. public string name_format = "b{0}";
  19. [Desc("登录密码", "Add")]
  20. public string password = "123456";
  21. [Desc("计数器", "Add")]
  22. public int index = 0;
  23. [Desc("数量", "Add")]
  24. public int count = 10;
  25. [Desc("名字格式", "Add")]
  26. public string digit_format = "D6";
  27. [Desc("ServerID", "Add")]
  28. [OptionalValue()]
  29. public string serverID = "0";
  30. //-------------------------------------------------------------------------------------
  31. [Desc("随机角色名", "角色")]
  32. public bool RandomRoleName = false;
  33. [Desc("角色名格式", "角色")]
  34. [DependOnProperty(nameof(RandomRoleName), false)]
  35. public string[] RoleNameFormat = new string[] { "角色{0}" };
  36. //-------------------------------------------------------------------------------------
  37. [Desc("模块配置", "模块")]
  38. [Expandable]
  39. public List<object> ModuleConfigs = new List<object>();
  40. //-------------------------------------------------------------------------------------
  41. public static AddBotConfig TryLoadAddConfig()
  42. {
  43. var add = BotFactory.Instance.CreateAddBotConfig();
  44. try
  45. {
  46. var saved = XmlUtil.LoadXML(Application.StartupPath + "/bot_add.save");
  47. if (saved != null)
  48. {
  49. add = XmlUtil.XmlToObject<AddBotConfig>(saved);
  50. }
  51. add.ModuleConfigs.Clear();
  52. var mts = BotFactory.Instance.GetModuleTypes();
  53. foreach (var mt in mts)
  54. {
  55. var mt_config = mt.GetNestedType("Config");
  56. if (mt_config != null)
  57. {
  58. add.ModuleConfigs.Add(Activator.CreateInstance(mt_config));
  59. }
  60. }
  61. foreach (object mt_config in add.ModuleConfigs)
  62. {
  63. var type = mt_config.GetType();
  64. LoadModule(type.DeclaringType.Name, type);
  65. }
  66. }
  67. catch (Exception err)
  68. {
  69. MessageBox.Show(err.Message);
  70. }
  71. return add;
  72. }
  73. public static void TrySaveAddConfig(AddBotConfig add)
  74. {
  75. if (BotLauncher.IsAuto == false)
  76. {
  77. var save = XmlUtil.ObjectToXml(add);
  78. XmlUtil.SaveXML(Application.StartupPath + "/bot_add.save", save);
  79. foreach (object mt_config in add.ModuleConfigs)
  80. {
  81. var type = mt_config.GetType();
  82. SaveModule(type.DeclaringType.Name, type);
  83. }
  84. }
  85. }
  86. private static void SaveModule(string name, Type type)
  87. {
  88. var prop = new DeepCore.Properties();
  89. prop.SaveStaticFields(type);
  90. File.WriteAllText(Application.StartupPath + "/bot.module." + name + ".save", prop.ToString(), CUtils.UTF8);
  91. }
  92. private static void LoadModule(string name, Type type)
  93. {
  94. if (File.Exists(Application.StartupPath + "/bot.module." + name + ".save"))
  95. {
  96. var text = File.ReadAllText(Application.StartupPath + "/bot.module." + name + ".save", CUtils.UTF8);
  97. if (text != null)
  98. {
  99. var prop = new DeepCore.Properties();
  100. prop.TryParseText(text);
  101. prop.LoadStaticFields(type);
  102. }
  103. }
  104. }
  105. }
  106. //-------------------------------------------------------------------------------------
  107. public class BotModuleConfig
  108. {
  109. [Desc("Modules")]
  110. [Expandable()]
  111. public HashMap<string, bool> Modules = new HashMap<string, bool>();
  112. }
  113. }