diff options
| author | 1992414357@qq.com <1992414357@qq.com> | 2025-06-22 02:11:45 +0800 |
|---|---|---|
| committer | 1992414357@qq.com <1992414357@qq.com> | 2025-06-22 02:11:45 +0800 |
| commit | d9ee1134eb8e0e151b380aa8a932a98ed1809209 (patch) | |
| tree | 6a9fca4fc863022107940fce87c851c21969dd0a | |
| parent | 6c558b2ed757dbcc0fbcc1fd550581b7f9fce1c4 (diff) | |
Update CSharp Bindings
10 files changed, 294 insertions, 36 deletions
diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlEvent.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlEvent.cs new file mode 100644 index 0000000..c2c992e --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlEvent.cs @@ -0,0 +1,15 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data.Message; + +public class ControlEvent +{ + public Player Player; + public ControlMessage Message; + + public ControlEvent(FfiControlEvent ffi) + { + Player = new Player(ffi.Player); + Message = ControlMessage.From(ffi.Message); + } +}
\ 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 index d1f64cf..5eb0c1c 100644 --- 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 @@ -96,7 +96,7 @@ public struct ControlMessage }; } - public FfiControlMessage Parse() + public FfiControlMessage Convert() { FfiControlMessage controlMessage = new FfiControlMessage(); int index = (int) MessageTag; 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 index b29c2c5..a4ce71b 100644 --- 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 @@ -63,7 +63,7 @@ public struct GameMessage }; } - public FfiGameMessage Parse() + public FfiGameMessage Convert() { FfiGameMessage gameMessage = new FfiGameMessage(); int index = (int) MessageTag; 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 3ad5d7f..e387f09 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 @@ -11,6 +11,11 @@ public class Player : IRustDataBorrow<FfiPlayer>, IRustDataUse<FfiPlayer> { _ffi = nogamepads_data.PlayerRegister(account, password); } + + public Player(FfiPlayer ffi) + { + _ffi = ffi; + } public string NickName { diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/PlayerList.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/PlayerList.cs new file mode 100644 index 0000000..fdee7a8 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/PlayerList.cs @@ -0,0 +1,27 @@ +using NoGamepads_Sharp; + +namespace NoGamepads_Core.Data; + +public static class PlayerList +{ + public static unsafe List<Player> ToPlayerList(this FfiPlayerList ffiPlayerList) + { + var playerList = new List<Player>(); + + var native = (FfiPlayerList.__Internal*) ffiPlayerList.__Instance; + + var playersPtr = (FfiPlayer.__Internal*) native -> players.ToPointer(); + ulong count = native->len; + + for (ulong i = 0; i < count; i++) + { + var current = playersPtr + (int)i; + IntPtr playerPtr = new IntPtr(current); + + FfiPlayer ffiPlayer = FfiPlayer.__CreateInstance(playerPtr); + playerList.Add(new Player(ffiPlayer)); + } + + return playerList; + } +}
\ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Status.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Status.cs new file mode 100644 index 0000000..e454fcc --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Status.cs @@ -0,0 +1,88 @@ +namespace NoGamepads_Core.Data; + +public struct ButtonStatus +{ + private bool _value; + + public bool Found; + public bool Pressed => _value; + public bool Released => !_value; + + public static ButtonStatus Press() + { + return new ButtonStatus + { + Found = true, + _value = true + }; + } + + public static ButtonStatus Release() + { + return new ButtonStatus + { + Found = true, + _value = false + }; + } + + public static ButtonStatus NotFound() + { + return new ButtonStatus + { + Found = false, + _value = false + }; + } +} + +public struct AxisStatus +{ + public bool Found; + public float X; + + public static AxisStatus Axis(float value) + { + return new AxisStatus + { + Found = true, + X = value + }; + } + + public static AxisStatus NotFound() + { + return new AxisStatus + { + Found = false, + X = 0 + }; + } +} + +public struct DirectionStatus +{ + public bool Found; + public float X; + public float Y; + + public static DirectionStatus Direction(float x, float y) + { + return new DirectionStatus + { + Found = true, + X = x, + Y = y + }; + } + + public static DirectionStatus NotFound() + { + return new DirectionStatus + { + Found = false, + X = 0, + Y = 0 + }; + } +}
\ No newline at end of file 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 index 373c6fd..da3a266 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/ControllerRuntime.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/ControllerRuntime.cs @@ -14,17 +14,26 @@ public class ControllerRuntime : IRustDataBorrow<FfiControllerRuntime>, IRustDat _ffi = nogamepads_data.ControllerDataBuildRuntime(data.Use()); } + public GameMessage RecentMessage + { + get + { + var result = nogamepads_data.ControllerRuntimePop(_ffi); + return result == null ? GameMessage.Error() : GameMessage.From(result); + } + } + public void Close() { nogamepads_data.ControllerRuntimeClose(_ffi); } - public void Pressed(int key) + public void Press(int key) { nogamepads_data.ControllerRuntimePressAButton(_ffi, (byte)key); } - public void Released(int key) + public void Release(int key) { nogamepads_data.ControllerRuntimeReleaseAButton(_ffi, (byte)key); } @@ -39,20 +48,14 @@ public class ControllerRuntime : IRustDataBorrow<FfiControllerRuntime>, IRustDat nogamepads_data.ControllerRuntimeChangeDirection(_ffi, (byte)key, x, y); } - public void SendTextMessage(string message) + public void SendText(string message) { nogamepads_data.ControllerRuntimeSendTextMessage(_ffi, message); } - public void SendMessage(ControlMessage message) - { - nogamepads_data.ControllerRuntimeSendMessage(_ffi, message.Parse()); - } - - public GameMessage PopMessage() + public void Send(ControlMessage message) { - var result = nogamepads_data.ControllerRuntimePop(_ffi); - return result == null ? GameMessage.Error() : GameMessage.From(result); + nogamepads_data.ControllerRuntimeSendMessage(_ffi, message.Convert()); } public FfiControllerRuntime? Borrow() 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 index 7211581..5bef52a 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/GameRuntime.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/GameRuntime.cs @@ -1,4 +1,6 @@ using NoGamepads_Core.Data; +using NoGamepads_Core.Data.Message; +using NoGamepads_Core.Services; using NoGamepads_Sharp; namespace NoGamepads_Core.Runtime; @@ -12,6 +14,139 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame { _ffi = nogamepads_data.GameDataBuildRuntime(data.Borrow()); } + + #region Status Management + + public bool LookStatus + { + get => nogamepads_data.GameRuntimeGetLockStatus(_ffi); + set + { + if (value) + { + nogamepads_data.GameRuntimeLock(_ffi); + } + else + { + nogamepads_data.GameRuntimeUnlock(_ffi); + } + } + } + + public void CloseGame() + { + nogamepads_data.GameRuntimeClose(_ffi); + } + + #endregion + + #region Player Management + + public void Kick(Player player, ServiceType type = ServiceType.Unknown) + { + var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; + nogamepads_data.GameRuntimeKickPlayer(_ffi, player.Borrow(), serviceType.Convert()); + } + + public void LetExit(Player player, ServiceType type = ServiceType.Unknown, ExitReason reason = ExitReason.Exit) + { + var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; + nogamepads_data.GameRuntimeLetExit(_ffi, player.Borrow(), serviceType.Convert(), reason.Convert()); + } + + public void Ban(Player player, ServiceType type = ServiceType.Unknown) + { + var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; + nogamepads_data.GameRuntimeBanPlayer(_ffi, player.Borrow(), serviceType.Convert()); + } + + public void Pardon(Player player) + { + nogamepads_data.GameRuntimePardonPlayer(_ffi, player.Borrow()); + } + + public ServiceType GetServiceType(Player player) + { + return nogamepads_data.GameRuntimeGetServiceType(_ffi, player.Borrow()).Convert(); + } + + public bool IsOnline(Player player) + { + var result = nogamepads_data.GameRuntimeIsPlayerOnline(_ffi, player.Borrow()); + if (result.Found) + { + return result.Result; + } + return false; + } + + public bool IsBanned(Player player) + { + var result = nogamepads_data.GameRuntimeIsPlayerBanned(_ffi, player.Borrow()); + if (result.Found) + { + return result.Result; + } + return false; + } + + public List<Player> OnlinePlayers => nogamepads_data.GameRuntimeGetOnlineList(_ffi).ToPlayerList(); + + public List<Player> BannedPlayers => nogamepads_data.GameRuntimeGetBannedList(_ffi).ToPlayerList(); + + #endregion + + #region Message Management + + public ControlEvent RecentEvent => new(nogamepads_data.GameRuntimePopControlEvent(_ffi)); + + public void SendText(string message, Player player, ServiceType type = ServiceType.Unknown) + { + var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; + nogamepads_data.GameRuntimeSendTextMessage(_ffi, player.Borrow(), serviceType.Convert(), message); + } + + public void Send(GameMessage message, Player player, ServiceType type = ServiceType.Unknown) + { + var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; + nogamepads_data.GameRuntimeSendMessageTo(_ffi, player.Borrow(), message.Convert(), serviceType.Convert()); + } + + #endregion + + #region Control Management + + public ButtonStatus Button(int key, Player player) + { + var result = nogamepads_data.GameRuntimeGetButtonStatus(_ffi, player.Borrow(), (byte)key); + if (result == null || !result.Found) + { + return ButtonStatus.NotFound(); + } + return result.Pressed ? ButtonStatus.Press() : ButtonStatus.Release(); + } + + public AxisStatus Axis(int key, Player player) + { + var result = nogamepads_data.GameRuntimeGetAxis(_ffi, player.Borrow(), (byte)key); + if (result == null || !result.Found) + { + return AxisStatus.NotFound(); + } + return AxisStatus.Axis((float)result.Axis); + } + + public DirectionStatus Direction(int key, Player player) + { + var result = nogamepads_data.GameRuntimeGetDirection(_ffi, player.Borrow(), (byte)key); + if (result == null || !result.Found) + { + return DirectionStatus.NotFound(); + } + return DirectionStatus.Direction((float) result.X, (float) result.Y); + } + + #endregion public FfiGameRuntime? Borrow() { 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 index 93d7cfa..dc1c0ab 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/ServiceType.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/ServiceType.cs @@ -4,33 +4,23 @@ namespace NoGamepads_Core.Services; public enum ServiceType { - Unknown, - TcpConnection, - BluetoothConnection, - UsbConnection, + Unknown = 0, + TcpConnection = 1, + BluetoothConnection = 2, + UsbConnection = 3, } 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; + var index = (int)serviceType; + return (FfiServiceType)index; } 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; + var index = (int)serviceType; + return (ServiceType)index; } }
\ 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 fbd560e..03569b7 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs @@ -19,10 +19,5 @@ internal class Program ControllerData data = new ControllerData(player); ControllerRuntime runtime = new ControllerRuntime(data); - - nogamepads_data.EnableLogger(2); - - FfiTcpClientService client = nogamepads_data.TcpClientBuild(runtime.Use()); - nogamepads_data.TcpClientConnect(client); } }
\ No newline at end of file |
