Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions Flow.Launcher.Core/Plugin/JsonPRCModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Flow Launcher and other plugins,
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Flow Launcher and other plugins,
* like python or other self-execute program. But, we added addtional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
Expand Down Expand Up @@ -62,37 +61,6 @@ public class JsonRPCRequestModel : JsonRPCModelBase
public override string ToString()
{
return JsonSerializer.Serialize(this);

string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0)
{
string parameters = $"[{string.Join(',', Parameters.Select(GetParameterByType))}]";
rpc = $@"{{\""method\"":\""{Method}\"",\""parameters\"":{parameters}";
}
else
{
rpc = $@"{{\""method\"":\""{Method}\"",\""parameters\"":[]";
}

return rpc;

}

private string GetParameterByType(object parameter)
=> parameter switch
{
null => "null",
string p => $@"\""{ReplaceEscapes(p)}\""",
bool p => $@"{p.ToString().ToLower()}",
_ => $@"\""{ReplaceEscapes(parameter.ToString())}\"""
};


private string ReplaceEscapes(string str)
{
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client
.Replace(@"""", @"\\""""");
}
}

Expand All @@ -101,7 +69,7 @@ private string ReplaceEscapes(string str)
/// </summary>
public class JsonRPCServerRequestModel : JsonRPCRequestModel
{

}

/// <summary>
Expand All @@ -122,4 +90,4 @@ public class JsonRPCResult : Result
{
public JsonRPCClientRequestModel JsonRPCAction { get; set; }
}
}
}
51 changes: 32 additions & 19 deletions Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ public List<Result> LoadContextMenus(Result selectedResult)
}
}

private static readonly JsonSerializerOptions _options = new() {Converters = {new JsonObjectConverter()}};
private static readonly JsonSerializerOptions _options = new()
{
Converters =
{
new JsonObjectConverter()
}
};

private async Task<List<Result>> DeserializedResultAsync(Stream output)
{
Expand Down Expand Up @@ -84,27 +90,31 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
{
if (result.JsonRPCAction == null) return false;

if (!string.IsNullOrEmpty(result.JsonRPCAction.Method))
if (string.IsNullOrEmpty(result.JsonRPCAction.Method))
{
return !result.JsonRPCAction.DontHideAfterAction;
}

if (result.JsonRPCAction.Method.StartsWith("Flow.Launcher."))
{
if (result.JsonRPCAction.Method.StartsWith("Flow.Launcher."))
ExecuteFlowLauncherAPI(result.JsonRPCAction.Method["Flow.Launcher.".Length..],
result.JsonRPCAction.Parameters);
}
else
{
var actionResponse = ExecuteCallback(result.JsonRPCAction);

if (string.IsNullOrEmpty(actionResponse))
{
ExecuteFlowLauncherAPI(result.JsonRPCAction.Method[14..],
result.JsonRPCAction.Parameters);
return !result.JsonRPCAction.DontHideAfterAction;
}
else

var jsonRpcRequestModel = JsonSerializer.Deserialize<JsonRPCRequestModel>(actionResponse, _options);

if (jsonRpcRequestModel?.Method?.StartsWith("Flow.Launcher.") ?? false)
{
string actionReponse = ExecuteCallback(result.JsonRPCAction);
if (string.IsNullOrEmpty(actionReponse))
return false;
JsonRPCRequestModel jsonRpcRequestModel =
JsonSerializer.Deserialize<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !string.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Flow.Launcher."))
{
ExecuteFlowLauncherAPI(jsonRpcRequestModel.Method.Substring(4),
jsonRpcRequestModel.Parameters);
}
ExecuteFlowLauncherAPI(jsonRpcRequestModel.Method["Flow.Launcher.".Length..],
jsonRpcRequestModel.Parameters);
}
}

Expand Down Expand Up @@ -181,7 +191,10 @@ 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;
}

Expand Down