@@ -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