Skip to content

Commit

Permalink
优化 情景加载
Browse files Browse the repository at this point in the history
新增 情景加载失败时缺失的插件提示
新增 插件搜索注入Icon获取的支持
优化 消息弹窗方法
  • Loading branch information
MakesYT committed Oct 14, 2023
1 parent 9d12044 commit 942ad24
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 180 deletions.
1 change: 1 addition & 0 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

<ItemGroup>
<ProjectReference Include="..\..\nodify\Nodify\Nodify.csproj"/>
<ProjectReference Include="..\..\wpfui\src\Wpf.Ui\Wpf.Ui.csproj"/>
<ProjectReference Include="..\PluginCore\PluginCore.csproj"/>
</ItemGroup>

Expand Down
19 changes: 14 additions & 5 deletions Core/SDKs/CustomScenario/CustomScenarioLoadFromJsonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

public class CustomScenarioLoadFromJsonException : Exception
{
public string PluginName;
public string MethodName;
public string PluginName
{
get;
set;
}

public string MethodName
{
get;
set;
}

public CustomScenarioLoadFromJsonException(string PluginName, string MethodName)
public CustomScenarioLoadFromJsonException(string pluginName, string methodName)
{
this.PluginName = PluginName;
this.MethodName = MethodName;
this.PluginName = pluginName;
this.MethodName = methodName;
}
}
87 changes: 49 additions & 38 deletions Core/SDKs/CustomScenario/CustomScenarioManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static void Init()
{
while (!PluginManager.isInitialized)
{
Thread.Sleep(100);
}
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "customScenarios"))
Expand All @@ -31,56 +32,66 @@ public static void Init()
var json = File.ReadAllText(fileInfo.FullName);
try
{
try
var deserializeObject = JsonConvert.DeserializeObject<CustomScenario.CustomScenario>(json)!;
foreach (var value in deserializeObject.AutoTriggerType.Where(value => value.IsUsed))
{
var deserializeObject = JsonConvert.DeserializeObject<CustomScenario.CustomScenario>(json)!;
foreach (var value in deserializeObject.AutoTriggerType.Where(value => value.IsUsed))
switch (value.AutoTriggerType)
{
switch (value.AutoTriggerType)
case AutoTriggerType.系统关闭时:
{
case AutoTriggerType.系统关闭时:
{
CustomScenarioExecutorManager.SystemShutdown.AddCustomScenario(deserializeObject);
break;
}
case AutoTriggerType.软件关闭时:
{
CustomScenarioExecutorManager.SoftwareShutdown.AddCustomScenario(deserializeObject);
break;
}
case AutoTriggerType.软件启动时:
CustomScenarioExecutorManager.SystemShutdown.AddCustomScenario(deserializeObject);
break;
}
case AutoTriggerType.软件关闭时:
{
CustomScenarioExecutorManager.SoftwareShutdown.AddCustomScenario(deserializeObject);
break;
}
case AutoTriggerType.软件启动时:
{
CustomScenarioExecutorManager.SoftwareStarted.AddCustomScenario(deserializeObject);
break;
}
case AutoTriggerType.Custom:
{
if (CustomScenarioExecutorManager.CustomExecutors.ContainsKey(
value.AutoTriggerTypeFrom))
{
CustomScenarioExecutorManager.SoftwareStarted.AddCustomScenario(deserializeObject);
break;
CustomScenarioExecutorManager.CustomExecutors[value.AutoTriggerTypeFrom]
.AddCustomScenario(deserializeObject);
}
case AutoTriggerType.Custom:
{
if (CustomScenarioExecutorManager.CustomExecutors.ContainsKey(
value.AutoTriggerTypeFrom))
{
CustomScenarioExecutorManager.CustomExecutors[value.AutoTriggerTypeFrom]
.AddCustomScenario(deserializeObject);
}
break;
}
default:
throw new ArgumentOutOfRangeException();
break;
}
default:
throw new ArgumentOutOfRangeException();
}
deserializeObject.IsRunning = false;
CustomScenarios.Add(deserializeObject);
}
catch (Exception e)
{
Console.WriteLine(e);
}
deserializeObject.IsRunning = false;
CustomScenarios.Add(deserializeObject);
}
catch (Exception e1)
{
Log.Error(e1);
Log.Error("情景文件加载失败");
// Log.Error(e1);
Log.Error($"情景文件\"{fileInfo.FullName}\"加载失败");
var pluginName = ((CustomScenarioLoadFromJsonException)e1).PluginName.Split("_");
var deserializeObject = JsonConvert.DeserializeObject<CustomScenario.CustomScenario>(json,
new JsonSerializerSettings()
{
Error = (sender, args) =>
{
// 忽略错误并继续反序列化
args.ErrorContext.Handled = true;
}
})!;
((IToastService)ServiceManager.Services!.GetService(typeof(IToastService))!).ShowMessageBoxW(
$"自定义情景\"{deserializeObject.Name}\"加载失败",
$"对应文件\n{fileInfo.FullName}\n情景所需的插件不存在\n需要来自作者{pluginName[0]}的插件{pluginName[1]}", new
ShowMessageContent("我知道了", null, "尝试在市场中自动安装", () =>
{
System.Windows.MessageBox.Show("未实现");
}, null, null));
}
}
}).Start();
Expand Down
18 changes: 6 additions & 12 deletions Core/SDKs/Services/Config/TypeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
writer.WriteValue($"System {type.FullName}");
return;
}
else
{
writer.WriteValue($"{a} {type.FullName}");
return;
}

