AssetGame_ResourceUpdate.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using UnityEngine.UI;
  8. public class AssetGame_ResourceUpdate : AssetGame_Base<AssetGame_ResourceUpdate>
  9. {
  10. /// <summary>
  11. /// 需要下载的总资源数
  12. /// </summary>
  13. private int MaxBundleNum = 0;
  14. /// <summary>
  15. /// 需要下载的资源总大小
  16. /// </summary>
  17. private int MaxBundleSize = 0;
  18. /// <summary>
  19. /// 废弃的变量
  20. /// </summary>
  21. private float mProgress = 0f;
  22. /// <summary>
  23. /// 在下载更新界面显示的进度条
  24. /// </summary>
  25. public Slider mSlider = null;
  26. /// <summary>
  27. /// 在下载更新界面显示的进度数字
  28. /// </summary>
  29. public Text mSliderText = null;
  30. /// <summary>
  31. /// 每下载一个文件,占总额度的百分比
  32. /// </summary>
  33. private float mPerPropress = 0f;
  34. /// <summary>
  35. ///需要下载的资源
  36. /// </summary>
  37. private List<string> NeedDownFiles=new List<string>();
  38. /// <summary>
  39. /// 增加新要下载的资源名
  40. /// </summary>
  41. /// <param name="nameString"></param>
  42. public void AddLoadRes(string nameString)
  43. {
  44. if (!NeedDownFiles.Contains(nameString))
  45. {
  46. NeedDownFiles.Add(nameString);
  47. }
  48. }
  49. /// <summary>
  50. /// 废弃的功能
  51. /// </summary>
  52. public float GetProgress
  53. {
  54. get {
  55. return mProgress ;
  56. }
  57. }
  58. /// <summary>
  59. /// 获得剩余需要下载的资源数量
  60. /// </summary>
  61. public int GetNeedDownFilesCount
  62. {
  63. get { return NeedDownFiles.Count; }
  64. }
  65. /// <summary>
  66. /// 获得需要下载的总资源数量
  67. /// </summary>
  68. public int GetMaxBundleNum
  69. {
  70. get { return MaxBundleNum; }
  71. }
  72. /// <summary>
  73. /// 设置需要下载的资源总数
  74. /// </summary>
  75. public int SetMaxBundleNum
  76. {
  77. set { MaxBundleNum = value; }
  78. }
  79. /// <summary>
  80. /// 获取资源总大小
  81. /// </summary>
  82. public int GetMaxBundleSize
  83. {
  84. get { return MaxBundleSize; }
  85. }
  86. /// <summary>
  87. /// 设置资源总大小
  88. /// </summary>
  89. public int SetMaxBundleSize
  90. {
  91. set { MaxBundleSize = value; }
  92. }
  93. /// <summary>
  94. /// 将赋值进度条
  95. /// </summary>
  96. /// <param name="sld"></param>
  97. public void SetSlider(Slider sld,Text txt)
  98. {
  99. mSlider = sld;
  100. mSlider.value = 0;
  101. mSliderText = txt;
  102. }
  103. /// <summary>
  104. /// 依次加载需要更新的资源
  105. /// </summary>
  106. public void DownLoadRes(string serverVersion)
  107. {
  108. if (NeedDownFiles.Count == 0)
  109. {
  110. AssetGame_VersionManager.Ins.SetUpdateLocalVersionFile();
  111. return;
  112. }
  113. string ServerMD5;
  114. string ServerResSize;
  115. string file = NeedDownFiles[0];
  116. LogHelper.Log("还有" + NeedDownFiles .Count +"个未更新资源,总数" + MaxBundleNum + "个。");
  117. float sizemega = 0;
  118. sizemega =(float ) MaxBundleSize / 1000000;
  119. LogHelper.Log(string .Format ("剩余{0:N1}", sizemega) + string .Format ("兆,{0:N0}" , MaxBundleSize) + "字节需要下载");
  120. LogHelper.Log(AssetConfig_GlobalSetting.strServerUrl + file);
  121. string cdnpath = AssetConfig_GlobalSetting.strServerUrl + serverVersion + "/";
  122. AssetSystem_CoroutineProvider.Instance.StartCoroutine(DownLoad(cdnpath + file, delegate(WWW w)
  123. {
  124. if (w.error != null)
  125. {
  126. LogHelper.Log("没有更新到资源 file=" + file);
  127. LogHelper.Log(w.error);
  128. }
  129. else
  130. {
  131. AssetGame_VersionManager.Ins.ServerResVersion.TryGetValue(file ,out ServerMD5);
  132. AssetGame_VersionManager.Ins.ServerResSizeVersion.TryGetValue(file, out ServerResSize);
  133. LogHelper.Log("将下载的资源替换本地的资源 ");
  134. //将下载的资源替换本地的资源
  135. // AssetSystem_Log.Ins.Log("wenjian " + file);
  136. // LogHelper.Log("wenjian "+ file);
  137. MaxBundleSize -= int.Parse (ServerResSize);
  138. //LogHelper.Log("当前数据" + ServerResSize + "/"+ MaxBundleSize );
  139. AssetSystem_AndroidFileHelper.Instance.ReWriteFileByByte(file, w.bytes);
  140. AddOrModifyXml(file,ServerMD5,ServerResSize);
  141. }
  142. NeedDownFiles.RemoveAt(0);
  143. DownLoadRes(serverVersion);
  144. }));
  145. }
  146. private void AddOrModifyXml(string strFilename,string strMD5,string strSize)
  147. {
  148. bool find = false;
  149. string savePath = AssetConfig_GlobalSetting.strLocalSavePath;
  150. XmlDocument XmlDoc = new XmlDocument();
  151. XmlDoc.Load(savePath + "/VersionMD5.txt");
  152. XmlNodeList nodeList = XmlDoc.SelectSingleNode("Files").ChildNodes;//获取Files节点的所有子节点
  153. foreach (XmlNode xn in nodeList)//遍历所有子节点
  154. {
  155. XmlElement xmlElem = (XmlElement)xn;//将子节点类型转换为XmlElement类型
  156. if (xmlElem.GetAttribute("FileName") == strFilename)//如果genre属性值为“李赞红”
  157. {
  158. xmlElem.SetAttribute("FileName", strFilename);
  159. xmlElem.SetAttribute("MD5", strMD5);
  160. xmlElem.SetAttribute("Size", strSize);
  161. find = true;
  162. }
  163. }
  164. if (!find)
  165. {
  166. XmlNode XmlRoot = XmlDoc.SelectSingleNode("Files");
  167. XmlElement xmlElem = XmlDoc.CreateElement("File");
  168. xmlElem.SetAttribute("FileName", strFilename);
  169. xmlElem.SetAttribute("MD5", strMD5);
  170. xmlElem.SetAttribute("Size", strSize);
  171. XmlRoot.AppendChild(xmlElem);
  172. }
  173. XmlDoc.Save(savePath + "/VersionMD5.txt");
  174. XmlDoc = null;
  175. }
  176. /// <summary>
  177. /// 读取string
  178. /// 逐行读入,以“,”分割
  179. /// </summary>
  180. /// <param name="content"></param>
  181. /// <param name="dict"></param>
  182. public void ToParseVersionFile(string content, Dictionary<string, string> dict, string log)
  183. {
  184. if (content == null || content.Length == 0)
  185. {
  186. LogHelper.Log(log + " is null");
  187. return;
  188. }
  189. string[] items = content.Split(new char[] { '\n' });
  190. foreach (string item in items)
  191. {
  192. string[] info = item.Split(new char[] { ',' });
  193. if (info != null && info.Length == 2)
  194. {
  195. dict.Add(info[0], info[1]);
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 读取XmlDocument
  201. /// 固定格式node,以属性添加数据【FileName】+【MD5】
  202. /// </summary>
  203. /// <param name="content"></param>
  204. /// <param name="dict"></param>
  205. /// <param name="log"></param>
  206. /// <returns></returns>
  207. public void ToParseVersionFileXml(string content, Dictionary<string, string> dict, string log)
  208. {
  209. if (content == null || content.Length == 0)
  210. {
  211. LogHelper.Log(log + " is null");
  212. return;
  213. }
  214. XmlDocument xmlDoc = new XmlDocument();
  215. xmlDoc.LoadXml(content);
  216. LogHelper.Log("ParseVersionFileXml:1");
  217. //获取根节点
  218. XmlElement rootNodeList = xmlDoc.DocumentElement;//Response
  219. //获取FlightDatas节点
  220. XmlNodeList xmlChildFlightNodeList = rootNodeList.GetElementsByTagName("Files");
  221. LogHelper.Log("ParseVersionFileXml:2");
  222. foreach (XmlNode node in rootNodeList.ChildNodes)
  223. {
  224. //node.Attributes[]
  225. string fileName = node.Attributes["FileName"].Value.ToString();
  226. string fileMd5 = node.Attributes["MD5"].Value.ToString();
  227. dict.Add(fileName, fileMd5);
  228. }
  229. LogHelper.Log("ParseVersionFileXml:3");
  230. }
  231. /// <summary>
  232. /// 读取XmlDocument
  233. /// 固定格式node,以属性添加数据【FileName】+【MD5】
  234. /// </summary>
  235. /// <param name="content"></param>
  236. /// <param name="dict"></param>
  237. /// <param name="log"></param>
  238. /// <returns></returns>
  239. public void ToParseVersionFileSizeXml(string content, Dictionary<string, string> dict, string log)
  240. {
  241. if (content == null || content.Length == 0)
  242. {
  243. LogHelper.Log(log + " is null");
  244. return;
  245. }
  246. XmlDocument xmlDoc = new XmlDocument();
  247. xmlDoc.LoadXml(content);
  248. //获取根节点
  249. XmlElement rootNodeList = xmlDoc.DocumentElement;//Response
  250. //获取FlightDatas节点
  251. XmlNodeList xmlChildFlightNodeList = rootNodeList.GetElementsByTagName("Files");
  252. foreach (XmlNode node in rootNodeList.ChildNodes)
  253. {
  254. //node.Attributes[]
  255. string fileName = node.Attributes["FileName"].Value.ToString();
  256. string fileSize = node.Attributes["Size"].Value.ToString();
  257. // LogHelper.Log(fileName + " ***** " + fileSize);
  258. dict.Add(fileName, fileSize);
  259. // LogHelper.LogError(fileName + "/" + fileSize );
  260. }
  261. }
  262. /// <summary>
  263. /// 加载这里,加入了和进度条有关的,这个以前测试过,是可以用的.ljp20170323
  264. /// </summary>
  265. /// <param name="url"></param>
  266. /// <param name="finishFun"></param>
  267. /// <returns></returns>
  268. public IEnumerator DownLoad(string url, HandleFinishDownload finishFun)
  269. {
  270. WWW www = new WWW(url);
  271. if (MaxBundleNum > 0)
  272. {
  273. mPerPropress = (float)1 / MaxBundleNum;
  274. }
  275. int displayProgress = 0;
  276. int toProgress = 0;
  277. while (!www.isDone)
  278. {
  279. // LogHelper.Log("01 "+displayProgress+" " + toProgress + " "+mPerPropress );
  280. toProgress = (int)(www.progress * mPerPropress * 100);
  281. while (displayProgress < toProgress)
  282. {
  283. ++displayProgress;
  284. SetProgress(displayProgress);
  285. yield return null;
  286. }
  287. yield return null;
  288. }
  289. toProgress = (int)(mPerPropress * 100);
  290. while (displayProgress < toProgress)
  291. {
  292. // LogHelper.Log("02");
  293. ++displayProgress;
  294. SetProgress(displayProgress);
  295. yield return null;
  296. }
  297. yield return www;
  298. if (finishFun != null)
  299. {
  300. finishFun(www);
  301. }
  302. www.Dispose();
  303. }
  304. public delegate void HandleFinishDownload(WWW www);
  305. /// <summary>
  306. /// 设置进度条
  307. /// </summary>
  308. /// <param name="progress"></param>
  309. public void SetProgress(int progress)
  310. {
  311. if (mSlider != null)
  312. {
  313. int xx = (int)(((1.0f) * MaxBundleNum - NeedDownFiles.Count) / MaxBundleNum * 100);
  314. float progressValue = (xx + progress) * 0.01f;
  315. if(progressValue > 1)
  316. {
  317. progressValue = 1.0f;
  318. }
  319. if(progressValue > mSlider.value)
  320. {
  321. mSlider.value = progressValue;
  322. mSliderText.text = (xx + progress) + "%";
  323. }
  324. }
  325. }
  326. /*
  327. *
  328. * 现在上面加的都是和进度条有关的,这个以前测试过,是可以用的.ljp20170323
  329. *
  330. *
  331. *
  332. *
  333. *
  334. *
  335. *
  336. *
  337. */
  338. }