Skip to content

Commit

Permalink
*[WIP]重构插件系统2/3
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Jul 19, 2024
1 parent 173d057 commit 9469aa3
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 325 deletions.
277 changes: 1 addition & 276 deletions Core/SDKs/CustomScenario/CustomScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Reflection;
using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Core.SDKs.CustomType;
using Core.SDKs.HotKey;
using Core.SDKs.Services;
using Core.SDKs.Services.Plugin;
using Core.SDKs.Tools;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using PluginCore;
using PluginCore.Attribute;

#endregion

Expand Down Expand Up @@ -528,278 +524,7 @@ private void ParsePointItem(Dictionary<ScenarioMethodNode, Thread?> threads,
{
try
{
if (nowScenarioMethodNode.Plugin == "Kitopia")
{
switch (nowScenarioMethodNode.MerthodName)
{
case "判断":
{
if (nowScenarioMethodNode.Input[1].InputObject is true)
{
nowScenarioMethodNode.Output[0].InputObject = "当前流";
nowScenarioMethodNode.Output[0].IsNotUsed = false;
nowScenarioMethodNode.Output[1].IsNotUsed = true;
nowScenarioMethodNode.Output[1].InputObject = "未使用的流";
}
else
{
nowScenarioMethodNode.Output[1].InputObject = "当前流";
nowScenarioMethodNode.Output[0].InputObject = "未使用的流";
nowScenarioMethodNode.Output[0].IsNotUsed = true;
nowScenarioMethodNode.Output[1].IsNotUsed = false;
}

break;
}
case "一对二":
{
nowScenarioMethodNode.Output[0].InputObject = "流1";
nowScenarioMethodNode.Output[1].InputObject = "流2";
break;
}
case "一对N":
{
for (var i = 0; i < nowScenarioMethodNode.Output.Count; i++)
{
nowScenarioMethodNode.Output[i].InputObject = $"{i + 1}";
}

break;
}
case "相等":
{
if (nowScenarioMethodNode.Input[1].InputObject is null)
{
nowScenarioMethodNode.Output[0].InputObject = false;
}
else if (nowScenarioMethodNode.Input[2].InputObject is null)
{
nowScenarioMethodNode.Output[0].InputObject = false;
}
else
{
nowScenarioMethodNode.Output[0].InputObject =
nowScenarioMethodNode.Input[1].InputObject!.Equals(nowScenarioMethodNode.Input[2]
.InputObject);
}

foreach (var item in nowScenarioMethodNode.Output[0]
.GetSourceOrNextConnectorItems(connections))
{
item.InputObject = nowScenarioMethodNode.Output[0].InputObject;
MakeSourcePointState(item, nowScenarioMethodNode);
}

break;
}
case "valueSet":
{
if (Values.ContainsKey(nowScenarioMethodNode.ValueRef!))
{
Values.SetValueWithoutNotify(nowScenarioMethodNode.ValueRef!,
nowScenarioMethodNode.Input[1].InputObject!);
}

break;
}
case "valueGet":
{
if (Values.ContainsKey(nowScenarioMethodNode.ValueRef!))
{
foreach (var item in nowScenarioMethodNode.Output[0]
.GetSourceOrNextConnectorItems(connections))
{
item.InputObject = Values[nowScenarioMethodNode.ValueRef!];
MakeSourcePointState(item, nowScenarioMethodNode);
}
}

break;
}
case "打开/运行本地项目":
{
if (nowScenarioMethodNode.Input.Count() >= 3)
{
List<object> parameterList = new();
for (var index = 2; index < nowScenarioMethodNode.Input.Count; index++)
{
parameterList.Add(nowScenarioMethodNode.Input[index].InputObject);
}

ServiceManager.Services.GetService<ISearchItemTool>()
.OpenSearchItemByOnlyKey((string)nowScenarioMethodNode.Input[1].InputObject,
parameterList.ToArray());
}
else
{
ServiceManager.Services.GetService<ISearchItemTool>()
.OpenSearchItemByOnlyKey((string)nowScenarioMethodNode.Input[1].InputObject);
}

break;
}
case "Main":
{
for (var i = 1; i < nowScenarioMethodNode.Output.Count; i++)
{
foreach (var sourceOrNextConnectorItem in nowScenarioMethodNode.Output[i]
.GetSourceOrNextConnectorItems(connections))
{
sourceOrNextConnectorItem.InputObject = nowScenarioMethodNode.Output[i].InputObject;
}
}

break;
}
default:
{
var userInputConnector = nowScenarioMethodNode.Input.FirstOrDefault();


if (userInputConnector is null)
{
break;
}

if (userInputConnector.Title == "流输入")
{
userInputConnector = nowScenarioMethodNode.Input[1];
}

var userInputData = userInputConnector.InputObject;
if (userInputData is null or "")
{
throw new NullReferenceException();
}

if (nowScenarioMethodNode.MerthodName == "System.Int32")
{
userInputData = int.Parse(userInputData.ToString()!);
}

nowScenarioMethodNode.Output[0].InputObject = userInputData;
foreach (var item in nowScenarioMethodNode.Output[0]
.GetSourceOrNextConnectorItems(connections))
{
item.InputObject = userInputData;
MakeSourcePointState(item, nowScenarioMethodNode);
}

break;
}
}
}
else
{
var plugin = PluginManager.EnablePlugin[nowScenarioMethodNode.Plugin];
var customScenarioNodeMethod =
PluginOverall.CustomScenarioNodeMethods[nowScenarioMethodNode.Plugin];

var methodInfo = customScenarioNodeMethod[nowScenarioMethodNode.MerthodName].Item1;
List<object> list = new();
var index = 1;
foreach (var parameterInfo in methodInfo.GetParameters())
{
if (parameterInfo.ParameterType.GetCustomAttribute(typeof(AutoUnbox)) is not null)
{
var autoUnboxIndex = nowScenarioMethodNode.Input[index].AutoUnboxIndex;
var parameterList = new List<object>();
List<Type> parameterTypesList = new();
while (nowScenarioMethodNode.Input.Count >= index &&
nowScenarioMethodNode.Input[index].AutoUnboxIndex == autoUnboxIndex)
{
var item = nowScenarioMethodNode.Input[index].InputObject;
if (item != null)
{
parameterList.Add(item);
parameterTypesList.Add(item.GetType());
}
else
{
valid = false;
goto finnish;
}

index++;
}

var instance = parameterInfo.ParameterType.GetConstructor(parameterTypesList.ToArray())
?.Invoke(parameterList.ToArray());
if (instance != null)
{
list.Add(instance);
}
else
{
valid = false;
goto finnish;
}

continue;
}

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

var inputObject = nowScenarioMethodNode.Input[index].InputObject;
if (inputObject != null)
{
list.Add(inputObject);
}
else
{
valid = false;
goto finnish;
}

index++;
}

var invoke = methodInfo.Invoke(plugin.ServiceProvider!.GetService(methodInfo.DeclaringType!),
list.ToArray());

if (methodInfo.ReturnParameter.ParameterType.GetCustomAttribute(typeof(AutoUnbox)) is not null)
{
var type = methodInfo.ReturnParameter.ParameterType;
foreach (var memberInfo in type.GetProperties())
{
foreach (var connectorItem in nowScenarioMethodNode.Output)
{
if (connectorItem.Type == memberInfo.PropertyType)
{
var value = invoke.GetType()
.InvokeMember(memberInfo.Name,
BindingFlags.Instance | BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.GetProperty, null, invoke, null);

connectorItem.InputObject = value;
foreach (var item in connectorItem.GetSourceOrNextConnectorItems(connections))
{
item.InputObject = value;
}

break;
}
}
}
}
else
{
if (nowScenarioMethodNode.Output.Any())
{
nowScenarioMethodNode.Output[0].InputObject = invoke;
foreach (var item in nowScenarioMethodNode.Output[0]
.GetSourceOrNextConnectorItems(connections))
{
item.InputObject = invoke;
}
}
}
}
nowScenarioMethodNode.Invoke(cancellationToken, connections, Values);
}
catch (Exception e)
{
Expand Down
5 changes: 5 additions & 0 deletions Core/SDKs/CustomScenario/ScenarioException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Core.SDKs.CustomScenario;

public class ScenarioException(string empty) : Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@

namespace Core.SDKs.CustomScenario;

public class ScenarioMethodInfo
public class ScenarioMethod
{
public ScenarioMethodInfo(MethodInfo method, PluginInfo pluginInfo, ScenarioMethodAttribute attribute)
public ScenarioMethod(MethodInfo method, PluginInfo pluginInfo, ScenarioMethodAttribute attribute,
ScenarioMethodType type)
{
Method = method;
PluginInfo = pluginInfo;
Attribute = attribute;
Type = type;
}


public IServiceProvider ServiceProvider => PluginManager.EnablePlugin[PluginInfo.ToPlgString()].ServiceProvider;
public bool IsFromPlugin => PluginInfo is not null;

public ScenarioMethodType Type { get; }

//某些特殊的类型需要存储一定的数据,例如(变量读取/设置 需要对应的变量名)
public object TypeDate { get; set; }
public MethodInfo Method { get; }
public PluginInfo PluginInfo { get; }
public PluginInfo? PluginInfo { get; }
public ScenarioMethodAttribute Attribute { get; }

public string MethodAbsolutelyName
Expand All @@ -42,7 +52,7 @@ public string MethodAbsolutelyName
continue;
}

sb.Append($"{plugin.ToPlgString()} {genericArgument.ParameterType.FullName}");
sb.Append($"{PluginInfo.ToPlgString()} {genericArgument.ParameterType.FullName}");
sb.Append("|");
}

Expand All @@ -61,7 +71,7 @@ public ScenarioMethodNode GenerateNode()
{
var pointItem = new ScenarioMethodNode()
{
ScenarioMethodInfo = this,
ScenarioMethod = this,
Title = MethodTitle
};
ObservableCollection<ConnectorItem> inpItems = new();
Expand Down Expand Up @@ -132,8 +142,7 @@ public ScenarioMethodNode GenerateNode()
connectorItem.IsSelf = true;
try
{
var service = PluginManager.EnablePlugin[PluginInfo.ToPlgString()].ServiceProvider
.GetService(customNodeInputType.Type);
var service = ServiceProvider.GetService(customNodeInputType.Type);
connectorItem.PluginInputConnector = service as INodeInputConnector;
}
catch (Exception e)
Expand All @@ -152,6 +161,14 @@ public ScenarioMethodNode GenerateNode()
if (Method.ReturnParameter.ParameterType != typeof(void))
{
ObservableCollection<ConnectorItem> outItems = new();
inpItems.Add(new ConnectorItem()
{
Source = pointItem,
IsOut = true,
Type = typeof(NodeConnectorClass),
Title = "流输出",
TypeName = "节点"
});
if (Method.ReturnParameter.ParameterType.GetCustomAttribute(typeof(AutoUnbox)) is not null)
{
autoUnboxIndex++;
Expand Down Expand Up @@ -208,6 +225,7 @@ public ScenarioMethodNode GenerateNode()

pointItem.Input = inpItems;


return pointItem;
}
}
Loading

0 comments on commit 9469aa3

Please sign in to comment.