Skip to content

Commit e468df8

Browse files
authored
Merge pull request #398 from Somfic/issues-396
2 parents 28bd72d + 24da605 commit e468df8

7 files changed

Lines changed: 174 additions & 155 deletions

File tree

EliteAPI.Tests/FlatteningTests.cs

Lines changed: 87 additions & 87 deletions
Large diffs are not rendered by default.

EliteAPI.Tests/StatusTrackerTests.cs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using EliteAPI.Events;
22
using EliteAPI.Journals;
33
using FluentAssertions;
4-
using ValueType = EliteAPI.Events.ValueType;
4+
using EventValueType = EliteAPI.Events.EventValueType;
55

66
namespace EliteAPI.Tests;
77

@@ -15,16 +15,16 @@ public void DetectsFieldChange_WhenBooleanValueChanges()
1515
// First update: Gear is false
1616
var initialPaths = new List<EventPath>
1717
{
18-
new("EliteAPI.Gear", false, ValueType.Boolean),
19-
new("EliteAPI.Hardpoints", false, ValueType.Boolean)
18+
new("EliteAPI.Gear", false, EventValueType.Boolean),
19+
new("EliteAPI.Hardpoints", false, EventValueType.Boolean)
2020
};
2121
tracker.UpdateState(initialPaths);
2222

2323
// Second update: Gear changes to true
2424
var updatedPaths = new List<EventPath>
2525
{
26-
new("EliteAPI.Gear", true, ValueType.Boolean),
27-
new("EliteAPI.Hardpoints", false, ValueType.Boolean)
26+
new("EliteAPI.Gear", true, EventValueType.Boolean),
27+
new("EliteAPI.Hardpoints", false, EventValueType.Boolean)
2828
};
2929

3030
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -41,16 +41,16 @@ public void DetectsFieldChange_WhenNumericValueChanges()
4141
// First update: GuiFocus is 0
4242
var initialPaths = new List<EventPath>
4343
{
44-
new("EliteAPI.GuiFocus", 0, ValueType.Number),
45-
new("EliteAPI.FireGroup", 1, ValueType.Number)
44+
new("EliteAPI.GuiFocus", 0, EventValueType.Number),
45+
new("EliteAPI.FireGroup", 1, EventValueType.Number)
4646
};
4747
tracker.UpdateState(initialPaths);
4848

4949
// Second update: GuiFocus changes to 3
5050
var updatedPaths = new List<EventPath>
5151
{
52-
new("EliteAPI.GuiFocus", 3, ValueType.Number),
53-
new("EliteAPI.FireGroup", 1, ValueType.Number)
52+
new("EliteAPI.GuiFocus", 3, EventValueType.Number),
53+
new("EliteAPI.FireGroup", 1, EventValueType.Number)
5454
};
5555

5656
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -67,16 +67,16 @@ public void DetectsFieldChange_WhenDecimalValueChanges()
6767
// First update: FuelMain is 32.0
6868
var initialPaths = new List<EventPath>
6969
{
70-
new("EliteAPI.Fuel.FuelMain", 32.0m, ValueType.Decimal),
71-
new("EliteAPI.Fuel.FuelReservoir", 0.5m, ValueType.Decimal)
70+
new("EliteAPI.Fuel.FuelMain", 32.0m, EventValueType.Decimal),
71+
new("EliteAPI.Fuel.FuelReservoir", 0.5m, EventValueType.Decimal)
7272
};
7373
tracker.UpdateState(initialPaths);
7474

7575
// Second update: FuelMain changes to 31.5
7676
var updatedPaths = new List<EventPath>
7777
{
78-
new("EliteAPI.Fuel.FuelMain", 31.5m, ValueType.Decimal),
79-
new("EliteAPI.Fuel.FuelReservoir", 0.5m, ValueType.Decimal)
78+
new("EliteAPI.Fuel.FuelMain", 31.5m, EventValueType.Decimal),
79+
new("EliteAPI.Fuel.FuelReservoir", 0.5m, EventValueType.Decimal)
8080
};
8181

8282
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -94,18 +94,18 @@ public void DetectsMultipleFieldChanges()
9494
// First update
9595
var initialPaths = new List<EventPath>
9696
{
97-
new("EliteAPI.Gear", false, ValueType.Boolean),
98-
new("EliteAPI.Hardpoints", false, ValueType.Boolean),
99-
new("EliteAPI.GuiFocus", 0, ValueType.Number)
97+
new("EliteAPI.Gear", false, EventValueType.Boolean),
98+
new("EliteAPI.Hardpoints", false, EventValueType.Boolean),
99+
new("EliteAPI.GuiFocus", 0, EventValueType.Number)
100100
};
101101
tracker.UpdateState(initialPaths);
102102

