Skip to content

Commit

Permalink
新增 情景CancellationToken以支持取消和资源回收
Browse files Browse the repository at this point in the history
新增 强制插件所有方法最后一个参数必须接收CancellationToken
新增 情景编辑添加复制节点方法
  • Loading branch information
MakesYT committed Oct 25, 2023
1 parent eed15a6 commit 7cd9ebc
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 11 deletions.
34 changes: 27 additions & 7 deletions Core/SDKs/CustomScenario/CustomScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

namespace Core.SDKs.CustomScenario;

public partial class CustomScenario : ObservableRecipient
public partial class CustomScenario : ObservableRecipient, IDisposable
{
private static readonly ILog Log = LogManager.GetLogger(nameof(CustomScenario));

private readonly Dictionary<PointItem, Thread?> _tasks = new();

[JsonIgnore] [ObservableProperty] private List<CustomScenarioInvoke> _autoTriggerType = new();
private CancellationTokenSource _cancellationTokenSource = new();

[JsonIgnore] [ObservableProperty] private string _description = "";

Expand Down Expand Up @@ -56,6 +57,11 @@ public BindingList<ConnectionItem> connections
set;
} = new();

public void Dispose()
{
_cancellationTokenSource.Dispose();
}

partial void OnNameChanged(string value)
{
WeakReferenceMessenger.Default.Send(new CustomScenarioChangeMsg()
Expand All @@ -74,14 +80,19 @@ private void StartRun(bool notRealTime)
return;
}


if (notRealTime)
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
IsRunning = true;
}


foreach (var task in _tasks)
{
task.Value?.Interrupt();
task.Value?.Join();
}

_tasks.Clear();
Expand Down Expand Up @@ -131,7 +142,7 @@ private void StartRun(bool notRealTime)
try
{
_tasks.Add(firstNodes, null);
ParsePointItem(firstNodes, false, notRealTime);
ParsePointItem(firstNodes, false, notRealTime, _cancellationTokenSource.Token);
}
catch (Exception e)
{
Expand Down Expand Up @@ -169,6 +180,7 @@ private void StartRun(bool notRealTime)
}
IsRunning = false;
_cancellationTokenSource.Cancel();
Log.Debug($"场景运行完成:{Name}");
break;
}
Expand Down Expand Up @@ -199,7 +211,8 @@ private void MakeSourcePointState(ConnectorItem targetConnectorItem, PointItem p
}
}

