|
| 1 | +// <copyright file="SimVarStructBinder.cs" company="BARS"> |
| 2 | +// Copyright (c) BARS. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +namespace SimConnect.NET.SimVar.Internal |
| 10 | +{ |
| 11 | + internal static class SimVarStructBinder |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Returns the ordered [SimVar]-annotated fields for T, validating .NET types vs SimConnect types. |
| 15 | + /// </summary> |
| 16 | + internal static (System.Reflection.FieldInfo Field, SimConnectAttribute Attr)[] GetOrderedFields<T>() |
| 17 | + { |
| 18 | + var t = typeof(T); |
| 19 | + if (!t.IsLayoutSequential) |
| 20 | + { |
| 21 | + throw new InvalidOperationException($"{t.Name} must be annotated with [StructLayout(LayoutKind.Sequential)]."); |
| 22 | + } |
| 23 | + |
| 24 | + var fields = t.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public) |
| 25 | + .Select(f => (Field: f, Attr: f.GetCustomAttribute<SimConnectAttribute>())) |
| 26 | + .Where(x => x.Attr != null) |
| 27 | + .OrderBy(x => x!.Attr!.Order) |
| 28 | + .ThenBy(x => x.Field.MetadataToken) |
| 29 | + .ToArray(); |
| 30 | + |
| 31 | + if (fields.Length == 0) |
| 32 | + { |
| 33 | + throw new InvalidOperationException($"{t.Name} has no fields annotated with [SimVar]."); |
| 34 | + } |
| 35 | + |
| 36 | + foreach (var (field, attr) in fields) |
| 37 | + { |
| 38 | + var ft = field.FieldType; |
| 39 | + switch (attr!.DataType) |
| 40 | + { |
| 41 | + case SimConnectDataType.FloatDouble: |
| 42 | + if (ft != typeof(double)) |
| 43 | + { |
| 44 | + throw Fail(field, "double"); |
| 45 | + } |
| 46 | + |
| 47 | + break; |
| 48 | + case SimConnectDataType.FloatSingle: |
| 49 | + if (ft != typeof(float)) |
| 50 | + { |
| 51 | + throw Fail(field, "float"); |
| 52 | + } |
| 53 | + |
| 54 | + break; |
| 55 | + case SimConnectDataType.Integer32: |
| 56 | + if (ft != typeof(int) && ft != typeof(uint)) |
| 57 | + { |
| 58 | + throw Fail(field, "int/uint"); |
| 59 | + } |
| 60 | + |
| 61 | + break; |
| 62 | + case SimConnectDataType.Integer64: |
| 63 | + if (ft != typeof(long) && ft != typeof(ulong)) |
| 64 | + { |
| 65 | + throw Fail(field, "long/ulong"); |
| 66 | + } |
| 67 | + |
| 68 | + break; |
| 69 | + case SimConnectDataType.String8: |
| 70 | + case SimConnectDataType.String32: |
| 71 | + case SimConnectDataType.String64: |
| 72 | + case SimConnectDataType.String128: |
| 73 | + case SimConnectDataType.String256: |
| 74 | + case SimConnectDataType.String260: |
| 75 | + if (ft != typeof(string)) |
| 76 | + { |
| 77 | + throw Fail(field, "string"); |
| 78 | + } |
| 79 | + |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return fields!; |
| 85 | + |
| 86 | + static InvalidOperationException Fail(FieldInfo f, string expected) |
| 87 | + => new($"Field {f.DeclaringType!.Name}.{f.Name} must be {expected} to match its [SimVar] attribute."); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Builds a single SimConnect data definition for T (using [SimVar] attributes), |
| 92 | + /// registers T for marshalling, and returns the definition ID. |
| 93 | + /// </summary> |
| 94 | + /// <param name="handle">Native SimConnect handle.</param> |
| 95 | + internal static (uint DefId, (System.Reflection.FieldInfo Field, SimConnectAttribute Attr)[] Fields) BuildAndRegisterFromStruct<T>(IntPtr handle) |
| 96 | + { |
| 97 | + var t = typeof(T); |
| 98 | + if (!t.IsLayoutSequential) |
| 99 | + { |
| 100 | + throw new InvalidOperationException($"{t.Name} must be annotated with [StructLayout(LayoutKind.Sequential)]."); |
| 101 | + } |
| 102 | + |
| 103 | + var fields = GetOrderedFields<T>(); |
| 104 | + |
| 105 | + uint defId = unchecked((uint)Guid.NewGuid().GetHashCode()); |
| 106 | + |
| 107 | + var result = SimConnectNative.SimConnect_ClearDataDefinition(handle, defId); |
| 108 | + if (result != 0) |
| 109 | + { |
| 110 | + throw new InvalidOperationException($"Failed to clear data definition for {t.Name}: {result}"); |
| 111 | + } |
| 112 | + |
| 113 | + foreach (var (field, attr) in fields) |
| 114 | + { |
| 115 | + // Add each SimVar field to the SimConnect data definition using the native layer |
| 116 | + if (attr == null) |
| 117 | + { |
| 118 | + throw new InvalidOperationException($"Field {field.Name} is missing [SimVar] attribute."); |
| 119 | + } |
| 120 | + |
| 121 | + result = SimConnectNative.SimConnect_AddToDataDefinition( |
| 122 | + handle, |
| 123 | + defId, |
| 124 | + attr.Name, |
| 125 | + attr.Unit ?? string.Empty, |
| 126 | + (uint)attr.DataType); |
| 127 | + |
| 128 | + if (result != 0) |
| 129 | + { |
| 130 | + throw new InvalidOperationException($"Failed to add data definition for {field.Name}: {result}"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + var size = SimVarDataTypeSizing.GetPayloadSizeBytes(fields.Select(f => f.Attr!.DataType)); |
| 135 | + var offsets = SimVarDataTypeSizing.ComputeOffsets(fields.Select(f => f.Attr!.DataType)); |
| 136 | + return (defId, fields); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments