using LitJson; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class DownLoadXRManager { public static Dictionary downLoadCache = new Dictionary(); public static bool DownLoadForBytes(string msg, Action bytes, Action presson) { JsonData data = JsonMapper.ToObject(msg); if(data["drive"].ToString()== "minio") { //string bucket = data["data"]["bucket"].ToString(); //string objectName = data["data"]["bucket"].ToString(); string Url = data["url"].ToString(); Debug.Log("DGJ ===> DownLoadForBytes " + msg); RoomFileMinio.Instance.getFile(RoomFileMinio.Instance.bucket, Url, (RoomFileMinio.RoomFileData roomFile) => { bytes?.Invoke(roomFile.bytes); }); } else if (data["drive"].ToString() == "COS") { return false; } else { DownLoadUrlConfig config = new DownLoadUrlConfig(); config.url = data["url"].ToString(); config.bytes = bytes; config.presson = presson; GameObject go = GameObject.Instantiate(Resources.Load("DownLoadURLXRingItem")); DownLoadURLXRItem dlitem = go.GetComponent(); dlitem.startDownload(config); } return true; } public static bool DownLoadForFilePath(string msg, Action bytes, Action presson) { string Url = ""; JsonData data = JsonMapper.ToObject(msg); if (data["drive"].ToString() == "minio") { Url = data["url"].ToString(); } else if (data["drive"].ToString() == "COS") { return false; } else { Url = data["url"].ToString(); } if (PlayerPrefs.HasKey(Url)) { bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url)); return true; } return DownLoadForBytes(msg, (byte[] fileBytes) => { if(fileBytes!=null) { if (PlayerPrefs.HasKey(Url)) { bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url)); }else { //文件流信息 //StreamWriter sw; Stream sw; string path = Application.persistentDataPath + "/DownLoadXR/" + Url.Split("/")[Url.Split("/").Length - 1]; Debug.Log("准备存文件===》"+path); FileInfo file = new FileInfo(path); try { if (file.Exists) { file.Delete(); } if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR")) { Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR"); } //如果此文件存在则打开 //sw = file .Append(); //如果此文件不存在则创建 sw = file.Create(); //以行的形式写入信息 //sw.WriteLine(info); sw.Write(fileBytes, 0, fileBytes.Length); sw.Close(); sw.Dispose(); } catch { /* JsonData data = new JsonData(); data["type"] = "fileError"; List backTip = new List(); backTip.Add(data.ToJson()); backTip.Add(data.ToJson()); backTip.Add(data.ToJson()); WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("文件提示", "文件存储失败"+ path, Color.white, "icon", backTip, false, "", 5, "知道了.", "", "").ToJson()); */ } bytes?.Invoke(path); PlayerPrefs.SetString("DownLoadXR_" + Url, path); } } else { bytes?.Invoke(null); } }, presson); } public static bool DownLoadForTexture(string msg, Action bytes, Action presson) { string Url = ""; JsonData data = JsonMapper.ToObject(msg); if (data["drive"].ToString() == "minio") { } else if (data["drive"].ToString() == "COS") { return false; } else { Url = data["url"].ToString(); } return DownLoadForBytes(msg, (byte[] fileBytes) => { if (fileBytes != null) { Texture2D tex = new Texture2D(10,10); tex.LoadImage(fileBytes); bytes?.Invoke(tex); } else { bytes?.Invoke(null); } }, presson); } public static string getTestData(string url) { JsonData data = new JsonData(); data["drive"] = "URL"; data["url"] = url; return data.ToJson(); } public static string getTestData(string url,string drive) { JsonData data = new JsonData(); data["drive"] = drive; data["url"] = url; return data.ToJson(); } public class DownLoadUrlConfig { public string url; public Action bytes; public Action presson; } public class DownLoadMinIoConfig { public string url; public Action bytes; public Action presson; } public class DownLoadCosConfig { public string url; public Action bytes; public Action presson; } }