Skip to content

Commit

Permalink
Improve binding generator to handle constraint clauses and generic ty…
Browse files Browse the repository at this point in the history
…pes attribute, fix JSON nodes
  • Loading branch information
Nytra committed Jul 14, 2024
1 parent a04daad commit cb63806
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 66 deletions.
10 changes: 9 additions & 1 deletion ProjectObsidian.SourceGenerators/BindingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -181,6 +182,8 @@ public override N Instantiate<N>()
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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ObjectFunctionNode<FrooxEngineContext, JObject>
public class JsonAddObjectToJObject<T> : ObjectFunctionNode<FrooxEngineContext, JObject>
{
public readonly ObjectInput<JObject> Input;
public readonly ObjectInput<string> Tag;
Expand Down
35 changes: 35 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonAddValueToJObject.cs
Original file line number Diff line number Diff line change
@@ -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<T> : ObjectFunctionNode<FrooxEngineContext, JObject> where T : unmanaged
{
public readonly ObjectInput<JObject> Input;
public readonly ObjectInput<string> Tag;
public readonly ValueInput<T> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ObjectFunctionNode<FrooxEngineContext, JArray>
public class JsonAppendObjectToJArray<T> : ObjectFunctionNode<FrooxEngineContext, JArray>
{
public readonly ObjectInput<JArray> Array;
public readonly ObjectInput<T> Object;
Expand Down
36 changes: 36 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonAppendValueToJArray.cs
Original file line number Diff line number Diff line change
@@ -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<T> : ObjectFunctionNode<FrooxEngineContext, JArray> where T : unmanaged
{
public readonly ObjectInput<JArray> Array;
public readonly ValueInput<T> 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json
{
[NodeCategory("Obsidian/Json")]
public class JsonCountArrayChildrenNode : ValueFunctionNode<FrooxEngineContext, int>
public class JsonCountJArrayChildren : ValueFunctionNode<FrooxEngineContext, int>
{
public readonly ObjectInput<JArray> Input;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Json
{
[NodeCategory("Obsidian/Json")]
public class JsonCountObjectChildrenNode : ValueFunctionNode<FrooxEngineContext, int>
public class JsonCountJObjectChildren : ValueFunctionNode<FrooxEngineContext, int>
{
public readonly ObjectInput<JObject> Input;

Expand Down
33 changes: 0 additions & 33 deletions ProjectObsidian/ProtoFlux/JSON/JsonEmpty.cs

This file was deleted.

16 changes: 16 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonEmptyJArray.cs
Original file line number Diff line number Diff line change
@@ -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<FrooxEngineContext, JArray>
{
protected override JArray Compute(FrooxEngineContext context)
{
return new JArray();
}
}
}
16 changes: 16 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonEmptyJObject.cs
Original file line number Diff line number Diff line change
@@ -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<FrooxEngineContext, JObject>
{
protected override JObject Compute(FrooxEngineContext context)
{
return new JObject();
}
}
}
36 changes: 36 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJArray.cs
Original file line number Diff line number Diff line change
@@ -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<T> : ObjectFunctionNode<FrooxEngineContext, T>
{
public readonly ObjectInput<JArray> Input;
public readonly ValueInput<int> 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<T>();
}
catch
{
return default;
}
}
}
}
36 changes: 36 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonGetObjectFromJObject.cs
Original file line number Diff line number Diff line change
@@ -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<T> : ObjectFunctionNode<FrooxEngineContext, T>
{
public readonly ObjectInput<JObject> Input;
public readonly ObjectInput<string> 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<T>();
}
catch
{
return default;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ObjectFunctionNode<FrooxEngineContext, T>
typeof(ulong), typeof(float), typeof(double))]
public class JsonGetValueFromJArray<T> : ValueFunctionNode<FrooxEngineContext, T> where T : unmanaged
{
public readonly ObjectInput<JArray> Input;
public readonly ObjectInput<int> Index;

public readonly ValueInput<int> Index;

protected override T Compute(FrooxEngineContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ObjectFunctionNode<FrooxEngineContext, T>
typeof(ulong), typeof(float), typeof(double))]
public class JsonGetValueFromJObject<T> : ValueFunctionNode<FrooxEngineContext, T> where T : unmanaged
{
public readonly ObjectInput<JObject> Input;
public readonly ObjectInput<string> Tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ObjectFunctionNode<FrooxEngineContext, JArray>
public class JsonInsertObjectToJArray<T> : ObjectFunctionNode<FrooxEngineContext, JArray>
{
public readonly ObjectInput<JArray> Array;
public readonly ObjectInput<T> Object;
public readonly ObjectInput<int> Index;
public readonly ValueInput<int> Index;

protected override JArray Compute(FrooxEngineContext context)
{
Expand Down
41 changes: 41 additions & 0 deletions ProjectObsidian/ProtoFlux/JSON/JsonInsertValueToJArray.cs
Original file line number Diff line number Diff line change
@@ -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<T> : ObjectFunctionNode<FrooxEngineContext, JArray> where T : unmanaged
{
public readonly ObjectInput<JArray> Array;
public readonly ValueInput<T> Value;
public readonly ValueInput<int> 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;
}
}
}
}
Loading

0 comments on commit cb63806

Please sign in to comment.