BotClientManager.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CommonAI.RTS.Manhattan;
  6. using CommonAI.Zone;
  7. using CommonAI.Zone.Helper;
  8. using CommonAI.Zone.ZoneEditor;
  9. using CommonAI.ZoneClient;
  10. using XmdsBattleClient.Battle;
  11. using XmdsCommon.Plugin;
  12. using XmdsCommon.ZoneClient;
  13. using CommonLang;
  14. using CommonLang.IO;
  15. using System.IO;
  16. namespace XmdsBattleClientBot
  17. {
  18. public static class BotClientManager
  19. {
  20. public static void Init(BotConfig cfg)
  21. {
  22. XmdsBattleManager.Init(cfg.DataRoot, new XmdsZoneFactoryBot());
  23. LoadTaskLua(cfg.LuaRoot);
  24. LoadItemLua(cfg.LuaRoot);
  25. LoadItemQualityLua(cfg.LuaRoot);
  26. //LoadDataLua(cfg.LuaRoot+ "\\data\\Section.lua");
  27. XmdsZoneFactoryBot.SaveMemory = cfg.SaveMemory;
  28. }
  29. //---------------------------------------------------------------------------------------------------------
  30. public static void LoadLuaTable<K>(HashMap<K, Dictionary<string, object>> dst, string file)
  31. {
  32. try
  33. {
  34. var text = Resource.LoadAllText(file);
  35. LuaInterface.Lua lua = new LuaInterface.Lua();
  36. LuaInterface.LuaTable src = lua.DoString(text)[0] as LuaInterface.LuaTable;
  37. LuaInterface.LuaTable keys = src["_key_"] as LuaInterface.LuaTable;
  38. foreach (var k in src.Keys)
  39. {
  40. K id;
  41. if (!k.ToString().Equals("_key_") && Parser.StringToObject<K>(k.ToString(), out id))
  42. {
  43. LuaInterface.LuaTable line = src[k] as LuaInterface.LuaTable;
  44. Dictionary<string, object> fields = new Dictionary<string, object>();
  45. foreach (var i in keys.Keys)
  46. {
  47. fields[keys[i].ToString()] = line[i].ToString();
  48. }
  49. dst[id] = fields;
  50. }
  51. }
  52. }
  53. catch (Exception err)
  54. {
  55. throw new Exception("Load Lua Error : " + file + "\r\n" + err.Message, err);
  56. }
  57. }
  58. //---------------------------------------------------------------------------------------------------------
  59. #region Task
  60. private static HashMap<int, Dictionary<string, object>> s_AllTasksTemplate;
  61. private static void LoadTaskLua(string lua_root)
  62. {
  63. if (s_AllTasksTemplate == null)
  64. {
  65. s_AllTasksTemplate = new HashMap<int, Dictionary<string, object>>();
  66. foreach (var file in Resource.ListFiles(lua_root + @"/Data/Tasks/"))
  67. {
  68. Console.WriteLine(lua_root + @"/Data/Tasks/" + file);
  69. BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/" + file);
  70. }
  71. // BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/BranchLine.lua");
  72. // BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/Daily.lua");
  73. // BotClientManager.LoadLuaTable(s_AllTasksTemplate, lua_root + @"/Data/Tasks/MainLine.lua");
  74. }
  75. }
  76. public static Dictionary<string, object> GetTaskTemplate(int tid)
  77. {
  78. return s_AllTasksTemplate.Get(tid);
  79. }
  80. #endregion
  81. #region Item
  82. private static HashMap<string, Dictionary<string, object>> s_AllItemTemplate;
  83. private static void LoadItemLua(string lua_root)
  84. {
  85. if (s_AllItemTemplate == null)
  86. {
  87. s_AllItemTemplate = new HashMap<string, Dictionary<string, object>>();
  88. foreach (var file in Resource.ListFiles(lua_root + @"/Data/Items/"))
  89. {
  90. Console.WriteLine(lua_root + @"/Data/Items/" + file);
  91. BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/" + file);
  92. }
  93. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/BlueEquip.lua");
  94. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/BlueEquipU.lua");
  95. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Book.lua");
  96. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Chest.lua");
  97. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/GreenEquip.lua");
  98. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/GreenEquipU.lua");
  99. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Jewel.lua");
  100. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/LegendEquip.lua");
  101. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/LegendEquipU.lua");
  102. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Mate.lua");
  103. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Misc.lua");
  104. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Paper.lua");
  105. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PetBook.lua");
  106. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PetItem.lua");
  107. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Potion.lua");
  108. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PurpleEquip.lua");
  109. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/PurpleEquipU.lua");
  110. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Rank.lua");
  111. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Ride.lua");
  112. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/Task.lua");
  113. // BotClientManager.LoadLuaTable(s_AllItemTemplate, lua_root + @"/Data/Items/VipItem.lua");
  114. }
  115. }
  116. public static Dictionary<string, object> GetItemTemplate(string code)
  117. {
  118. return s_AllItemTemplate.Get(code);
  119. }
  120. #endregion
  121. #region ItemQualityConfig
  122. private static HashMap<int, Dictionary<string, object>> s_AllItemQualityTemplate;
  123. private static void LoadItemQualityLua(string lua_root)
  124. {
  125. if (s_AllItemQualityTemplate == null)
  126. {
  127. s_AllItemQualityTemplate = new HashMap<int, Dictionary<string, object>>();
  128. Console.WriteLine(lua_root + @"/Data/ItemQualityConfig.lua");
  129. BotClientManager.LoadLuaTable(s_AllItemQualityTemplate, lua_root + @"/Data/ItemQualityConfig.lua");
  130. }
  131. }
  132. public static Dictionary<string, object> GetItemQuality(int tid)
  133. {
  134. return s_AllItemQualityTemplate.Get(tid);
  135. }
  136. #endregion
  137. //---------------------------------------------------------------------------------------------------------
  138. #region AllLuaConfig
  139. private static HashMap<string,HashMap<int, Dictionary<string, object>>> s_AllLuaConfig;
  140. private static void LoadDataLua(string lua_root)
  141. {
  142. if (s_AllLuaConfig == null)
  143. {
  144. s_AllLuaConfig = new HashMap<string, HashMap<int, Dictionary<string, object>>>();
  145. if (lua_root.EndsWith(".lua"))
  146. {
  147. HashMap<int, Dictionary<string, object>> fileData = new HashMap<int, Dictionary<string, object>>();
  148. Console.WriteLine("load luadata file name ---->>{0}", lua_root);
  149. BotClientManager.LoadLuaTable(fileData, lua_root);
  150. s_AllLuaConfig[lua_root.Substring(lua_root.LastIndexOf('\\') + 1)] = fileData;
  151. }
  152. else
  153. {
  154. foreach (var file in Resource.ListFiles(lua_root))
  155. {
  156. HashMap<int, Dictionary<string, object>> fileData = new HashMap<int, Dictionary<string, object>>();
  157. string fileName = file.ToString().Substring(1);
  158. Console.WriteLine("load luadata file name ---->>{0}", fileName);
  159. BotClientManager.LoadLuaTable(fileData, lua_root + file);
  160. s_AllLuaConfig[fileName] = fileData;
  161. }
  162. }
  163. }
  164. }
  165. public static HashMap<int, Dictionary<string, object>> GetLuaData(string fileName)
  166. {
  167. return s_AllLuaConfig.Get(fileName);
  168. }
  169. public static Dictionary<string, object> GetLuaData(string fileName,int tid)
  170. {
  171. HashMap<int, Dictionary<string, object>> fileData = GetLuaData(fileName);
  172. if (fileData != null) {
  173. return fileData.Get(tid);
  174. }
  175. return null;
  176. }
  177. #endregion
  178. }
  179. }