private static void DownloadHelper( DownloadableFile[] downloadableFiles, int retry = 1, System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null, System.ComponentModel.AsyncCompletedEventHandler onDownloadFileCompleted = null) { if (downloadableFiles == null || downloadableFiles.Length == 0) { if (onDownloadFileCompleted != null) { onDownloadFileCompleted(null, null); } } else if (downloadableFiles.Length == 1) { DownloadHelper(downloadableFiles[0], retry, onDownloadProgressChanged, onDownloadFileCompleted); } else { DownloadableFile currentFile = downloadableFiles[0]; DownloadableFile[] remainingFiles = new DownloadableFile[downloadableFiles.Length - 1]; Array.Copy(downloadableFiles, 1, remainingFiles, 0, remainingFiles.Length); DownloadHelper(currentFile, retry, onDownloadProgressChanged, (object sender, System.ComponentModel.AsyncCompletedEventArgs e) => { DownloadHelper(remainingFiles, retry, onDownloadProgressChanged, onDownloadFileCompleted); } ); } }
private static async Task DownloadHelper( DownloadableFile downloadableFile, int retry = 1, System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null ) { if (downloadableFile.Url == null) { return; } //uncomment the following line to force re-download every time. //File.Delete(downloadableFile.LocalFile); if (!File.Exists(downloadableFile.LocalFile) || new FileInfo(downloadableFile.LocalFile).Length == 0) { try { //Download the file Trace.WriteLine("downloading file from:" + downloadableFile.Url + " to: " + downloadableFile.LocalFile); System.Net.WebClient downloadClient = new System.Net.WebClient(); if (onDownloadProgressChanged != null) { downloadClient.DownloadProgressChanged += onDownloadProgressChanged; } FileInfo fi = new FileInfo(downloadableFile.LocalFile); if (!fi.Directory.Exists) { fi.Directory.Create(); } await downloadClient.DownloadFileTaskAsync(new Uri(downloadableFile.Url), downloadableFile.LocalFile); } catch (Exception e) { if (File.Exists(downloadableFile.LocalFile)) { //The downloaded file may be corrupted, should delete it File.Delete(downloadableFile.LocalFile); } if (retry > 0) { await DownloadHelper(downloadableFile, retry - 1); } else { #if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE UnityEngine.Debug.Log(e.StackTrace); #else Trace.WriteLine(e); #endif throw; } } } }
private static async Task DownloadHelperMultiple( DownloadableFile[] downloadableFiles, int retry = 1, System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null) { if (downloadableFiles == null || downloadableFiles.Length == 0) { return; } else if (downloadableFiles.Length == 1) { await DownloadHelper(downloadableFiles[0], retry, onDownloadProgressChanged); } else { DownloadableFile currentFile = downloadableFiles[0]; DownloadableFile[] remainingFiles = new DownloadableFile[downloadableFiles.Length - 1]; Array.Copy(downloadableFiles, 1, remainingFiles, 0, remainingFiles.Length); await DownloadHelper(currentFile, retry, onDownloadProgressChanged); await DownloadHelperMultiple(remainingFiles, retry, onDownloadProgressChanged); } }
private static void DownloadHelper( DownloadableFile downloadableFile, int retry = 1, System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null, System.ComponentModel.AsyncCompletedEventHandler onDownloadFileCompleted = null ) { if (downloadableFile.Url == null) { return; } //uncomment the following line to force re-download every time. //File.Delete(downloadableFile.LocalFile); if (!File.Exists(downloadableFile.LocalFile) || new FileInfo(downloadableFile.LocalFile).Length == 0) { try { //Download the file Trace.WriteLine("downloading file from:" + downloadableFile.Url + " to: " + downloadableFile.LocalFile); System.Net.WebClient downloadClient = new System.Net.WebClient(); if (onDownloadProgressChanged != null) { downloadClient.DownloadProgressChanged += onDownloadProgressChanged; } if (onDownloadFileCompleted != null) { downloadClient.DownloadFileCompleted += (sender, e) => { bool fileExist = File.Exists(downloadableFile.LocalFile); if (fileExist) { fileExist = new FileInfo(downloadableFile.LocalFile).Length > 0; } if (!fileExist) { e = new System.ComponentModel.AsyncCompletedEventArgs(new FileNotFoundException("Failed to download file"), e.Cancelled, e.UserState); } onDownloadFileCompleted(sender, e); } } ; downloadClient.DownloadFileAsync(new Uri(downloadableFile.Url), downloadableFile.LocalFile); } catch (Exception e) { if (File.Exists(downloadableFile.LocalFile)) { //The downloaded file may be corrupted, should delete it File.Delete(downloadableFile.LocalFile); } if (retry > 0) { DownloadHelper(downloadableFile, retry - 1); } else { #if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE UnityEngine.Debug.Log(e.StackTrace); #else Debug.WriteLine(e); #endif throw; } } } else { if (onDownloadFileCompleted != null) { onDownloadFileCompleted(null, null); } } }
/// <summary> /// Add a file to download /// </summary> /// <param name="downloadableFile">The file to be downloaded</param> public void AddFile(DownloadableFile downloadableFile) { _files.Add(downloadableFile); }
private static async Task DownloadHelper( DownloadableFile downloadableFile, int retry = 1, System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null ) { if (downloadableFile.Url == null) { return; } //uncomment the following line to force re-download every time. //File.Delete(downloadableFile.LocalFile); if (!downloadableFile.IsLocalFileValid) { try { if (File.Exists(downloadableFile.LocalFile)) { Trace.WriteLine($"Delete existing corrupted file :{downloadableFile.LocalFile}"); File.Delete(downloadableFile.LocalFile); } //Download the file Trace.WriteLine($"downloading file from:{downloadableFile.Url} to: {downloadableFile.LocalFile}"); FileInfo fi = new FileInfo(downloadableFile.LocalFile); if (!fi.Directory.Exists) { fi.Directory.Create(); } System.Net.WebClient downloadClient = new System.Net.WebClient(); if (onDownloadProgressChanged != null) { downloadClient.DownloadProgressChanged += onDownloadProgressChanged; } await downloadClient.DownloadFileTaskAsync(new Uri(downloadableFile.Url), downloadableFile.LocalFile); if ((!downloadableFile.IsLocalFileValid) && File.Exists(downloadableFile.LocalFile)) { Trace.WriteLine($"File on disk ({downloadableFile.LocalFile}) is corrupted, deleting file."); //The downloaded file may be corrupted, should delete it File.Delete(downloadableFile.LocalFile); } else { Trace.WriteLine($"File downloaded from: {downloadableFile.Url} to: {downloadableFile.LocalFile}"); } } catch (Exception e) { if ((!downloadableFile.IsLocalFileValid) && File.Exists(downloadableFile.LocalFile)) { Trace.WriteLine($"File on disk ({downloadableFile.LocalFile}) is corrupted, deleting file."); //The downloaded file may be corrupted, should delete it File.Delete(downloadableFile.LocalFile); } if (retry > 0) { await DownloadHelper(downloadableFile, retry - 1); } else { Trace.WriteLine(e); throw; } } } }