Skip to content

Commit 4d24d62

Browse files
committed
cache LlamaLib to prevent re-downloads
1 parent 4744079 commit 4d24d62

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

Runtime/LLMUnitySetup.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public class LLMUnitySetup
113113
public static string LLMUnityStore = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "LLMUnity");
114114
/// <summary> Model download path </summary>
115115
public static string modelDownloadPath = Path.Combine(LLMUnityStore, "models");
116+
/// <summary> cache download path </summary>
117+
public static string cacheDownloadPath = Path.Combine(LLMUnityStore, "cache");
116118
/// <summary> Path of file with build information for runtime </summary>
117119
public static string LLMManagerPath = GetAssetPath("LLMManager.json");
118120

@@ -282,31 +284,33 @@ public static void CancelDownload(string savePath)
282284

283285
public static async Task DownloadFile(
284286
string fileUrl, string savePath, bool overwrite = false,
285-
Action<string> callback = null, Action<float> progressCallback = null
287+
Action<string> callback = null, Action<float> progressCallback = null, bool debug = true
286288
)
287289
{
288290
if (File.Exists(savePath) && !overwrite)
289291
{
290-
Log($"File already exists at: {savePath}");
292+
if(debug) Log($"File already exists at: {savePath}");
291293
}
292294
else
293295
{
294-
Log($"Downloading {fileUrl} to {savePath}...");
296+
if(debug) Log($"Downloading {fileUrl} to {savePath}...");
295297
string tmpPath = Path.Combine(Application.temporaryCachePath, Path.GetFileName(savePath));
296298

297299
ResumingWebClient client = new ResumingWebClient();
298300
downloadClients[savePath] = client;
301+
if (File.Exists(tmpPath) && overwrite) File.Delete(tmpPath);
299302
await client.DownloadFileTaskAsyncResume(new Uri(fileUrl), tmpPath, !overwrite, progressCallback);
300303
downloadClients.Remove(savePath);
301304
#if UNITY_EDITOR
302305
AssetDatabase.StartAssetEditing();
303306
#endif
304307
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
308+
if (File.Exists(savePath)) File.Delete(savePath);
305309
File.Move(tmpPath, savePath);
306310
#if UNITY_EDITOR
307311
AssetDatabase.StopAssetEditing();
308312
#endif
309-
Log($"Download complete!");
313+
if(debug) Log($"Download complete!");
310314
}
311315

312316
progressCallback?.Invoke(1f);
@@ -452,18 +456,43 @@ static void ExtractInsideDirectory(string zipPath, string extractPath, string pr
452456
static async Task DownloadAndExtractInsideDirectory(string url, string path, string setupDir)
453457
{
454458
string urlName = Path.GetFileName(url);
459+
string zipPath = Path.Combine(cacheDownloadPath, urlName);
455460
string setupFile = Path.Combine(setupDir, urlName + ".complete");
456461
if (File.Exists(setupFile)) return;
457462

458-
string zipPath = Path.Combine(Application.temporaryCachePath, urlName);
459-
await DownloadFile(url, zipPath, true, null, SetLibraryProgress);
463+
Directory.CreateDirectory(cacheDownloadPath);
464+
foreach (string existingZipPath in Directory.GetFiles(cacheDownloadPath, "*.zip"))
465+
{
466+
if (existingZipPath != zipPath)
467+
{
468+
Debug.Log(existingZipPath);
469+
File.Delete(existingZipPath);
470+
}
471+
}
472+
473+
string hashurl = url + ".sha256";
474+
string hashPath = zipPath + ".sha256";
475+
string hash = File.Exists(hashPath)? File.ReadAllText(hashPath).Trim() : "";
476+
bool same_hash = false;
477+
try
478+
{
479+
new ResumingWebClient().GetURLFileSize(hashurl); // avoid showing error if url doesn't exist
480+
await DownloadFile(hashurl, hashPath+".new", debug: false);
481+
same_hash = File.ReadAllText(hashPath+".new").Trim() == hash;
482+
} catch {}
483+
484+
if (!File.Exists(zipPath) || !same_hash) await DownloadFile(url, zipPath, true, null, SetLibraryProgress);
460485

461486
AssetDatabase.StartAssetEditing();
462487
ExtractInsideDirectory(zipPath, path, $"{libraryName}/runtimes/");
463488
CreateEmptyFile(setupFile);
464489
AssetDatabase.StopAssetEditing();
465490

466-
File.Delete(zipPath);
491+
if (File.Exists(hashPath+".new"))
492+
{
493+
if (File.Exists(hashPath)) File.Delete(hashPath);
494+
File.Move(hashPath+".new", hashPath);
495+
}
467496
}
468497

469498
static void DeleteEarlierVersions()

0 commit comments

Comments
 (0)