Skip to content

Commit 19c46ac

Browse files
committed
add button to redownload LlamaLib
1 parent 1c3daa0 commit 19c46ac

3 files changed

Lines changed: 66 additions & 24 deletions

File tree

Editor/LLMEditor.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,24 @@ async Task createButtons()
280280
}
281281
}
282282

283+
public override async Task AddOtherToggles()
284+
{
285+
if (GUILayout.Button("Redownload libraries", GUILayout.Width(buttonWidth)))
286+
{
287+
bool confirmed = EditorUtility.DisplayDialog(
288+
"Confirm Action",
289+
"Are you sure you want to redownload the libraries?",
290+
"Yes",
291+
"Cancel"
292+
);
293+
294+
if (confirmed)
295+
{
296+
await LLMUnitySetup.RedownloadLibrary();
297+
}
298+
}
299+
}
300+
283301
async Task AddLoadButtons()
284302
{
285303
if (showCustomURL) await createCustomURLField();

Editor/PropertyEditor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using System.Reflection;
55
using System.Collections.Generic;
6+
using System.Threading.Tasks;
67

78
namespace LLMUnity
89
{
@@ -96,11 +97,17 @@ public virtual void AddLogo()
9697
}
9798
}
9899

100+
public virtual async Task AddOtherToggles()
101+
{
102+
await Task.CompletedTask;
103+
}
104+
99105
public virtual void AddOptionsToggles(SerializedObject llmScriptSO)
100106
{
101107
AddLogo();
102108
AddAdvancedOptionsToggle(llmScriptSO);
103109
AddDebugModeToggle();
110+
_ = AddOtherToggles();
104111
Space();
105112
}
106113

Runtime/LLMUnitySetup.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public class LLMUnitySetup
115115
public static string modelDownloadPath = Path.Combine(LLMUnityStore, "models");
116116
/// <summary> cache download path </summary>
117117
public static string cacheDownloadPath = Path.Combine(LLMUnityStore, "cache");
118+
public static string cacheZipPath = Path.Combine(cacheDownloadPath, Path.GetFileName(LlamaLibURL));
119+
public static string cacheZipHashPath = cacheZipPath + ".sha256";
118120
/// <summary> Path of file with build information for runtime </summary>
119121
public static string LLMManagerPath = GetAssetPath("LLMManager.json");
120122

@@ -464,45 +466,45 @@ static void ExtractInsideDirectory(string zipPath, string extractPath, string pr
464466
}
465467
}
466468

467-
static async Task DownloadAndExtractInsideDirectory(string url, string path, string setupDir)
469+
static async Task DownloadAndExtractInsideDirectory()
468470
{
469-
string urlName = Path.GetFileName(url);
470-
string zipPath = Path.Combine(cacheDownloadPath, urlName);
471-
string setupFile = Path.Combine(setupDir, urlName + ".complete");
471+
string setupDir = Path.Combine(libraryPath, "setup");
472+
Directory.CreateDirectory(setupDir);
473+
474+
string setupFile = Path.Combine(setupDir, Path.GetFileName(LlamaLibURL) + ".complete");
472475
if (File.Exists(setupFile)) return;
473476

474477
Directory.CreateDirectory(cacheDownloadPath);
475478
foreach (string existingZipPath in Directory.GetFiles(cacheDownloadPath, "*.zip"))
476479
{
477-
if (existingZipPath != zipPath)
480+
if (existingZipPath != cacheZipPath)
478481
{
479-
Debug.Log(existingZipPath);
480482
File.Delete(existingZipPath);
481483
}
482484
}
483485

484-
string hashurl = url + ".sha256";
485-
string hashPath = zipPath + ".sha256";
486-
string hash = File.Exists(hashPath)? File.ReadAllText(hashPath).Trim() : "";
486+
string hashurl = LlamaLibURL + ".sha256";
487+
string cacheZipNewHashPath = cacheZipHashPath + ".new";
488+
string hash = File.Exists(cacheZipHashPath)? File.ReadAllText(cacheZipHashPath).Trim() : "";
487489
bool same_hash = false;
488490
try
489491
{
490492
new ResumingWebClient().GetURLFileSize(hashurl); // avoid showing error if url doesn't exist
491-
await DownloadFile(hashurl, hashPath+".new", debug: false);
492-
same_hash = File.ReadAllText(hashPath+".new").Trim() == hash;
493+
await DownloadFile(hashurl, cacheZipNewHashPath, debug: false);
494+
same_hash = File.ReadAllText(cacheZipNewHashPath).Trim() == hash;
493495
} catch {}
494496

495-
if (!File.Exists(zipPath) || !same_hash) await DownloadFile(url, zipPath, true, null, SetLibraryProgress);
497+
if (!File.Exists(cacheZipPath) || !same_hash) await DownloadFile(LlamaLibURL, cacheZipPath, true, null, SetLibraryProgress);
496498

497499
AssetDatabase.StartAssetEditing();
498-
ExtractInsideDirectory(zipPath, path, $"{libraryName}/runtimes/");
500+
ExtractInsideDirectory(cacheZipPath, libraryPath, $"{libraryName}/runtimes/");
499501
CreateEmptyFile(setupFile);
500502
AssetDatabase.StopAssetEditing();
501503

502-
if (File.Exists(hashPath+".new"))
504+
if (File.Exists(cacheZipNewHashPath))
503505
{
504-
if (File.Exists(hashPath)) File.Delete(hashPath);
505-
File.Move(hashPath+".new", hashPath);
506+
if (File.Exists(cacheZipHashPath)) File.Delete(cacheZipHashPath);
507+
File.Move(cacheZipNewHashPath, cacheZipHashPath);
506508
}
507509
}
508510

@@ -537,26 +539,41 @@ static void DeleteEarlierVersions()
537539
static async Task DownloadLibrary()
538540
{
539541
if (libraryProgress < 1) return;
540-
libraryProgress = 0;
541-
542542
try
543543
{
544544
DeleteEarlierVersions();
545-
546-
string setupDir = Path.Combine(libraryPath, "setup");
547-
Directory.CreateDirectory(setupDir);
548-
549-
// setup LlamaLib in StreamingAssets
550-
await DownloadAndExtractInsideDirectory(LlamaLibURL, libraryPath, setupDir);
551545
}
552546
catch (Exception e)
553547
{
554548
LogError(e.Message);
555549
}
556550

551+
for (int i=1; i<=3; i++)
552+
{
553+
if (i > 1) Log("Downloading LlamaLib failed, try #" + i);
554+
libraryProgress = 0;
555+
try
556+
{
557+
await DownloadAndExtractInsideDirectory();
558+
break;
559+
}
560+
catch (Exception e)
561+
{
562+
LogError(e.Message);
563+
}
564+
}
565+
557566
libraryProgress = 1;
558567
}
559568

569+
public static async Task RedownloadLibrary()
570+
{
571+
if (File.Exists(cacheZipPath)) File.Delete(cacheZipPath);
572+
if (File.Exists(cacheZipHashPath)) File.Delete(cacheZipHashPath);
573+
if (Directory.Exists(libraryPath)) Directory.Delete(libraryPath, true);
574+
await DownloadLibrary();
575+
}
576+
560577
private static void SetLibraryProgress(float progress)
561578
{
562579
libraryProgress = Math.Min(0.99f, progress);

0 commit comments

Comments
 (0)