123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnityEngine;
- public class DownloadResManager : MonoSingleton<DownloadResManager>
- {
- /// <summary>
- /// 本地已下载素材列表
- /// </summary>
- private List<DownLoadMaterial> listCompletedMaterial;
- /// <summary>
- /// 未下载素材列表
- /// </summary>
- private List<DownLoadMaterial> listNotMaterial;
- /// <summary>
- /// 下载失败素材列表
- /// </summary>
- private List<DownLoadMaterial> listFaildMaterial;
- private string path;
- // Start is called before the first frame update
- void Start()
- {
- path = Application.persistentDataPath + "/Material";
- listNotMaterial = new List<DownLoadMaterial>();
- listFaildMaterial = new List<DownLoadMaterial>();
- //先判断是否存在,再创建
- if (!File.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- else
- {
- string msg = File.ReadAllText(path+ "/CompletedMaterial.txt");
- listCompletedMaterial = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(msg);
- }
- if (listCompletedMaterial == null)
- listCompletedMaterial = new List<DownLoadMaterial>();
- for (int i = 0; i < listCompletedMaterial.Count; i++)
- {
- if(!File.Exists(listCompletedMaterial[i].localLoadPath))
- {
- listCompletedMaterial.RemoveAt(i);
- }
- }
- }
- /// <summary>
- /// 批量素材下载
- /// </summary>
- /// <param name="listDLMaterial"> 所有素材列表 </param>
- public void DownLoad(List<DownLoadMaterial> listDLMaterial)
- {
- Debug.Log(listDLMaterial.Count);
- for (int i = 0; i < listDLMaterial.Count; i++)
- {
- Screen(listDLMaterial[i]);
- }
- // COSDownLoad cos = new COSDownLoad();
- COSDownLoad.Instance.TransferBatchDownloadObjects(listNotMaterial, path);
- }
- /// <summary>
- /// 单个素材下载
- /// </summary>
- /// <param name="downloadMaterial"></param>
- public void DownLoad(DownLoadMaterial downloadMaterial)
- {
- if( Screen( downloadMaterial) == false)
- {
- COSDownLoad.Instance.TransferDownloadObject(downloadMaterial, path);
- }
- else
- {
- LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
- }
- }
- /// <summary>
- /// 检测
- /// </summary>
- /// <param name="downloadMaterial"></param>
- /// <returns></returns>
- private bool Screen(DownLoadMaterial downloadMaterial)
- {
- bool finish = false;
- for (int j = 0; j < listCompletedMaterial.Count; j++)
- {
- if (downloadMaterial.downLoadPath == listCompletedMaterial[j].downLoadPath)
- {
- if (downloadMaterial.updataTime == listCompletedMaterial[j].updataTime)
- finish = true;
- }
- }
- if (!finish)
- listNotMaterial.Add(downloadMaterial);
- return finish;
- }
- /// <summary>
- /// 下载成功
- /// </summary>
- /// <param name="localFilePath"></param>
- public void DownLoadCompleted(string localFilePath)
- {
-
- for (int i = 0; i < listNotMaterial.Count; i++)
- {
- if(Path.GetFileName( listNotMaterial[i].localLoadPath) == localFilePath)
- {
- listCompletedMaterial.Add(listNotMaterial[i]);
- LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
- listNotMaterial.RemoveAt(i);
- break;
- }
- }
- }
- /// <summary>
- /// 下载失败
- /// </summary>
- /// <param name="localFilePath"></param>
- public void DownLoadFaild(string localFilePath)
- {
- for (int i = 0; i < listNotMaterial.Count; i++)
- {
- if (Path.GetFileName(listNotMaterial[i].localLoadPath) == localFilePath)
- {
- listFaildMaterial.Add(listNotMaterial[i]);
- MsgHandler.SendMsg(listNotMaterial[i].localLoadPath, null);
- listNotMaterial.RemoveAt(i);
- break;
- }
- }
- }
- private void OnDisable()
- {
- string msg = JsonConvert.SerializeObject(listCompletedMaterial);
- Debug.Log(msg);
- System.IO.Directory.CreateDirectory(path);
- // string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
- Debug.Log(path);
- using (System.IO.StreamWriter writer = System.IO.File.CreateText(path + "/CompletedMaterial.txt"))
- {
- writer.Write(msg);
- //writer.
- }
- }
- }
- public class MaterailDetail
- {
- public int id { get; set; }
- }
- public class DownLoadMaterial
- {
- public string downLoadPath { get; set; }
- public string localLoadPath { get; set; }
- public long updataTime { get; set; }
- public string type { get; set; }
- }
|