StreamingAssetsUtility.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using WebP;
  7. public class StreamingAssetsUtility
  8. {
  9. public static async Task<byte[]> ReadFile(string path)
  10. {
  11. using (FileStream fileStream = new FileStream(
  12. path, FileMode.Open, FileAccess.Read))
  13. {
  14. var resultBytes = new byte[fileStream.Length];
  15. await fileStream.ReadAsync(resultBytes, 0, (int)fileStream.Length);
  16. return resultBytes;
  17. }
  18. }
  19. #region 画像の取得
  20. public static Texture2D BinaryToTexture(byte[] bytes)
  21. {
  22. Texture2D texture = new Texture2D(1, 1);
  23. texture.LoadImage(bytes);
  24. return texture;
  25. }
  26. public static async Task<Sprite> GetSprite(string fileName, bool isCard = false, bool isLauncher = false)
  27. {
  28. string path = "";
  29. if (isCard)
  30. {
  31. if (fileName.Contains("-token"))
  32. {
  33. return await GetTokenImageData(Path.Combine(GetStreamingAssetPath("Textures", isLauncher), $"Card/{fileName}.png").Replace("\\", "/"));
  34. }
  35. else
  36. {
  37. path = Path.Combine(GetStreamingAssetPath("Textures", isLauncher), $"Card/{fileName}.webp").Replace("\\", "/");
  38. if (!File.Exists(path))
  39. {
  40. return await GetCardImageData(fileName, path);
  41. }
  42. else
  43. {
  44. return await GetCardImageDataLocal(path);
  45. }
  46. }
  47. }
  48. else
  49. {
  50. return await GetSpriteImage(fileName, isLauncher);
  51. }
  52. }
  53. public static async Task<Sprite> GetSpriteImage(string fileName, bool isLauncher = false)
  54. {
  55. string path = Path.Combine(GetStreamingAssetPath("Textures", isLauncher), $"{fileName}.jpg").Replace("\\", "/");
  56. if (!File.Exists(path))
  57. path = Path.Combine(GetStreamingAssetPath("Textures", isLauncher), $"{fileName}.png").Replace("\\", "/");
  58. if (File.Exists(path))
  59. {
  60. byte[] imageBuff = await ReadFile(path);
  61. Texture2D tex = BinaryToTexture(imageBuff);
  62. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  63. return sprite;
  64. }
  65. return null;
  66. }
  67. public static async Task<Sprite> GetTokenImageData(string path)
  68. {
  69. if (File.Exists(path))
  70. {
  71. byte[] imageBuff = await ReadFile(path);
  72. Texture2D tex = BinaryToTexture(imageBuff);
  73. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  74. return sprite;
  75. }
  76. return null;
  77. }
  78. public static async Task<Sprite> GetCardImageDataLocal(string path)
  79. {
  80. if (File.Exists(path))
  81. {
  82. Debug.Log($"File Exists Locally: {path}");
  83. byte[] imageBuff = await ReadFile(path);
  84. Debug.Log($"Grabbing image bytes: {imageBuff}");
  85. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  86. Debug.Log($"Converting WebP to Texture2D: {texture}");
  87. if (lError == WebP.Error.Success)
  88. {
  89. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  90. return sprite;
  91. }
  92. else
  93. {
  94. Debug.Log(lError.ToString());
  95. }
  96. }
  97. return null;
  98. }
  99. public static async Task<Sprite> GetCardImageData(string fileName, string filePath)
  100. {
  101. Sprite sprite;
  102. // Attempt to get the card image from repo
  103. sprite = await HandleCardImage(fileName, filePath);
  104. if (sprite != null) return sprite;
  105. else
  106. {
  107. // Attempt to get the card image from repo, this time with the sample suffix
  108. sprite = await HandleCardImage(fileName, filePath, isSample: true);
  109. if (sprite != null) return sprite;
  110. return null;
  111. }
  112. }
  113. public static async Task<Sprite> HandleCardImage(string fileName, string filePath, bool isSample = false)
  114. {
  115. string urlPath = $"https://raw.githubusercontent.com/TakaOtaku/Digimon-Card-App/main/src/assets/images/cards/{fileName}";
  116. if (isSample) urlPath += $"-Sample.webp";
  117. else urlPath += $".webp";
  118. UnityWebRequest webReq_CardImage = UnityWebRequest.Get(urlPath);
  119. UnityWebRequestAsyncOperation operation = webReq_CardImage.SendWebRequest();
  120. while (!operation.isDone)
  121. {
  122. await Task.Yield();
  123. }
  124. Debug.Log($"WebRequest isDone: {fileName}");
  125. if (webReq_CardImage.result == UnityWebRequest.Result.ConnectionError)
  126. return null;
  127. else if (webReq_CardImage.result == UnityWebRequest.Result.ProtocolError)
  128. return null;
  129. else
  130. {
  131. Debug.Log($"WebRequest Successful: Checking local file - {File.Exists(filePath)}");
  132. if (!File.Exists(filePath))
  133. File.WriteAllBytes(filePath, webReq_CardImage.downloadHandler.data);
  134. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(webReq_CardImage.downloadHandler.data, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  135. if (lError == WebP.Error.Success)
  136. {
  137. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  138. return sprite;
  139. }
  140. else Debug.Log($"Failed to convert: {lError.ToString()}");
  141. return null;
  142. }
  143. }
  144. #endregion
  145. public static bool IsCardExists(CEntity_Base cEntity_Base)
  146. {
  147. string path = Path.Combine(GetStreamingAssetPath("Textures", false), $"Card/{cEntity_Base.CardSpriteName}.webp").Replace("\\", "/");
  148. if (cEntity_Base.CardSpriteName.Contains("token"))
  149. path = Path.Combine(GetStreamingAssetPath("Textures", false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
  150. return File.Exists(path);
  151. }
  152. #region テキストファイルの取得
  153. public static string GetText(string fileName)
  154. {
  155. string path = Path.Combine(GetStreamingAssetPath("", false), $"{fileName}.txt").Replace("\\", "/");
  156. if (File.Exists(path))
  157. {
  158. return File.ReadAllText(path);
  159. }
  160. return "";
  161. }
  162. #endregion
  163. public static string GetStreamingAssetPath(string subPath, bool isLauncher)
  164. {
  165. if (isLauncher)
  166. {
  167. string path = Application.streamingAssetsPath;
  168. path = GetOneUpperDirectoryPath(path);
  169. path = Path.Combine(path, $"Assets/{subPath}").Replace("\\", "/");
  170. return path;
  171. }
  172. else
  173. {
  174. string path = Application.streamingAssetsPath;
  175. path = GetOneUpperDirectoryPath(path);
  176. path = GetOneUpperDirectoryPath(path);
  177. path = Path.Combine(path, $"Assets/{subPath}").Replace("\\", "/");
  178. return path;
  179. }
  180. }
  181. static string GetOneUpperDirectoryPath(string path)
  182. {
  183. if (String.IsNullOrEmpty(path)) return "";
  184. path = path.Replace("\\", "/");
  185. if (!path.Contains("/")) return path;
  186. path = path.Substring(0, path.LastIndexOf("/") + 1);
  187. if (path.Length >= 1)
  188. {
  189. if (path[path.Length - 1] == '/')
  190. {
  191. path = path.Substring(0, path.LastIndexOf("/"));
  192. }
  193. }
  194. return path.Substring(0, path.LastIndexOf("/") + 1);
  195. }
  196. }