using Serilog; using SharpCompress; using SPTInstaller.Interfaces; using SPTInstaller.Models; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace SPTInstaller.Controllers; public class InstallController { public event EventHandler TaskChanged = delegate { }; private IPreCheck[] _preChecks { get; set; } private IProgressableTask[] _tasks { get; set; } public InstallController(IProgressableTask[] tasks, IPreCheck[] preChecks = null) { _tasks = tasks; _preChecks = preChecks; } public async Task RunPreChecks() { Log.Information("-<>--<>- Running PreChecks -<>--<>-"); var requiredResults = new List(); _preChecks.ForEach(x => x.IsPending = true); foreach (var check in _preChecks) { var result = await check.RunCheck(); Log.Information($"PreCheck: {check.Name} ({(check.IsRequired ? "Required" : "Optional")}) -> {(result.Succeeded ? "Passed" : "Failed")}\nDetail: {check.PreCheckDetails.ReplaceLineEndings(" ")}"); if (check.IsRequired) { requiredResults.Add(result); } } if (requiredResults.Any(result => !result.Succeeded)) { return Result.FromError("Some required checks have failed"); } return Result.FromSuccess(); } public async Task RunTasks() { Log.Information("-<>--<>- Running Installer Tasks -<>--<>-"); foreach (var task in _tasks) { TaskChanged?.Invoke(null, task); var result = await task.RunAsync(); if (!result.Succeeded) return result; } return Result.FromSuccess("Install Complete. Happy Playing!"); } }