daily update on 20240828 by nitload youmu, autotexture added
This commit is contained in:
@@ -29,7 +29,7 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp;
|
||||
using StgSharp.Logic;
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -43,35 +43,72 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
namespace StgSharp.BluePrint
|
||||
{
|
||||
/// <summary>
|
||||
/// A sets of complex logic designed for running in multi threads environment.
|
||||
/// Warning: do not run a <see cref="Blueprint"/> instance in multi threads more than once.
|
||||
///
|
||||
/// </summary>
|
||||
public partial class Blueprint : IConvertableToBlueprintNode
|
||||
{
|
||||
|
||||
private BeginningNode beginNode;
|
||||
private Dictionary<string, BlueprintNode> allNode;
|
||||
private EndingNode endNode;
|
||||
private Func<bool> terminate;
|
||||
|
||||
private int currentNodeIndex;
|
||||
private List<Task> allThread;
|
||||
private Func<bool> terminate;
|
||||
private List<BlueprintNode> globalNodeList;
|
||||
private List<BlueprintNode> nativeNodeList;
|
||||
private List<BlueprintNode> nodeList;
|
||||
|
||||
private SemaphoreSlim runningStat;
|
||||
|
||||
internal int currentGlobalNodeIndex;
|
||||
internal int currentNativeNodeIndex;
|
||||
|
||||
public Blueprint()
|
||||
{
|
||||
allNode = new Dictionary<string, BlueprintNode>();
|
||||
allThread = new List<Task>();
|
||||
nodeList = new List<BlueprintNode>();
|
||||
globalNodeList = new List<BlueprintNode>();
|
||||
nativeNodeList = new List<BlueprintNode>();
|
||||
terminate = () => true;
|
||||
beginNode = new BeginningNode();
|
||||
endNode = new EndingNode();
|
||||
runningStat = new SemaphoreSlim(1, 1);
|
||||
beginNode = new BeginningNode(this);
|
||||
endNode = new EndingNode(this);
|
||||
}
|
||||
|
||||
public BlueprintNode BeginLayer => beginNode;
|
||||
public BlueprintNode EndLayer => endNode;
|
||||
|
||||
public bool IsRunning => allThread.Any(t => t.Status == TaskStatus.Running);
|
||||
public bool IsRunning
|
||||
{
|
||||
get => runningStat.CurrentCount == 0;
|
||||
}
|
||||
|
||||
/**/
|
||||
internal Func<bool> ShouldClose
|
||||
{
|
||||
get => terminate;
|
||||
set => terminate = value;
|
||||
}
|
||||
|
||||
internal SemaphoreSlim RunningStat
|
||||
{
|
||||
get => runningStat;
|
||||
}
|
||||
/**/
|
||||
|
||||
public void AddNode(BlueprintNode node, bool isNative)
|
||||
{
|
||||
_ = node ?? throw new ArgumentNullException(nameof(node));
|
||||
node.IsNative = isNative;
|
||||
allNode.Add(node.Name, node);
|
||||
}
|
||||
|
||||
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output)
|
||||
{
|
||||
BlueprintRunner.Run(this, () => true);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public (string name, BlueprintPipelineArgs arg)[] GetOutput()
|
||||
@@ -79,115 +116,21 @@ namespace StgSharp.Logic
|
||||
return endNode.OutputData;
|
||||
}
|
||||
|
||||
public void Init(int threadCount)
|
||||
public void Init()
|
||||
{
|
||||
if (threadCount > nodeList.Count)
|
||||
foreach (KeyValuePair<string, BlueprintNode> item in allNode)
|
||||
{
|
||||
InternalIO.InternalWriteLog(
|
||||
"Thread setting error: amount of threads of this blueprint is larger than amount of nodes.", LogType.Warning);
|
||||
threadCount = nodeList.Count;
|
||||
}
|
||||
if (threadCount!=0)
|
||||
{
|
||||
foreach (var item in allNode)
|
||||
if (item.Value.IsNative)
|
||||
{
|
||||
if (item.Value.IsNative)
|
||||
{
|
||||
nativeNodeList.Add(item.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeList.Add(item.Value);
|
||||
}
|
||||
nativeNodeList.Add(item.Value);
|
||||
}
|
||||
nativeNodeList.Sort();
|
||||
PrepareTasks(threadCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in allNode)
|
||||
else
|
||||
{
|
||||
nodeList.Add(item.Value);
|
||||
globalNodeList.Add(item.Value);
|
||||
}
|
||||
}
|
||||
nodeList.Sort();
|
||||
}
|
||||
|
||||
public void PrepareTasks(int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
allThread.Add(new Task(IndividualTask));
|
||||
}
|
||||
}
|
||||
|
||||
private void IndividualTask()
|
||||
{
|
||||
do
|
||||
{
|
||||
BlueprintNode node;
|
||||
while (RequestNextNode(out node))
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
} while (!terminate());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run all nodes repeatedly until it is instructed to terminate.
|
||||
/// </summary>
|
||||
/// <param name="terminateInstructor">Instructor to terminate all tasks. Returns <see langword="true"/> if tasks should terminate</param>
|
||||
public void RunRepeatedly(Func<bool> terminateInstructor)
|
||||
{
|
||||
if (allThread.Count != 0)
|
||||
{
|
||||
MultiThreadRun(terminateInstructor);
|
||||
return;
|
||||
}
|
||||
SingleThreadRun(terminateInstructor);
|
||||
}
|
||||
|
||||
private void MultiThreadRun(Func<bool> terminateInstructor)
|
||||
{
|
||||
if (terminate != terminateInstructor)
|
||||
{
|
||||
terminate = terminateInstructor;
|
||||
if (allThread.Count != 0)
|
||||
{
|
||||
foreach (var t in allThread)
|
||||
{
|
||||
t.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
beginNode.Run();
|
||||
foreach (var node in nativeNodeList)
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
EndLayer.Run();
|
||||
}
|
||||
|
||||
private void SingleThreadRun(Func<bool> terminateInstructor)
|
||||
{
|
||||
beginNode.Run();
|
||||
foreach (var item in nodeList)
|
||||
{
|
||||
item.Run();
|
||||
}
|
||||
EndLayer.Run();
|
||||
}
|
||||
|
||||
public void AddNativeNode(BlueprintNode node)
|
||||
{
|
||||
node.IsNative = true;
|
||||
allNode.Add(node.Name, node);
|
||||
}
|
||||
|
||||
public void AddAsyncNode(BlueprintNode node)
|
||||
{
|
||||
node.IsNative = false;
|
||||
allNode.Add(node.Name, node);
|
||||
nativeNodeList.Sort();
|
||||
globalNodeList.Sort();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -196,59 +139,47 @@ namespace StgSharp.Logic
|
||||
beginNode.InputData = parameters;
|
||||
}
|
||||
|
||||
private bool RequestNextNode(out BlueprintNode node)
|
||||
public void Terminate()
|
||||
{
|
||||
if (currentNodeIndex < nodeList.Count)
|
||||
Interlocked.Exchange(ref currentGlobalNodeIndex, int.MaxValue);
|
||||
Interlocked.Exchange(ref currentNativeNodeIndex, int.MaxValue);
|
||||
}
|
||||
|
||||
internal bool RequestNexGlobalNode(out BlueprintNode node)
|
||||
{
|
||||
if (currentGlobalNodeIndex < globalNodeList.Count)
|
||||
{
|
||||
BlueprintNode ret = nodeList[currentNodeIndex];
|
||||
Interlocked.Increment(ref currentNodeIndex);
|
||||
node = ret;
|
||||
node = globalNodeList[currentGlobalNodeIndex];
|
||||
Interlocked.Increment(ref currentGlobalNodeIndex);
|
||||
return true;
|
||||
}
|
||||
node = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
internal bool RequestNextNativeNode(out BlueprintNode node)
|
||||
{
|
||||
|
||||
if (currentNativeNodeIndex < nativeNodeList.Count)
|
||||
{
|
||||
node = nativeNodeList[currentNativeNodeIndex];
|
||||
Interlocked.Increment(ref currentNativeNodeIndex);
|
||||
return true;
|
||||
}
|
||||
Interlocked.Exchange(ref currentNodeIndex, 0);
|
||||
node = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] IConvertableToBlueprintNode.InputInterfacesName
|
||||
{
|
||||
get
|
||||
{
|
||||
return beginNode.OutputInterfaces.Select(
|
||||
p => p.Key).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
BlueprintNodeAction IConvertableToBlueprintNode.MainExecution
|
||||
{
|
||||
get
|
||||
{
|
||||
return (in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output)
|
||||
=>
|
||||
{
|
||||
beginNode.Run();
|
||||
foreach (var node in nativeNodeList)
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
endNode.Run();
|
||||
};
|
||||
}
|
||||
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
|
||||
}
|
||||
|
||||
string[] IConvertableToBlueprintNode.OutputInterfacesName
|
||||
{
|
||||
get
|
||||
{
|
||||
return beginNode.OutputInterfaces.Select(
|
||||
p => p.Key).ToArray();
|
||||
}
|
||||
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
|
||||
}
|
||||
}
|
||||
|
||||
internal class NodeClearException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user