diff options
| author | 1992414357@qq.com <1992414357@qq.com> | 2025-06-20 06:09:37 +0800 |
|---|---|---|
| committer | 1992414357@qq.com <1992414357@qq.com> | 2025-06-20 06:09:37 +0800 |
| commit | 60ebb5b4ec93ba94c3550babd6e927ca4b929ef2 (patch) | |
| tree | 84d7372b47baf3f2dfe69e8c4e0d9d911d1e56cb /core | |
| parent | ca2ebc3a39d210cb0a0e82b14342dde33b270398 (diff) | |
Update Csharp Bindings
Diffstat (limited to 'core')
15 files changed, 519 insertions, 20 deletions
diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/ControllerData.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/ControllerData.cs new file mode 100644 index 0000000..c2d6391 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/ControllerData.cs @@ -0,0 +1,19 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data; + +public class ControllerData : IRawData<FfiControllerData> +{ + private readonly FfiControllerData _ffi; + + public ControllerData(Player player) + { + _ffi = nogamepads_data.ControllerDataNew(); + nogamepads_data.ControllerDataBindPlayer(_ffi, player.GetRawData()); + } + + public FfiControllerData GetRawData() + { + return _ffi; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameArchiveData.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameArchiveData.cs new file mode 100644 index 0000000..85b37ac --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameArchiveData.cs @@ -0,0 +1,18 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data; + +public class GameArchiveData : IRawData<FfiGameRuntimeArchive> +{ + private readonly FfiGameRuntimeArchive _ffi = nogamepads_data.GameArchiveDataNew(); + + public void AddBannedPlayer(Player player) + { + nogamepads_data.GameArchiveDataAddBanPlayer(_ffi, player.GetRawData()); + } + + public FfiGameRuntimeArchive GetRawData() + { + return _ffi; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameData.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameData.cs new file mode 100644 index 0000000..480636b --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameData.cs @@ -0,0 +1,33 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data; + +public class GameData : IRawData<FfiGameData> +{ + private readonly FfiGameData _ffi = nogamepads_data.GameDataNew(); + + public string Name + { + set => nogamepads_data.GameDataSetNameInfo(_ffi, value); + } + + public string Version + { + set => nogamepads_data.GameDataSetVersionInfo(_ffi, value); + } + + public void EditInfo(string infoName, string value) + { + nogamepads_data.GameDataAddInfo(_ffi, infoName, value); + } + + public void LoadArchive(GameArchiveData gameArchiveData) + { + nogamepads_data.GameDataLoadArchive(_ffi, gameArchiveData.GetRawData()); + } + + public FfiGameData GetRawData() + { + return _ffi; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/IRawData.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/IRawData.cs new file mode 100644 index 0000000..a0e72ea --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/IRawData.cs @@ -0,0 +1,6 @@ +namespace NoGamepads_Core.Data; + +public interface IRawData<T> +{ + T GetRawData(); +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlMessage.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlMessage.cs new file mode 100644 index 0000000..fb41612 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlMessage.cs @@ -0,0 +1,158 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data.Message; + +public struct ControlMessage +{ + public enum Tag + { + Message = 0, + Pressed = 1, + Released = 2, + Axis = 3, + Direction = 4, + Exit = 5, + Error = 6, + End = 7 + } + + public Tag MessageTag => _tag; + + private Tag _tag; + private string _content; + private int _key; + private float _x; // Axis or DirectionX + private float _y; + + public static ControlMessage Message(string messageContent) + { + return new ControlMessage + { + _tag = Tag.Message, + _content = messageContent + }; + } + + public static ControlMessage Pressed(int key) + { + return new ControlMessage + { + _tag = Tag.Pressed, + _key = key + }; + } + + public static ControlMessage Released(int key) + { + return new ControlMessage + { + _tag = Tag.Released, + _key = key + }; + } + + public static ControlMessage Axis(int key, float x) + { + return new ControlMessage + { + _tag = Tag.Axis, + _key = key, + _x = x + }; + } + + public static ControlMessage Direction(int key, float x, float y) + { + return new ControlMessage + { + _tag = Tag.Direction, + _key = key, + _x = x, + _y = y + }; + } + + public static ControlMessage Exit() + { + return new ControlMessage + { + _tag = Tag.Exit, + }; + } + + public static ControlMessage Error() + { + return new ControlMessage + { + _tag = Tag.Error, + }; + } + + public static ControlMessage End() + { + return new ControlMessage + { + _tag = Tag.End, + }; + } + + public FfiControlMessage Parse() + { + FfiControlMessage controlMessage = new FfiControlMessage(); + int index = (int) MessageTag; + FfiControlMessageTag tag = (FfiControlMessageTag) index; + controlMessage.Tag = tag; + switch (tag) + { + case FfiControlMessageTag.CtrlMsg: + unsafe { controlMessage.Data = controlMessage.Data with { Message = PtrStringConverter.ToPtr(_content) }; } + break; + case FfiControlMessageTag.CtrlPressed: + controlMessage.Data = controlMessage.Data with { Key = (byte) _key }; + break; + case FfiControlMessageTag.CtrlReleased: + controlMessage.Data = controlMessage.Data with { Key = (byte) _key }; + break; + case FfiControlMessageTag.CtrlAxis: + FfiKeyAndAxis keyAndAxis = new FfiKeyAndAxis(); + keyAndAxis.Key = (byte) _key; + keyAndAxis.Axis = _x; + controlMessage.Data = controlMessage.Data with { KeyAndAxis = keyAndAxis }; + break; + case FfiControlMessageTag.CtrlDir: + FfiKeyAndDirection keyAndDirection = new FfiKeyAndDirection(); + keyAndDirection.Key = (byte) _key; + keyAndDirection.X = _x; + keyAndDirection.Y = _y; + controlMessage.Data = controlMessage.Data with { KeyAndDirection = keyAndDirection }; + break; + } + return controlMessage; + } + + public static ControlMessage From(FfiControlMessage message) + { + switch (message.Tag) + { + case FfiControlMessageTag.CtrlMsg: + unsafe { return Message(PtrStringConverter.ToString(message.Data.Message)); } + case FfiControlMessageTag.CtrlPressed: + return Pressed(message.Data.Key); + case FfiControlMessageTag.CtrlReleased: + return Released(message.Data.Key); + case FfiControlMessageTag.CtrlAxis: + FfiKeyAndAxis keyAndAxis = message.Data.KeyAndAxis; + return Axis(keyAndAxis.Key, (float) keyAndAxis.Axis); + case FfiControlMessageTag.CtrlDir: + FfiKeyAndDirection keyAndDirection = message.Data.KeyAndDirection; + return Direction(keyAndDirection.Key, (float) keyAndDirection.X, (float) keyAndDirection.Y); + case FfiControlMessageTag.CtrlExit: + return Exit(); + case FfiControlMessageTag.CtrlError: + return Error(); + case FfiControlMessageTag.CtrlEnd: + return End(); + } + return Error(); + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ExitReason.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ExitReason.cs new file mode 100644 index 0000000..0541d7f --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ExitReason.cs @@ -0,0 +1,28 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data.Message; + +public enum ExitReason +{ + Exit = 0, + GameOverReason = 1, + ServerClosedReason = 2, + YouAreKickedReason = 3, + YouAreBannedReason = 4, + ErrorReason = 5 +} + +public static class ExitReasonConvertor +{ + public static ExitReason Convert(this FfiExitReason reason) + { + int index = (int) reason; + return (ExitReason) index; + } + + public static FfiExitReason Convert(this ExitReason reason) + { + int index = (int) reason; + return (FfiExitReason) index; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/GameMessage.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/GameMessage.cs new file mode 100644 index 0000000..427c674 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/GameMessage.cs @@ -0,0 +1,104 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data.Message; + +public struct GameMessage +{ + public enum Tag + { + EventTrigger = 0, + Message = 1, + LetExit = 2, + Error = 3, + End = 4 + } + + public Tag MessageTag => _tag; + + private Tag _tag; + private string _content; + private int _key; + private ExitReason _exitReason; + + public static GameMessage EventTrigger(int key) + { + return new GameMessage + { + _tag = Tag.EventTrigger, + _key = key + }; + } + + public static GameMessage Message(string messageContent) + { + return new GameMessage + { + _tag = Tag.Message, + _content = messageContent + }; + } + + public static GameMessage LetExit(ExitReason exitReason) + { + return new GameMessage + { + _tag = Tag.LetExit, + _exitReason = exitReason + }; + } + + public static GameMessage Error() + { + return new GameMessage + { + _tag = Tag.Error + }; + } + + public static GameMessage End() + { + return new GameMessage + { + _tag = Tag.End + }; + } + + public FfiGameMessage Parse() + { + FfiGameMessage gameMessage = new FfiGameMessage(); + int index = (int) MessageTag; + FfiGameMessageTag tag = (FfiGameMessageTag) index; + gameMessage.Tag = tag; + switch (tag) + { + case FfiGameMessageTag.GameEventTrigger: + gameMessage.Data = gameMessage.Data with { Key = (byte) _key }; + break; + case FfiGameMessageTag.GameMsg: + unsafe { gameMessage.Data = gameMessage.Data with { Message = PtrStringConverter.ToPtr(_content) }; } + break; + case FfiGameMessageTag.GameLetExit: + gameMessage.Data = gameMessage.Data with { ExitReason = _exitReason.Convert()}; + break; + } + return gameMessage; + } + + public static GameMessage From(FfiGameMessage message) + { + switch (message.Tag) + { + case FfiGameMessageTag.GameEventTrigger: + return EventTrigger(message.Data.Key); + case FfiGameMessageTag.GameMsg: + unsafe { return Message(PtrStringConverter.ToString(message.Data.Message)); } + case FfiGameMessageTag.GameLetExit: + return LetExit(message.Data.ExitReason.Convert()); + case FfiGameMessageTag.GameError: + return Error(); + case FfiGameMessageTag.GameEnd: + return End(); + } + return Error(); + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Player.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Player.cs index ec8ebc6..55280d1 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Player.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Player.cs @@ -2,7 +2,7 @@ using NoGamepads_Sharp; namespace NoGamepads_Core.Data; -public class Player +public class Player : IRawData<FfiPlayer> { private readonly FfiPlayer _ffi; @@ -42,7 +42,8 @@ public class Player { unsafe { - return PtrStringConverter.ToString(_ffi.Account.PlayerHash); + var get = _ffi.Account.PlayerHash; + return PtrStringConverter.ToString(get); } } } @@ -85,7 +86,7 @@ public class Player return (float) (_ffi.Customize?.ColorSaturation ?? 1f); } - public FfiPlayer GetRawPlayer() + public FfiPlayer GetRawData() { return _ffi; } diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/NoGamepads_Core.csproj b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/NoGamepads_Core.csproj index cdf58f8..82e60e0 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/NoGamepads_Core.csproj +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/NoGamepads_Core.csproj @@ -11,8 +11,8 @@ <Content Include="Native\**"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> - <Folder Include="Runtime\" /> - <Folder Include="Services\" /> + <Folder Include="Services\Bluetooth\" /> + <Folder Include="Services\Usb\" /> <ProjectReference Include="..\NoGamepads_Bindings\NoGamepads_Bindings.csproj" /> </ItemGroup> diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/ControllerRuntime.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/ControllerRuntime.cs new file mode 100644 index 0000000..0716c04 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/ControllerRuntime.cs @@ -0,0 +1,61 @@ +using NoGamepads_Core.Data; +using NoGamepads_Core.Data.Message; +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Runtime; + +public class ControllerRuntime : IRawData<FfiControllerRuntime> +{ + private readonly FfiControllerRuntime _ffi; + + public ControllerRuntime(ControllerData data) + { + _ffi = nogamepads_data.ControllerDataBuildRuntime(data.GetRawData()); + } + + public void Close() + { + nogamepads_data.ControllerRuntimeClose(_ffi); + } + + public void Pressed(int key) + { + nogamepads_data.ControllerRuntimePressAButton(_ffi, (byte)key); + } + + public void Released(int key) + { + nogamepads_data.ControllerRuntimeReleaseAButton(_ffi, (byte)key); + } + + public void ChangeAxis(int key, float axis) + { + nogamepads_data.ControllerRuntimeChangeAxis(_ffi, (byte)key, axis); + } + + public void ChangeDirection(int key, float x, float y) + { + nogamepads_data.ControllerRuntimeChangeDirection(_ffi, (byte)key, x, y); + } + + public void SendTextMessage(string message) + { + nogamepads_data.ControllerRuntimeSendTextMessage(_ffi, message); + } + + public void SendMessage(ControlMessage message) + { + nogamepads_data.ControllerRuntimeSendMessage(_ffi, message.Parse()); + } + + public GameMessage PopMessage() + { + var result = nogamepads_data.ControllerRuntimePop(_ffi); + return result == null ? GameMessage.Error() : GameMessage.From(result); + } + + public FfiControllerRuntime GetRawData() + { + return _ffi; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/GameRuntime.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/GameRuntime.cs new file mode 100644 index 0000000..9ba5e14 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/GameRuntime.cs @@ -0,0 +1,19 @@ +using NoGamepads_Core.Data; +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Runtime; + +public class GameRuntime : IRawData<FfiGameRuntime> +{ + private readonly FfiGameRuntime _ffi; + + public GameRuntime(GameData data) + { + _ffi = nogamepads_data.GameDataBuildRuntime(data.GetRawData()); + } + + public FfiGameRuntime GetRawData() + { + return _ffi; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/ServiceType.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/ServiceType.cs new file mode 100644 index 0000000..93d7cfa --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/ServiceType.cs @@ -0,0 +1,36 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Services; + +public enum ServiceType +{ + Unknown, + TcpConnection, + BluetoothConnection, + UsbConnection, +} + +public static class ServiceTypeConverter +{ + public static FfiServiceType Convert(this ServiceType serviceType) + { + if (serviceType == ServiceType.TcpConnection) + return FfiServiceType.TCPConnection; + if (serviceType == ServiceType.BluetoothConnection) + return FfiServiceType.BlueTooth; + if (serviceType == ServiceType.UsbConnection) + return FfiServiceType.USB; + return FfiServiceType.Unknown; + } + + public static ServiceType Convert(this FfiServiceType serviceType) + { + if (serviceType == FfiServiceType.TCPConnection) + return ServiceType.TcpConnection; + if (serviceType == FfiServiceType.BlueTooth) + return ServiceType.BluetoothConnection; + if (serviceType == FfiServiceType.USB) + return ServiceType.UsbConnection; + return ServiceType.Unknown; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpClient.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpClient.cs new file mode 100644 index 0000000..40c9aba --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpClient.cs @@ -0,0 +1,6 @@ +namespace NoGamepads_Core.Services.Tcp; + +public class PadTcpClient +{ + +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpServer.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpServer.cs new file mode 100644 index 0000000..bfbef46 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpServer.cs @@ -0,0 +1,6 @@ +namespace NoGamepads_Core.Services.Tcp; + +public class PadTcpServer +{ + +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs index c89a220..11b0d76 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs @@ -1,19 +1,23 @@ using NoGamepads_Core.Data; +using NoGamepads_Core.Runtime; -Player player = new Player("Pzw", "123456") -{ - NickName = "" -}; - -Console.WriteLine("Player hash : " + player.Hash); -Console.WriteLine("Player name : " + player.NickName); - -player.NickName = "OMG"; +namespace NoGamepads_Example; -Console.WriteLine("Player name : " + player.NickName); - -player.Hue = 92; -player.Value = 1; -player.Saturation = 1; +internal class Program +{ + public static void Main(string[] args) + { + Player player = new Player("CatilGrass", "Unknown Password"); -Console.WriteLine($"Color : H: {player.Hue}, S: {player.Saturation}, V: {player.Value}");
\ No newline at end of file + player.Hue = 200; + + ControllerData data = new ControllerData(player); + + ControllerRuntime runtime = new ControllerRuntime(data); + + runtime.SendTextMessage("Hello World! "); + Console.WriteLine(player.Hue + "!"); + + // Console.WriteLine(player.Hash); + } +}
\ No newline at end of file |
