summaryrefslogtreecommitdiff
path: root/grapher/Models/Serialized/LookupTable.cs
blob: 578c5bf48817b28147a5e270b1fd0c75274af0d7 (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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace grapher.Models.Serialized
{
    [Serializable]
    public class LookupTable
    {
        [Serializable]
        public class Point
        {
            public Point(double x, double y)
            {
                X = x;
                Y = y;
            }

            double X { get; set; }

            double Y { get; set; }
        }

        public LookupTable(Point[] points)
        {
            Points = points;
        }

        public Point[] Points { get; }

        public bool InSensGraph { get; }

        public static LookupTable Deserialize(string lutFile)
        {
            if (!File.Exists(lutFile))
            {
                throw new Exception($"LUT file does not exist at {lutFile}.");
            }

            JObject lutJObject = JObject.Parse(File.ReadAllText(lutFile));

            var lut = lutJObject.ToObject<LookupTable>(JsonSerializer.Create(RawAccelSettings.SerializerSettings));

            if (lut is null || lut.Points is null)
            {
                throw new Exception($"{lutFile} does not contain valid lookuptable json.");
            }

            lut.Verify();

            return lut;
        }

        private void Verify()
        {
            if (Points.Length >= short.MaxValue)
            {
                throw new Exception($"LUT file with {Points.Length} points is too long. Max points: {short.MaxValue}");
            }
        }
    }
}