add c# binding including example

This commit is contained in:
Vinzenz Schroeter 2024-05-13 00:17:40 +02:00
parent 3bf5249d08
commit eb723701cf
20 changed files with 884 additions and 5 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
target
.idea
out
bin
obj

31
Cargo.lock generated
View file

@ -144,7 +144,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
"syn 2.0.63",
]
[[package]]
@ -168,6 +168,16 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "csbindgen"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf70eb656f35e0e6956cbde31c66431c53d8a546823489719099c71525767a9c"
dependencies = [
"regex",
"syn 1.0.109",
]
[[package]]
name = "env_filter"
version = "0.1.0"
@ -410,6 +420,14 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "servicepoint-binding-cs"
version = "0.1.0"
dependencies = [
"csbindgen",
"servicepoint2",
]
[[package]]
name = "servicepoint2"
version = "0.2.0"
@ -427,6 +445,17 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.63"

View file

@ -7,4 +7,5 @@ members = [
"examples/moving_line",
"examples/wiping_clear",
"examples/random_brightness",
"servicepoint2-binding-cs",
]

View file

@ -0,0 +1,19 @@
using ServicePoint2;
using var connection = Connection.Open("127.0.0.1:2342");
connection.Send(Command.Clear());
connection.Send(Command.Brightness(128));
using var pixels = PixelGrid.New(Constants.PixelWidth, Constants.PixelHeight);
for (var offset = 0; offset < int.MaxValue; offset++)
{
pixels.Fill(false);
for (var y = 0; y < pixels.Height; y++)
pixels[(y + offset) % Constants.PixelWidth, y] = true;
connection.Send(Command.BitmapLinearWin(0, 0, pixels.Clone()));
Thread.Sleep(14);
}

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>lang_cs</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\servicepoint2-binding-cs\ServicePoint2\ServicePoint2.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\target\debug\libservicepoint2.so" CopyToOutputDirectory="Always">
<Link>libservicepoint2.so</Link>
</Content>
</ItemGroup>
</Project>

View file

@ -0,0 +1,13 @@
[package]
name = "servicepoint-binding-cs"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
servicepoint2 = { path = "../servicepoint2" }
[build-dependencies]
csbindgen = "1.8.0"

View file

