-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
188 lines (156 loc) · 6.56 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace BorderlessMinecraft
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Context());
}
}
internal class Context : ApplicationContext
{
public static bool autoborderless;
private static bool _autostart;
private readonly NotifyIcon _trayIcon;
private readonly WindowMonitor _windowMonitor;
public static KeyboardHook _keyboardHook;
public static CancellationTokenSource cts = new CancellationTokenSource();
public Context()
{
// Create Tray Icon context menu and it's items
ContextMenuStrip contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Autoborderless", null, OnAutoborderless);
contextMenu.Items.Add("Autostart", null, OnAutostart);
contextMenu.Items.Add("Exit", null, OnExit);
// Item settings and set state (bool defaults to false)
if (contextMenu.Items[0] is ToolStripMenuItem item0)
{
item0.CheckOnClick = true;
item0.Checked = IsAutoborderlessEnabled();
}
if (contextMenu.Items[1] is ToolStripMenuItem item1)
{
item1.CheckOnClick = true;
item1.Checked = IsAutostartEnabled();
}
// Create Tray Icon
_trayIcon = new NotifyIcon()
{
Text = "BorderlessMinecraft",
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath),
ContextMenuStrip = contextMenu,
Visible = true,
};
// Setup KeyboardHook
_keyboardHook = new KeyboardHook();
// Start in WindowMonitor, Stop in Cleaner (and Exit ofc)
// Setup Window Monitor
_windowMonitor = new WindowMonitor();
_windowMonitor.Start();
// Start Cleaner
CleanProcesses();
}
// Action when clicking 'Autoborderless'
private void OnAutoborderless(Object sender, EventArgs e)
{
autoborderless = !autoborderless;
if (autoborderless)
Registry.CurrentUser.CreateSubKey(@"Software\AutoBorderlessMinecraft");
else
Registry.CurrentUser.DeleteSubKey(@"Software\AutoBorderlessMinecraft", false);
}
// Action when clicking 'Autostart'
private void OnAutostart(Object sender, EventArgs e)
{
_autostart = !_autostart;
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
{
if (_autostart)
key.SetValue("BorderlessMinecraft", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BorderlessMinecraft.exe"));
else
key.DeleteValue("BorderlessMinecraft", false);
}
}
// Action when clicking 'Exit'
private void OnExit(Object sender, EventArgs e)
{
_trayIcon.Visible = false;
cts.Cancel();
_windowMonitor.Stop();
_keyboardHook.RemoveHook();
Application.Exit();
}
// Check if 'Autoborderless' is enabled
private bool IsAutoborderlessEnabled()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\AutoBorderlessMinecraft", false))
{
autoborderless = key != null;
return key != null;
}
}
// Check if 'Autostart' is enabled
private bool IsAutostartEnabled()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", false))
{
String currentPath = key?.GetValue("BorderlessMinecraft") as string;
if (currentPath == null || !currentPath.Equals(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BorderlessMinecraft.exe")))
return false;
_autostart = true;
return true;
}
}
// Cleaner for PIDs got in WindowMonitor and WindowManager (the Token stuff makes it cancellable when the program exits)
private static void CleanProcesses()
{
Task.Run(() =>
{
while (!cts.Token.IsCancellationRequested)
{
while (WindowMonitor.processesDetected.Count > 0 && !cts.Token.IsCancellationRequested)
{
List<int> processesTerminated = new List<int>();
foreach (int pid in WindowMonitor.processesDetected)
{
try
{
Process.GetProcessById(pid);
}
catch (ArgumentException)
{
processesTerminated.Add(pid);
}
catch (Exception e)
{
Console.WriteLine($"Error checking PID {pid}: {e.Message}");
}
}
foreach (int pid in processesTerminated)
{
WindowMonitor.processesDetected.Remove(pid);
WindowManager.windowPropertiesByPID.Remove(pid);
}
// Remove KeyboardHook if no minecraft process is left
if (WindowMonitor.processesDetected.Count == 0)
_keyboardHook.RemoveHook();
cts.Token.WaitHandle.WaitOne(10000);
}
cts.Token.WaitHandle.WaitOne(10000);
}
}, cts.Token);
}
}
}