daily update on 20240528 by nitload youmu
This commit is contained in:
@@ -45,7 +45,7 @@ using System.Xml.Serialization;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public class Blueprint : IConvertableToBlueprintNode
|
||||
public partial class Blueprint : IConvertableToBlueprintNode
|
||||
{
|
||||
private BeginningNode beginNode;
|
||||
private Dictionary<string, BlueprintNode> allNode;
|
||||
|
||||
@@ -30,9 +30,12 @@
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
|
||||
using StgSharp.Entities;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -52,59 +55,64 @@ namespace StgSharp.Logic
|
||||
/// 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 BlueprintNode : IComparable<BlueprintNode>
|
||||
#pragma warning restore CA1036
|
||||
{
|
||||
|
||||
private Blueprint local;
|
||||
private BlueprintNodeAction mainOperation;
|
||||
|
||||
private bool levelAvailabel, nativeRequested;
|
||||
private int level;
|
||||
private protected bool levelAvailable, nativeRequested;
|
||||
private protected int _level;
|
||||
private string name;
|
||||
|
||||
protected Dictionary<string, BlueprintPipeline> inputInterfaces;
|
||||
protected Dictionary<string, BlueprintPipeline> outputInterfaces;
|
||||
private protected Dictionary<string, BlueprintPipeline> _inputInterfaces;
|
||||
private protected Dictionary<string, BlueprintPipeline> _outputInterfaces;
|
||||
|
||||
|
||||
internal BlueprintNode(string name,
|
||||
BlueprintNodeAction operation, bool isNative,
|
||||
string[] inputPorts,
|
||||
string[] outputPorts)
|
||||
{
|
||||
this.levelAvailabel = false;
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
nativeRequested = isNative;
|
||||
mainOperation = operation;
|
||||
inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length * outputPorts.Length == 0)
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length == 0 )
|
||||
{
|
||||
throw new Exception();
|
||||
throw new ArgumentNullException(nameof(inputPorts));
|
||||
}
|
||||
if (outputPorts.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(outputPorts));
|
||||
}
|
||||
foreach (var portName in inputPorts)
|
||||
{
|
||||
inputInterfaces.Add(portName, null);
|
||||
_inputInterfaces.Add(portName, null);
|
||||
}
|
||||
foreach (var portName in outputPorts)
|
||||
{
|
||||
outputInterfaces.Add(portName, null);
|
||||
_outputInterfaces.Add(portName, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public BlueprintNode(string name, IConvertableToBlueprintNode convertable)
|
||||
{
|
||||
this.levelAvailabel = false;
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
mainOperation = convertable.MainExecution;
|
||||
foreach (var soket in convertable.InputInterfacesName)
|
||||
{
|
||||
inputInterfaces.Add(soket, null);
|
||||
_inputInterfaces.Add(soket, null);
|
||||
}
|
||||
foreach (var soket in convertable.OutputInterfacesName)
|
||||
{
|
||||
outputInterfaces.Add(soket, null);
|
||||
_outputInterfaces.Add(soket, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,21 +121,21 @@ namespace StgSharp.Logic
|
||||
string[] outputPorts
|
||||
)
|
||||
{
|
||||
this.levelAvailabel = false;
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length * outputPorts.Length == 0)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
foreach (var portName in inputPorts)
|
||||
{
|
||||
inputInterfaces.Add(portName, null);
|
||||
_inputInterfaces.Add(portName, null);
|
||||
}
|
||||
foreach (var portName in outputPorts)
|
||||
{
|
||||
outputInterfaces.Add(portName, null);
|
||||
_outputInterfaces.Add(portName, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,35 +144,36 @@ namespace StgSharp.Logic
|
||||
get => nativeRequested;
|
||||
internal set => nativeRequested = value;
|
||||
}
|
||||
|
||||
public int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (levelAvailabel)
|
||||
if (levelAvailable)
|
||||
{
|
||||
return level;
|
||||
return _level;
|
||||
}
|
||||
level = 0;
|
||||
foreach (var item in inputInterfaces)
|
||||
_level = 0;
|
||||
foreach (var item in _inputInterfaces)
|
||||
{
|
||||
if (item.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (level <= item.Value.Level)
|
||||
if (_level <= item.Value.Level)
|
||||
{
|
||||
level = item.Value.Level;
|
||||
_level = item.Value.Level;
|
||||
}
|
||||
}
|
||||
level += 1;
|
||||
return level;
|
||||
_level += 1;
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
public string Name => name;
|
||||
|
||||
internal Dictionary<string, BlueprintPipeline> InputInterfaces => inputInterfaces;
|
||||
internal Dictionary<string, BlueprintPipeline> OutputInterfaces => outputInterfaces;
|
||||
internal Dictionary<string, BlueprintPipeline> InputInterfaces => _inputInterfaces;
|
||||
internal Dictionary<string, BlueprintPipeline> OutputInterfaces => _outputInterfaces;
|
||||
|
||||
|
||||
public void AddAfter(
|
||||
@@ -172,7 +181,7 @@ namespace StgSharp.Logic
|
||||
string formerOutputPortName,
|
||||
string thisInputPortName)
|
||||
{
|
||||
this.levelAvailabel = false;
|
||||
this.levelAvailable = false;
|
||||
BlueprintPipeline pipeline = new BlueprintPipeline(former, this);
|
||||
if (former is BeginningNode bnode)
|
||||
{
|
||||
@@ -191,7 +200,7 @@ namespace StgSharp.Logic
|
||||
string thisOutputPortName,
|
||||
string afterInputPortName)
|
||||
{
|
||||
this.levelAvailabel = false;
|
||||
this.levelAvailable = false;
|
||||
BlueprintPipeline pipeline = new BlueprintPipeline(this, after);
|
||||
if (after is EndingNode enode)
|
||||
{
|
||||
@@ -210,8 +219,8 @@ namespace StgSharp.Logic
|
||||
try
|
||||
{
|
||||
//Console.WriteLine(name);
|
||||
BlueprintPipeline.WaitAll(inputInterfaces);
|
||||
mainOperation(in inputInterfaces, in outputInterfaces);
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
mainOperation(in _inputInterfaces, in _outputInterfaces);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -219,55 +228,58 @@ namespace StgSharp.Logic
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int IComparable<BlueprintNode>.CompareTo(BlueprintNode other)
|
||||
public int CompareTo(BlueprintNode other)
|
||||
{
|
||||
#pragma warning disable CA1062
|
||||
return this.Level.CompareTo(other.Level);
|
||||
#pragma warning restore CA1062
|
||||
}
|
||||
|
||||
#region port operation
|
||||
|
||||
public BlueprintPipeline GetCertainInputPort(string name)
|
||||
{
|
||||
if (!inputInterfaces.ContainsKey(name))
|
||||
BlueprintPipeline ret;
|
||||
if (!_inputInterfaces.TryGetValue(name,out ret))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
return inputInterfaces[name];
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public BlueprintPipeline GetCertainOutputPort(string name)
|
||||
{
|
||||
if (!outputInterfaces.ContainsKey(name))
|
||||
BlueprintPipeline ret;
|
||||
if (!_outputInterfaces.TryGetValue(name, out ret))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
return outputInterfaces[name];
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void SetCertainInputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (!inputInterfaces.ContainsKey(name))
|
||||
if (!_inputInterfaces.ContainsKey(name))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
inputInterfaces[name] = pipeline;
|
||||
_inputInterfaces[name] = pipeline;
|
||||
}
|
||||
|
||||
public void SetCertainOutputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (!outputInterfaces.ContainsKey(name))
|
||||
if (!_outputInterfaces.ContainsKey(name))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
outputInterfaces[name] = pipeline;
|
||||
_outputInterfaces[name] = pipeline;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}//--------------------------------- end of class -----------------------------
|
||||
}//--------------------------------- end of class -----------------------------------
|
||||
|
||||
public interface IConvertableToBlueprintNode
|
||||
{
|
||||
@@ -280,15 +292,25 @@ namespace StgSharp.Logic
|
||||
|
||||
public class BlueprintNodeRunException : Exception
|
||||
{
|
||||
public BlueprintNodeRunException()
|
||||
{
|
||||
}
|
||||
|
||||
public BlueprintNodeRunException(Exception ex) : base(string.Empty, ex)
|
||||
{
|
||||
}
|
||||
|
||||
public BlueprintNodeRunException(string message, Exception ex) : base(message, ex)
|
||||
public BlueprintNodeRunException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public BlueprintNodeRunException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected BlueprintNodeRunException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace StgSharp.Logic
|
||||
|
||||
public int Level => former.Level;
|
||||
|
||||
public BlueprintPipelineArgs Value
|
||||
public BlueprintPipelineArgs Args
|
||||
{
|
||||
get => args;
|
||||
set => args = value;
|
||||
@@ -120,5 +120,7 @@ namespace StgSharp.Logic
|
||||
|
||||
public abstract class BlueprintPipelineArgs
|
||||
{
|
||||
public object? Value { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="RouteSwitch.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.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public class RouteSwitch : BlueprintNode
|
||||
{
|
||||
private protected Dictionary<string, (BlueprintPipeline pipeline, Func<bool> openingRule)> outputDictionary;
|
||||
|
||||
public RouteSwitch(string name, string inputPorts) :
|
||||
base(name, [inputPorts], [string.Empty])
|
||||
{
|
||||
outputDictionary = new Dictionary<string, (BlueprintPipeline pipeline, Func<bool> openingRule)>();
|
||||
}
|
||||
|
||||
public new int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (levelAvailable)
|
||||
{
|
||||
return _level;
|
||||
}
|
||||
_level = 0;
|
||||
KeyValuePair<string, BlueprintPipeline> item = _inputInterfaces.First();
|
||||
if (item.Value == null)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
if (_level <= item.Value.Level)
|
||||
{
|
||||
_level = item.Value.Level;
|
||||
}
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
public new void AddBefore(
|
||||
BlueprintNode after,
|
||||
string thisOutputPortName,
|
||||
string afterInputPortName)
|
||||
{
|
||||
this.AppendNode(after, afterInputPortName, () => true);
|
||||
}
|
||||
|
||||
public void AppendNode(BlueprintNode node, string portName, Func<bool> openningRule)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(node));
|
||||
}
|
||||
string indexName = $"{node.Name}.{portName}";
|
||||
BlueprintPipeline p = new BlueprintPipeline(this, node);
|
||||
node.InputInterfaces[portName] = p;
|
||||
outputDictionary.Add(indexName, (p, openningRule));
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
BlueprintPipeline input = InputInterfaces.First().Value;
|
||||
input.WaitForStart();
|
||||
foreach (KeyValuePair<string, (BlueprintPipeline pipeline, Func<bool> openingRule)> item in outputDictionary)
|
||||
{
|
||||
if (item.Value.openingRule())
|
||||
{
|
||||
item.Value.pipeline.CompleteAndWriteData(input.Args);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public sealed class RouteHub : BlueprintNode
|
||||
{
|
||||
|
||||
private Dictionary<string, (BlueprintPipeline pipeline, Func<bool> controllingFunc)> inputDictionary;
|
||||
|
||||
public RouteHub(string name, string[] inputPorts, string[] outputPorts) : base(name, inputPorts, outputPorts)
|
||||
{
|
||||
inputDictionary = new Dictionary<string, (BlueprintPipeline pipeline, Func<bool> controllingFunc)>();
|
||||
}
|
||||
|
||||
|
||||
public new int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (levelAvailable)
|
||||
{
|
||||
return _level;
|
||||
}
|
||||
_level = 0;
|
||||
foreach (KeyValuePair<string, BlueprintPipeline> item in _inputInterfaces)
|
||||
{
|
||||
if (item.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (_level <= item.Value.Level)
|
||||
{
|
||||
_level = item.Value.Level;
|
||||
}
|
||||
}
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
public void PrependNode(BlueprintNode node, string portName, Func<bool> usingRule)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(node));
|
||||
}
|
||||
string indexName = $"{node.Name}.{portName}";
|
||||
BlueprintPipeline p = new BlueprintPipeline(this, node);
|
||||
node.InputInterfaces[portName] = p;
|
||||
inputDictionary.Add(indexName, (p, usingRule));
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (KeyValuePair<string, (BlueprintPipeline pipeline, Func<bool> controllingFunc)> item in inputDictionary)
|
||||
{
|
||||
item.Value.pipeline.WaitForStart();
|
||||
if (item.Value.controllingFunc())
|
||||
{
|
||||
_outputInterfaces.First().Value.CompleteAndWriteData(item.Value.pipeline.Args);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,12 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
internal sealed class BeginningNode:BlueprintNode
|
||||
internal sealed class BeginningNode : BlueprintNode
|
||||
{
|
||||
public BeginningNode():base(name : "BeginningNode",null,
|
||||
public BeginningNode() : base(name: "BeginningNode", null,
|
||||
true, ["default"], ["default"]
|
||||
) { }
|
||||
)
|
||||
{ }
|
||||
|
||||
internal (string name, BlueprintPipelineArgs arg)[] InputData
|
||||
{
|
||||
@@ -25,60 +26,61 @@ namespace StgSharp.Logic
|
||||
}
|
||||
foreach (var parameter in value)
|
||||
{
|
||||
OutputInterfaces[parameter.name].Value = parameter.arg;
|
||||
OutputInterfaces[parameter.name].Args = parameter.arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public new void SetCertainOutputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (outputInterfaces.ContainsKey(name))
|
||||
if (_outputInterfaces.ContainsKey(name))
|
||||
{
|
||||
outputInterfaces[name] = pipeline;
|
||||
_outputInterfaces[name] = pipeline;
|
||||
}
|
||||
outputInterfaces.Add(name,pipeline);
|
||||
_outputInterfaces.Add(name, pipeline);
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
BlueprintPipeline.SkipAll(outputInterfaces);
|
||||
BlueprintPipeline.SkipAll(_outputInterfaces);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class EndingNode : BlueprintNode
|
||||
{
|
||||
public EndingNode():base(name:"EndingNode",
|
||||
public EndingNode() : base(name: "EndingNode",
|
||||
operation: (
|
||||
in Dictionary<string , BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output
|
||||
) =>
|
||||
) =>
|
||||
{
|
||||
},
|
||||
true, ["default"], ["default"]
|
||||
) { }
|
||||
)
|
||||
{ }
|
||||
|
||||
internal (string name, BlueprintPipelineArgs arg)[] OutputData
|
||||
{
|
||||
get
|
||||
{
|
||||
get
|
||||
{
|
||||
return InputInterfaces.Select(
|
||||
key => (key.Key, key.Value.Value) ).
|
||||
key => (key.Key, key.Value.Args)).
|
||||
ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public new void SetCertainInputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (inputInterfaces.ContainsKey(name))
|
||||
if (_inputInterfaces.ContainsKey(name))
|
||||
{
|
||||
inputInterfaces[name] = pipeline;
|
||||
_inputInterfaces[name] = pipeline;
|
||||
}
|
||||
inputInterfaces.Add(name, pipeline);
|
||||
_inputInterfaces.Add(name, pipeline);
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
public new void Run()
|
||||
{
|
||||
BlueprintPipeline.WaitAll(inputInterfaces);
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="TimeFunction.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.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public class TimeFunction
|
||||
{
|
||||
private float _beginning, _ending;
|
||||
private Func<float, float> _timeRelatedCalculation;
|
||||
private TimeSpanProvider _timeProvider;
|
||||
|
||||
public TimeFunction(
|
||||
TimeSpanProvider timeSpanProvider,
|
||||
Func<float, float> timeRelatedCalculation,
|
||||
float range)
|
||||
{
|
||||
_timeProvider = timeSpanProvider;
|
||||
_timeRelatedCalculation = timeRelatedCalculation;
|
||||
if (timeSpanProvider == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_beginning = _timeProvider.CurrentSecond;
|
||||
_ending = _timeProvider.CurrentSecond + range;
|
||||
}
|
||||
|
||||
internal TimeFunction() { }
|
||||
|
||||
public bool IsComplete
|
||||
{
|
||||
get => _timeProvider.CurrentSecond > _ending;
|
||||
}
|
||||
|
||||
public Func<float, float> TimeRelatedCalculation
|
||||
{
|
||||
get { return _timeRelatedCalculation; }
|
||||
set { _timeRelatedCalculation = value; }
|
||||
}
|
||||
|
||||
public TimeSpanProvider TimeSpanProvider
|
||||
{
|
||||
get { return _timeProvider; }
|
||||
set { _timeProvider = value; }
|
||||
}
|
||||
|
||||
public float Calculate()
|
||||
{
|
||||
if (_timeProvider.CurrentSecond < _ending)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return _timeRelatedCalculation(_timeProvider.CurrentSecond - _beginning);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
float range = _ending - _beginning;
|
||||
_beginning = _timeProvider.CurrentSecond;
|
||||
_ending = _beginning + range;
|
||||
}
|
||||
|
||||
public TimeFunction Renew(Func<float,float> func,float range)
|
||||
{
|
||||
return new TimeFunction(_timeProvider,func,range);
|
||||
}
|
||||
|
||||
public TimeFunction Renew((Func<float, float> func, float range) funcPair)
|
||||
{
|
||||
return new TimeFunction(_timeProvider, funcPair.func, funcPair.range);
|
||||
}
|
||||
|
||||
public static TimeFunction One
|
||||
{
|
||||
get => new TimeFunctionOne();
|
||||
}
|
||||
|
||||
public static TimeFunction Proportional(TimeSpanProvider time,float begin, float range)
|
||||
{
|
||||
return new TimeFunction(
|
||||
time, (float t) => t + begin, range
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class TimeFunctionOne : TimeFunction
|
||||
{
|
||||
internal TimeFunctionOne() : base()
|
||||
{
|
||||
}
|
||||
|
||||
public new bool IsCompleted
|
||||
{
|
||||
get => true;
|
||||
}
|
||||
|
||||
public new void Reset()
|
||||
{
|
||||
}
|
||||
|
||||
public new TimeFunction Renew(Func<float, float> func, float range)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
public new TimeFunction Renew((Func<float, float> func, float range) funcPair)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public float Calculate()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,15 +43,17 @@ namespace StgSharp
|
||||
public class TimeSpanProvider
|
||||
{
|
||||
|
||||
private Action _frameUpdateMissCallback = null;
|
||||
|
||||
private readonly int _maxSpanCount;
|
||||
private Action _frameUpdateMissCallback = () => { };
|
||||
private Action _refreshCallback;
|
||||
private SemaphoreSlim _refreshSemaphore = new SemaphoreSlim(0, 1);
|
||||
private long _spanBegin;
|
||||
private readonly long _spanLength;
|
||||
private bool _subscribed;
|
||||
private Counter<int> timeSpanCount = new Counter<int>(-1, 1, 1);
|
||||
private readonly int _maxSpanCount;
|
||||
private long _spanBegin;
|
||||
private readonly long _spanLength;
|
||||
private SemaphoreSlim _refreshSemaphore = new SemaphoreSlim(0, 1);
|
||||
private readonly TimeSourceProviderBase _timeSource;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="TimeSpanProvider"/> and define the length of time span. Length of a single time span is defined in microseconds.
|
||||
@@ -60,6 +62,11 @@ namespace StgSharp
|
||||
/// <param name="provider"></param>
|
||||
public TimeSpanProvider(long spanMicroSeconds, TimeSourceProviderBase provider)
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(provider));
|
||||
}
|
||||
_timeSource = provider;
|
||||
_spanLength = spanMicroSeconds;
|
||||
_maxSpanCount = int.MaxValue;
|
||||
provider.AddSubscriber(this);
|
||||
@@ -75,6 +82,10 @@ namespace StgSharp
|
||||
/// <param name="provider"></param>
|
||||
public TimeSpanProvider(double spanSeconds, TimeSourceProviderBase provider)
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(provider));
|
||||
}
|
||||
_spanLength = (long)(spanSeconds * 1000L * 1000L);
|
||||
_spanBegin = -_spanLength;
|
||||
_maxSpanCount = int.MaxValue;
|
||||
@@ -91,6 +102,10 @@ namespace StgSharp
|
||||
/// <param name="provider"></param>
|
||||
public TimeSpanProvider(long spanLength, int maxSpan, TimeSourceProviderBase provider)
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(provider));
|
||||
}
|
||||
if (maxSpan <= 0)
|
||||
{
|
||||
maxSpan = int.MaxValue;
|
||||
@@ -101,7 +116,14 @@ namespace StgSharp
|
||||
}
|
||||
|
||||
public int CurrentSpan
|
||||
=> timeSpanCount.Value;
|
||||
{
|
||||
get => timeSpanCount.Value;
|
||||
}
|
||||
|
||||
public float CurrentSecond
|
||||
{
|
||||
get => timeSpanCount.Value * SpanLengthLowPrecession;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Time source provider provided in <see cref="StgSharp"/> internal framework.
|
||||
@@ -114,6 +136,16 @@ namespace StgSharp
|
||||
set => _frameUpdateMissCallback = value;
|
||||
}
|
||||
|
||||
public double SpanLength
|
||||
{
|
||||
get => (_spanLength * 1.0) / (1000L * 1000L);
|
||||
}
|
||||
|
||||
public float SpanLengthLowPrecession
|
||||
{
|
||||
get => (_spanLength * 1.0f) / (1000 * 1000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The method this <see cref="TimeSpanProvider"/> will call every time a new time span started.
|
||||
/// </summary>
|
||||
@@ -125,6 +157,10 @@ namespace StgSharp
|
||||
|
||||
public void SubscribeToTimeSource(TimeSourceProviderBase serviceHandler)
|
||||
{
|
||||
if (serviceHandler == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serviceHandler));
|
||||
}
|
||||
if (_subscribed)
|
||||
{
|
||||
return;
|
||||
@@ -144,7 +180,7 @@ namespace StgSharp
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
_spanBegin = microseconds;
|
||||
timeSpanCount++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user