mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 11:26:27 +00:00
Core - Fix node script and pin bugs found by the tests (#911)
* Core - Keep nodes of missing plugins and the connection an input reads from in node scripts - Saving a script keeps nodes whose plugin is missing, and their connections, so they come back once the plugin is available again - Loading keeps the first connection of an input, the one it reads from - Disabling a node provider only removes the nodes it created, not those of another provider registering the same type - RemoveNode only raises NodeRemoved for nodes that were in the script * Core - Fix pin connections, null values and failing nodes - Connecting pins that are already connected no longer adds a second connection - Untyped pins turn null into the default value for value types, as intended - A new InputPin<T> reads its connection right away instead of after its first reset - Matching a specific enum to a pin of any enum now depends on forgivingEnumMatching - A node that fails to evaluate sets its outputs to their default values, so it is evaluated once per run and old values don't flow on * Core - Round Numeric to long and clamp it to the range of an sbyte
This commit is contained in:
1 parent
83cf6da830
commit
5ddd50b4a8
12 files changed
+118
-92
No files matched your search
@@ -182,7 +182,7 @@ public readonly struct Numeric : IComparable<Numeric>, IConvertible
|
||||
|
||||
public static implicit operator long(Numeric p)
|
||||
{
|
||||
return (long) p._value;
|
||||
return (long) MathF.Round(p._value, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
public static bool operator >(Numeric a, Numeric b)
|
||||
@@ -367,7 +367,7 @@ public readonly struct Numeric : IComparable<Numeric>, IConvertible
|
||||
/// <inheritdoc />
|
||||
public sbyte ToSByte(IFormatProvider? provider)
|
||||
{
|
||||
return (sbyte) Math.Clamp(_value, 0, 255);
|
||||
return (sbyte) Math.Clamp(_value, sbyte.MinValue, sbyte.MaxValue);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -22,7 +22,10 @@ public abstract class NodeScript : CorePropertyChanged, INodeScript
|
||||
|
||||
private void NodeTypeStoreOnNodeTypeRemoved(object? sender, NodeTypeStoreEvent e)
|
||||
{
|
||||
List<INode> nodes = Nodes.Where(n => n.GetType() == e.TypeRegistration.NodeData.Type).ToList();
|
||||
// Match on the node data rather than the type, another provider may have registered the same type
|
||||
List<INode> nodes = Nodes.Where(n => n.NodeData != null
|
||||
? n.NodeData == e.TypeRegistration.NodeData
|
||||
: n.GetType() == e.TypeRegistration.NodeData.Type).ToList();
|
||||
foreach (INode node in nodes)
|
||||
RemoveNode(node);
|
||||
}
|
||||
@@ -145,7 +148,8 @@ public abstract class NodeScript : CorePropertyChanged, INodeScript
|
||||
{
|
||||
lock (_nodes)
|
||||
{
|
||||
_nodes.Remove(node);
|
||||
if (!_nodes.Remove(node))
|
||||
return;
|
||||
}
|
||||
|
||||
NodeRemoved?.Invoke(this, new SingleValueEventArgs<INode>(node));
|
||||
@@ -253,6 +257,8 @@ public abstract class NodeScript : CorePropertyChanged, INodeScript
|
||||
public void LoadConnections()
|
||||
{
|
||||
List<INode> nodes = Nodes.ToList();
|
||||
// An input reads from its first connection, so when there are several the first one is kept
|
||||
HashSet<IPin> connectedInputs = [];
|
||||
foreach (NodeConnectionEntity nodeConnectionEntity in Entity.Connections.OrderBy(p => p.SourcePinCollectionId))
|
||||
{
|
||||
INode? source = nodes.FirstOrDefault(n => n.Id == nodeConnectionEntity.SourceNode);
|
||||
@@ -275,6 +281,9 @@ public abstract class NodeScript : CorePropertyChanged, INodeScript
|
||||
// Ensure the connection is valid
|
||||
if (sourcePin.Direction == targetPin.Direction)
|
||||
continue;
|
||||
IPin inputPin = targetPin.Direction == PinDirection.Input ? targetPin : sourcePin;
|
||||
if (!connectedInputs.Add(inputPin))
|
||||
continue;
|
||||
|
||||
// Clear existing connections on input pins, we don't want none of that now
|
||||
if (targetPin.Direction == PinDirection.Input)
|
||||
@@ -310,7 +319,16 @@ public abstract class NodeScript : CorePropertyChanged, INodeScript
|
||||
{
|
||||
Entity.Name = Name;
|
||||
Entity.Description = Description;
|
||||
|
||||
// Nodes whose type isn't registered couldn't be loaded, most likely because their plugin is missing or disabled.
|
||||
// Keep them and their connections, so they come back once the plugin is available again
|
||||
List<NodeEntity> unavailableNodes = Entity.Nodes.Where(e => !e.IsExitNode && Nodes.All(n => n.Id != e.Id) && NodeTypeStore.Get(e) == null).ToList();
|
||||
List<NodeConnectionEntity> unavailableConnections = Entity.Connections
|
||||
.Where(c => unavailableNodes.Any(n => n.Id == c.SourceNode || n.Id == c.TargetNode))
|
||||
.ToList();
|
||||
|
||||
Entity.Nodes.Clear();
|
||||
Entity.Nodes.AddRange(unavailableNodes);
|
||||
|
||||
foreach (INode node in Nodes)
|
||||
{
|
||||
@@ -346,6 +364,7 @@ public abstract class NodeScript : CorePropertyChanged, INodeScript
|
||||
|
||||
// Store connections
|
||||
Entity.Connections.Clear();
|
||||
Entity.Connections.AddRange(unavailableConnections);
|
||||
foreach (INode node in Nodes)
|
||||
{
|
||||
SavePins(node, -1, node.Pins);
|
||||
|
||||
@@ -385,7 +385,13 @@ public abstract class Node : BreakableModel, INode
|
||||
/// <inheritdoc />
|
||||
public void TryEvaluate()
|
||||
{
|
||||
TryOrBreak(Evaluate, "Failed to evaluate");
|
||||
if (TryOrBreak(Evaluate, "Failed to evaluate"))
|
||||
return;
|
||||
|
||||
// Don't let the values of the last successful run flow on, and don't try again for every pin that reads from this
|
||||
// node. Setting a value marks the pin as evaluated until the next run
|
||||
foreach (Pin pin in Pins.Concat(PinCollections.SelectMany(c => c)).OfType<Pin>().Where(p => p.Direction == PinDirection.Output))
|
||||
pin.SetDefaultValue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -12,7 +12,7 @@ public sealed class InputPin<T> : Pin
|
||||
internal InputPin(INode node, string name)
|
||||
: base(node, name)
|
||||
{
|
||||
Value = default;
|
||||
_value = default;
|
||||
IsNumeric = typeof(T) == typeof(Numeric);
|
||||
}
|
||||
|
||||
@@ -149,15 +149,13 @@ public sealed class InputPin : Pin
|
||||
|
||||
private set
|
||||
{
|
||||
// We can't take null for value types so use the default value for that type
|
||||
if (Type.IsValueType && value == null)
|
||||
// We can't take null for value types so set it to the default value for that type
|
||||
_value = Type.GetDefault();
|
||||
else if (value != null)
|
||||
// If a value was given make sure it matches
|
||||
if (!Type.IsInstanceOfType(value))
|
||||
throw new ArgumentException($"Value of type '{value.GetType().Name}' can't be assigned to a pin of type {Type.Name}.");
|
||||
value = Type.GetDefault();
|
||||
// If a value was given make sure it matches
|
||||
else if (value != null && !Type.IsInstanceOfType(value))
|
||||
throw new ArgumentException($"Value of type '{value.GetType().Name}' can't be assigned to a pin of type {Type.Name}.");
|
||||
|
||||
// Otherwise we're good and we can put a null here if it happens to be that
|
||||
_value = value;
|
||||
IsEvaluated = true;
|
||||
OnPropertyChanged(nameof(PinValue));
|
||||
|
||||
@@ -29,6 +29,11 @@ public sealed class OutputPin<T> : Pin
|
||||
/// <inheritdoc />
|
||||
public override PinDirection Direction => PinDirection.Output;
|
||||
|
||||
internal override void SetDefaultValue()
|
||||
{
|
||||
Value = default;
|
||||
}
|
||||
|
||||
private T? _value;
|
||||
|
||||
/// <summary>
|
||||
@@ -100,6 +105,11 @@ public sealed class OutputPin : Pin
|
||||
/// <inheritdoc />
|
||||
public override PinDirection Direction => PinDirection.Output;
|
||||
|
||||
internal override void SetDefaultValue()
|
||||
{
|
||||
Value = Type.GetDefault();
|
||||
}
|
||||
|
||||
private object? _value;
|
||||
private Type _type;
|
||||
|
||||
@@ -117,15 +127,13 @@ public sealed class OutputPin : Pin
|
||||
}
|
||||
set
|
||||
{
|
||||
// We can't take null for value types so use the default value for that type
|
||||
if (Type.IsValueType && value == null)
|
||||
// We can't take null for value types so set it to the default value for that type
|
||||
_value = Type.GetDefault();
|
||||
else if (value != null)
|
||||
// If a value was given make sure it matches
|
||||
if (!Type.IsInstanceOfType(value))
|
||||
throw new ArgumentException($"Value of type '{value.GetType().Name}' can't be assigned to a pin of type {Type.Name}.");
|
||||
value = Type.GetDefault();
|
||||
// If a value was given make sure it matches
|
||||
else if (value != null && !Type.IsInstanceOfType(value))
|
||||
throw new ArgumentException($"Value of type '{value.GetType().Name}' can't be assigned to a pin of type {Type.Name}.");
|
||||
|
||||
// Otherwise we're good and we can put a null here if it happens to be that
|
||||
_value = value;
|
||||
IsEvaluated = true;
|
||||
OnPropertyChanged(nameof(PinValue));
|
||||
|
||||
@@ -85,9 +85,19 @@ public abstract class Pin : CorePropertyChanged, IPin
|
||||
IsEvaluated = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of an output pin to the default value of its type, used when its node fails to evaluate
|
||||
/// </summary>
|
||||
internal virtual void SetDefaultValue()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ConnectTo(IPin pin)
|
||||
{
|
||||
if (_connectedTo.Contains(pin))
|
||||
return;
|
||||
|
||||
_connectedTo.Add(pin);
|
||||
if (!pin.ConnectedTo.Contains(this))
|
||||
pin.ConnectTo(this);
|
||||
@@ -129,11 +139,15 @@ public abstract class Pin : CorePropertyChanged, IPin
|
||||
/// <inheritdoc />
|
||||
public bool IsTypeCompatible(Type type, bool forgivingEnumMatching = true)
|
||||
{
|
||||
return Type == type
|
||||
|| (Direction == PinDirection.Input && type.IsAssignableTo(Type))
|
||||
|| (Direction == PinDirection.Output && type.IsAssignableFrom(Type))
|
||||
|| (Direction == PinDirection.Input && Type == typeof(Enum) && type.IsEnum && forgivingEnumMatching)
|
||||
|| (Direction == PinDirection.Output && type == typeof(Enum) && Type.IsEnum && forgivingEnumMatching);
|
||||
if (Type == type)
|
||||
return true;
|
||||
|
||||
// A specific enum and a pin of any enum only match when forgiving, every enum is assignable to Enum otherwise
|
||||
if ((Direction == PinDirection.Input && Type == typeof(Enum) && type.IsEnum) || (Direction == PinDirection.Output && type == typeof(Enum) && Type.IsEnum))
|
||||
return forgivingEnumMatching;
|
||||
|
||||
return (Direction == PinDirection.Input && type.IsAssignableTo(Type))
|
||||
|| (Direction == PinDirection.Output && type.IsAssignableFrom(Type));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user