DownloadResManager.cs 10 KB

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