DownloadResManager.cs 9.4 KB

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