123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 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>
- {
-
-
-
- private List<DownLoadMaterial> listCompletedMaterial;
-
-
-
- private List<DownLoadMaterial> listNotMaterial;
-
-
-
- private List<DownLoadMaterial> listFaildMaterial;
- private Queue<DownLoadMaterial> qDownload;
- private int maxDownLoad = 1;
- private int nowDownLoad = 0;
- private string path;
-
- void Start()
- {
- path = Application.persistentDataPath + "/Material/";
- listNotMaterial = new List<DownLoadMaterial>();
- listFaildMaterial = new List<DownLoadMaterial>();
- qDownload = new Queue<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);
- }
- }
- StartCoroutine(DownLoad());
- }
-
-
-
-
- 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 (Screen(downloadMaterial) == false)
- {
-
-
-
- }
- else
- {
- LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
- }
- }
- private IEnumerator DownLoad()
- {
-
- while(true)
- {
- yield return new WaitForFixedUpdate();
- if (qDownload.Count>0 && nowDownLoad<maxDownLoad)
- {
- nowDownLoad++;
- COSDownLoad.Instance.TransferDownloadObject(qDownload.Dequeue(), path);
- }
- }
- }
-
-
-
-
-
- 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);
- qDownload.Enqueue(downloadMaterial);
- }
-
- return finish;
- }
-
-
-
-
- public void DownLoadCompleted(string localFilePath)
- {
- nowDownLoad--;
- for (int i = 0; i < listNotMaterial.Count; i++)
- {
- Debug.Log("Hjj DownLoadCompleted :" + listNotMaterial[i].downLoadPath + "——" + localFilePath);
- if (Path.GetFileName( listNotMaterial[i].downLoadPath) == localFilePath)
- {
- listCompletedMaterial.Add(listNotMaterial[i]);
- LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
- listNotMaterial.RemoveAt(i);
- 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].localLoadPath, null);
- listNotMaterial.RemoveAt(i);
- break;
- }
- }
- }
- private void OnDisable()
- {
- string msg = JsonConvert.SerializeObject(listCompletedMaterial);
- Debug.Log(msg);
- System.IO.Directory.CreateDirectory(path);
-
- Debug.Log(path);
- 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; }
-
- }
|