123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 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>
- public List<DownLoadMaterial> listCompletedMaterial;
- /// <summary>
- /// 未下载素材列表
- /// </summary>
- public List<DownLoadMaterial> listNotMaterial;
- /// <summary>
- /// 下载失败素材列表
- /// </summary>
- public List<DownLoadMaterial> listFaildMaterial;
- public Queue<DownLoadMaterial> qDownload;
- // private Queue<DownLoadMaterial> orderDownload;
- private int maxDownLoad = 1;
- private int nowDownLoad = 0;
- private string path;
- private bool state = false;
- // Start is called before the first frame update
- void Start()
- {
- if (File.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml"))
- {
- File.Delete(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml");
- }
- if (File.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat"))
- {
- File.Delete(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat");
- }
- path = Application.persistentDataPath + "/Material/";
- listNotMaterial = new List<DownLoadMaterial>();
- listFaildMaterial = new List<DownLoadMaterial>();
- qDownload = new Queue<DownLoadMaterial>();
- Debug.Log("DGJ CompletedMaterial ==>" + path + "CompletedMaterial.txt");
- // PlayerPrefs.DeleteAll();
- //先判断是否存在,再创建
- if (!File.Exists(path + "CompletedMaterial.txt"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/Material");
- }
- else
- {
- string msg = File.ReadAllText(path + "CompletedMaterial.txt");
- Debug.Log("DGJ CompletedMaterial ==>" + msg);
- listCompletedMaterial = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(msg);
- }
-
- /*
- if(PlayerPrefs.HasKey("CompletedMaterial"))
- {
- string msg = PlayerPrefs.GetString("CompletedMaterial");
- listCompletedMaterial = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(msg);
- }*/
- if (listCompletedMaterial == null)
- listCompletedMaterial = new List<DownLoadMaterial>();
- int ct = listCompletedMaterial.Count;
- for (int i = ct - 1; i>=0; i--)
- {
- if(!File.Exists(listCompletedMaterial[i].localLoadPath))
- {
- listCompletedMaterial.RemoveAt(i);
- }
- }
- // StartCoroutine(DownLoadFile());
- }
- /// <summary>
- /// 批量素材下载
- /// </summary>
- /// <param name="listDLMaterial"> 所有素材列表 </param>
- public void DownLoad(List<DownLoadMaterial> listDLMaterial)
- {
- // Debug.Log(listDLMaterial.Count);
- for (int i = 0; i < listDLMaterial.Count; i++)
- {
- // Debug.Log(" DGJ DownLoad ===>" + listDLMaterial[i].downLoadPath);
- Screen(listDLMaterial[i]);
- }
- // COSDownLoad cos = new COSDownLoad();
- // COSDownLoad.Instance.TransferBatchDownloadObjects(listNotMaterial, path);
- }
- /// <summary>
- /// 单个素材下载
- /// </summary>
- /// <param name="downloadMaterial"></param>
- public void DownLoad(DownLoadMaterial downloadMaterial)
- {
- Debug.LogError("单个素材下载 " + downloadMaterial.downLoadPath);
- //List<DownLoadMaterial> list = new List<DownLoadMaterial>();
- //list.Add(downloadMaterial);
- // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path);
- if(downloadMaterial.type=="-9")
- {
- LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
- return;
- }
-
-
- if (Screen(downloadMaterial) == false)
- {
- //List<DownLoadMaterial> list = new List<DownLoadMaterial>();
- //list.Add(downloadMaterial);
- // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path);
- }
- else
- {
- LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial);
- }
- }
- public void StartDownLoad()
- {
- Debug.Log("DGJ ====> StartDownLoad ");
- StartCoroutine(DownLoadFile());
- // state = true;
- }
- 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 )
- {
- // SaveLocaData();
- //loadUI.ProgressStr = "";
- UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.updateEnd);
- yield break;
- }
- // loadUI.ProgressStr = ((float)(m_Downloaded.Count + m_FiledDownload.Count) / count).ToString("P");
- yield return null;
- }
-
- /// <summary>
- /// 检测
- /// </summary>
- /// <param name="downloadMaterial"></param>
- /// <returns></returns>
- private bool Screen(DownLoadMaterial downloadMaterial)
- {
- bool finish = false;
- if(string.IsNullOrEmpty(downloadMaterial.downLoadPath))
- {
- Debug.LogError( " 素材 没有下载地址 ");
- return finish;
- }
- for (int j = 0; j < listCompletedMaterial.Count; j++)
- {
- if (downloadMaterial.downLoadPath == listCompletedMaterial[j].downLoadPath)
- {
- if (!UserInfo.Instance.is20)
- {
- if (LoginPanel.downloadMatList.ContainsKey(downloadMaterial.downLoadPath) && LoginPanel.downloadMatList[downloadMaterial.downLoadPath] == listCompletedMaterial[j].updataTime)
- finish = true;
- }
- else
- {
- if (downloadMaterial.updataTime == listCompletedMaterial[j].updataTime)
- finish = true;
- }
- }
- }
- if (!File.Exists(downloadMaterial.localLoadPath))
- {
- finish = false;
- }
- if (!finish)
- {
- listNotMaterial.Add(downloadMaterial);
- // Debug.Log("DGJ ===> showDownLoadCount " + COSDownLoad.showDownLoadCount );
- COSDownLoad.showDownLoadCount++;
- qDownload.Enqueue(downloadMaterial);
- }
-
- return finish;
- }
- /// <summary>
- /// 下载成功
- /// </summary>
- /// <param name="localFilePath"></param>
- 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);
- break;
- }
- if((localFilePath.Split('.')[1]=="xml"|| localFilePath.Split('.')[1]=="dat")&&( (localFilePath.Split('.')[1] == Path.GetFileName(listNotMaterial[i].downLoadPath).Split('.')[1])))
- {
- listCompletedMaterial.Add(listNotMaterial[i]);
- LocalLoadManager.Instance.LocalLoadMaterial(listNotMaterial[i]);
- listNotMaterial.RemoveAt(i);
- break;
- }
- }
- }
- /// <summary>
- /// 下载失败
- /// </summary>
- /// <param name="localFilePath"></param>
- 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);
- break;
- }
- }
- }
- private void OnDisable()
- {
- // Save();
-
- }
- public void Save()
- {
- 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("DGJ =====》 保存 "+ msg);
- using (System.IO.StreamWriter writer = System.IO.File.CreateText(path + "CompletedMaterial.txt"))
- {
- writer.Write(msg);
- //writer.
- }
- // string ms2g = JsonConvert.SerializeObject(listCompletedMaterial);
- // PlayerPrefs.SetString("CompletedMaterial", ms2g);
- }
- }
- public class MaterailDetail
- {
- public int id { get; set; }
- }
- public class DownLoadMaterial
- {
- public string name;
- public string downLoadPath { get; set; }
- public string localLoadPath { get; set; }
- private long _updataTime;
- public long updataTime { get { return _updataTime; } set {
- Debug.Log(value+"DGJ ===> Screen " + downLoadPath);
- _updataTime = value; } }
- public string type { get; set; }
- public DownLoadMaterial()
- {
- }
- public DownLoadMaterial(MaterialObjValue value)
- {
- this.name = value.name;
- this.downLoadPath = value.DownloadPath;
- this.type = value.type.ToString();
- this.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
- }
- }
|