Skip to content

Commit

Permalink
Automate running ark-tools update-data
Browse files Browse the repository at this point in the history
Ark tools database is now updated each time the app is loaded. Closes #6.
  • Loading branch information
coldino committed Jan 17, 2018
1 parent d0e7c1d commit 87bd661
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
77 changes: 46 additions & 31 deletions LarkatorGUI/ArkReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LarkatorGUI
Expand All @@ -21,7 +22,6 @@ public class ArkReader
public bool Tamed { get; private set; }

Dictionary<string, string> ClassMapping = new Dictionary<string, string>();
private Process process;
private bool executing;
private string outputDir;

Expand Down Expand Up @@ -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<int>();
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()
Expand Down Expand Up @@ -150,7 +130,36 @@ private async Task LoadSpecies(string speciesName)
LoadedSpecies.Add(speciesName);
}

private async Task<string> ReadFileAsync(string path)
private static async Task<ProcessResult> ExecuteCommand(string commandLine, string workingDir)
{
var completionTask = new TaskCompletionSource<int>();
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<string> ReadFileAsync(string path)
{
string content;
using (var reader = File.OpenText(path))
Expand All @@ -160,5 +169,11 @@ private async Task<string> ReadFileAsync(string path)

return content;
}

private class ProcessResult
{
public string ErrorOutput { get; set; }
public int ExitCode { get; set; }
}
}
}
15 changes: 15 additions & 0 deletions LarkatorGUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 87bd661

Please sign in to comment.