This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
100 lines (89 loc) · 3.89 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;
using System.Collections.Generic;
using System.Linq;
namespace Wox.Plugin.mpv
{
public class Main : IPlugin
{
Dictionary<string, string> mpvDic = new Dictionary<string, string>();
public void Init(PluginInitContext context) => Populate();
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
foreach (var pair in mpvDic)
{
if (pair.Key.ToLower().Contains(query.Search) ||
pair.Value.ToLower().Contains(query.Search))
{
results.Add(new Result()
{
Title = pair.Value,
SubTitle = pair.Key,
IcoPath = "mpv.ico",
Action = e =>
{
using (var pipe = new NamedPipeClientStream(
".", "mpvsocket", PipeDirection.Out,
PipeOptions.Asynchronous,
TokenImpersonationLevel.Anonymous))
{
pipe.Connect(2000);
StreamWriter pipeOut = new StreamWriter(pipe) { AutoFlush = true };
pipeOut.WriteAsync(pair.Key + "\n");
}
return true;
}
});
}
}
return results;
}
void Populate()
{
string inputConfPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\mpv\\input.conf";
if (!File.Exists(inputConfPath)) return;
string content = File.ReadAllText(inputConfPath);
List<string> lines = null;
Dictionary<string, string> commandInputDic = new Dictionary<string, string>();
if (content.Contains("#menu:"))
lines = content.Split("\r\n".ToCharArray()).ToList();
else
{
lines = Properties.Resources.input_conf.Split("\r\n".ToCharArray()).ToList();
foreach (string i in content.Split("\r\n".ToCharArray()))
{
string line = i.Trim();
if (line.StartsWith("#") || !line.Contains(" ")) continue;
string input = line.Substring(0, line.IndexOf(" ")).Trim();
string command = line.Substring(line.IndexOf(" ") + 1).Trim();
commandInputDic[command] = input;
}
}
foreach (string i in lines)
{
if (!i.Contains("#menu:")) continue;
string left = i.Substring(0, i.IndexOf("#menu:")).Trim();
if (left.StartsWith("#")) continue;
string command = left.Substring(left.IndexOf(" ") + 1).Trim();
if (command == "ignore") continue;
string menu = i.Substring(i.IndexOf("#menu:") + "#menu:".Length).Trim();
string input = left.Substring(0, left.IndexOf(" "));
if (input == "_") input = "";
if (menu.Contains(";")) input = menu.Substring(0, menu.IndexOf(";")).Trim();
string path = menu.Substring(menu.IndexOf(";") + 1).Trim().Replace("&", "&&").Replace("...", "");
if (path == "" || command == "") continue;
if (commandInputDic.Count > 0)
if (commandInputDic.ContainsKey(command))
input = commandInputDic[command];
else
input = "";
if (!string.IsNullOrEmpty(input))
path += $" ({input})";
mpvDic[command] = path;
}
}
}
}