move c_api into binding_c crate, update C# binding
This commit is contained in:
parent
edcad6fd03
commit
f759395393
40 changed files with 895 additions and 916 deletions
|
@ -0,0 +1,289 @@
|
|||
// <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 ServicePoint.BindGen
|
||||
{
|
||||
public static unsafe partial class NativeMethods
|
||||
{
|
||||
const string __DllName = "servicepoint_binding_c";
|
||||
|
||||
public const nuint TILE_SIZE = 8;
|
||||
public const nuint TILE_WIDTH = 56;
|
||||
public const nuint TILE_HEIGHT = 20;
|
||||
|
||||
|
||||
/// <summary>Creates a new `BitVec` instance. The returned instance has to be freed with `bit_vec_dealloc`.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_bit_vec_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern BitVec* sp_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 = "sp_bit_vec_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern BitVec* sp_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 = "sp_bit_vec_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern BitVec* sp_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 = "sp_bit_vec_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_bit_vec_dealloc(BitVec* @this);
|
||||
|
||||
/// <summary>Gets the value of a bit from the `BitVec`.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_bit_vec_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
[return: MarshalAs(UnmanagedType.U1)]
|
||||
public static extern bool sp_bit_vec_get(BitVec* @this, nuint index);
|
||||
|
||||
/// <summary>Sets the value of a bit in the `BitVec`.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_bit_vec_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
[return: MarshalAs(UnmanagedType.U1)]
|
||||
public static extern bool sp_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 = "sp_bit_vec_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_bit_vec_fill(BitVec* @this, [MarshalAs(UnmanagedType.U1)] bool value);
|
||||
|
||||
/// <summary>Gets the length of the `BitVec` in bits.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_bit_vec_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern nuint sp_bit_vec_len(BitVec* @this);
|
||||
|
||||
/// <summary>Returns true if length is 0.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_bit_vec_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
[return: MarshalAs(UnmanagedType.U1)]
|
||||
public static extern bool sp_bit_vec_is_empty(BitVec* @this);
|
||||
|
||||
/// <summary>Gets an unsafe reference to the data of the `BitVec` instance. ## Safety The caller has to make sure to never access the returned memory after the `BitVec` instance has been consumed or manually deallocated. Reading and writing concurrently to either the original instance or the returned data will result in undefined behavior.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_bit_vec_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern CByteSlice sp_bit_vec_unsafe_data_ref(BitVec* @this);
|
||||
|
||||
/// <summary>Creates a new `ByteGrid` instance. The returned instance has to be freed with `byte_grid_dealloc`.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern ByteGrid* sp_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 = "sp_byte_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern ByteGrid* sp_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 = "sp_byte_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern ByteGrid* sp_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 = "sp_byte_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_byte_grid_dealloc(ByteGrid* @this);
|
||||
|
||||
/// <summary>Get the current value at the specified position</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern byte sp_byte_grid_get(ByteGrid* @this, nuint x, nuint y);
|
||||
|
||||
/// <summary>Sets the current value at the specified position</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_byte_grid_set(ByteGrid* @this, nuint x, nuint y, byte value);
|
||||
|
||||
/// <summary>Fills the whole `ByteGrid` with the specified value</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_byte_grid_fill(ByteGrid* @this, byte value);
|
||||
|
||||
/// <summary>Gets the width in pixels of the `ByteGrid` instance.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern nuint sp_byte_grid_width(ByteGrid* @this);
|
||||
|
||||
/// <summary>Gets the height in pixels of the `ByteGrid` instance.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern nuint sp_byte_grid_height(ByteGrid* @this);
|
||||
|
||||
/// <summary>Gets an unsafe reference to the data of the `ByteGrid` instance. ## Safety The caller has to make sure to never access the returned memory after the `ByteGrid` instance has been consumed or manually deallocated. Reading and writing concurrently to either the original instance or the returned data will result in undefined behavior.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_byte_grid_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern CByteSlice sp_byte_grid_unsafe_data_ref(ByteGrid* @this);
|
||||
|
||||
/// <summary>Tries to turn a `Packet` into a `Command`. The packet is gets deallocated in the process. Returns: pointer to command or NULL</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_try_from_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_try_from_packet(Packet* packet);
|
||||
|
||||
/// <summary>Clones a `Command` instance</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_clone(Command* original);
|
||||
|
||||
/// <summary>Allocates a new `Command::Clear` instance</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_clear();
|
||||
|
||||
/// <summary>Allocates a new `Command::HardReset` instance</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_hard_reset", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_hard_reset();
|
||||
|
||||
/// <summary>Allocates a new `Command::FadeOut` instance</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_fade_out", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_fade_out();
|
||||
|
||||
/// <summary>Allocates a new `Command::Brightness` instance</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_brightness(byte brightness);
|
||||
|
||||
/// <summary>Allocates a new `Command::CharBrightness` instance. The passed `ByteGrid` gets deallocated in the process.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_char_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_char_brightness(nuint x, nuint y, ByteGrid* byte_grid);
|
||||
|
||||
/// <summary>Allocates a new `Command::BitmapLinear` instance. The passed `BitVec` gets deallocated in the process.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_bitmap_linear(nuint 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 = "sp_command_bitmap_linear_and", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_bitmap_linear_and(nuint 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 = "sp_command_bitmap_linear_or", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_bitmap_linear_or(nuint 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 = "sp_command_bitmap_linear_xor", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_bitmap_linear_xor(nuint 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 = "sp_command_cp437_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_cp437_data(nuint x, nuint y, ByteGrid* byte_grid);
|
||||
|
||||
/// <summary>Allocates a new `Command::BitmapLinearWin` instance. The passed `PixelGrid` gets deallocated in the process.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear_win", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Command* sp_command_bitmap_linear_win(nuint x, nuint y, PixelGrid* byte_grid, CompressionCode compression_code);
|
||||
|
||||
/// <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 = "sp_command_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_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 = "sp_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Connection* sp_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 = "sp_connection_send", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
[return: MarshalAs(UnmanagedType.U1)]
|
||||
public static extern bool sp_connection_send(Connection* connection, Packet* command_ptr);
|
||||
|
||||
/// <summary>Closes and deallocates a connection instance</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_connection_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_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 = "sp_pixel_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern PixelGrid* sp_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 = "sp_pixel_grid_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern PixelGrid* sp_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 = "sp_pixel_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern PixelGrid* sp_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 = "sp_pixel_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_pixel_grid_dealloc(PixelGrid* @this);
|
||||
|
||||
/// <summary>Get the current value at the specified position</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_pixel_grid_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
[return: MarshalAs(UnmanagedType.U1)]
|
||||
public static extern bool sp_pixel_grid_get(PixelGrid* @this, nuint x, nuint y);
|
||||
|
||||
/// <summary>Sets the current value at the specified position</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_pixel_grid_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_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 = "sp_pixel_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_pixel_grid_fill(PixelGrid* @this, [MarshalAs(UnmanagedType.U1)] bool value);
|
||||
|
||||
/// <summary>Gets the width in pixels of the `PixelGrid` instance.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_pixel_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern nuint sp_pixel_grid_width(PixelGrid* @this);
|
||||
|
||||
/// <summary>Gets the height in pixels of the `PixelGrid` instance.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_pixel_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern nuint sp_pixel_grid_height(PixelGrid* @this);
|
||||
|
||||
/// <summary>Gets an unsafe reference to the data of the `PixelGrid` instance. ## Safety The caller has to make sure to never access the returned memory after the `PixelGrid` instance has been consumed or manually deallocated. Reading and writing concurrently to either the original instance or the returned data will result in undefined behavior.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_pixel_grid_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern CByteSlice sp_pixel_grid_unsafe_data_ref(PixelGrid* @this);
|
||||
|
||||
/// <summary>Turns a `Command` into a `Packet`. The command gets deallocated in the process.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_packet_from_command", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Packet* sp_packet_from_command(Command* command);
|
||||
|
||||
/// <summary>Tries to load a `Packet` from the passed array with the specified length. returns: NULL in case of an error, pointer to the allocated packet otherwise</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_packet_try_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern Packet* sp_packet_try_load(byte* data, nuint length);
|
||||
|
||||
/// <summary>Deallocates a `Packet`. Note: do not call this if the instance has been consumed in another way, e.g. by sending it.</summary>
|
||||
[DllImport(__DllName, EntryPoint = "sp_packet_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
public static extern void sp_packet_dealloc(Packet* @this);
|
||||
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe partial struct CByteSlice
|
||||
{
|
||||
public byte* start;
|
||||
public nuint length;
|
||||
}
|
||||
|
||||
[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
|
||||
{
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe partial struct Packet
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public enum Command
|
||||
{
|
||||
Clear,
|
||||
HardReset,
|
||||
FadeOut,
|
||||
CharBrightness,
|
||||
Brightness,
|
||||
BitmapLegacy,
|
||||
BitmapLinear,
|
||||
BitmapLinearAnd,
|
||||
BitmapLinearOr,
|
||||
BitmapLinearXor,
|
||||
Cp437Data,
|
||||
BitmapLinearWin,
|
||||
}
|
||||
|
||||
public enum CompressionCode : ushort
|
||||
{
|
||||
Uncompressed = 0,
|
||||
Zlib = 26490,
|
||||
Bzip2 = 25210,
|
||||
Lzma = 27770,
|
||||
Zstd = 31347,
|
||||
}
|
||||
|
||||
|
||||
}
|
91
crates/servicepoint_binding_cs/ServicePoint/BitVec.cs
Normal file
91
crates/servicepoint_binding_cs/ServicePoint/BitVec.cs
Normal file
|
@ -0,0 +1,91 @@
|
|||
using ServicePoint.BindGen;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public sealed class BitVec : SpNativeInstance<BindGen.BitVec>
|
||||
{
|
||||
public static BitVec New(int size)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new BitVec(NativeMethods.sp_bit_vec_new((nuint)size));
|
||||
}
|
||||
}
|
||||
|
||||
public static BitVec Load(Span<byte> bytes)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* bytesPtr = bytes)
|
||||
{
|
||||
return new BitVec(NativeMethods.sp_bit_vec_load(bytesPtr, (nuint)bytes.Length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BitVec Clone()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new BitVec(NativeMethods.sp_bit_vec_clone(Instance));
|
||||
}
|
||||
}
|
||||
|
||||
public bool this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return NativeMethods.sp_bit_vec_get(Instance, (nuint)index);
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
NativeMethods.sp_bit_vec_set(Instance, (nuint)index, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Fill(bool value)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
NativeMethods.sp_bit_vec_fill(Instance, value);
|
||||
}
|
||||
}
|
||||
|
||||
public int Length
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return (int)NativeMethods.sp_bit_vec_len(Instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Span<byte> Data
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var slice = NativeMethods.sp_bit_vec_unsafe_data_ref(Instance);
|
||||
return new Span<byte>(slice.start, (int)slice.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe BitVec(BindGen.BitVec* instance) : base(instance)
|
||||
{
|
||||
}
|
||||
|
||||
private protected override unsafe void Dealloc()
|
||||
{
|
||||
NativeMethods.sp_bit_vec_dealloc(Instance);
|
||||
}
|
||||
}
|
134
crates/servicepoint_binding_cs/ServicePoint/ByteGrid.cs
Normal file
134
crates/servicepoint_binding_cs/ServicePoint/ByteGrid.cs
Normal file
|
@ -0,0 +1,134 @@
|
|||
using System.Text;
|
||||
using ServicePoint.BindGen;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public sealed class ByteGrid : SpNativeInstance<BindGen.ByteGrid>
|
||||
{
|
||||
public static ByteGrid New(int width, int height)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new ByteGrid(NativeMethods.sp_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.sp_byte_grid_load((nuint)width, (nuint)height, bytesPtr,
|
||||
(nuint)bytes.Length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ByteGrid Clone()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new ByteGrid(NativeMethods.sp_byte_grid_clone(Instance));
|
||||
}
|
||||
}
|
||||
|
||||
public byte this[int x, int y]
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return NativeMethods.sp_byte_grid_get(Instance, (nuint)x, (nuint)y);
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
NativeMethods.sp_byte_grid_set(Instance, (nuint)x, (nuint)y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string this[int y]
|
||||
{
|
||||
set
|
||||
{
|
||||
var width = Width;
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Length, width);
|
||||
|
||||
var x = 0;
|
||||
for (; x < value.Length; x++)
|
||||
this[x, y] = (byte)value[x];
|
||||
|
||||
for (; x < width; x++)
|
||||
this[x, y] = 0;
|
||||
}
|
||||
|
||||
get
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
for (int x = 0; x < Width; x++)
|
||||
{
|
||||
var val = this[x, y];
|
||||
if (val == 0)
|
||||
break;
|
||||
sb.Append((char)val);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public void Fill(byte value)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
NativeMethods.sp_byte_grid_fill(Instance, value);
|
||||
}
|
||||
}
|
||||
|
||||
public int Width
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return (int)NativeMethods.sp_byte_grid_width(Instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Height
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return (int)NativeMethods.sp_byte_grid_height(Instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Span<byte> Data
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var slice = NativeMethods.sp_byte_grid_unsafe_data_ref(Instance);
|
||||
return new Span<byte>(slice.start, (int)slice.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe ByteGrid(BindGen.ByteGrid* instance) : base(instance)
|
||||
{
|
||||
}
|
||||
|
||||
private protected override unsafe void Dealloc()
|
||||
{
|
||||
NativeMethods.sp_byte_grid_dealloc(Instance);
|
||||
}
|
||||
}
|
132
crates/servicepoint_binding_cs/ServicePoint/Command.cs
Normal file
132
crates/servicepoint_binding_cs/ServicePoint/Command.cs
Normal file
|
@ -0,0 +1,132 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using ServicePoint.BindGen;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public sealed class Command : SpNativeInstance<BindGen.Command>
|
||||
{
|
||||
public static bool TryFromPacket(Packet packet, [MaybeNullWhen(false)] out Command command)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var result = NativeMethods.sp_command_try_from_packet(packet.Into());
|
||||
if (result == null)
|
||||
{
|
||||
command = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
command = new Command(result);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public Command Clone()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_clone(Instance));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command Clear()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_clear());
|
||||
}
|
||||
}
|
||||
|
||||
public static Command HardReset()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_hard_reset());
|
||||
}
|
||||
}
|
||||
|
||||
public static Command FadeOut()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_fade_out());
|
||||
}
|
||||
}
|
||||
|
||||
public static Command Brightness(byte brightness)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_brightness(brightness));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command CharBrightness(int x, int y, ByteGrid grid)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_char_brightness((ushort)x, (ushort)y, grid.Into()));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command BitmapLinear(int offset, BitVec bitVec, CompressionCode compressionCode)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(
|
||||
NativeMethods.sp_command_bitmap_linear((ushort)offset, bitVec.Into(), compressionCode));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command BitmapLinearAnd(int offset, BitVec bitVec, CompressionCode compressionCode)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(
|
||||
NativeMethods.sp_command_bitmap_linear_and((ushort)offset, bitVec.Into(), compressionCode));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command BitmapLinearOr(int offset, BitVec bitVec, CompressionCode compressionCode)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(
|
||||
NativeMethods.sp_command_bitmap_linear_or((ushort)offset, bitVec.Into(), compressionCode));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command BitmapLinearXor(int offset, BitVec bitVec, CompressionCode compressionCode)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(
|
||||
NativeMethods.sp_command_bitmap_linear_xor((ushort)offset, bitVec.Into(), compressionCode));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command BitmapLinearWin(int x, int y, PixelGrid pixelGrid, CompressionCode compression)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_bitmap_linear_win((ushort)x, (ushort)y, pixelGrid.Into(), compression));
|
||||
}
|
||||
}
|
||||
|
||||
public static Command Cp437Data(int x, int y, ByteGrid byteGrid)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Command(NativeMethods.sp_command_cp437_data((ushort)x, (ushort)y, byteGrid.Into()));
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe Command(BindGen.Command* instance) : base(instance)
|
||||
{
|
||||
}
|
||||
|
||||
private protected override unsafe void Dealloc()
|
||||
{
|
||||
NativeMethods.sp_command_dealloc(Instance);
|
||||
}
|
||||
}
|
35
crates/servicepoint_binding_cs/ServicePoint/Connection.cs
Normal file
35
crates/servicepoint_binding_cs/ServicePoint/Connection.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using System.Text;
|
||||
using ServicePoint.BindGen;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public sealed class Connection : SpNativeInstance<BindGen.Connection>
|
||||
{
|
||||
public static Connection Open(string host)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* bytePtr = Encoding.UTF8.GetBytes(host))
|
||||
{
|
||||
return new Connection(NativeMethods.sp_connection_open(bytePtr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool Send(Packet packet)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return NativeMethods.sp_connection_send(Instance, packet.Into());
|
||||
}
|
||||
}
|
||||
|
||||
private protected override unsafe void Dealloc()
|
||||
{
|
||||
NativeMethods.sp_connection_dealloc(Instance);
|
||||
}
|
||||
|
||||
private unsafe Connection(BindGen.Connection* instance) : base(instance)
|
||||
{
|
||||
}
|
||||
}
|
22
crates/servicepoint_binding_cs/ServicePoint/Constants.cs
Normal file
22
crates/servicepoint_binding_cs/ServicePoint/Constants.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
namespace ServicePoint;
|
||||
|
||||
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;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
global using System;
|
39
crates/servicepoint_binding_cs/ServicePoint/Packet.cs
Normal file
39
crates/servicepoint_binding_cs/ServicePoint/Packet.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using ServicePoint.BindGen;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public sealed class Packet : SpNativeInstance<BindGen.Packet>
|
||||
{
|
||||
public static Packet FromCommand(Command command)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new Packet(NativeMethods.sp_packet_from_command(command.Into()));
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryFromBytes(Span<byte> bytes, [MaybeNullWhen(false)] out Packet packet)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* bytesPtr = bytes)
|
||||
{
|
||||
var instance = NativeMethods.sp_packet_try_load(bytesPtr, (nuint)bytes.Length);
|
||||
packet = instance == null
|
||||
? null
|
||||
: new Packet(instance);
|
||||
return packet != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe Packet(BindGen.Packet* instance) : base(instance)
|
||||
{
|
||||
}
|
||||
|
||||
private protected override unsafe void Dealloc()
|
||||
{
|
||||
NativeMethods.sp_packet_dealloc(Instance);
|
||||
}
|
||||
}
|
103
crates/servicepoint_binding_cs/ServicePoint/PixelGrid.cs
Normal file
103
crates/servicepoint_binding_cs/ServicePoint/PixelGrid.cs
Normal file
|
@ -0,0 +1,103 @@
|
|||
using ServicePoint.BindGen;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public sealed class PixelGrid : SpNativeInstance<BindGen.PixelGrid>
|
||||
{
|
||||
public static PixelGrid New(int width, int height)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new PixelGrid(NativeMethods.sp_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.sp_pixel_grid_load((nuint)width, (nuint)height, bytesPtr,
|
||||
(nuint)bytes.Length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PixelGrid Clone()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return new PixelGrid(NativeMethods.sp_pixel_grid_clone(Instance));
|
||||
}
|
||||
}
|
||||
|
||||
public bool this[int x, int y]
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return NativeMethods.sp_pixel_grid_get(Instance, (nuint)x, (nuint)y);
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
NativeMethods.sp_pixel_grid_set(Instance, (nuint)x, (nuint)y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Fill(bool value)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
NativeMethods.sp_pixel_grid_fill(Instance, value);
|
||||
}
|
||||
}
|
||||
|
||||
public int Width
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return (int)NativeMethods.sp_pixel_grid_width(Instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Height
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return (int)NativeMethods.sp_pixel_grid_height(Instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Span<byte> Data
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var slice = NativeMethods.sp_pixel_grid_unsafe_data_ref(Instance);
|
||||
return new Span<byte>(slice.start, (int)slice.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe PixelGrid(BindGen.PixelGrid* instance) : base(instance)
|
||||
{
|
||||
}
|
||||
|
||||
private protected override unsafe void Dealloc()
|
||||
{
|
||||
NativeMethods.sp_pixel_grid_dealloc(Instance);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
||||
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<PackageId>ServicePoint</PackageId>
|
||||
<Version>0.3.0</Version>
|
||||
<Authors>Repository Authors</Authors>
|
||||
<Company>None</Company>
|
||||
<Product>ServicePoint</Product>
|
||||
<PackageTags>CCCB</PackageTags>
|
||||
<Description>
|
||||
C# bindings for the rust crate servicepoint. You will need a suitable native shared library to use this.
|
||||
For documentation, see the rust documentation: https://docs.rs/servicepoint/latest/servicepoint/.
|
||||
Note that this library is still in early development. Breaking changes are expected before 1.0 is released.
|
||||
</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- generate C# bindings -->
|
||||
<Target Name="BuildBindings" Condition="'$(Configuration)'=='Release'" BeforeTargets="Build">
|
||||
<Exec Command="cargo build --release"/>
|
||||
</Target>
|
||||
<Target Name="BuildBindings" Condition="'$(Configuration)'=='Debug'" BeforeTargets="Build">
|
||||
<Exec Command="cargo build"/>
|
||||
</Target>
|
||||
|
||||
<!-- build native library to include in output -->
|
||||
<Target Name="BuildLibrary" Condition="'$(Configuration)'=='Release'" BeforeTargets="Build">
|
||||
<Exec Command="cargo build --manifest-path ../../../crates/servicepoint/Cargo.toml --release"/>
|
||||
</Target>
|
||||
<Target Name="BuildLibrary" Condition="'$(Configuration)'=='Debug'" BeforeTargets="Build">
|
||||
<Exec Command="cargo build --manifest-path ../../../crates/servicepoint/Cargo.toml"/>
|
||||
</Target>
|
||||
|
||||
<!-- include native binary in output -->
|
||||
<ItemGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<Content Include="../../../target/debug/libservicepoint_binding_c.so" CopyToOutputDirectory="Always">
|
||||
<Link>libservicepoint_binding_c.so</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="'$(Configuration)'=='Release'">
|
||||
<Content Include="../../../target/release/libservicepoint_binding_c.so" CopyToOutputDirectory="Always">
|
||||
<Link>libservicepoint_binding_c.so</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- include link to source code at revision -->
|
||||
<None Include="../../../README.md" Pack="true" PackagePath="\"/>
|
||||
<!-- add README.md to package -->
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,16 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace ServicePoint;
|
||||
|
||||
public static class ServicePointExtensions
|
||||
{
|
||||
public static Packet IntoPacket(this Command command)
|
||||
{
|
||||
return Packet.FromCommand(command);
|
||||
}
|
||||
|
||||
public static bool TryIntoCommand(this Packet packet, [MaybeNullWhen(false)] out Command command)
|
||||
{
|
||||
return Command.TryFromPacket(packet, out command);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
namespace ServicePoint;
|
||||
|
||||
public abstract class SpNativeInstance<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 SpNativeInstance(T* instance)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(instance);
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
|
||||
~SpNativeInstance()
|
||||
{
|
||||
ReleaseUnmanagedResources();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue