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 { /// /// 本地已下载素材列表 /// public List listCompletedMaterial; /// /// 未下载素材列表 /// public List listNotMaterial; /// /// 下载失败素材列表 /// public List listFaildMaterial; public Queue qDownload; // private Queue 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(); listFaildMaterial = new List(); qDownload = new Queue(); 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>(msg); } /* if(PlayerPrefs.HasKey("CompletedMaterial")) { string msg = PlayerPrefs.GetString("CompletedMaterial"); listCompletedMaterial = JsonConvert.DeserializeObject>(msg); }*/ if (listCompletedMaterial == null) listCompletedMaterial = new List(); int ct = listCompletedMaterial.Count; for (int i = ct - 1; i>=0; i--) { if(!File.Exists(listCompletedMaterial[i].localLoadPath)) { listCompletedMaterial.RemoveAt(i); } } // StartCoroutine(DownLoadFile()); } /// /// 批量素材下载 /// /// 所有素材列表 public void DownLoad(List 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); } /// /// 单个素材下载 /// /// public void DownLoad(DownLoadMaterial downloadMaterial) { Debug.LogError("单个素材下载 " + downloadMaterial.downLoadPath); //List list = new List(); //list.Add(downloadMaterial); // COSDownLoad.Instance.TransferBatchDownloadObjects(list, path); if(downloadMaterial.type=="-9") { LocalLoadManager.Instance.LocalLoadMaterial(downloadMaterial); return; } if (Screen(downloadMaterial) == false) { //List list = new List(); //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; }   /// /// 检测 /// /// /// 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; } /// /// 下载成功 /// /// 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; } } } /// /// 下载失败 /// /// 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); } }