DownloadResManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 string path;
  23. // Start is called before the first frame update
  24. void Start()
  25. {
  26. path = Application.persistentDataPath + "/Material";
  27. listNotMaterial = new List<DownLoadMaterial>();
  28. listFaildMaterial = new List<DownLoadMaterial>();
  29. //先判断是否存在,再创建
  30. if (!File.Exists(path))
  31. {
  32. Directory.CreateDirectory(path);
  33. }
  34. else
  35. {
  36. string msg = File.ReadAllText(path+ "/CompletedMaterial.txt");
  37. listCompletedMaterial = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(msg);
  38. }
  39. if (listCompletedMaterial == null)
  40. listCompletedMaterial = new List<DownLoadMaterial>();
  41. for (int i = 0; i < listCompletedMaterial.Count; i++)
  42. {
  43. if(!File.Exists(listCompletedMaterial[i].localLoadPath))
  44. {
  45. listCompletedMaterial.RemoveAt(i);
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// 批量素材下载
  51. /// </summary>
  52. /// <param name="listDLMaterial"> 所有素材列表 </param>
  53. public void DownLoad(List<DownLoadMaterial> listDLMaterial)
  54. {
  55. Debug.Log(listDLMaterial.Count);
  56. for (int i = 0; i < listDLMaterial.Count; i++)
  57. {
  58. Screen(listDLMaterial[i]);
  59. }
  60. // COSDownLoad cos = new COSDownLoad();
  61. COSDownLoad.Instance.TransferBatchDownloadObjects(listNotMaterial, path);
  62. }
  63. /// <summary>
  64. /// 单个素材下载
  65. /// </summary>
  66. /// <param name="downloadMaterial"></param>
  67. public void DownLoad(DownLoadMaterial downloadMaterial)
  68. {
  69. if( Screen( downloadMaterial) == false)
  70. {
  71. COSDownLoad.Instance.TransferDownloadObject(downloadMaterial, path);
  72. }
  73. else
  74. {
  75. LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
  76. }
  77. }
  78. /// <summary>
  79. /// 检测
  80. /// </summary>
  81. /// <param name="downloadMaterial"></param>
  82. /// <returns></returns>
  83. private bool Screen(DownLoadMaterial downloadMaterial)
  84. {
  85. bool finish = false;
  86. for (int j = 0; j < listCompletedMaterial.Count; j++)
  87. {
  88. if (downloadMaterial.downLoadPath == listCompletedMaterial[j].downLoadPath)
  89. {
  90. if (downloadMaterial.updataTime == listCompletedMaterial[j].updataTime)
  91. finish = true;
  92. }
  93. }
  94. if (!finish)
  95. listNotMaterial.Add(downloadMaterial);
  96. return finish;
  97. }
  98. /// <summary>
  99. /// 下载成功
  100. /// </summary>
  101. /// <param name="localFilePath"></param>
  102. public void DownLoadCompleted(string localFilePath)
  103. {
  104. for (int i = 0; i < listNotMaterial.Count; i++)
  105. {
  106. if(Path.GetFileName( listNotMaterial[i].localLoadPath) == localFilePath)
  107. {
  108. listCompletedMaterial.Add(listNotMaterial[i]);
  109. LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
  110. listNotMaterial.RemoveAt(i);
  111. break;
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 下载失败
  117. /// </summary>
  118. /// <param name="localFilePath"></param>
  119. public void DownLoadFaild(string localFilePath)
  120. {
  121. for (int i = 0; i < listNotMaterial.Count; i++)
  122. {
  123. if (Path.GetFileName(listNotMaterial[i].localLoadPath) == localFilePath)
  124. {
  125. listFaildMaterial.Add(listNotMaterial[i]);
  126. MsgHandler.SendMsg(listNotMaterial[i].localLoadPath, null);
  127. listNotMaterial.RemoveAt(i);
  128. break;
  129. }
  130. }
  131. }
  132. private void OnDisable()
  133. {
  134. string msg = JsonConvert.SerializeObject(listCompletedMaterial);
  135. Debug.Log(msg);
  136. System.IO.Directory.CreateDirectory(path);
  137. // string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
  138. Debug.Log(path);
  139. using (System.IO.StreamWriter writer = System.IO.File.CreateText(path + "/CompletedMaterial.txt"))
  140. {
  141. writer.Write(msg);
  142. //writer.
  143. }
  144. }
  145. }
  146. public class MaterailDetail
  147. {
  148. public int id { get; set; }
  149. }
  150. public class DownLoadMaterial
  151. {
  152. public string downLoadPath { get; set; }
  153. public string localLoadPath { get; set; }
  154. public long updataTime { get; set; }
  155. public string type { get; set; }
  156. }