-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiddenLauncherManager.cs
More file actions
206 lines (162 loc) · 5.49 KB
/
HiddenLauncherManager.cs
File metadata and controls
206 lines (162 loc) · 5.49 KB
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
using System.IO;
namespace GameTaskPlugin
{
public class HiddenLauncherManager
{
private readonly Logger logger;
private readonly string createLauncherPath;
private readonly string deleteLauncherPath;
public HiddenLauncherManager(Logger logger, string pluginDataPath)
{
this.logger = logger;
string cacheFolder = Path.Combine(pluginDataPath, "Cache");
Directory.CreateDirectory(cacheFolder);
string createPs1Path = Path.Combine(cacheFolder, "CreateTasks.ps1");
string deletePs1Path = Path.Combine(cacheFolder, "DeleteTasks.ps1");
File.WriteAllText(createPs1Path, GetCreateTasksScript());
File.WriteAllText(deletePs1Path, GetDeleteTasksScript());
createLauncherPath = Path.Combine(cacheFolder, "HiddenCreateTasks.vbs");
deleteLauncherPath = Path.Combine(cacheFolder, "HiddenDeleteTasks.vbs");
File.WriteAllText(
createLauncherPath,
$@"Set shell = CreateObject(""WScript.Shell"")
shell.Run ""powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File """"{createPs1Path}"""" "", 0, False
");
File.WriteAllText(
deleteLauncherPath,
$@"Set shell = CreateObject(""WScript.Shell"")
shell.Run ""powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File """"{deletePs1Path}"""" "", 0, False
");
logger.Log("Hidden launchers and PowerShell helpers created.");
}
public string GetCreateLauncherPath()
{
return createLauncherPath;
}
public string GetDeleteLauncherPath()
{
return deleteLauncherPath;
}
private string GetCreateTasksScript()
{
return @"
$taskFolder = '\GameTask\'
$pendingFile = Join-Path $PSScriptRoot 'PendingTasks.txt'
$logFile = Join-Path $PSScriptRoot '..\Logs\PS1.log'
New-Item -ItemType Directory -Force -Path (Split-Path $logFile) | Out-Null
Add-Content $logFile ('`n===== CREATE START ' + (Get-Date) + ' =====`n')
if (!(Test-Path $pendingFile)) {
Add-Content $logFile 'No PendingTasks.txt found.'
exit
}
$service = New-Object -ComObject Schedule.Service
$service.Connect()
try {
$service.GetFolder($taskFolder) | Out-Null
}
catch {
$root = $service.GetFolder('\')
$root.CreateFolder('GameTask')
Add-Content $logFile 'Task Scheduler folder \GameTask created.'
}
$lines = [System.IO.File]::ReadAllLines(
$pendingFile,
[System.Text.Encoding]::UTF8
)
foreach ($line in $lines) {
if ([string]::IsNullOrWhiteSpace($line)) {
continue
}
$parts = $line.Split('|')
if ($parts.Count -lt 2) {
Add-Content $logFile ""SKIP invalid line: $line""
continue
}
$gameName = $parts[0].Trim()
$exePath = $parts[1].Trim()
if (!(Test-Path $exePath)) {
Add-Content $logFile ""SKIP exe not found: $gameName -> $exePath""
continue
}
$safeName = $gameName -replace '[^a-zA-Z0-9_\- ]', '_'
$taskName = 'GameTask_v1_' + $safeName
try {
Unregister-ScheduledTask `
-TaskName $taskName `
-TaskPath $taskFolder `
-Confirm:$false `
-ErrorAction SilentlyContinue
}
catch {}
try {
$exeDir = Split-Path $exePath -Parent
$action = New-ScheduledTaskAction `
-Execute $exePath `
-WorkingDirectory $exeDir
$principal = New-ScheduledTaskPrincipal `
-UserId $env:USERNAME `
-RunLevel Highest `
-LogonType Interactive
$settings = New-ScheduledTaskSettingsSet `
-StartWhenAvailable `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries
$task = New-ScheduledTask `
-Action $action `
-Principal $principal `
-Settings $settings
# Set priority to Normal (4) — default for scheduled tasks is Below Normal (7)
$task.Settings.Priority = 4
Register-ScheduledTask `
-TaskName $taskName `
-TaskPath $taskFolder `
-InputObject $task `
-Force
Add-Content $logFile ""CREATED: $taskName -> $exePath""
}
catch {
Add-Content $logFile ""ERROR creating task: $taskName -> $_""
}
}
Clear-Content $pendingFile
Add-Content $logFile ('===== CREATE END ' + (Get-Date) + ' =====`n')
";
}
private string GetDeleteTasksScript()
{
return @"
$taskFolder = '\GameTask\'
$deleteFile = Join-Path $PSScriptRoot 'DeleteTasks.txt'
$logFile = Join-Path $PSScriptRoot '..\Logs\PS1.log'
New-Item -ItemType Directory -Force -Path (Split-Path $logFile) | Out-Null
Add-Content $logFile ('`n===== DELETE START ' + (Get-Date) + ' =====`n')
if (!(Test-Path $deleteFile)) {
Add-Content $logFile 'No DeleteTasks.txt found.'
exit
}
$lines = [System.IO.File]::ReadAllLines(
$deleteFile,
[System.Text.Encoding]::UTF8
)
foreach ($taskName in $lines) {
if ([string]::IsNullOrWhiteSpace($taskName)) {
continue
}
try {
Unregister-ScheduledTask `
-TaskName $taskName `
-TaskPath $taskFolder `
-Confirm:$false `
-ErrorAction SilentlyContinue
Add-Content $logFile ""DELETED OR NOT FOUND: $taskName""
}
catch {
Add-Content $logFile ""ERROR deleting task: $taskName -> $_""
}
}
Clear-Content $deleteFile
Add-Content $logFile ('===== DELETE END ' + (Get-Date) + ' =====`n')
";
}
}
}