BattleResourceMgr.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using CommonAI;
  2. using CommonAI.RTS.Manhattan;
  3. using CommonAI.Zone;
  4. using CommonAI.Zone.Helper;
  5. using CommonAI.Zone.ZoneEditor;
  6. using CommonAI.ZoneClient;
  7. using ET;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using XmdsCommon.EditorData;
  12. using XmdsCommon.Plugin;
  13. using Log = ET.Log;
  14. public class BattleResourceMgr : Singleton<BattleResourceMgr>, ISingletonAwake
  15. {
  16. public EditorTemplates GameEditorTemplates;
  17. public SynchronizedBattleCodec BattleMsgDecoder;
  18. public void Awake()
  19. {
  20. InitAsync().Coroutine();
  21. }
  22. public async ETTask InitAsync()
  23. {
  24. GameEditorTemplates = new EditorTemplates("", TemplateManager.MessageCodec);
  25. TemplateManager.setFactory(new XmdsUnityLocalFactory());
  26. var op = await YooAssetProxy.GetRawFileAsync("GameEditor_e1");
  27. var e1 = op.GetRawBytes();
  28. if (e1 == null)
  29. {
  30. Log.Error("not found GameEditor e1 data.");
  31. return;
  32. }
  33. Log.Debug("to load e1");
  34. //var zippedStream = new MemoryStream(e1);
  35. var binLoadStream = new MemoryStream(e1);
  36. //LZMAHelper.Decompress(zippedStream, e1.Length, binLoadStream);
  37. var binMemDic = InitBinFilePostionDic(binLoadStream);
  38. var editorTemplates = this.GameEditorTemplates;
  39. editorTemplates.LoadAllCFG(binMemDic["config"]);
  40. LoadTemplates<CommonAI.Zone.UnitInfo>(editorTemplates, binMemDic["units"]);
  41. LoadTemplates<SkillTemplate>(editorTemplates, binMemDic["skills"]);
  42. LoadTemplates<SpellTemplate>(editorTemplates, binMemDic["spells"]);
  43. LoadTemplates<BuffTemplate>(editorTemplates, binMemDic["buffs"]);
  44. LoadTemplates<ItemTemplate>(editorTemplates, binMemDic["items"]);
  45. LoadTemplates<UnitEventTemplate>(editorTemplates, binMemDic["unit_events"]);
  46. LoadTemplates<UnitTriggerTemplate>(editorTemplates, binMemDic["unit_triggers"]);
  47. LoadSceneSnapData(binMemDic["snaps"]);
  48. LoadNewSceneSnapData(binMemDic["newsnaps"]);
  49. TemplateManager.Formula.InitPluginsData(editorTemplates);
  50. editorTemplates.Templates.RehashAll();
  51. Log.Info("load e1 end");
  52. op = await YooAssetProxy.GetRawFileAsync("GameEditor_ex");
  53. var ex = op.GetRawBytes();
  54. if (ex == null)
  55. {
  56. Log.Error("not found GameEditor ex data.");
  57. return;
  58. }
  59. editorTemplates.SetSceneDataLoader(new SceneDataLoader(ex));
  60. Log.Info("load ex end");
  61. this.BattleMsgDecoder = new SynchronizedBattleCodec(this.GameEditorTemplates.Templates);
  62. }
  63. private static Dictionary<string, MemoryStream> InitBinFilePostionDic(MemoryStream binLoadStream)
  64. {
  65. var binMemDic = new Dictionary<string, MemoryStream>();
  66. var br = new BinaryReader(binLoadStream);
  67. binLoadStream.Position = 0;
  68. var count = br.ReadByte();
  69. var names = new string[count];
  70. var sizes = new int[count];
  71. for (int i = 0; i < count; i++)
  72. {
  73. names[i] = br.ReadString();
  74. sizes[i] = br.ReadInt32();
  75. }
  76. var offset = (int) binLoadStream.Position;
  77. for (int i = 0; i < count; i++)
  78. {
  79. var name = names[i];
  80. var size = sizes[i];
  81. binMemDic[name] = new MemoryStream();
  82. binLoadStream.Position = offset;
  83. binLoadStream.CopyTo(binMemDic[name], size);
  84. binMemDic[name].Position = 0;
  85. offset += size;
  86. }
  87. return binMemDic;
  88. }
  89. public static void LoadTemplates<T>(EditorTemplates template, Stream stream) where T : class, ITemplateData
  90. {
  91. try
  92. {
  93. var br = new BinaryReader(stream);
  94. var n = br.ReadInt32();
  95. for (var i = 0; i < n; i++)
  96. {
  97. var name = br.ReadString();
  98. var len = br.ReadInt32();
  99. var rpos = stream.Position;
  100. try
  101. {
  102. template.LoadTemplate<T>(stream);
  103. }
  104. catch (Exception err)
  105. {
  106. Log.Error("LoadTemplate:{0},{1}", typeof(T), name);
  107. Log.Error(err);
  108. }
  109. finally
  110. {
  111. stream.Position = rpos + len;
  112. }
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Error(e);
  118. }
  119. }
  120. public static void LoadSceneSnapData(Stream stream)
  121. {
  122. try
  123. {
  124. var br = new BinaryReader(stream);
  125. var n = br.ReadInt32();
  126. for (var i = 0; i < n; i++)
  127. {
  128. var _id = br.ReadInt32();
  129. var _len = br.ReadInt32();
  130. var _dat = br.ReadBytes(_len);
  131. try
  132. {
  133. SceneSnapManager.LoadSceneSnapData(_id, _dat);
  134. }
  135. catch (Exception err)
  136. {
  137. Log.Error(err);
  138. }
  139. }
  140. }
  141. catch (Exception e)
  142. {
  143. Log.Error(e);
  144. }
  145. }
  146. public static void LoadNewSceneSnapData(Stream stream)
  147. {
  148. try
  149. {
  150. var br = new BinaryReader(stream);
  151. var n = br.ReadInt32();
  152. for (var i = 0; i < n; i++)
  153. {
  154. var _id = br.ReadInt32();
  155. var _len = br.ReadInt32();
  156. var _dat = br.ReadBytes(_len);
  157. try
  158. {
  159. SceneSnapManager.LoadBinUnitSnapData(_id, _dat);
  160. }
  161. catch (Exception err)
  162. {
  163. Log.Error(err);
  164. }
  165. }
  166. }
  167. catch (Exception e)
  168. {
  169. Log.Error(e);
  170. }
  171. }
  172. public class XmdsUnityLocalFactory : XmdsZoneFactory
  173. {
  174. public override ZoneLayer CreateClientZoneLayer(EditorTemplates templates, ILayerClient listener)
  175. {
  176. return new HZUnityZoneLayer(templates, listener);
  177. }
  178. }
  179. //-----------------------------------------------------------------------------------------------------
  180. #region 接入Untiy寻路
  181. public class HZUnityZoneLayer : XmdsCommon.ZoneClient.HZZoneLayer
  182. {
  183. public HZUnityZoneLayer(EditorTemplates dataroot, ILayerClient client)
  184. : base(dataroot, client)
  185. {
  186. }
  187. protected override void InitTerrain(ClientEnterScene msg, out ZoneManhattanMap terrain_data,
  188. out AstarManhattan path_finder, out ManhattanMapAreaGenerator area_gen)
  189. {
  190. var data = TerrainSrc.Clone() as ZoneInfo;
  191. terrain_data = new ZoneManhattanMap(data, Templates.TerrainDefinition);
  192. path_finder = new HZUnityAstarManhattan(msg.sceneID, terrain_data, true, 0);
  193. area_gen = new ManhattanMapAreaGenerator(terrain_data.Data);
  194. //this.Data.Terrain = null;
  195. this.IsShareTerrain = false;
  196. this.IsIgnoreTerrainTouch = false;
  197. }
  198. }
  199. public class HZUnityAstarManhattan : AstarManhattan
  200. {
  201. public HZUnityAstarManhattan(int sceneId, IManhattanMap map_data, bool inclined = true, int space_size = 0)
  202. : base(sceneId, map_data, inclined, space_size)
  203. {
  204. }
  205. public override FindPathResult findPath(float sx, float sy, float dx, float dy, out MWayPoint ret,
  206. bool optimize = true)
  207. {
  208. // TODO 接入Untiy寻路
  209. // NavMesh.CalculatePath();
  210. return base.findPath(sx, sy, dx, dy, out ret, optimize);
  211. }
  212. }
  213. #endregion
  214. }