基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-03 c3a5f58e60f85400edfb5f5379145390316394c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.IO;
using System.Text.RegularExpressions;
 
namespace Moli.Desktop.Plugins;
 
public sealed partial class PluginManifestValidator : IPluginManifestValidator
{
    public PluginManifestValidationResult Validate(string pluginDirectory, PluginManifest manifest)
    {
        if (!IdPattern().IsMatch(manifest.Id))
        {
            return PluginManifestValidationResult.Failure("PluginIdInvalid", "Plugin id must be 3-64 characters and use letters, numbers, dot, underscore or dash.");
        }
 
        if (string.IsNullOrWhiteSpace(manifest.Name) || manifest.Name.Length > 80)
        {
            return PluginManifestValidationResult.Failure("PluginNameInvalid", "Plugin name length must be between 1 and 80.");
        }
 
        if (!SemVerPattern().IsMatch(manifest.Version))
        {
            return PluginManifestValidationResult.Failure("PluginVersionInvalid", "Plugin version must be SemVer-like, for example 1.0.0.");
        }
 
        if (string.IsNullOrWhiteSpace(manifest.Entry))
        {
            return PluginManifestValidationResult.Failure("PluginEntryMissing", "Plugin entry is required.");
        }
 
        if (!ValidateDefaultLayout(manifest.DefaultLayout, out var layoutError))
        {
            return PluginManifestValidationResult.Failure("PluginDefaultLayoutInvalid", layoutError);
        }
 
        if (Uri.TryCreate(manifest.Entry, UriKind.Absolute, out var remoteUri)
            && (remoteUri.Scheme == Uri.UriSchemeHttp || remoteUri.Scheme == Uri.UriSchemeHttps))
        {
            return PluginManifestValidationResult.Success(remoteUri, isRemoteEntry: true);
        }
 
        var root = Path.GetFullPath(pluginDirectory);
        var entryPath = Path.GetFullPath(Path.Combine(root, manifest.Entry));
        if (!entryPath.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)
            && !string.Equals(entryPath, root, StringComparison.OrdinalIgnoreCase))
        {
            return PluginManifestValidationResult.Failure("PluginEntryOutOfRoot", "Plugin entry must stay inside the plugin directory.");
        }
 
        if (!File.Exists(entryPath))
        {
            return PluginManifestValidationResult.Failure("PluginEntryMissing", "Plugin entry file does not exist.");
        }
 
        return PluginManifestValidationResult.Success(new Uri(entryPath), isRemoteEntry: false);
    }
 
    private static bool ValidateDefaultLayout(PluginDefaultLayout layout, out string error)
    {
        if (layout.Width is < 80 or > 3840)
        {
            error = "defaultLayout.width must be between 80 and 3840.";
            return false;
        }
 
        if (layout.Height is < 40 or > 2160)
        {
            error = "defaultLayout.height must be between 40 and 2160.";
            return false;
        }
 
        if (layout.Opacity is < 0.1 or > 1)
        {
            error = "defaultLayout.opacity must be between 0.1 and 1.0.";
            return false;
        }
 
        if (layout.Scale is < 0.5 or > 3)
        {
            error = "defaultLayout.scale must be between 0.5 and 3.0.";
            return false;
        }
 
        error = "";
        return true;
    }
 
    [GeneratedRegex("^[a-zA-Z0-9][a-zA-Z0-9._-]{2,63}$")]
    private static partial Regex IdPattern();
 
    [GeneratedRegex(@"^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$")]
    private static partial Regex SemVerPattern();
}