Skip to content

Commit ffcaa02

Browse files
committed
Use JsonLibrary to Parse object, and replace direct argument with the new api to allow auto escape
1 parent 04536c4 commit ffcaa02

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

Flow.Launcher.Core/Plugin/JsonPRCModel.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using System.Text.Json.Serialization;
1919
using Flow.Launcher.Plugin;
20+
using System.Text.Json;
2021

2122
namespace Flow.Launcher.Core.Plugin
2223
{
@@ -59,6 +60,8 @@ public class JsonRPCRequestModel : JsonRPCModelBase
5960

6061
public override string ToString()
6162
{
63+
return JsonSerializer.Serialize(this);
64+
6265
string rpc = string.Empty;
6366
if (Parameters != null && Parameters.Length > 0)
6467
{
@@ -78,9 +81,9 @@ private string GetParameterByType(object parameter)
7881
=> parameter switch
7982
{
8083
null => "null",
81-
string _ => $@"\""{ReplaceEscapes(parameter.ToString())}\""",
82-
bool _ => $@"{parameter.ToString().ToLower()}",
83-
_ => parameter.ToString()
84+
string p => $@"\""{ReplaceEscapes(p)}\""",
85+
bool p => $@"{p.ToString().ToLower()}",
86+
_ => $@"\""{ReplaceEscapes(parameter.ToString())}\"""
8487
};
8588

8689

@@ -97,11 +100,7 @@ private string ReplaceEscapes(string str)
97100
/// </summary>
98101
public class JsonRPCServerRequestModel : JsonRPCRequestModel
99102
{
100-
public override string ToString()
101-
{
102-
string rpc = base.ToString();
103-
return rpc + "}";
104-
}
103+
105104
}
106105

107106
/// <summary>
@@ -110,12 +109,6 @@ public override string ToString()
110109
public class JsonRPCClientRequestModel : JsonRPCRequestModel
111110
{
112111
public bool DontHideAfterAction { get; set; }
113-
114-
public override string ToString()
115-
{
116-
string rpc = base.ToString();
117-
return rpc + "}";
118-
}
119112
}
120113

121114
/// <summary>

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public List<Result> LoadContextMenus(Result selectedResult)
4646
}
4747

4848

49-
5049
private async Task<List<Result>> DeserializedResultAsync(Stream output)
5150
{
5251
if (output == Stream.Null) return null;
@@ -71,7 +70,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
7170
var results = new List<Result>();
7271
if (queryResponseModel.Result == null) return null;
7372

74-
if(!string.IsNullOrEmpty(queryResponseModel.DebugMessage))
73+
if (!string.IsNullOrEmpty(queryResponseModel.DebugMessage))
7574
{
7675
context.API.ShowMsg(queryResponseModel.DebugMessage);
7776
}
@@ -177,12 +176,11 @@ protected string Execute(ProcessStartInfo startInfo)
177176

178177
if (result.StartsWith("DEBUG:"))
179178
{
180-
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
179+
MessageBox.Show(new Form {TopMost = true}, result.Substring(6));
181180
return string.Empty;
182181
}
183182

184183
return result;
185-
186184
}
187185
catch (Exception e)
188186
{
@@ -248,10 +246,10 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
248246
}
249247
}
250248

251-
public Task InitAsync(PluginInitContext context)
249+
public virtual Task InitAsync(PluginInitContext context)
252250
{
253251
this.context = context;
254252
return Task.CompletedTask;
255253
}
256254
}
257-
}
255+
}

Flow.Launcher.Core/Plugin/PythonPlugin.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ public PythonPlugin(string filename)
2828
var path = Path.Combine(Constant.ProgramDirectory, JsonRPC);
2929
_startInfo.EnvironmentVariables["PYTHONPATH"] = path;
3030

31+
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
32+
_startInfo.ArgumentList.Add("-B");
3133
}
3234

3335
protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token)
3436
{
3537
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
3638
{
37-
Method = "query",
38-
Parameters = new object[] { query.Search },
39+
Method = "query", Parameters = new object[] {query.Search},
3940
};
40-
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
41-
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
41+
42+
_startInfo.ArgumentList[2] = request.ToString();
43+
4244
// todo happlebao why context can't be used in constructor
4345
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
4446

@@ -47,22 +49,31 @@ protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken
4749

4850
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
4951
{
50-
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
52+
_startInfo.ArgumentList[2] = rpcRequest.ToString();
5153
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
5254
// TODO: Async Action
5355
return Execute(_startInfo);
5456
}
5557

56-
protected override string ExecuteContextMenu(Result selectedResult) {
57-
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
58-
Method = "context_menu",
59-
Parameters = new object[] { selectedResult.ContextData },
58+
protected override string ExecuteContextMenu(Result selectedResult)
59+
{
60+
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
61+
{
62+
Method = "context_menu", Parameters = new object[] {selectedResult.ContextData},
6063
};
6164
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
6265
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
6366

6467
// TODO: Async Action
6568
return Execute(_startInfo);
6669
}
70+
71+
public override Task InitAsync(PluginInitContext context)
72+
{
73+
this.context = context;
74+
_startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);
75+
_startInfo.ArgumentList.Add("");
76+
return Task.CompletedTask;
77+
}
6778
}
6879
}

0 commit comments

Comments
 (0)