DownLoadManage.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using QFramework;
  5. using System;
  6. using LitJson;
  7. using System.IO;
  8. public class DownLoadManage : MonoSingleton<DownLoadManage>
  9. {
  10. /// <summary>
  11. /// 最大下载数
  12. /// </summary>
  13. public int MaxDownLoad = 2;
  14. /// <summary>
  15. /// 当前下载数
  16. /// </summary>
  17. public int NowDownLoad = 0;
  18. /// <summary>
  19. /// 所有待下载数
  20. /// </summary>
  21. public int AllDownLoad = 0;
  22. private bool dlState = true;
  23. private string methodName;
  24. /// <summary>
  25. /// 待下载列表
  26. /// </summary>
  27. private List<DownLoadMaterial> listMaterialData;
  28. /// <summary>
  29. /// 当前正在下载列表
  30. /// </summary>
  31. private List<DownLoadMaterial> listDownLoading;
  32. /// <summary>
  33. /// 下载本地素材列表
  34. /// </summary>
  35. private List<DownLoadMaterial> listLocalMaterialData;
  36. private void Start()
  37. {
  38. listMaterialData = new List<DownLoadMaterial>();
  39. listDownLoading = new List<DownLoadMaterial>();
  40. listLocalMaterialData = new List<DownLoadMaterial>();
  41. StartCoroutine(DownLoadControl());
  42. }
  43. IEnumerator DownLoadControl()
  44. {
  45. while (true)
  46. {
  47. // Debug.Log(NowDownLoad + " Load " + MaxDownLoad);
  48. if( NowDownLoad < MaxDownLoad)
  49. {
  50. if (listMaterialData.Count > 0)
  51. {
  52. methodName = "/file/download";
  53. //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  54. // methodName = "/cdn/download";
  55. NowDownLoad++;
  56. Debug.Log("@@@@@@@" + listMaterialData[0].mObj.name + " " + listMaterialData[0].mObj.type + " " + listMaterialData[0].mObj.DownloadPath + " " + listMaterialData[0].mObj.localSavePath);
  57. switch ((MaterialType)int.Parse(listMaterialData[0].mObj.type))
  58. {
  59. case MaterialType.Text:
  60. //HttpTool.Instance.PostTxt(listMaterialData[0], listMaterialData[0].mObj.DownloadPath, "",
  61. // CallBack);
  62. Debug.Log("TEST");
  63. break;
  64. case MaterialType.Image:
  65. SendMaterials sendImage = new SendMaterials();
  66. sendImage.url = listMaterialData[0].mObj.DownloadPath;
  67. if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  68. sendImage.url = listMaterialData[0].mObj.cdnUrl;
  69. string jsonImage = JsonMapper.ToJson(sendImage);
  70. // Debug.Log("@@@@@@@" + jsonImage);
  71. if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  72. HttpTool.Instance.PostCDNImage(listMaterialData[0], jsonImage, CallBack);
  73. else
  74. HttpTool.Instance.PostImage(listMaterialData[0], methodName, jsonImage, CallBack);
  75. break;
  76. case MaterialType.Video:
  77. SendMaterials sendVideo = new SendMaterials();
  78. sendVideo.url = listMaterialData[0].mObj.DownloadPath;
  79. if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  80. sendVideo.url = listMaterialData[0].mObj.cdnUrl;
  81. string jsonVideo = JsonMapper.ToJson(sendVideo);
  82. if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  83. HttpTool.Instance.PostCDNVideo(listMaterialData[0], jsonVideo, CallBack);
  84. else
  85. HttpTool.Instance.PostVideo(listMaterialData[0], methodName, jsonVideo, CallBack);
  86. break;
  87. case MaterialType.Model:
  88. if (Path.GetFileName(listMaterialData[0].mObj.DownloadPath).Contains("."))
  89. {
  90. ErrorLogPanel.Instance.Show(" 上传的模型文件格式不正确 " + Path.GetFileName(listMaterialData[0].mObj.DownloadPath));
  91. }
  92. Debug.Log(listMaterialData[0].mObj.cdnUrl);
  93. if(listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  94. if(Path.GetFileName(listMaterialData[0].mObj.cdnUrl).Contains("."))
  95. {
  96. ErrorLogPanel.Instance.Show(" 上传的模型文件格式不正确 " + Path.GetFileName(listMaterialData[0].mObj.DownloadPath));
  97. }
  98. SendMaterials sendModel = new SendMaterials();
  99. sendModel.url = listMaterialData[0].mObj.DownloadPath;
  100. if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  101. sendModel.url = listMaterialData[0].mObj.cdnUrl;
  102. string jsonModel = JsonMapper.ToJson(sendModel);
  103. if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  104. HttpTool.Instance.PostCDNBundle(listMaterialData[0], listMaterialData[0].updateTime, jsonModel, CallBack);
  105. else
  106. HttpTool.Instance.PostBundle(listMaterialData[0], methodName, listMaterialData[0].updateTime, jsonModel, CallBack);
  107. break;
  108. default:
  109. break;
  110. }
  111. // 下载
  112. listDownLoading.Add(listMaterialData[0]);
  113. listMaterialData.Remove(listMaterialData[0]);
  114. AllDownLoad--;
  115. }
  116. if (listLocalMaterialData.Count > 0)
  117. {
  118. methodName = "/file/download";
  119. //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  120. // methodName = "/cdn/download";
  121. NowDownLoad++;
  122. switch ((MaterialType)int.Parse(listLocalMaterialData[0].mObj.type))
  123. {
  124. case MaterialType.NULL:
  125. break;
  126. case MaterialType.Image:
  127. HttpTool.Instance.GetLocalSprite(listLocalMaterialData[0], CallBack);
  128. break;
  129. case MaterialType.Model:
  130. //SendMaterials sendModel = new SendMaterials();
  131. //sendModel.url = listMaterialData[0].mObj.DownloadPath;
  132. //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
  133. // sendModel.url = listMaterialData[0].mObj.cdnUrl;
  134. //string jsonModel = JsonMapper.ToJson(sendModel);
  135. //HttpTool.Instance.PostBundle(listLocalMaterialData[0], methodName, listLocalMaterialData[0].updateTime, jsonModel, CallBack);
  136. HttpTool.Instance.GetLocalModel(listLocalMaterialData[0], CallBack);
  137. break;
  138. default:
  139. break;
  140. }
  141. GameManager.Instance.text.text = AllDownLoad.ToString();
  142. listDownLoading.Add(listLocalMaterialData[0]);
  143. listLocalMaterialData.Remove(listLocalMaterialData[0]);
  144. AllDownLoad--;
  145. }
  146. }
  147. yield return new WaitForSeconds(0.5f);
  148. }
  149. }
  150. /// <summary>
  151. /// 添加下载数据
  152. /// </summary>
  153. /// <param name="value"></param>
  154. public void AddDownLoadData(DownLoadMaterial value)
  155. {
  156. // Debug.Log("添加本地下载数据 " + value.mObj.name);
  157. listMaterialData.Add(value);
  158. AllDownLoad++;
  159. }
  160. /// <summary>
  161. /// 添加本地下载数据
  162. /// </summary>
  163. /// <param name="value"></param>
  164. public void AddLocalData(DownLoadMaterial value)
  165. {
  166. // Debug.Log("添加本地下载数据");
  167. listLocalMaterialData.Add(value);
  168. AllDownLoad++;
  169. }
  170. public void CallBack(DownLoadMaterial mObj, object value)
  171. {
  172. listDownLoading.Remove(mObj);
  173. NowDownLoad--;
  174. // Debug.Log(AllDownLoad + " AllDownLoad");
  175. if (AllDownLoad < 1)
  176. LoadManager.Instance.DownLoadFinish();
  177. // 通知 下载完成
  178. LoadManager.Instance.DownLoadEnd(mObj, value);
  179. }
  180. }