diff --git a/LarkatorGUI/ArkReader.cs b/LarkatorGUI/ArkReader.cs index b0e5cd1..4579eb7 100644 --- a/LarkatorGUI/ArkReader.cs +++ b/LarkatorGUI/ArkReader.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; using System.Threading.Tasks; namespace LarkatorGUI @@ -21,7 +22,6 @@ public class ArkReader public bool Tamed { get; private set; } Dictionary ClassMapping = new Dictionary(); - private Process process; private bool executing; private string outputDir; @@ -84,41 +84,21 @@ private async Task RunArkTools() FoundDinos.Clear(); } - private async Task ExecuteArkTools(string op, string saveFile, string outDir) + public static async Task ExecuteArkTools(string op, params string[] args) { var exe = Properties.Resources.ArkToolsExe; var exeDir = Path.GetDirectoryName(Properties.Settings.Default.ArkTools); - var commandLine = $"/S /C {exe} -p {op} \"{saveFile}\" \"{outDir}\""; + var sb = new StringBuilder($"/S /C {exe} -p {op}"); + foreach (var arg in args) + sb.Append(" \"" + arg + "\""); - var completionTask = new TaskCompletionSource(); - var psi = new ProcessStartInfo("cmd.exe", commandLine) - { - CreateNoWindow = true, - UseShellExecute = false, - RedirectStandardError = true, - RedirectStandardOutput = true, - ErrorDialog = true, - WorkingDirectory = exeDir, - }; - process = new Process - { - EnableRaisingEvents = true, - StartInfo = psi, - }; + var result = await ExecuteCommand(sb.ToString(), exeDir); - string collectedErrors = ""; - process.ErrorDataReceived += (s, e) => { if (!String.IsNullOrWhiteSpace(e.Data)) collectedErrors += e.Data; }; - process.Exited += (s, e) => completionTask.SetResult(process.ExitCode); - process.Start(); - process.BeginErrorReadLine(); - await completionTask.Task; - process.CancelErrorRead(); + if (!String.IsNullOrWhiteSpace(result.ErrorOutput)) + throw new ExternalToolsException(result.ErrorOutput); - if (!String.IsNullOrWhiteSpace(collectedErrors)) - throw new ExternalToolsException(collectedErrors); - - if (process.ExitCode != 0) - throw new ExternalToolsException("ARK Tools failed with no output but exit code " + process.ExitCode); + if (result.ExitCode != 0) + throw new ExternalToolsException("ARK Tools failed with no output but exit code " + result.ExitCode); } private async Task LoadClassesJson() @@ -150,7 +130,36 @@ private async Task LoadSpecies(string speciesName) LoadedSpecies.Add(speciesName); } - private async Task ReadFileAsync(string path) + private static async Task ExecuteCommand(string commandLine, string workingDir) + { + var completionTask = new TaskCompletionSource(); + var psi = new ProcessStartInfo("cmd.exe", commandLine) + { + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardError = true, + RedirectStandardOutput = true, + ErrorDialog = true, + WorkingDirectory = workingDir, + }; + var process = new Process + { + EnableRaisingEvents = true, + StartInfo = psi, + }; + + var collectedErrors = new StringBuilder(); + process.ErrorDataReceived += (s, e) => { if (!String.IsNullOrWhiteSpace(e.Data)) collectedErrors.Append(e.Data); }; + process.Exited += (s, e) => completionTask.SetResult(process.ExitCode); + process.Start(); + process.BeginErrorReadLine(); + await completionTask.Task; + process.CancelErrorRead(); + + return new ProcessResult { ErrorOutput = collectedErrors.ToString(), ExitCode = process.ExitCode }; + } + + private static async Task ReadFileAsync(string path) { string content; using (var reader = File.OpenText(path)) @@ -160,5 +169,11 @@ private async Task ReadFileAsync(string path) return content; } + + private class ProcessResult + { + public string ErrorOutput { get; set; } + public int ExitCode { get; set; } + } } } diff --git a/LarkatorGUI/MainWindow.xaml.cs b/LarkatorGUI/MainWindow.xaml.cs index ef170eb..ddc1971 100644 --- a/LarkatorGUI/MainWindow.xaml.cs +++ b/LarkatorGUI/MainWindow.xaml.cs @@ -206,6 +206,7 @@ private async void ReloadTimer_Tick(object sender, EventArgs e) private async void Window_Loaded(object sender, RoutedEventArgs e) { + await UpdateArkToolsData(); await ReReadArk(); } @@ -376,6 +377,20 @@ private void LoadSavedSearches() } } + private async Task UpdateArkToolsData() + { + StatusText = "Updating ark-tools database"; + try + { + await ArkReader.ExecuteArkTools("update-data"); + StatusText = "Updated ark-tools database"; + } + catch (Exception e) + { + StatusText = "Failed to update ark-tools database: " + e.Message; + } + } + private async Task ReReadArk(bool force = false) { await PerformConversion(force);