Skip to content

Commit

Permalink
Create custom widget; Set read-only vectors; Resize vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
wegylexy committed Sep 3, 2021
1 parent 7138aaf commit 8db8bc5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ P/Invoke for X-Plane plugin library manager
## Reference Packages
`Microsoft.DotNet.ILCompiler` must be a top level dependency for native AOT:
```xml
<PackageReference Include="FlyByWireless.XPLM" Version="1.0.5-*"/>
<PackageReference Include="FlyByWireless.XPLM" Version="1.0.6-*"/>
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="6.0.0-*"/>
```
## Implement
Expand Down
99 changes: 88 additions & 11 deletions XPLM/DataAccess.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace FlyByWireless.XPLM.DataAccess;

Expand Down Expand Up @@ -265,9 +266,23 @@ public sealed class DoubleAccessor : IAccessor

public sealed class IntArrayAccessor : IAccessor
{
readonly int[] _array;
int[] _array;

public IntArrayAccessor(int length) => _array = new int[length];
public Span<int> AsIntVector => _array;

public IntArrayAccessor(int length) => _array = length > 0 ? new int[length] : Array.Empty<int>();

public void Resize(int length)
{
if (length > 0)
{
Array.Resize(ref _array, length);
}
else
{
_array = Array.Empty<int>();
}
}

int CountIntVector() => _array.Length;
int ReadIntVector(int offset, Span<int> destination)
Expand All @@ -285,9 +300,23 @@ void WriteIntVector(int offset, ReadOnlySpan<int> source)

public sealed class FloatArrayAccessor : IAccessor
{
readonly float[] _array;
float[] _array;

public Span<float> AsFloatVector => _array;

public FloatArrayAccessor(int length) => _array = new float[length];
public FloatArrayAccessor(int length) => _array = length > 0 ? new float[length] : Array.Empty<float>();

public void Resize(int length)
{
if (length > 0)
{
Array.Resize(ref _array, length);
}
else
{
_array = Array.Empty<float>();
}
}

int CountFloatVector() => _array.Length;
int ReadFloatVector(int offset, Span<float> destination)
Expand All @@ -303,9 +332,57 @@ void WriteFloatVector(int offset, ReadOnlySpan<float> source)
}
}

public sealed class DataAccessor : IAccessor
{
byte[] _array;

public Span<byte> AsByteVector => _array;

public string AsASCII
{
get => Encoding.ASCII.GetString(_array);
set => _array = Encoding.ASCII.GetBytes(value);
}

public string AsUTF8
{
get => Encoding.UTF8.GetString(_array);
set => _array = Encoding.UTF8.GetBytes(value);
}

public DataAccessor(int length = 0) => _array = length > 0 ? new byte[length] : Array.Empty<byte>();

public void Resize(int length)
{
if (length > 0)
{
Array.Resize(ref _array, length);
}
else
{
_array = Array.Empty<byte>();
}
}

int CountByteVector() => _array.Length;
int ReadByteVector(int offset, Span<byte> destination)
{
var length = Math.Min(_array.Length - offset, destination.Length);
_array.AsSpan(offset, length).CopyTo(destination);
return length;
}
void WriteByteVector(int offset, ReadOnlySpan<byte> source)
{
var count = Math.Min(_array.Length - offset, source.Length);
source[..count].CopyTo(_array.AsSpan(offset, count));
}
}

public sealed class Accessor<T> : IAccessor where T : unmanaged
{
readonly T _data;
T _data;

public ref T AsData => ref _data;

public Accessor() =>
Debug.Assert(typeof(T) != typeof(int) && typeof(T) != typeof(float) && typeof(T) != typeof(double));
Expand Down Expand Up @@ -433,12 +510,12 @@ static void WriteByteVector(nint handle, byte* values, int offset, int count) =>
var isData = type.HasFlag(DataTypes.Data);
nint r = GCHandle.ToIntPtr(_handle = GCHandle.Alloc(accessor));
DataRef = new(XPLMRegisterDataAccessor(name, (int)type, isWritable ? 1 : 0,
isInt ? &ReadInt : null, isInt ? &WriteInt : null,
isFloat ? &ReadFloat : null, isFloat ? &WriteFloat : null,
isDouble ? &ReadDouble : null, isDouble ? &WriteDouble : null,
isIntArray ? &ReadIntVector : null, isIntArray ? &WriteIntVector : null,
isFloatArray ? &ReadFloatVector : null, isFloatArray ? &WriteFloatVector : null,
isData ? &ReadByteVector : null, isData ? &WriteByteVector : null,
isInt ? &ReadInt : null, isWritable && isInt ? &WriteInt : null,
isFloat ? &ReadFloat : null, isWritable && isFloat ? &WriteFloat : null,
isDouble ? &ReadDouble : null, isWritable && isDouble ? &WriteDouble : null,
isIntArray ? &ReadIntVector : null, isWritable && isIntArray ? &WriteIntVector : null,
isFloatArray ? &ReadFloatVector : null, isWritable && isFloatArray ? &WriteFloatVector : null,
isData ? &ReadByteVector : null, isWritable && isData ? &WriteByteVector : null,
r, r
));
}
Expand Down
2 changes: 1 addition & 1 deletion XPLM/Processing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static float Loop(float elapsedSinceLastCall, float elapsedSinceLastFlightLoop,
}
catch (Exception ex)
{
Utilities.DebugString(ex.ToString());
Utilities.DebugString(ex.ToString() + "\n");
return 0;
}
}
Expand Down
6 changes: 3 additions & 3 deletions XPLM/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static int B(nint id, CommandPhase phase, nint state)
}
catch (Exception ex)
{
Utilities.DebugString(ex.ToString());
Utilities.DebugString(ex.ToString() + "\n");
return 1;
}
}
Expand All @@ -83,7 +83,7 @@ static int A(nint id, CommandPhase phase, nint state)
}
catch (Exception ex)
{
Utilities.DebugString(ex.ToString());
Utilities.DebugString(ex.ToString() + "\n");
return 1;
}
}
Expand Down Expand Up @@ -278,7 +278,7 @@ static void E(nint message)
}
catch (Exception ex)
{
DebugString(ex.ToString());
DebugString(ex.ToString() + "\n");
}
}

Expand Down
8 changes: 7 additions & 1 deletion XPLM/Widgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ internal unsafe Widget(Rectangle rectangle, bool visible, string descriptor, Wid
XPAddWidgetCallback(_id, &Callback);
}

// TODO: create custom widget
public unsafe Widget(Rectangle rectangle, bool visible, string descriptor, Widget? container)
{
[DllImport(Defs.Lib)]
static extern nint XPCreateCustomWidget(int left, int top, int right, int bottom, int visible, [MarshalAs(UnmanagedType.LPUTF8Str)] string descriptor, int isRoot, nint container, delegate* unmanaged<WidgetMessage, nint, nint, nint, int> callback);

_widgets.Add(_id = XPCreateCustomWidget(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom, visible ? 1 : 0, descriptor, container == null ? 1 : 0, container?._id ?? 0, &Callback), this);
}

protected virtual void Destroy()
{
Expand Down
2 changes: 1 addition & 1 deletion XPLM/XPLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<AssemblyName>FlyByWireless.XPLM</AssemblyName>
<Version>1.0.5-alpha</Version>
<Version>1.0.6-alpha</Version>
<Authors>WONG Tin Chi Timothy</Authors>
<Company>Timmy Net Net Company</Company>
<Product>P/Invoke for X-Plane plugin library manager</Product>
Expand Down

0 comments on commit 8db8bc5

Please sign in to comment.