reworked program startup and settings. client seletions is WIP

This commit is contained in:
IsWaffle 2022-05-17 21:51:02 -04:00
parent d36d925d33
commit b243a150f5
14 changed files with 393 additions and 121 deletions

View File

@ -1,4 +1,5 @@
using EftPatchHelper.Helpers;
using EftPatchHelper.Model;
using Spectre.Console;
namespace EftPatchHelper.EftInfo

View File

@ -8,7 +8,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.43.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Spectre.Console" Version="0.44.0" />
</ItemGroup>
<ItemGroup>
<None Update="settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,21 @@
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EftPatchHelper.Extensions
{
public static class BoolExtensions
{
public static void ValidateOrExit(this bool toValidate)
{
if(!toValidate)
{
AnsiConsole.Prompt(new TextPrompt<string>("Press [blue]enter[/] to close ...").AllowEmpty());
Environment.Exit(0);
}
}
}
}

View File

@ -1,17 +1,24 @@
using EftPatchHelper.EftInfo;
using EftPatchHelper.Model;
using Spectre.Console;
using System.Diagnostics;
namespace EftPatchHelper.Helpers
{
public static class EftClientSelector
public class EftClientSelector
{
private static List<EftClient> clientList = new List<EftClient>();
private List<EftClient> _clientList = new List<EftClient>();
private Settings _settings;
public static string? GetLiveVersion(Settings settings)
public EftClientSelector(Settings settings)
{
_settings = settings;
}
public string? GetLiveVersion()
{
// Get eft live version
string eftVersion = FileVersionInfo.GetVersionInfo(Path.Join(settings.LiveEftPath, "EscapeFromTarkov.exe")).ProductVersion?.Replace('-', '.');
string eftVersion = FileVersionInfo.GetVersionInfo(Path.Join(_settings.LiveEftPath, "EscapeFromTarkov.exe")).ProductVersion?.Replace('-', '.');
//remove leading 0 from version number
if (eftVersion != null && eftVersion.StartsWith("0."))
@ -24,32 +31,32 @@ namespace EftPatchHelper.Helpers
return string.Join('.', fixedVersion);
}
public static EftClient GetClient(string Version)
public EftClient GetClient(string Version)
{
return clientList.Where(x => x.Version == Version).FirstOrDefault();
return _clientList.Where(x => x.Version == Version).FirstOrDefault();
}
public static void LoadClientList(Settings settings)
public void LoadClientList()
{
clientList.Clear();
_clientList.Clear();
string? eftVersion = GetLiveVersion(settings);
string? eftVersion = GetLiveVersion();
if (eftVersion != null)
{
// add eft live version to version options
clientList.Add(new EftClient()
_clientList.Add(new EftClient()
{
FolderPath = settings.LiveEftPath,
FolderPath = _settings.LiveEftPath,
Version = eftVersion,
PrepPath = Path.Join(settings.PrepFolderPath, eftVersion),
PrepPath = Path.Join(_settings.PrepFolderPath, eftVersion),
Location = EftClientLocation.Live
});
}
// add backup folders to version options
foreach (string backup in Directory.GetDirectories(settings.BackupFolderPath))
foreach (string backup in Directory.GetDirectories(_settings.BackupFolderPath))
{
DirectoryInfo backupDir = new DirectoryInfo(backup);
@ -58,17 +65,17 @@ namespace EftPatchHelper.Helpers
continue;
}
clientList.Add(new EftClient()
_clientList.Add(new EftClient()
{
FolderPath = backupDir.FullName,
Version = backupDir.Name,
PrepPath = Path.Join(settings.PrepFolderPath, backupDir.Name),
PrepPath = Path.Join(_settings.PrepFolderPath, backupDir.Name),
Location = EftClientLocation.Backup
});
}
}
public static EftClient GetClientSelection(string Prompt)
public EftClient GetClientSelection(string Prompt)
{
SelectionPrompt<EftClient> clientPrompt = new SelectionPrompt<EftClient>()
{
@ -78,7 +85,7 @@ namespace EftPatchHelper.Helpers
Converter = (x) => x.DisplayName
};
clientPrompt.AddChoices(clientList);
clientPrompt.AddChoices(_clientList);
return clientPrompt.Show(AnsiConsole.Console);
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EftPatchHelper.Interfaces
{
public interface IClientSelectionTask : ITaskable
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EftPatchHelper.Interfaces
{
public interface ISettingsTask : ITaskable
{
}
}

View File

@ -0,0 +1,13 @@
using EftPatchHelper.Model;
namespace EftPatchHelper.Interfaces
{
public interface ITaskable
{
/// <summary>
/// Runs a predefined task
/// </summary>
/// <returns>Returns true if the task succeeded, otherwise false</returns>
public bool Run();
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EftPatchHelper.Model
{
public class Options
{
public bool PromptToOverwriteDirectories = true;
}
}

View File

@ -0,0 +1,12 @@
namespace EftPatchHelper.Model
{
public enum PatcherExitCode
{
ProgramClosed = 0,
Success = 10,
EftExeNotFound = 11,
NoPatchFolder = 12,
MissingFile = 13,
MissingDir = 14
}
}

View File

@ -1,29 +1,32 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace EftPatchHelper
namespace EftPatchHelper.Model
{
public class Settings
{
[JsonIgnore]
public static string settingsFile = Path.Join(Directory.GetCurrentDirectory(), "settings.json");
[JsonPropertyName("target_eft_version")]
[JsonPropertyName("targetEftVersion")]
public string TargetEftVersion { get; set; } = "";
[JsonPropertyName("prep_folder_path")]
[JsonPropertyName("prepFolderPath")]
public string PrepFolderPath { get; set; } = "";
[JsonPropertyName("backup_folder_path")]
[JsonPropertyName("backupFolderPath")]
public string BackupFolderPath { get; set; } = "";
[JsonPropertyName("live_eft_path")]
[JsonPropertyName("liveEftPath")]
public string LiveEftPath { get; set; } = "";
[JsonPropertyName("auto_zip")]
[JsonPropertyName("autoZip")]
public bool AutoZip { get; set; } = true;
[JsonPropertyName("patcher_exe_path")]
[JsonPropertyName("autoClose")]
public bool AutoClose { get; set; } = false;
[JsonPropertyName("patcherExePath")]
public string PatcherEXEPath { get; set; } = "";
public void Save()
@ -35,14 +38,14 @@ namespace EftPatchHelper
File.WriteAllText(settingsFile, json);
}
public static Settings? Load()
{
if (!File.Exists(settingsFile)) return null;
//public static Settings? Load()
//{
// if (!File.Exists(settingsFile)) return null;
string json = File.ReadAllText(settingsFile);
// string json = File.ReadAllText(settingsFile);
return JsonSerializer.Deserialize<Settings>(json);
}
// return JsonSerializer.Deserialize<Settings>(json);
//}
public bool Validate()
{

View File

@ -1,117 +1,175 @@
// See https://aka.ms/new-console-template for more information
using EftPatchHelper;
using EftPatchHelper.EftInfo;
using EftPatchHelper.Extensions;
using EftPatchHelper.Helpers;
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
using EftPatchHelper.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Spectre.Console;
using System.Diagnostics;
Settings? settings = Settings.Load();
// check settings file exists
if(settings == null)
namespace EftPatchHelper
{
settings = new Settings();
settings.Save();
AnsiConsole.MarkupLine($"Settings file was create here: \n[blue]{Settings.settingsFile}[/]\n\nPlease update it and try again.");
AnsiConsole.MarkupLine("Press [blue]Enter[/] to close ...");
Console.ReadLine();
return;
}
// validate settings
if(!settings.Validate())
public class Program
{
AnsiConsole.MarkupLine($"[red]Settings file seems to be missing some information, please fix it[/]\n\nPath to file:\n[blue]{Settings.settingsFile}[/]\n\n");
AnsiConsole.MarkupLine("Press [blue]Enter[/] to close ...");
Console.ReadLine();
return;
}
ITaskable _settingsTasks;
ITaskable _clientSelectionTasks;
/// Fancy
public static void Main(string[] args)
{
// Fancy
AnsiConsole.Write(new FigletText("EFT Patch Helper").Centered().Color(Color.Blue));
// show some settings information
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine($"Current target version is [purple]{settings.TargetEftVersion}[/]");
AnsiConsole.MarkupLine($"Prep folder path is [purple]{settings.PrepFolderPath}[/]");
AnsiConsole.MarkupLine($"Backup folder path is [purple]{settings.BackupFolderPath}[/]");
AnsiConsole.WriteLine();
EftClientSelector.LoadClientList(settings);
EftClient targetClient = EftClientSelector.GetClient(settings.TargetEftVersion);
EftClient sourceClient;
AnsiConsole.WriteLine();
bool promptToOverwrite = new ConfirmationPrompt("Prompt to overwrite directories?").Show(AnsiConsole.Console);
AnsiConsole.WriteLine();
ConfirmationPrompt confirmTarget = new ConfirmationPrompt($"Use version [purple]{settings.TargetEftVersion}[/] as target?");
if (!confirmTarget.Show(AnsiConsole.Console) || targetClient == null)
{
targetClient = EftClientSelector.GetClientSelection("Select [yellow]Target[/] Version");
AnsiConsole.WriteLine();
ConfirmationPrompt changeVersion = new ConfirmationPrompt($"Update settings target version to use [purple]{targetClient.Version}[/]?");
if(changeVersion.Show(AnsiConsole.Console))
{
settings.TargetEftVersion = targetClient.Version;
settings.Save();
}
var host = ConfigureHost(args);
host.Services.GetRequiredService<Program>().Run();
}
sourceClient = EftClientSelector.GetClientSelection("Select [blue]Source[/] Version");
//backup data if needed
targetClient.Backup(settings, !promptToOverwrite);
sourceClient.Backup(settings, !promptToOverwrite);
//copy source to prep directory
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[gray]Copying[/] [blue]source[/][gray] to prep area ...[/]");
FolderCopy sourceCopy = new FolderCopy(sourceClient.FolderPath, sourceClient.PrepPath);
sourceCopy.Start(!promptToOverwrite);
//copy target to prep directory
AnsiConsole.MarkupLine("[gray]Copying[/] [blue]target[/][gray] to prep area ...[/]");
FolderCopy targetCopy = new FolderCopy(targetClient.FolderPath, targetClient.PrepPath);
targetCopy.Start(!promptToOverwrite);
// clean prep source and target folders of uneeded data
FolderCleaner.Clean(sourceClient.PrepPath);
FolderCleaner.Clean(targetClient.PrepPath);
// start patcher
if(File.Exists(settings.PatcherEXEPath))
public Program(
ISettingsTask settingsTasks,
IClientSelectionTask clientSelectionTasks
)
{
string patcherOutputName = $"Patcher_{sourceClient.Version}_to_{targetClient.Version}";
_settingsTasks = settingsTasks;
_clientSelectionTasks = clientSelectionTasks;
}
AnsiConsole.Markup("Starting patcher ... ");
Process.Start(new ProcessStartInfo()
public void Run()
{
FileName = settings.PatcherEXEPath,
WorkingDirectory = new FileInfo(settings.PatcherEXEPath).Directory?.FullName ?? Directory.GetCurrentDirectory(),
ArgumentList = {$"OutputFolderName::{patcherOutputName}", $"SourceFolderPath::{sourceClient.PrepPath}", $"TargetFolderPath::{targetClient.PrepPath}", $"AutoZip::{settings.AutoZip}"}
_settingsTasks.Run().ValidateOrExit();
_clientSelectionTasks.Run().ValidateOrExit();
}
private static IHost ConfigureHost(string[] args)
{
return Host.CreateDefaultBuilder(args).ConfigureServices((_, services) =>
{
services.AddSingleton<Options>();
services.AddSingleton<Settings>(serviceProvider =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var settings = configuration.Get<Settings>();
if (settings == null) throw new Exception("Failed to retrieve settings");
return settings;
});
services.AddScoped<EftClientSelector>();
services.AddTransient<ISettingsTask, StartupSettingsTask>();
services.AddTransient<IClientSelectionTask, ClientSelectionTask>();
services.AddTransient<Program>();
})
.ConfigureAppConfiguration((_, config) =>
{
config.AddJsonFile(Settings.settingsFile, optional: true, reloadOnChange: true);
})
.Build();
}
}
}
AnsiConsole.MarkupLine("[green]done[/]");
//EftClientSelector.LoadClientList(settings);
AnsiConsole.WriteLine();
//EftClient targetClient = EftClientSelector.GetClient(settings.TargetEftVersion);
//EftClient sourceClient;
// done
AnsiConsole.MarkupLine("Press [blue]Enter[/] to close ...");
//AnsiConsole.WriteLine();
//ConfirmationPrompt confirmTarget = new ConfirmationPrompt($"Use version [purple]{_settings.TargetEftVersion}[/] as target?");
Console.ReadLine();
//if (!confirmTarget.Show(AnsiConsole.Console) || targetClient == null)
//{
// targetClient = EftClientSelector.GetClientSelection("Select [yellow]Target[/] Version");
// AnsiConsole.WriteLine();
// ConfirmationPrompt changeVersion = new ConfirmationPrompt($"Update settings target version to use [purple]{targetClient.Version}[/]?");
// if (changeVersion.Show(AnsiConsole.Console))
// {
// settings.TargetEftVersion = targetClient.Version;
// settings.Save();
// }
//}
//sourceClient = EftClientSelector.GetClientSelection("Select [blue]Source[/] Version");
////backup data if needed
//targetClient.Backup(settings, !promptToOverwrite);
//sourceClient.Backup(settings, !promptToOverwrite);
////copy source to prep directory
//AnsiConsole.WriteLine();
//AnsiConsole.MarkupLine("[gray]Copying[/] [blue]source[/][gray] to prep area ...[/]");
//FolderCopy sourceCopy = new FolderCopy(sourceClient.FolderPath, sourceClient.PrepPath);
//sourceCopy.Start(!promptToOverwrite);
////copy target to prep directory
//AnsiConsole.MarkupLine("[gray]Copying[/] [blue]target[/][gray] to prep area ...[/]");
//FolderCopy targetCopy = new FolderCopy(targetClient.FolderPath, targetClient.PrepPath);
//targetCopy.Start(!promptToOverwrite);
//// clean prep source and target folders of uneeded data
//FolderCleaner.Clean(sourceClient.PrepPath);
//FolderCleaner.Clean(targetClient.PrepPath);
//// start patcher
//if(File.Exists(settings.PatcherEXEPath))
//{
// string patcherOutputName = $"Patcher_{sourceClient.Version}_to_{targetClient.Version}";
// AnsiConsole.Markup("Starting patcher ... ");
// var genProc = Process.Start(new ProcessStartInfo()
// {
// FileName = settings.PatcherEXEPath,
// WorkingDirectory = new FileInfo(settings.PatcherEXEPath).Directory?.FullName ?? Directory.GetCurrentDirectory(),
// ArgumentList =
// {
// $"OutputFolderName::{patcherOutputName}",
// $"SourceFolderPath::{sourceClient.PrepPath}",
// $"TargetFolderPath::{targetClient.PrepPath}",
// $"AutoZip::{settings.AutoZip}",
// $"AutoClose::{settings.AutoClose}"
// }
// });
// genProc?.WaitForExit();
// switch((PatcherExitCode)genProc.ExitCode)
// {
// case PatcherExitCode.ProgramClosed:
// {
// break;
// }
// case PatcherExitCode.Success:
// {
// break;
// }
// case PatcherExitCode.MissingDir:
// {
// break;
// }
// default:
// {
// break;
// }
// }
//}
//AnsiConsole.MarkupLine("[green]done[/]");
//AnsiConsole.WriteLine();
//// done
//AnsiConsole.MarkupLine("Press [blue]Enter[/] to close ...");
//Console.ReadLine();

View File

@ -0,0 +1,25 @@
using EftPatchHelper.Helpers;
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
namespace EftPatchHelper.Tasks
{
public class ClientSelectionTask : IClientSelectionTask
{
private Settings _settings;
private Options _options;
private EftClientSelector _clientSelector;
public ClientSelectionTask(Settings settings, Options options, EftClientSelector clientSelector)
{
_settings = settings;
_options = options;
_clientSelector = clientSelector;
}
public bool Run()
{
}
}
}

View File

@ -0,0 +1,77 @@
using EftPatchHelper.Extensions;
using EftPatchHelper.Interfaces;
using EftPatchHelper.Model;
using Spectre.Console;
namespace EftPatchHelper.Tasks
{
public class StartupSettingsTask : ISettingsTask
{
private Settings? _settings { get; set; }
private Options _options { get; set; }
public StartupSettingsTask(Settings? settings, Options options)
{
_settings = settings;
_options = options;
}
public void PrintSummary()
{
AnsiConsole.WriteLine();
// show some settings information
Table settingsTable = new Table()
.Alignment(Justify.Center)
.HorizontalBorder()
.HideHeaders()
.BorderStyle(Style.Parse("blue"))
.AddColumn("Data")
.AddColumn("Value")
.AddRow("Current target version", $"[purple]{_settings?.TargetEftVersion}[/]")
.AddRow("Prep folder path", $"[purple]{_settings?.PrepFolderPath}[/]")
.AddRow("Backup folder path", $"[purple]{_settings?.BackupFolderPath}[/]");
AnsiConsole.Write(settingsTable);
AnsiConsole.WriteLine();
}
private bool ValidateSettings()
{
// check settings file exists
if (_settings == null)
{
_settings = new Settings();
_settings.Save();
AnsiConsole.MarkupLine($"Settings file was create here: \n[blue]{Settings.settingsFile}[/]\n\nPlease update it and try again.");
return false;
}
// validate settings
if (!_settings.Validate())
{
AnsiConsole.MarkupLine($"[red]Settings file seems to be missing some information, please fix it[/]\n\nPath to file:\n[blue]{Settings.settingsFile}[/]\n\n");
return false;
}
return true;
}
private void ConfirmOptions()
{
_options.PromptToOverwriteDirectories = new ConfirmationPrompt("Prompt to overwrite directories?").Show(AnsiConsole.Console);
}
public bool Run()
{
ValidateSettings().ValidateOrExit();
ConfirmOptions();
PrintSummary();
return true;
}
}
}

View File

@ -0,0 +1,9 @@
{
"targetEftVersion": "C:\\users\\johno\\desktop\\tmp",
"prepFolderPath": "C:\\users\\johno\\desktop\\tmp",
"backupFolderPath": "C:\\users\\johno\\desktop\\tmp",
"liveEftPath": "C:\\users\\johno\\desktop\\tmp",
"patcherExePath": "C:\\users\\johno\\desktop\\tmp",
"autoZip": true,
"autoClose": false
}