103103
// Second update: Multiple fields change
104104
var updatedPaths = new List<EventPath>
105105
{
106-
new("EliteAPI.Gear", true, ValueType.Boolean),
107-
new("EliteAPI.Hardpoints", true, ValueType.Boolean),
108-
new("EliteAPI.GuiFocus", 0, ValueType.Number)
106+
new("EliteAPI.Gear", true, EventValueType.Boolean),
107+
new("EliteAPI.Hardpoints", true, EventValueType.Boolean),
108+
new("EliteAPI.GuiFocus", 0, EventValueType.Number)
109109
};
110110

111111
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -123,16 +123,16 @@ public void ReturnsEmptyList_WhenNoFieldsChange()
123123
// First update
124124
var initialPaths = new List<EventPath>
125125
{
126-
new("EliteAPI.Gear", false, ValueType.Boolean),
127-
new("EliteAPI.GuiFocus", 0, ValueType.Number)
126+
new("EliteAPI.Gear", false, EventValueType.Boolean),
127+
new("EliteAPI.GuiFocus", 0, EventValueType.Number)
128128
};
129129
tracker.UpdateState(initialPaths);
130130

131131
// Second update: Same values
132132
var updatedPaths = new List<EventPath>
133133
{
134-
new("EliteAPI.Gear", false, ValueType.Boolean),
135-
new("EliteAPI.GuiFocus", 0, ValueType.Number)
134+
new("EliteAPI.Gear", false, EventValueType.Boolean),
135+
new("EliteAPI.GuiFocus", 0, EventValueType.Number)
136136
};
137137

138138
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -148,9 +148,9 @@ public void ReturnsEmptyList_OnFirstUpdate()
148148
// First update (no previous state)
149149
var paths = new List<EventPath>
150150
{
151-
new("EliteAPI.Gear", false, ValueType.Boolean),
152-
new("EliteAPI.Hardpoints", false, ValueType.Boolean),
153-
new("EliteAPI.GuiFocus", 0, ValueType.Number)
151+
new("EliteAPI.Gear", false, EventValueType.Boolean),
152+
new("EliteAPI.Hardpoints", false, EventValueType.Boolean),
153+
new("EliteAPI.GuiFocus", 0, EventValueType.Number)
154154
};
155155

156156
var changedFields = tracker.GetChangedFieldNames(paths);
@@ -167,18 +167,18 @@ public void ExtractsRootFieldName_FromNestedPath()
167167
// First update
168168
var initialPaths = new List<EventPath>
169169
{
170-
new("EliteAPI.Fuel.FuelMain", 32.0m, ValueType.Decimal),
171-
new("EliteAPI.Fuel.FuelReservoir", 0.5m, ValueType.Decimal),
172-
new("EliteAPI.Destination.System", 123456789L, ValueType.Number)
170+
new("EliteAPI.Fuel.FuelMain", 32.0m, EventValueType.Decimal),
171+
new("EliteAPI.Fuel.FuelReservoir", 0.5m, EventValueType.Decimal),
172+
new("EliteAPI.Destination.System", 123456789L, EventValueType.Number)
173173
};
174174
tracker.UpdateState(initialPaths);
175175

176176
// Second update: Change nested field
177177
var updatedPaths = new List<EventPath>
178178
{
179-
new("EliteAPI.Fuel.FuelMain", 31.0m, ValueType.Decimal),
180-
new("EliteAPI.Fuel.FuelReservoir", 0.4m, ValueType.Decimal),
181-
new("EliteAPI.Destination.System", 987654321L, ValueType.Number)
179+
new("EliteAPI.Fuel.FuelMain", 31.0m, EventValueType.Decimal),
180+
new("EliteAPI.Fuel.FuelReservoir", 0.4m, EventValueType.Decimal),
181+
new("EliteAPI.Destination.System", 987654321L, EventValueType.Number)
182182
};
183183

184184
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -197,16 +197,16 @@ public void OnlyReportsRootFieldOnce_WhenMultipleSubfieldsChange()
197197
// First update
198198
var initialPaths = new List<EventPath>
199199
{
200-
new("EliteAPI.Fuel.FuelMain", 32.0m, ValueType.Decimal),
201-
new("EliteAPI.Fuel.FuelReservoir", 0.5m, ValueType.Decimal)
200+
new("EliteAPI.Fuel.FuelMain", 32.0m, EventValueType.Decimal),
201+
new("EliteAPI.Fuel.FuelReservoir", 0.5m, EventValueType.Decimal)
202202
};
203203
tracker.UpdateState(initialPaths);
204204

