DownloadResManager.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. public List<DownLoadMaterial> listCompletedMaterial;
  14. /// <summary>
  15. /// 未下载素材列表
  16. /// </summary>
  17. public List<DownLoadMaterial> listNotMaterial;
  18. /// <summary>
  19. /// 下载失败素材列表
  20. /// </summary>
  21. public List<DownLoadMaterial> listFaildMaterial;
  22. public Queue<DownLoadMaterial> qDownload;
  23. // private Queue<DownLoadMaterial> orderDownload;
  24. private int maxDownLoad = 1;
  25. private int nowDownLoad = 0;
  26. private string path;
  27. private bool state = false;
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. path = Application.persistentDataPath + "/Material/";
  32. listNotMaterial = new List<DownLoadMaterial>();
  33. listFaildMaterial = new List<DownLoadMaterial>();
  34. qDownload = new Queue<DownLoadMaterial>();
  35. //先判断是否存在,再创建
  36. if (!File.Exists(path + "CompletedMaterial.txt"))
  37. {
  38. Directory.CreateDirectory(Application.persistentDataPath + "/Material");
  39. }
  40. else
  41. {
  42. string msg = File.ReadAllText(path + "CompletedMaterial.txt");
  43. Debug.Log("DGJ CompletedMaterial ==>" + msg);
  44. // listCompletedMaterial = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(msg);
  45. }
  46. if (listCompletedMaterial == null)
  47. listCompletedMaterial = new List<DownLoadMaterial>();
  48. for (int i = 0; i < listCompletedMaterial.Count; i++)
  49. {
  50. if(!File.Exists(listCompletedMaterial[i].localLoadPath))
  51. {
  52. listCompletedMaterial.RemoveAt(i);
  53. }
  54. }
  55. // StartCoroutine(DownLoadFile());
  56. }
  57. /// <summary>
  58. /// 批量素材下载
  59. /// </summary>
  60. /// <param name="listDLMaterial"> 所有素材列表 </param>
  61. public void DownLoad(List<DownLoadMaterial> listDLMaterial)
  62. {
  63. Debug.Log(listDLMaterial.Count);
  64. for (int i = 0; i < listDLMaterial.Count; i++)
  65. {
  66. Screen(listDLMaterial[i]);
  67. }
  68. // COSDownLoad cos = new COSDownLoad();
  69. // COSDownLoad.Instance.TransferBatchDownloadObjects(listNotMaterial, path);
  70. }
  71. /// <summary>
  72. /// 单个素材下载
  73. /// </summary>
  74. /// <param name="downloadMaterial"></param>
  75. public void DownLoad(DownLoadMaterial downloadMaterial)
  76. {
  77. //Debug.LogError("单个素材下载 " + downloadMaterial.downLoadPath);
  78. //List<DownLoadMaterial> list = new List<DownLoadMaterial>();
  79. //list.Add(downloadMaterial);
  80. // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path);
  81. if(downloadMaterial.type=="-9")
  82. LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
  83. if (Screen(downloadMaterial) == false)
  84. {
  85. //List<DownLoadMaterial> list = new List<DownLoadMaterial>();
  86. //list.Add(downloadMaterial);
  87. // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path);
  88. }
  89. else
  90. {
  91. LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
  92. }
  93. }
  94. public void StartDownLoad()
  95. {
  96. Debug.Log("DGJ ====> StartDownLoad ");
  97. StartCoroutine(DownLoadFile());
  98. // state = true;
  99. }
  100. private IEnumerator DownLoadFile()
  101. {
  102. Debug.Log("DGJ ====> DownLoadFile ");
  103. while (true)
  104. {
  105. yield return new WaitForFixedUpdate();
  106. Debug.Log("DGJ ====> DownLoadFile 2");
  107. if (qDownload.Count > 0 && nowDownLoad < maxDownLoad)
  108. {
  109. nowDownLoad++;
  110. COSDownLoad.Instance.TransferDownloadObject(qDownload.Dequeue(), path);
  111. }
  112. }
  113. }
  114. private IEnumerator DownLoadOK()
  115. {
  116. if (qDownload.Count <= 0 )
  117. {
  118. // SaveLocaData();
  119. //loadUI.ProgressStr = "";
  120. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.updateEnd);
  121. yield break;
  122. }
  123. // loadUI.ProgressStr = ((float)(m_Downloaded.Count + m_FiledDownload.Count) / count).ToString("P");
  124. yield return null;
  125. }
  126.  
  127. /// <summary>
  128. /// 检测
  129. /// </summary>
  130. /// <param name="downloadMaterial"></param>
  131. /// <returns></returns>
  132. private bool Screen(DownLoadMaterial downloadMaterial)
  133. {
  134. bool finish = false;
  135. for (int j = 0; j < listCompletedMaterial.Count; j++)
  136. {
  137. if (downloadMaterial.downLoadPath == listCompletedMaterial[j].downLoadPath)
  138. {
  139. if (downloadMaterial.updataTime == listCompletedMaterial[j].updataTime)
  140. finish = true;
  141. }
  142. }
  143. if (!finish)
  144. {
  145. listNotMaterial.Add(downloadMaterial);
  146. COSDownLoad.showDownLoadCount++;
  147. qDownload.Enqueue(downloadMaterial);
  148. }
  149. return finish;
  150. }
  151. /// <summary>
  152. /// 下载成功
  153. /// </summary>
  154. /// <param name="localFilePath"></param>
  155. public void DownLoadCompleted(string localFilePath)
  156. {
  157. nowDownLoad--;
  158. for (int i = 0; i < listNotMaterial.Count; i++)
  159. {
  160. if (Path.GetFileName( listNotMaterial[i].downLoadPath) == localFilePath)
  161. {
  162. listCompletedMaterial.Add(listNotMaterial[i]);
  163. LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
  164. listNotMaterial.RemoveAt(i);
  165. Save();
  166. break;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 下载失败
  172. /// </summary>
  173. /// <param name="localFilePath"></param>
  174. public void DownLoadFaild(string localFilePath)
  175. {
  176. nowDownLoad--;
  177. for (int i = 0; i < listNotMaterial.Count; i++)
  178. {
  179. Debug.Log("Hjj DownLoadFaild :" + listNotMaterial[i].downLoadPath + "——" + localFilePath);
  180. if (Path.GetFileName(listNotMaterial[i].downLoadPath) == localFilePath)
  181. {
  182. listFaildMaterial.Add(listNotMaterial[i]);
  183. MsgHandler.SendMsg(listNotMaterial[i].downLoadPath, new Msg(listNotMaterial[i].downLoadPath,null));
  184. listNotMaterial.RemoveAt(i);
  185. Save();
  186. break;
  187. }
  188. }
  189. }
  190. private void OnDisable()
  191. {
  192. Save();
  193. }
  194. private void Save()
  195. {
  196. string msg = JsonConvert.SerializeObject(listCompletedMaterial);
  197. // Debug.Log(msg);
  198. // System.IO.Directory.CreateDirectory(path);
  199. // string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
  200. Debug.Log("DGJ =====》 保存 "+ msg);
  201. using (System.IO.StreamWriter writer = System.IO.File.CreateText(path + "CompletedMaterial.txt"))
  202. {
  203. writer.Write(msg);
  204. //writer.
  205. }
  206. }
  207. }
  208. public class MaterailDetail
  209. {
  210. public int id { get; set; }
  211. }
  212. public class DownLoadMaterial
  213. {
  214. public string downLoadPath { get; set; }
  215. public string localLoadPath { get; set; }
  216. public long updataTime { get; set; }
  217. public string type { get; set; }
  218. public DownLoadMaterial()
  219. {
  220. }
  221. public DownLoadMaterial(MaterialObjValue value)
  222. {
  223. this.downLoadPath = value.DownloadPath;
  224. this.type = value.type;
  225. this.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  226. }
  227. }