diff options
| author | Weicao Mao <1992414357@qq.com> | 2025-06-22 03:28:32 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-22 03:28:32 +0800 |
| commit | 207599b774d4082e702c7ab7bc7457c8a224e7b9 (patch) | |
| tree | 61ddb86415e7ee36dcf800d5662c803253b3a52d /core | |
| parent | e7779d078e0a4920488e1df216aeb6287b2428c7 (diff) | |
| parent | bc824a6209d72e6366b5c390c5b6cc68aad6e094 (diff) | |
Csharp bindings
Diffstat (limited to 'core')
7 files changed, 62 insertions, 15 deletions
diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/LoggerManagement.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/LoggerManagement.cs index f1d2bd9..0d0ccc1 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/LoggerManagement.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/LoggerManagement.cs @@ -4,7 +4,7 @@ namespace NoGamepads_Core.Data; public static class LoggerManagement { - public static void EnableLogger(int level) + public static void EnableLogger(int level = 0) { nogamepads_data.EnableLogger((byte) level); } 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 da3a266..264ab73 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 @@ -18,6 +18,7 @@ public class ControllerRuntime : IRustDataBorrow<FfiControllerRuntime>, IRustDat { get { + if (IsUsed()) return GameMessage.End(); var result = nogamepads_data.ControllerRuntimePop(_ffi); return result == null ? GameMessage.Error() : GameMessage.From(result); } @@ -25,36 +26,43 @@ public class ControllerRuntime : IRustDataBorrow<FfiControllerRuntime>, IRustDat public void Close() { + if (IsUsed()) return; nogamepads_data.ControllerRuntimeClose(_ffi); } public void Press(int key) { + if (IsUsed()) return; nogamepads_data.ControllerRuntimePressAButton(_ffi, (byte)key); } public void Release(int key) { + if (IsUsed()) return; nogamepads_data.ControllerRuntimeReleaseAButton(_ffi, (byte)key); } public void ChangeAxis(int key, float axis) { + if (IsUsed()) return; nogamepads_data.ControllerRuntimeChangeAxis(_ffi, (byte)key, axis); } public void ChangeDirection(int key, float x, float y) { + if (IsUsed()) return; nogamepads_data.ControllerRuntimeChangeDirection(_ffi, (byte)key, x, y); } public void SendText(string message) { + if (IsUsed()) return; nogamepads_data.ControllerRuntimeSendTextMessage(_ffi, message); } public void Send(ControlMessage message) { + if (IsUsed()) return; nogamepads_data.ControllerRuntimeSendMessage(_ffi, message.Convert()); } 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 5bef52a..986b4ab 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 @@ -44,34 +44,40 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame public void Kick(Player player, ServiceType type = ServiceType.Unknown) { + if (IsUsed()) return; 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) { + if (IsUsed()) return; 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) { + if (IsUsed()) return; var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; nogamepads_data.GameRuntimeBanPlayer(_ffi, player.Borrow(), serviceType.Convert()); } public void Pardon(Player player) { + if (IsUsed()) return; nogamepads_data.GameRuntimePardonPlayer(_ffi, player.Borrow()); } public ServiceType GetServiceType(Player player) { + if (IsUsed()) return ServiceType.Unknown; return nogamepads_data.GameRuntimeGetServiceType(_ffi, player.Borrow()).Convert(); } public bool IsOnline(Player player) { + if (IsUsed()) return false; var result = nogamepads_data.GameRuntimeIsPlayerOnline(_ffi, player.Borrow()); if (result.Found) { @@ -82,6 +88,7 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame public bool IsBanned(Player player) { + if (IsUsed()) return false; var result = nogamepads_data.GameRuntimeIsPlayerBanned(_ffi, player.Borrow()); if (result.Found) { @@ -90,24 +97,26 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame return false; } - public List<Player> OnlinePlayers => nogamepads_data.GameRuntimeGetOnlineList(_ffi).ToPlayerList(); + public List<Player>? OnlinePlayers => IsUsed() ? null : nogamepads_data.GameRuntimeGetOnlineList(_ffi).ToPlayerList(); - public List<Player> BannedPlayers => nogamepads_data.GameRuntimeGetBannedList(_ffi).ToPlayerList(); + public List<Player>? BannedPlayers => IsUsed() ? null : nogamepads_data.GameRuntimeGetBannedList(_ffi).ToPlayerList(); #endregion #region Message Management - public ControlEvent RecentEvent => new(nogamepads_data.GameRuntimePopControlEvent(_ffi)); + public ControlEvent? RecentEvent => IsUsed() ? null : new(nogamepads_data.GameRuntimePopControlEvent(_ffi)); public void SendText(string message, Player player, ServiceType type = ServiceType.Unknown) { + if (IsUsed()) return; 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) { + if (IsUsed()) return; var serviceType = type == ServiceType.Unknown ? GetServiceType(player) : type; nogamepads_data.GameRuntimeSendMessageTo(_ffi, player.Borrow(), message.Convert(), serviceType.Convert()); } @@ -118,6 +127,7 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame public ButtonStatus Button(int key, Player player) { + if (IsUsed()) return ButtonStatus.NotFound(); var result = nogamepads_data.GameRuntimeGetButtonStatus(_ffi, player.Borrow(), (byte)key); if (result == null || !result.Found) { @@ -128,6 +138,7 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame public AxisStatus Axis(int key, Player player) { + if (IsUsed()) return AxisStatus.NotFound(); var result = nogamepads_data.GameRuntimeGetAxis(_ffi, player.Borrow(), (byte)key); if (result == null || !result.Found) { @@ -138,6 +149,7 @@ public class GameRuntime : IRustDataBorrow<FfiGameRuntime>, IRustDataUse<FfiGame public DirectionStatus Direction(int key, Player player) { + if (IsUsed()) return DirectionStatus.NotFound(); var result = nogamepads_data.GameRuntimeGetDirection(_ffi, player.Borrow(), (byte)key); if (result == null || !result.Found) { 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 index 6eb81d9..2ea07f5 100644 --- 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 @@ -6,26 +6,29 @@ namespace NoGamepads_Core.Services.Tcp; public class PadTcpClient { private readonly FfiTcpClientService _serviceFfi; + private readonly ControllerRuntime _runtime; public PadTcpClient(ControllerRuntime runtime) { + _runtime = runtime; _serviceFfi = nogamepads_data.TcpClientBuild(runtime.Borrow()); } public PadTcpClient SetAddressV4(int a0 = 127, int a1 = 0, int a2 = 0, int a3 = 1, int port = 5989) { - nogamepads_data.TcpClientBindAddressV4(_serviceFfi, (byte) a0, (byte) a1, (byte) a2, (byte) a3, (byte) port); + nogamepads_data.TcpClientBindAddressV4(_serviceFfi, (byte) a0, (byte) a1, (byte) a2, (byte) a3, ushort.Parse(port + "")); return this; } public PadTcpClient SetAddressV6(string address = "::1", int port = 5989) { - nogamepads_data.TcpClientBindAddressV6(_serviceFfi, address, (byte) port); + nogamepads_data.TcpClientBindAddressV6(_serviceFfi, address, ushort.Parse(port + "")); return this; } public void Connect() { nogamepads_data.TcpClientConnect(_serviceFfi); + _runtime.Use(); } }
\ 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 index 05deda9..ea71595 100644 --- 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 @@ -6,26 +6,29 @@ namespace NoGamepads_Core.Services.Tcp; public class PadTcpServer { private readonly FfiTcpServerService _serviceFfi; + private readonly GameRuntime _runtime; public PadTcpServer(GameRuntime runtime) { + _runtime = runtime; _serviceFfi = nogamepads_data.TcpServerBuild(runtime.Borrow()); } public PadTcpServer SetAddressV4(int a0 = 127, int a1 = 0, int a2 = 0, int a3 = 1, int port = 5989) { - nogamepads_data.TcpServerBindAddressV4(_serviceFfi, (byte) a0, (byte) a1, (byte) a2, (byte) a3, (byte) port); + nogamepads_data.TcpServerBindAddressV4(_serviceFfi, (byte) a0, (byte) a1, (byte) a2, (byte) a3, ushort.Parse(port + "")); return this; } public PadTcpServer SetAddressV6(string address = "::1", int port = 5989) { - nogamepads_data.TcpServerBindAddressV6(_serviceFfi, address, (byte) port); + nogamepads_data.TcpServerBindAddressV6(_serviceFfi, address, ushort.Parse(port + "")); return this; } public void BlockOn() { nogamepads_data.TcpServerListeningBlockOn(_serviceFfi); + _runtime.Use(); } }
\ 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 48ec6f6..135cbe5 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/Program.cs @@ -1,7 +1,7 @@ using NoGamepads_Core.Data; +using NoGamepads_Core.Data.Message; using NoGamepads_Core.Runtime; using NoGamepads_Core.Services.Tcp; -using NoGamepads_Sharp; namespace NoGamepads_Example; @@ -9,7 +9,7 @@ internal class Program { public static void Main(string[] args) { - LoggerManagement.EnableLogger(2); + LoggerManagement.EnableLogger(); Player player = new Player("CatilGrass", "123456"); @@ -23,8 +23,29 @@ internal class Program ControllerRuntime runtime = new ControllerRuntime(data); - new PadTcpClient(runtime) - .SetAddressV4() - .Connect(); + var threadService = new Thread(() => + { + new PadTcpClient(runtime) + .SetAddressV4() + .Connect(); + }); + + var threadRuntime = new Thread(() => + { + while (true) + { + var recent = runtime.RecentMessage; + + if (recent.MessageTag == GameMessage.Tag.End) + break; + + if (recent.MessageTag != GameMessage.Tag.Error) + Console.WriteLine(recent); + } + Console.WriteLine("\nPress any key to continue..."); + }); + + threadService.Start(); + threadRuntime.Start(); } }
\ No newline at end of file diff --git a/core/src/service/tcp_network/pad_client/pad_client_service.rs b/core/src/service/tcp_network/pad_client/pad_client_service.rs index 9b1826b..fe3da29 100644 --- a/core/src/service/tcp_network/pad_client/pad_client_service.rs +++ b/core/src/service/tcp_network/pad_client/pad_client_service.rs @@ -85,8 +85,6 @@ impl PadClientNetwork { pub fn connect(self) { let runtime = build_tokio_runtime("padclient_tcp".to_string()); - - info!("[TCP Client] Connecting to {}:{}", self.addr.ip().to_string(), self.addr.port()); runtime.block_on(self.build_entry()); } } @@ -94,6 +92,8 @@ impl PadClientNetwork { impl PadClientNetwork { async fn connection_thread(self: Arc<PadClientNetwork>) { + info!("[TCP Client] Connecting to {}:{}", self.addr.ip().to_string(), self.addr.port()); + let mut buffer = [0; 1024]; // Requests game infos |
