From 96b69641654a09c88622f472b2523defc80b9135 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Sat, 9 Jul 2022 13:14:03 -0400 Subject: [PATCH] ran code cleanup --- Aki.Core/Interfaces/ILiveTaskTableEntry.cs | 4 - Aki.Core/Interfaces/IProgressableTask.cs | 8 +- Aki.Core/InternalData.cs | 2 +- Aki.Core/Model/LiveTableTask.cs | 1 - Aki.Core/Model/LiveTableTaskRunner.cs | 6 +- Aki.Core/SPTInstaller.cs | 6 +- Aki.Core/Tasks/DownloadTask.cs | 14 ++-- Aki.Core/Tasks/IntializationTask.cs | 1 - Aki.Core/Tasks/ReleaseCheckTask.cs | 2 +- Aki.Core/Tasks/SetupClientTask.cs | 8 +- Aki.Helper/DownloadHelper.cs | 10 +-- Aki.Helper/FileHelper.cs | 70 +---------------- Aki.Helper/HttpClientProgressExtensions.cs | 90 +++++++++++----------- Aki.Helper/PreCheckHelper.cs | 31 +------- Aki.Helper/ProcessHelper.cs | 6 +- Aki.Helper/ZipHelper.cs | 6 +- 16 files changed, 82 insertions(+), 183 deletions(-) diff --git a/Aki.Core/Interfaces/ILiveTaskTableEntry.cs b/Aki.Core/Interfaces/ILiveTaskTableEntry.cs index 5fcbc04..b334661 100644 --- a/Aki.Core/Interfaces/ILiveTaskTableEntry.cs +++ b/Aki.Core/Interfaces/ILiveTaskTableEntry.cs @@ -1,9 +1,5 @@ using Spectre.Console; using SPT_AKI_Installer.Aki.Core.Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace SPT_AKI_Installer.Aki.Core.Interfaces diff --git a/Aki.Core/Interfaces/IProgressableTask.cs b/Aki.Core/Interfaces/IProgressableTask.cs index a2f56f3..7237c7a 100644 --- a/Aki.Core/Interfaces/IProgressableTask.cs +++ b/Aki.Core/Interfaces/IProgressableTask.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SPT_AKI_Installer.Aki.Core.Interfaces +namespace SPT_AKI_Installer.Aki.Core.Interfaces { internal interface IProgressableTask { diff --git a/Aki.Core/InternalData.cs b/Aki.Core/InternalData.cs index 1c31cbb..690d20a 100644 --- a/Aki.Core/InternalData.cs +++ b/Aki.Core/InternalData.cs @@ -19,7 +19,7 @@ namespace SPT_AKI_Installer.Aki.Core /// The original EFT game version /// public string OriginalGameVersion { get; set; } - + /// /// Patcher zip file info /// diff --git a/Aki.Core/Model/LiveTableTask.cs b/Aki.Core/Model/LiveTableTask.cs index 34f9677..df90b56 100644 --- a/Aki.Core/Model/LiveTableTask.cs +++ b/Aki.Core/Model/LiveTableTask.cs @@ -1,7 +1,6 @@ using Spectre.Console; using SPT_AKI_Installer.Aki.Core.Interfaces; using System; -using System.Text; using System.Threading; using System.Threading.Tasks; diff --git a/Aki.Core/Model/LiveTableTaskRunner.cs b/Aki.Core/Model/LiveTableTaskRunner.cs index bf1c62b..be8ee98 100644 --- a/Aki.Core/Model/LiveTableTaskRunner.cs +++ b/Aki.Core/Model/LiveTableTaskRunner.cs @@ -62,11 +62,11 @@ namespace SPT_AKI_Installer.Aki.Core.Model var result = await RunAllTasksAsync(tasks, context, table); // if a task failed and returned early, set any remaining task status to cancelled - if(!result.Item1) + if (!result.Item1) { - var cancelledTasks = tasks.Take(new Range(tasks.IndexOf(result.Item2)+1, tasks.Count)); + var cancelledTasks = tasks.Take(new Range(tasks.IndexOf(result.Item2) + 1, tasks.Count)); - foreach(var task in cancelledTasks) + foreach (var task in cancelledTasks) { task.SetStatus("[grey]Cancelled[/]"); } diff --git a/Aki.Core/SPTInstaller.cs b/Aki.Core/SPTInstaller.cs index b80e362..6dfa6d8 100644 --- a/Aki.Core/SPTInstaller.cs +++ b/Aki.Core/SPTInstaller.cs @@ -1,12 +1,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using System; -using System.Threading.Tasks; using Spectre.Console; -using System.IO; using SPT_AKI_Installer.Aki.Core.Model; using SPT_AKI_Installer.Aki.Core.Tasks; +using System; using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; namespace SPT_AKI_Installer.Aki.Core { diff --git a/Aki.Core/Tasks/DownloadTask.cs b/Aki.Core/Tasks/DownloadTask.cs index e14f5f2..7b9e52d 100644 --- a/Aki.Core/Tasks/DownloadTask.cs +++ b/Aki.Core/Tasks/DownloadTask.cs @@ -1,11 +1,11 @@ -using Newtonsoft.Json; +using CG.Web.MegaApiClient; +using Newtonsoft.Json; using SPT_AKI_Installer.Aki.Core.Model; using SPT_AKI_Installer.Aki.Helper; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; -using CG.Web.MegaApiClient; namespace SPT_AKI_Installer.Aki.Core.Tasks { @@ -70,7 +70,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks return GenericResult.FromSuccess(); } - catch(Exception) + catch (Exception) { //most likely a 509 (Bandwidth limit exceeded) due to mega's user quotas. continue; @@ -79,7 +79,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks var result = await DownloadHelper.DownloadFile(_data.PatcherZipInfo, mirror, progress); - if(result.Succeeded) + if (result.Succeeded) { return GenericResult.FromSuccess(); } @@ -91,7 +91,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks public override async Task RunAsync() { _data.PatcherZipInfo = new FileInfo(Path.Join(_data.TargetInstallPath, "patcher.zip")); - _data.AkiZipInfo= new FileInfo(Path.Join(_data.TargetInstallPath, "sptaki.zip")); + _data.AkiZipInfo = new FileInfo(Path.Join(_data.TargetInstallPath, "sptaki.zip")); if (_data.PatchNeeded) { @@ -103,7 +103,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks { return buildResult; } - + Progress = 0; var progress = new Progress((d) => { Progress = (int)Math.Floor(d); }); @@ -120,7 +120,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks SetStatus("Downloading SPT-AKI", false); Progress = 0; - + var akiProgress = new Progress((d) => { Progress = (int)Math.Floor(d); }); var releaseDownloadResult = await DownloadHelper.DownloadFile(_data.AkiZipInfo, _data.AkiReleaseDownloadLink, akiProgress); diff --git a/Aki.Core/Tasks/IntializationTask.cs b/Aki.Core/Tasks/IntializationTask.cs index 0123545..7ba3a52 100644 --- a/Aki.Core/Tasks/IntializationTask.cs +++ b/Aki.Core/Tasks/IntializationTask.cs @@ -1,6 +1,5 @@ using SPT_AKI_Installer.Aki.Core.Model; using SPT_AKI_Installer.Aki.Helper; -using System; using System.IO; using System.Threading.Tasks; diff --git a/Aki.Core/Tasks/ReleaseCheckTask.cs b/Aki.Core/Tasks/ReleaseCheckTask.cs index 69762ce..865fd7d 100644 --- a/Aki.Core/Tasks/ReleaseCheckTask.cs +++ b/Aki.Core/Tasks/ReleaseCheckTask.cs @@ -67,7 +67,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks string status = $"Current Release: {latestAkiVersion}"; - if(_data.PatchNeeded) + if (_data.PatchNeeded) { status += " - Patch Available"; } diff --git a/Aki.Core/Tasks/SetupClientTask.cs b/Aki.Core/Tasks/SetupClientTask.cs index 6d76b42..15aedc4 100644 --- a/Aki.Core/Tasks/SetupClientTask.cs +++ b/Aki.Core/Tasks/SetupClientTask.cs @@ -27,7 +27,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks var extractPatcherResult = ZipHelper.Decompress(_data.PatcherZipInfo, patcherOutputDir, extractPatcherProgress); - if(!extractPatcherResult.Succeeded) + if (!extractPatcherResult.Succeeded) { return extractPatcherResult; } @@ -42,7 +42,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks var copyPatcherResult = FileHelper.CopyDirectoryWithProgress(patcherDirInfo, targetInstallDirInfo, copyPatcherProgress); - if(!copyPatcherResult.Succeeded) + if (!copyPatcherResult.Succeeded) { return copyPatcherResult; } @@ -55,7 +55,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks var patchingResult = ProcessHelper.PatchClientFiles(patcherEXE, targetInstallDirInfo); - if(!patchingResult.Succeeded) + if (!patchingResult.Succeeded) { return patchingResult; } @@ -68,7 +68,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks var extractReleaseResult = ZipHelper.Decompress(_data.AkiZipInfo, targetInstallDirInfo, extractReleaseProgress); - if(!extractReleaseResult.Succeeded) + if (!extractReleaseResult.Succeeded) { return extractPatcherResult; } diff --git a/Aki.Helper/DownloadHelper.cs b/Aki.Helper/DownloadHelper.cs index a583824..ff199dd 100644 --- a/Aki.Helper/DownloadHelper.cs +++ b/Aki.Helper/DownloadHelper.cs @@ -1,9 +1,9 @@ -using System; +using HttpClientProgress; +using SPT_AKI_Installer.Aki.Core.Model; +using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; -using HttpClientProgress; -using SPT_AKI_Installer.Aki.Core.Model; namespace SPT_AKI_Installer.Aki.Helper { @@ -25,14 +25,14 @@ namespace SPT_AKI_Installer.Aki.Helper outputFile.Refresh(); - if(!outputFile.Exists) + if (!outputFile.Exists) { return GenericResult.FromError($"Failed to download {outputFile.Name}"); } return GenericResult.FromSuccess(); } - catch(Exception ex) + catch (Exception ex) { return GenericResult.FromError(ex.Message); } diff --git a/Aki.Helper/FileHelper.cs b/Aki.Helper/FileHelper.cs index 423decc..9bcdb44 100644 --- a/Aki.Helper/FileHelper.cs +++ b/Aki.Helper/FileHelper.cs @@ -1,7 +1,6 @@ -using System; +using SPT_AKI_Installer.Aki.Core.Model; +using System; using System.IO; -using Spectre.Console; -using SPT_AKI_Installer.Aki.Core.Model; namespace SPT_AKI_Installer.Aki.Helper { @@ -29,73 +28,10 @@ namespace SPT_AKI_Installer.Aki.Helper return GenericResult.FromSuccess(); } - catch(Exception ex) + catch (Exception ex) { return GenericResult.FromError(ex.Message); } } - - public static void DeleteFiles(string filePath, bool allFolders = false) - { - if (File.Exists(filePath) || Directory.Exists(filePath)) - { - if (filePath.Contains('.')) - { - File.Delete(filePath); - } - else - { - Directory.Delete(filePath, allFolders); - } - } - } - - public static string FindFile(string path, string name) - { - string[] filePaths = Directory.GetFiles(path); - foreach (string file in filePaths) - { - if (file.Contains(name, StringComparison.OrdinalIgnoreCase)) - { - return file; - } - } - return null; - } - - public static string FindFile(string path, string name, string altName) - { - string[] filePaths = Directory.GetFiles(path); - foreach (string file in filePaths) - { - if (file.Contains(name, StringComparison.OrdinalIgnoreCase) && - file.Contains(altName, StringComparison.OrdinalIgnoreCase)) - { - return file; - } - } - return null; - } - - public static bool FindFolder(string patchRef, string targetPath, out DirectoryInfo dir) - { - var dirInfo = new DirectoryInfo(targetPath).GetDirectories(); - string patchInner = null; - foreach (var file in dirInfo) - { - if (file.FullName.Contains("patcher", StringComparison.OrdinalIgnoreCase)) - { - patchInner = file.FullName; - } - } - var path = new DirectoryInfo(patchInner); - if (path.Exists) - { - dir = path; - return true; - } - dir = null; - return false; - } } } diff --git a/Aki.Helper/HttpClientProgressExtensions.cs b/Aki.Helper/HttpClientProgressExtensions.cs index 2c8d58e..3568717 100644 --- a/Aki.Helper/HttpClientProgressExtensions.cs +++ b/Aki.Helper/HttpClientProgressExtensions.cs @@ -6,52 +6,52 @@ using System.Threading.Tasks; namespace HttpClientProgress { - public static class HttpClientProgressExtensions - { - public static async Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, IProgress progress = null, CancellationToken cancellationToken = default(CancellationToken)) - { - using (var response = await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead)) - { - var contentLength = response.Content.Headers.ContentLength; - using (var download = await response.Content.ReadAsStreamAsync()) - { - // no progress... no contentLength... very sad - if (progress is null || !contentLength.HasValue) - { - await download.CopyToAsync(destination); - return; - } - // Such progress and contentLength much reporting Wow! - var progressWrapper = new Progress(totalBytes => progress.Report(GetProgressPercentage(totalBytes, contentLength.Value))); - await download.CopyToAsync(destination, 81920, progressWrapper, cancellationToken); - } - } + public static class HttpClientProgressExtensions + { + public static async Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, IProgress progress = null, CancellationToken cancellationToken = default(CancellationToken)) + { + using (var response = await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead)) + { + var contentLength = response.Content.Headers.ContentLength; + using (var download = await response.Content.ReadAsStreamAsync()) + { + // no progress... no contentLength... very sad + if (progress is null || !contentLength.HasValue) + { + await download.CopyToAsync(destination); + return; + } + // Such progress and contentLength much reporting Wow! + var progressWrapper = new Progress(totalBytes => progress.Report(GetProgressPercentage(totalBytes, contentLength.Value))); + await download.CopyToAsync(destination, 81920, progressWrapper, cancellationToken); + } + } - float GetProgressPercentage(float totalBytes, float currentBytes) => (totalBytes / currentBytes) * 100f; - } + float GetProgressPercentage(float totalBytes, float currentBytes) => (totalBytes / currentBytes) * 100f; + } - static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress progress = null, CancellationToken cancellationToken = default(CancellationToken)) - { - if (bufferSize < 0) - throw new ArgumentOutOfRangeException(nameof(bufferSize)); - if (source is null) - throw new ArgumentNullException(nameof(source)); - if (!source.CanRead) - throw new InvalidOperationException($"'{nameof(source)}' is not readable."); - if (destination == null) - throw new ArgumentNullException(nameof(destination)); - if (!destination.CanWrite) - throw new InvalidOperationException($"'{nameof(destination)}' is not writable."); + static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress progress = null, CancellationToken cancellationToken = default(CancellationToken)) + { + if (bufferSize < 0) + throw new ArgumentOutOfRangeException(nameof(bufferSize)); + if (source is null) + throw new ArgumentNullException(nameof(source)); + if (!source.CanRead) + throw new InvalidOperationException($"'{nameof(source)}' is not readable."); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (!destination.CanWrite) + throw new InvalidOperationException($"'{nameof(destination)}' is not writable."); - var buffer = new byte[bufferSize]; - long totalBytesRead = 0; - int bytesRead; - while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) - { - await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); - totalBytesRead += bytesRead; - progress?.Report(totalBytesRead); - } - } - } + var buffer = new byte[bufferSize]; + long totalBytesRead = 0; + int bytesRead; + while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) + { + await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); + totalBytesRead += bytesRead; + progress?.Report(totalBytesRead); + } + } + } } \ No newline at end of file diff --git a/Aki.Helper/PreCheckHelper.cs b/Aki.Helper/PreCheckHelper.cs index a12da89..6919988 100644 --- a/Aki.Helper/PreCheckHelper.cs +++ b/Aki.Helper/PreCheckHelper.cs @@ -1,7 +1,7 @@ using Microsoft.Win32; -using System.Runtime.InteropServices; -using System.IO; using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; namespace SPT_AKI_Installer.Aki.Helper { @@ -18,7 +18,7 @@ namespace SPT_AKI_Installer.Aki.Helper var uninstallStringValue = Registry.LocalMachine.OpenSubKey(registryInstall, false) ?.GetValue("UninstallString"); var info = (uninstallStringValue is string key) ? new FileInfo(key) : null; - + return info?.DirectoryName; } @@ -26,30 +26,5 @@ namespace SPT_AKI_Installer.Aki.Helper { return FileVersionInfo.GetVersionInfo(Path.Join(gamePath + "/EscapeFromTarkov.exe")).ProductVersion.Replace('-', '.').Split('.')[^2]; } - - //public static string GetPatcherZipPath(string gameVersion, string targetPath) - //{ - // // example patch name - Patcher.12.12.15.17861.to.12.12.15.17349.zip - // var patchZip = FileHelper.FindFile(targetPath, gameVersion, "Patcher"); - // if (patchZip == null) - // { - // patchZip = FileHelper.FindFile(targetPath, "PATCHERZIP"); - // } - - // return patchZip; - //} - - //public static string GetAkiZipPath(string targetPath) - //{ - // // example aki name - RELEASE-SPT-2.3.1-17349.zip - // var akiZip = FileHelper.FindFile(targetPath, "SPT", "RELEASE"); - - // if (akiZip == null) - // { - // akiZip = FileHelper.FindFile(targetPath, "AKIZIP"); - // } - - // return akiZip; - //} } } diff --git a/Aki.Helper/ProcessHelper.cs b/Aki.Helper/ProcessHelper.cs index e859e42..8dd4feb 100644 --- a/Aki.Helper/ProcessHelper.cs +++ b/Aki.Helper/ProcessHelper.cs @@ -1,6 +1,6 @@ -using System.Diagnostics; +using SPT_AKI_Installer.Aki.Core.Model; +using System.Diagnostics; using System.IO; -using SPT_AKI_Installer.Aki.Core.Model; namespace SPT_AKI_Installer.Aki.Helper { @@ -19,7 +19,7 @@ namespace SPT_AKI_Installer.Aki.Helper { public static GenericResult PatchClientFiles(FileInfo executable, DirectoryInfo workingDir) { - if(!executable.Exists || !workingDir.Exists) + if (!executable.Exists || !workingDir.Exists) { return GenericResult.FromError($"Could not find executable ({executable.Name}) or working directory ({workingDir.Name})"); } diff --git a/Aki.Helper/ZipHelper.cs b/Aki.Helper/ZipHelper.cs index 84a0490..aabcf52 100644 --- a/Aki.Helper/ZipHelper.cs +++ b/Aki.Helper/ZipHelper.cs @@ -10,7 +10,7 @@ namespace SPT_AKI_Installer.Aki.Helper { public static class ZipHelper { - public static GenericResult Decompress(FileInfo ArchivePath, DirectoryInfo OutputFolderPath, IProgress progress = null) + public static GenericResult Decompress(FileInfo ArchivePath, DirectoryInfo OutputFolderPath, IProgress progress = null) { try { @@ -40,14 +40,14 @@ namespace SPT_AKI_Installer.Aki.Helper OutputFolderPath.Refresh(); - if(!OutputFolderPath.Exists) + if (!OutputFolderPath.Exists) { return GenericResult.FromError($"Failed to extract files: {ArchivePath.Name}"); } return GenericResult.FromSuccess(); } - catch(Exception ex) + catch (Exception ex) { return GenericResult.FromError(ex.Message); }