Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新功能:自动修改显卡设置 #5845

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Plain Craft Launcher 2/Modules/Minecraft/ModLaunch.vb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ Public Module ModLaunch
Loaders.Insert(3, New LoaderTask(Of Integer, Integer)("内存优化", AddressOf McLaunchMemoryOptimize) With {.ProgressWeight = 30})
Case 2 '关闭
End Select
'显示设置
If Setup.Get("LaunchGraphicSetup") Then
Loaders.Insert(3, New LoaderTask(Of Integer, Integer)("检查显示设置", AddressOf McLaunchCheckGraphicSettings) With {.ProgressWeight = 2})
End If
Dim LaunchLoader As New LoaderCombo(Of Object)("Minecraft 启动", Loaders) With {.Show = False}
If McLoginLoader.State = LoadState.Finished Then McLoginLoader.State = LoadState.Waiting '要求重启登录主加载器,它会自行决定是否启动副加载器
'等待加载器执行并更新 UI
Expand Down Expand Up @@ -203,6 +207,48 @@ NextInner:

#End Region

#Region "显示设置"

Private Sub McLaunchCheckGraphicSettings(Loader As LoaderTask(Of Integer, Integer))
Const GPU_KEY As String = "Software\Microsoft\DirectX\UserGpuPreferences"
Const TARGET_SETUP As String = "GpuPreference=2;"
Dim javaPath As String = McLaunchJavaSelected.PathJavaw

McLaunchLog("显示设置开始")
'读取现行设置
Try
Dim parentKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser
Dim regKey As Microsoft.Win32.RegistryKey = parentKey.OpenSubKey(GPU_KEY, False)
If regKey IsNot Nothing Then
Dim currentSetting As Object = regKey.GetValue(javaPath)
If currentSetting IsNot Nothing And TARGET_SETUP.Equals(currentSetting.ToString()) Then
McLaunchLog("不需要调整显示设置")
Exit Sub
End If
regKey.Close()
End If
Catch ex As Exception
McLaunchLog("无法读取注册表,跳过显示设置")
Exit Sub
End Try
'更新设置
Try
If Not IsAdmin() Then RunAsAdmin("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里大概不应该直接重启 PCL……如果没有权限的话,弹一个 Hint 然后跳过处理会比较好

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是说RunAsAdmin需要重启吗?这个确实不知道,应该跟内存优化是一样的,但是内存优化又没有放出来

Copy link
Collaborator

@shimoranla shimoranla Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是说RunAsAdmin需要重启吗?这个确实不知道,应该跟内存优化是一样的,但是内存优化又没有放出来

出于安全考虑,主流操作系统一般不会允许修改进程权限,所以 RunAsAdmin 确实是需要重启的

Dim parentKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser
Dim regKey As Microsoft.Win32.RegistryKey = parentKey.OpenSubKey(GPU_KEY, True)
If regKey Is Nothing Then
regKey = parentKey.CreateSubKey(GPU_KEY)
End If
regKey.SetValue(javaPath, TARGET_SETUP)
Catch ex As Exception
McLaunchLog("无法写入显示设置")
Exit Sub
End Try
McLaunchLog("已修改显示设置")
End Sub

#End Region

#Region "预检测"

Private Sub McLaunchPrecheck()
Expand Down
1 change: 1 addition & 0 deletions Plain Craft Launcher 2/Pages/PageSetup/ModSetup.vb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
{"LaunchAdvanceAssets", New SetupEntry(False)},
{"LaunchAdvanceUseLaunchWrapper", New SetupEntry(True)},
{"LaunchAdvanceJava", New SetupEntry(False)},
{"LaunchGraphicSetup", New SetupEntry(False)},
{"LaunchRamType", New SetupEntry(0)},
{"LaunchRamCustom", New SetupEntry(15)},
{"LinkEula", New SetupEntry(False, Source:=SetupSource.Registry)},
Expand Down
7 changes: 7 additions & 0 deletions Plain Craft Launcher 2/Pages/PageSetup/PageSetupLaunch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@
</Grid>
</StackPanel>
</local:MyCard>
<local:MyCard Margin="0,0,0,15" Title="显示卡" CanSwap="True" IsSwaped="True">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议放到高级设置里,不要单开一个卡片

<StackPanel Margin="25,40,25,15">
<local:MyHint Text="此选项对应 Windows 设置中的“系统 > 屏幕 > 显示卡”页面,只在部分设备上有实际效果。" IsWarn="False" Margin="0,0,0,10" />
<local:MyCheckBox Text="启动前调整图形设置" x:Name="CheckAdvanceGraphicSetup" Tag="LaunchGraphicSetup"
ToolTip="启动前将 Java 的显示卡设置调整为“高性能”。若游戏运行时 GPU 占用过低,或在使用 OBS 捕捉时遇到黑屏问题,可尝试此功能。" />
</StackPanel>
</local:MyCard>
<local:MyCard x:Name="CardSkin" Margin="0,0,0,15" Title="离线皮肤" CanSwap="True" IsSwaped="True">
<StackPanel Margin="25,40,25,15">
<local:MyHint Text="由于技术问题,此功能只保证对 1.19.2 以前的版本有效!" IsWarn="True" Margin="0,0,0,10" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
CType(FindName("RadioRamType" & Setup.Load("LaunchRamType")), MyRadioBox).Checked = True
SliderRamCustom.Value = Setup.Get("LaunchRamCustom")

'显示卡
CheckAdvanceGraphicSetup.Checked = Setup.Get("LaunchGraphicSetup")

'高级设置
TextAdvanceJvm.Text = Setup.Get("LaunchAdvanceJvm")
TextAdvanceGame.Text = Setup.Get("LaunchAdvanceGame")
Expand Down Expand Up @@ -85,6 +88,7 @@
Setup.Reset("LaunchAdvanceAssets")
Setup.Reset("LaunchAdvanceRun")
Setup.Reset("LaunchAdvanceRunWait")
Setup.Reset("LaunchGraphicSetup")

Setup.Reset("LaunchArgumentJavaAll")
Setup.Reset("LaunchArgumentJavaSelect")
Expand Down Expand Up @@ -112,7 +116,7 @@
Private Shared Sub ComboChange(sender As MyComboBox, e As Object) Handles ComboArgumentIndie.SelectionChanged, ComboArgumentVisibie.SelectionChanged, ComboArgumentWindowType.SelectionChanged, ComboArgumentPriority.SelectionChanged
If AniControlEnabled = 0 Then Setup.Set(sender.Tag, sender.SelectedIndex)
End Sub
Private Shared Sub CheckBoxChange(sender As MyCheckBox, e As Object) Handles CheckAdvanceAssets.Change, CheckAdvanceJava.Change, CheckAdvanceRunWait.Change, CheckArgumentRam.Change, CheckAdvanceUseLaunchWrapper.Change
Private Shared Sub CheckBoxChange(sender As MyCheckBox, e As Object) Handles CheckAdvanceAssets.Change, CheckAdvanceJava.Change, CheckAdvanceRunWait.Change, CheckArgumentRam.Change, CheckAdvanceUseLaunchWrapper.Change, CheckAdvanceGraphicSetup.Change
If AniControlEnabled = 0 Then Setup.Set(sender.Tag, sender.Checked)
End Sub

Expand Down