From 4f01b375c09606a47d83cac42a3530cf6476f7e2 Mon Sep 17 00:00:00 2001 From: Thad House Date: Mon, 22 Jan 2024 23:18:36 -0800 Subject: [PATCH] Switch ntcore to unified string --- src/ntcore/ConnectionInfo.cs | 8 +- src/ntcore/Generated/BooleanArrayTopic.cs | 26 +- src/ntcore/Generated/BooleanTopic.cs | 26 +- src/ntcore/Generated/DoubleArrayTopic.cs | 26 +- src/ntcore/Generated/DoubleTopic.cs | 26 +- src/ntcore/Generated/FloatArrayTopic.cs | 26 +- src/ntcore/Generated/FloatTopic.cs | 26 +- src/ntcore/Generated/IntegerArrayTopic.cs | 26 +- src/ntcore/Generated/IntegerTopic.cs | 26 +- src/ntcore/Generated/RawTopic.cs | 24 +- src/ntcore/Generated/StringArrayTopic.cs | 26 +- src/ntcore/Generated/StringTopic.cs | 26 +- src/ntcore/LogMessage.cs | 10 +- .../Natives/NetworkTableValueMarshaller.cs | 6 +- src/ntcore/Natives/NtCore.ClientServer.cs | 21 +- src/ntcore/Natives/NtCore.Datalog.cs | 9 +- src/ntcore/Natives/NtCore.Free.cs | 4 - src/ntcore/Natives/NtCore.Listener.cs | 2 +- src/ntcore/Natives/NtCore.Schema.cs | 11 +- src/ntcore/Natives/NtCore.cs | 138 +++------- .../Natives/NtLengthStringMarshaller.cs | 31 --- src/ntcore/Natives/NtString.cs | 19 -- src/ntcore/Natives/WriteStringWrapper.cs | 32 --- src/ntcore/NetworkTableInstance.cs | 4 +- src/ntcore/Topic.cs | 31 ++- src/ntcore/TopicInfo.cs | 12 +- src/wpiutil/Marshal/WPIStringMarshaller.cs | 10 + test/ntcore.test/NtStringMarshallerTests.cs | 253 +++++++++--------- .../StringUtf8ReturnMarshallerTests.cs | 60 ++--- 29 files changed, 416 insertions(+), 529 deletions(-) delete mode 100644 src/ntcore/Natives/NtLengthStringMarshaller.cs delete mode 100644 src/ntcore/Natives/NtString.cs delete mode 100644 src/ntcore/Natives/WriteStringWrapper.cs diff --git a/src/ntcore/ConnectionInfo.cs b/src/ntcore/ConnectionInfo.cs index 131a725a..3d6da579 100644 --- a/src/ntcore/ConnectionInfo.cs +++ b/src/ntcore/ConnectionInfo.cs @@ -24,8 +24,8 @@ public static ConnectionInfo ConvertToManaged(in NativeConnectionInfo unmanaged) { return new ConnectionInfo { - RemoteId = StringLengthPairMarshaller.ManagedConvert(unmanaged.remoteId) ?? "", - RemoteIp = StringLengthPairMarshaller.ManagedConvert(unmanaged.remoteIp) ?? "", + RemoteId = unmanaged.remoteId.ConvertToString(), + RemoteIp = unmanaged.remoteIp.ConvertToString(), RemotePort = unmanaged.remotePort, LastUpdate = unmanaged.lastUpdate, ProtocolVersion = unmanaged.protocolVersion, @@ -41,8 +41,8 @@ public static NativeConnectionInfo ConvertToUnmanaged(ConnectionInfo managed) [StructLayout(LayoutKind.Sequential)] public struct NativeConnectionInfo { - public NtString remoteId; - public NtString remoteIp; + public WpiStringMarshaller.WpiStringNative remoteId; + public WpiStringMarshaller.WpiStringNative remoteIp; public uint remotePort; public ulong lastUpdate; public uint protocolVersion; diff --git a/src/ntcore/Generated/BooleanArrayTopic.cs b/src/ntcore/Generated/BooleanArrayTopic.cs index 23642ac0..4f8fd6c3 100644 --- a/src/ntcore/Generated/BooleanArrayTopic.cs +++ b/src/ntcore/Generated/BooleanArrayTopic.cs @@ -58,7 +58,7 @@ public IBooleanArraySubscriber Subscribe( { return new BooleanArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.BooleanArray, "boolean[]"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IBooleanArraySubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IBooleanArraySubscriber SubscribeExDangerous( + internal IBooleanArraySubscriber SubscribeEx( ReadOnlySpan typeString, bool[] defaultValue, PubSubOptions options) { return new BooleanArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.BooleanArray, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IBooleanArrayPublisher Publish( { return new BooleanArrayEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.BooleanArray, "boolean[]"u8, options), []); @@ -194,14 +194,14 @@ public IBooleanArrayPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IBooleanArrayPublisher PublishExDangerous( + internal IBooleanArrayPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new BooleanArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.BooleanArray, typeString, properties, options), []); @@ -225,14 +225,14 @@ internal IBooleanArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IBooleanArrayPublisher PublishExDangerous( + internal IBooleanArrayPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new BooleanArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.BooleanArray, typeString, properties, options), []); @@ -256,14 +256,14 @@ internal IBooleanArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IBooleanArrayPublisher PublishExDangerous( + internal IBooleanArrayPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new BooleanArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.BooleanArray, typeString, properties, options), []); @@ -295,7 +295,7 @@ public IBooleanArrayEntry GetEntry( { return new BooleanArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.BooleanArray, "boolean[]"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IBooleanArrayEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IBooleanArrayEntry GetEntryExDangerous( + internal IBooleanArrayEntry GetEntryEx( ReadOnlySpan typeString, bool[] defaultValue, PubSubOptions options) { return new BooleanArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.BooleanArray, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/BooleanTopic.cs b/src/ntcore/Generated/BooleanTopic.cs index 25ccb8c6..9eb15cdd 100644 --- a/src/ntcore/Generated/BooleanTopic.cs +++ b/src/ntcore/Generated/BooleanTopic.cs @@ -58,7 +58,7 @@ public IBooleanSubscriber Subscribe( { return new BooleanEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Boolean, "boolean"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IBooleanSubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IBooleanSubscriber SubscribeExDangerous( + internal IBooleanSubscriber SubscribeEx( ReadOnlySpan typeString, bool defaultValue, PubSubOptions options) { return new BooleanEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Boolean, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IBooleanPublisher Publish( { return new BooleanEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.Boolean, "boolean"u8, options), false); @@ -194,14 +194,14 @@ public IBooleanPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IBooleanPublisher PublishExDangerous( + internal IBooleanPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new BooleanEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Boolean, typeString, properties, options), false); @@ -225,14 +225,14 @@ internal IBooleanPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IBooleanPublisher PublishExDangerous( + internal IBooleanPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new BooleanEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Boolean, typeString, properties, options), false); @@ -256,14 +256,14 @@ internal IBooleanPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IBooleanPublisher PublishExDangerous( + internal IBooleanPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new BooleanEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Boolean, typeString, properties, options), false); @@ -295,7 +295,7 @@ public IBooleanEntry GetEntry( { return new BooleanEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Boolean, "boolean"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IBooleanEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IBooleanEntry GetEntryExDangerous( + internal IBooleanEntry GetEntryEx( ReadOnlySpan typeString, bool defaultValue, PubSubOptions options) { return new BooleanEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Boolean, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/DoubleArrayTopic.cs b/src/ntcore/Generated/DoubleArrayTopic.cs index 77f80510..4ad53054 100644 --- a/src/ntcore/Generated/DoubleArrayTopic.cs +++ b/src/ntcore/Generated/DoubleArrayTopic.cs @@ -58,7 +58,7 @@ public IDoubleArraySubscriber Subscribe( { return new DoubleArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.DoubleArray, "double[]"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IDoubleArraySubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IDoubleArraySubscriber SubscribeExDangerous( + internal IDoubleArraySubscriber SubscribeEx( ReadOnlySpan typeString, double[] defaultValue, PubSubOptions options) { return new DoubleArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.DoubleArray, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IDoubleArrayPublisher Publish( { return new DoubleArrayEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.DoubleArray, "double[]"u8, options), []); @@ -194,14 +194,14 @@ public IDoubleArrayPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IDoubleArrayPublisher PublishExDangerous( + internal IDoubleArrayPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new DoubleArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.DoubleArray, typeString, properties, options), []); @@ -225,14 +225,14 @@ internal IDoubleArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IDoubleArrayPublisher PublishExDangerous( + internal IDoubleArrayPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new DoubleArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.DoubleArray, typeString, properties, options), []); @@ -256,14 +256,14 @@ internal IDoubleArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IDoubleArrayPublisher PublishExDangerous( + internal IDoubleArrayPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new DoubleArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.DoubleArray, typeString, properties, options), []); @@ -295,7 +295,7 @@ public IDoubleArrayEntry GetEntry( { return new DoubleArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.DoubleArray, "double[]"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IDoubleArrayEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IDoubleArrayEntry GetEntryExDangerous( + internal IDoubleArrayEntry GetEntryEx( ReadOnlySpan typeString, double[] defaultValue, PubSubOptions options) { return new DoubleArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.DoubleArray, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/DoubleTopic.cs b/src/ntcore/Generated/DoubleTopic.cs index 3c606b99..cf6a7b4c 100644 --- a/src/ntcore/Generated/DoubleTopic.cs +++ b/src/ntcore/Generated/DoubleTopic.cs @@ -58,7 +58,7 @@ public IDoubleSubscriber Subscribe( { return new DoubleEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Double, "double"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IDoubleSubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IDoubleSubscriber SubscribeExDangerous( + internal IDoubleSubscriber SubscribeEx( ReadOnlySpan typeString, double defaultValue, PubSubOptions options) { return new DoubleEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Double, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IDoublePublisher Publish( { return new DoubleEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.Double, "double"u8, options), 0); @@ -194,14 +194,14 @@ public IDoublePublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IDoublePublisher PublishExDangerous( + internal IDoublePublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new DoubleEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Double, typeString, properties, options), 0); @@ -225,14 +225,14 @@ internal IDoublePublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IDoublePublisher PublishExDangerous( + internal IDoublePublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new DoubleEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Double, typeString, properties, options), 0); @@ -256,14 +256,14 @@ internal IDoublePublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IDoublePublisher PublishExDangerous( + internal IDoublePublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new DoubleEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Double, typeString, properties, options), 0); @@ -295,7 +295,7 @@ public IDoubleEntry GetEntry( { return new DoubleEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Double, "double"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IDoubleEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IDoubleEntry GetEntryExDangerous( + internal IDoubleEntry GetEntryEx( ReadOnlySpan typeString, double defaultValue, PubSubOptions options) { return new DoubleEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Double, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/FloatArrayTopic.cs b/src/ntcore/Generated/FloatArrayTopic.cs index a78dc7ab..ea3b9b61 100644 --- a/src/ntcore/Generated/FloatArrayTopic.cs +++ b/src/ntcore/Generated/FloatArrayTopic.cs @@ -58,7 +58,7 @@ public IFloatArraySubscriber Subscribe( { return new FloatArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.FloatArray, "float[]"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IFloatArraySubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IFloatArraySubscriber SubscribeExDangerous( + internal IFloatArraySubscriber SubscribeEx( ReadOnlySpan typeString, float[] defaultValue, PubSubOptions options) { return new FloatArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.FloatArray, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IFloatArrayPublisher Publish( { return new FloatArrayEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.FloatArray, "float[]"u8, options), []); @@ -194,14 +194,14 @@ public IFloatArrayPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IFloatArrayPublisher PublishExDangerous( + internal IFloatArrayPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new FloatArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.FloatArray, typeString, properties, options), []); @@ -225,14 +225,14 @@ internal IFloatArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IFloatArrayPublisher PublishExDangerous( + internal IFloatArrayPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new FloatArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.FloatArray, typeString, properties, options), []); @@ -256,14 +256,14 @@ internal IFloatArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IFloatArrayPublisher PublishExDangerous( + internal IFloatArrayPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new FloatArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.FloatArray, typeString, properties, options), []); @@ -295,7 +295,7 @@ public IFloatArrayEntry GetEntry( { return new FloatArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.FloatArray, "float[]"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IFloatArrayEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IFloatArrayEntry GetEntryExDangerous( + internal IFloatArrayEntry GetEntryEx( ReadOnlySpan typeString, float[] defaultValue, PubSubOptions options) { return new FloatArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.FloatArray, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/FloatTopic.cs b/src/ntcore/Generated/FloatTopic.cs index 7d216832..b7e7e811 100644 --- a/src/ntcore/Generated/FloatTopic.cs +++ b/src/ntcore/Generated/FloatTopic.cs @@ -58,7 +58,7 @@ public IFloatSubscriber Subscribe( { return new FloatEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Float, "float"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IFloatSubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IFloatSubscriber SubscribeExDangerous( + internal IFloatSubscriber SubscribeEx( ReadOnlySpan typeString, float defaultValue, PubSubOptions options) { return new FloatEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Float, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IFloatPublisher Publish( { return new FloatEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.Float, "float"u8, options), 0); @@ -194,14 +194,14 @@ public IFloatPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IFloatPublisher PublishExDangerous( + internal IFloatPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new FloatEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Float, typeString, properties, options), 0); @@ -225,14 +225,14 @@ internal IFloatPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IFloatPublisher PublishExDangerous( + internal IFloatPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new FloatEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Float, typeString, properties, options), 0); @@ -256,14 +256,14 @@ internal IFloatPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IFloatPublisher PublishExDangerous( + internal IFloatPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new FloatEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Float, typeString, properties, options), 0); @@ -295,7 +295,7 @@ public IFloatEntry GetEntry( { return new FloatEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Float, "float"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IFloatEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IFloatEntry GetEntryExDangerous( + internal IFloatEntry GetEntryEx( ReadOnlySpan typeString, float defaultValue, PubSubOptions options) { return new FloatEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Float, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/IntegerArrayTopic.cs b/src/ntcore/Generated/IntegerArrayTopic.cs index 1f8b7b12..a9239585 100644 --- a/src/ntcore/Generated/IntegerArrayTopic.cs +++ b/src/ntcore/Generated/IntegerArrayTopic.cs @@ -58,7 +58,7 @@ public IIntegerArraySubscriber Subscribe( { return new IntegerArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.IntegerArray, "int[]"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IIntegerArraySubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IIntegerArraySubscriber SubscribeExDangerous( + internal IIntegerArraySubscriber SubscribeEx( ReadOnlySpan typeString, long[] defaultValue, PubSubOptions options) { return new IntegerArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.IntegerArray, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IIntegerArrayPublisher Publish( { return new IntegerArrayEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.IntegerArray, "int[]"u8, options), []); @@ -194,14 +194,14 @@ public IIntegerArrayPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IIntegerArrayPublisher PublishExDangerous( + internal IIntegerArrayPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new IntegerArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.IntegerArray, typeString, properties, options), []); @@ -225,14 +225,14 @@ internal IIntegerArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IIntegerArrayPublisher PublishExDangerous( + internal IIntegerArrayPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new IntegerArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.IntegerArray, typeString, properties, options), []); @@ -256,14 +256,14 @@ internal IIntegerArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IIntegerArrayPublisher PublishExDangerous( + internal IIntegerArrayPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new IntegerArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.IntegerArray, typeString, properties, options), []); @@ -295,7 +295,7 @@ public IIntegerArrayEntry GetEntry( { return new IntegerArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.IntegerArray, "int[]"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IIntegerArrayEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IIntegerArrayEntry GetEntryExDangerous( + internal IIntegerArrayEntry GetEntryEx( ReadOnlySpan typeString, long[] defaultValue, PubSubOptions options) { return new IntegerArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.IntegerArray, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/IntegerTopic.cs b/src/ntcore/Generated/IntegerTopic.cs index 1794ad2c..4c361bfa 100644 --- a/src/ntcore/Generated/IntegerTopic.cs +++ b/src/ntcore/Generated/IntegerTopic.cs @@ -58,7 +58,7 @@ public IIntegerSubscriber Subscribe( { return new IntegerEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Integer, "int"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IIntegerSubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IIntegerSubscriber SubscribeExDangerous( + internal IIntegerSubscriber SubscribeEx( ReadOnlySpan typeString, long defaultValue, PubSubOptions options) { return new IntegerEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Integer, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IIntegerPublisher Publish( { return new IntegerEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.Integer, "int"u8, options), 0); @@ -194,14 +194,14 @@ public IIntegerPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IIntegerPublisher PublishExDangerous( + internal IIntegerPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new IntegerEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Integer, typeString, properties, options), 0); @@ -225,14 +225,14 @@ internal IIntegerPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IIntegerPublisher PublishExDangerous( + internal IIntegerPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new IntegerEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Integer, typeString, properties, options), 0); @@ -256,14 +256,14 @@ internal IIntegerPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IIntegerPublisher PublishExDangerous( + internal IIntegerPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new IntegerEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Integer, typeString, properties, options), 0); @@ -295,7 +295,7 @@ public IIntegerEntry GetEntry( { return new IntegerEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Integer, "int"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IIntegerEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IIntegerEntry GetEntryExDangerous( + internal IIntegerEntry GetEntryEx( ReadOnlySpan typeString, long defaultValue, PubSubOptions options) { return new IntegerEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Integer, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/RawTopic.cs b/src/ntcore/Generated/RawTopic.cs index eeb70614..4edf955c 100644 --- a/src/ntcore/Generated/RawTopic.cs +++ b/src/ntcore/Generated/RawTopic.cs @@ -79,14 +79,14 @@ public IRawSubscriber Subscribe( * @param options subscribe options * @return subscriber */ - internal IRawSubscriber SubscribeDangerous( + internal IRawSubscriber Subscribe( ReadOnlySpan typeString, byte[] defaultValue, PubSubOptions options) { return new RawEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.Raw, typeString, options), defaultValue); @@ -136,13 +136,13 @@ public IRawPublisher Publish( * @param options publish options * @return publisher */ - internal IRawPublisher PublishDangerous( + internal IRawPublisher Publish( ReadOnlySpan typeString, PubSubOptions options) { return new RawEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.Raw, typeString, options), []); @@ -194,14 +194,14 @@ public IRawPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IRawPublisher PublishExDangerous( + internal IRawPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new RawEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Raw, typeString, properties, options), []); @@ -225,14 +225,14 @@ internal IRawPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IRawPublisher PublishExDangerous( + internal IRawPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new RawEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Raw, typeString, properties, options), []); @@ -256,14 +256,14 @@ internal IRawPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IRawPublisher PublishExDangerous( + internal IRawPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new RawEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.Raw, typeString, properties, options), []); @@ -324,14 +324,14 @@ public IRawEntry GetEntry( * @param options publish and/or subscribe options * @return entry */ - internal IRawEntry GetEntryDangerous( + internal IRawEntry GetEntry( ReadOnlySpan typeString, byte[] defaultValue, PubSubOptions options) { return new RawEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.Raw, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/StringArrayTopic.cs b/src/ntcore/Generated/StringArrayTopic.cs index f62b7841..d5e3e4bd 100644 --- a/src/ntcore/Generated/StringArrayTopic.cs +++ b/src/ntcore/Generated/StringArrayTopic.cs @@ -58,7 +58,7 @@ public IStringArraySubscriber Subscribe( { return new StringArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.StringArray, "string[]"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IStringArraySubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IStringArraySubscriber SubscribeExDangerous( + internal IStringArraySubscriber SubscribeEx( ReadOnlySpan typeString, string[] defaultValue, PubSubOptions options) { return new StringArrayEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.StringArray, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IStringArrayPublisher Publish( { return new StringArrayEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.StringArray, "string[]"u8, options), []); @@ -194,14 +194,14 @@ public IStringArrayPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IStringArrayPublisher PublishExDangerous( + internal IStringArrayPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new StringArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.StringArray, typeString, properties, options), []); @@ -225,14 +225,14 @@ internal IStringArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IStringArrayPublisher PublishExDangerous( + internal IStringArrayPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new StringArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.StringArray, typeString, properties, options), []); @@ -256,14 +256,14 @@ internal IStringArrayPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IStringArrayPublisher PublishExDangerous( + internal IStringArrayPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new StringArrayEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.StringArray, typeString, properties, options), []); @@ -295,7 +295,7 @@ public IStringArrayEntry GetEntry( { return new StringArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.StringArray, "string[]"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IStringArrayEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IStringArrayEntry GetEntryExDangerous( + internal IStringArrayEntry GetEntryEx( ReadOnlySpan typeString, string[] defaultValue, PubSubOptions options) { return new StringArrayEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.StringArray, typeString, options), defaultValue); diff --git a/src/ntcore/Generated/StringTopic.cs b/src/ntcore/Generated/StringTopic.cs index 4e33f75e..7e901852 100644 --- a/src/ntcore/Generated/StringTopic.cs +++ b/src/ntcore/Generated/StringTopic.cs @@ -58,7 +58,7 @@ public IStringSubscriber Subscribe( { return new StringEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.String, "string"u8, options), defaultValue); @@ -109,14 +109,14 @@ public IStringSubscriber SubscribeEx( * @param options subscribe options * @return subscriber */ - internal IStringSubscriber SubscribeExDangerous( + internal IStringSubscriber SubscribeEx( ReadOnlySpan typeString, string defaultValue, PubSubOptions options) { return new StringEntryImpl( this, - NtCore.SubscribeDangerous( + NtCore.Subscribe( Handle, NetworkTableType.String, typeString, options), defaultValue); @@ -142,7 +142,7 @@ public IStringPublisher Publish( { return new StringEntryImpl( this, - NtCore.PublishDangerous( + NtCore.Publish( Handle, NetworkTableType.String, "string"u8, options), ""); @@ -194,14 +194,14 @@ public IStringPublisher PublishEx( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IStringPublisher PublishExDangerous( + internal IStringPublisher PublishEx( ReadOnlySpan typeString, string properties, PubSubOptions options) { return new StringEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.String, typeString, properties, options), ""); @@ -225,14 +225,14 @@ internal IStringPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IStringPublisher PublishExDangerous( + internal IStringPublisher PublishEx( string typeString, ReadOnlySpan properties, PubSubOptions options) { return new StringEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.String, typeString, properties, options), ""); @@ -256,14 +256,14 @@ internal IStringPublisher PublishExDangerous( * @return publisher * @throws IllegalArgumentException if properties is not a JSON object */ - internal IStringPublisher PublishExDangerous( + internal IStringPublisher PublishEx( ReadOnlySpan typeString, ReadOnlySpan properties, PubSubOptions options) { return new StringEntryImpl( this, - NtCore.PublishExDangerous( + NtCore.PublishEx( Handle, NetworkTableType.String, typeString, properties, options), ""); @@ -295,7 +295,7 @@ public IStringEntry GetEntry( { return new StringEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.String, "string"u8, options), defaultValue); @@ -356,14 +356,14 @@ public IStringEntry GetEntryEx( * @param options publish and/or subscribe options * @return entry */ - internal IStringEntry GetEntryExDangerous( + internal IStringEntry GetEntryEx( ReadOnlySpan typeString, string defaultValue, PubSubOptions options) { return new StringEntryImpl( this, - NtCore.GetEntryDangerous( + NtCore.GetEntry( Handle, NetworkTableType.String, typeString, options), defaultValue); diff --git a/src/ntcore/LogMessage.cs b/src/ntcore/LogMessage.cs index a2dd80bf..007bf400 100644 --- a/src/ntcore/LogMessage.cs +++ b/src/ntcore/LogMessage.cs @@ -1,5 +1,7 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; +using WPIUtil; +using WPIUtil.Marshal; namespace NetworkTables; @@ -15,8 +17,8 @@ public static LogMessage ConvertToManaged(in NativeLogMessage unmanaged) return new LogMessage { LogLevel = (int)unmanaged.level, - Message = Utf8StringMarshaller.ConvertToManaged(unmanaged.message) ?? "", - Filename = Utf8StringMarshaller.ConvertToManaged(unmanaged.filename) ?? "", + Message = unmanaged.message.ConvertToString(), + Filename = unmanaged.message.ConvertToString(), Line = (int)unmanaged.line }; } @@ -25,9 +27,9 @@ public static LogMessage ConvertToManaged(in NativeLogMessage unmanaged) public unsafe struct NativeLogMessage { public uint level; - public byte* filename; + public WpiStringMarshaller.WpiStringNative filename; public uint line; - public byte* message; + public WpiStringMarshaller.WpiStringNative message; } } diff --git a/src/ntcore/Natives/NetworkTableValueMarshaller.cs b/src/ntcore/Natives/NetworkTableValueMarshaller.cs index 4b43fe25..5f661a7b 100644 --- a/src/ntcore/Natives/NetworkTableValueMarshaller.cs +++ b/src/ntcore/Natives/NetworkTableValueMarshaller.cs @@ -1,6 +1,8 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; +using WPIUtil; +using WPIUtil.Marshal; namespace NetworkTables.Natives; @@ -53,7 +55,7 @@ public struct NtValueUnion public double valueDouble; [FieldOffset(0)] - public NtString valueString; + public WpiStringMarshaller.WpiStringNative valueString; [FieldOffset(0)] public NtValueRaw valueRaw; @@ -108,7 +110,7 @@ public unsafe struct NtValueIntArray public unsafe partial struct NtValueStringArray { - public NtString* arr; + public WpiStringMarshaller.WpiStringNative* arr; public nuint size; } } diff --git a/src/ntcore/Natives/NtCore.ClientServer.cs b/src/ntcore/Natives/NtCore.ClientServer.cs index 1d1cddca..1d680d72 100644 --- a/src/ntcore/Natives/NtCore.ClientServer.cs +++ b/src/ntcore/Natives/NtCore.ClientServer.cs @@ -4,6 +4,7 @@ using System.Runtime.InteropServices.Marshalling; using CommunityToolkit.Diagnostics; using NetworkTables.Handles; +using WPIUtil; using WPIUtil.Marshal; namespace NetworkTables.Natives; @@ -22,33 +23,33 @@ public static partial class NtCore [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void StopLocal(NtInst inst); - [LibraryImport("ntcore", EntryPoint = "NT_StartServer", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_StartServer")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void StartServer(NtInst inst, string persistFilename, string listenAddres, uint port3, uint port4); + public static partial void StartServer(NtInst inst, WpiString persistFilename, WpiString listenAddres, uint port3, uint port4); [LibraryImport("ntcore", EntryPoint = "NT_StopServer")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void StopServer(NtInst inst); - [LibraryImport("ntcore", EntryPoint = "NT_StartClient3", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_StartClient3")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void StartClient3(NtInst inst, string identity); + public static partial void StartClient3(NtInst inst, WpiString identity); - [LibraryImport("ntcore", EntryPoint = "NT_StartClient4", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_StartClient4")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void StartClient4(NtInst inst, string identity); + public static partial void StartClient4(NtInst inst, WpiString identity); [LibraryImport("ntcore", EntryPoint = "NT_StopClient")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void StopClient(NtInst inst); - [LibraryImport("ntcore", EntryPoint = "NT_SetServer", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_SetServer")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void SetServer(NtInst inst, string serverName, uint port); + public static partial void SetServer(NtInst inst, WpiString serverName, uint port); - [LibraryImport("ntcore", EntryPoint = "NT_SetServerMulti", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_SetServerMulti")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial void SetServerMulti(NtInst inst, nuint count, ReadOnlySpan serverNames, ReadOnlySpan ports); + internal static partial void SetServerMulti(NtInst inst, nuint count, [MarshalUsing(typeof(WpiStringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan serverNames, ReadOnlySpan ports); public static void SetServerMulti(NtInst inst, ReadOnlySpan serverNames, ReadOnlySpan ports) { diff --git a/src/ntcore/Natives/NtCore.Datalog.cs b/src/ntcore/Natives/NtCore.Datalog.cs index f4cf4832..d9dec884 100644 --- a/src/ntcore/Natives/NtCore.Datalog.cs +++ b/src/ntcore/Natives/NtCore.Datalog.cs @@ -1,23 +1,24 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using NetworkTables.Handles; +using WPIUtil; using WPIUtil.Natives; namespace NetworkTables.Natives; public static unsafe partial class NtCore { - [LibraryImport("ntcore", EntryPoint = "NT_StartEntryDataLog", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_StartEntryDataLog")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial NtDataLogger StartEntryDataLog(NtInst inst, OpaqueDataLog* log, string prefix, string logPrefix); + public static partial NtDataLogger StartEntryDataLog(NtInst inst, OpaqueDataLog* log, WpiString prefix, WpiString logPrefix); [LibraryImport("ntcore", EntryPoint = "NT_StopEntryDataLog")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void StopEntryDataLog(NtDataLogger logger); - [LibraryImport("ntcore", EntryPoint = "NT_StartConnectionDataLog", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_StartConnectionDataLog")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial NtConnectionDataLogger StartConnectionDataLog(NtInst inst, OpaqueDataLog* log, string name); + public static partial NtConnectionDataLogger StartConnectionDataLog(NtInst inst, OpaqueDataLog* log, WpiString name); [LibraryImport("ntcore", EntryPoint = "NT_StopEntryDataLog")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] diff --git a/src/ntcore/Natives/NtCore.Free.cs b/src/ntcore/Natives/NtCore.Free.cs index f494e782..df838ff6 100644 --- a/src/ntcore/Natives/NtCore.Free.cs +++ b/src/ntcore/Natives/NtCore.Free.cs @@ -9,10 +9,6 @@ public static unsafe partial class NtCore [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void DisposeValue(NetworkTableValueMarshaller.NativeNetworkTableValue* ptr); - [LibraryImport("ntcore", EntryPoint = "NT_DisposeString")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void DisposeString(NtString* ptr); - [LibraryImport("ntcore", EntryPoint = "NT_DisposeValueArray")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void DisposeValueArray(NetworkTableValueMarshaller.NativeNetworkTableValue* ptr, nuint count); diff --git a/src/ntcore/Natives/NtCore.Listener.cs b/src/ntcore/Natives/NtCore.Listener.cs index a423a14d..386be6f5 100644 --- a/src/ntcore/Natives/NtCore.Listener.cs +++ b/src/ntcore/Natives/NtCore.Listener.cs @@ -29,7 +29,7 @@ public static NetworkTableEvent[] ReadListenerQueue(NtListenerPoller poller) [LibraryImport("ntcore", EntryPoint = "NT_AddPolledListenerMultiple")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial NtListener AddListener(NtListenerPoller poller, [MarshalUsing(typeof(StringLengthPairMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan prefixes, nuint prefixesLen, EventFlags flags); + internal static partial NtListener AddListener(NtListenerPoller poller, [MarshalUsing(typeof(WpiStringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan prefixes, nuint prefixesLen, EventFlags flags); public static NtListener AddListener(NtListenerPoller poller, ReadOnlySpan prefixes, EventFlags flags) { diff --git a/src/ntcore/Natives/NtCore.Schema.cs b/src/ntcore/Natives/NtCore.Schema.cs index 48ab0fc0..73b9f716 100644 --- a/src/ntcore/Natives/NtCore.Schema.cs +++ b/src/ntcore/Natives/NtCore.Schema.cs @@ -2,21 +2,22 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using NetworkTables.Handles; +using WPIUtil; namespace NetworkTables.Natives; public static unsafe partial class NtCore { - [LibraryImport("ntcore", EntryPoint = "NT_HasSchema", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_HasSchema")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [return: MarshalAs(UnmanagedType.I4)] - public static partial bool HasSchema(NtInst inst, string name); + public static partial bool HasSchema(NtInst inst, WpiString name); - [LibraryImport("ntcore", EntryPoint = "NT_AddSchema", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_AddSchema")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial void AddSchema(NtInst inst, string name, string type, ReadOnlySpan schema, nuint schemaSize); + internal static partial void AddSchema(NtInst inst, WpiString name, WpiString type, ReadOnlySpan schema, nuint schemaSize); - public static void AddSchema(NtInst inst, string name, string type, ReadOnlySpan schema) + public static void AddSchema(NtInst inst, WpiString name, WpiString type, ReadOnlySpan schema) { AddSchema(inst, name, type, schema, (nuint)schema.Length); } diff --git a/src/ntcore/Natives/NtCore.cs b/src/ntcore/Natives/NtCore.cs index e8511c39..a5ee911a 100644 --- a/src/ntcore/Natives/NtCore.cs +++ b/src/ntcore/Natives/NtCore.cs @@ -4,6 +4,7 @@ using System.Runtime.InteropServices.Marshalling; using System.Text; using NetworkTables.Handles; +using WPIUtil; using WPIUtil.Marshal; namespace NetworkTables.Natives; @@ -24,24 +25,11 @@ public static partial class NtCore [LibraryImport("ntcore", EntryPoint = "NT_GetEntry")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static unsafe partial NtEntry GetEntry(NtInst inst, WriteStringWrapper name, nuint nameLen); + public static unsafe partial NtEntry GetEntry(NtInst inst, WpiString name); - public static unsafe NtEntry GetEntry(NtInst inst, string name) - { - WriteStringWrapper wrapper = name; - return GetEntry(inst, wrapper, wrapper.Len); - } - - // Cannot build source generator due to not being able to get the len for a string source generator [LibraryImport("ntcore", EntryPoint = "NT_GetEntryName")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - [return: MarshalUsing(typeof(NtLengthStringMarshaller<>), CountElementName = nameof(nameLen))] - internal static unsafe partial string GetEntryName(int entry, out nuint nameLen); - - public static unsafe string GetEntryName(T entry) where T : struct, INtEntryHandle - { - return GetEntryName(entry.Handle, out var _); - } + public static unsafe partial void GetEntryName(int entry, [MarshalUsing(typeof(WpiStringMarshaller))] out string name); [LibraryImport("ntcore", EntryPoint = "NT_GetEntryType")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] @@ -96,8 +84,7 @@ public static unsafe bool SetDefaultEntryValue(T entry, in RefNetworkTableVal byte[] stringData = Encoding.UTF8.GetBytes(defaultValue.m_stringValue!); fixed (byte* stringPtr = stringData) { - nativeValue.data.valueString.str = stringPtr; - nativeValue.data.valueString.len = (nuint)stringData.Length; + nativeValue.data.valueString = new(stringPtr, (nuint)stringData.Length); return SetDefaultEntryValue(entry.Handle, &nativeValue); } case NetworkTableType.Raw: @@ -179,8 +166,7 @@ public static unsafe bool SetEntryValue(T entry, in RefNetworkTableValue valu byte[] stringData = Encoding.UTF8.GetBytes(value.m_stringValue!); fixed (byte* stringPtr = stringData) { - nativeValue.data.valueString.str = stringPtr; - nativeValue.data.valueString.len = (nuint)stringData.Length; + nativeValue.data.valueString = new(stringPtr, (nuint)stringData.Length); return SetEntryValue(entry.Handle, &nativeValue); } case NetworkTableType.Raw: @@ -268,45 +254,41 @@ public static unsafe NetworkTableValue[] ReadQueueValue(T subentry) where T : [LibraryImport("ntcore", EntryPoint = "NT_GetTopics")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [return: MarshalUsing(typeof(CustomFreeArrayMarshaller<,>), CountElementName = "count")] - internal static unsafe partial NtTopic[] GetTopics(NtInst inst, WriteStringWrapper prefix, nuint prefixLen, NetworkTableType types, out nuint count); + internal static unsafe partial NtTopic[] GetTopics(NtInst inst, WpiString prefix, NetworkTableType types, out nuint count); - public static unsafe NtTopic[] GetTopics(NtInst inst, string prefix, NetworkTableType types) + public static unsafe NtTopic[] GetTopics(NtInst inst, WpiString prefix, NetworkTableType types) { - WriteStringWrapper wrapper = prefix; - return GetTopics(inst, wrapper, wrapper.Len, types, out var _); + return GetTopics(inst, prefix, types, out var _); } [LibraryImport("ntcore", EntryPoint = "NT_GetTopicsStr")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [return: MarshalUsing(typeof(CustomFreeArrayMarshaller<,>), CountElementName = "count")] - internal static unsafe partial NtTopic[] GetTopics(NtInst inst, WriteStringWrapper prefix, nuint prefixLen, [MarshalUsing(typeof(Utf8StringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan types, nuint typesLen, out nuint count); + internal static unsafe partial NtTopic[] GetTopics(NtInst inst, WpiString prefix, [MarshalUsing(typeof(Utf8StringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan types, nuint typesLen, out nuint count); public static unsafe NtTopic[] GetTopics(NtInst inst, string prefix, ReadOnlySpan types) { - WriteStringWrapper wrapper = prefix; - return GetTopics(inst, wrapper, wrapper.Len, types, (nuint)types.Length, out var _); + return GetTopics(inst, prefix, types, (nuint)types.Length, out var _); } [LibraryImport("ntcore", EntryPoint = "NT_GetTopicInfos")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [return: MarshalUsing(typeof(CustomFreeArrayMarshaller<,>), CountElementName = nameof(count))] - internal static unsafe partial TopicInfo[] GetTopicInfos(NtInst inst, WriteStringWrapper prefix, nuint prefixLen, NetworkTableType types, out nuint count); + internal static unsafe partial TopicInfo[] GetTopicInfos(NtInst inst, WpiString prefix, NetworkTableType types, out nuint count); public static TopicInfo[] GetTopicInfos(NtInst inst, string prefix, NetworkTableType types) { - WriteStringWrapper wrapper = prefix; - return GetTopicInfos(inst, wrapper, wrapper.Len, types, out var _); + return GetTopicInfos(inst, prefix, types, out var _); } [LibraryImport("ntcore", EntryPoint = "NT_GetTopicInfosStr")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [return: MarshalUsing(typeof(CustomFreeArrayMarshaller<,>), CountElementName = nameof(count))] - internal static unsafe partial TopicInfo[] GetTopicInfos(NtInst inst, WriteStringWrapper prefix, nuint prefixLen, [MarshalUsing(typeof(Utf8StringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan types, nuint typesLen, out nuint count); + internal static unsafe partial TopicInfo[] GetTopicInfos(NtInst inst, WpiString prefix, [MarshalUsing(typeof(Utf8StringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan types, nuint typesLen, out nuint count); public static TopicInfo[] GetTopicInfos(NtInst inst, string prefix, ReadOnlySpan types) { - WriteStringWrapper wrapper = prefix; - return GetTopicInfos(inst, wrapper, wrapper.Len, types, (nuint)types.Length, out var _); + return GetTopicInfos(inst, prefix, types, (nuint)types.Length, out var _); } [LibraryImport("ntcore", EntryPoint = "NT_GetTopicInfo")] @@ -323,23 +305,12 @@ public static TopicInfo[] GetTopicInfos(NtInst inst, string prefix, ReadOnlySpan [LibraryImport("ntcore", EntryPoint = "NT_GetTopic")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static unsafe partial NtTopic GetTopic(NtInst inst, WriteStringWrapper name, nuint nameLen); - - public static NtTopic GetTopic(NtInst inst, string name) - { - WriteStringWrapper wrapper = name; - return GetTopic(inst, wrapper, wrapper.Len); - } + public static unsafe partial NtTopic GetTopic(NtInst inst, WpiString name); [LibraryImport("ntcore", EntryPoint = "NT_GetTopicName")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - [return: MarshalUsing(typeof(NtLengthStringMarshaller<>), CountElementName = nameof(nameLen))] - internal static unsafe partial string GetTopicName(NtTopic topic, out nuint nameLen); + public static unsafe partial void GetTopicName(NtTopic topic, [MarshalUsing(typeof(WpiStringMarshaller))] out string name); - public static unsafe string GetTopicName(NtTopic topic) - { - return GetTopicName(topic, out var _); - } [LibraryImport("ntcore", EntryPoint = "NT_GetTopicType")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] @@ -347,13 +318,7 @@ public static unsafe string GetTopicName(NtTopic topic) [LibraryImport("ntcore", EntryPoint = "NT_GetTopicTypeString")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - [return: MarshalUsing(typeof(NtLengthStringMarshaller<>), CountElementName = nameof(typeLen))] - internal static unsafe partial string GetTopicTypeString(NtTopic topic, out nuint typeLen); - - public static unsafe string GetTopicTypeString(NtTopic topic) - { - return GetTopicTypeString(topic, out var _); - } + internal static unsafe partial void GetTopicTypeString(NtTopic topic, [MarshalUsing(typeof(WpiStringMarshaller))] out string name); [LibraryImport("ntcore", EntryPoint = "NT_SetTopicPersistent")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] @@ -397,74 +362,42 @@ public static bool GetTopicExists(T handle) where T : struct, INtEntryHandle return GetTopicExists(handle.Handle); } - [LibraryImport("ntcore", EntryPoint = "NT_GetTopicProperty", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_GetTopicProperty")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - [return: MarshalUsing(typeof(NtLengthStringMarshaller<>), CountElementName = nameof(len))] - internal static unsafe partial string GetTopicProperty(NtTopic topic, string name, out nuint len); + public static unsafe partial void GetTopicProperty(NtTopic topic, WpiString name, [MarshalUsing(typeof(WpiStringMarshaller))] out string property); - public static unsafe string GetTopicProperty(NtTopic topic, string name) - { - return GetTopicProperty(topic, name, out var _); - } - - [LibraryImport("ntcore", EntryPoint = "NT_SetTopicProperty", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_SetTopicProperty")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [return: MarshalAs(UnmanagedType.I4)] - public static partial bool SetTopicProperty(NtTopic topic, string name, string value); + public static partial bool SetTopicProperty(NtTopic topic, WpiString name, WpiString value); - [LibraryImport("ntcore", EntryPoint = "NT_DeleteTopicProperty", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_DeleteTopicProperty")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void DeleteTopicProperty(NtTopic topic, string name); + public static partial void DeleteTopicProperty(NtTopic topic, WpiString name); [LibraryImport("ntcore", EntryPoint = "NT_GetTopicProperties")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - [return: MarshalUsing(typeof(NtLengthStringMarshaller<>), CountElementName = nameof(len))] - internal static unsafe partial string GetTopicProperties(NtTopic topic, out nuint len); + public static unsafe partial void GetTopicProperties(NtTopic topic, [MarshalUsing(typeof(WpiStringMarshaller))] out string properties); - public static string GetTopicProperties(NtTopic topic) - { - return GetTopicProperties(topic, out var _); - } - - [LibraryImport("ntcore", EntryPoint = "NT_SetTopicProperties", StringMarshalling = StringMarshalling.Utf8)] + [LibraryImport("ntcore", EntryPoint = "NT_SetTopicProperties")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial void SetTopicProperties(NtTopic topic, string properties); - - [LibraryImport("ntcore", EntryPoint = "NT_Subscribe", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial NtSubscriber Subscribe(NtTopic topic, NetworkTableType type, string typeStr, in PubSubOptions options); + public static partial void SetTopicProperties(NtTopic topic, WpiString properties); [LibraryImport("ntcore", EntryPoint = "NT_Subscribe")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial NtSubscriber SubscribeDangerous(NtTopic topic, NetworkTableType type, ReadOnlySpan typeStr, in PubSubOptions options); + public static partial NtSubscriber Subscribe(NtTopic topic, NetworkTableType type, WpiString typeStr, in PubSubOptions options); [LibraryImport("ntcore", EntryPoint = "NT_Unsubscribe")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] public static partial void Unsubscribe(NtSubscriber sub); - [LibraryImport("ntcore", EntryPoint = "NT_Publish", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial NtPublisher Publish(NtTopic topic, NetworkTableType type, string typeStr, in PubSubOptions options); - [LibraryImport("ntcore", EntryPoint = "NT_Publish")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial NtPublisher PublishDangerous(NtTopic topic, NetworkTableType type, ReadOnlySpan typeStr, in PubSubOptions options); - - [LibraryImport("ntcore", EntryPoint = "NT_PublishEx", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial NtPublisher PublishEx(NtTopic topic, NetworkTableType type, string typeStr, string properties, in PubSubOptions options); - - [LibraryImport("ntcore", EntryPoint = "NT_PublishEx", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial NtPublisher PublishExDangerous(NtTopic topic, NetworkTableType type, ReadOnlySpan typeStr, string properties, in PubSubOptions options); - - [LibraryImport("ntcore", EntryPoint = "NT_PublishEx", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial NtPublisher PublishExDangerous(NtTopic topic, NetworkTableType type, string typeStr, ReadOnlySpan properties, in PubSubOptions options); + public static partial NtPublisher Publish(NtTopic topic, NetworkTableType type, WpiString typeStr, in PubSubOptions options); [LibraryImport("ntcore", EntryPoint = "NT_PublishEx")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial NtPublisher PublishExDangerous(NtTopic topic, NetworkTableType type, ReadOnlySpan typeStr, ReadOnlySpan properties, in PubSubOptions options); + public static partial NtPublisher PublishEx(NtTopic topic, NetworkTableType type, WpiString typeStr, WpiString properties, in PubSubOptions options); [LibraryImport("ntcore", EntryPoint = "NT_Unpublish")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] @@ -475,13 +408,9 @@ public static void Unpublish(T pubsubentry) where T : struct, INtEntryHandle Unpublish(pubsubentry.Handle); } - [LibraryImport("ntcore", EntryPoint = "NT_GetEntryEx", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static unsafe partial NtEntry GetEntry(NtTopic topic, NetworkTableType type, string typeStr, in PubSubOptions options); - [LibraryImport("ntcore", EntryPoint = "NT_GetEntryEx")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static unsafe partial NtEntry GetEntryDangerous(NtTopic topic, NetworkTableType type, ReadOnlySpan typeStr, in PubSubOptions options); + public static unsafe partial NtEntry GetEntry(NtTopic topic, NetworkTableType type, WpiString typeStr, in PubSubOptions options); [LibraryImport("ntcore", EntryPoint = "NT_ReleaseEntry")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] @@ -515,7 +444,12 @@ public static NtTopic GetTopicFromHandle(T pubsubentry) where T : struct, INt [LibraryImport("ntcore", EntryPoint = "NT_SubscribeMultiple")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - public static partial NtMultiSubscriber SubscribeMultiple(NtInst inst, [MarshalUsing(typeof(StringLengthPairMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan prefixes, nuint prefixesLen, in PubSubOptions options); + internal static partial NtMultiSubscriber SubscribeMultiple(NtInst inst, [MarshalUsing(typeof(WpiStringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan prefixes, nuint prefixesLen, in PubSubOptions options); + + public static NtMultiSubscriber SubscribeMultiple(NtInst inst, [MarshalUsing(typeof(WpiStringMarshaller), ElementIndirectionDepth = 1)] ReadOnlySpan prefixes, in PubSubOptions options) + { + return SubscribeMultiple(inst, prefixes, (nuint)prefixes.Length, options); + } [LibraryImport("ntcore", EntryPoint = "NT_UnsubscribeMultiple")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] diff --git a/src/ntcore/Natives/NtLengthStringMarshaller.cs b/src/ntcore/Natives/NtLengthStringMarshaller.cs deleted file mode 100644 index a691f351..00000000 --- a/src/ntcore/Natives/NtLengthStringMarshaller.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Runtime.InteropServices.Marshalling; -using System.Text; - -namespace NetworkTables.Natives; - -[CustomMarshaller(typeof(string), MarshalMode.ManagedToUnmanagedOut, typeof(NtLengthStringMarshaller<>))] -[ContiguousCollectionMarshaller] -public static unsafe class NtLengthStringMarshaller where TUnmanagedElement : unmanaged -{ - public static string AllocateContainerForManagedElements(TUnmanagedElement* unmanaged, int numElements) - { - if (unmanaged is null) - return ""; - - if (typeof(TUnmanagedElement) != typeof(byte)) - { - throw new InvalidOperationException("TUnmanagedElement must be byte"); - } - - string ret = Encoding.UTF8.GetString((byte*)unmanaged, numElements); - NtCore.FreeCharArray((byte*)unmanaged); - return ret; - } - - public static Span GetManagedValuesDestination(string managed) - => new(); - - public static ReadOnlySpan GetUnmanagedValuesSource(TUnmanagedElement* unmanagedValue, int numElements) - => new(); -} diff --git a/src/ntcore/Natives/NtString.cs b/src/ntcore/Natives/NtString.cs deleted file mode 100644 index da10ff6d..00000000 --- a/src/ntcore/Natives/NtString.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Runtime.InteropServices; -using WPIUtil.Marshal; - -namespace NetworkTables.Natives; - -[StructLayout(LayoutKind.Sequential)] -public unsafe struct NtString : INativeFree, IStringLengthPair -{ - public byte* str; - public nuint len; - - public byte* Ptr { readonly get => str; set => str = value; } - public nuint Len { readonly get => len; set => len = value; } - - public static void Free(NtString* ptr) - { - NtCore.DisposeString(ptr); - } -} diff --git a/src/ntcore/Natives/WriteStringWrapper.cs b/src/ntcore/Natives/WriteStringWrapper.cs deleted file mode 100644 index 9ad312ed..00000000 --- a/src/ntcore/Natives/WriteStringWrapper.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Runtime.InteropServices.Marshalling; -using System.Text; - -namespace NetworkTables.Natives; - -[CustomMarshaller(typeof(WriteStringWrapper), MarshalMode.ManagedToUnmanagedIn, typeof(WriteStringWrapperMarshaller))] -public static unsafe class WriteStringWrapperMarshaller -{ - public static ref readonly byte GetPinnableReference(WriteStringWrapper managed) - { - return ref managed.Str.AsSpan().GetPinnableReference(); - } - - public static byte* ConvertToUnmanaged(WriteStringWrapper managed) - { - throw new NotSupportedException("Have to have to satify the marshaller"); - } -} - -[NativeMarshalling(typeof(WriteStringWrapperMarshaller))] -public readonly ref struct WriteStringWrapper(string value) -{ - public readonly byte[] Str = Encoding.UTF8.GetBytes(value); - - public nuint Len => (nuint)Str.Length; - - public static implicit operator WriteStringWrapper(string value) - { - return new WriteStringWrapper(value); - } -} diff --git a/src/ntcore/NetworkTableInstance.cs b/src/ntcore/NetworkTableInstance.cs index 58445218..9dcb5c90 100644 --- a/src/ntcore/NetworkTableInstance.cs +++ b/src/ntcore/NetworkTableInstance.cs @@ -506,7 +506,7 @@ public void AddSchema(string name, string type, ReadOnlySpan schema) bool added = false; var pub = m_schemas.GetOrAdd($"/.schema/{name}", k => { - IRawPublisher pub = GetRawTopic(k).PublishExDangerous(type, "{\"retained\":true}"u8, PubSubOptions.None); + IRawPublisher pub = GetRawTopic(k).PublishEx(type, "{\"retained\":true}"u8, PubSubOptions.None); added = true; return pub; }); @@ -520,7 +520,7 @@ public void AddSchema(string name, string type, string schema) { var pub = m_schemas.GetOrAdd($"/.schema/{name}", k => { - IRawPublisher pub = GetRawTopic(k).PublishExDangerous(type, "{\"retained\":true}"u8, PubSubOptions.None); + IRawPublisher pub = GetRawTopic(k).PublishEx(type, "{\"retained\":true}"u8, PubSubOptions.None); pub.SetDefault(Encoding.UTF8.GetBytes(schema)); return pub; }); diff --git a/src/ntcore/Topic.cs b/src/ntcore/Topic.cs index e113eb51..56b2749e 100644 --- a/src/ntcore/Topic.cs +++ b/src/ntcore/Topic.cs @@ -20,11 +20,25 @@ internal Topic(NetworkTableInstance inst, NtTopic handle) public NetworkTableInstance Instance { get; } - public string Name => NtCore.GetTopicName(Handle); + public string Name + { + get + { + NtCore.GetTopicName(Handle, out var name); + return name; + } + } public NetworkTableType Type => NtCore.GetTopicType(Handle); - public string TypeString => NtCore.GetTopicTypeString(Handle); + public string TypeString + { + get + { + NtCore.GetTopicTypeString(Handle, out var typeString); + return typeString; + } + } public TopicInfo? Info => NtCore.GetTopicInfo(Handle); @@ -48,7 +62,11 @@ public bool Cached public bool Exists => NtCore.GetTopicExists(Handle); - public string GetProperty(string name) => NtCore.GetTopicProperty(Handle, name); + public string GetProperty(string name) + { + NtCore.GetTopicProperty(Handle, name, out var property); + return property; + } public void SetProperty(string name, string value) => NtCore.SetTopicProperty(Handle, name, value); @@ -56,7 +74,12 @@ public bool Cached public string Properties { - get => NtCore.GetTopicProperties(Handle); + get + { + NtCore.GetTopicProperties(Handle, out var properties); + return properties; + } + set => NtCore.SetTopicProperties(Handle, value); } diff --git a/src/ntcore/TopicInfo.cs b/src/ntcore/TopicInfo.cs index a407ccc2..88807de9 100644 --- a/src/ntcore/TopicInfo.cs +++ b/src/ntcore/TopicInfo.cs @@ -50,10 +50,10 @@ public static TopicInfo ConvertToManaged(in NativeTopicInfo unmanaged) return new TopicInfo { TopicHandle = new NtTopic(unmanaged.topic), - Name = StringLengthPairMarshaller.ManagedConvert(unmanaged.name) ?? "", + Name = unmanaged.name.ConvertToString(), Type = unmanaged.type, - TypeStr = StringLengthPairMarshaller.ManagedConvert(unmanaged.typeStr) ?? "", - Properties = StringLengthPairMarshaller.ManagedConvert(unmanaged.properties) ?? "", + TypeStr = unmanaged.typeStr.ConvertToString(), + Properties = unmanaged.properties.ConvertToString(), }; } @@ -68,10 +68,10 @@ public static NativeTopicInfo ConvertToUnmanaged(in TopicInfo managed) public struct NativeTopicInfo { public int topic; - public NtString name; + public WpiStringMarshaller.WpiStringNative name; public NetworkTableType type; - public NtString typeStr; - public NtString properties; + public WpiStringMarshaller.WpiStringNative typeStr; + public WpiStringMarshaller.WpiStringNative properties; } } diff --git a/src/wpiutil/Marshal/WPIStringMarshaller.cs b/src/wpiutil/Marshal/WPIStringMarshaller.cs index 930eb2ac..b609a347 100644 --- a/src/wpiutil/Marshal/WPIStringMarshaller.cs +++ b/src/wpiutil/Marshal/WPIStringMarshaller.cs @@ -133,5 +133,15 @@ public readonly unsafe struct WpiStringNative(byte* str, nuint len) { public readonly byte* Str = str; public readonly nuint Len = len; + + public readonly string ConvertToString() + { + if (Len == 0) + { + return ""; + } + + return Encoding.UTF8.GetString(Str, checked((int)Len)); + } } } diff --git a/test/ntcore.test/NtStringMarshallerTests.cs b/test/ntcore.test/NtStringMarshallerTests.cs index d1fed394..d327a975 100644 --- a/test/ntcore.test/NtStringMarshallerTests.cs +++ b/test/ntcore.test/NtStringMarshallerTests.cs @@ -1,140 +1,139 @@ using NetworkTables.Natives; using Xunit; -using Marshaller = WPIUtil.Marshal.StringLengthPairMarshaller; namespace NetworkTables; -public unsafe class NtStringMarshallerTests -{ - [Fact] - public void TestNullString() - { - NtString tmp = new(); - try - { - tmp = Marshaller.PassToArray.ConvertToUnmanaged(null); - Assert.Equal((nuint)0, tmp.len); - Assert.True(tmp.str is not null); - } - finally - { - Marshaller.PassToArray.Free(tmp); - } - } +// public unsafe class NtStringMarshallerTests +// { +// [Fact] +// public void TestNullString() +// { +// NtString tmp = new(); +// try +// { +// tmp = Marshaller.PassToArray.ConvertToUnmanaged(null); +// Assert.Equal((nuint)0, tmp.len); +// Assert.True(tmp.str is not null); +// } +// finally +// { +// Marshaller.PassToArray.Free(tmp); +// } +// } - [Fact] - public void TestEmptyString() - { - NtString tmp = new(); - try - { - tmp = Marshaller.PassToArray.ConvertToUnmanaged(""); - Assert.Equal((nuint)0, tmp.len); - Assert.True(tmp.str is not null); - } - finally - { - Marshaller.PassToArray.Free(tmp); - } - } +// [Fact] +// public void TestEmptyString() +// { +// NtString tmp = new(); +// try +// { +// tmp = Marshaller.PassToArray.ConvertToUnmanaged(""); +// Assert.Equal((nuint)0, tmp.len); +// Assert.True(tmp.str is not null); +// } +// finally +// { +// Marshaller.PassToArray.Free(tmp); +// } +// } - [Fact] - public void TestString() - { - ReadOnlySpan str = "Hello World"u8; - NtString tmp = new(); - try - { - tmp = Marshaller.PassToArray.ConvertToUnmanaged("Hello World"); - Assert.Equal((nuint)str.Length, tmp.len); - Assert.True(tmp.str is not null); - Assert.True(str.SequenceEqual(new ReadOnlySpan(tmp.str, (int)tmp.len))); - } - finally - { - Marshaller.PassToArray.Free(tmp); - } - } +// [Fact] +// public void TestString() +// { +// ReadOnlySpan str = "Hello World"u8; +// NtString tmp = new(); +// try +// { +// tmp = Marshaller.PassToArray.ConvertToUnmanaged("Hello World"); +// Assert.Equal((nuint)str.Length, tmp.len); +// Assert.True(tmp.str is not null); +// Assert.True(str.SequenceEqual(new ReadOnlySpan(tmp.str, (int)tmp.len))); +// } +// finally +// { +// Marshaller.PassToArray.Free(tmp); +// } +// } - [Fact] - public void TestStringIntegratedNull() - { - ReadOnlySpan str = "Hello\0World"u8; - NtString tmp = new(); - try - { - tmp = Marshaller.PassToArray.ConvertToUnmanaged("Hello\0World"); - Assert.Equal((nuint)str.Length, tmp.len); - Assert.True(tmp.str is not null); - Assert.True(str.SequenceEqual(new ReadOnlySpan(tmp.str, (int)tmp.len))); - } - finally - { - Marshaller.PassToArray.Free(tmp); - } - } +// [Fact] +// public void TestStringIntegratedNull() +// { +// ReadOnlySpan str = "Hello\0World"u8; +// NtString tmp = new(); +// try +// { +// tmp = Marshaller.PassToArray.ConvertToUnmanaged("Hello\0World"); +// Assert.Equal((nuint)str.Length, tmp.len); +// Assert.True(tmp.str is not null); +// Assert.True(str.SequenceEqual(new ReadOnlySpan(tmp.str, (int)tmp.len))); +// } +// finally +// { +// Marshaller.PassToArray.Free(tmp); +// } +// } - [Fact] - public void TestUnmanagedToManagedNull() - { - NtString tmp = new NtString() - { - str = null, - len = 0, - }; - Assert.Null(Marshaller.ManagedConvert(tmp)); - } +// [Fact] +// public void TestUnmanagedToManagedNull() +// { +// NtString tmp = new NtString() +// { +// str = null, +// len = 0, +// }; +// Assert.Null(Marshaller.ManagedConvert(tmp)); +// } - [Fact] - public void TestUnmanagedToManaged0Length() - { - byte b = 0; - NtString tmp = new NtString() - { - str = &b, - len = 0, - }; - Assert.Equal("", Marshaller.ManagedConvert(tmp)); - } +// [Fact] +// public void TestUnmanagedToManaged0Length() +// { +// byte b = 0; +// NtString tmp = new NtString() +// { +// str = &b, +// len = 0, +// }; +// Assert.Equal("", Marshaller.ManagedConvert(tmp)); +// } - [Fact] - public void TestUnmanagedToManagedNullLongerLength() - { - NtString tmp = new NtString() - { - str = null, - len = 42, - }; - Assert.Null(Marshaller.ManagedConvert(tmp)); - } +// [Fact] +// public void TestUnmanagedToManagedNullLongerLength() +// { +// NtString tmp = new NtString() +// { +// str = null, +// len = 42, +// }; +// Assert.Null(Marshaller.ManagedConvert(tmp)); +// } - [Fact] - public void TestUnmanagedToManagedString() - { - ReadOnlySpan span = "Hello World"u8; - fixed (byte* pinned = span) - { - NtString tmp = new NtString() - { - str = pinned, - len = (nuint)span.Length, - }; - Assert.Equal("Hello World", Marshaller.ManagedConvert(tmp)); - } - } +// [Fact] +// public void TestUnmanagedToManagedString() +// { +// ReadOnlySpan span = "Hello World"u8; +// fixed (byte* pinned = span) +// { +// NtString tmp = new NtString() +// { +// str = pinned, +// len = (nuint)span.Length, +// }; +// Assert.Equal("Hello World", Marshaller.ManagedConvert(tmp)); +// } +// } - [Fact] - public void TestUnmanagedToManagedStringContainingNull() - { - ReadOnlySpan span = "Hello\0World"u8; - fixed (byte* pinned = span) - { - NtString tmp = new NtString() - { - str = pinned, - len = (nuint)span.Length, - }; - Assert.Equal("Hello\0World", Marshaller.ManagedConvert(tmp)); - } - } -} +// [Fact] +// public void TestUnmanagedToManagedStringContainingNull() +// { +// ReadOnlySpan span = "Hello\0World"u8; +// fixed (byte* pinned = span) +// { +// NtString tmp = new NtString() +// { +// str = pinned, +// len = (nuint)span.Length, +// }; +// Assert.Equal("Hello\0World", Marshaller.ManagedConvert(tmp)); +// } +// } +// } diff --git a/test/ntcore.test/StringUtf8ReturnMarshallerTests.cs b/test/ntcore.test/StringUtf8ReturnMarshallerTests.cs index e4b6cc17..d49f5275 100644 --- a/test/ntcore.test/StringUtf8ReturnMarshallerTests.cs +++ b/test/ntcore.test/StringUtf8ReturnMarshallerTests.cs @@ -1,38 +1,38 @@ using NetworkTables.Natives; using Xunit; -using Marshaller = NetworkTables.Natives.NtLengthStringMarshaller; +//using Marshaller = NetworkTables.Natives.NtLengthStringMarshaller; namespace NetworkTables; -public unsafe class StringUtf8ReturnMarshallerTests -{ - [Fact] - public unsafe void CopyWorks() - { - Marshaller.GetUnmanagedValuesSource(null, 0).CopyTo(Marshaller.GetManagedValuesDestination("")); - } +// public unsafe class StringUtf8ReturnMarshallerTests +// { +// [Fact] +// public unsafe void CopyWorks() +// { +// Marshaller.GetUnmanagedValuesSource(null, 0).CopyTo(Marshaller.GetManagedValuesDestination("")); +// } - [Fact] - public unsafe void NullIsEmpty() - { - Assert.Equal("", Marshaller.AllocateContainerForManagedElements(null, 0)); - } +// [Fact] +// public unsafe void NullIsEmpty() +// { +// Assert.Equal("", Marshaller.AllocateContainerForManagedElements(null, 0)); +// } - [Fact] - public unsafe void ValidStringWorks() - { - ReadOnlySpan testString = "UTF8String"u8; - byte* allocate = NtCore.AllocateCharArray((nuint)testString.Length); - testString.CopyTo(new Span(allocate, testString.Length)); - Assert.Equal("UTF8String", Marshaller.AllocateContainerForManagedElements(allocate, testString.Length)); - } +// [Fact] +// public unsafe void ValidStringWorks() +// { +// ReadOnlySpan testString = "UTF8String"u8; +// byte* allocate = NtCore.AllocateCharArray((nuint)testString.Length); +// testString.CopyTo(new Span(allocate, testString.Length)); +// Assert.Equal("UTF8String", Marshaller.AllocateContainerForManagedElements(allocate, testString.Length)); +// } - [Fact] - public unsafe void IntegratedNullTerminator() - { - ReadOnlySpan testString = "UTF8\0String"u8; - byte* allocate = NtCore.AllocateCharArray((nuint)testString.Length); - testString.CopyTo(new Span(allocate, testString.Length)); - Assert.Equal("UTF8\0String", Marshaller.AllocateContainerForManagedElements(allocate, testString.Length)); - } -} +// [Fact] +// public unsafe void IntegratedNullTerminator() +// { +// ReadOnlySpan testString = "UTF8\0String"u8; +// byte* allocate = NtCore.AllocateCharArray((nuint)testString.Length); +// testString.CopyTo(new Span(allocate, testString.Length)); +// Assert.Equal("UTF8\0String", Marshaller.AllocateContainerForManagedElements(allocate, testString.Length)); +// } +// }