private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRealTime)
private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRealTime,
CancellationToken cancellationToken)
{
Log.Debug($"解析节点:{nowPointItem.Title}");
var valid = true;
Expand Down Expand Up @@ -245,7 +258,7 @@ private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRe
{
var task = new Thread(() =>
{
ParsePointItem(sourceSource, true, notRealTime);
ParsePointItem(sourceSource, true, notRealTime, cancellationToken);
});

// Log.Debug(sourceSource.Title);
Expand All @@ -262,8 +275,9 @@ private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRe
{
thread.Join();
}
//这是连接当前节点的节点

//这是连接当前节点的节点
cancellationToken.ThrowIfCancellationRequested();
foreach (var connectorItem in nowPointItem.Input)
{
foreach (var sourceSource in connectorItem.GetSourceOrNextPointItems(connections))
Expand Down Expand Up @@ -426,6 +440,12 @@ private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRe
continue;
}

if (index == nowPointItem.Input.Count)
{
list.Add(cancellationToken);
break;
}

var inputObject = nowPointItem.Input[index].InputObject;
if (inputObject != null)
{
Expand Down Expand Up @@ -501,7 +521,7 @@ private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRe

var task = new Thread(() =>
{
ParsePointItem(nextPointItem, false, notRealTime);
ParsePointItem(nextPointItem, false, notRealTime, cancellationToken);
});

_tasks.Add(nextPointItem, task);
Expand Down
10 changes: 10 additions & 0 deletions Core/SDKs/Services/Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public Plugin(string path)
{
if (methodInfo.GetCustomAttributes(typeof(PluginMethod)).Any()) //情景的可用节点
{
if (methodInfo.GetParameters()[^1].ParameterType.FullName != "System.Threading.CancellationToken")
{
continue;
}

methodInfos.Add(ToMtdString(methodInfo),
(methodInfo, GetPointItemByMethodInfo(methodInfo)));
}
Expand Down Expand Up @@ -256,6 +261,11 @@ private object GetPointItemByMethodInfo(MethodInfo methodInfo)
for (var index = 0; index < methodInfo.GetParameters().Length; index++)
{
var parameterInfo = methodInfo.GetParameters()[index];
if (parameterInfo.ParameterType.FullName == "System.Threading.CancellationToken")
{
continue;
}

if (parameterInfo.ParameterType.GetCustomAttribute(typeof(AutoUnbox)) is not null)
{
autoUnboxIndex++;
Expand Down
69 changes: 68 additions & 1 deletion Core/ViewModel/TaskEditor/TaskEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public TaskEditorViewModel()
return;
}
if (Scenario.nodes.Contains(e.PointItem) || _nodeMethods.Any(a => a.Contains(e.PointItem)))
if (Scenario.nodes.Contains(e.PointItem) || NodeMethods.Any(a => a.Contains(e.PointItem)))
{
if (e.ConnectorItem is not null)
{
Expand Down Expand Up @@ -168,7 +168,74 @@ private void AddNodes(PointItem pointItem)
RealType = connectorItem.RealType,
InputObject = connectorItem.InputObject,
AutoUnboxIndex = connectorItem.AutoUnboxIndex,
IsSelf = connectorItem.IsSelf,
IsOut = connectorItem.IsOut
});
}

ObservableCollection<ConnectorItem> output = new();
foreach (var connectorItem in pointItem.Output)
{
var connectorItem1 = new ConnectorItem
{
Anchor = new Point(connectorItem.Anchor.X, connectorItem.Anchor.Y),
Source = item,
Title = connectorItem.Title,
TypeName = connectorItem.TypeName,
RealType = connectorItem.RealType,
AutoUnboxIndex = connectorItem.AutoUnboxIndex,
Type = connectorItem.Type,
IsConnected = connectorItem.IsConnected,
IsOut = connectorItem.IsOut
};
if (connectorItem.Interfaces is { Count: > 0 })
{
List<string> interfaces = new();
foreach (var connectorItemInterface in connectorItem.Interfaces)
{
interfaces.Add(connectorItemInterface);
}

connectorItem1.Interfaces = interfaces;
}

output.Add(connectorItem1);
}

item.Input = input;
item.Output = output;
Scenario.nodes.Add(item);
}

[RelayCommand]
private void CopyNode(PointItem pointItem)
{
IsModified = true;
if (Scenario.nodes.IndexOf(pointItem) == 0)
{
return;
}

var item = new PointItem
{
Title = pointItem.Title,
Plugin = pointItem.Plugin,
MerthodName = pointItem.MerthodName,
Location = new Point(pointItem.Location.X + 40, pointItem.Location.Y + 40)
};
ObservableCollection<ConnectorItem> input = new();
foreach (var connectorItem in pointItem.Input)
{
input.Add(new ConnectorItem
{
Anchor = new Point(connectorItem.Anchor.X, connectorItem.Anchor.Y),
Source = item,
TypeName = connectorItem.TypeName,
Title = connectorItem.Title,
Type = connectorItem.Type,
RealType = connectorItem.RealType,
InputObject = connectorItem.InputObject,
AutoUnboxIndex = connectorItem.AutoUnboxIndex,
IsSelf = connectorItem.IsSelf,
IsOut = connectorItem.IsOut
});
Expand Down
5 changes: 3 additions & 2 deletions KitopiaEx/SearchItemEx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PluginCore;
using System.Threading;
using PluginCore;
using PluginCore.Attribute;

namespace KitopiaEx;
Expand All @@ -7,7 +8,7 @@ public class SearchItemEx
{
[PluginMethod("打开/运行本地项目", $"{nameof(item)}=本地项目",
"return=返回参数")]
public void OpenSearchViewItem(string item)
public void OpenSearchViewItem(string item, CancellationToken cancellationToken)
{
Kitopia.ISearchItemTool.OpenSearchItemByOnlyKey(item);
}
Expand Down
2 changes: 1 addition & 1 deletion uToolkitopia/View/Pages/Plugin/MyDataTemplateSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override DataTemplate SelectTemplate(object item, DependencyObject contai
}

// Check the type of the item and return the corresponding data template from the resources
if (item is not ConnectorItem pointItem || !pointItem.IsSelf)
if (item is not ConnectorItem { IsSelf: true } pointItem)
{
return element.FindResource("InputTemplate") as DataTemplate;
}
Expand Down
4 changes: 4 additions & 0 deletions uToolkitopia/View/TaskEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,12 @@
<nodify:Node.ContextMenu>
<ContextMenu DataContext="{StaticResource viewModel}">
<!-- ReSharper disable once Xaml.RedundantResource -->
<MenuItem Header="复制节点" Command="{Binding Data.CopyNodeCommand}"
CommandParameter="{Binding Data,Source={StaticResource runRecordList}}" />
<!-- ReSharper disable once Xaml.RedundantResource -->
<MenuItem Header="删除此节点" Command="{Binding Data.DelNodeCommand}"
CommandParameter="{Binding Data,Source={StaticResource runRecordList}}" />

</ContextMenu>
</nodify:Node.ContextMenu>
<nodify:Node.Resources>
Expand Down

0 comments on commit 7cd9ebc

Please sign in to comment.