daily update on 20240828 by nitload youmu, autotexture added
This commit is contained in:
@@ -1,4 +1,34 @@
|
||||
using System;
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="BlueprintRunner.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 System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@@ -8,20 +38,57 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
using StgSharp.Threading;
|
||||
|
||||
namespace StgSharp.BluePrint
|
||||
{
|
||||
public static class BlueprintRunner
|
||||
{
|
||||
private static int _callingThread;
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>();
|
||||
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
|
||||
private static SemaphoreSlim bpLock = new SemaphoreSlim(0,1);
|
||||
|
||||
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource();
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>();
|
||||
private static int _callingThread;
|
||||
private static SemaphoreSlim bpLock = new SemaphoreSlim(0, 1);
|
||||
|
||||
internal static bool IsValidCalling
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((NodeRunner.CurrentBlueprint == null) || //no bp running at all
|
||||
((blueprintInWaiting.Count == 0) && (NodeRunner.CurrentBlueprint.IsRunning == false))) // a bp is quiting
|
||||
{
|
||||
//in case no bp is running
|
||||
return true;
|
||||
}
|
||||
int? id = Task.CurrentId;
|
||||
if (id is null)
|
||||
{
|
||||
// in case calling from main thread and current running bp
|
||||
//Console.WriteLine($"\t{_callingThread}");
|
||||
//Console.WriteLine($"\t{Thread.CurrentThread.ManagedThreadId}");
|
||||
return _callingThread == Thread.CurrentThread.ManagedThreadId;
|
||||
}
|
||||
foreach (NodeRunner item in allAvailableTask)
|
||||
{
|
||||
if (item.Id == id)
|
||||
{
|
||||
//if calling from any bp running task
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Run(Blueprint blueprint, Func<bool> terminateInstructor)
|
||||
{
|
||||
//Console.WriteLine();
|
||||
if (blueprint == null) return;
|
||||
if (blueprint == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (terminateInstructor == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(terminateInstructor));
|
||||
@@ -35,87 +102,23 @@ namespace StgSharp.Logic
|
||||
{
|
||||
//Console.WriteLine("valid and run");
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
do { } while(InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
{
|
||||
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bpLock.Wait();
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
{
|
||||
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
}
|
||||
|
||||
bpLock.Release();
|
||||
}
|
||||
Interlocked.Exchange(ref _callingThread, -1);
|
||||
}
|
||||
|
||||
internal static bool IsValidCalling
|
||||
{
|
||||
get
|
||||
{
|
||||
if (NodeRunner.CurrentBlueprint == null || //no bp running at all
|
||||
(blueprintInWaiting.Count == 0 && NodeRunner.CurrentBlueprint.IsRunning == false)) // a bp is quiting
|
||||
{
|
||||
//in case no bp is running
|
||||
return true;
|
||||
}
|
||||
int? id = Task.CurrentId;
|
||||
if (id is null)
|
||||
{
|
||||
// in case calling from main thread and current running bp
|
||||
//Console.WriteLine($"\t{_callingThread}");
|
||||
//Console.WriteLine($"\t{Thread.CurrentThread.ManagedThreadId}");
|
||||
return _callingThread == Thread.CurrentThread.ManagedThreadId;
|
||||
}
|
||||
foreach (var item in allAvailableTask)
|
||||
{
|
||||
if (item.Id == id)
|
||||
{
|
||||
//if calling from any bp running task
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool InterruptAndRunNewBp(Blueprint newBp)
|
||||
{
|
||||
Debug.Assert(newBp != null);
|
||||
if (newBp != NodeRunner.CurrentBlueprint && NodeRunner.CurrentBlueprint != null)
|
||||
{
|
||||
//if a running bp exists
|
||||
blueprintInWaiting.Push(NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
//load new bp to current bp
|
||||
Interlocked.Exchange(ref NodeRunner.CurrentBlueprint!, newBp);
|
||||
Interlocked.Exchange(ref _callingThread, Thread.CurrentThread.ManagedThreadId);
|
||||
//run beginning node to init it first.
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
(NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode)!.Run();
|
||||
}
|
||||
//refresh all tasks
|
||||
for (int i = 0; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
var item = allAvailableTask[i];
|
||||
if (item is null || item.IsCompleted)
|
||||
{
|
||||
allAvailableTask[i] = NodeRunner.Run();
|
||||
}
|
||||
}
|
||||
BlueprintNode node;
|
||||
while (!cts.Token.IsCancellationRequested
|
||||
&& NodeRunner.CurrentBlueprint.RequestNextNativeNode(out node))
|
||||
{
|
||||
node.Run();
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
(NodeRunner.CurrentBlueprint.EndLayer as EndingNode)!.Run();
|
||||
return blueprintInWaiting.TryPeek(out NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
|
||||
|
||||
public static void SetMaxTaskCount(int size)
|
||||
{
|
||||
@@ -135,41 +138,81 @@ namespace StgSharp.Logic
|
||||
}
|
||||
Array.Resize(ref allAvailableTask, size);
|
||||
}
|
||||
|
||||
|
||||
public static void Terminate()
|
||||
{
|
||||
cts.Cancel();
|
||||
NodeRunner.CurrentBlueprint.Terminate();
|
||||
foreach (var task in allAvailableTask)
|
||||
foreach (NodeRunner task in allAvailableTask)
|
||||
{
|
||||
task.Cts.Cancel();
|
||||
}
|
||||
foreach (var bp in blueprintInWaiting)
|
||||
foreach (Blueprint bp in blueprintInWaiting)
|
||||
{
|
||||
bp.Terminate();
|
||||
}
|
||||
blueprintInWaiting.Clear();
|
||||
}
|
||||
|
||||
internal static bool InterruptAndRunNewBp(Blueprint newBp)
|
||||
{
|
||||
Debug.Assert(newBp != null);
|
||||
if ((newBp != NodeRunner.CurrentBlueprint) && (NodeRunner.CurrentBlueprint != null))
|
||||
{
|
||||
//if a running bp exists
|
||||
blueprintInWaiting.Push(NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
//load new bp to current bp
|
||||
Interlocked.Exchange(ref NodeRunner.CurrentBlueprint!, newBp);
|
||||
Interlocked.Exchange(ref _callingThread, Thread.CurrentThread.ManagedThreadId);
|
||||
//run beginning node to init it first.
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
(NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode)!.Run();
|
||||
}
|
||||
//refresh all tasks
|
||||
for (int i = 0; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
NodeRunner item = allAvailableTask[i];
|
||||
if ((item is null) || item.IsCompleted)
|
||||
{
|
||||
allAvailableTask[i] = NodeRunner.Run();
|
||||
}
|
||||
}
|
||||
BlueprintNode node;
|
||||
while (!cts.Token.IsCancellationRequested
|
||||
&& NodeRunner.CurrentBlueprint.RequestNextNativeNode(out node))
|
||||
{
|
||||
node.Run();
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
(NodeRunner.CurrentBlueprint.EndLayer as EndingNode)!.Run();
|
||||
return blueprintInWaiting.TryPeek(out NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class NodeRunner: CancellableTask
|
||||
internal class NodeRunner : CancellableTask
|
||||
{
|
||||
private static Blueprint _current;
|
||||
|
||||
internal static ref Blueprint CurrentBlueprint
|
||||
{
|
||||
get => ref _current;
|
||||
}
|
||||
private static Blueprint _current;
|
||||
|
||||
internal NodeRunner() : base()
|
||||
{
|
||||
_startup = InternalRun;
|
||||
}
|
||||
|
||||
internal static ref Blueprint CurrentBlueprint
|
||||
{
|
||||
get => ref _current;
|
||||
}
|
||||
|
||||
internal static NodeRunner Run()
|
||||
{
|
||||
var ret = new NodeRunner() { _startup = InternalRun};
|
||||
NodeRunner ret = new NodeRunner { _startup = InternalRun };
|
||||
ret.taskInternal = Task.Run(ret.InternalAction);
|
||||
return ret;
|
||||
}
|
||||
@@ -178,7 +221,7 @@ namespace StgSharp.Logic
|
||||
{
|
||||
//Console.WriteLine("Task Begin");
|
||||
BlueprintNode item;
|
||||
while (CurrentBlueprint != null && CurrentBlueprint.IsRunning && !token.IsCancellationRequested
|
||||
while ((CurrentBlueprint != null) && CurrentBlueprint.IsRunning && !token.IsCancellationRequested
|
||||
&& CurrentBlueprint.RequestNexGlobalNode(out item))
|
||||
{
|
||||
//Console.WriteLine($"Get node of {item.Name}, going to run.");
|
||||
@@ -186,5 +229,6 @@ namespace StgSharp.Logic
|
||||
}
|
||||
//Console.WriteLine("Task Completed");
|
||||
}
|
||||
|
||||
}//------------------------------------- End of Class ------------------------------------------
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user