SnapData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. rgs.mapShow = unit.MapShow;
  231. rgs.unitMName = unit.UnitName;
  232. rgs.unitMIcon = unit.UnitIcon;
  233. rgs.offsetX = unit.OffsetX;
  234. rgs.offsetY = unit.OffsetY;
  235. rgs.lableOffsetX = unit.LableOffsetX;
  236. rgs.lableOffsetY = unit.LableOffsetY;
  237. this.units.Add(rgs);
  238. }
  239. catch (Exception err)
  240. {
  241. throw new Exception(string.Format("parse snap unit data error : scene={0} region={1}", src, unit.Name), err);
  242. }
  243. }
  244. // 路点信息
  245. foreach (var point in src.Points)
  246. {
  247. try
  248. {
  249. PointFlagSnapDataItem pointFlag = new PointFlagSnapDataItem();
  250. pointFlag.pointName = point.Name;
  251. pointFlag.x = point.X;
  252. pointFlag.y = point.Y;
  253. this.pointFlags.Add(pointFlag);
  254. }
  255. catch (Exception err)
  256. {
  257. throw new Exception(string.Format("parse snap point flag data error : scene={0} region={1}", src, point.Name), err);
  258. }
  259. }
  260. }
  261. public void ReadExternal(IInputStream input)
  262. {
  263. this.units = input.GetList<UnitSnapDataItem>(input.GetExt<UnitSnapDataItem>);
  264. this.pointFlags = input.GetList<PointFlagSnapDataItem>(input.GetExt<PointFlagSnapDataItem>);
  265. }
  266. public void WriteExternal(IOutputStream output)
  267. {
  268. output.PutList(this.units, output.PutExt);
  269. output.PutList(this.pointFlags, output.PutExt);
  270. }
  271. }
  272. [MessageType(0xFA00003)]
  273. public class UnitSnapDataItem : IExternalizable
  274. {
  275. public String unitName;
  276. public int unitID;
  277. public float x, y;
  278. public bool mapShow;
  279. public String unitMName;
  280. public String unitMIcon;
  281. public float offsetX;
  282. public float offsetY;
  283. public float lableOffsetX;
  284. public float lableOffsetY;
  285. public void ReadExternal(IInputStream input)
  286. {
  287. this.unitName = input.GetUTF();
  288. this.unitID = input.GetS32();
  289. this.x = input.GetF32();
  290. this.y = input.GetF32();
  291. this.mapShow = input.GetBool();
  292. this.unitMName = input.GetUTF();
  293. this.unitMIcon = input.GetUTF();
  294. this.offsetX = input.GetF32();
  295. this.offsetY = input.GetF32();
  296. this.lableOffsetX = input.GetF32();
  297. this.lableOffsetY = input.GetF32();
  298. }
  299. public void WriteExternal(IOutputStream output)
  300. {
  301. output.PutUTF(this.unitName);
  302. output.PutS32(this.unitID);
  303. output.PutF32(this.x);
  304. output.PutF32(this.y);
  305. output.PutBool(this.mapShow);
  306. output.PutUTF(this.unitMName);
  307. output.PutUTF(this.unitMIcon);
  308. output.PutF32(this.offsetX);
  309. output.PutF32(this.offsetY);
  310. output.PutF32(this.lableOffsetX);
  311. output.PutF32(this.lableOffsetY);
  312. }
  313. }
  314. // 编辑器路点信息
  315. [MessageType(0xFA00004)]
  316. public class PointFlagSnapDataItem : IExternalizable
  317. {
  318. public String pointName;
  319. public float x, y;
  320. public void ReadExternal(IInputStream input)
  321. {
  322. this.pointName = input.GetUTF();
  323. this.x = input.GetF32();
  324. this.y = input.GetF32();
  325. }
  326. public void WriteExternal(IOutputStream output)
  327. {
  328. output.PutUTF(this.pointName);
  329. output.PutF32(this.x);
  330. output.PutF32(this.y);
  331. }
  332. }
  333. }