Skip to content

Commit

Permalink
fix: fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed Mar 2, 2024
1 parent dbf07ae commit 168f382
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 20 deletions.
30 changes: 30 additions & 0 deletions src/LipUI/BuiltInPlugins/LipPanel/LipPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using LipUI.Models.Plugin;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;

namespace LipUI.BuiltInPlugins.LipPanel;

//[LipUIModule]
internal class LipPanel : IHomePageModule
{
public Type PageType => typeof(LipPanelPage);

public string PluginName => "LipPanel";

public bool DefaultEnabled => true;

public Guid Guid => new("21A13C74-BEC2-41BE-782F-1EF4BD7701AB");

FrameworkElement IHomePageModule.IconContent => new SymbolIcon(Symbol.Remote)
{
Height = 32,
Width = 32,
};

Brush IHomePageModule.IconBackground =>
LipuiServices.ApplicationTheme is ApplicationTheme.Light ?
new SolidColorBrush(Colors.LightGray) :
new SolidColorBrush(Colors.DarkGray);
}
15 changes: 15 additions & 0 deletions src/LipUI/BuiltInPlugins/LipPanel/LipPanelPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Page
x:Class="LipUI.BuiltInPlugins.LipPanel.LipPanelPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:LipUI.BuiltInPlugins.LipPanel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>

</Grid>
</Page>
31 changes: 31 additions & 0 deletions src/LipUI/BuiltInPlugins/LipPanel/LipPanelPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace LipUI.BuiltInPlugins.LipPanel
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class LipPanelPage : Page
{
public LipPanelPage()
{
this.InitializeComponent();
}
}
}
16 changes: 14 additions & 2 deletions src/LipUI/BuiltInPlugins/LipuiTips/LipuiTips.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
namespace LipUI.BuiltInPlugins.LipuiTips;
using LipUI.Models.Plugin;

internal class LipuiTips
namespace LipUI.BuiltInPlugins.LipuiTips;

//[LipUIModule]
internal class LipuiTips : IPlugin
{
public string PluginName => "Tips";

public bool DefaultEnabled => true;

public Guid Guid => new("9116AABC-6448-9933-DE77-D477EB10B857");

void IPlugin.OnEnable(LipuiServices services)
{
}
}
9 changes: 9 additions & 0 deletions src/LipUI/LipUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<WindowsPackageType>None</WindowsPackageType>

</PropertyGroup>
<ItemGroup>
<None Remove="BuiltInPlugins\LipPanel\LipPanelPage.xaml" />
</ItemGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Runtime.Loader.UseRidGraph" Value="true" />
Expand All @@ -33,6 +36,12 @@
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>

<ItemGroup>
<Page Update="BuiltInPlugins\LipPanel\LipPanelPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>

<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
Expand Down
2 changes: 2 additions & 0 deletions src/LipUI/Models/Plugin/LipuiServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ public ConfigCollection GetPluginConfig(IPlugin plugin)
Theme = Main.Config.PersonalizationSettings.ColorTheme,
ApplicationWindow = InternalServices.MainWindow ?? throw new NullReferenceException()
};

public static ApplicationTheme ApplicationTheme => InternalServices.ApplicationTheme;
}
52 changes: 35 additions & 17 deletions src/LipUI/Models/Plugin/PluginConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,44 @@
namespace LipUI.Models.Plugin;

internal record struct PluginConfigFileInfo(string AssemblyName, string Name, Guid Guid);
internal record struct PluginKeyInfo(string AssemblyName, Guid Guid);

internal static partial class PluginConfigManager
{
private static readonly Dictionary<PluginKey, (PluginConfigFileInfo Info, ConfigCollection Config)> pluginConfigs = [];

[GeneratedRegex(@"(?<assembly_name>.+?)\+(?<name>.+)\+(?<guid>[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})$")]
private static partial Regex PluginConfigInfoRegex();
private static partial Regex PluginConfigFileNameRegex();

private static bool TryParse(string str, [NotNullWhen(true)] out PluginConfigFileInfo info)
[GeneratedRegex(@"(?<assembly_name>.+?)\+(?<guid>[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})$")]
private static partial Regex PluginKeyRegex();

private static string GetPluginKey(string assemblyName, in Guid guid) => $"{assemblyName}+{guid}";

private static bool TryParseFileInfo(string str, [NotNullWhen(true)] out PluginConfigFileInfo info)
{
info = default;

var match = PluginConfigInfoRegex().Match(str);
var match = PluginConfigFileNameRegex().Match(str);
if (match.Success is false)
return false;

info = new(match.Groups["assembly_name"].Value, match.Groups["name"].Value, Guid.Parse(match.Groups["guid"].Value));
return true;
}

private static bool TryParseKeyInfo(string str, [NotNullWhen(true)] out PluginKeyInfo info)
{
info = default;

var match = PluginKeyRegex().Match(str);
if (match.Success is false)
return false;

info = new(match.Groups["assembly_name"].Value, Guid.Parse(match.Groups["guid"].Value));
return true;
}

public static void Load()
{
lock (pluginConfigs)
Expand All @@ -37,22 +55,22 @@ public static void Load()
{
var key = Path.GetFileNameWithoutExtension(file.Name);

if (TryParse(key, out var info) is false)
if (TryParseFileInfo(key, out var info) is false)
continue;

pluginConfigs.Add(key, (info, ConfigCollection.Deserialize(File.ReadAllText(file.FullName))));
pluginConfigs.Add($"{info.AssemblyName}+{info.Guid}", (info, ConfigCollection.Deserialize(File.ReadAllText(file.FullName))));
}
}
}

