-
Notifications
You must be signed in to change notification settings - Fork 346
/
Watcher.cs
131 lines (101 loc) · 4.04 KB
/
Watcher.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
using Bloxstrap.AppData;
using Bloxstrap.Integrations;
using Bloxstrap.Models;
namespace Bloxstrap
{
public class Watcher : IDisposable
{
private readonly InterProcessLock _lock = new("Watcher");
private readonly WatcherData? _watcherData;
private readonly NotifyIconWrapper? _notifyIcon;
public readonly ActivityWatcher? ActivityWatcher;
public readonly DiscordRichPresence? RichPresence;
public Watcher()
{
const string LOG_IDENT = "Watcher";
if (!_lock.IsAcquired)
{
App.Logger.WriteLine(LOG_IDENT, "Watcher instance already exists");
return;
}
string? watcherDataArg = App.LaunchSettings.WatcherFlag.Data;
#if DEBUG
if (String.IsNullOrEmpty(watcherDataArg))
{
string path = new RobloxPlayerData().ExecutablePath;
using var gameClientProcess = Process.Start(path);
_watcherData = new() { ProcessId = gameClientProcess.Id };
}
#else
if (String.IsNullOrEmpty(watcherDataArg))
throw new Exception("Watcher data not specified");
#endif
if (!String.IsNullOrEmpty(watcherDataArg))
_watcherData = JsonSerializer.Deserialize<WatcherData>(Encoding.UTF8.GetString(Convert.FromBase64String(watcherDataArg)));
if (_watcherData is null)
throw new Exception("Watcher data is invalid");
if (App.Settings.Prop.EnableActivityTracking)
{
ActivityWatcher = new(_watcherData.LogFile);
if (App.Settings.Prop.UseDisableAppPatch)
{
ActivityWatcher.OnAppClose += delegate
{
App.Logger.WriteLine(LOG_IDENT, "Received desktop app exit, closing Roblox");
using var process = Process.GetProcessById(_watcherData.ProcessId);
process.CloseMainWindow();
};
}
if (App.Settings.Prop.UseDiscordRichPresence)
RichPresence = new(ActivityWatcher);
}
_notifyIcon = new(this);
}
public void KillRobloxProcess() => CloseProcess(_watcherData!.ProcessId, true);
public void CloseProcess(int pid, bool force = false)
{
const string LOG_IDENT = "Watcher::CloseProcess";
try
{
using var process = Process.GetProcessById(pid);
App.Logger.WriteLine(LOG_IDENT, $"Killing process '{process.ProcessName}' (pid={pid}, force={force})");
if (process.HasExited)
{
App.Logger.WriteLine(LOG_IDENT, $"PID {pid} has already exited");
return;
}
if (force)
process.Kill();
else
process.CloseMainWindow();
}
catch (Exception ex)
{
App.Logger.WriteLine(LOG_IDENT, $"PID {pid} could not be closed");
App.Logger.WriteException(LOG_IDENT, ex);
}
}
public async Task Run()
{
if (!_lock.IsAcquired || _watcherData is null)
return;
ActivityWatcher?.Start();
while (Utilities.GetProcessesSafe().Any(x => x.Id == _watcherData.ProcessId))
await Task.Delay(1000);
if (_watcherData.AutoclosePids is not null)
{
foreach (int pid in _watcherData.AutoclosePids)
CloseProcess(pid);
}
if (App.LaunchSettings.TestModeFlag.Active)
Process.Start(Paths.Process, "-settings -testmode");
}
public void Dispose()
{
App.Logger.WriteLine("Watcher::Dispose", "Disposing Watcher");
_notifyIcon?.Dispose();
RichPresence?.Dispose();
GC.SuppressFinalize(this);
}
}
}