-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
frmMain.cs
296 lines (262 loc) · 12.5 KB
/
frmMain.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using RePlays.Recorders;
using RePlays.Services;
using Squirrel;
using RePlays.Utils;
using static RePlays.Utils.Functions;
using System.Collections.Generic;
using System.IO;
using System;
namespace RePlays {
public partial class frmMain : Form {
public static Microsoft.Web.WebView2.WinForms.WebView2 webView2;
public ContextMenuStrip recentLinksMenu;
public static frmMain Instance;
public frmMain() {
Instance = this;
SettingsService.LoadSettings();
SettingsService.SaveSettings();
StorageService.ManageStorage();
HotkeyService.Start();
InitializeComponent();
PurgeTempVideos();
Updater.CheckForUpdates();
notifyIcon1.Icon = this.Icon;
}
private void frmMain_Load(object sender, System.EventArgs e) {
recentLinksMenu = new();
recentLinksMenu.Items.Add("Left click to copy to clipboard. Right click to open URL.").Enabled = false;
if (SettingsService.Settings.generalSettings.startMinimized) {
this.Size = new Size(1080, 600);
this.FormBorderStyle = FormBorderStyle.Sizable;
CenterToScreen();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
firstTime = false;
}
else {
InitializeWebView2();
}
}
private void RefreshLoader() {
pictureBox1.Size = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);
pictureBox1.Location = new Point((pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Image.Width / 2),
(pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Image.Height / 2));
pictureBox1.Refresh();
}
bool firstLaunch = true;
private async void InitializeWebView2() {
RefreshLoader();
try {
if(firstLaunch) {
Logger.WriteLine("WebView2 Version: " + CoreWebView2Environment.GetAvailableBrowserVersionString());
firstLaunch = false;
}
}
catch (WebView2RuntimeNotFoundException exception) {
Logger.WriteLine(exception.Message);
Logger.WriteLine("Prompting user to install WebView2");
DialogResult result =
MessageBox.Show(
"Microsoft Edge WebView2 Runtime is required to display interface. Would you like to run the installer?",
"Missing Microsoft Edge WebView2 Runtime", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes) {
var startInfo = new ProcessStartInfo {
#if (DEBUG)
FileName = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"/Resources/runtimes/win/MicrosoftEdgeWebview2Setup.exe",
#elif (RELEASE)
FileName = Application.StartupPath + @"/runtimes/win/MicrosoftEdgeWebview2Setup.exe",
#endif
Arguments = "/install"
};
var process = Process.Start(startInfo);
process.WaitForExit();
Logger.WriteLine("MicrosoftEdgeWebview2Setup.exe exited with code: " + process.ExitCode.ToString());
}
}
if (webView2 == null || webView2.IsDisposed) {
webView2 = new Microsoft.Web.WebView2.WinForms.WebView2();
webView2.Dock = DockStyle.Fill;
webView2.CoreWebView2InitializationCompleted += CoreWebView2InitializationCompleted;
webView2.WebMessageReceived += WebMessageReceivedAsync;
CoreWebView2EnvironmentOptions environmentOptions = new CoreWebView2EnvironmentOptions() {
AdditionalBrowserArguments = "--unlimited-storage --disable-web-security --allow-file-access-from-files --allow-file-access"
};
CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(null, null, environmentOptions);
await webView2.EnsureCoreWebView2Async(environment);
}
}
private void DisposeWebView2() {
if (webView2 != null && !webView2.IsDisposed) {
webView2.CoreWebView2.PermissionRequested -= CoreWebView2PermissionRequested;
webView2.CoreWebView2InitializationCompleted -= CoreWebView2InitializationCompleted;
webView2.WebMessageReceived -= WebMessageReceivedAsync;
webView2.Dispose();
webView2 = null;
}
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
}
private async void CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) {
await webView2.CoreWebView2.CallDevToolsProtocolMethodAsync("Security.setIgnoreCertificateErrors", "{\"ignore\": true}");
webView2.CoreWebView2.Settings.IsStatusBarEnabled = false;
webView2.CoreWebView2.Settings.IsWebMessageEnabled = true;
webView2.CoreWebView2.PermissionRequested += CoreWebView2PermissionRequested;
#if (DEBUG)
webView2.CoreWebView2.SetVirtualHostNameToFolderMapping("videos.replays.app", GetPlaysFolder(), CoreWebView2HostResourceAccessKind.Allow);
webView2.CoreWebView2.SetVirtualHostNameToFolderMapping("replays.app", Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + "/ClientApp/public/", CoreWebView2HostResourceAccessKind.Allow);
#elif (RELEASE)
webView2.CoreWebView2.SetVirtualHostNameToFolderMapping("replays.app", Application.StartupPath + "/ClientApp/build/", CoreWebView2HostResourceAccessKind.Allow);
webView2.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
#endif
webView2.CoreWebView2.Navigate("https://replays.app/preload.html");
}
private void CoreWebView2PermissionRequested(object sender, CoreWebView2PermissionRequestedEventArgs e) {
e.State = CoreWebView2PermissionState.Allow;
}
public static void PostWebMessageAsJson(string message) {
if (webView2 == null || webView2.IsDisposed == true) return;
if (webView2.InvokeRequired) {
// Call this same method but make sure it is on UI thread
webView2.BeginInvoke((MethodInvoker)delegate {
PostWebMessageAsJson(message);
});
}
else
if(webView2 != null && webView2.CoreWebView2 != null)
webView2.CoreWebView2.PostWebMessageAsJson(message);
}
bool firstTime = true;
private async void WebMessageReceivedAsync(object sender, CoreWebView2WebMessageReceivedEventArgs e) {
var webMessage = await WebMessage.RecieveMessage(e.WebMessageAsJson);
if (!this.Controls.Contains(webView2) && webMessage.message == "Initialize") {
this.Controls.Add(webView2);
webView2.BringToFront();
if (firstTime) {
this.Size = new Size(1080, 600);
this.FormBorderStyle = FormBorderStyle.Sizable;
CenterToScreen();
firstTime = false;
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
DisposeWebView2();
}
}
FormWindowState _PreviousWindowState;
private void frmMain_Resize(object sender, System.EventArgs e) {
RefreshLoader();
if (this.WindowState != FormWindowState.Minimized)
_PreviousWindowState = WindowState;
if (this.WindowState != FormWindowState.Minimized) {
if(!firstTime) InitializeWebView2();
this.ShowInTaskbar = true;
}
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) {
this.Activate();
if (this.WindowState == FormWindowState.Minimized) {
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = _PreviousWindowState;
}
}
private void exitToolStripMenuItem_Click(object sender, System.EventArgs e) {
notifyIcon1.Visible = false;
this.Close();
Application.Exit();
}
private void checkForUpdatesToolStripMenuItem_Click(object sender, System.EventArgs e) {
Updater.CheckForUpdates();
}
bool moving;
Point offset;
Point original;
private void frmMain_MouseDown(object sender, MouseEventArgs e) {
moving = true;
this.Capture = true;
offset = MousePosition;
original = this.Location;
}
private void frmMain_MouseMove(object sender, MouseEventArgs e) {
if (!moving)
return;
int x = original.X + MousePosition.X - offset.X;
int y = original.Y + MousePosition.Y - offset.Y;
this.Location = new Point(x, y);
}
private void frmMain_MouseUp(object sender, MouseEventArgs e) {
moving = false;
this.Capture = false;
}
private void recentLinks_ToolStripMenuItem_DropDownOpening(object sender, System.EventArgs e) {
PopulateRecentLinks(recentLinksToolStripMenuItem.DropDown.Items);
}
public void PopulateRecentLinks(ToolStripItemCollection itemCollection=null) {
if (itemCollection == null) {
PopulateRecentLinks(recentLinksMenu.Items);
recentLinksMenu.Show(Cursor.Position);
return;
}
for (int i = 1; i < itemCollection.Count; i++) {
itemCollection.RemoveAt(i);
}
foreach (var url in SettingsService.Settings.uploadSettings.recentLinks) {
itemCollection.Add(url).MouseDown += (s, e) => {
if (e.Button == MouseButtons.Right) {
Process.Start(new ProcessStartInfo(url.Split("] ")[1]) { UseShellExecute = true });
}
else if (e.Button == MouseButtons.Left) {
Clipboard.SetText(url.Split("] ")[1]);
}
};
}
}
public void DisplayNotification(string title, string context) {
notifyIcon1.BalloonTipText = context;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = title;
notifyIcon1.ShowBalloonTip(500);
}
public void HideRecentLinks() {
recentLinksMenu.Hide();
}
private List<Keys> PressedKeys = new();
private void button1_KeyDown(object sender, KeyEventArgs e) {
Keys k = e.KeyCode;
if (k.ToString().Contains(e.Modifiers.ToString())) k = e.Modifiers;
if (!PressedKeys.Contains(k)) {
PressedKeys.Add(k);
}
PressedKeys.Sort();
PressedKeys.Reverse();
Logger.WriteLine(string.Join(" | ", PressedKeys.ToArray()));
}
private void button1_KeyUp(object sender, KeyEventArgs e) {
if(HotkeyService.EditId != null && SettingsService.Settings.keybindings.ContainsKey(HotkeyService.EditId)) {
SettingsService.Settings.keybindings[HotkeyService.EditId] = string.Join(" | ", PressedKeys.ToArray()).Split(" | ");
SettingsService.SaveSettings();
WebMessage.SendMessage(GetUserSettings());
HotkeyService.Start();
}
if (webView2 != null) webView2.Focus();
else pictureBox1.Focus();
PressedKeys.Clear();
}
public void EditKeybind(string id) {
HotkeyService.EditId = id;
HotkeyService.Stop();
//button1 is a hacky way of logging keypresses
this.button1.Focus();
}
}
}