CheckFilePool.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using hirdParty.DownloadSystem;
  7. using UnityEngine;
  8. namespace ThirdParty.DownloadSystem
  9. {
  10. public class CheckFilePool : IDisposable, ICheckUpdate
  11. {
  12. public List<MD5FileInfo> CheckFile = new List<MD5FileInfo>();
  13. public System.Action OnFinish;
  14. public List<MD5FileInfo> shiBaiFile = new List<MD5FileInfo>();
  15. private List<CheckFileThran> _checkFileThrans = new List<CheckFileThran>();
  16. public bool _isStart;
  17. private int _maxCount;
  18. public bool isFinish;
  19. public string streamingAssetsPath;
  20. public bool isStreamingAssetsPath;
  21. private bool isRendCheckStreamingAssetsPath;
  22. private bool isLoadAllFinish;
  23. private string rootPath;
  24. public int FileCount()
  25. {
  26. lock (shiBaiFile)
  27. {
  28. int count = CheckFile.Count;
  29. return _maxCount - count;
  30. }
  31. }
  32. public void Start(string dPath, List<MD5FileInfo> CheckFile)
  33. {
  34. this.CheckFile = CheckFile;
  35. _maxCount = CheckFile.Count;
  36. isRendCheckStreamingAssetsPath = true;
  37. isLoadAllFinish = false;
  38. this.rootPath = dPath;
  39. for (int i = 0; i < 4; i++)
  40. {
  41. CheckFileThran checkFileThran = new CheckFileThran();
  42. checkFileThran.OnClick = Finish;
  43. checkFileThran.Start(dPath, this);
  44. _checkFileThrans.Add(checkFileThran);
  45. }
  46. FileDownloadSystem.Instance.AddCheckUpdate(this);
  47. }
  48. public void Update()
  49. {
  50. }
  51. private void Finish()
  52. {
  53. lock (_checkFileThrans)
  54. {
  55. for (int i = 0; i < _checkFileThrans.Count; i++)
  56. {
  57. if (!_checkFileThrans[i].isEnd)
  58. {
  59. return;
  60. }
  61. }
  62. try
  63. {
  64. shiBaiFile.Clear();
  65. for (int i = 0; i < _checkFileThrans.Count; i++)
  66. {
  67. shiBaiFile.AddRange(_checkFileThrans[i].shiBaiFile);
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. Debug.LogError(e);
  73. }
  74. }
  75. shiBaiFile.Clear();
  76. List<MD5FileInfo> fileInfo = new List<MD5FileInfo>();
  77. for (int i = 0; i < _checkFileThrans.Count; i++)
  78. {
  79. fileInfo.AddRange(_checkFileThrans[i].shiBaiFile);
  80. }
  81. isLoadAllFinish = true;
  82. shiBaiFile.AddRange(fileInfo);
  83. }
  84. private void CheckFileFinish()
  85. {
  86. OnFinish?.Invoke();
  87. isFinish = true;
  88. OnFinish = null;
  89. }
  90. private void CheckLoadFlie(List<MD5FileInfo> fileInfo)
  91. {
  92. int count = fileInfo.Count;
  93. if (count <= 0)
  94. {
  95. CheckFileFinish();
  96. return;
  97. }
  98. for (int i = 0; i < fileInfo.Count; i++)
  99. {
  100. MD5FileInfo md5FileInfo = fileInfo[i];
  101. DownloadFileData downloadFileData = new DownloadFileData();
  102. downloadFileData.remoteUrl =
  103. streamingAssetsPath + md5FileInfo.fileName;
  104. Debug.Log("下载文件" + downloadFileData.remoteUrl);
  105. DownloadHander fileDow = FileDownloadSystem.Instance.DownloadFile(downloadFileData);
  106. fileDow.OnFinishCallBack = delegate(DownloadHander hander)
  107. {
  108. byte[] data = hander.Data;
  109. count--;
  110. string md5 = FileMD5(data);
  111. if (!md5FileInfo.md5.Equals(md5))
  112. {
  113. Debug.Log("MD5校验失败" + downloadFileData.remoteUrl);
  114. shiBaiFile.Add(md5FileInfo);
  115. }
  116. else
  117. {
  118. string rp = rootPath + md5FileInfo.fileName;
  119. File.WriteAllBytes(rp,data);
  120. Debug.Log("拷贝文件到" +rp);
  121. }
  122. if (count <= 0)
  123. {
  124. CheckFileFinish();
  125. }
  126. };
  127. }
  128. }
  129. public static string FileMD5(byte[] data)
  130. {
  131. if (data == null)
  132. {
  133. return "";
  134. }
  135. byte[] retVal = null;
  136. MD5 md5 = new MD5CryptoServiceProvider();
  137. retVal = md5.ComputeHash(data);
  138. return ByteArrayToString(retVal);
  139. }
  140. public static string ByteArrayToString(byte[] ba)
  141. {
  142. StringBuilder hex = new StringBuilder(ba.Length * 2);
  143. foreach (byte b in ba)
  144. {
  145. hex.AppendFormat("{0:x2}", b);
  146. }
  147. return hex.ToString();
  148. }
  149. public void Dispose()
  150. {
  151. FileDownloadSystem.Instance.RemvoeCheckUpdate(this);
  152. }
  153. public void CheckUpdate()
  154. {
  155. if (!isLoadAllFinish)
  156. {
  157. return;
  158. }
  159. if (isRendCheckStreamingAssetsPath && isStreamingAssetsPath && shiBaiFile.Count > 0)
  160. {
  161. List<MD5FileInfo> fileInfo = new List<MD5FileInfo>();
  162. fileInfo.AddRange(shiBaiFile);
  163. shiBaiFile.Clear();
  164. CheckLoadFlie(fileInfo);
  165. isRendCheckStreamingAssetsPath = false;
  166. FileDownloadSystem.Instance.RemvoeCheckUpdate(this);
  167. }
  168. else
  169. {
  170. CheckFileFinish();
  171. FileDownloadSystem.Instance.RemvoeCheckUpdate(this);
  172. }
  173. }
  174. }
  175. }