DownLoadXRManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. public class DownLoadXRManager
  8. {
  9. public static Dictionary<string, byte[]> downLoadCache = new Dictionary<string, byte[]>();
  10. public static bool DownLoadForBytes(string msg, Action<byte[]> bytes, Action<float> presson)
  11. {
  12. JsonData data = JsonMapper.ToObject(msg);
  13. if(data["drive"].ToString()== "minio")
  14. {
  15. return false;
  16. }
  17. else if (data["drive"].ToString() == "COS")
  18. {
  19. return false;
  20. }
  21. else
  22. {
  23. DownLoadUrlConfig config = new DownLoadUrlConfig();
  24. config.url = data["url"].ToString();
  25. config.bytes = bytes;
  26. config.presson = presson;
  27. GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadURLXRingItem"));
  28. DownLoadURLXRItem dlitem = go.GetComponent<DownLoadURLXRItem>();
  29. dlitem.startDownload(config);
  30. }
  31. return true;
  32. }
  33. public static bool DownLoadForFilePath(string msg, Action<string> bytes, Action<float> presson)
  34. {
  35. string Url = "";
  36. JsonData data = JsonMapper.ToObject(msg);
  37. if (data["drive"].ToString() == "minio")
  38. {
  39. return false;
  40. }
  41. else if (data["drive"].ToString() == "COS")
  42. {
  43. return false;
  44. }
  45. else
  46. {
  47. Url = data["url"].ToString();
  48. }
  49. if (PlayerPrefs.HasKey(Url))
  50. {
  51. bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
  52. return true;
  53. }
  54. return DownLoadForBytes(msg, (byte[] fileBytes) => {
  55. if(fileBytes!=null)
  56. {
  57. if (PlayerPrefs.HasKey(Url))
  58. {
  59. bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
  60. }else
  61. {
  62. //文件流信息
  63. //StreamWriter sw;
  64. Stream sw;
  65. string path = Application.persistentDataPath + "/DownLoadXR/" + Url.Split("/")[Url.Split("/").Length - 1];
  66. Debug.Log("准备存文件===》"+path);
  67. FileInfo file = new FileInfo(path);
  68. try
  69. {
  70. if (file.Exists)
  71. {
  72. file.Delete();
  73. }
  74. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
  75. {
  76. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
  77. }
  78. //如果此文件存在则打开
  79. //sw = file .Append();
  80. //如果此文件不存在则创建
  81. sw = file.Create();
  82. //以行的形式写入信息
  83. //sw.WriteLine(info);
  84. sw.Write(fileBytes, 0, fileBytes.Length);
  85. sw.Close();
  86. sw.Dispose();
  87. }
  88. catch
  89. {
  90. /*
  91. JsonData data = new JsonData();
  92. data["type"] = "fileError";
  93. List<string> backTip = new List<string>();
  94. backTip.Add(data.ToJson());
  95. backTip.Add(data.ToJson());
  96. backTip.Add(data.ToJson());
  97. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("文件提示", "文件存储失败"+ path, Color.white, "icon", backTip, false, "", 5, "知道了.", "", "").ToJson());
  98. */
  99. }
  100. bytes?.Invoke(path);
  101. PlayerPrefs.SetString("DownLoadXR_" + Url, path);
  102. }
  103. }
  104. else
  105. {
  106. bytes?.Invoke(null);
  107. }
  108. }, presson);
  109. }
  110. public static bool DownLoadForTexture(string msg, Action<Texture2D> bytes, Action<float> presson)
  111. {
  112. string Url = "";
  113. JsonData data = JsonMapper.ToObject(msg);
  114. if (data["drive"].ToString() == "minio")
  115. {
  116. return false;
  117. }
  118. else if (data["drive"].ToString() == "COS")
  119. {
  120. return false;
  121. }
  122. else
  123. {
  124. Url = data["url"].ToString();
  125. }
  126. return DownLoadForBytes(msg, (byte[] fileBytes) => {
  127. if (fileBytes != null)
  128. {
  129. Texture2D tex = new Texture2D(10,10);
  130. tex.LoadImage(fileBytes);
  131. bytes?.Invoke(tex);
  132. }
  133. else
  134. {
  135. bytes?.Invoke(null);
  136. }
  137. }, presson);
  138. }
  139. public static string getTestData(string url)
  140. {
  141. JsonData data = new JsonData();
  142. data["drive"] = "URL";
  143. data["url"] = url;
  144. return data.ToJson();
  145. }
  146. public class DownLoadUrlConfig
  147. {
  148. public string url;
  149. public Action<byte[]> bytes;
  150. public Action<float> presson;
  151. }
  152. public class DownLoadMinIoConfig
  153. {
  154. public string url;
  155. public Action<byte[]> bytes;
  156. public Action<float> presson;
  157. }
  158. public class DownLoadCosConfig
  159. {
  160. public string url;
  161. public Action<byte[]> bytes;
  162. public Action<float> presson;
  163. }
  164. }