diff --git a/.gitignore b/.gitignore index 023f499..bec1c54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ target .idea out +bin +obj diff --git a/Cargo.lock b/Cargo.lock index 9862a35..d1631de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 558abf6..10d6087 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ members = [ "examples/moving_line", "examples/wiping_clear", "examples/random_brightness", + "servicepoint2-binding-cs", ] diff --git a/examples/lang-cs/Program.cs b/examples/lang-cs/Program.cs new file mode 100644 index 0000000..a150eb9 --- /dev/null +++ b/examples/lang-cs/Program.cs @@ -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); +} diff --git a/examples/lang-cs/lang-cs.csproj b/examples/lang-cs/lang-cs.csproj new file mode 100644 index 0000000..4dffb94 --- /dev/null +++ b/examples/lang-cs/lang-cs.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + lang_cs + enable + enable + + + + + + + + + libservicepoint2.so + + + + diff --git a/servicepoint2-binding-cs/Cargo.toml b/servicepoint2-binding-cs/Cargo.toml new file mode 100644 index 0000000..bd5d81b --- /dev/null +++ b/servicepoint2-binding-cs/Cargo.toml @@ -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" diff --git a/servicepoint2-binding-cs/ServicePoint2/BindGen/ServicePoint2.g.cs b/servicepoint2-binding-cs/ServicePoint2/BindGen/ServicePoint2.g.cs new file mode 100644 index 0000000..894b667 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/BindGen/ServicePoint2.g.cs @@ -0,0 +1,245 @@ +// +// This code is generated by csbindgen. +// DON'T CHANGE THIS DIRECTLY. +// +#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"; + + + + /// Creates a new `BitVec` instance. The returned instance has to be freed with `bit_vec_dealloc`. + [DllImport(__DllName, EntryPoint = "sp2_bit_vec_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern BitVec* sp2_bit_vec_new(nuint size); + + /// Loads a `BitVec` from the provided data. The returned instance has to be freed with `bit_vec_dealloc`. + [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); + + /// Clones a `BitVec`. The returned instance has to be freed with `bit_vec_dealloc`. + [DllImport(__DllName, EntryPoint = "sp2_bit_vec_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern BitVec* sp2_bit_vec_clone(BitVec* @this); + + /// Deallocates a `BitVec`. Note: do not call this if the grid has been consumed in another way, e.g. to create a command. + [DllImport(__DllName, EntryPoint = "sp2_bit_vec_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp2_bit_vec_dealloc(BitVec* @this); + + /// Gets the value of a bit from the `BitVec`. + [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); + + /// Sets the value of a bit in the `BitVec`. + [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); + + /// Sets the value of all bits in the `BitVec`. + [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); + + /// Gets the length of the `BitVec` in bits. + [DllImport(__DllName, EntryPoint = "sp2_bit_vec_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp2_bit_vec_len(BitVec* @this); + + /// Creates a new `ByteGrid` instance. The returned instance has to be freed with `byte_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp2_byte_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern ByteGrid* sp2_byte_grid_new(nuint width, nuint height); + + /// Loads a `ByteGrid` with the specified dimensions from the provided data. The returned instance has to be freed with `byte_grid_dealloc`. + [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); + + /// Clones a `ByteGrid`. The returned instance has to be freed with `byte_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp2_byte_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern ByteGrid* sp2_byte_grid_clone(ByteGrid* @this); + + /// Deallocates a `ByteGrid`. Note: do not call this if the grid has been consumed in another way, e.g. to create a command. + [DllImport(__DllName, EntryPoint = "sp2_byte_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp2_byte_grid_dealloc(ByteGrid* @this); + + /// Get the current value at the specified position + [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); + + /// Sets the current value at the specified position + [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); + + /// Fills the whole `ByteGrid` with the specified value + [DllImport(__DllName, EntryPoint = "sp2_byte_grid_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp2_byte_grid_fill(ByteGrid* @this, byte value); + + /// Gets the width in pixels of the `ByteGrid` instance. + [DllImport(__DllName, EntryPoint = "sp2_byte_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp2_byte_grid_width(ByteGrid* @this); + + /// Gets the height in pixels of the `ByteGrid` instance. + [DllImport(__DllName, EntryPoint = "sp2_byte_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp2_byte_grid_height(ByteGrid* @this); + + /// 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 + [DllImport(__DllName, EntryPoint = "sp2_command_try_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Command* sp2_command_try_load(byte* data, nuint length); + + /// Clones a `Command` instance + [DllImport(__DllName, EntryPoint = "sp2_command_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Command* sp2_command_clone(Command* original); + + /// Allocates a new `Command::Clear` instance + [DllImport(__DllName, EntryPoint = "sp2_command_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Command* sp2_command_clear(); + + /// Allocates a new `Command::HardReset` instance + [DllImport(__DllName, EntryPoint = "sp2_command_hard_reset", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Command* sp2_command_hard_reset(); + + /// Allocates a new `Command::FadeOut` instance + [DllImport(__DllName, EntryPoint = "sp2_command_fade_out", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Command* sp2_command_fade_out(); + + /// Allocates a new `Command::Brightness` instance + [DllImport(__DllName, EntryPoint = "sp2_command_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Command* sp2_command_brightness(byte brightness); + + /// Allocates a new `Command::CharBrightness` instance. The passed `ByteGrid` gets deallocated in the process. + [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); + + /// Allocates a new `Command::BitmapLinear` instance. The passed `BitVec` gets deallocated in the process. + [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); + + /// Allocates a new `Command::BitmapLinearAnd` instance. The passed `BitVec` gets deallocated in the process. + [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); + + /// Allocates a new `Command::BitmapLinearOr` instance. The passed `BitVec` gets deallocated in the process. + [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); + + /// Allocates a new `Command::BitmapLinearXor` instance. The passed `BitVec` gets deallocated in the process. + [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); + + /// Allocates a new `Command::Cp437Data` instance. The passed `ByteGrid` gets deallocated in the process. + [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); + + /// Allocates a new `Command::BitmapLinearWin` instance. The passed `PixelGrid` gets deallocated in the process. + [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); + + /// 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. + [DllImport(__DllName, EntryPoint = "sp2_command_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp2_command_dealloc(Command* ptr); + + /// 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 + [DllImport(__DllName, EntryPoint = "sp2_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Connection* sp2_connection_open(byte* host); + + /// Sends the command instance. The instance is consumed / destroyed and cannot be used after this call. + [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); + + /// Closes and deallocates a connection instance + [DllImport(__DllName, EntryPoint = "sp2_connection_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp2_connection_dealloc(Connection* ptr); + + /// Creates a new `PixelGrid` instance. The returned instance has to be freed with `pixel_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp2_pixel_grid_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern PixelGrid* sp2_pixel_grid_new(nuint width, nuint height); + + /// Loads a `PixelGrid` with the specified dimensions from the provided data. The returned instance has to be freed with `pixel_grid_dealloc`. + [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); + + /// Clones a `PixelGrid`. The returned instance has to be freed with `pixel_grid_dealloc`. + [DllImport(__DllName, EntryPoint = "sp2_pixel_grid_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern PixelGrid* sp2_pixel_grid_clone(PixelGrid* @this); + + /// Deallocates a `PixelGrid`. Note: do not call this if the grid has been consumed in another way, e.g. to create a command. + [DllImport(__DllName, EntryPoint = "sp2_pixel_grid_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void sp2_pixel_grid_dealloc(PixelGrid* @this); + + /// Get the current value at the specified position + [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); + + /// Sets the current value at the specified position + [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); + + /// Fills the whole `PixelGrid` with the specified value + [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); + + /// Gets the width in pixels of the `PixelGrid` instance. + [DllImport(__DllName, EntryPoint = "sp2_pixel_grid_width", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern nuint sp2_pixel_grid_width(PixelGrid* @this); + + /// Gets the height in pixels of the `PixelGrid` instance. + [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, + } + + +} diff --git a/servicepoint2-binding-cs/ServicePoint2/BitVec.cs b/servicepoint2-binding-cs/ServicePoint2/BitVec.cs new file mode 100644 index 0000000..202a4b5 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/BitVec.cs @@ -0,0 +1,79 @@ +using ServicePoint2.BindGen; + +namespace ServicePoint2; + +public sealed class BitVec : Sp2NativeInstance +{ + public static BitVec New(int size) + { + unsafe + { + return new BitVec(NativeMethods.sp2_bit_vec_new((nuint)size)); + } + } + + public static BitVec Load(Span 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); + } +} diff --git a/servicepoint2-binding-cs/ServicePoint2/ByteGrid.cs b/servicepoint2-binding-cs/ServicePoint2/ByteGrid.cs new file mode 100644 index 0000000..4c023a8 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/ByteGrid.cs @@ -0,0 +1,91 @@ +using ServicePoint2.BindGen; + +namespace ServicePoint2; + +public sealed class ByteGrid : Sp2NativeInstance +{ + 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 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); + } +} diff --git a/servicepoint2-binding-cs/ServicePoint2/Command.cs b/servicepoint2-binding-cs/ServicePoint2/Command.cs new file mode 100644 index 0000000..73d502c --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/Command.cs @@ -0,0 +1,131 @@ +using System.Diagnostics.CodeAnalysis; +using ServicePoint2.BindGen; + +namespace ServicePoint2; + +public sealed class Command : Sp2NativeInstance +{ + public Command Clone() + { + unsafe + { + return new Command(NativeMethods.sp2_command_clone(Instance)); + } + } + + public static bool TryLoad(Span 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); + } +} diff --git a/servicepoint2-binding-cs/ServicePoint2/Connection.cs b/servicepoint2-binding-cs/ServicePoint2/Connection.cs new file mode 100644 index 0000000..58bb532 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/Connection.cs @@ -0,0 +1,35 @@ +using System.Text; +using ServicePoint2.BindGen; + +namespace ServicePoint2; + +public sealed class Connection : Sp2NativeInstance +{ + 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) + { + } +} diff --git a/servicepoint2-binding-cs/ServicePoint2/Constants.cs b/servicepoint2-binding-cs/ServicePoint2/Constants.cs new file mode 100644 index 0000000..bad08fd --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/Constants.cs @@ -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; +} diff --git a/servicepoint2-binding-cs/ServicePoint2/PixelGrid.cs b/servicepoint2-binding-cs/ServicePoint2/PixelGrid.cs new file mode 100644 index 0000000..9c202c7 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/PixelGrid.cs @@ -0,0 +1,91 @@ +using ServicePoint2.BindGen; + +namespace ServicePoint2; + +public sealed class PixelGrid : Sp2NativeInstance +{ + 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 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); + } +} diff --git a/servicepoint2-binding-cs/ServicePoint2/ServicePoint2.csproj b/servicepoint2-binding-cs/ServicePoint2/ServicePoint2.csproj new file mode 100644 index 0000000..4a24632 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/ServicePoint2.csproj @@ -0,0 +1,10 @@ + + + + net8.0 + enable + enable + true + + + diff --git a/servicepoint2-binding-cs/ServicePoint2/ServicePoint2.sln b/servicepoint2-binding-cs/ServicePoint2/ServicePoint2.sln new file mode 100644 index 0000000..eae4f49 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/ServicePoint2.sln @@ -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 diff --git a/servicepoint2-binding-cs/ServicePoint2/Sp2NativeInstance.cs b/servicepoint2-binding-cs/ServicePoint2/Sp2NativeInstance.cs new file mode 100644 index 0000000..3c44f28 --- /dev/null +++ b/servicepoint2-binding-cs/ServicePoint2/Sp2NativeInstance.cs @@ -0,0 +1,51 @@ +namespace ServicePoint2; + +public abstract class Sp2NativeInstance + : 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(); + } +} diff --git a/servicepoint2-binding-cs/build.rs b/servicepoint2-binding-cs/build.rs new file mode 100644 index 0000000..3629935 --- /dev/null +++ b/servicepoint2-binding-cs/build.rs @@ -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(); +} \ No newline at end of file diff --git a/servicepoint2-binding-cs/src/lib.rs b/servicepoint2-binding-cs/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/servicepoint2/Cargo.toml b/servicepoint2/Cargo.toml index 783663f..2e2eb4a 100644 --- a/servicepoint2/Cargo.toml +++ b/servicepoint2/Cargo.toml @@ -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" diff --git a/servicepoint2/src/byte_grid.rs b/servicepoint2/src/byte_grid.rs index a41468e..e8a986b 100644 --- a/servicepoint2/src/byte_grid.rs +++ b/servicepoint2/src/byte_grid.rs @@ -61,7 +61,7 @@ impl Into> 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 } } \ No newline at end of file