From a3367abd7a735f3d3bc0a5c916eb5d8e43106827 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 26 Mar 2023 14:04:06 -0500 Subject: [PATCH] fix some bug (v1 still broken) --- .../Plugin/JsonRPCPluginBase.cs | 6 +++-- Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs | 27 ++++--------------- Flow.Launcher.Core/Plugin/PortableSettings.cs | 13 +++++++++ Flow.Launcher.Core/Plugin/PythonPlugin.cs | 3 ++- Flow.Launcher.Core/Plugin/PythonPluginV2.cs | 13 +++++++-- .../Storage/JsonStorage.cs | 3 +++ 6 files changed, 38 insertions(+), 27 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs b/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs index 2ff076926..c3790709d 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs @@ -47,7 +47,7 @@ namespace Flow.Launcher.Core.Plugin public abstract List LoadContextMenus(Result selectedResult); - private static readonly JsonSerializerOptions options = new() + protected static readonly JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true, #pragma warning disable SYSLIB0020 @@ -155,6 +155,8 @@ namespace Flow.Launcher.Core.Plugin API = Context.API }; + await Settings.InitializeAsync(); + } public virtual async Task InitAsync(PluginInitContext context) @@ -165,7 +167,7 @@ namespace Flow.Launcher.Core.Plugin public void Save() { - Settings.Save(); + Settings?.Save(); } public Control CreateSettingPanel() { diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs b/Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs index 3bef2f191..bc5d00ed4 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs @@ -11,7 +11,7 @@ using Flow.Launcher.Plugin; namespace Flow.Launcher.Core.Plugin { - public abstract class JsonRpcPluginV2 : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable + internal abstract class JsonRpcPluginV2 : JsonRPCPluginBase { public abstract string SupportedLanguage { get; set; } @@ -56,14 +56,10 @@ namespace Flow.Launcher.Core.Plugin await JsonSerializer.SerializeAsync(InputStream, fullMessage, cancellationToken: token); } - public async Task> QueryAsync(Query query, CancellationToken token) + protected override async Task> QueryRequestAsync(JsonRPCRequestModel query, CancellationToken token) { int currentRequestId = Interlocked.Add(ref RequestId, 1); - var message = new JsonRPCRequestModel(currentRequestId, "query", new object[] - { - query - }); - await InputMessageChannel.Writer.WriteAsync(message, token); + await InputMessageChannel.Writer.WriteAsync(query, token); await Task.Delay(50, token); await InputStream.FlushAsync(token); var task = new TaskCompletionSource(); @@ -72,8 +68,9 @@ namespace Flow.Launcher.Core.Plugin //TODO: Parse Result return new List(); } - public virtual Task InitAsync(PluginInitContext context) + public override async Task InitAsync(PluginInitContext context) { + await base.InitAsync(context); InputMessageChannel = Channel.CreateUnbounded(); MessageCancellationTokenSource = new CancellationTokenSource(); SendMessageAsync(context.CurrentPluginMetadata, MessageCancellationTokenSource.Token); @@ -81,20 +78,6 @@ namespace Flow.Launcher.Core.Plugin // MessageTask = // (SendMessageAsync(context.CurrentPluginMetadata, MessageCancellationTokenSource.Token), // ReceiveMessageAsync(MessageCancellationTokenSource.Token)); - return Task.CompletedTask; - } - public List LoadContextMenus(Result selectedResult) - { - throw new System.NotImplementedException(); - } - public Control CreateSettingPanel() - { - // TODO: Implement CreateSettingPanel - return new Control(); - } - public void Save() - { - // TODO: Save settings } } } diff --git a/Flow.Launcher.Core/Plugin/PortableSettings.cs b/Flow.Launcher.Core/Plugin/PortableSettings.cs index 36d09c3f1..542460877 100644 --- a/Flow.Launcher.Core/Plugin/PortableSettings.cs +++ b/Flow.Launcher.Core/Plugin/PortableSettings.cs @@ -35,6 +35,19 @@ namespace Flow.Launcher.Core.Plugin { _storage = new JsonStorage>(SettingPath); Settings = await _storage.LoadAsync(); + + foreach (var (type, attributes) in Configuration.Body) + { + if (attributes.Name == null) + { + continue; + } + + if (!Settings.ContainsKey(attributes.Name)) + { + Settings[attributes.Name] = attributes.DefaultValue; + } + } } diff --git a/Flow.Launcher.Core/Plugin/PythonPlugin.cs b/Flow.Launcher.Core/Plugin/PythonPlugin.cs index 96838a1d1..62f260867 100644 --- a/Flow.Launcher.Core/Plugin/PythonPlugin.cs +++ b/Flow.Launcher.Core/Plugin/PythonPlugin.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Flow.Launcher.Infrastructure; @@ -38,7 +39,7 @@ namespace Flow.Launcher.Core.Plugin protected override Task RequestAsync(JsonRPCRequestModel request, CancellationToken token = default) { - _startInfo.ArgumentList[2] = request.ToString(); + _startInfo.ArgumentList[2] = JsonSerializer.Serialize(request); return ExecuteAsync(_startInfo, token); } diff --git a/Flow.Launcher.Core/Plugin/PythonPluginV2.cs b/Flow.Launcher.Core/Plugin/PythonPluginV2.cs index d5033e056..1e7a74a58 100644 --- a/Flow.Launcher.Core/Plugin/PythonPluginV2.cs +++ b/Flow.Launcher.Core/Plugin/PythonPluginV2.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Threading; @@ -8,7 +9,7 @@ using Flow.Launcher.Plugin; namespace Flow.Launcher.Core.Plugin { - public class PythonPluginV2 : JsonRpcPluginV2 + internal class PythonPluginV2 : JsonRpcPluginV2 { private readonly ProcessStartInfo _startInfo; private Process _process; @@ -42,8 +43,16 @@ namespace Flow.Launcher.Core.Plugin //Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable _startInfo.ArgumentList.Add("-B"); } - + + public override List LoadContextMenus(Result selectedResult) + { + throw new NotImplementedException(); + } + protected override Task ExecuteResultAsync(JsonRPCResult result) + { + throw new NotImplementedException(); + } public override async Task InitAsync(PluginInitContext context) { _startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath); diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index 7181ae225..642250627 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -34,6 +34,9 @@ namespace Flow.Launcher.Infrastructure.Storage public JsonStorage(string filePath) { FilePath = filePath; + DirectoryPath = Path.GetDirectoryName(filePath) ?? throw new ArgumentException("Invalid file path"); + + Helper.ValidateDirectory(DirectoryPath); } public async Task LoadAsync()