DownloadResManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using UnityEngine;
  8. public class DownloadResManager : MonoSingleton<DownloadResManager>
  9. {
  10. /// <summary>
  11. /// 本地已下载素材列表
  12. /// </summary>
  13. private List<DownLoadMaterial> listCompletedMaterial;
  14. /// <summary>
  15. /// 未下载素材列表
  16. /// </summary>
  17. private List<DownLoadMaterial> listNotMaterial;
  18. /// <summary>
  19. /// 下载失败素材列表
  20. /// </summary>
  21. private List<DownLoadMaterial> listFaildMaterial;
  22. private Queue<DownLoadMaterial> qDownload;
  23. private int maxDownLoad = 1;
  24. private int nowDownLoad = 0;
  25. private string path;
  26. // Start is called before the first frame update
  27. void Start()
  28. {
  29. path = Application.persistentDataPath + "/Material/";
  30. listNotMaterial = new List<DownLoadMaterial>();
  31. listFaildMaterial = new List<DownLoadMaterial>();
  32. qDownload = new Queue<DownLoadMaterial>();
  33. //先判断是否存在,再创建
  34. if (!File.Exists(path))
  35. {
  36. Directory.CreateDirectory(path);
  37. }
  38. else
  39. {
  40. string msg = File.ReadAllText(path + "/CompletedMaterial.txt");
  41. listCompletedMaterial = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(msg);
  42. }
  43. if (listCompletedMaterial == null)
  44. listCompletedMaterial = new List<DownLoadMaterial>();
  45. for (int i = 0; i < listCompletedMaterial.Count; i++)
  46. {
  47. if(!File.Exists(listCompletedMaterial[i].localLoadPath))
  48. {
  49. listCompletedMaterial.RemoveAt(i);
  50. }
  51. }
  52. StartCoroutine(DownLoad());
  53. }
  54. /// <summary>
  55. /// 批量素材下载
  56. /// </summary>
  57. /// <param name="listDLMaterial"> 所有素材列表 </param>
  58. public void DownLoad(List<DownLoadMaterial> listDLMaterial)
  59. {
  60. Debug.Log(listDLMaterial.Count);
  61. for (int i = 0; i < listDLMaterial.Count; i++)
  62. {
  63. Screen(listDLMaterial[i]);
  64. }
  65. // COSDownLoad cos = new COSDownLoad();
  66. // COSDownLoad.Instance.TransferBatchDownloadObjects(listNotMaterial, path);
  67. }
  68. /// <summary>
  69. /// 单个素材下载
  70. /// </summary>
  71. /// <param name="downloadMaterial"></param>
  72. public void DownLoad(DownLoadMaterial downloadMaterial)
  73. {
  74. //Debug.LogError("单个素材下载 " + downloadMaterial.downLoadPath);
  75. //List<DownLoadMaterial> list = new List<DownLoadMaterial>();
  76. //list.Add(downloadMaterial);
  77. // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path);
  78. if (Screen(downloadMaterial) == false)
  79. {
  80. //List<DownLoadMaterial> list = new List<DownLoadMaterial>();
  81. //list.Add(downloadMaterial);
  82. // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path);
  83. }
  84. else
  85. {
  86. LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
  87. }
  88. }
  89. private IEnumerator DownLoad()
  90. {
  91. while(true)
  92. {
  93. yield return new WaitForFixedUpdate();
  94. if (qDownload.Count>0 && nowDownLoad<maxDownLoad)
  95. {
  96. nowDownLoad++;
  97. COSDownLoad.Instance.TransferDownloadObject(qDownload.Dequeue(), path);
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 检测
  103. /// </summary>
  104. /// <param name="downloadMaterial"></param>
  105. /// <returns></returns>
  106. private bool Screen(DownLoadMaterial downloadMaterial)
  107. {
  108. bool finish = false;
  109. for (int j = 0; j < listCompletedMaterial.Count; j++)
  110. {
  111. if (downloadMaterial.downLoadPath == listCompletedMaterial[j].downLoadPath)
  112. {
  113. if (downloadMaterial.updataTime == listCompletedMaterial[j].updataTime)
  114. finish = true;
  115. }
  116. }
  117. if (!finish)
  118. {
  119. listNotMaterial.Add(downloadMaterial);
  120. qDownload.Enqueue(downloadMaterial);
  121. }
  122. return finish;
  123. }
  124. /// <summary>
  125. /// 下载成功
  126. /// </summary>
  127. /// <param name="localFilePath"></param>
  128. public void DownLoadCompleted(string localFilePath)
  129. {
  130. nowDownLoad--;
  131. for (int i = 0; i < listNotMaterial.Count; i++)
  132. {
  133. if(Path.GetFileName( listNotMaterial[i].localLoadPath) == localFilePath)
  134. {
  135. listCompletedMaterial.Add(listNotMaterial[i]);
  136. LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
  137. listNotMaterial.RemoveAt(i);
  138. break;
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 下载失败
  144. /// </summary>
  145. /// <param name="localFilePath"></param>
  146. public void DownLoadFaild(string localFilePath)
  147. {
  148. nowDownLoad--;
  149. for (int i = 0; i < listNotMaterial.Count; i++)
  150. {
  151. if (Path.GetFileName(listNotMaterial[i].localLoadPath) == localFilePath)
  152. {
  153. listFaildMaterial.Add(listNotMaterial[i]);
  154. MsgHandler.SendMsg(listNotMaterial[i].localLoadPath, null);
  155. listNotMaterial.RemoveAt(i);
  156. break;
  157. }
  158. }
  159. }
  160. private void OnDisable()
  161. {
  162. string msg = JsonConvert.SerializeObject(listCompletedMaterial);
  163. Debug.Log(msg);
  164. System.IO.Directory.CreateDirectory(path);
  165. // string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
  166. Debug.Log(path);
  167. using (System.IO.StreamWriter writer = System.IO.File.CreateText(path + "/CompletedMaterial.txt"))
  168. {
  169. writer.Write(msg);
  170. //writer.
  171. }
  172. }
  173. }
  174. public class MaterailDetail
  175. {
  176. public int id { get; set; }
  177. }
  178. public class DownLoadMaterial
  179. {
  180. public string downLoadPath { get; set; }
  181. public string localLoadPath { get; set; }
  182. public long updataTime { get; set; }
  183. public string type { get; set; }
  184. }