Skip to content

Commit

Permalink
修复 情景加载错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Aug 25, 2024
1 parent 32b0797 commit 030751b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 43 deletions.
10 changes: 10 additions & 0 deletions Core/JsonConverter/ScenarioMethodJsonCtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public override ScenarioMethod Read(ref Utf8JsonReader reader, Type typeToConver
ScenarioMethod scenario = JsonSerializer.Deserialize<ScenarioMethod>(ref reader, options)!;
if (scenario.IsFromPlugin)
{
if (!PluginManager.EnablePlugin.ContainsKey(scenario.PluginInfo!.ToPlgString()))
{
if (PluginManager.AllPluginInfos.Any(e=>e.ToPlgString()==scenario.PluginInfo!.ToPlgString()))
{
throw new CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType.插件未启用,
scenario.PluginInfo, null);
}
throw new CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType.插件未找到,
scenario.PluginInfo, null);
}
scenario.ServiceProvider = PluginManager.EnablePlugin[scenario.PluginInfo!.ToPlgString()].ServiceProvider!;
scenario.Method = PluginManager.EnablePlugin[scenario.PluginInfo.ToPlgString()]
.GetMethod(scenario._methodAbsolutelyName);
Expand Down
6 changes: 3 additions & 3 deletions Core/JsonConverter/TypeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer
}
}

throw new CustomScenarioLoadFromJsonException(strings[0], strings[1]);
throw new CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType.类未找到,strings[0], strings[1]);
}

if (PluginManager.EnablePlugin.TryGetValue(strings[0], out var value))
{
return value.GetType(strings[1]);
return value.GetType(strings[1])?? throw new CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType.类未找到,strings[0], strings[1]);;
}

throw new CustomScenarioLoadFromJsonException(strings[0], strings[1]);
throw new CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType.插件未找到,strings[0], strings[1]);
}

public override void Write(Utf8JsonWriter writer, Type type, JsonSerializerOptions options)
Expand Down
42 changes: 36 additions & 6 deletions Core/SDKs/CustomScenario/CustomScenarioLoadFromJsonException.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
namespace Core.SDKs.CustomScenario;
using PluginCore;

namespace Core.SDKs.CustomScenario;

public enum CustomScenarioLoadFromJsonFailedType
{
插件未找到,
插件未启用,
方法未找到,
类未找到
}
public class CustomScenarioLoadFromJsonException : Exception
{
public string PluginName
private string? _pluginName;
public CustomScenarioLoadFromJsonFailedType FailedType { get; set; }

public string? PluginName

{
get;
set;
get
{
if (_pluginName is null)
return PluginInfo.Name;
return _pluginName;
}
set => _pluginName = value;
}

public string MethodName
public PluginInfo PluginInfo
{
get;
set;
}
public string? MethodName
{
get;
set;
}

public CustomScenarioLoadFromJsonException(string pluginName, string methodName)
public CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType customScenarioLoadFromJsonFailedType,string pluginName, string? methodName)
{
this.PluginName = pluginName;
this.MethodName = methodName;
this.FailedType = customScenarioLoadFromJsonFailedType;
}
public CustomScenarioLoadFromJsonException(CustomScenarioLoadFromJsonFailedType customScenarioLoadFromJsonFailedType,PluginInfo pluginInfo, string? methodName)
{
this.PluginInfo = pluginInfo;
this.MethodName = methodName;
this.FailedType = customScenarioLoadFromJsonFailedType;
}
}
112 changes: 80 additions & 32 deletions Core/SDKs/CustomScenario/CustomScenarioManger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.ObjectModel;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using CommunityToolkit.Mvvm.Messaging;
using Core.SDKs.HotKey;
using Core.SDKs.Services;
Expand Down Expand Up @@ -95,8 +98,9 @@ public static void Load(FileInfo fileInfo)
{
continue;
}

}

deserializeObject.HasInit = true;
deserializeObject.IsRunning = false;

Expand Down Expand Up @@ -143,47 +147,91 @@ void ConnectorInit(ConnectorItem connectorItem)

