public Link AddLink(Output output, Input input, bool writeInDb = true)
{
Node inputNode = GetInputOwner(input);
Node outputNode = GetOutputOwner(output);
if (inputNode == null || outputNode == null)
{
LogEngineError($"Can`t create link from [{output.Id}] to [{input.Id}]. Does not exist.");
return null;
}
if (inputNode == outputNode)
{
LogEngineError($"Can`t create link from [{output.Id}] to [{input.Id}]. Input and output belong to the same node.");
return null;
}
if (inputNode.PanelId != outputNode.PanelId)
{
LogEngineError($"Can`t create link from {outputNode.GetType().Name} to {inputNode.GetType().Name}. Nodes are on different panels.");
return null;
}
Link link = new Link(output.Id, input.Id, inputNode.PanelId);
//prevent two links to one input
Link oldLink = GetLinkForInput(input);
if (oldLink != null)
RemoveLink(oldLink, true);
LogEngineInfo($"New link from [{outputNode.GetType().Name}] to [{inputNode.GetType().Name}]");
lock (linksLock)
links.Add(link);
if (writeInDb)
nodesDb?.AddLink(link);
OnNewLink?.Invoke(link);
if (started)
input.Value = output.Value;
return link;
}