/// <summary> /// Attempts to download a patchlist from a given url, then read as string and returned to caller. /// If download failed, will throw an exception. /// </summary> /// <param name="url"></param> /// <param name="attempts">Causes the download to be retried as much as the number allows, with exponential backoff algorithm.</param> /// <returns></returns> private async Task <string> DownloadPatchlist(string url, int attempts = 4) { if (attempts < 1) { attempts = 1; } for (int i = 0; i < attempts; i++) { await ExponentialBackoff(i, url); var client = new AquaClient(); var download = client.DownloadStringTaskAsync(url); Output.OnDownloadStart(url, client); try { var response = await download; if (string.IsNullOrEmpty(response)) { throw new Exception("Empty response!"); } Output.OnDownloadFinish(url); return(response); } catch (Exception Ex) { Output.AppendLog($"Download failed {url} because: {Ex.Message}"); } } Output.OnDownloadAborted(url); throw new Exception("Unable to download " + url); }
/// <summary> /// Attempts to download a patch into a target directory. /// </summary> /// <param name="target"></param> /// <param name="directory"></param> /// <param name="successLog">If provided, will write a line containing the patch file name then flush immediately into the log when a download is successful.</param> /// <param name="attempts">Causes the download to be retried as much as the number allows, with exponential backoff algorithm</param> /// <returns>True if download is successful, else false.</returns> public async Task <bool> DownloadGamePatch(PatchInfo target, string directory, StreamWriter successLog = null, int attempts = 4) { if (SkipList.Contains(target.File)) { Output.AppendLog($"Skipping download: {target.File}"); return(true); } if (attempts < 1) { attempts = 1; } var file = Path.Combine(directory, target.File); var folder = new FileInfo(file).DirectoryName; //Directory.CreateDirectory(folder); var baseUrl = target.Old ? OldPatchBaseUrl : PatchBaseUrl; var url = baseUrl + target.File + ".pat"; for (int i = 0; i < attempts; i++) { await ExponentialBackoff(i, url); var client = new AquaClient(); var download = client.DownloadFileTaskAsync(url, file); Output.AppendLog($"Downloading a file from {url} to {file}"); Output.OnDownloadStart(url, client); try { await download; if (new FileInfo(file).Length == 0) { throw new Exception("Empty response!"); } Output.OnDownloadFinish(url); if (successLog != null) { lock (UpdateManager.DownloadSuccessLogLock) { successLog.WriteLine(target.File); successLog.Flush(); } } return(true); } catch (Exception Ex) { Output.AppendLog($"Download failed {url} because: {Ex.Message}"); } } Output.OnDownloadAborted(url); return(false); //throw new Exception("Unable to download " + url); }