@ -0,0 +1,245 @@
// <auto-generated>
// This code is generated by csbindgen.
// DON'T CHANGE THIS DIRECTLY.
// </auto-generated>
#pragma warning disable CS8500
#pragma warning disable CS8981
using System;
using System.Runtime.InteropServices;
namespace ServicePoint2.BindGen
{
public static unsafe partial class NativeMethods
{
const string __DllName = "servicepoint2";
/// <summary>Creates a new `BitVec` instance. The returned instance has to be freed with `bit_vec_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern BitVec* sp2_bit_vec_new(nuint size);
/// <summary>Loads a `BitVec` from the provided data. The returned instance has to be freed with `bit_vec_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern BitVec* sp2_bit_vec_load(byte* data, nuint data_length);
/// <summary>Clones a `BitVec`. The returned instance has to be freed with `bit_vec_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern BitVec* sp2_bit_vec_clone(BitVec* @this);
/// <summary>Deallocates a `BitVec`. Note: do not call this if the grid has been consumed in another way, e.g. to create a command.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_bit_vec_dealloc(BitVec* @this);
/// <summary>Gets the value of a bit from the `BitVec`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool sp2_bit_vec_get(BitVec* @this, nuint index);
/// <summary>Sets the value of a bit in the `BitVec`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool sp2_bit_vec_set(BitVec* @this, nuint index, [MarshalAs(UnmanagedType.U1)] bool value);
/// <summary>Sets the value of all bits in the `BitVec`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_bit_vec_fill(BitVec* @this, [MarshalAs(UnmanagedType.U1)] bool value);
/// <summary>Gets the length of the `BitVec` in bits.</summary>
[DllImport(__DllName, EntryPoint = "sp2_bit_vec_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_bit_vec_len(BitVec* @this);
/// <summary>Creates a new `ByteGrid` instance. The returned instance has to be freed with `byte_grid_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ByteGrid* sp2_byte_grid_new(nuint width, nuint height);
/// <summary>Loads a `ByteGrid` with the specified dimensions from the provided data. The returned instance has to be freed with `byte_grid_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ByteGrid* sp2_byte_grid_load(nuint width, nuint height, byte* data, nuint data_length);
/// <summary>Clones a `ByteGrid`. The returned instance has to be freed with `byte_grid_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ByteGrid* sp2_byte_grid_clone(ByteGrid* @this);
/// <summary>Deallocates a `ByteGrid`. Note: do not call this if the grid has been consumed in another way, e.g. to create a command.</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_byte_grid_dealloc(ByteGrid* @this);
/// <summary>Get the current value at the specified position</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern byte sp2_byte_grid_get(ByteGrid* @this, nuint x, nuint y);
/// <summary>Sets the current value at the specified position</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_byte_grid_set(ByteGrid* @this, nuint x, nuint y, byte value);
/// <summary>Fills the whole `ByteGrid` with the specified value</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_byte_grid_fill(ByteGrid* @this, byte value);
/// <summary>Gets the width in pixels of the `ByteGrid` instance.</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_byte_grid_width(ByteGrid* @this);
/// <summary>Gets the height in pixels of the `ByteGrid` instance.</summary>
[DllImport(__DllName, EntryPoint = "sp2_byte_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_byte_grid_height(ByteGrid* @this);
/// <summary>Tries to load a `Command` from the passed array with the specified length. returns: NULL in case of an error, pointer to the allocated command otherwise</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_try_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_try_load(byte* data, nuint length);
/// <summary>Clones a `Command` instance</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_clone(Command* original);
/// <summary>Allocates a new `Command::Clear` instance</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_clear();
/// <summary>Allocates a new `Command::HardReset` instance</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_hard_reset", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_hard_reset();
/// <summary>Allocates a new `Command::FadeOut` instance</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_fade_out", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_fade_out();
/// <summary>Allocates a new `Command::Brightness` instance</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_brightness(byte brightness);
/// <summary>Allocates a new `Command::CharBrightness` instance. The passed `ByteGrid` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_char_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_char_brightness(ushort x, ushort y, ByteGrid* byte_grid);
/// <summary>Allocates a new `Command::BitmapLinear` instance. The passed `BitVec` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_bitmap_linear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_bitmap_linear(ushort offset, BitVec* bit_vec, CompressionCode compression);
/// <summary>Allocates a new `Command::BitmapLinearAnd` instance. The passed `BitVec` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_bitmap_linear_and", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_bitmap_linear_and(ushort offset, BitVec* bit_vec, CompressionCode compression);
/// <summary>Allocates a new `Command::BitmapLinearOr` instance. The passed `BitVec` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_bitmap_linear_or", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_bitmap_linear_or(ushort offset, BitVec* bit_vec, CompressionCode compression);
/// <summary>Allocates a new `Command::BitmapLinearXor` instance. The passed `BitVec` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_bitmap_linear_xor", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_bitmap_linear_xor(ushort offset, BitVec* bit_vec, CompressionCode compression);
/// <summary>Allocates a new `Command::Cp437Data` instance. The passed `ByteGrid` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_cp437_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_cp437_data(ushort x, ushort y, ByteGrid* byte_grid);
/// <summary>Allocates a new `Command::BitmapLinearWin` instance. The passed `PixelGrid` gets deallocated in the process.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_bitmap_linear_win", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp2_command_bitmap_linear_win(ushort x, ushort y, PixelGrid* byte_grid);
/// <summary>Deallocates a `Command`. Note that connection_send does this implicitly, so you only need to do this if you use the library for parsing commands.</summary>
[DllImport(__DllName, EntryPoint = "sp2_command_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_command_dealloc(Command* ptr);
/// <summary>Creates a new instance of Connection. The returned instance has to be deallocated with `connection_dealloc`. returns: NULL if connection fails or connected instance Panics: bad string encoding</summary>
[DllImport(__DllName, EntryPoint = "sp2_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Connection* sp2_connection_open(byte* host);
/// <summary>Sends the command instance. The instance is consumed / destroyed and cannot be used after this call.</summary>
[DllImport(__DllName, EntryPoint = "sp2_connection_send", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool sp2_connection_send(Connection* connection, Command* command_ptr);
/// <summary>Closes and deallocates a connection instance</summary>
[DllImport(__DllName, EntryPoint = "sp2_connection_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_connection_dealloc(Connection* ptr);
/// <summary>Creates a new `PixelGrid` instance. The returned instance has to be freed with `pixel_grid_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern PixelGrid* sp2_pixel_grid_new(nuint width, nuint height);
/// <summary>Loads a `PixelGrid` with the specified dimensions from the provided data. The returned instance has to be freed with `pixel_grid_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern PixelGrid* sp2_pixel_grid_load(nuint width, nuint height, byte* data, nuint data_length);
/// <summary>Clones a `PixelGrid`. The returned instance has to be freed with `pixel_grid_dealloc`.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern PixelGrid* sp2_pixel_grid_clone(PixelGrid* @this);
/// <summary>Deallocates a `PixelGrid`. Note: do not call this if the grid has been consumed in another way, e.g. to create a command.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_pixel_grid_dealloc(PixelGrid* @this);
/// <summary>Get the current value at the specified position</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool sp2_pixel_grid_get(PixelGrid* @this, nuint x, nuint y);
/// <summary>Sets the current value at the specified position</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_pixel_grid_set(PixelGrid* @this, nuint x, nuint y, [MarshalAs(UnmanagedType.U1)] bool value);
/// <summary>Fills the whole `PixelGrid` with the specified value</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp2_pixel_grid_fill(PixelGrid* @this, [MarshalAs(UnmanagedType.U1)] bool value);
/// <summary>Gets the width in pixels of the `PixelGrid` instance.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_pixel_grid_width(PixelGrid* @this);
/// <summary>Gets the height in pixels of the `PixelGrid` instance.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_pixel_grid_height(PixelGrid* @this);
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct BitVec
{
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct ByteGrid
{
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Connection
{
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct PixelGrid
{
}
public enum Command
{
Clear,
HardReset,
FadeOut,
CharBrightness,
Brightness,
BitmapLegacy,
BitmapLinear,
BitmapLinearAnd,
BitmapLinearOr,
BitmapLinearXor,
Cp437Data,
BitmapLinearWin,
}
public enum CompressionCode : ushort
{
Uncompressed = 0,
Gz = 26490,
Bz = 25210,
Lz = 27770,
Zs = 31347,
}
}

View file

@ -0,0 +1,79 @@
using ServicePoint2.BindGen;
namespace ServicePoint2;
public sealed class BitVec : Sp2NativeInstance<BindGen.BitVec>
{
public static BitVec New(int size)
{
unsafe
{
return new BitVec(NativeMethods.sp2_bit_vec_new((nuint)size));
}
}
public static BitVec Load(Span<byte> bytes)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
return new BitVec(NativeMethods.sp2_bit_vec_load(bytesPtr, (nuint)bytes.Length));
}
}
}
public BitVec Clone()
{
unsafe
{
return new BitVec(NativeMethods.sp2_bit_vec_clone(Instance));
}
}
public bool this[int index]
{
get
{
unsafe
{
return NativeMethods.sp2_bit_vec_get(Instance, (nuint)index);
}
}
set
{
unsafe
{
NativeMethods.sp2_bit_vec_set(Instance, (nuint)index, value);
}
}
}
public void Fill(bool value)
{
unsafe
{
NativeMethods.sp2_bit_vec_fill(Instance, value);
}
}
public int Length
{
get
{
unsafe
{
return (int)NativeMethods.sp2_bit_vec_len(Instance);
}
}
}
private unsafe BitVec(BindGen.BitVec* instance) : base(instance)
{
}
protected override unsafe void Dealloc()
{
NativeMethods.sp2_bit_vec_dealloc(Instance);
}
}

View file

@ -0,0 +1,91 @@
using ServicePoint2.BindGen;
namespace ServicePoint2;
public sealed class ByteGrid : Sp2NativeInstance<BindGen.ByteGrid>
{
public static ByteGrid New(int width, int height)
{
unsafe
{
return new ByteGrid(NativeMethods.sp2_byte_grid_new((nuint)width, (nuint)height));
}
}
public static ByteGrid Load(int width, int height, Span<byte> bytes)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
return new ByteGrid(NativeMethods.sp2_byte_grid_load((nuint)width, (nuint)height, bytesPtr,
(nuint)bytes.Length));
}
}
}
public ByteGrid Clone()
{
unsafe
{
return new ByteGrid(NativeMethods.sp2_byte_grid_clone(Instance));
}
}
public byte this[int x, int y]
{
get
{
unsafe
{
return NativeMethods.sp2_byte_grid_get(Instance, (nuint)x, (nuint)y);
}
}
set
{
unsafe
{
NativeMethods.sp2_byte_grid_set(Instance, (nuint)x, (nuint)y, value);
}
}
}
public void Fill(byte value)
{
unsafe
{
NativeMethods.sp2_byte_grid_fill(Instance, value);
}
}
public int Width
{
get
{
unsafe
{
return (int)NativeMethods.sp2_byte_grid_width(Instance);
}
}
}
public int Height
{
get
{
unsafe
{
return (int)NativeMethods.sp2_byte_grid_height(Instance);
}
}
}
private unsafe ByteGrid(BindGen.ByteGrid* instance) : base(instance)
{
}
protected override unsafe void Dealloc()
{
NativeMethods.sp2_byte_grid_dealloc(Instance);
}
}

View file

@ -0,0 +1,131 @@
using System.Diagnostics.CodeAnalysis;
using ServicePoint2.BindGen;
namespace ServicePoint2;
public sealed class Command : Sp2NativeInstance<BindGen.Command>
{
public Command Clone()
{
unsafe
{
return new Command(NativeMethods.sp2_command_clone(Instance));
}
}
public static bool TryLoad(Span<byte> bytes, [MaybeNullWhen(false)] out Command command)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
var instance = NativeMethods.sp2_command_try_load(bytesPtr, (nuint)bytes.Length);
command = instance == null
? null
: new Command(instance);
return command != null;
}
}
}
public static Command Clear()
{
unsafe
{
return new Command(NativeMethods.sp2_command_clear());
}
}
public static Command HardReset()
{
unsafe
{
return new Command(NativeMethods.sp2_command_hard_reset());
}
}
public static Command FadeOut()
{
unsafe
{
return new Command(NativeMethods.sp2_command_fade_out());
}
}
public static Command Brightness(byte brightness)
{
unsafe
{
return new Command(NativeMethods.sp2_command_brightness(brightness));
}
}
public static Command CharBrightness(int x, int y, ByteGrid grid)
{
unsafe
{
return new Command(NativeMethods.sp2_command_char_brightness((ushort)x, (ushort)y, grid.Into()));
}
}
public static Command BitmapLinear(int offset, BitVec bitVec, CompressionCode compressionCode)
{
unsafe
{
return new Command(
NativeMethods.sp2_command_bitmap_linear((ushort)offset, bitVec.Into(), compressionCode));
}
}
public static Command BitmapLinearAnd(int offset, BitVec bitVec, CompressionCode compressionCode)
{
unsafe
{
return new Command(
NativeMethods.sp2_command_bitmap_linear_and((ushort)offset, bitVec.Into(), compressionCode));
}
}
public static Command BitmapLinearOr(int offset, BitVec bitVec, CompressionCode compressionCode)
{
unsafe
{
return new Command(
NativeMethods.sp2_command_bitmap_linear_or((ushort)offset, bitVec.Into(), compressionCode));
}
}
public static Command BitmapLinearXor(int offset, BitVec bitVec, CompressionCode compressionCode)
{
unsafe
{
return new Command(
NativeMethods.sp2_command_bitmap_linear_xor((ushort)offset, bitVec.Into(), compressionCode));
}
}
public static Command BitmapLinearWin(int x, int y, PixelGrid pixelGrid)
{
unsafe
{
return new Command(NativeMethods.sp2_command_bitmap_linear_win((ushort)x, (ushort)y, pixelGrid.Into()));
}
}
public static Command Cp437Data(int x, int y, ByteGrid byteGrid)
{
unsafe
{
return new Command(NativeMethods.sp2_command_cp437_data((ushort)x, (ushort)y, byteGrid.Into()));
}
}
private unsafe Command(BindGen.Command* instance) : base(instance)
{
}
protected override unsafe void Dealloc()
{
NativeMethods.sp2_command_dealloc(Instance);
}
}

View file

@ -0,0 +1,35 @@
using System.Text;
using ServicePoint2.BindGen;
namespace ServicePoint2;
public sealed class Connection : Sp2NativeInstance<BindGen.Connection>
{
public static Connection Open(string host)
{
unsafe
{
fixed (byte* bytePtr = Encoding.UTF8.GetBytes(host))
{
return new Connection(NativeMethods.sp2_connection_open(bytePtr));
}
}
}
public bool Send(Command command)
{
unsafe
{
return NativeMethods.sp2_connection_send(Instance, command.Into());
}
}
protected override unsafe void Dealloc()
{
NativeMethods.sp2_connection_dealloc(Instance);
}
private unsafe Connection(BindGen.Connection* instance) : base(instance)
{
}
}

View file

@ -0,0 +1,22 @@
namespace ServicePoint2;
public static class Constants
{
/// size of a single tile in one dimension
public const int TileSize = 8;
/// tile count in the x-direction
public const int TileWidth = 56;
/// tile count in the y-direction
public const int TileHeight = 20;
/// screen width in pixels
public const int PixelWidth = TileWidth * TileSize;
/// screen height in pixels
public const int PixelHeight = TileHeight * TileSize;
/// pixel count on whole screen
public const int PixelCount = PixelWidth * PixelHeight;
}

View file

@ -0,0 +1,91 @@
using ServicePoint2.BindGen;
namespace ServicePoint2;
public sealed class PixelGrid : Sp2NativeInstance<BindGen.PixelGrid>
{
public static PixelGrid New(int width, int height)
{
unsafe
{
return new PixelGrid(NativeMethods.sp2_pixel_grid_new((nuint)width, (nuint)height));
}
}
public static PixelGrid Load(int width, int height, Span<byte> bytes)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
return new PixelGrid(NativeMethods.sp2_pixel_grid_load((nuint)width, (nuint)height, bytesPtr,
(nuint)bytes.Length));
}
}
}
public PixelGrid Clone()
{
unsafe
{
return new PixelGrid(NativeMethods.sp2_pixel_grid_clone(Instance));
}
}
public bool this[int x, int y]
{
get
{
unsafe
{
return NativeMethods.sp2_pixel_grid_get(Instance, (nuint)x, (nuint)y);
}
}
set
{
unsafe
{
NativeMethods.sp2_pixel_grid_set(Instance, (nuint)x, (nuint)y, value);
}
}
}
public void Fill(bool value)
{
unsafe
{
NativeMethods.sp2_pixel_grid_fill(Instance, value);
}
}
public int Width
{
get
{
unsafe
{
return (int)NativeMethods.sp2_pixel_grid_width(Instance);
}
}
}
public int Height
{
get
{
unsafe
{
return (int)NativeMethods.sp2_pixel_grid_height(Instance);
}
}
}
private unsafe PixelGrid(BindGen.PixelGrid* instance) : base(instance)
{
}
protected override unsafe void Dealloc()
{
NativeMethods.sp2_pixel_grid_dealloc(Instance);
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint2", "ServicePoint2.csproj", "{70EFFA3F-012A-4518-9627-466BEAE4252E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lang-cs", "..\..\examples\lang-cs\lang-cs.csproj", "{DA3B8B6E-993A-47DA-844B-F92AF520FF59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{70EFFA3F-012A-4518-9627-466BEAE4252E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70EFFA3F-012A-4518-9627-466BEAE4252E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70EFFA3F-012A-4518-9627-466BEAE4252E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70EFFA3F-012A-4518-9627-466BEAE4252E}.Release|Any CPU.Build.0 = Release|Any CPU
{DA3B8B6E-993A-47DA-844B-F92AF520FF59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA3B8B6E-993A-47DA-844B-F92AF520FF59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA3B8B6E-993A-47DA-844B-F92AF520FF59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA3B8B6E-993A-47DA-844B-F92AF520FF59}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,51 @@
namespace ServicePoint2;
public abstract class Sp2NativeInstance<T>
: IDisposable
where T : unmanaged
{
private unsafe T* _instance;
internal unsafe T* Instance
{
get
{
if (_instance == null)
throw new NullReferenceException("instance is null");
return _instance;
}
}
private protected unsafe Sp2NativeInstance(T* instance)
{
ArgumentNullException.ThrowIfNull(instance);
_instance = instance;
}
protected abstract void Dealloc();
internal unsafe T* Into()
{
var instance = _instance;
_instance = null;
return instance;
}
private unsafe void ReleaseUnmanagedResources()
{
if (_instance != null)
Dealloc();
_instance = null;
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~Sp2NativeInstance()
{
ReleaseUnmanagedResources();
}
}

View file

@ -0,0 +1,17 @@
fn main() {
println!("cargo:rerun-if-changed=DOESNOTEXIST"); // rebuild every time
csbindgen::Builder::default()
.input_extern_file("../servicepoint2/src/bit_vec.rs")
.input_extern_file("../servicepoint2/src/byte_grid.rs")
.input_extern_file("../servicepoint2/src/command.rs")
.input_extern_file("../servicepoint2/src/compression_code.rs")
.input_extern_file("../servicepoint2/src/connection.rs")
.input_extern_file("../servicepoint2/src/pixel_grid.rs")
.input_extern_file("../servicepoint2/src/lib.rs")
.csharp_dll_name("servicepoint2")
.csharp_namespace("ServicePoint2.BindGen")
.csharp_use_nint_types(true)
.csharp_class_accessibility("public")
.generate_csharp_file("ServicePoint2/BindGen/ServicePoint2.g.cs")
.unwrap();
}

View file

View file

@ -10,7 +10,7 @@ repository = "https://github.com/kaesaecracker/servicepoint"
readme = "../README.md"
[lib]
crate-type = ["staticlib", "rlib"]
crate-type = ["staticlib", "rlib", "cdylib"]
[dependencies]
log = "0.4"

View file

@ -61,7 +61,7 @@ impl Into<Vec<u8>> for ByteGrid {
#[cfg(feature = "c-api")]
pub mod c_api
{
use crate::{ByteGrid, PixelGrid};
use crate::ByteGrid;
/// Creates a new `ByteGrid` instance.
/// The returned instance has to be freed with `byte_grid_dealloc`.
@ -113,13 +113,13 @@ pub mod c_api
/// Gets the width in pixels of the `ByteGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_width(this: *const PixelGrid) -> usize {
pub unsafe extern "C" fn sp2_byte_grid_width(this: *const ByteGrid) -> usize {
(*this).width
}
/// Gets the height in pixels of the `ByteGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_byte_grid_height(this: *const PixelGrid) -> usize {
pub unsafe extern "C" fn sp2_byte_grid_height(this: *const ByteGrid) -> usize {
(*this).height
}
}