From 1f26956fafb7b45069eb16348b7d85a254a15de7 Mon Sep 17 00:00:00 2001 From: JoC0de <53140583+JoC0de@users.noreply.github.com> Date: Sun, 5 Jan 2025 15:54:13 +0100 Subject: [PATCH] Load icon texture directly from file instead of using web-request with a file-uri (#690) --- .../Helper/NugetPackageTextureHelper.cs | 85 +++++++++---------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/src/NuGetForUnity/Editor/Helper/NugetPackageTextureHelper.cs b/src/NuGetForUnity/Editor/Helper/NugetPackageTextureHelper.cs index 5f423546..fad02bfc 100644 --- a/src/NuGetForUnity/Editor/Helper/NugetPackageTextureHelper.cs +++ b/src/NuGetForUnity/Editor/Helper/NugetPackageTextureHelper.cs @@ -51,63 +51,60 @@ internal static Task DownloadImageAsync([NotNull] string url) } #endif - var fromCache = false; if (url.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) { - // we only cache images coming from a remote server. - fromCache = true; + return Task.FromResult(LoadTextureFromFile(new Uri(url, UriKind.Absolute).LocalPath)); } - else if (ExistsInDiskCache(url)) + + var cacheFilePath = GetCacheFilePath(url); + if (File.Exists(cacheFilePath)) { - url = "file:///" + GetFilePath(url); - fromCache = true; + return Task.FromResult(LoadTextureFromFile(cacheFilePath)); } var taskCompletionSource = new TaskCompletionSource(); var request = UnityWebRequest.Get(new Uri(url, UriKind.Absolute)); - { - var downloadHandler = new DownloadHandlerTexture(false); + var downloadHandler = new DownloadHandlerTexture(false); - request.downloadHandler = downloadHandler; - request.timeout = 1; // 1 second - var operation = request.SendWebRequest(); - operation.completed += _ => + request.downloadHandler = downloadHandler; + request.timeout = 1; // 1 second + var operation = request.SendWebRequest(); + operation.completed += _ => + { + try { - try + if (!string.IsNullOrEmpty(request.error)) { - if (!string.IsNullOrEmpty(request.error)) - { #if UNITY_2020_1_OR_NEWER - NugetLogger.LogVerbose( - "Downloading image {0} failed! Web error: {1}, Handler error: {2}.", - url, - request.error, - downloadHandler.error); + NugetLogger.LogVerbose( + "Downloading image {0} failed! Web error: {1}, Handler error: {2}.", + url, + request.error, + downloadHandler.error); #else - NugetLogger.LogVerbose("Downloading image {0} failed! Web error: {1}.", url, request.error); + NugetLogger.LogVerbose("Downloading image {0} failed! Web error: {1}.", url, request.error); #endif - taskCompletionSource.TrySetResult(null); - return; - } - - var result = downloadHandler.texture; + taskCompletionSource.TrySetResult(null); + return; + } - if (result != null && !fromCache) - { - CacheTextureOnDisk(url, downloadHandler.data); - } + var result = downloadHandler.texture; - taskCompletionSource.TrySetResult(result); - } - finally + if (result != null) { - request.Dispose(); + CacheTextureOnDisk(cacheFilePath, downloadHandler.data); } - }; - return taskCompletionSource.Task; - } + taskCompletionSource.TrySetResult(result); + } + finally + { + request.Dispose(); + } + }; + + return taskCompletionSource.Task; } catch (Exception exception) { @@ -116,19 +113,21 @@ internal static Task DownloadImageAsync([NotNull] string url) } } - private static void CacheTextureOnDisk([NotNull] string url, [NotNull] byte[] bytes) + private static Texture2D LoadTextureFromFile(string localPath) { - var diskPath = GetFilePath(url); - File.WriteAllBytes(diskPath, bytes); + var imageBytes = File.ReadAllBytes(localPath); + var texture = new Texture2D(2, 2); + texture.LoadImage(imageBytes, true); + return texture; } - private static bool ExistsInDiskCache([NotNull] string url) + private static void CacheTextureOnDisk([NotNull] string cacheFilePath, [NotNull] byte[] bytes) { - return File.Exists(GetFilePath(url)); + File.WriteAllBytes(cacheFilePath, bytes); } [NotNull] - private static string GetFilePath([NotNull] string url) + private static string GetCacheFilePath([NotNull] string url) { return Path.Combine(Application.temporaryCachePath, GetHash(url)); }