public static void Save(PluginKey key, ConfigCollection config)
public static void Save(in PluginConfigFileInfo info, ConfigCollection config)
{
lock (config)
{
if (config.Changed is false)
return;

var path = Path.Combine(DefaultSettings.ConfigsDirectory, $"{key}.json");
var path = Path.Combine(DefaultSettings.ConfigsDirectory, $"{info.AssemblyName}+{info.Name}+{info.Guid}.json");
if (File.Exists(path))
File.Delete(path);
File.WriteAllText(path, config.Serialize());
Expand All @@ -65,14 +83,14 @@ public static void Save()
{
lock (pluginConfigs)
{
foreach (var (key, (_, config)) in pluginConfigs)
Save(key, config);
foreach (var (_, (info, config)) in pluginConfigs)
Save(info, config);
}
}

public static ConfigCollection? InitPluginConfig(PluginKey key)
public static ConfigCollection? InitPluginConfig(PluginKey key, string pluginDisplayName)
{
if (TryParse(key, out var info) is false)
if (TryParseKeyInfo(key, out var info) is false)
return null;

lock (pluginConfigs)
Expand All @@ -82,20 +100,20 @@ public static void Save()

var ret = new ConfigCollection();

pluginConfigs.Add(key, (info, ret));
pluginConfigs.Add(key, (new(info.AssemblyName, pluginDisplayName, info.Guid), ret));

return ret;
}
}

public static ConfigCollection? InitPluginConfig(IPlugin plugin)
=> InitPluginConfig(PluginSystem.GetPluginKey(plugin));
=> InitPluginConfig(PluginSystem.GetPluginKey(plugin), plugin.PluginName);

public static bool TryGetPluginConfig(PluginKey key, [NotNullWhen(true)] out ConfigCollection? config)
{
config = null;

if (TryParse(key, out var _) && pluginConfigs.TryGetValue(key, out var temp) is true)
if (TryParseKeyInfo(key, out var _) && pluginConfigs.TryGetValue(key, out var temp) is true)
{
config = temp.Config;
return true;
Expand All @@ -107,16 +125,16 @@ public static bool TryGetPluginConfig(PluginKey key, [NotNullWhen(true)] out Con
public static bool TryGetPluginConfig(IPlugin plugin, [NotNullWhen(true)] out ConfigCollection? config)
=> TryGetPluginConfig(PluginSystem.GetPluginKey(plugin), out config);

public static ConfigCollection GetOrCreatePluginConfig(PluginKey key)
public static ConfigCollection GetOrCreatePluginConfig(PluginKey key, string pluginDisplayName)
{
if (TryGetPluginConfig(key, out var config))
return config;

return InitPluginConfig(key) ?? throw new NullReferenceException();
return InitPluginConfig(key, pluginDisplayName) ?? throw new NullReferenceException();
}

public static ConfigCollection GetOrCreatePluginConfig(IPlugin plugin)
=> GetOrCreatePluginConfig(PluginSystem.GetPluginKey(plugin));
=> GetOrCreatePluginConfig(PluginSystem.GetPluginKey(plugin), plugin.PluginName);

public static bool Contains(IPlugin plugin) => pluginConfigs.ContainsKey(PluginSystem.GetPluginKey(plugin));
}
2 changes: 1 addition & 1 deletion src/LipUI/Models/Plugin/PluginSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private static async ValueTask InitializePlugins(IEnumerable<IPlugin> plugins)
}

public static string GetPluginKey(IPlugin plugin)
=> $"{plugin.GetType().Assembly.GetName().Name}+{plugin.PluginName}+{plugin.Guid}";
=> $"{plugin.GetType().Assembly.GetName().Name}+{plugin.Guid}";

private static async ValueTask EnablePlugins(IEnumerable<IPlugin> plugins)
{
Expand Down

0 comments on commit 168f382

Please sign in to comment.