-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
209 lines (192 loc) · 6.76 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Plugin;
namespace AltV.PowerToysRun.Natives;
internal record Native(string Jhash, string? Comment, Dictionary<string, string>? Hashes, string AltName);
public class Main : IPlugin, IContextMenu, IDisposable
{
private Dictionary<string, Native> _natives = new();
private readonly HttpClient _httpClient = new();
private bool _initialized;
private int _initAttempt;
public async void Init(PluginInitContext context)
{
try
{
await InitAsync();
_initialized = true;
}
catch (Exception)
{
_initAttempt++;
if (_initAttempt > 5) return;
_ = Task.Delay(2000).ContinueWith(_ => Init(context));
}
}
private async Task InitAsync()
{
var nativesJsonMessage = await _httpClient.GetAsync("https://natives.altv.mp/natives");
var nativesJson = await nativesJsonMessage.Content.ReadAsStringAsync();
var namespaces = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Native>>>(
nativesJson,
new JsonSerializerOptions
{
IncludeFields = true,
PropertyNameCaseInsensitive = true
}
)!;
_natives = namespaces.Values.SelectMany(e => e).ToDictionary(e => e.Key, e => e.Value);
}
public string Name => "alt:V natives";
public string Description => "Searches alt:V native reference";
private readonly Regex _splitRegex = new(@"[ _]+");
public List<Result> Query(Query query)
{
if (!_initialized)
{
return new List<Result>
{
new()
{
Title = (_initAttempt == 6 ? "Plugin failed to initialize" : "Initializing plugin...") + " (attempt " + _initAttempt + ")",
SubTitle = "alt:V",
QueryTextDisplay = " "
}
};
}
var strs = _splitRegex
.Split(string.Join(" ", query.Terms))
.Select(e => e.Trim().ToLowerInvariant())
.Where(e => e.Length > 0).ToArray();
var results = new List<(Native native, string key, List<int> titleHighlight, List<int> subTitleHighlight)>();
var count = 0;
foreach (var (key, native) in _natives)
{
var titleHighlight = new List<int>();
var subTitleHighlight = new List<int>();
var found = 0;
foreach (var str in strs)
{
var nameIndex = native.AltName.ToLowerInvariant().IndexOf(str.ToLowerInvariant(), StringComparison.Ordinal);
if (nameIndex != -1)
{
found++;
titleHighlight.AddRange(Enumerable.Range(nameIndex, str.Length));
}
else if (native.Jhash.ToLowerInvariant() == str)
{
found++;
}
else if (native.Hashes != null)
{
foreach (var (_, hash) in native.Hashes)
{
if (hash.ToLowerInvariant() == str)
{
found++;
break;
}
}
}
if (found == strs.Length)
{
count++;
results.Add((native, key, titleHighlight, subTitleHighlight));
continue;
}
if (count >= 200)
{
break;
}
}
}
return results.Select(e => new Result
{
Title = e.native.AltName,
TitleHighlightData = e.titleHighlight,
SubTitle = e.native.Comment is not ("" or null) ? e.native.Comment : "alt:V native",
SubTitleHighlightData = e.subTitleHighlight,
ToolTipData = new ToolTipData("Native " + e.native.AltName, e.native.Comment is not ("" or null) ? e.native.Comment : "alt:V native"),
QueryTextDisplay = e.native.AltName,
ContextData = e,
IcoPath = "Images\\altv.green.png",
Action = _ =>
{
var link = "https://natives.altv.mp/#/" + e.key;
var psi = new ProcessStartInfo
{
FileName = link,
UseShellExecute = true
};
Process.Start(psi);
return false;
}
}).ToList();
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
var e = ((Native native, string key, List<int> titleHighlight, List<int> subTitleHighlight)) selectedResult.ContextData;
var link = "https://natives.altv.mp/#/" + e.key;
return new List<ContextMenuResult>
{
new()
{
Title = "Copy link (Ctrl + Enter)",
Action = _ => Copy(link),
Glyph = "\uE71B",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Return,
AcceleratorModifiers = ModifierKeys.Control
},
new()
{
Title = "Copy name (Shift + Enter)",
Action = _ => Copy(e.native.AltName),
Glyph = "\uE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Return,
AcceleratorModifiers = ModifierKeys.Shift
},
new()
{
Title = "Copy capitalized name (Ctrl + Shift + Enter)",
Action = _ => Copy(e.native.AltName[0].ToString().ToUpperInvariant() + e.native.AltName[1..]),
Glyph = "\uE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Return,
AcceleratorModifiers = ModifierKeys.Shift | ModifierKeys.Control,
}
};
}
private bool Copy(string text)
{
var ret = false;
var thread = new Thread(_ =>
{
try
{
Clipboard.SetText(text);
ret = true;
}
catch (ExternalException)
{
MessageBox.Show("Failed to copy text");
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return ret;
}
public void Dispose()
{
}
}