Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wegylexy committed Feb 11, 2022
1 parent d0deaa0 commit c4e9ad1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 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.7-*"/>
<PackageReference Include="FlyByWireless.XPLM" Version="1.0.8-*"/>
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="6.0.0-*"/>
```
## Implement
Expand Down
6 changes: 3 additions & 3 deletions XPL/XPL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<Authors>WONG Tin Chi Timothy</Authors>
<Company>Timmy Net Net Company</Company>
<Product>X-Plane plugin library template</Product>
<Copyright>(C) 2021 WONG Tin Chi Timothy. All rights reserved.</Copyright>
<Copyright>(C) 2021-2022 WONG Tin Chi Timothy. All rights reserved.</Copyright>
<RepositoryUrl>https://github.com/wegylexy/XPLM</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.0.7-alpha</Version>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.0.8-beta</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FlyByWireless.XPLM" Version="1.0.7-*" />
<PackageReference Include="FlyByWireless.XPLM" Version="1.0.8-*" />
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="6.0.0-*" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions XPLM.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{91CA81B1-BE3C-4368-B890-6EB81B5DF5AD}"
ProjectSection(SolutionItems) = preProject
nuget.config = nuget.config
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "refs", "refs", "{FE584BE4-CCC5-47D2-B108-F8A99C954B00}"
Expand Down
11 changes: 5 additions & 6 deletions XPLM/Camera.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace FlyByWireless.XPLM;

Expand Down Expand Up @@ -27,14 +26,14 @@ public static class Camera
public static unsafe void Control(CameraControlDuration howLong, CameraControl control)
{
[DllImport(Defs.Lib)]
static extern void XPLMControlCamera(CameraControlDuration howLong, delegate* unmanaged<ref CameraPosition, int, nint, int> control, nint state);
static extern void XPLMControlCamera(CameraControlDuration howLong, delegate* unmanaged<CameraPosition*, int, nint, int> control, nint state);

[UnmanagedCallersOnly]
static int C(ref CameraPosition cameraPosition, int isLosingControl, nint state)
static int C(CameraPosition* cameraPosition, int isLosingControl, nint state)
{
var c = ((CameraControl)GCHandle.FromIntPtr(state).Target!)(out var position, isLosingControl != 0);
if (position.HasValue && !Unsafe.IsNullRef(ref cameraPosition))
cameraPosition = position!.Value;
if (position.HasValue && cameraPosition != null)
*cameraPosition = position!.Value;
return c ? 1 : 0;
}

Expand Down
22 changes: 11 additions & 11 deletions XPLM/DataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public sealed class DataRef
return i == 0 ? null : new(i);
}

public static DataRef? Find(string name) => Find(Encoding.UTF8.GetBytes(name));
public static DataRef? Find(string name) => Find(Encoding.ASCII.GetBytes(name));

internal readonly nint _id;

Expand Down Expand Up @@ -369,7 +369,7 @@ public static DataRefRegistration Register<T>(ReadOnlySpan<byte> name, bool isWr
new(name, DataTypes.Data, isWritable, accessor);

public static DataRefRegistration Register<T>(string name, bool isWritable, Accessor<T> accessor) where T : unmanaged =>
new(Encoding.UTF8.GetBytes(name), DataTypes.Data, isWritable, accessor);
new(Encoding.ASCII.GetBytes(name), DataTypes.Data, isWritable, accessor);

public DataRef DataRef { get; }

Expand Down Expand Up @@ -486,31 +486,31 @@ static void WriteByteVector(nint handle, byte* values, int offset, int count) =>
}

public DataRefRegistration(string name, bool isWritable, IntAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), isWritable, accessor)
{ }

public DataRefRegistration(string name, bool isWritable, FloatAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), isWritable, accessor)
{ }

public DataRefRegistration(string name, bool isWritable, DoubleAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), isWritable, accessor)
{ }

public DataRefRegistration(string name, bool isWritable, IntArrayAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), isWritable, accessor)
{ }

public DataRefRegistration(string name, bool isWritable, FloatArrayAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), isWritable, accessor)
{ }

public DataRefRegistration(string name, bool isWritable, DataAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), isWritable, accessor)
{ }

public unsafe DataRefRegistration(string name, DataTypes type, bool isWritable, IAccessor accessor) :
this(Encoding.UTF8.GetBytes(name), type, isWritable, accessor)
this(Encoding.ASCII.GetBytes(name), type, isWritable, accessor)
{ }

~DataRefRegistration() => Dispose();
Expand All @@ -536,7 +536,7 @@ public sealed class Shared : IDisposable
public static unsafe bool TryShare(string name, DataTypes type, Action notification, out Shared share)
{
[DllImport(Defs.Lib)]
static extern unsafe int XPLMShareData([MarshalAs(UnmanagedType.LPUTF8Str)] string dataName, DataTypes dataType, delegate* unmanaged<nint, void> notification, nint handle);
static extern unsafe int XPLMShareData([MarshalAs(UnmanagedType.LPStr)] string dataName, DataTypes dataType, delegate* unmanaged<nint, void> notification, nint handle);

var h = GCHandle.Alloc(notification);
if (XPLMShareData(name, type, &Notify, GCHandle.ToIntPtr(h)) == 0)
Expand Down Expand Up @@ -565,7 +565,7 @@ public unsafe void Dispose()
if (!_disposed)
{
[DllImport(Defs.Lib)]
static extern unsafe void XPLMUnshareData([MarshalAs(UnmanagedType.LPUTF8Str)] string dataName, DataTypes dataType, delegate* unmanaged<nint, void> notification, nint handle);
static extern unsafe void XPLMUnshareData([MarshalAs(UnmanagedType.LPStr)] string dataName, DataTypes dataType, delegate* unmanaged<nint, void> notification, nint handle);

XPLMUnshareData(Name, Type, &Notify, GCHandle.ToIntPtr(_handle));
_handle.Free();
Expand Down
12 changes: 6 additions & 6 deletions XPLM/Program.cs.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
private static XPluginBase? _plugin;

[UnmanagedCallersOnly(EntryPoint = "XPluginStart")]
private static int Start(ref byte name, ref byte signature, ref byte description)
private static unsafe int Start(byte* name, byte* signature, byte* description)
{
#if DEBUG
Utilities.ErrorCallback = message =>
Expand All @@ -24,17 +24,17 @@
};
#endif

static void StrCpy(ref byte u, string value)
static void StrCpy(byte* u, string value)
{
var s = MemoryMarshal.CreateSpan(ref u, 256);
Span<byte> s = new(u, 256);
s[Encoding.UTF8.GetBytes(value, s[..^1])] = 0;
}
try
{
_plugin = new $RootNamespace$.XPlugin();
StrCpy(ref name, _plugin.Name ?? "$AssemblyName$");
StrCpy(ref signature, _plugin.Signature ?? "$RootNamespace$");
StrCpy(ref description, _plugin.Description ?? "Built with FlyByWireless.XPLM");
StrCpy(name, _plugin.Name ?? "$AssemblyName$");
StrCpy(signature, _plugin.Signature ?? "$RootNamespace$");
StrCpy(description, _plugin.Description ?? "Built with FlyByWireless.XPLM");
return 1;
}
catch (Exception ex)
Expand Down
5 changes: 3 additions & 2 deletions XPLM/XPLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<AssemblyName>FlyByWireless.XPLM</AssemblyName>
<Version>1.0.7-alpha</Version>
<Version>1.0.8-beta</Version>
<Authors>WONG Tin Chi Timothy</Authors>
<Company>Timmy Net Net Company</Company>
<Product>P/Invoke for X-Plane plugin library manager</Product>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Copyright>(C) 2021 WONG Tin Chi Timothy. All rights reserved.</Copyright>
<Copyright>(C) 2021-2022 WONG Tin Chi Timothy. All rights reserved.</Copyright>
<PackageProjectUrl>https://github.com/wegylexy/XPLM</PackageProjectUrl>
<RepositoryUrl>https://github.com/wegylexy/XPLM</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand All @@ -17,6 +17,7 @@
<Nullable>enable</Nullable>
<RootNamespace>FlyByWireless.XPLM</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ImplicitUsings>enable</ImplicitUsings>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64</RuntimeIdentifiers>
<ExtrasBuildEachRuntimeIdentifier>true</ExtrasBuildEachRuntimeIdentifier>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
2 changes: 1 addition & 1 deletion refs/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.0-alpha</Version>
<Version>1.0.8-beta</Version>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down

0 comments on commit c4e9ad1

Please sign in to comment.