-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
160 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Net; | ||
using System.Text.Json.Serialization; | ||
using OpenShock.ShockOsc.Utils; | ||
|
||
// ReSharper disable InconsistentNaming | ||
|
||
namespace OpenShock.ShockOsc.OscQueryLibrary; | ||
|
||
public sealed class HostInfo | ||
{ | ||
[JsonPropertyName("NAME")] | ||
public required string Name { get; set; } | ||
|
||
[JsonPropertyName("OSC_IP")] | ||
[JsonConverter(typeof(JsonIPAddressConverter))] | ||
public required IPAddress OscIp { get; set; } | ||
|
||
[JsonPropertyName("OSC_PORT")] | ||
public required ushort OscPort { get; set; } | ||
|
||
[JsonPropertyName("OSC_TRANSPORT")] | ||
[JsonConverter(typeof(JsonStringEnumConverter<OscTransportType>))] | ||
public required OscTransportType OscTransport { get; set; } | ||
|
||
[JsonPropertyName("EXTENSIONS")] | ||
public required ExtensionsNode Extensions { get; set; } | ||
|
||
public enum OscTransportType | ||
{ | ||
TCP, | ||
UDP | ||
} | ||
|
||
public sealed class ExtensionsNode | ||
{ | ||
[JsonPropertyName("ACCESS")] | ||
public required bool Access { get; set; } | ||
|
||
[JsonPropertyName("CLIPMODE")] | ||
public required bool ClipMode { get; set; } | ||
|
||
[JsonPropertyName("RANGE")] | ||
public required bool Range { get; set; } | ||
|
||
[JsonPropertyName("TYPE")] | ||
public required bool Type { get; set; } | ||
|
||
[JsonPropertyName("VALUE")] | ||
public required bool Value { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OpenShock.ShockOsc.OscQueryLibrary; | ||
|
||
// technically every class in the JSON is this "Node" class but that's gross | ||
public class Node | ||
{ | ||
[JsonPropertyName("DESCRIPTION")] | ||
public string? Description { get; set; } | ||
|
||
[JsonPropertyName("FULL_PATH")] | ||
public required string FullPath { get; set; } | ||
|
||
[JsonPropertyName("ACCESS")] | ||
public required int Access { get; set; } | ||
} | ||
|
||
public class Node<T> : Node | ||
{ | ||
[JsonPropertyName("CONTENTS")] | ||
public T? Contents { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,32 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OpenShock.ShockOsc.OscQueryLibrary; | ||
|
||
public class OscQueryModels | ||
public sealed class RootNode : Node<RootNode.RootContents> | ||
{ | ||
public class RootNode | ||
{ | ||
public SubNode? CONTENTS { get; set; } | ||
} | ||
|
||
public class SubNode | ||
public sealed class RootContents | ||
{ | ||
public Node input { get; set; } | ||
public TrackingRootNode tracking { get; set; } | ||
public Node chatbox { get; set; } | ||
public AvatarRootNode? avatar { get; set; } | ||
[JsonPropertyName("avatar")] public Node<AvatarContents>? Avatar { get; set; } | ||
} | ||
} | ||
|
||
public class TrackingRootNode | ||
{ | ||
public string FULL_PATH { get; set; } | ||
public int ACCESS { get; set; } | ||
public TrackingNode CONTENTS { get; set; } | ||
} | ||
public sealed class AvatarContents | ||
{ | ||
[JsonPropertyName("change")] public required OscParameterNodeEnd<string> Change { get; set; } | ||
|
||
public class TrackingNode | ||
{ | ||
public Node trackers { get; set; } | ||
public Node eye { get; set; } | ||
public Node vrsystem { get; set; } | ||
} | ||
[JsonPropertyName("parameters")] public Node<IDictionary<string, OscParameterNode>>? Parameters { get; set; } | ||
} | ||
|
||
public class AvatarRootNode | ||
{ | ||
public string FULL_PATH { get; set; } | ||
public int ACCESS { get; set; } | ||
public AvatarNode CONTENTS { get; set; } | ||
} | ||
public sealed class OscParameterNode : Node<IDictionary<string, OscParameterNode>> | ||
{ | ||
[JsonPropertyName("TYPE")] public string? Type { get; set; } | ||
|
||
public class AvatarNode | ||
{ | ||
public Node change { get; set; } | ||
public Node? parameters { get; set; } | ||
} | ||
[JsonPropertyName("VALUE")] public IEnumerable<object>? Value { get; set; } | ||
} | ||
|
||
// technically every class in the JSON is this "Node" class but that's gross | ||
public class Node | ||
{ | ||
public string? DESCRIPTION { get; set; } | ||
public string FULL_PATH { get; set; } | ||
public int ACCESS { get; set; } | ||
public Dictionary<string, Node>? CONTENTS { get; set; } | ||
public string? TYPE { get; set; } | ||
public List<object>? VALUE { get; set; } | ||
} | ||
public sealed class OscParameterNodeEnd<T> : Node | ||
{ | ||
[JsonPropertyName("TYPE")] public required string Type { get; set; } | ||
|
||
public class HostInfo | ||
{ | ||
public string NAME { get; set; } | ||
public string OSC_IP { get; set; } | ||
public int OSC_PORT { get; set; } | ||
public string OSC_TRANSPORT { get; set; } | ||
public Extensions EXTENSIONS { get; set; } | ||
} | ||
|
||
public class Extensions | ||
{ | ||
public bool ACCESS { get; set; } | ||
public bool CLIPMODE { get; set; } | ||
public bool RANGE { get; set; } | ||
public bool TYPE { get; set; } | ||
public bool VALUE { get; set; } | ||
} | ||
[JsonPropertyName("VALUE")] public required IEnumerable<T> Value { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Buffers; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OpenShock.ShockOsc.Utils; | ||
|
||
/// <summary> | ||
/// JSON converter for <see cref="IPAddress"/> that uses the <see cref="IPAddress.TryFormat(Span{char}, out int)"/> method. | ||
/// </summary> | ||
public class JsonIPAddressConverter : JsonConverter<IPAddress> | ||
{ | ||
/// <inheritdoc/> | ||
public override IPAddress Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.String) throw new JsonException($"Expected string but got {reader.TokenType}."); | ||
|
||
Span<char> charData = stackalloc char[45]; | ||
|
||
var count = Encoding.UTF8.GetChars(reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan, charData); | ||
|
||
return !IPAddress.TryParse(charData[..count], out var value) | ||
? throw new JsonException($"Could not parse IPAddress from [{charData[..count].ToString()}].") | ||
: value; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Write(Utf8JsonWriter writer, IPAddress value, JsonSerializerOptions options) | ||
{ | ||
var data = value.AddressFamily == AddressFamily.InterNetwork ? stackalloc char[15] : stackalloc char[45]; | ||
if (!value.TryFormat(data, out var charsWritten)) throw new JsonException($"IPAddress [{value}] could not be written to JSON."); | ||
writer.WriteStringValue(data[..charsWritten]); | ||
} | ||
|
||
} |