summaryrefslogtreecommitdiff
path: root/writer/Program.cs
blob: 6cbcf8e0559c2be7e4020af71891775f9384a145 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace writer
{

    class Program
    {

        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)
        {
            try
            {
                VersionHelper.ValidOrThrow();
            }
            catch (InteropException e)
            {
                ExitWithMessage(e.Message);
            }

            try
            {
                var settings = Parse(new List<string>(args));
                var errors = new SettingsErrors(settings);

                if (errors.Empty())
                {
                    new ManagedAccel(settings).Activate();
                }
                else
                {
                    ExitWithMessage($"Bad settings:\n\n{errors}");
                }
            }
            catch (System.IO.FileNotFoundException e)
            {
                ExitWithMessage(e.Message);
            }
            catch (JsonException e)
            {
                ExitWithMessage($"Settings invalid:\n\n{e.Message}");
            }
            catch (Exception e)
            {
                ExitWithMessage($"Error:\n\n{e}");
            }
        }
    }
}