CustomScenarios.Add(deserializeObject);
}
catch (Exception e1)
catch (CustomScenarioLoadFromJsonException e1)
{
// Log.Error(e1);
Log.Error($"情景文件\"{fileInfo.FullName}\"加载失败");
try
var Name = string.Empty;
Log.Error($"情景文件\"{fileInfo.FullName}\"加载失败,内部异常");
var utf8JsonReader = new Utf8JsonReader(File.ReadAllBytes(fileInfo.FullName));
utf8JsonReader.Read();
while (utf8JsonReader.Read())
{
var pluginName = ((CustomScenarioLoadFromJsonException)e1).PluginName.Split("_");


var deserializeObject = JsonSerializer.Deserialize<CustomScenario>(json, ConfigManger.DefaultOptions)!;
deserializeObject.HasInit = false;
deserializeObject.IsRunning = false;
if (utf8JsonReader.TokenType==JsonTokenType.StartObject)
{
utf8JsonReader.Skip();
}

var content = $"对应文件\n{fileInfo.FullName}\n情景所需的插件不存在\n需要来自作者{pluginName[0]}的插件{pluginName[1]}";
deserializeObject.InitError = content;
CustomScenarios.Add(deserializeObject);
var dialog = new DialogContent()
if (utf8JsonReader.TokenType==JsonTokenType.PropertyName)
{
Title = $"自定义情景\"{deserializeObject.Name}\"加载失败",
Content = content,
PrimaryButtonText = "尝试在市场中自动安装",
CloseButtonText = "我知道了",
PrimaryAction = () => { }
};
((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null,
dialog);
var name = utf8JsonReader.GetString();
Log.Debug(name);
if (name==nameof(CustomScenario.Name))
{
utf8JsonReader.Read();
Name = utf8JsonReader.GetString();
break;
}
}
}
catch (Exception e)
switch (e1.FailedType)
{
var content = $"情景文件\n{fileInfo.FullName}\n加载失败疑似文件已损坏";
var dialog = new DialogContent()
case CustomScenarioLoadFromJsonFailedType.插件未找到:
{

var content = $"对应文件\n{fileInfo.FullName}\n情景所需的插件不存在\n需要插件{e1.PluginName}";
var dialog = new DialogContent()
{
Title = $"自定义情景\"{Name}\"加载失败",
Content = content,
PrimaryButtonText = "尝试在市场中自动安装",
CloseButtonText = "我知道了",
PrimaryAction = () => { }
};
((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null,
dialog);
break;
}
case CustomScenarioLoadFromJsonFailedType.插件未启用:
{
var content = $"对应文件\n{fileInfo.FullName}\n情景所需的插件未启用\n需要插件{e1.PluginName}";

var dialog = new DialogContent()
{
Title = $"自定义情景\"{Name}\"加载失败",
Content = content,
PrimaryButtonText = "启用该插件",
CloseButtonText = "我知道了",
PrimaryAction = () => { }
};
((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null,
dialog);
break;
}
case CustomScenarioLoadFromJsonFailedType.方法未找到:
{
Title = $"自定义情景\"{fileInfo.Name}\"加载失败",
Content = content,
CloseButtonText = "我知道了",
PrimaryAction = () => { }
};
((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null,
dialog);
break;
}
case CustomScenarioLoadFromJsonFailedType.类未找到:
{
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
catch (Exception e)
{
var content = $"情景文件\n{fileInfo.FullName}\n加载失败疑似文件已损坏";
var dialog = new DialogContent()
{
Title = $"自定义情景\"{fileInfo.Name}\"加载失败",
Content = content,
CloseButtonText = "我知道了",
PrimaryAction = () => { }
};
((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null,
dialog);
}
}


Expand Down
1 change: 0 additions & 1 deletion Core/SDKs/Services/Config/ConfigManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static class ConfigManger
WriteIndented = true,
ReferenceHandler = ReferenceHandler.Preserve,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),

// DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

Expand Down
2 changes: 1 addition & 1 deletion Core/SDKs/Services/Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Plugin(PluginInfo pluginInfo)
public PluginInfo PluginInfo { set; get; }


public Type GetType(string typeName)
public Type? GetType(string typeName)
{
foreach (var pluginAssembly in _plugin.Assemblies)
{
Expand Down

0 comments on commit 030751b

Please sign in to comment.