diff --git a/Editor/LLMEditor.cs b/Editor/LLMEditor.cs index b3e8d092..e20bde9c 100644 --- a/Editor/LLMEditor.cs +++ b/Editor/LLMEditor.cs @@ -280,6 +280,24 @@ async Task createButtons() } } + public override async Task AddOtherToggles() + { + if (GUILayout.Button("Redownload libraries", GUILayout.Width(buttonWidth))) + { + bool confirmed = EditorUtility.DisplayDialog( + "Confirm Action", + "Are you sure you want to redownload the libraries?", + "Yes", + "Cancel" + ); + + if (confirmed) + { + await LLMUnitySetup.RedownloadLibrary(); + } + } + } + async Task AddLoadButtons() { if (showCustomURL) await createCustomURLField(); diff --git a/Editor/PropertyEditor.cs b/Editor/PropertyEditor.cs index 8bab355f..48ca83b4 100644 --- a/Editor/PropertyEditor.cs +++ b/Editor/PropertyEditor.cs @@ -3,6 +3,7 @@ using UnityEngine; using System.Reflection; using System.Collections.Generic; +using System.Threading.Tasks; namespace LLMUnity { @@ -96,11 +97,17 @@ public virtual void AddLogo() } } + public virtual async Task AddOtherToggles() + { + await Task.CompletedTask; + } + public virtual void AddOptionsToggles(SerializedObject llmScriptSO) { AddLogo(); AddAdvancedOptionsToggle(llmScriptSO); AddDebugModeToggle(); + _ = AddOtherToggles(); Space(); } diff --git a/Runtime/LLMUnitySetup.cs b/Runtime/LLMUnitySetup.cs index cfa226bb..02e2ff44 100644 --- a/Runtime/LLMUnitySetup.cs +++ b/Runtime/LLMUnitySetup.cs @@ -115,6 +115,8 @@ public class LLMUnitySetup public static string modelDownloadPath = Path.Combine(LLMUnityStore, "models"); /// cache download path public static string cacheDownloadPath = Path.Combine(LLMUnityStore, "cache"); + public static string cacheZipPath = Path.Combine(cacheDownloadPath, Path.GetFileName(LlamaLibURL)); + public static string cacheZipHashPath = cacheZipPath + ".sha256"; /// Path of file with build information for runtime public static string LLMManagerPath = GetAssetPath("LLMManager.json"); @@ -464,45 +466,45 @@ static void ExtractInsideDirectory(string zipPath, string extractPath, string pr } } - static async Task DownloadAndExtractInsideDirectory(string url, string path, string setupDir) + static async Task DownloadAndExtractInsideDirectory() { - string urlName = Path.GetFileName(url); - string zipPath = Path.Combine(cacheDownloadPath, urlName); - string setupFile = Path.Combine(setupDir, urlName + ".complete"); + string setupDir = Path.Combine(libraryPath, "setup"); + Directory.CreateDirectory(setupDir); + + string setupFile = Path.Combine(setupDir, Path.GetFileName(LlamaLibURL) + ".complete"); if (File.Exists(setupFile)) return; Directory.CreateDirectory(cacheDownloadPath); foreach (string existingZipPath in Directory.GetFiles(cacheDownloadPath, "*.zip")) { - if (existingZipPath != zipPath) + if (existingZipPath != cacheZipPath) { - Debug.Log(existingZipPath); File.Delete(existingZipPath); } } - string hashurl = url + ".sha256"; - string hashPath = zipPath + ".sha256"; - string hash = File.Exists(hashPath)? File.ReadAllText(hashPath).Trim() : ""; + string hashurl = LlamaLibURL + ".sha256"; + string cacheZipNewHashPath = cacheZipHashPath + ".new"; + string hash = File.Exists(cacheZipHashPath)? File.ReadAllText(cacheZipHashPath).Trim() : ""; bool same_hash = false; try { new ResumingWebClient().GetURLFileSize(hashurl); // avoid showing error if url doesn't exist - await DownloadFile(hashurl, hashPath+".new", debug: false); - same_hash = File.ReadAllText(hashPath+".new").Trim() == hash; + await DownloadFile(hashurl, cacheZipNewHashPath, debug: false); + same_hash = File.ReadAllText(cacheZipNewHashPath).Trim() == hash; } catch {} - if (!File.Exists(zipPath) || !same_hash) await DownloadFile(url, zipPath, true, null, SetLibraryProgress); + if (!File.Exists(cacheZipPath) || !same_hash) await DownloadFile(LlamaLibURL, cacheZipPath, true, null, SetLibraryProgress); AssetDatabase.StartAssetEditing(); - ExtractInsideDirectory(zipPath, path, $"{libraryName}/runtimes/"); + ExtractInsideDirectory(cacheZipPath, libraryPath, $"{libraryName}/runtimes/"); CreateEmptyFile(setupFile); AssetDatabase.StopAssetEditing(); - if (File.Exists(hashPath+".new")) + if (File.Exists(cacheZipNewHashPath)) { - if (File.Exists(hashPath)) File.Delete(hashPath); - File.Move(hashPath+".new", hashPath); + if (File.Exists(cacheZipHashPath)) File.Delete(cacheZipHashPath); + File.Move(cacheZipNewHashPath, cacheZipHashPath); } } @@ -537,26 +539,41 @@ static void DeleteEarlierVersions() static async Task DownloadLibrary() { if (libraryProgress < 1) return; - libraryProgress = 0; - try { DeleteEarlierVersions(); - - string setupDir = Path.Combine(libraryPath, "setup"); - Directory.CreateDirectory(setupDir); - - // setup LlamaLib in StreamingAssets - await DownloadAndExtractInsideDirectory(LlamaLibURL, libraryPath, setupDir); } catch (Exception e) { LogError(e.Message); } + for (int i=1; i<=3; i++) + { + if (i > 1) Log("Downloading LlamaLib failed, try #" + i); + libraryProgress = 0; + try + { + await DownloadAndExtractInsideDirectory(); + break; + } + catch (Exception e) + { + LogError(e.Message); + } + } + libraryProgress = 1; } + public static async Task RedownloadLibrary() + { + if (File.Exists(cacheZipPath)) File.Delete(cacheZipPath); + if (File.Exists(cacheZipHashPath)) File.Delete(cacheZipHashPath); + if (Directory.Exists(libraryPath)) Directory.Delete(libraryPath, true); + await DownloadLibrary(); + } + private static void SetLibraryProgress(float progress) { libraryProgress = Math.Min(0.99f, progress);