DownloadResManager.cs 8.0 KB

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