Repository navigation
Expand file tree
/
Copy pathGameBoostGUI.ps1
More file actions
348 lines (310 loc) · 14.5 KB
/
Copy pathGameBoostGUI.ps1
File metadata and controls
348 lines (310 loc) · 14.5 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#Requires -Version 5.1
# ============================================================
# GameBoostGUI.ps1 - GameBoost 可视化客户端(主窗口)
# 显示:系统实时状态 / 优化项明细(具体调了什么) / 操作日志
# 功能:一键优化 / 体检 / 监控 / 恢复 / 游戏联动 / 开机自启
# 启动: 双击 gui_launch.vbs 或 GameBoostGUI.bat
# ============================================================
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$script:Root = Split-Path -Parent $MyInvocation.MyCommand.Path
# ---------- 加载模块与配置 ----------
Get-ChildItem (Join-Path $script:Root "modules") -Filter "*.ps1" | ForEach-Object { . $_.FullName }
$script:ConfigPath = Join-Path $script:Root "config.json"
$script:Config = Get-GBConfig $script:ConfigPath
Write-GBLog "INFO" "GUI 启动开始 (管理员: $(Test-GBAdmin))"
# ---------- 管理员提权(杀 GameViewerService 需要) ----------
if ($env:GB_ISADMIN -ne "1") {
$env:GB_ISADMIN = "1"
try {
Start-Process -FilePath "powershell.exe" -Verb RunAs -ArgumentList @(
"-NoProfile", "-ExecutionPolicy", "Bypass", "-WindowStyle", "Hidden",
"-File", "`"$PSCommandPath`""
) -ErrorAction Stop
Write-GBLog "INFO" "已请求管理员提权"
exit 0
} catch {
Write-GBLog "WARN" "未获得管理员权限:GameViewer 服务联动将受限"
}
}
# ---------- 开机自启 ----------
function Test-GBAutoStart { return [bool](Get-ScheduledTask -TaskName "GameBoostClient" -ErrorAction SilentlyContinue) }
function Set-GBAutoStart {
$ps = Join-Path $script:Root "GameBoostClient.ps1"
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument ('-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "' + $ps + '"')
$trigger = New-ScheduledTaskTrigger -AtLogOn
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero)
Register-ScheduledTask -TaskName "GameBoostClient" -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -Force | Out-Null
Append-GBLog "已设置开机自启(任务计划,最高权限)"
}
function Remove-GBAutoStart {
Unregister-ScheduledTask -TaskName "GameBoostClient" -Confirm:$false -ErrorAction SilentlyContinue
Append-GBLog "已取消开机自启"
}
# ============================================================
# UI 构建
# ============================================================
$form = New-Object System.Windows.Forms.Form
$form.Text = "GameBoost 可视化客户端"
$form.Size = New-Object System.Drawing.Size(780, 700)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.BackColor = [System.Drawing.Color]::FromArgb(246, 248, 251)
# 标题
$lblTitle = New-Object System.Windows.Forms.Label
$lblTitle.Text = "GameBoost 竞技游戏优化客户端"
$lblTitle.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 15, [System.Drawing.FontStyle]::Bold)
$lblTitle.ForeColor = [System.Drawing.Color]::FromArgb(30, 60, 110)
$lblTitle.Location = New-Object System.Drawing.Point(16, 12)
$lblTitle.Size = New-Object System.Drawing.Size(420, 30)
$form.Controls.Add($lblTitle)
# 按钮行
function New-GBButton {
param([string]$Text, [int]$X, [int]$Y, [int]$W = 116, [int]$H = 34)
$b = New-Object System.Windows.Forms.Button
$b.Text = $Text
$b.Location = New-Object System.Drawing.Point($X, $Y)
$b.Size = New-Object System.Drawing.Size($W, $H)
$b.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10)
$b.FlatStyle = [System.Windows.Forms.FlatStyle]::Standard
$b.BackColor = [System.Drawing.Color]::FromArgb(224, 233, 245)
return $b
}
$btnOptimize = New-GBButton "一键优化" 16 50
$btnDiagnose = New-GBButton "系统体检" 140 50
$btnMonitor = New-GBButton "实时监控" 264 50
$btnRestore = New-GBButton "一键恢复" 388 50
$btnRefresh = New-GBButton "刷新状态" 512 50
$form.Controls.Add($btnOptimize)
$form.Controls.Add($btnDiagnose)
$form.Controls.Add($btnMonitor)
$form.Controls.Add($btnRestore)
$form.Controls.Add($btnRefresh)
# 勾选行
$cbAware = New-Object System.Windows.Forms.CheckBox
$cbAware.Text = "游戏联动(开游戏自动暂停远控)"
$cbAware.Location = New-Object System.Drawing.Point(16, 92)
$cbAware.Size = New-Object System.Drawing.Size(240, 22)
$cbAware.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 9.5)
$cbAware.Checked = $script:Config.gameAware.enable
$form.Controls.Add($cbAware)
$cbAutoStart = New-Object System.Windows.Forms.CheckBox
$cbAutoStart.Text = "开机自启"
$cbAutoStart.Location = New-Object System.Drawing.Point(268, 92)
$cbAutoStart.Size = New-Object System.Drawing.Size(160, 22)
$cbAutoStart.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 9.5)
$cbAutoStart.Checked = Test-GBAutoStart
$form.Controls.Add($cbAutoStart)
# 状态面板
$panelStatus = New-Object System.Windows.Forms.Panel
$panelStatus.Location = New-Object System.Drawing.Point(16, 122)
$panelStatus.Size = New-Object System.Drawing.Size(744, 74)
$panelStatus.BackColor = [System.Drawing.Color]::FromArgb(255, 255, 255)
$panelStatus.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
$form.Controls.Add($panelStatus)
$lblSys = New-Object System.Windows.Forms.Label
$lblSys.Location = New-Object System.Drawing.Point(12, 8)
$lblSys.Size = New-Object System.Drawing.Size(720, 24)
$lblSys.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10.5, [System.Drawing.FontStyle]::Bold)
$lblSys.ForeColor = [System.Drawing.Color]::FromArgb(40, 70, 110)
$panelStatus.Controls.Add($lblSys)
$lblLink = New-Object System.Windows.Forms.Label
$lblLink.Location = New-Object System.Drawing.Point(12, 38)
$lblLink.Size = New-Object System.Drawing.Size(720, 24)
$lblLink.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10)
$lblLink.ForeColor = [System.Drawing.Color]::FromArgb(60, 90, 130)
$panelStatus.Controls.Add($lblLink)
# 优化项表格
$lblGridTitle = New-Object System.Windows.Forms.Label
$lblGridTitle.Text = "优化项明细(当前系统实际设置)"
$lblGridTitle.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10, [System.Drawing.FontStyle]::Bold)
$lblGridTitle.Location = New-Object System.Drawing.Point(16, 204)
$lblGridTitle.Size = New-Object System.Drawing.Size(300, 22)
$form.Controls.Add($lblGridTitle)
$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = New-Object System.Drawing.Point(16, 230)
$grid.Size = New-Object System.Drawing.Size(744, 262)
$grid.BackgroundColor = [System.Drawing.Color]::White
$grid.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
$grid.RowHeadersVisible = $false
$grid.AllowUserToAddRows = $false
$grid.AllowUserToDeleteRows = $false
$grid.AllowUserToResizeRows = $false
$grid.ReadOnly = $true
$grid.SelectionMode = [System.Windows.Forms.DataGridViewSelectionMode]::FullRowSelect
$grid.ColumnHeadersHeightSizeMode = [System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode]::DisableResizing
$grid.ColumnHeadersHeight = 30
$grid.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 9.5)
[void]$grid.Columns.Add("Item", "优化项")
[void]$grid.Columns.Add("Current", "当前值")
[void]$grid.Columns.Add("Expected", "期望值")
[void]$grid.Columns.Add("Status", "状态")
$grid.Columns["Item"].Width = 200
$grid.Columns["Current"].Width = 230
$grid.Columns["Expected"].Width = 200
$grid.Columns["Status"].Width = 100
$form.Controls.Add($grid)
# 日志区
$lblLogTitle = New-Object System.Windows.Forms.Label
$lblLogTitle.Text = "操作日志"
$lblLogTitle.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 10, [System.Drawing.FontStyle]::Bold)
$lblLogTitle.Location = New-Object System.Drawing.Point(16, 500)
$lblLogTitle.Size = New-Object System.Drawing.Size(200, 22)
$form.Controls.Add($lblLogTitle)
$log = New-Object System.Windows.Forms.RichTextBox
$log.Location = New-Object System.Drawing.Point(16, 526)
$log.Size = New-Object System.Drawing.Size(744, 118)
$log.ReadOnly = $true
$log.BackColor = [System.Drawing.Color]::FromArgb(250, 250, 252)
$log.Font = New-Object System.Drawing.Font("Consolas", 9)
$log.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
$form.Controls.Add($log)
# 底部
$lblFooter = New-Object System.Windows.Forms.Label
$lblFooter.Text = '所有修改自动备份到 backup\ ,可用「一键恢复」还原 | 日志目录: logs\'
$lblFooter.Font = New-Object System.Drawing.Font("Microsoft YaHei UI", 8.5)
$lblFooter.ForeColor = [System.Drawing.Color]::FromArgb(130, 140, 155)
$lblFooter.Location = New-Object System.Drawing.Point(16, 652)
$lblFooter.Size = New-Object System.Drawing.Size(744, 18)
$form.Controls.Add($lblFooter)
# ============================================================
# 逻辑函数
# ============================================================
function Append-GBLog {
param([string]$Msg)
$log.AppendText((Get-Date -Format "[HH:mm:ss] ") + $Msg + "`r`n")
$log.SelectionStart = $log.TextLength
$log.ScrollToCaret()
}
function Update-GBStatusGrid {
$grid.Rows.Clear()
try {
$st = Get-GBAppliedStatus
foreach ($s in $st) {
$idx = $grid.Rows.Add($s.Item, $s.Current, $s.Expected, $s.Status)
$row = $grid.Rows[$idx]
if ($s.Status -like "✅*") { $row.DefaultCellStyle.ForeColor = [System.Drawing.Color]::FromArgb(0, 130, 60) }
elseif ($s.Status -like "⚠️*") { $row.DefaultCellStyle.ForeColor = [System.Drawing.Color]::FromArgb(200, 120, 0) }
else { $row.DefaultCellStyle.ForeColor = [System.Drawing.Color]::FromArgb(190, 60, 60) }
}
} catch {
Append-GBLog "状态读取失败: $($_.Exception.Message)"
}
}
function Update-GBLiveStatus {
try {
$s = Get-GBLiveStatus
$lblSys.Text = "CPU $($s.Cpu)% 内存 $($s.Mem)% GPU $($s.Gpu)% 温度 $($s.GpuTemp)°C"
} catch {
$lblSys.Text = "CPU -- 内存 -- GPU -- 温度 --"
}
}
function Update-GBLinkStatus {
$game = Test-GBGameRunning
$gameNames = @()
foreach ($gp in $script:Config.gameAware.gameProcesses) {
if (Get-Process -Name $gp -ErrorAction SilentlyContinue) { $gameNames += $gp }
}
$remote = Get-GBRemoteState
$gameTxt = if ($gameNames.Count -gt 0) { "游戏中 ($($gameNames -join ', '))" } else { "未检测到游戏" }
$icon = if ($game) { "🎮" } else { "○" }
$lblLink.Text = "游戏联动: $icon $gameTxt | 远控(GameViewer): $remote"
# 联动逻辑轮询(游戏启动/退出自动切换)
if ($script:Config.gameAware.enable) {
$msg = Update-GBGameAware
if ($msg) { Append-GBLog $msg }
}
}
# ============================================================
# 按钮行为
# ============================================================
# 一键优化 - 后台线程执行,完成后刷新
$worker = New-Object System.ComponentModel.BackgroundWorker
$worker.Add_DoWork({
Invoke-GBOptimize
})
$worker.Add_RunWorkerCompleted({
$btnOptimize.Enabled = $true
$btnOptimize.Text = "一键优化"
Update-GBStatusGrid
Append-GBLog ">>> 一键优化完成,状态已刷新"
})
$btnOptimize.Add_Click({
$btnOptimize.Enabled = $false
$btnOptimize.Text = "优化中..."
Append-GBLog ">>> 开始一键优化(电源 + GPU + 清理 + 网络)..."
$worker.RunWorkerAsync()
})
$btnDiagnose.Add_Click({
Append-GBLog "打开系统体检窗口..."
Start-Process -FilePath "powershell.exe" -ArgumentList @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$(Join-Path $script:Root 'GameBoost.ps1')`"", "-Diagnose")
})
$btnMonitor.Add_Click({
Append-GBLog "打开实时监控窗口(Ctrl+C 退出)..."
Start-Process -FilePath "powershell.exe" -ArgumentList @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$(Join-Path $script:Root 'GameBoost.ps1')`"", "-Monitor")
})
$btnRestore.Add_Click({
$ans = [System.Windows.Forms.MessageBox]::Show("确定要恢复最近一次优化前的全部设置吗?", "一键恢复", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($ans -eq [System.Windows.Forms.DialogResult]::Yes) {
Append-GBLog ">>> 开始一键恢复..."
Invoke-GBRestore
Update-GBStatusGrid
Append-GBLog ">>> 恢复完成,状态已刷新"
}
})
$btnRefresh.Add_Click({
Update-GBStatusGrid
Update-GBLinkStatus
Append-GBLog "已刷新状态"
})
$cbAware.Add_CheckedChanged({
$script:Config.gameAware.enable = $cbAware.Checked
Save-GBConfig -Path $script:ConfigPath -ConfigObj $script:Config
Append-GBLog ("游戏联动已" + $(if ($cbAware.Checked) { "开启" } else { "关闭" }))
})
$cbAutoStart.Add_CheckedChanged({
if ($cbAutoStart.Checked) { Set-GBAutoStart } else { Remove-GBAutoStart }
})
# 关闭确认
$form.Add_FormClosing({
param($sender, $e)
if (-not $script:Closing) {
$ans = [System.Windows.Forms.MessageBox]::Show("退出后将停止游戏联动监控,确定退出?", "GameBoost", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($ans -eq [System.Windows.Forms.DialogResult]::No) { $e.Cancel = $true }
}
})
# 定时器:系统状态 2s / 联动 3s / 日志跟随 3s
$timerSys = New-Object System.Windows.Forms.Timer
$timerSys.Interval = 2000
$timerSys.Add_Tick({ Update-GBLiveStatus })
$timerSys.Start()
$timerLink = New-Object System.Windows.Forms.Timer
$timerLink.Interval = [math]::Max(1, [int]$script:Config.gameAware.pollIntervalSec) * 1000
$timerLink.Add_Tick({ Update-GBLinkStatus })
$timerLink.Start()
$script:LogTail = 0
$timerLog = New-Object System.Windows.Forms.Timer
$timerLog.Interval = 3000
$timerLog.Add_Tick({
$logDir = Join-Path $script:Root "logs"
$lf = Get-ChildItem $logDir -Filter "gameboost_*.log" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($lf) {
$lines = @(Get-Content $lf.FullName -Encoding UTF8 -ErrorAction SilentlyContinue)
if ($lines.Count -gt $script:LogTail) {
$lines | Select-Object -Skip $script:LogTail | ForEach-Object { Append-GBLog $_ }
$script:LogTail = $lines.Count
}
}
})
$timerLog.Start()
# 初始加载
Update-GBLiveStatus
Update-GBLinkStatus
Update-GBStatusGrid
Append-GBLog "GameBoost 可视化客户端已启动(管理员: $(Test-GBAdmin))"
# 消息循环
[System.Windows.Forms.Application]::Run($form)
$script:Closing = $true
$timerSys.Stop(); $timerLink.Stop(); $timerLog.Stop()
Write-GBLog "INFO" "GUI 已退出"