123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- 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>
- {
-
-
-
- public List<DownLoadMaterial> listCompletedMaterial;
-
-
-
- public List<DownLoadMaterial> listNotMaterial;
-
-
-
- public List<DownLoadMaterial> listFaildMaterial;
- public Queue<DownLoadMaterial> qDownload;
-
- private int maxDownLoad = 1;
- private int nowDownLoad = 0;
- private string path;
- private bool state = false;
-
- void Start()
- {
- path = Application.persistentDataPath + "/Material/";
- listNotMaterial = new List<DownLoadMaterial>();
- listFaildMaterial = new List<DownLoadMaterial>();
- qDownload = new Queue<DownLoadMaterial>();
-
- if (!File.Exists(path + "CompletedMaterial.txt"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/Material");
- }
- else
- {
- string msg = File.ReadAllText(path + "CompletedMaterial.txt");
- Debug.Log("DGJ CompletedMaterial ==>" + 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);
- }
- }
-
- }
-
-
-
-
- public void DownLoad(List<DownLoadMaterial> listDLMaterial)
- {
- Debug.Log(listDLMaterial.Count);
- for (int i = 0; i < listDLMaterial.Count; i++)
- {
- Screen(listDLMaterial[i]);
- }
-
-
- }
-
-
-
-
- public void DownLoad(DownLoadMaterial downloadMaterial)
- {
-
-
-
-
- if(downloadMaterial.type=="-9")
- LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
- if (Screen(downloadMaterial) == false)
- {
-
-
-
- }
- else
- {
- LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
- }
- }
- public void StartDownLoad()
- {
- Debug.Log("DGJ ====> StartDownLoad ");
- StartCoroutine(DownLoadFile());
-
- }
- private IEnumerator DownLoadFile()
- {
- Debug.Log("DGJ ====> DownLoadFile ");
- while (true)
- {
- yield return new WaitForFixedUpdate();
- Debug.Log("DGJ ====> DownLoadFile 2");
- if (qDownload.Count > 0 && nowDownLoad < maxDownLoad)
- {
- nowDownLoad++;
- COSDownLoad.Instance.TransferDownloadObject(qDownload.Dequeue(), path);
- }
- }
- }
- private IEnumerator DownLoadOK()
- {
- if (qDownload.Count <= 0 )
- {
-
-
- UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.updateEnd);
- yield break;
- }
-
- yield return null;
- }
-
- 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);
- COSDownLoad.showDownLoadCount++;
- qDownload.Enqueue(downloadMaterial);
- }
-
- return finish;
- }
-
-
-
-
- public void DownLoadCompleted(string localFilePath)
- {
- nowDownLoad--;
- for (int i = 0; i < listNotMaterial.Count; i++)
- {
-
- if (Path.GetFileName( listNotMaterial[i].downLoadPath) == localFilePath)
- {
- listCompletedMaterial.Add(listNotMaterial[i]);
- LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
- listNotMaterial.RemoveAt(i);
- Save();
- break;
- }
- }
- }
-
-
-
-
- public void DownLoadFaild(string localFilePath)
- {
- nowDownLoad--;
- for (int i = 0; i < listNotMaterial.Count; i++)
- {
- Debug.Log("Hjj DownLoadFaild :" + listNotMaterial[i].downLoadPath + "——" + localFilePath);
- if (Path.GetFileName(listNotMaterial[i].downLoadPath) == localFilePath)
- {
- listFaildMaterial.Add(listNotMaterial[i]);
- MsgHandler.SendMsg(listNotMaterial[i].downLoadPath, new Msg(listNotMaterial[i].downLoadPath,null));
- listNotMaterial.RemoveAt(i);
- Save();
- break;
- }
- }
- }
- private void OnDisable()
- {
- Save();
- }
- private void Save()
- {
- string msg = JsonConvert.SerializeObject(listCompletedMaterial);
-
-
-
- Debug.Log("DGJ =====》 保存 "+ msg);
- using (System.IO.StreamWriter writer = System.IO.File.CreateText(path + "CompletedMaterial.txt"))
- {
- writer.Write(msg);
-
- }
- }
- }
- 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; }
- public DownLoadMaterial()
- {
- }
- public DownLoadMaterial(MaterialObjValue value)
- {
- this.downLoadPath = value.DownloadPath;
- this.type = value.type;
- this.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
- }
- }
|