This commit is contained in:
Nitload
2025-08-25 09:50:30 +08:00
parent bd0e31e922
commit 1359ff4ecc
1630 changed files with 738486 additions and 2399 deletions
+146 -132
View File
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="PipelineNode.cs"
// file="PipeLineNode.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
@@ -47,71 +47,75 @@ namespace StgSharp.PipeLine
///
/// </summary>
/// <risk>
/// Reset pipeline has risk, this operation is a direct operation to port in current node, the
/// port on the other node cannot get the change.
/// Reset pipeline has risk, this operation is a direct operation to port in current node, the
/// port on the other node cannot get the change.
/// </risk>
#pragma warning disable CA1036
public class PipelineNode
public class PipeLineNode : IComparable<PipeLineNode>
#pragma warning restore CA1036
{
private static Dictionary<string, PipelineConnector> _emptyLayer = new();
private static PipelineNode _emptyNode = new PipelineNode(
PipelineNodeLabel.Empty, null!, false );
private IConvertableToPipelineNode _mainOperation;
private PipelineNodeLabel _label;
private protected bool _nativeRequested;
private protected Dictionary<string, PipelineNodeInPort> _input;
private protected Dictionary<string, PipelineNodeOutPort> _output;
private protected int _level, _count;
internal PipelineNode(
PipelineNodeLabel label,
IConvertableToPipelineNode node,
bool isNative )
private static PipeLineNode _emptyNode = new PipeLineNode(
string.Empty, null!, false );
private IConvertableToBlueprintNode _mainOperation;
private string _name;
private protected bool _levelAvailable, _nativeRequested;
private protected Dictionary<string, PipelineConnector> _inputInterfaces;
private protected Dictionary<string, PipelineConnector> _outputInterfaces;
private protected int _level;
internal PipeLineNode(
string name,
IConvertableToBlueprintNode node,
bool isNative )
{
_label = label;
_levelAvailable = false;
_name = name;
_nativeRequested = isNative;
_mainOperation = node;
InputPorts = new();
OutputPorts = new();
foreach( string port in node.InputPortName ) {
InputPorts.Add( port, new PipelineNodeInPort( this, port ) );
_inputInterfaces = new Dictionary<string, PipelineConnector>();
_outputInterfaces = new Dictionary<string, PipelineConnector>();
foreach( string portName in node.InputInterfacesName ) {
_inputInterfaces.Add( portName, null! );
}
foreach( string port in node.OutputPortName ) {
OutputPorts.Add( port, new PipelineNodeOutPort( this, port ) );
foreach( string portName in node.OutputInterfacesName ) {
_outputInterfaces.Add( portName, null! );
}
}
#pragma warning disable CS8618
internal PipelineNode(
PipelineNodeOperation operation,
PipelineNodeLabel name,
bool isNative,
string[] inputNames,
string[] outputNames )
internal PipeLineNode(
BlueprintNodeOperation operation,
string name,
bool isNative,
string[] inputPorts,
string[] outputPorts )
#pragma warning restore CS8618
{
if( ( inputNames == null ) || ( inputNames.Length == 0 ) ) {
throw new ArgumentNullException( nameof( inputNames ) );
if( ( inputPorts == null ) || ( inputPorts.Length == 0 ) ) {
throw new ArgumentNullException( nameof( inputPorts ) );
}
if( ( outputNames == null ) || ( outputNames.Length == 0 ) ) {
throw new ArgumentNullException( nameof( outputNames ) );
if( ( outputPorts == null ) || ( outputPorts.Length == 0 ) ) {
throw new ArgumentNullException( nameof( outputPorts ) );
}
_label = name;
_levelAvailable = false;
_name = name;
_nativeRequested = isNative;
if( operation == null ) {
operation = DefaultOperation;
}
operation = operation ?? DefaultOperation;
_mainOperation = new DefaultConvertableToBlueprintNode(
operation, inputNames, outputNames );
InputPorts = new Dictionary<string, PipelineNodeInPort>();
OutputPorts = new Dictionary<string, PipelineNodeOutPort>();
foreach( string port in inputNames ) {
InputPorts.Add( port, new PipelineNodeInPort( this, port ) );
operation, inputPorts, outputPorts );
_inputInterfaces = new Dictionary<string, PipelineConnector>();
_outputInterfaces = new Dictionary<string, PipelineConnector>();
foreach( string portName in inputPorts ) {
_inputInterfaces.Add(
portName,
SealedBlueprintPiipeline.SealOutput( this, name ) );
}
foreach( string port in outputNames ) {
OutputPorts.Add( port, new PipelineNodeOutPort( this, port ) );
foreach( string portName in outputPorts ) {
_outputInterfaces.Add( portName, null! );
}
}
@@ -126,160 +130,170 @@ namespace StgSharp.PipeLine
get => _emptyLayer;
}
public IConvertableToPipelineNode NodeOperation => _mainOperation;
public int Level
{
get => _level;
private set { _level = value; }
get
{
if( _levelAvailable ) {
return _level;
}
_level = 0;
foreach( KeyValuePair<string, PipelineConnector> item in _inputInterfaces ) {
if( item.Value == null ) {
continue;
}
if( _level < item.Value.Level ) {
_level = item.Value.Level;
}
}
_level += 1;
_levelAvailable = true;
return _level;
}
}
public static PipelineNode Empty
public static PipeLineNode Empty
{
get => _emptyNode;
}
public PipelineNodeLabel Label => _label;
public string Name => _name;
internal Dictionary<string, PipelineNodeInPort> InputPorts
{
get => _input;
private set => _input = value;
}
internal Dictionary<string, PipelineConnector> InputInterfaces => _inputInterfaces;
internal Dictionary<string, PipelineNodeOutPort> OutputPorts
{
get => _output;
private set => _output = value;
}
internal Dictionary<string, PipelineConnector> OutputInterfaces => _outputInterfaces;
internal HashSet<PipelineNode> Previous { get; } = new HashSet<PipelineNode>();
internal HashSet<PipelineNode> Next { get; } = new HashSet<PipelineNode>();
internal IConvertableToPipelineNode Value
internal IConvertableToBlueprintNode Value
{
get => _mainOperation;
}
internal int RemainingIncreaseCount => Previous.Count - _count;
public static void Connect(
PipelineNode front,
string frontNodePort,
string backNodePort,
PipelineNode back )
public void Append(
[NotNull] PipeLineNode next,
string thisOutputPortName,
string afterInputPortName )
{
ArgumentNullException.ThrowIfNull( front );
ArgumentNullException.ThrowIfNull( back );
if( !front.OutputPorts.TryGetValue( frontNodePort,
out PipelineNodeOutPort? frontPort ) ) {
throw new ArgumentOutOfRangeException( $"Cannot find port named {frontNodePort}" );
this._levelAvailable = false;
PipelineConnector pipeline = new PipelineConnector( this, next );
if( next is EndingNode enode ) {
enode.SetCertainInputPort( afterInputPortName, pipeline );
} else {
next.SetCertainInputPort( afterInputPortName, pipeline );
}
if( !back.InputPorts.TryGetValue( backNodePort, out PipelineNodeInPort? backPort ) ) {
throw new ArgumentOutOfRangeException( $"Cannot find port named {backNodePort}" );
}
backPort.Connect( frontPort );
frontPort.Connect( backPort );
SetCertainOutputPort( thisOutputPortName, pipeline );
}
public IEnumerator<PipelineNode> GetFormerNodes()
public int CompareTo( PipeLineNode other )
{
foreach( PipelineNodeInPort port in InputPorts.Values )
{
IEnumerator<PipelineNode> enumerator = port.GetFormerNodes();
while( enumerator.MoveNext() ) {
yield return enumerator.Current;
}
if( null == other ) {
throw new ArgumentNullException( nameof( other ) );
}
return this.Level.CompareTo( other.Level );
}
public void Prepend(
[NotNull] PipeLineNode former,
string formerOutputPortName,
string thisInputPortName )
{
this._levelAvailable = false;
PipelineConnector pipeline = new PipelineConnector( former, this );
if( former is BeginningNode bnode ) {
bnode.SetCertainOutputPort( formerOutputPortName, pipeline );
} else {
former.SetCertainOutputPort( formerOutputPortName, pipeline );
}
SetCertainInputPort( thisInputPortName, pipeline );
}
public virtual void Run()
{
try
{
//Console.WriteLine(_label);
PipelineNodeInPort.WaitAll( InputPorts );
try {
//Console.WriteLine(_name);
PipelineConnector.WaitAll( _inputInterfaces );
if( _mainOperation != null ) {
_mainOperation.NodeMain( in _input, in _output );
_mainOperation.Operation(
in _inputInterfaces, in _outputInterfaces );
}
}
catch( Exception )
{
catch( Exception ) {
throw;
}
}
internal bool TryIncreaseLevelTo( int level )
{
_count++;
if( _count > Previous.Count ) {
return false;
}
Level = int.Max( level, this.Level );
return true;
}
protected static void DefaultOperation(
in Dictionary<string, PipelineNodeInPort> input,
in Dictionary<string, PipelineNodeOutPort> output )
in Dictionary<string, PipelineConnector> input,
in Dictionary<string, PipelineConnector> output )
{
PipelineNodeOutPort.SkipAll( output );
PipelineConnector.SkipAll( output );
}
#region port operation
public PipelineNodeInPort GetInputPort( string lable )
public PipelineConnector GetInputPort( string name )
{
if( !InputPorts.TryGetValue( lable, out PipelineNodeInPort? ret ) ) {
PipelineConnector ret;
if( !_inputInterfaces.TryGetValue( name, out ret ) ) {
throw new ArgumentOutOfRangeException(
$"Cannot find the interface named {lable} in node {Label}." );
$"Cannot find the interface named {name} in node {Name}." );
}
return ret;
}
public PipelineNodeOutPort GetOutputPort( string lable )
public PipelineConnector GetOutputPort( string name )
{
if( !OutputPorts.TryGetValue( lable, out PipelineNodeOutPort? ret ) ) {
PipelineConnector ret;
if( !_outputInterfaces.TryGetValue( name, out ret ) ) {
throw new ArgumentOutOfRangeException(
$"Cannot find the interface named {lable} in node {Label}." );
$"Cannot find the interface named {name} in node {Name}." );
}
return ret;
}
public PipelineNodeInPort DefineCertainInputPort( string label )
public void SetCertainInputPort(
string name,
PipelineConnector pipeline )
{
if( InputPorts.ContainsKey( label ) ) {
return null;
if( !_inputInterfaces.ContainsKey( name ) ) {
throw new ArgumentException(
$"Cannot find the interface named {name} in node {Name}." );
}
PipelineNodeInPort port = new PipelineNodeInPort( this, label );
InputPorts[ label ] = port;
return port;
_inputInterfaces[ name ] = pipeline;
}
public PipelineNodeOutPort DefineCertainOutputPort( string label )
public void SetCertainOutputPort(
string name,
PipelineConnector pipeline )
{
if( OutputPorts.ContainsKey( label ) ) {
return null;
if( !_outputInterfaces.ContainsKey( name ) ) {
throw new ArgumentOutOfRangeException(
$"Cannot find the interface named {name} in node {Name}." );
}
PipelineNodeOutPort port = new PipelineNodeOutPort( this, label );
OutputPorts[ label ] = port;
return port;
_outputInterfaces[ name ] = pipeline;
}
#endregion
}//--------------------------------- end of class -----------------------------------
public class PipelineNodeRunException : Exception
public class BlueprintNodeRunException : Exception
{
public PipelineNodeRunException() { }
protected BlueprintNodeRunException(
SerializationInfo info,
StreamingContext context )
: base( info, context ) { }
public PipelineNodeRunException( Exception ex ) : base( string.Empty, ex ) { }
public BlueprintNodeRunException() { }
public PipelineNodeRunException( string message ) : base( message ) { }
public BlueprintNodeRunException( Exception ex )
: base( string.Empty, ex ) { }
public PipelineNodeRunException( string message, Exception innerException )
public BlueprintNodeRunException( string message ) : base( message ) { }
public BlueprintNodeRunException(
string message,
Exception innerException )
: base( message, innerException ) { }
}