writer.WriteValue(type.AssemblyQualifiedName);
writer.WriteValue($"{a} {type.FullName}");
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Expand All @@ -51,14 +46,13 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

throw new CustomScenarioLoadFromJsonException(strings[0], strings[1]);
}
else return type;

return type;
}
else

if (PluginManager.EnablePlugin.TryGetValue(strings[0], out var value))
{
if (PluginManager.EnablePlugin.TryGetValue(strings[0], out var value))
{
return value.GetType(strings[1]);
}
return value.GetType(strings[1]);
}

throw new CustomScenarioLoadFromJsonException(strings[0], strings[1]);
Expand Down
7 changes: 3 additions & 4 deletions Core/SDKs/Services/IToastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

public interface IToastService
{
public void show(string text);
public void Show(string text);

[STAThread]
public void showMessageBox(string Title, string Content, Action? yesAction, Action? noAction);
public void ShowMessageBox(string title, string content, Action? yesAction, Action? noAction);

void showMessageBoxW(string title, object content, string CloseButtonText, string SecondaryButtonText,
string PrimaryButtonText, Action? yes, Action? no, Action? cancel);
void ShowMessageBoxW(string title, object content, ShowMessageContent showMessageContent);
}
38 changes: 19 additions & 19 deletions Core/SDKs/Services/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ public static void Init()
}
}
// #if DEBUG
// Log.Debug("Debug加载测试插件");
//
// var pluginInfoEx1 = Plugin.GetPluginInfoEx(
// @"D:\WPF.net\uToolkitopia\PluginDemo\bin\Debug\net7.0-windows\PluginDemo.dll",
// out var alcWeakRef1);
//
//
// while (alcWeakRef1.IsAlive)
// {
// GC.Collect();
// GC.WaitForPendingFinalizers();
// }
//
// Plugin.LoadBypath($"{pluginInfoEx1.Author}_{pluginInfoEx1.PluginId}", pluginInfoEx1.Path);
// //((ITaskEditorOpenService)ServiceManager.Services!.GetService(typeof(ITaskEditorOpenService))!)!.Open();
//
//
// #endif
#if DEBUG
// Log.Debug("Debug加载测试插件");
//
// var pluginInfoEx1 = Plugin.GetPluginInfoEx(
// @"D:\WPF.net\uToolkitopia\PluginDemo\bin\Debug\net7.0-windows\PluginDemo.dll",
// out var alcWeakRef1);
//
//
// while (alcWeakRef1.IsAlive)
// {
// GC.Collect();
// GC.WaitForPendingFinalizers();
// }
//
// Plugin.LoadBypath($"{pluginInfoEx1.Author}_{pluginInfoEx1.PluginId}", pluginInfoEx1.Path);
// //((ITaskEditorOpenService)ServiceManager.Services!.GetService(typeof(ITaskEditorOpenService))!)!.Open();
#endif
isInitialized = true;
});
}
Expand Down
51 changes: 51 additions & 0 deletions Core/SDKs/Services/ShowMessageContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Core.SDKs.Services;

public class ShowMessageContent
{
public ShowMessageContent(string? closeButtonText, string? secondaryButtonText, string? primaryButtonText,
Action? yes, Action? no, Action? cancel)
{
CloseButtonText = closeButtonText;
SecondaryButtonText = secondaryButtonText;
PrimaryButtonText = primaryButtonText;
Yes = yes;
No = no;
Cancel = cancel;
}

public string? CloseButtonText
{
get;
private set;
}

public string? SecondaryButtonText
{
get;
private set;
}

public string? PrimaryButtonText
{
get;
private set;
}

public Action? Yes
{
get;
private set;
}

public Action? No
{
get;
private set;
}

public Action? Cancel
{
get;
private set;
}
}
4 changes: 2 additions & 2 deletions Core/SDKs/Tools/AppTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void AutoStartEverything(Dictionary<string, SearchViewItem> collec
var 程序名称 = "noUAC.Everything";
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "noUAC\\" + 程序名称 + ".lnk"))
{
((IToastService)ServiceManager.Services!.GetService(typeof(IToastService))!).showMessageBox(
((IToastService)ServiceManager.Services!.GetService(typeof(IToastService))!).ShowMessageBox(
"Kitopia提示",
"Kitopia即将使用任务计划来创建绕过UAC启动Everything的快捷方式\n需要确认UAC权限\n按下取消则关闭自动启动功能\n路径:" +
AppDomain.CurrentDomain.BaseDirectory + "noUAC\\" + 程序名称 + ".lnk",
Expand Down Expand Up @@ -234,7 +234,7 @@ public static void GetAllApps(Dictionary<string, SearchViewItem> collection,
}

log.Debug(c.ToString());
((IToastService)ServiceManager.Services!.GetService(typeof(IToastService))!).showMessageBox("Kitopia建议",
((IToastService)ServiceManager.Services!.GetService(typeof(IToastService))!).ShowMessageBox("Kitopia建议",
c.ToString(),
() =>
{
Expand Down
4 changes: 2 additions & 2 deletions Core/ViewModel/Pages/SettingPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ partial void OnAutoStartChanged(bool value)
try
{
registry.SetValue("Kitopia", $"\"{strName}\""); //设置该子项的新的“键值对”
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).show("开机自启设置成功");
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启设置成功");
}
catch (Exception exception)
{
log.Error("开机自启设置失败");
log.Error(exception.StackTrace);
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).show("开机自启设置失败");
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启设置失败");
}
}
else
Expand Down
Loading

0 comments on commit 942ad24

Please sign in to comment.