diff --git a/Flow.Launcher.Core/Plugin/JsonPRCModel.cs b/Flow.Launcher.Core/Plugin/JsonPRCModel.cs index 319f760d192..27734c7f781 100644 --- a/Flow.Launcher.Core/Plugin/JsonPRCModel.cs +++ b/Flow.Launcher.Core/Plugin/JsonPRCModel.cs @@ -17,6 +17,7 @@ using System.Linq; using System.Text.Json.Serialization; using Flow.Launcher.Plugin; +using System.Text.Json; namespace Flow.Launcher.Core.Plugin { @@ -59,6 +60,8 @@ public class JsonRPCRequestModel : JsonRPCModelBase public override string ToString() { + return JsonSerializer.Serialize(this); + string rpc = string.Empty; if (Parameters != null && Parameters.Length > 0) { @@ -78,9 +81,9 @@ private string GetParameterByType(object parameter) => parameter switch { null => "null", - string _ => $@"\""{ReplaceEscapes(parameter.ToString())}\""", - bool _ => $@"{parameter.ToString().ToLower()}", - _ => parameter.ToString() + string p => $@"\""{ReplaceEscapes(p)}\""", + bool p => $@"{p.ToString().ToLower()}", + _ => $@"\""{ReplaceEscapes(parameter.ToString())}\""" }; @@ -97,11 +100,7 @@ private string ReplaceEscapes(string str) /// public class JsonRPCServerRequestModel : JsonRPCRequestModel { - public override string ToString() - { - string rpc = base.ToString(); - return rpc + "}"; - } + } /// @@ -110,12 +109,6 @@ public override string ToString() public class JsonRPCClientRequestModel : JsonRPCRequestModel { public bool DontHideAfterAction { get; set; } - - public override string ToString() - { - string rpc = base.ToString(); - return rpc + "}"; - } } /// diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 7a088bd091d..11d1abb6328 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -46,7 +46,6 @@ public List LoadContextMenus(Result selectedResult) } - private async Task> DeserializedResultAsync(Stream output) { if (output == Stream.Null) return null; @@ -71,7 +70,7 @@ private List ParseResults(JsonRPCQueryResponseModel queryResponseModel) var results = new List(); if (queryResponseModel.Result == null) return null; - if(!string.IsNullOrEmpty(queryResponseModel.DebugMessage)) + if (!string.IsNullOrEmpty(queryResponseModel.DebugMessage)) { context.API.ShowMsg(queryResponseModel.DebugMessage); } @@ -92,6 +91,8 @@ private List ParseResults(JsonRPCQueryResponseModel queryResponseModel) else { string actionReponse = ExecuteCallback(result.JsonRPCAction); + if (string.IsNullOrEmpty(actionReponse)) + return false; JsonRPCRequestModel jsonRpcRequestModel = JsonSerializer.Deserialize(actionReponse); if (jsonRpcRequestModel != null @@ -177,12 +178,11 @@ protected string Execute(ProcessStartInfo startInfo) if (result.StartsWith("DEBUG:")) { - MessageBox.Show(new Form { TopMost = true }, result.Substring(6)); + MessageBox.Show(new Form {TopMost = true}, result.Substring(6)); return string.Empty; } return result; - } catch (Exception e) { @@ -248,10 +248,10 @@ public async Task> QueryAsync(Query query, CancellationToken token) } } - public Task InitAsync(PluginInitContext context) + public virtual Task InitAsync(PluginInitContext context) { this.context = context; return Task.CompletedTask; } } -} +} \ No newline at end of file diff --git a/Flow.Launcher.Core/Plugin/PythonPlugin.cs b/Flow.Launcher.Core/Plugin/PythonPlugin.cs index 356a68a81b1..16d84136fae 100644 --- a/Flow.Launcher.Core/Plugin/PythonPlugin.cs +++ b/Flow.Launcher.Core/Plugin/PythonPlugin.cs @@ -28,17 +28,19 @@ public PythonPlugin(string filename) var path = Path.Combine(Constant.ProgramDirectory, JsonRPC); _startInfo.EnvironmentVariables["PYTHONPATH"] = path; + //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"); } protected override Task ExecuteQueryAsync(Query query, CancellationToken token) { JsonRPCServerRequestModel request = new JsonRPCServerRequestModel { - Method = "query", - Parameters = new object[] { query.Search }, + Method = "query", Parameters = new object[] {query.Search}, }; - //Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable - _startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\""; + + _startInfo.ArgumentList[2] = request.ToString(); + // todo happlebao why context can't be used in constructor _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; @@ -47,16 +49,17 @@ protected override Task ExecuteQueryAsync(Query query, CancellationToken protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest) { - _startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\""; + _startInfo.ArgumentList[2] = rpcRequest.ToString(); _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; // TODO: Async Action return Execute(_startInfo); } - protected override string ExecuteContextMenu(Result selectedResult) { - JsonRPCServerRequestModel request = new JsonRPCServerRequestModel { - Method = "context_menu", - Parameters = new object[] { selectedResult.ContextData }, + protected override string ExecuteContextMenu(Result selectedResult) + { + JsonRPCServerRequestModel request = new JsonRPCServerRequestModel + { + Method = "context_menu", Parameters = new object[] {selectedResult.ContextData}, }; _startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\""; _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; @@ -64,5 +67,13 @@ protected override string ExecuteContextMenu(Result selectedResult) { // TODO: Async Action return Execute(_startInfo); } + + public override Task InitAsync(PluginInitContext context) + { + this.context = context; + _startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath); + _startInfo.ArgumentList.Add(""); + return Task.CompletedTask; + } } } \ No newline at end of file