205205
// Second update: Both Fuel subfields change
206206
var updatedPaths = new List<EventPath>
207207
{
208-
new("EliteAPI.Fuel.FuelMain", 31.0m, ValueType.Decimal),
209-
new("EliteAPI.Fuel.FuelReservoir", 0.4m, ValueType.Decimal)
208+
new("EliteAPI.Fuel.FuelMain", 31.0m, EventValueType.Decimal),
209+
new("EliteAPI.Fuel.FuelReservoir", 0.4m, EventValueType.Decimal)
210210
};
211211

212212
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -224,14 +224,14 @@ public void HandlesStringValueChanges()
224224
// First update
225225
var initialPaths = new List<EventPath>
226226
{
227-
new("EliteAPI.LegalState", "Clean", ValueType.String)
227+
new("EliteAPI.LegalState", "Clean", EventValueType.String)
228228
};
229229
tracker.UpdateState(initialPaths);
230230

231231
// Second update: LegalState changes
232232
var updatedPaths = new List<EventPath>
233233
{
234-
new("EliteAPI.LegalState", "Wanted", ValueType.String)
234+
new("EliteAPI.LegalState", "Wanted", EventValueType.String)
235235
};
236236

237237
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -248,18 +248,18 @@ public void HandlesPipsArrayChanges()
248248
// First update
249249
var initialPaths = new List<EventPath>
250250
{
251-
new("EliteAPI.Pips.Systems", 2, ValueType.Number),
252-
new("EliteAPI.Pips.Engines", 8, ValueType.Number),
253-
new("EliteAPI.Pips.Weapons", 2, ValueType.Number)
251+
new("EliteAPI.Pips.Systems", 2, EventValueType.Number),
252+
new("EliteAPI.Pips.Engines", 8, EventValueType.Number),
253+
new("EliteAPI.Pips.Weapons", 2, EventValueType.Number)
254254
};
255255
tracker.UpdateState(initialPaths);
256256

257257
// Second update: Pips change
258258
var updatedPaths = new List<EventPath>
259259
{
260-
new("EliteAPI.Pips.Systems", 4, ValueType.Number),
261-
new("EliteAPI.Pips.Engines", 4, ValueType.Number),
262-
new("EliteAPI.Pips.Weapons", 4, ValueType.Number)
260+
new("EliteAPI.Pips.Systems", 4, EventValueType.Number),
261+
new("EliteAPI.Pips.Engines", 4, EventValueType.Number),
262+
new("EliteAPI.Pips.Weapons", 4, EventValueType.Number)
263263
};
264264

265265
var changedFields = tracker.GetChangedFieldNames(updatedPaths);
@@ -277,7 +277,7 @@ public void IsThreadSafe_WhenAccessedConcurrently()
277277
// Initialize
278278
var initialPaths = new List<EventPath>
279279
{
280-
new("EliteAPI.Gear", false, ValueType.Boolean)
280+
new("EliteAPI.Gear", false, EventValueType.Boolean)
281281
};
282282
tracker.UpdateState(initialPaths);
283283

