SnapData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.ZoneEditor;
  3. using CommonLang;
  4. using CommonLang.IO;
  5. using CommonLang.IO.Attribute;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. namespace XmdsCommon.EditorData
  12. {
  13. public static class SceneSnapManager
  14. {
  15. private static string mSnapDataRoot;
  16. private static string mSnapDataRootExt;
  17. public static void SaveAllSceneSnap(EditorTemplatesData datas, DirectoryInfo data_root)
  18. {
  19. if (!data_root.Exists) { data_root.Create(); }
  20. foreach (var sd in datas.Scenes.Values)
  21. {
  22. //场景快照
  23. var snd = new SceneSnapData(sd);
  24. var bin = IOUtil.ObjectToBin(TemplateManager.MessageCodec, snd);
  25. File.WriteAllBytes(data_root.FullName + Path.DirectorySeparatorChar + sd.ID + ".snd", bin);
  26. }
  27. }
  28. public static void SaveAllSceneSnapExt(EditorTemplatesData datas, DirectoryInfo data_root)
  29. {
  30. if (!data_root.Exists) { data_root.Create(); }
  31. foreach (var sd in datas.Scenes.Values)
  32. {
  33. //单位快照
  34. var snd = new UnitSnapData(sd);
  35. var bin = IOUtil.ObjectToBin(TemplateManager.MessageCodec, snd);
  36. File.WriteAllBytes(data_root.FullName + Path.DirectorySeparatorChar + sd.ID + ".snd", bin);
  37. }
  38. }
  39. internal static void InitSceneSnap(string path, string pathExt)
  40. {
  41. mSnapDataRoot = path;
  42. mSnapDataRootExt = pathExt;
  43. }
  44. private static readonly Dictionary<int, SceneSnapData> dic = new Dictionary<int, SceneSnapData>();
  45. private static readonly Dictionary<int, UnitSnapData> unitDic = new Dictionary<int, UnitSnapData>();
  46. public static SceneSnapData LoadSceneSnapData(int sid)
  47. {
  48. SceneSnapData ret;
  49. if(dic.TryGetValue(sid , out ret))
  50. {
  51. return ret;
  52. }
  53. byte[] bin = Resource.LoadData(mSnapDataRoot + "/" + sid + ".snd");
  54. if (bin != null)
  55. {
  56. try
  57. {
  58. ret = IOUtil.BinToObject<SceneSnapData>(TemplateManager.MessageCodec, bin);
  59. dic.Add(sid , ret);
  60. return ret;
  61. }
  62. catch (Exception err)
  63. {
  64. throw new Exception("Load SceneSnap error ! " + sid, err);
  65. }
  66. }
  67. return null;
  68. }
  69. public static SceneSnapData LoadSceneSnapData(int sid ,byte[] bin)
  70. {
  71. SceneSnapData ret;
  72. if (dic.TryGetValue(sid, out ret))
  73. {
  74. return ret;
  75. }
  76. if (bin != null)
  77. {
  78. ret = IOUtil.BinToObject<SceneSnapData>(TemplateManager.MessageCodec, bin);
  79. dic.Add(sid, ret);
  80. return ret;
  81. }
  82. return null;
  83. }
  84. public static UnitSnapData LoadBinUnitSnapData(int sid, byte[] bin)
  85. {
  86. UnitSnapData ret;
  87. if (unitDic.TryGetValue(sid, out ret))
  88. {
  89. return ret;
  90. }
  91. if (bin != null)
  92. {
  93. ret = IOUtil.BinToObject<UnitSnapData>(TemplateManager.MessageCodec, bin);
  94. unitDic.Add(sid, ret);
  95. return ret;
  96. }
  97. return null;
  98. }
  99. public static UnitSnapData LoadSceneUnitSnapData(int sid)
  100. {
  101. UnitSnapData ret;
  102. if (unitDic.TryGetValue(sid, out ret))
  103. {
  104. return ret;
  105. }
  106. byte[] bin = Resource.LoadData(mSnapDataRootExt + "/" + sid + ".snd");
  107. if (bin != null)
  108. {
  109. try
  110. {
  111. ret = IOUtil.BinToObject<UnitSnapData>(TemplateManager.MessageCodec, bin);
  112. unitDic.Add(sid, ret);
  113. return ret;
  114. }
  115. catch (Exception err)
  116. {
  117. throw new Exception("Load UnitSnapData error ! " + sid, err);
  118. }
  119. }
  120. return null;
  121. }
  122. }
  123. /// <summary>
  124. /// 场景快照数据
  125. /// </summary>
  126. [MessageType(0xFA00000)]
  127. public class SceneSnapData : IExternalizable
  128. {
  129. public int id;
  130. public string name;
  131. public float width, height;
  132. public List<RegionSnapData> regions = new List<RegionSnapData>();
  133. public SceneSnapData() { }
  134. public SceneSnapData(SceneData src)
  135. {
  136. this.id = src.ID;
  137. this.name = src.Name;
  138. this.width = src.ZoneData.TotalWidth;
  139. this.height = src.ZoneData.TotalHeight;
  140. char[] split = new char[] { '_' };
  141. foreach (var rg in src.Regions)
  142. {
  143. try
  144. {
  145. if (rg.Name.StartsWith("snap_"))
  146. {
  147. RegionSnapData rgs = new RegionSnapData();
  148. rgs.full_name = rg.Name;
  149. rgs.x = rg.X;
  150. rgs.y = rg.Y;
  151. rgs.attributes.ParseLines(rg.Attributes);
  152. this.regions.Add(rgs);
  153. }
  154. }
  155. catch (Exception err)
  156. {
  157. throw new Exception(string.Format("parse snap data error : scene={0} region={1}", src, rg.Name), err);
  158. }
  159. }
  160. }
  161. public void ReadExternal(IInputStream input)
  162. {
  163. this.id = input.GetS32();
  164. this.name = input.GetUTF();
  165. this.width = input.GetF32();
  166. this.height = input.GetF32();
  167. this.regions = input.GetList<RegionSnapData>(input.GetExt<RegionSnapData>);
  168. }
  169. public void WriteExternal(IOutputStream output)
  170. {
  171. output.PutS32(this.id);
  172. output.PutUTF(this.name);
  173. output.PutF32(this.width);
  174. output.PutF32(this.height);
  175. output.PutList(this.regions, output.PutExt);
  176. }
  177. }
  178. [MessageType(0xFA00001)]
  179. public class RegionSnapData : IExternalizable
  180. {
  181. public string full_name;
  182. public float x, y;
  183. public readonly Properties attributes = new Properties();
  184. public void ReadExternal(IInputStream input)
  185. {
  186. this.full_name = input.GetUTF();
  187. this.x = input.GetF32();
  188. this.y = input.GetF32();
  189. int count = input.GetS32();
  190. for (int i=0; i< count; i++)
  191. {
  192. string k = input.GetUTF();
  193. string v = input.GetUTF();
  194. attributes[k] = v;
  195. }
  196. }
  197. public void WriteExternal(IOutputStream output)
  198. {
  199. output.PutUTF(this.full_name);
  200. output.PutF32(this.x);
  201. output.PutF32(this.y);
  202. output.PutS32(this.attributes.Count);
  203. foreach (var e in this.attributes)
  204. {
  205. output.PutUTF(e.Key);
  206. output.PutUTF(e.Value);
  207. }
  208. }
  209. }
  210. [MessageType(0xFA00002)]
  211. public class UnitSnapData : IExternalizable
  212. {
  213. //单位信息
  214. public List<UnitSnapDataItem> units = new List<UnitSnapDataItem>();
  215. //路点信息
  216. public List<PointFlagSnapDataItem> pointFlags = new List<PointFlagSnapDataItem>();
  217. public UnitSnapData() { }
  218. public UnitSnapData(SceneData src)
  219. {
  220. //单位信息
  221. foreach (var unit in src.Units)
  222. {
  223. try
  224. {
  225. UnitSnapDataItem rgs = new UnitSnapDataItem();
  226. rgs.unitID = unit.UnitTemplateID;
  227. rgs.unitName = unit.Name;
  228. rgs.x = unit.X;
  229. rgs.y = unit.Y;
  230. this.units.Add(rgs);
  231. }
  232. catch (Exception err)
  233. {
  234. throw new Exception(string.Format("parse snap unit data error : scene={0} region={1}", src, unit.Name), err);
  235. }
  236. }
  237. // 路点信息
  238. foreach (var point in src.Points)
  239. {
  240. try
  241. {
  242. PointFlagSnapDataItem pointFlag = new PointFlagSnapDataItem();
  243. pointFlag.pointName = point.Name;
  244. pointFlag.x = point.X;
  245. pointFlag.y = point.Y;
  246. this.pointFlags.Add(pointFlag);
  247. }
  248. catch (Exception err)
  249. {
  250. throw new Exception(string.Format("parse snap point flag data error : scene={0} region={1}", src, point.Name), err);
  251. }
  252. }
  253. }
  254. public void ReadExternal(IInputStream input)
  255. {
  256. this.units = input.GetList<UnitSnapDataItem>(input.GetExt<UnitSnapDataItem>);
  257. this.pointFlags = input.GetList<PointFlagSnapDataItem>(input.GetExt<PointFlagSnapDataItem>);
  258. }
  259. public void WriteExternal(IOutputStream output)
  260. {
  261. output.PutList(this.units, output.PutExt);
  262. output.PutList(this.pointFlags, output.PutExt);
  263. }
  264. }
  265. [MessageType(0xFA00003)]
  266. public class UnitSnapDataItem : IExternalizable
  267. {
  268. public String unitName;
  269. public int unitID;
  270. public float x, y;
  271. public void ReadExternal(IInputStream input)
  272. {
  273. this.unitName = input.GetUTF();
  274. this.unitID = input.GetS32();
  275. this.x = input.GetF32();
  276. this.y = input.GetF32();
  277. }
  278. public void WriteExternal(IOutputStream output)
  279. {
  280. output.PutUTF(this.unitName);
  281. output.PutS32(this.unitID);
  282. output.PutF32(this.x);
  283. output.PutF32(this.y);
  284. }
  285. }
  286. // 编辑器路点信息
  287. [MessageType(0xFA00004)]
  288. public class PointFlagSnapDataItem : IExternalizable
  289. {
  290. public String pointName;
  291. public float x, y;
  292. public void ReadExternal(IInputStream input)
  293. {
  294. this.pointName = input.GetUTF();
  295. this.x = input.GetF32();
  296. this.y = input.GetF32();
  297. }
  298. public void WriteExternal(IOutputStream output)
  299. {
  300. output.PutUTF(this.pointName);
  301. output.PutF32(this.x);
  302. output.PutF32(this.y);
  303. }
  304. }
  305. }