Advertisement
Guest User

Untitled

a guest
Oct 29th, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.63 KB | None | 0 0
  1. #region 模块信息
  2. /*----------------------------------------------------------------
  3. // Copyright (C) 2013 广州,爱游
  4. //
  5. // 模块名:GameData
  6. // 创建者:Ash Tang
  7. // 修改者列表:
  8. // 创建日期:2013.3.26
  9. // 模块描述:配置数据抽象类。
  10. //----------------------------------------------------------------*/
  11. #endregion
  12.  
  13. using System;
  14. using System.IO;
  15. using System.Collections.Generic;
  16. using System.Reflection;
  17. using UnityEngine;
  18. using Mogo.Util;
  19. using System.Diagnostics;
  20. using HMF;
  21.  
  22. namespace Mogo.GameData
  23. {
  24. public abstract class GameData
  25. {
  26. public int id { get; protected set; }
  27.  
  28. protected static Dictionary<int, T> GetDataMap<T>()
  29. {
  30. Dictionary<int, T> dataMap;
  31. Stopwatch sw = new Stopwatch();
  32. sw.Start();
  33. var type = typeof(T);
  34. var fileNameField = type.GetField("fileName");
  35. if (fileNameField != null)
  36. {
  37. var fileName = fileNameField.GetValue(null) as String;
  38. var result = GameDataControler.Instance.FormatData(fileName, typeof(Dictionary<int, T>), type);
  39. dataMap = result as Dictionary<int, T>;
  40. }
  41. else
  42. {
  43. dataMap = new Dictionary<int, T>();
  44. }
  45. sw.Stop();
  46. LoggerHelper.Info(String.Concat(type, " time: ", sw.ElapsedMilliseconds));
  47. return dataMap;
  48. }
  49. }
  50. public abstract class GameData<T> : GameData where T : GameData<T>
  51. {
  52. private static Dictionary<int, T> m_dataMap;
  53.  
  54. public static Dictionary<int, T> dataMap
  55. {
  56. get
  57. {
  58. if (m_dataMap == null)
  59. m_dataMap = GetDataMap<T>();
  60. return m_dataMap;
  61. }
  62. set { m_dataMap = value; }
  63. }
  64. }
  65.  
  66. public class GameDataControler : DataLoader
  67. {
  68. private List<Type> m_defaultData = new List<Type>() { typeof(GlobalData), typeof(MapData), typeof(LanguageData), typeof(UIMapData),
  69. typeof(SoundData), typeof(InstanceLevelGridPosData), typeof(MapUIMappingData) };
  70.  
  71. private static GameDataControler m_instance;
  72.  
  73. public static GameDataControler Instance
  74. {
  75. get { return m_instance; }
  76. }
  77.  
  78. static GameDataControler()
  79. {
  80. m_instance = new GameDataControler();
  81. }
  82.  
  83. public static void Init(Action<int, int> progress = null, Action finished = null)
  84. {
  85. if (SystemSwitch.UseHmf)
  86. {
  87. Stopwatch sw = new Stopwatch();
  88. sw.Start();
  89. m_instance.LoadData(m_instance.m_defaultData, m_instance.FormatHmfData, null);
  90. sw.Stop();
  91. LoggerHelper.Info("InitSynHmfData time: " + sw.ElapsedMilliseconds);
  92. if (m_isPreloadData)
  93. {
  94. Action action = () => { m_instance.InitAsynData(m_instance.FormatHmfData, progress, finished); };
  95. if (SystemSwitch.ReleaseMode)
  96. action.BeginInvoke(null, null);
  97. else
  98. action();
  99. }
  100. else
  101. {
  102. finished();
  103. }
  104. }
  105. else
  106. {
  107. m_instance.LoadData(m_instance.m_defaultData, m_instance.FormatXMLData, null);
  108. if (m_isPreloadData)
  109. {
  110. Action action = () => { m_instance.InitAsynData(m_instance.FormatXMLData, progress, finished); };
  111. if (SystemSwitch.ReleaseMode)
  112. action.BeginInvoke(null, null);
  113. else
  114. action();
  115. }
  116. else
  117. {
  118. finished();
  119. }
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// 进行读取数据准备工作和调用处理方法
  125. /// </summary>
  126. /// <param name="formatData">格式化数据方法</param>
  127. /// <param name="progress">处理进度回调</param>
  128. /// <param name="finished">处理完成回调</param>
  129. private void InitAsynData(Func<string, Type, Type, object> formatData, Action<int, int> progress, Action finished)
  130. {
  131. try
  132. {
  133. Stopwatch sw = new Stopwatch();
  134. sw.Start();
  135. List<Type> gameDataType = new List<Type>();
  136. Assembly ass = typeof(GameDataControler).Assembly;
  137. var types = ass.GetTypes();
  138. foreach (var item in types)
  139. {
  140. if (item.Namespace == "Mogo.GameData")
  141. {
  142. var type = item.BaseType;
  143. while (type != null)
  144. {
  145. //#if UNITY_IPHONE
  146. if (type == typeof(GameData) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(GameData<>)))//type == typeof(GameData) ||
  147. //#else
  148. // if ((type.IsGenericType && type.GetGenericTypeDefinition() == typeof(GameData<>)))
  149. //#endif
  150. {
  151. if (!m_defaultData.Contains(item))
  152. gameDataType.Add(item);
  153. break;
  154. }
  155. else
  156. {
  157. type = type.BaseType;
  158. }
  159. }
  160. }
  161. }
  162. LoadData(gameDataType, formatData, progress);
  163. sw.Stop();
  164. LoggerHelper.Debug("Asyn GameData init time: " + sw.ElapsedMilliseconds);
  165. GC.Collect();
  166. if (finished != null)
  167. finished();
  168. }
  169. catch (Exception ex)
  170. {
  171. LoggerHelper.Error("InitData Error: " + ex.Message);
  172. }
  173. }
  174.  
  175. /// <summary>
  176. /// 加载数据逻辑
  177. /// </summary>
  178. /// <param name="gameDataType">加载数据列表</param>
  179. /// <param name="formatData">处理数据方法</param>
  180. /// <param name="progress">数据处理进度</param>
  181. private void LoadData(List<Type> gameDataType, Func<string, Type, Type, object> formatData, Action<int, int> progress)
  182. {
  183. var count = gameDataType.Count;
  184. var i = 1;
  185. foreach (var item in gameDataType)
  186. {
  187. var p = item.GetProperty("dataMap", ~BindingFlags.DeclaredOnly);
  188. var fileNameField = item.GetField("fileName");
  189. if (p != null && fileNameField != null)
  190. {
  191. var fileName = fileNameField.GetValue(null) as String;
  192. var result = formatData(String.Concat(m_resourcePath, fileName, m_fileExtention), p.PropertyType, item);
  193. p.GetSetMethod().Invoke(null, new object[] { result });
  194. }
  195. if (progress != null)
  196. progress(i, count);
  197. i++;
  198. }
  199. }
  200.  
  201. public object FormatData(string fileName, Type dicType, Type type)
  202. {
  203. if (SystemSwitch.UseHmf)
  204. return FormatHmfData(String.Concat(m_resourcePath, fileName, m_fileExtention), dicType, type);
  205. else
  206. return FormatXMLData(String.Concat(m_resourcePath, fileName, m_fileExtention), dicType, type);
  207. }
  208.  
  209. #region xml
  210.  
  211. private object FormatXMLData(string fileName, Type dicType, Type type)
  212. {
  213. object result = null;
  214. try
  215. {
  216. //LoggerHelper.Debug(fileName);
  217. //var dicType = dicProp.PropertyType;
  218. result = dicType.GetConstructor(Type.EmptyTypes).Invoke(null);
  219. Dictionary<Int32, Dictionary<String, String>> map;//int32 为 id, string 为 属性名, string 为 属性值
  220. if (XMLParser.LoadIntMap(fileName, m_isUseOutterConfig, out map))
  221. {
  222. var props = type.GetProperties();//获取实体属性
  223. foreach (var item in map)
  224. {
  225. var t = type.GetConstructor(Type.EmptyTypes).Invoke(null);//构造实体实例
  226. foreach (var prop in props)
  227. {
  228. if (prop.Name == "id")
  229. {
  230. prop.SetValue(t, item.Key, null);
  231. }
  232. else
  233. {
  234. if (item.Value.ContainsKey(prop.Name))
  235. {
  236. var value = Utils.GetValue(item.Value[prop.Name], prop.PropertyType);
  237. prop.SetValue(t, value, null);
  238. }
  239. }
  240. }
  241. dicType.GetMethod("Add").Invoke(result, new object[] { item.Key, t });
  242. }
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. LoggerHelper.Error("FormatData Error: " + fileName + " " + ex.Message);
  248. }
  249.  
  250. return result;
  251. }
  252.  
  253. #endregion
  254.  
  255. #region hmf
  256.  
  257. private object FormatHmfData(string fileName, Type dicType, Type type)
  258. {
  259. object result = null;
  260. try
  261. {
  262. //var dicType = dicProp.PropertyType;
  263. result = result = dicType.GetConstructor(Type.EmptyTypes).Invoke(null);
  264. //LoggerHelper.Warning("fileName: " + fileName);
  265. byte[] bs = XMLParser.LoadBytes(fileName);
  266. System.IO.MemoryStream stream = new MemoryStream(bs);
  267. stream.Seek(0, SeekOrigin.Begin);
  268.  
  269. Hmf h = new Hmf();
  270. Dictionary<object, object> map = (Dictionary<object, object>)h.ReadObject(stream);
  271.  
  272. var props = type.GetProperties();//获取实体属性
  273. foreach (var item in map)
  274. {
  275. var t = type.GetConstructor(Type.EmptyTypes).Invoke(null);//构造实体实例
  276. foreach (var prop in props)
  277. {
  278. if (prop.Name == "id")
  279. {
  280. var v = Utils.GetValue((string)item.Key, prop.PropertyType);
  281. prop.SetValue(t, v, null);
  282. }
  283. else
  284. {
  285. Dictionary<object, object> m = (Dictionary<object, object>)item.Value;
  286. if (m.ContainsKey((object)prop.Name))
  287. {
  288. var value = Utils.GetValue((string)m[(object)prop.Name], prop.PropertyType);
  289. prop.SetValue(t, value, null);
  290. }
  291. }
  292. }
  293. var v1 = Utils.GetValue((string)item.Key, typeof(Int32));
  294. dicType.GetMethod("Add").Invoke(result, new object[] { v1, t });
  295. }
  296. }
  297. catch (Exception ex)
  298. {
  299. LoggerHelper.Error("FormatData Error: " + fileName + " " + ex.Message);
  300. }
  301. return result;
  302. }
  303.  
  304. #endregion
  305. }
  306. }
  307.  
  308. public abstract class DataLoader
  309. {
  310. protected static readonly bool m_isPreloadData = true;
  311. protected readonly String m_resourcePath;
  312. protected readonly String m_fileExtention;
  313. protected readonly bool m_isUseOutterConfig;
  314. protected Action<int, int> m_progress;
  315. protected Action m_finished;
  316.  
  317. protected DataLoader()
  318. {
  319. m_isUseOutterConfig = SystemConfig.IsUseOutterConfig;
  320. if (m_isUseOutterConfig)
  321. {
  322. m_resourcePath = String.Concat(SystemConfig.OutterPath, SystemConfig.CONFIG_SUB_FOLDER);
  323. m_fileExtention = SystemConfig.XML;
  324. }
  325. else
  326. {
  327. m_resourcePath = SystemConfig.CONFIG_SUB_FOLDER;//兼容文件模块
  328. m_fileExtention = SystemConfig.CONFIG_FILE_EXTENSION;
  329. }
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement