Files
stgsharp/源代码/StgSharp/Logic/BlueprintNode.cs
T

187 lines
5.9 KiB
C#

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="BlueprintNode.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharpDebug.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Logic
{
public delegate void BlueprintNodeAction(
in Dictionary<string, BlueprintPipeline> inpput,
in Dictionary<string, BlueprintPipeline> output);
public abstract class BlueprintNode:IComparable<BlueprintNode>
{
private Blueprint local;
private BlueprintNodeAction mainOperation;
private bool levelAvailabel, nativeRequested;
private Dictionary<string, BlueprintPipeline> inputInterfaces;
private Dictionary<string, BlueprintPipeline> outputInterface;
private int level;
private string name;
protected BlueprintNode(string name, BlueprintNodeAction operation)
{
this.levelAvailabel = false;
this.name = name;
mainOperation = operation;
inputInterfaces = new Dictionary<string, BlueprintPipeline>();
outputInterface = new Dictionary<string, BlueprintPipeline>();
}
internal BlueprintNode(string name, bool isNative)
{
this.levelAvailabel = false;
this.name = name;
nativeRequested = isNative;
inputInterfaces = new Dictionary<string, BlueprintPipeline>();
outputInterface = new Dictionary<string, BlueprintPipeline>();
}
public bool IsNative => nativeRequested;
public int Level
{
get
{
if (levelAvailabel)
{
return level;
}
level = 0;
foreach (var item in inputInterfaces)
{
if (level <= item.Value.Level)
{
level = item.Value.Level;
}
}
level += 1;
return level;
}
}
internal Dictionary<string, BlueprintPipeline> InputInterfaces => inputInterfaces;
internal Dictionary<string, BlueprintPipeline> OutputInterface => outputInterface;
public void AddAfter(
BlueprintNode former,
string formerOutputPortName,
string thisInputPortName)
{
this.levelAvailabel = false;
BlueprintPipeline pipeline = new BlueprintPipeline(former,this);
}
public void Run()
{
try
{
foreach (var item in inputInterfaces)
{
item.Value.WaitForStart();
}
mainOperation(in inputInterfaces, in outputInterface);
}
catch (Exception)
{
throw;
}
}
int IComparable<BlueprintNode>.CompareTo(BlueprintNode other)
{
return this.Level.CompareTo(other.Level);
}
}//--------------------------------- end of class -----------------------------
public class BlueprintNodeRunException : Exception
{
private Exception realException;
public BlueprintNodeRunException(string message, Exception ex) : base(message, ex)
{
}
public BlueprintNodeRunException(Exception ex):base("",ex)
{
}
}
public class BlueprintPipeline
{
private BlueprintNode former;
private BlueprintNode after;
private BlueprintPipelineArgs args;
private SemaphoreSlim formerCompleteSemaphore;
public BlueprintPipeline(BlueprintNode former, BlueprintNode after)
{
this.former = former;
this.after = after;
formerCompleteSemaphore = new SemaphoreSlim(initialCount: 0, maxCount: 1);
}
public BlueprintNode Former => former;
public BlueprintNode After => after;
public int Level => former.Level;
public void CompleteAndWriteData(BlueprintPipelineArgs args)
{
formerCompleteSemaphore.Release();
this.args = args;
}
public void WaitForStart()
{
formerCompleteSemaphore.Release();
}
}
public abstract class BlueprintPipelineArgs
{
}
}