daily update on 20240428 by nitload youmu

This commit is contained in:
YoumuLiu
2024-04-29 00:27:16 +08:00
parent 0e01d99e1e
commit 0cd01ed9dd
17 changed files with 424 additions and 97 deletions
+167
View File
@@ -0,0 +1,167 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Blueprint.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 StgSharp;
using StgSharp.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharpDebug.Logic
{
public class Blueprint
{
private BlueprintNode beginNode;
private Dictionary<string, BlueprintNode> allNode;
private Func<bool> terminate;
private int currentNodeIndex = 0;
private List<Task> allThread;
private List<BlueprintNode> nativeNodeList;
private List<BlueprintNode> nodeList;
public Blueprint()
{
allThread = new List<Task>();
nodeList = new List<BlueprintNode>();
nativeNodeList = new List<BlueprintNode>();
terminate = () => false;
}
public bool IsRunning => allThread.Any(t => t.Status == TaskStatus.Running);
public void Init(int threadCount)
{
foreach (var item in allNode)
{
if (item.Value.IsNative)
{
nativeNodeList.Add(item.Value);
}
else
{
nodeList.Add(item.Value);
}
}
nativeNodeList.Sort();
nodeList.Sort();
if (threadCount > nodeList.Count)
{
InternalIO.InternalWriteLog(
"Thread setting error: amount of threads of this blueprint is larger than amount of nodes.", LogType.Warning);
threadCount = nodeList.Count;
}
for (int i = 0; i < threadCount; i++)
{
allThread.Add(new Task(
() =>
{
while (true)
{
try
{
while (true)
{
RequestNextNode().Run();
}
}
catch (NodeClearException) { }
if (terminate())
{
return;
}
}
}
));
}
}
/// <summary>
/// Run all nodes in current blueprint once.
/// </summary>
public void Run()
{
foreach (var t in allThread)
{
t.Start();
}
foreach (var node in nativeNodeList)
{
node.Run();
}
Task.WaitAll(allThread.ToArray());
}
/// <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)
{
terminate = terminateInstructor;
foreach (var t in allThread)
{
t.Start();
}
foreach (var node in nativeNodeList)
{
node.Run();
}
foreach (var node in nativeNodeList)
{
node.Run();
}
}
private BlueprintNode RequestNextNode()
{
if (currentNodeIndex < nodeList.Count)
{
BlueprintNode ret = nodeList[currentNodeIndex];
Interlocked.Increment(ref currentNodeIndex);
return ret;
}
throw new NodeClearException();
}
}
internal class NodeClearException : Exception
{
}
}
+186
View File
@@ -0,0 +1,186 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// 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
{
}
}