DownloadResManager.cs 8.5 KB

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