From ca2ebc3a39d210cb0a0e82b14342dde33b270398 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Fri, 20 Jun 2025 02:42:54 +0800 Subject: Update Csharp Bindings --- Project_Export.toml | 4 ++ .../NoGamepads_CharpBindings/.run/Example.run.xml | 20 ------ .../.run/Generate Bindings.run.xml | 20 ++++++ .../.run/Generate Csharp Bindings.run.xml | 20 ------ .../.run/Run Example.run.xml | 20 ++++++ .../NoGamepads_Bindings/NoGamepads_Bindings.csproj | 1 + .../NoGamepads_Bindings/Program.cs | 58 ++++++++++++++-- .../NoGamepads_Core/Data/Player.cs | 77 +++++++++++++++++++++- .../NoGamepads_Example/Program.cs | 20 ++++-- 9 files changed, 189 insertions(+), 51 deletions(-) delete mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Example.run.xml create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Bindings.run.xml delete mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Csharp Bindings.run.xml create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Run Example.run.xml diff --git a/Project_Export.toml b/Project_Export.toml index ba2c27d..e45285b 100644 --- a/Project_Export.toml +++ b/Project_Export.toml @@ -56,4 +56,8 @@ c_binding_example_deps_win = { raw = "./", target = "./core/bindings/lang/clang/ csharp_binding_deps_win = { raw = "./", target = "./core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/Native/", files = [ "nogamepads_c.dll", +] } + +csharp_binding_libs_win = { raw = "./", target = "./core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Example/bin/Debug/net6.0/", files = [ + "nogamepads_c.dll" ] } \ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Example.run.xml b/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Example.run.xml deleted file mode 100644 index 91f4cc3..0000000 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Example.run.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - \ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Bindings.run.xml b/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Bindings.run.xml new file mode 100644 index 0000000..0e7c565 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Bindings.run.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Csharp Bindings.run.xml b/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Csharp Bindings.run.xml deleted file mode 100644 index e70ca09..0000000 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Generate Csharp Bindings.run.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - \ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Run Example.run.xml b/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Run Example.run.xml new file mode 100644 index 0000000..792c169 --- /dev/null +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/.run/Run Example.run.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/NoGamepads_Bindings.csproj b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/NoGamepads_Bindings.csproj index b12992a..aaec264 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/NoGamepads_Bindings.csproj +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/NoGamepads_Bindings.csproj @@ -15,6 +15,7 @@ + diff --git a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/Program.cs b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/Program.cs index ebb5f76..f65f182 100644 --- a/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/Program.cs +++ b/core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Bindings/Program.cs @@ -1,16 +1,63 @@ using CppSharp; using CppSharp.AST; +using CppSharp.AST.Extensions; using CppSharp.Generators; -using CppSharp.Generators.AST; -using RecordArgABI = CppSharp.Parser.AST.RecordArgABI; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace NoGamepads_Bindings; internal static class Program { - public static void Main(string[] args) + public static void Main() { ConsoleDriver.Run(new NoGamepadsLibrary()); + FixRefProperties(); + } + + private static void FixRefProperties() + { + var directory = "NoGamepads_Core/Generated"; + var files = Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories); + + foreach (var file in files) + { + var code = File.ReadAllText(file); + var tree = CSharpSyntaxTree.ParseText(code); + var root = tree.GetRoot(); + + var newRoot = root.ReplaceNodes( + root.DescendantNodes().OfType(), + (prop, _) => + { + // internal ref FfiXXXUnion.__Internal __Instance => ref __instance; + if (prop.Identifier.Text != "__Instance") + return prop; + + if (prop.Type is RefTypeSyntax refType && + prop.ExpressionBody?.Expression is RefExpressionSyntax refExpr && + refType.Type.ToString().EndsWith(".__Internal", StringComparison.Ordinal) && + refExpr.Expression is IdentifierNameSyntax fieldRef && + fieldRef.Identifier.Text == "__instance") + { + var newType = refType.Type.WithTriviaFrom(refType); + var newExpr = fieldRef.WithTriviaFrom(refExpr); + + return prop + .WithType(newType) + .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(newExpr)) + .WithSemicolonToken(prop.SemicolonToken); + } + + return prop; + }); + + if (!newRoot.IsEquivalentTo(root)) + { + File.WriteAllText(file, newRoot.ToFullString()); + } + } } } @@ -29,9 +76,10 @@ public class NoGamepadsLibrary : ILibrary options.OutputDir = "NoGamepads_Core/Generated"; options.GenerateClassTemplates = false; - var module = options.AddModule("NoGamepads.Native"); + var module = options.AddModule("nogamepads_c"); module.OutputNamespace = "NoGamepads_Sharp"; module.IncludeDirs.Add("NoGamepads_Bindings/Native"); module.Headers.Add("nogamepads_data.h"); } -} \ 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 a3c3ddd..ec8ebc6 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 @@ -4,9 +4,39 @@ namespace NoGamepads_Core.Data; public class Player { - private FfiPlayer _ffi = nogamepads_data.PlayerRegister("Fuck", "AAAAA"); + private readonly FfiPlayer _ffi; - public String name + public Player(string account, string password) + { + _ffi = nogamepads_data.PlayerRegister(account, password); + } + + public string NickName + { + get + { + unsafe + { + var get = _ffi.Customize; + return get == null ? "" : PtrStringConverter.ToString(get.Nickname); + } + } + + set => nogamepads_data.PlayerSetNickname(_ffi, value); + } + + public string Id + { + get + { + unsafe + { + return PtrStringConverter.ToString(_ffi.Account.Id); + } + } + } + + public string Hash { get { @@ -16,4 +46,47 @@ public class Player } } } + + public int Hue + { + get => GetHue(); + set => SetHsv(value, Value, Saturation); + } + + public float Value + { + get => GetValue(); + set => SetHsv(Hue, value, Saturation); + } + + public float Saturation + { + get => GetSaturation(); + set => SetHsv(Hue, Value, value); + } + + private void SetHsv(int h, float s, float v) + { + nogamepads_data.PlayerSetHsv(_ffi, h, s, v); + } + + private int GetHue() + { + return _ffi.Customize?.ColorHue ?? 0; + } + + private float GetValue() + { + return (float) (_ffi.Customize?.ColorValue ?? 1f); + } + + private float GetSaturation() + { + return (float) (_ffi.Customize?.ColorSaturation ?? 1f); + } + + public FfiPlayer GetRawPlayer() + { + return _ffi; + } } \ 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 6c450f9..c89a220 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,19 @@ -// See https://aka.ms/new-console-template for more information +using NoGamepads_Core.Data; -using NoGamepads_Core.Data; +Player player = new Player("Pzw", "123456") +{ + NickName = "" +}; -Player player = new Player(); +Console.WriteLine("Player hash : " + player.Hash); +Console.WriteLine("Player name : " + player.NickName); -Console.WriteLine(player.name); \ No newline at end of file +player.NickName = "OMG"; + +Console.WriteLine("Player name : " + player.NickName); + +player.Hue = 92; +player.Value = 1; +player.Saturation = 1; + +Console.WriteLine($"Color : H: {player.Hue}, S: {player.Saturation}, V: {player.Value}"); \ No newline at end of file -- cgit From 60ebb5b4ec93ba94c3550babd6e927ca4b929ef2 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Fri, 20 Jun 2025 06:09:37 +0800 Subject: Update Csharp Bindings --- .../NoGamepads_Core/Data/ControllerData.cs | 19 +++ .../NoGamepads_Core/Data/GameArchiveData.cs | 18 +++ .../NoGamepads_Core/Data/GameData.cs | 33 +++++ .../NoGamepads_Core/Data/IRawData.cs | 6 + .../NoGamepads_Core/Data/Message/ControlMessage.cs | 158 +++++++++++++++++++++ .../NoGamepads_Core/Data/Message/ExitReason.cs | 28 ++++ .../NoGamepads_Core/Data/Message/GameMessage.cs | 104 ++++++++++++++ .../NoGamepads_Core/Data/Player.cs | 7 +- .../NoGamepads_Core/NoGamepads_Core.csproj | 4 +- .../NoGamepads_Core/Runtime/ControllerRuntime.cs | 61 ++++++++ .../NoGamepads_Core/Runtime/GameRuntime.cs | 19 +++ .../NoGamepads_Core/Services/ServiceType.cs | 36 +++++ .../NoGamepads_Core/Services/Tcp/PadTcpClient.cs | 6 + .../NoGamepads_Core/Services/Tcp/PadTcpServer.cs | 6 + .../NoGamepads_Example/Program.cs | 34 +++-- 15 files changed, 519 insertions(+), 20 deletions(-) create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/ControllerData.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameArchiveData.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/GameData.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/IRawData.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ControlMessage.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/ExitReason.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Data/Message/GameMessage.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/ControllerRuntime.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Runtime/GameRuntime.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/ServiceType.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpClient.cs create mode 100644 core/bindings/lang/csharp/NoGamepads_CharpBindings/NoGamepads_Core/Services/Tcp/PadTcpServer.cs 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 +{ + 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 +{ + 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 +{ + 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 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 { 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 @@ PreserveNewest - - + + 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 +{ + 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 +{ + 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 -- cgit