diff options
Diffstat (limited to 'writer/Program.cs')
| -rw-r--r-- | writer/Program.cs | 88 |
1 files changed, 74 insertions, 14 deletions
diff --git a/writer/Program.cs b/writer/Program.cs index d381c16..6cbcf8e 100644 --- a/writer/Program.cs +++ b/writer/Program.cs @@ -1,8 +1,9 @@ using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; using System.IO; +using System.Linq; using System.Windows.Forms; namespace writer @@ -11,9 +12,71 @@ namespace writer class Program { - static void Show(string msg) + static void ExitWithMessage(string msg) { MessageBox.Show(msg, "Raw Accel writer"); + Environment.Exit(1); + } + + static void ExitWithUsage() + { + ExitWithMessage($"Usage: {System.AppDomain.CurrentDomain.FriendlyName} <settings file path>"); + } + + delegate string PopOption(params string[] aliases); + + static string Read(string path) + { + return path == null ? null : File.ReadAllText(path); + } + + static ExtendedSettings Parse(List<string> args) + { + PopOption maybePop = aliases => + { + int idx = args.FindIndex(aliases.Contains); + + if (idx == -1) return null; + + if (idx == args.Count - 1) ExitWithUsage(); + + string val = args[idx + 1]; + args.RemoveRange(idx, 2); + return val; + }; + + string settingsPath = null; + + string tablePath = maybePop("table", "t"); + + if (tablePath != null) + { + if (args.Count > 1) ExitWithUsage(); + else if (args.Count == 1) settingsPath = args[0]; + + return new ExtendedSettings(Read(settingsPath), Read(tablePath)); + } + + string xTablePath = maybePop("xtable", "xt"); + string yTablePath = maybePop("ytable", "yt"); + + if (args.Count > 1) ExitWithUsage(); + else if (args.Count == 1) settingsPath = args[0]; + else if (xTablePath == null && yTablePath == null) ExitWithUsage(); + + string xTableJson = Read(xTablePath); + string yTableJson = null; + + if (xTablePath != null && xTablePath.Equals(yTablePath)) + { + yTableJson = xTableJson; + } + else + { + yTableJson = Read(yTablePath); + } + + return new ExtendedSettings(Read(settingsPath), xTableJson, yTableJson); } static void Main(string[] args) @@ -24,19 +87,12 @@ namespace writer } catch (InteropException e) { - Show(e.Message); - return; - } - - if (args.Length != 1) - { - Show($"Usage: {System.AppDomain.CurrentDomain.FriendlyName} <settings file path>"); - return; + ExitWithMessage(e.Message); } try { - var settings = DriverSettings.FromFile(args[0]); + var settings = Parse(new List<string>(args)); var errors = new SettingsErrors(settings); if (errors.Empty()) @@ -45,16 +101,20 @@ namespace writer } else { - Show($"Bad settings:\n\n{errors}"); + ExitWithMessage($"Bad settings:\n\n{errors}"); } } + catch (System.IO.FileNotFoundException e) + { + ExitWithMessage(e.Message); + } catch (JsonException e) { - Show($"Settings invalid:\n\n{e.Message}"); + ExitWithMessage($"Settings invalid:\n\n{e.Message}"); } catch (Exception e) { - Show($"Error:\n\n{e}"); + ExitWithMessage($"Error:\n\n{e}"); } } } |