123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using QFramework;
- using System;
- using LitJson;
- using System.IO;
- public class DownLoadManage : MonoSingleton<DownLoadManage>
- {
- /// <summary>
- /// 最大下载数
- /// </summary>
- public int MaxDownLoad = 2;
- /// <summary>
- /// 当前下载数
- /// </summary>
- public int NowDownLoad = 0;
- /// <summary>
- /// 所有待下载数
- /// </summary>
- public int AllDownLoad = 0;
- private bool dlState = true;
- private string methodName;
- /// <summary>
- /// 待下载列表
- /// </summary>
- private List<DownLoadMaterial> listMaterialData;
- /// <summary>
- /// 当前正在下载列表
- /// </summary>
- private List<DownLoadMaterial> listDownLoading;
- /// <summary>
- /// 下载本地素材列表
- /// </summary>
- private List<DownLoadMaterial> listLocalMaterialData;
- private void Start()
- {
- listMaterialData = new List<DownLoadMaterial>();
- listDownLoading = new List<DownLoadMaterial>();
- listLocalMaterialData = new List<DownLoadMaterial>();
- StartCoroutine(DownLoadControl());
- }
- IEnumerator DownLoadControl()
- {
-
- while (true)
- {
- // Debug.Log(NowDownLoad + " Load " + MaxDownLoad);
- if( NowDownLoad < MaxDownLoad)
- {
- if (listMaterialData.Count > 0)
- {
- methodName = "/file/download";
- //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- // methodName = "/cdn/download";
- NowDownLoad++;
- Debug.Log("@@@@@@@" + listMaterialData[0].mObj.name + " " + listMaterialData[0].mObj.type + " " + listMaterialData[0].mObj.DownloadPath + " " + listMaterialData[0].mObj.localSavePath);
- switch ((MaterialType)int.Parse(listMaterialData[0].mObj.type))
- {
- case MaterialType.Text:
- //HttpTool.Instance.PostTxt(listMaterialData[0], listMaterialData[0].mObj.DownloadPath, "",
- // CallBack);
- Debug.Log("TEST");
- break;
- case MaterialType.Image:
- SendMaterials sendImage = new SendMaterials();
- sendImage.url = listMaterialData[0].mObj.DownloadPath;
- if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- sendImage.url = listMaterialData[0].mObj.cdnUrl;
- string jsonImage = JsonMapper.ToJson(sendImage);
- // Debug.Log("@@@@@@@" + jsonImage);
- if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- HttpTool.Instance.PostCDNImage(listMaterialData[0], jsonImage, CallBack);
- else
- HttpTool.Instance.PostImage(listMaterialData[0], methodName, jsonImage, CallBack);
- break;
- case MaterialType.Video:
- SendMaterials sendVideo = new SendMaterials();
- sendVideo.url = listMaterialData[0].mObj.DownloadPath;
- if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- sendVideo.url = listMaterialData[0].mObj.cdnUrl;
- string jsonVideo = JsonMapper.ToJson(sendVideo);
- if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- HttpTool.Instance.PostCDNVideo(listMaterialData[0], jsonVideo, CallBack);
- else
- HttpTool.Instance.PostVideo(listMaterialData[0], methodName, jsonVideo, CallBack);
- break;
- case MaterialType.Model:
- if (Path.GetFileName(listMaterialData[0].mObj.DownloadPath).Contains("."))
- {
- ErrorLogPanel.Instance.Show(" 上传的模型文件格式不正确 " + Path.GetFileName(listMaterialData[0].mObj.DownloadPath));
- }
- Debug.Log(listMaterialData[0].mObj.cdnUrl);
- if(listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- if(Path.GetFileName(listMaterialData[0].mObj.cdnUrl).Contains("."))
- {
- ErrorLogPanel.Instance.Show(" 上传的模型文件格式不正确 " + Path.GetFileName(listMaterialData[0].mObj.DownloadPath));
- }
- SendMaterials sendModel = new SendMaterials();
- sendModel.url = listMaterialData[0].mObj.DownloadPath;
- if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- sendModel.url = listMaterialData[0].mObj.cdnUrl;
- string jsonModel = JsonMapper.ToJson(sendModel);
- if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- HttpTool.Instance.PostCDNBundle(listMaterialData[0], listMaterialData[0].updateTime, jsonModel, CallBack);
- else
- HttpTool.Instance.PostBundle(listMaterialData[0], methodName, listMaterialData[0].updateTime, jsonModel, CallBack);
- break;
- default:
- break;
- }
- // 下载
- listDownLoading.Add(listMaterialData[0]);
- listMaterialData.Remove(listMaterialData[0]);
- AllDownLoad--;
- }
- if (listLocalMaterialData.Count > 0)
- {
- methodName = "/file/download";
- //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- // methodName = "/cdn/download";
- NowDownLoad++;
- switch ((MaterialType)int.Parse(listLocalMaterialData[0].mObj.type))
- {
- case MaterialType.NULL:
- break;
- case MaterialType.Image:
- HttpTool.Instance.GetLocalSprite(listLocalMaterialData[0], CallBack);
- break;
- case MaterialType.Model:
- //SendMaterials sendModel = new SendMaterials();
- //sendModel.url = listMaterialData[0].mObj.DownloadPath;
- //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty())
- // sendModel.url = listMaterialData[0].mObj.cdnUrl;
- //string jsonModel = JsonMapper.ToJson(sendModel);
- //HttpTool.Instance.PostBundle(listLocalMaterialData[0], methodName, listLocalMaterialData[0].updateTime, jsonModel, CallBack);
- HttpTool.Instance.GetLocalModel(listLocalMaterialData[0], CallBack);
- break;
- default:
- break;
- }
- GameManager.Instance.text.text = AllDownLoad.ToString();
- listDownLoading.Add(listLocalMaterialData[0]);
- listLocalMaterialData.Remove(listLocalMaterialData[0]);
- AllDownLoad--;
- }
- }
- yield return new WaitForSeconds(0.5f);
- }
- }
- /// <summary>
- /// 添加下载数据
- /// </summary>
- /// <param name="value"></param>
- public void AddDownLoadData(DownLoadMaterial value)
- {
- // Debug.Log("添加本地下载数据 " + value.mObj.name);
- listMaterialData.Add(value);
- AllDownLoad++;
- }
- /// <summary>
- /// 添加本地下载数据
- /// </summary>
- /// <param name="value"></param>
- public void AddLocalData(DownLoadMaterial value)
- {
- // Debug.Log("添加本地下载数据");
- listLocalMaterialData.Add(value);
- AllDownLoad++;
- }
- public void CallBack(DownLoadMaterial mObj, object value)
- {
- listDownLoading.Remove(mObj);
- NowDownLoad--;
-
- // Debug.Log(AllDownLoad + " AllDownLoad");
- if (AllDownLoad < 1)
- LoadManager.Instance.DownLoadFinish();
- // 通知 下载完成
- LoadManager.Instance.DownLoadEnd(mObj, value);
- }
- }
|