Skip to content

Commit a85d690

Browse files
committed
Feat | BasePluginMetaData
1 parent b396a32 commit a85d690

12 files changed

+135
-96
lines changed

ShadowPluginLoader.WinUI/AbstractPluginLoader.Assignable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ShadowPluginLoader.WinUI;
1414
/// </summary>
1515
public abstract partial class AbstractPluginLoader<TMeta, TAPlugin> : IPluginLoader<TMeta, TAPlugin>
1616
where TAPlugin : AbstractPlugin<TMeta>
17-
where TMeta : AbstractPluginMetaData
17+
where TMeta : BasePluginMetaData
1818
{
1919
/// <summary>
2020
/// DependencyChecker

ShadowPluginLoader.WinUI/AbstractPluginLoader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ protected virtual void LoadPlugin(TMeta meta)
6565
stopwatch.Start();
6666
try
6767
{
68-
BeforeLoadPlugin(meta.MainPlugin.EntryPointType, meta);
69-
var instance = LoadMainPlugin(meta.MainPlugin.EntryPointType, meta);
70-
AfterLoadPlugin(meta.MainPlugin.EntryPointType, instance, meta);
68+
BeforeLoadPlugin(meta.MainPlugin, meta);
69+
var instance = LoadMainPlugin(meta.MainPlugin, meta);
70+
AfterLoadPlugin(meta.MainPlugin, instance, meta);
7171
_plugins[meta.Id] = instance;
7272
var enabled = PluginSettingsHelper.GetPluginIsEnabled(meta.Id);
7373
instance.Loaded();

ShadowPluginLoader.WinUI/AbstractPluginMetaData.cs

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Reflection;
8+
using System.Text.Json;
9+
using ShadowPluginLoader.WinUI.Helpers;
810

911
namespace ShadowPluginLoader.WinUI;
1012

@@ -50,11 +52,6 @@ public abstract record AbstractPluginMetaData : IPluginMetaData
5052
[Meta(Required = false, AsString = true)]
5153
public VersionRange SdkVersion { get; init; } = null!;
5254

53-
/// <summary>
54-
/// <inheritdoc cref="IPluginMetaData.MainPlugin"/>
55-
/// </summary>
56-
[Meta(Exclude = true)]
57-
public PluginEntryPointType MainPlugin { get; private set; } = null!;
5855

5956
/// <summary>
6057
/// <inheritdoc cref="IPluginMetaData.Priority"/>
@@ -68,62 +65,33 @@ public abstract record AbstractPluginMetaData : IPluginMetaData
6865
[Meta(Exclude = true, AsString = true)]
6966
public PluginDependency[] Dependencies { get; init; } = [];
7067

71-
/// <summary>
72-
/// <inheritdoc cref="IPluginMetaData.EntryPoints"/>
73-
/// </summary>
74-
[Meta(Exclude = true)]
75-
public PluginEntryPoint[] EntryPoints { get; init; } = [];
7668

7769
/// <summary>
7870
///
7971
/// </summary>
8072
private static readonly Type TargetTypeList = typeof(PluginEntryPointType[]);
8173

8274
/// <summary>
83-
/// LoadEntryPoint
75+
/// <inheritdoc cref="IPluginMetaData.Raw"/>
8476
/// </summary>
77+
[Meta(Exclude = true)]
78+
public JsonElement Raw { get; private set; }
79+
80+
/// <summary>
81+
///
82+
/// </summary>
83+
/// <param name="content"></param>
84+
/// <typeparam name="TMeta"></typeparam>
8585
/// <returns></returns>
86-
public void LoadEntryPoint(PropertyPath[] propertyPaths, Assembly assembly)
86+
public static TMeta? ToMeta<TMeta>(string content) where TMeta : AbstractPluginMetaData
8787
{
88-
foreach (var path in propertyPaths)
89-
{
90-
object? current = this;
91-
for (var i = 0; i < path.Path.Count - 1; i++)
92-
{
93-
var prop = path.Path[i];
94-
var next = prop.GetValue(current);
95-
if (next == null)
96-
{
97-
next = Activator.CreateInstance(prop.PropertyType);
98-
prop.SetValue(current, next);
99-
}
100-
current = next;
101-
}
102-
103-
var targetProp = path.TargetProperty;
104-
var isList = targetProp.PropertyType == TargetTypeList;
105-
106-
if (isList)
107-
{
108-
List<PluginEntryPointType> entryPoints = [];
109-
foreach (var entryPoint in EntryPoints)
110-
{
111-
if (entryPoint.Name == targetProp.Name && assembly.GetType(entryPoint.Type) is { } type)
112-
entryPoints.Add(new PluginEntryPointType(type));
113-
}
114-
targetProp.SetValue(current, entryPoints.ToArray());
115-
}
116-
else
117-
{
118-
PluginEntryPointType? entryPoint = null;
119-
foreach (var ep in EntryPoints)
120-
{
121-
if (ep.Name != targetProp.Name || assembly.GetType(ep.Type) is not { } type) continue;
122-
entryPoint = new PluginEntryPointType(type);
123-
break;
124-
}
125-
targetProp.SetValue(current, entryPoint);
126-
}
127-
}
88+
using var doc = JsonDocument.Parse(content);
89+
var root = doc.RootElement;
90+
91+
var meta = root.Deserialize<TMeta>(MetaDataHelper.Options);
92+
if (meta != null) meta.Raw = root.Clone();
93+
return meta;
12894
}
95+
96+
12997
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text.Json;
5+
using System.Text.Json.Nodes;
6+
using ShadowPluginLoader.Attributes;
7+
using ShadowPluginLoader.WinUI.Exceptions;
8+
using ShadowPluginLoader.WinUI.Helpers;
9+
using ShadowPluginLoader.WinUI.Models;
10+
11+
namespace ShadowPluginLoader.WinUI;
12+
13+
/// <summary>
14+
/// BasePluginMetaData
15+
/// </summary>
16+
public record BasePluginMetaData : AbstractPluginMetaData
17+
{
18+
/// <summary>
19+
/// Plugin Type
20+
/// </summary>
21+
[Meta(Exclude = true)]
22+
public Type MainPlugin { get; private set; } = null!;
23+
24+
/// <summary>
25+
/// EntryPoints
26+
/// </summary>
27+
[Meta(Exclude = true)]
28+
public PluginEntryPointType[] EntryPoints { get; private set; } = [];
29+
30+
/// <summary>
31+
///
32+
/// </summary>
33+
/// <returns></returns>
34+
public void ToBase(Assembly assembly)
35+
{
36+
if (Raw.TryGetProperty(nameof(MainPlugin), out var mainPluginProp) &&
37+
mainPluginProp.GetString() is { } mainPlugin)
38+
{
39+
MainPlugin = assembly.GetType(mainPlugin)!;
40+
}
41+
42+
if (MainPlugin == null) throw new PluginLoadMetaException($"Not Found MainPlugin In {Raw.ToString()}");
43+
if (!Raw.TryGetProperty(nameof(EntryPoints), out var entryPointsProp) ||
44+
entryPointsProp.ValueKind != JsonValueKind.Array) return;
45+
var entryPoints = new List<PluginEntryPointType>();
46+
foreach (var item in entryPointsProp.EnumerateArray())
47+
{
48+
var name = item.GetProperty("Name").GetString();
49+
var type = item.GetProperty("Type").GetString();
50+
if (name == null || type == null) continue;
51+
entryPoints.Add(new PluginEntryPointType(name, assembly.GetType(type)!));
52+
}
53+
54+
EntryPoints = entryPoints.ToArray();
55+
}
56+
}

ShadowPluginLoader.WinUI/Converters/NuGetVersionJsonConverter.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,33 @@
55

66
namespace ShadowPluginLoader.WinUI.Converters;
77

8+
/// <summary>
9+
/// 自定义的 JSON 转换器,用于处理 NuGetVersion 类型的序列化和反序列化
10+
/// </summary>
811
public class NuGetVersionJsonConverter : JsonConverter<NuGetVersion>
912
{
13+
/// <summary>
14+
/// 将 JSON 数据反序列化为 NuGetVersion 对象
15+
/// </summary>
16+
/// <param name="reader">UTF-8 JSON 读取器</param>
17+
/// <param name="typeToConvert">要转换的目标类型</param>
18+
/// <param name="options">JSON 序列化选项</param>
19+
/// <returns>反序列化后的 NuGetVersion 对象</returns>
1020
public override NuGetVersion? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1121
{
22+
// 从读取器中获取字符串值并创建新的 NuGetVersion 实例
1223
return new NuGetVersion(reader.GetString()!);
1324
}
1425

26+
/// <summary>
27+
/// 将 NuGetVersion 对象序列化为 JSON 数据
28+
/// </summary>
29+
/// <param name="writer">UTF-8 JSON 写入器</param>
30+
/// <param name="value">要序列化的 NuGetVersion 对象</param>
31+
/// <param name="options">JSON 序列化选项</param>
1532
public override void Write(Utf8JsonWriter writer, NuGetVersion value, JsonSerializerOptions options)
1633
{
34+
// 将 NuGetVersion 对象的字符串表示形式写入 JSON 输出
1735
writer.WriteStringValue(value.ToString());
1836
}
1937
}

ShadowPluginLoader.WinUI/Converters/PluginDependencyJsonConverter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ namespace ShadowPluginLoader.WinUI.Converters;
1010
using System.Text.Json.Serialization;
1111

1212
/// <summary>
13-
///
13+
/// 自定义JSON转换器,用于处理PluginDependency类型的序列化和反序列化
1414
/// </summary>
1515
public class PluginDependencyJsonConverter : JsonConverter<PluginDependency>
1616
{
17+
/// <summary>
18+
/// 从JSON中读取并转换为PluginDependency对象
19+
/// </summary>
20+
/// <param name="reader">UTF-8 JSON读取器</param>
21+
/// <param name="typeToConvert">目标转换类型</param>
22+
/// <param name="options">JSON序列化选项</param>
23+
/// <returns>反序列化后的PluginDependency对象</returns>
1724
public override PluginDependency? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1825
{
1926
if (reader.TokenType != JsonTokenType.StartObject)
@@ -42,7 +49,7 @@ public class PluginDependencyJsonConverter : JsonConverter<PluginDependency>
4249
break;
4350

4451
default:
45-
reader.Skip(); // 跳过未知字段
52+
reader.Skip();
4653
break;
4754
}
4855
}
@@ -51,6 +58,12 @@ public class PluginDependencyJsonConverter : JsonConverter<PluginDependency>
5158
return new PluginDependency(id, need);
5259
}
5360

61+
/// <summary>
62+
/// 将PluginDependency对象写入JSON
63+
/// </summary>
64+
/// <param name="writer">UTF-8 JSON写入器</param>
65+
/// <param name="value">要序列化的PluginDependency对象</param>
66+
/// <param name="options">JSON序列化选项</param>
5467
public override void Write(Utf8JsonWriter writer, PluginDependency value, JsonSerializerOptions options)
5568
{
5669
writer.WriteStartObject();

ShadowPluginLoader.WinUI/DIFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static DiFactory()
3434
/// </summary>
3535
public static void Init<TAPlugin, TMeta>()
3636
where TAPlugin : AbstractPlugin<TMeta>
37-
where TMeta : AbstractPluginMetaData
37+
where TMeta : BasePluginMetaData
3838
{
3939
MetaDataHelper.Init<TMeta>();
4040
var baseSdkConfig = BaseSdkConfig.Load();

ShadowPluginLoader.WinUI/Helpers/MetaDataHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ void Collect(Type? currentType, List<PropertyInfo> currentPath)
7171
{
7272
if (currentType == null || currentType == typeof(object) || !visitedTypes.Add(currentType)) return;
7373

74-
var props = currentType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
74+
var props = currentType.GetProperties(BindingFlags.Public | BindingFlags.Instance |
75+
BindingFlags.DeclaredOnly);
7576
foreach (var prop in props)
7677
{
7778
var newPath = new List<PropertyInfo>(currentPath) { prop };
@@ -125,8 +126,9 @@ public static void Init<TMeta>() where TMeta : AbstractPluginMetaData
125126
/// <returns></returns>
126127
public static TMeta? ToMeta<TMeta>(string content) where TMeta : AbstractPluginMetaData
127128
{
128-
return JsonSerializer.Deserialize<TMeta>(content, Options);
129+
return AbstractPluginMetaData.ToMeta<TMeta>(content);
129130
}
131+
130132
/// <summary>
131133
///
132134
/// </summary>

ShadowPluginLoader.WinUI/Interfaces/IPluginMetaData.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using NuGet.Versioning;
22
using ShadowPluginLoader.WinUI.Models;
33
using System;
4+
using System.Text.Json;
45

56
namespace ShadowPluginLoader.WinUI.Interfaces;
67

@@ -33,31 +34,27 @@ internal interface IPluginMetaData
3334
/// Plugin Sdk Version (When Build)
3435
/// </summary>
3536
VersionRange SdkVersion { get; init; }
36-
37-
/// <summary>
38-
/// Plugin Type
39-
/// </summary>
40-
PluginEntryPointType MainPlugin { get; }
41-
37+
4238
/// <summary>
4339
/// Plugin Dependencies
4440
/// </summary>
4541
PluginDependency[] Dependencies { get; init; }
46-
42+
4743
/// <summary>
4844
/// Loading Order Priority
4945
/// Determines the execution order of the plugin.
5046
/// Plugins with lower values are executed earlier.
5147
/// </summary>
5248
int Priority { get; init; }
5349

54-
/// <summary>
55-
/// EntryPoints
56-
/// </summary>
57-
PluginEntryPoint[] EntryPoints { get; init; }
5850

5951
/// <summary>
6052
/// BuiltIn Plugin
6153
/// </summary>
6254
bool BuiltIn { get; init; }
55+
56+
/// <summary>
57+
/// Raw JsonElement
58+
/// </summary>
59+
JsonElement Raw { get; }
6360
}

ShadowPluginLoader.WinUI/Models/PluginEntryPoint.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
namespace ShadowPluginLoader.WinUI.Models;
44

5-
/// <summary>
6-
/// Plugin EntryPoint
7-
/// </summary>
8-
public record PluginEntryPoint(string Name, string Type);
9-
105
/// <summary>
116
/// EntryPointType
127
/// </summary>
13-
public record PluginEntryPointType(Type EntryPointType);
8+
public record PluginEntryPointType(string Name, Type EntryPointType);

0 commit comments

Comments
 (0)