summaryrefslogtreecommitdiff
path: root/writer/Program.cs
diff options
context:
space:
mode:
Diffstat (limited to 'writer/Program.cs')
-rw-r--r--writer/Program.cs106
1 files changed, 74 insertions, 32 deletions
diff --git a/writer/Program.cs b/writer/Program.cs
index 37e384c..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,68 +12,109 @@ 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 Send(JToken settingsToken)
+ static void ExitWithUsage()
{
- var settings = settingsToken.ToObject<DriverSettings>();
+ ExitWithMessage($"Usage: {System.AppDomain.CurrentDomain.FriendlyName} <settings file path>");
+ }
- var errors = DriverInterop.GetSettingsErrors(settings);
- if (errors.Empty())
- {
- DriverInterop.Write(settings);
- return;
- }
+ delegate string PopOption(params string[] aliases);
- Show($"Bad settings:\n\n{errors}");
+ static string Read(string path)
+ {
+ return path == null ? null : File.ReadAllText(path);
}
- static void Main(string[] args)
+ static ExtendedSettings Parse(List<string> args)
{
- try
+ PopOption maybePop = aliases =>
{
- VersionHelper.ValidateAndGetDriverVersion(typeof(Program).Assembly.GetName().Version);
- }
- catch (VersionException e)
+ 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)
{
- Show(e.Message);
- return;
+ if (args.Count > 1) ExitWithUsage();
+ else if (args.Count == 1) settingsPath = args[0];
+
+ return new ExtendedSettings(Read(settingsPath), Read(tablePath));
}
- if (args.Length != 1)
+ 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
{
- Show($"Usage: {System.AppDomain.CurrentDomain.FriendlyName} <settings file path>");
- return;
+ yTableJson = Read(yTablePath);
}
- if (!File.Exists(args[0]))
+ return new ExtendedSettings(Read(settingsPath), xTableJson, yTableJson);
+ }
+
+ static void Main(string[] args)
+ {
+ try
{
- Show($"Settings file not found at {args[0]}");
- return;
+ VersionHelper.ValidOrThrow();
+ }
+ catch (InteropException e)
+ {
+ ExitWithMessage(e.Message);
}
try
{
- var JO = JObject.Parse(File.ReadAllText(args[0]));
+ var settings = Parse(new List<string>(args));
+ var errors = new SettingsErrors(settings);
- if (JO.ContainsKey(DriverSettings.Key))
+ if (errors.Empty())
{
- Send(JO[DriverSettings.Key]);
- return;
+ new ManagedAccel(settings).Activate();
}
-
- Send(JO);
+ else
+ {
+ 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}");
}
}
}