BattleResourceMgr.cs 7.3 KB

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