From cb6380651e90576a8c72c1401b760960e599b58d Mon Sep 17 00:00:00 2001 From: Nytra <14206961+Nytra@users.noreply.github.com> Date: Sun, 14 Jul 2024 05:32:09 +0100 Subject: [PATCH] Improve binding generator to handle constraint clauses and generic types attribute, fix JSON nodes --- .../BindingGenerator.cs | 10 ++++- ...dToObject.cs => JsonAddObjectToJObject.cs} | 5 +-- .../ProtoFlux/JSON/JsonAddValueToJObject.cs | 35 ++++++++++++++++ ...ToArray.cs => JsonAppendObjectToJArray.cs} | 5 +-- .../ProtoFlux/JSON/JsonAppendValueToJArray.cs | 36 ++++++++++++++++ ...drenNode.cs => JsonCountJArrayChildren.cs} | 2 +- ...renNode.cs => JsonCountJObjectChildren.cs} | 2 +- ProjectObsidian/ProtoFlux/JSON/JsonEmpty.cs | 33 --------------- .../ProtoFlux/JSON/JsonEmptyJArray.cs | 16 ++++++++ .../ProtoFlux/JSON/JsonEmptyJObject.cs | 16 ++++++++ .../ProtoFlux/JSON/JsonGetObjectFromJArray.cs | 36 ++++++++++++++++ .../JSON/JsonGetObjectFromJObject.cs | 36 ++++++++++++++++ ...ArrayNode.cs => JsonGetValueFromJArray.cs} | 8 ++-- ...omObject.cs => JsonGetValueFromJObject.cs} | 5 +-- ...rayNode.cs => JsonInsertObjectToJArray.cs} | 7 ++-- .../ProtoFlux/JSON/JsonInsertValueToJArray.cs | 41 +++++++++++++++++++ .../ProtoFlux/JSON/JsonNullValue.cs | 16 ++++++++ ...tringArray.cs => JsonParseStringJArray.cs} | 2 +- ...tringNode.cs => JsonParseStringJObject.cs} | 3 +- .../ProtoFlux/JSON/JsonQuickGetObject.cs | 5 +-- .../ProtoFlux/JSON/JsonQuickGetValue.cs | 37 +++++++++++++++++ ...omArrayNode.cs => JsonRemoveFromJArray.cs} | 5 +-- ...ObjectNode.cs => JsonRemoveFromJObject.cs} | 3 +- .../{JsonToStringNode.cs => JsonToString.cs} | 2 +- 24 files changed, 300 insertions(+), 66 deletions(-) rename ProjectObsidian/ProtoFlux/JSON/{JsonAddToObject.cs => JsonAddObjectToJObject.cs} (79%) create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonAddValueToJObject.cs rename ProjectObsidian/ProtoFlux/JSON/{JsonAppendToArray.cs => JsonAppendObjectToJArray.cs} (75%) create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonAppendValueToJArray.cs rename ProjectObsidian/ProtoFlux/JSON/{JsonCountArrayChildrenNode.cs => JsonCountJArrayChildren.cs} (83%) rename ProjectObsidian/ProtoFlux/JSON/{JsonCountObjectChildrenNode.cs => JsonCountJObjectChildren.cs} (83%) delete mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonEmpty.cs create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonEmptyJArray.cs create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonEmptyJObject.cs create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJArray.cs create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJObject.cs rename ProjectObsidian/ProtoFlux/JSON/{JsonGetFromArrayNode.cs => JsonGetValueFromJArray.cs} (75%) rename ProjectObsidian/ProtoFlux/JSON/{JsonGetFromObject.cs => JsonGetValueFromJObject.cs} (79%) rename ProjectObsidian/ProtoFlux/JSON/{JsonInsertToArrayNode.cs => JsonInsertObjectToJArray.cs} (74%) create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonInsertValueToJArray.cs create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonNullValue.cs rename ProjectObsidian/ProtoFlux/JSON/{JsonParseStringArray.cs => JsonParseStringJArray.cs} (89%) rename ProjectObsidian/ProtoFlux/JSON/{JsonParseStringNode.cs => JsonParseStringJObject.cs} (89%) create mode 100644 ProjectObsidian/ProtoFlux/JSON/JsonQuickGetValue.cs rename ProjectObsidian/ProtoFlux/JSON/{JsonRemoveFromArrayNode.cs => JsonRemoveFromJArray.cs} (86%) rename ProjectObsidian/ProtoFlux/JSON/{JsonRemoveFromObjectNode.cs => JsonRemoveFromJObject.cs} (88%) rename ProjectObsidian/ProtoFlux/JSON/{JsonToStringNode.cs => JsonToString.cs} (86%) diff --git a/ProjectObsidian.SourceGenerators/BindingGenerator.cs b/ProjectObsidian.SourceGenerators/BindingGenerator.cs index 6c591f4..9f43ced 100644 --- a/ProjectObsidian.SourceGenerators/BindingGenerator.cs +++ b/ProjectObsidian.SourceGenerators/BindingGenerator.cs @@ -146,8 +146,9 @@ public string Result namespace {BindingPrefix}{_currentNameSpace}; +{_genericTypesAttribute} [Category(new string[] {{""ProtoFlux/Runtimes/Execution/Nodes/{_category}""}})] -public partial class {_fullName} : global::FrooxEngine.ProtoFlux.Runtimes.Execution.{_baseType} +public partial class {_fullName} : global::FrooxEngine.ProtoFlux.Runtimes.Execution.{_baseType} {_constraintClauses} {{ {Declarations} {_nodeNameOverride} @@ -181,6 +182,8 @@ public override N Instantiate() private string _match; private string _category; private string _nodeNameOverride = ""; + private string _constraintClauses; + private string _genericTypesAttribute; private bool TypedFieldDetection(string type, string name, string targetTypeName, string declarationFormat, OrderedCount counter) { @@ -285,6 +288,9 @@ public override void VisitClassDeclaration(ClassDeclarationSyntax node) _baseType = baseTypeName; + // Add the generic parameter constraints + _constraintClauses = string.Join(",", node.ConstraintClauses); + if (!node.AttributeLists.Any()) { base.VisitClassDeclaration(node); @@ -294,6 +300,8 @@ public override void VisitClassDeclaration(ClassDeclarationSyntax node) var find = node.AttributeLists.SelectMany(i => i.Attributes) .FirstOrDefault(i => i.Name.ToString() == "NodeCategory"); + _genericTypesAttribute = node.AttributeLists.FirstOrDefault(attr => attr.Attributes.Any(attr2 => attr2.Name.ToString() == "GenericTypes"))?.ToString(); + if (find?.ArgumentList is null) { base.VisitClassDeclaration(node); diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonAddToObject.cs b/ProjectObsidian/ProtoFlux/JSON/JsonAddObjectToJObject.cs similarity index 79% rename from ProjectObsidian/ProtoFlux/JSON/JsonAddToObject.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonAddObjectToJObject.cs index 1088fd6..31f9f10 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonAddToObject.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonAddObjectToJObject.cs @@ -10,10 +10,9 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeName("Add To Object")] [NodeCategory("Obsidian/Json")] - [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), - typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(Uri), + [GenericTypes(typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), typeof(JArray))] - public class JsonAddToObjectNode : ObjectFunctionNode + public class JsonAddObjectToJObject : ObjectFunctionNode { public readonly ObjectInput Input; public readonly ObjectInput Tag; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonAddValueToJObject.cs b/ProjectObsidian/ProtoFlux/JSON/JsonAddValueToJObject.cs new file mode 100644 index 0000000..cbc7f2f --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonAddValueToJObject.cs @@ -0,0 +1,35 @@ +using System; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using Elements.Core; +using FrooxEngine; +using FrooxEngine.ProtoFlux; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeName("Add To Object")] + [NodeCategory("Obsidian/Json")] + [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), + typeof(long), typeof(ulong), typeof(float), typeof(double))] + public class JsonAddValueToJObject : ObjectFunctionNode where T : unmanaged + { + public readonly ObjectInput Input; + public readonly ObjectInput Tag; + public readonly ValueInput Value; + + protected override JObject Compute(FrooxEngineContext context) + { + var input = Input.Evaluate(context); + if (input == null) return null; + + var tag = Tag.Evaluate(context); + var value = Value.Evaluate(context); + if (string.IsNullOrEmpty(tag)) return input; + + var in2 = (JObject)input.DeepClone(); + in2[tag] = new JValue(value); + return in2; + } + } +} diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonAppendToArray.cs b/ProjectObsidian/ProtoFlux/JSON/JsonAppendObjectToJArray.cs similarity index 75% rename from ProjectObsidian/ProtoFlux/JSON/JsonAppendToArray.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonAppendObjectToJArray.cs index 37c5037..ce8f4d7 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonAppendToArray.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonAppendObjectToJArray.cs @@ -9,10 +9,9 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), - typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), + [GenericTypes(typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), typeof(JArray))] - public class JsonAppendToArrayNode : ObjectFunctionNode + public class JsonAppendObjectToJArray : ObjectFunctionNode { public readonly ObjectInput Array; public readonly ObjectInput Object; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonAppendValueToJArray.cs b/ProjectObsidian/ProtoFlux/JSON/JsonAppendValueToJArray.cs new file mode 100644 index 0000000..5197552 --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonAppendValueToJArray.cs @@ -0,0 +1,36 @@ +using System; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using FrooxEngine; +using Elements.Core; +using FrooxEngine.ProtoFlux; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), + typeof(ulong), typeof(float), typeof(double))] + public class JsonAppendValueToJArray : ObjectFunctionNode where T : unmanaged + { + public readonly ObjectInput Array; + public readonly ValueInput Value; + protected override JArray Compute(FrooxEngineContext context) + { + var array = Array.Evaluate(context); + var value = Value.Evaluate(context); + if (array == null) return null; + + try + { + var output = (JArray)array.DeepClone(); + output.Add(new JValue(value)); + return output; + } + catch + { + return null; + } + } + } +} diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonCountArrayChildrenNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonCountJArrayChildren.cs similarity index 83% rename from ProjectObsidian/ProtoFlux/JSON/JsonCountArrayChildrenNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonCountJArrayChildren.cs index 3517dd2..ac351a2 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonCountArrayChildrenNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonCountJArrayChildren.cs @@ -6,7 +6,7 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - public class JsonCountArrayChildrenNode : ValueFunctionNode + public class JsonCountJArrayChildren : ValueFunctionNode { public readonly ObjectInput Input; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonCountObjectChildrenNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonCountJObjectChildren.cs similarity index 83% rename from ProjectObsidian/ProtoFlux/JSON/JsonCountObjectChildrenNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonCountJObjectChildren.cs index a4086da..f0c16d3 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonCountObjectChildrenNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonCountJObjectChildren.cs @@ -6,7 +6,7 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - public class JsonCountObjectChildrenNode : ValueFunctionNode + public class JsonCountJObjectChildren : ValueFunctionNode { public readonly ObjectInput Input; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonEmpty.cs b/ProjectObsidian/ProtoFlux/JSON/JsonEmpty.cs deleted file mode 100644 index fc712e5..0000000 --- a/ProjectObsidian/ProtoFlux/JSON/JsonEmpty.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FrooxEngine.ProtoFlux; -using Newtonsoft.Json.Linq; -using ProtoFlux.Core; -using ProtoFlux.Runtimes.Execution; - -namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json -{ - [NodeCategory("Obsidian/Json")] - public class JsonEmptyObjectNode : ObjectFunctionNode - { - protected override JObject Compute(FrooxEngineContext context) - { - return new JObject(); - } - } - - [NodeCategory("Obsidian/Json")] - public class JsonEmptyArrayNode : ObjectFunctionNode - { - protected override JArray Compute(FrooxEngineContext context) - { - return new JArray(); - } - } - [NodeCategory("Obsidian/Json")] - public class JsonNullValueNode : ObjectFunctionNode - { - protected override JToken Compute(FrooxEngineContext context) - { - return JValue.CreateNull(); - } - } -} \ No newline at end of file diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonEmptyJArray.cs b/ProjectObsidian/ProtoFlux/JSON/JsonEmptyJArray.cs new file mode 100644 index 0000000..7b406fe --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonEmptyJArray.cs @@ -0,0 +1,16 @@ +using FrooxEngine.ProtoFlux; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + public class JsonEmptyJArray : ObjectFunctionNode + { + protected override JArray Compute(FrooxEngineContext context) + { + return new JArray(); + } + } +} \ No newline at end of file diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonEmptyJObject.cs b/ProjectObsidian/ProtoFlux/JSON/JsonEmptyJObject.cs new file mode 100644 index 0000000..affdafe --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonEmptyJObject.cs @@ -0,0 +1,16 @@ +using FrooxEngine.ProtoFlux; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + public class JsonEmptyJObject : ObjectFunctionNode + { + protected override JObject Compute(FrooxEngineContext context) + { + return new JObject(); + } + } +} \ No newline at end of file diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJArray.cs b/ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJArray.cs new file mode 100644 index 0000000..cbfbb9f --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJArray.cs @@ -0,0 +1,36 @@ +using System; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using Elements.Core; +using FrooxEngine; +using FrooxEngine.ProtoFlux; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + [GenericTypes(typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), + typeof(JArray))] + public class JsonGetObjectFromJArray : ObjectFunctionNode + { + public readonly ObjectInput Input; + public readonly ValueInput Index; + + protected override T Compute(FrooxEngineContext context) + { + var input = Input.Evaluate(context); + var index = Index.Evaluate(context); + if (input == null || index < 0 || index >= input.Count) + return default; + + try + { + return input[index].Value(); + } + catch + { + return default; + } + } + } +} diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJObject.cs b/ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJObject.cs new file mode 100644 index 0000000..1ca9c4e --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJObject.cs @@ -0,0 +1,36 @@ +using System; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using Elements.Core; +using FrooxEngine; +using FrooxEngine.ProtoFlux; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + [GenericTypes(typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), + typeof(JArray))] + public class JsonGetObjectFromJObject : ObjectFunctionNode + { + public readonly ObjectInput Input; + public readonly ObjectInput Tag; + + protected override T Compute(FrooxEngineContext context) + { + var input = Input.Evaluate(context); + var tag = Tag.Evaluate(context); + if (input == null || string.IsNullOrEmpty(tag)) + return default; + + try + { + return input[tag].Value(); + } + catch + { + return default; + } + } + } +} diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonGetFromArrayNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonGetValueFromJArray.cs similarity index 75% rename from ProjectObsidian/ProtoFlux/JSON/JsonGetFromArrayNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonGetValueFromJArray.cs index 2524b5a..679b005 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonGetFromArrayNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonGetValueFromJArray.cs @@ -10,13 +10,11 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), - typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), - typeof(JArray))] - public class JsonGetFromArrayNode : ObjectFunctionNode + typeof(ulong), typeof(float), typeof(double))] + public class JsonGetValueFromJArray : ValueFunctionNode where T : unmanaged { public readonly ObjectInput Input; - public readonly ObjectInput Index; - + public readonly ValueInput Index; protected override T Compute(FrooxEngineContext context) { diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonGetFromObject.cs b/ProjectObsidian/ProtoFlux/JSON/JsonGetValueFromJObject.cs similarity index 79% rename from ProjectObsidian/ProtoFlux/JSON/JsonGetFromObject.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonGetValueFromJObject.cs index 64f8da4..4a5769e 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonGetFromObject.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonGetValueFromJObject.cs @@ -10,9 +10,8 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), - typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), - typeof(JArray))] - public class JsonGetFromObjectNode : ObjectFunctionNode + typeof(ulong), typeof(float), typeof(double))] + public class JsonGetValueFromJObject : ValueFunctionNode where T : unmanaged { public readonly ObjectInput Input; public readonly ObjectInput Tag; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonInsertToArrayNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonInsertObjectToJArray.cs similarity index 74% rename from ProjectObsidian/ProtoFlux/JSON/JsonInsertToArrayNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonInsertObjectToJArray.cs index 044c924..920cc75 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonInsertToArrayNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonInsertObjectToJArray.cs @@ -9,14 +9,13 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), - typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), + [GenericTypes(typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), typeof(JArray))] - public class JsonInsertToArrayNode : ObjectFunctionNode + public class JsonInsertObjectToJArray : ObjectFunctionNode { public readonly ObjectInput Array; public readonly ObjectInput Object; - public readonly ObjectInput Index; + public readonly ValueInput Index; protected override JArray Compute(FrooxEngineContext context) { diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonInsertValueToJArray.cs b/ProjectObsidian/ProtoFlux/JSON/JsonInsertValueToJArray.cs new file mode 100644 index 0000000..ae2674d --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonInsertValueToJArray.cs @@ -0,0 +1,41 @@ +using System; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using Elements.Core; +using FrooxEngine; +using FrooxEngine.ProtoFlux; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), + typeof(ulong), typeof(float), typeof(double))] + public class JsonInsertValueToJArray : ObjectFunctionNode where T : unmanaged + { + public readonly ObjectInput Array; + public readonly ValueInput Value; + public readonly ValueInput Index; + + protected override JArray Compute(FrooxEngineContext context) + { + var array = Array.Evaluate(context); + var value = Value.Evaluate(context); + var index = Index.Evaluate(context); + if (array == null || index < 0 || index > array.Count) + return null; + + try + { + var output = (JArray)array.DeepClone(); + var token = new JValue(value); + output.Insert(index, token); + return output; + } + catch + { + return null; + } + } + } +} diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonNullValue.cs b/ProjectObsidian/ProtoFlux/JSON/JsonNullValue.cs new file mode 100644 index 0000000..c595cae --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonNullValue.cs @@ -0,0 +1,16 @@ +using FrooxEngine.ProtoFlux; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + public class JsonNullValue : ObjectFunctionNode + { + protected override JToken Compute(FrooxEngineContext context) + { + return JValue.CreateNull(); + } + } +} \ No newline at end of file diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonParseStringArray.cs b/ProjectObsidian/ProtoFlux/JSON/JsonParseStringJArray.cs similarity index 89% rename from ProjectObsidian/ProtoFlux/JSON/JsonParseStringArray.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonParseStringJArray.cs index 7d18d4d..dd44310 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonParseStringArray.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonParseStringJArray.cs @@ -7,7 +7,7 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - public class JsonParseStringArrayNode : ObjectFunctionNode + public class JsonParseStringJArray : ObjectFunctionNode { public readonly ObjectInput Input; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonParseStringNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonParseStringJObject.cs similarity index 89% rename from ProjectObsidian/ProtoFlux/JSON/JsonParseStringNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonParseStringJObject.cs index 1624cd1..b884bb2 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonParseStringNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonParseStringJObject.cs @@ -7,10 +7,9 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - public class JsonParseStringNode : ObjectFunctionNode + public class JsonParseStringJObject : ObjectFunctionNode { public readonly ObjectInput Input; - protected override JObject Compute(FrooxEngineContext context) { diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetObject.cs b/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetObject.cs index b5e491d..cda93af 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetObject.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetObject.cs @@ -9,10 +9,9 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), - typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), + [GenericTypes(typeof(string), typeof(Uri), typeof(JToken), typeof(JObject), typeof(JArray))] - public class JsonQuickGetFromObjectNode : ObjectFunctionNode + public class JsonQuickGetObject : ObjectFunctionNode { public readonly ObjectInput Input; public readonly ObjectInput Tag; diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetValue.cs b/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetValue.cs new file mode 100644 index 0000000..9f31931 --- /dev/null +++ b/ProjectObsidian/ProtoFlux/JSON/JsonQuickGetValue.cs @@ -0,0 +1,37 @@ +using System; +using Newtonsoft.Json.Linq; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using Elements.Core; +using FrooxEngine; +using FrooxEngine.ProtoFlux; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json +{ + [NodeCategory("Obsidian/Json")] + [GenericTypes(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), + typeof(ulong), typeof(float), typeof(double))] + public class JsonQuickGetValue : ValueFunctionNode where T : unmanaged + { + public readonly ObjectInput Input; + public readonly ObjectInput Tag; + protected override T Compute(FrooxEngineContext context) + { + string input = Input.Evaluate(context); + string tag = Tag.Evaluate(context); + if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(tag)) + return default; + + try + { + var inputObject = JObject.Parse(input); + return inputObject[tag].Value(); + } + catch + { + // In case of parsing error or if the tag is not found + return default; + } + } + } +} \ No newline at end of file diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromArrayNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromJArray.cs similarity index 86% rename from ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromArrayNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromJArray.cs index 25ea200..a22adf9 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromArrayNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromJArray.cs @@ -7,11 +7,10 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - public class JsonRemoveFromArrayNode : ObjectFunctionNode + public class JsonRemoveFromJArray : ObjectFunctionNode { public readonly ObjectInput Array; - public readonly ObjectInput Index; - + public readonly ValueInput Index; protected override JArray Compute(FrooxEngineContext context) { diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromObjectNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromJObject.cs similarity index 88% rename from ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromObjectNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromJObject.cs index 7fa503b..f9d081f 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromObjectNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonRemoveFromJObject.cs @@ -7,11 +7,10 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json { [NodeCategory("Obsidian/Json")] - public class JsonRemoveFromObjectNode : ObjectFunctionNode + public class JsonRemoveFromJObject : ObjectFunctionNode { public readonly ObjectInput Input; public readonly ObjectInput Tag; - protected override JObject Compute(FrooxEngineContext context) { diff --git a/ProjectObsidian/ProtoFlux/JSON/JsonToStringNode.cs b/ProjectObsidian/ProtoFlux/JSON/JsonToString.cs similarity index 86% rename from ProjectObsidian/ProtoFlux/JSON/JsonToStringNode.cs rename to ProjectObsidian/ProtoFlux/JSON/JsonToString.cs index 6d2ddd5..0f33177 100644 --- a/ProjectObsidian/ProtoFlux/JSON/JsonToStringNode.cs +++ b/ProjectObsidian/ProtoFlux/JSON/JsonToString.cs @@ -9,7 +9,7 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json; [NodeCategory("Obsidian/Json")] [GenericTypes(typeof(JToken), typeof(JObject), typeof(JArray))] -public class JsonToStringNode : ObjectFunctionNode +public class JsonToString : ObjectFunctionNode { public readonly ObjectInput Input;