@@ -290,7 +290,7 @@ public void IsThreadSafe_WhenAccessedConcurrently()
290290
{
291291
var paths = new List<EventPath>
292292
{
293-
new("EliteAPI.Gear", value, ValueType.Boolean)
293+
new("EliteAPI.Gear", value, EventValueType.Boolean)
294294
};
295295
tracker.GetChangedFieldNames(paths);
296296
tracker.UpdateState(paths);

EliteAPI/EliteDangerousApi.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ internal void Invoke(string json, IEvent? @event)
240240

241241
private void HandleBindingsPreset(string presetContent)
242242
{
243+
// look at the preset that occurs the most
244+
243245
var presets = presetContent.Split('\n').Where(line => !string.IsNullOrWhiteSpace(line)).ToList();
244246

245247
// Make sure all presets use the same format

EliteAPI/Events/EventPath.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11

2+
using System;
3+
using System.Runtime.CompilerServices;
4+
25
namespace EliteAPI.Events;
36

4-
public readonly struct EventPath(string path, dynamic value, ValueType type)
7+
public readonly struct EventPath(string path, dynamic value, EventValueType type)
58
{
69
public string Path { get; init; } = path;
710

811
public dynamic Value { get; init; } = value;
912

10-
public ValueType Type { get; init; } = type;
13+
public EventValueType Type { get; init; } = type;
1114

1215
internal EventPath WithPath(string path) => new(path.Replace("..", "."), Value, Type);
1316
}
1417

15-
public enum ValueType
18+
public enum EventValueType
1619
{
1720
String,
1821
Number,
1922
Decimal,
2023
Boolean,
2124
DateTime,
2225
}
26+
27+
28+
public static class EventUtils
29+
{
30+
public static string ToDisplayType(this EventValueType valueType) => valueType switch
31+
{
32+
EventValueType.String => "TXT",
33+
EventValueType.Number => "INT",
34+
EventValueType.Decimal => "DEC",
35+
EventValueType.Boolean => "BOOL",
36+
EventValueType.DateTime => "DATE",
37+
_ => "",
38+
};
39+
}

EliteAPI/Journals/JournalUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Text.RegularExpressions;
77
using EliteAPI.Events;
88
using EliteAPI.Json;
9-
using ValueType = EliteAPI.Events.ValueType;
9+
using EventValueType = EliteAPI.Events.EventValueType;
1010

1111
namespace EliteAPI.Journals;
1212

@@ -29,13 +29,13 @@ public static List<EventPath> ToPaths(string json)
2929
var flagsPath = paths.FirstOrDefault(p => p.Path == "Flags");
3030
var flagsValue = flagsPath.Equals(default(EventPath)) ? 0 : Convert.ToInt32(flagsPath.Value);
3131
foreach (var flag in StatusUtils.GetFlags(flagsValue))
32-
paths.Add(new EventPath(flag.Key, flag.Value, ValueType.Boolean));
32+
paths.Add(new EventPath(flag.Key, flag.Value, EventValueType.Boolean));
3333

3434
// expand flags2
3535
var flags2Path = paths.FirstOrDefault(p => p.Path == "Flags2");
3636
var flags2Value = flags2Path.Equals(default(EventPath)) ? 0 : Convert.ToInt32(flags2Path.Value);
3737
foreach (var flag in StatusUtils.GetFlags2(flags2Value))
38-
paths.Add(new EventPath(flag.Key, flag.Value, ValueType.Boolean));
38+
paths.Add(new EventPath(flag.Key, flag.Value, EventValueType.Boolean));
3939

4040
// replace pips array with individual pips
4141
paths.Add(paths.FirstOrDefault(p => p.Path == "Pips[0]").WithPath("Pips.Systems"));
@@ -47,7 +47,7 @@ public static List<EventPath> ToPaths(string json)
4747
if (!balancePath.Equals(default(EventPath)))
4848
{
4949
paths.Remove(balancePath);
50-
paths.Add(new EventPath("Balance", Convert.ToDecimal(balancePath.Value), ValueType.Decimal));
50+
paths.Add(new EventPath("Balance", Convert.ToDecimal(balancePath.Value), EventValueType.Decimal));
5151
}
5252
}
5353

EliteAPI/Json/JsonUtils.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using EliteAPI.Json.SerializationSettings;
77
using Newtonsoft.Json;
88
using Newtonsoft.Json.Linq;
9-
using ValueType = EliteAPI.Events.ValueType;
9+
using EventValueType = EliteAPI.Events.EventValueType;
1010

1111
namespace EliteAPI.Json;
1212

@@ -108,37 +108,37 @@ private static EventPath ToJsonPath(this JValue value)
108108
{
109109
Path = path,
110110
Value = Convert.ToInt32(value.Value),
111-
Type = ValueType.Number
111+
Type = EventValueType.Number
112112
},
113113
JTokenType.Float => new EventPath
114114
{
115115
Path = path,
116116
Value = value.Value is BigInteger bigInt ? (decimal)bigInt : Convert.ToDecimal(value.Value),
117-
Type = ValueType.Decimal
117+
Type = EventValueType.Decimal
118118
},
119119
JTokenType.Uri or JTokenType.Guid or JTokenType.String => new EventPath
120120
{
121121
Path = path,
122122
Value = Convert.ToString(value.Value) ?? string.Empty,
123-
Type = ValueType.String
123+
Type = EventValueType.String
124124
},
125125
JTokenType.Boolean => new EventPath
126126
{
127127
Path = path,
128128
Value = Convert.ToBoolean(value.Value),
129-
Type = ValueType.Boolean
129+
Type = EventValueType.Boolean
130130
},
131131
JTokenType.Date => new EventPath
132132
{
133133
Path = path,
134134
Value = Convert.ToDateTime(value.Value),
135-
Type = ValueType.DateTime
135+
Type = EventValueType.DateTime
136136
},
137137
_ => new EventPath
138138
{
139139
Path = "",
140140
Value = "",
141-
Type = ValueType.String
141+
Type = EventValueType.String
142142
},
143143
};
144144
}

0 commit comments

Comments
 (0)