Skip to content

Commit

Permalink
✨ 完成 Hex-Dragon#1834 导入/导出启动器设置
Browse files Browse the repository at this point in the history
  • Loading branch information
youzi-2333 committed Jun 20, 2024
1 parent 9b6e92b commit bb85173
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
89 changes: 88 additions & 1 deletion Plain Craft Launcher 2/Pages/PageSetup/ModSetup.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Public Class ModSetup
Imports System.Text
Imports Newtonsoft.Json

Public Class ModSetup

''' <summary>
''' 设置的更新号。
Expand Down Expand Up @@ -688,4 +691,88 @@

#End Region

#Region "导入/导出"
''' <summary>
''' 导出设置。
''' </summary>
''' <param name="Target">目标文件。</param>
''' <param name="Registry">是否导出注册表的设置。</param>
''' <returns>是否成功。</returns>
Public Function SetupExport(Target As String, Optional Registry As Boolean = False) As Boolean
Try
Log($"[Setup] 导出设置:到 {Target},{If(Registry, "含注册表", "不含注册表")}")
If File.Exists(Target) Then File.Delete(Target) '选文件的时候已经确认要给他替换掉了
File.Copy(Path & "PCL\Setup.ini", Target)
If Registry Then
ShellAndGetOutput("cmd", Arguments:=$"/C reg export HKCU\Software\{RegFolder} ""{PathTemp}\ExportTemp.reg""")
WriteIni(Target, "RegInfo", Convert.ToBase64String(File.ReadAllBytes($"{PathTemp}\ExportTemp.reg")))
WriteIni(Target, "Identify", Setup.Get("Identify"))
File.Delete($"{PathTemp}\ExportTemp.reg")
End If
Return True
Catch ex As Exception
Log(ex, "导出设置失败", Level:=LogLevel.Hint)
Return False
End Try
End Function
''' <summary>
''' 导入设置。
''' </summary>
''' <param name="Source">源文件。</param>
''' <returns>0 为成功需重启,1 为成功不需重启,2 为失败。</returns>
Public Function SetupImport(Source As String) As Byte
Try
Log($"[Setup] 导入设置:从 {Source}")
If ReadIni(Source, "Identify", "null") = "null" Then
Hint("不是有效的配置文件!", HintType.Critical)
Return 2 '失败
End If

'还原 Setup.ini
'If File.Exists(Path & "PCL\Setup.ini") Then File.Delete(Path & "PCL\Setup.ini")
'File.Create(Path & "PCL\Setup.ini")
Dim id As String = "null"
Dim hasReg As Boolean = False
For Each Ln In File.ReadAllLines(Source)
Dim pair As String() = Ln.Split(":".ToCharArray, 2)
Dim key As String = pair.First()
Dim val As String = If(pair.Length < 2, "", pair.Last())
If Ln.StartsWithF("RegInfo:") Then '注册表
File.WriteAllBytes($"{PathTemp}\ImportTemp.reg", Convert.FromBase64String(val))
hasReg = True
ElseIf Ln.StartsWithF("Identify:") Then
If val <> "" Then id = val
Else
Setup.Set(key, val, ForceReload:=True)
End If
Next
If hasReg Then
If Setup.Get("Identify") <> id Then
#If BETA Then
Dim msg As String = "隐藏主题、正版账号、LittleSkin 账号"
#Else
Dim msg As String = "正版账号、LittleSkin 账号"
#End If
If MyMsgBox(
$"导入的设置可能来自另一台电脑,且将会导致 PCL {msg}等信息失效。" & vbCrLf &
"即使你导出了当前设置,也可能无法再次还原!" & vbCrLf & 'youzi-2333:别问我是怎么知道的
"除非你知道你在做什么,否则请取消导入注册表项!" & vbCrLf &
vbCrLf &
"点击 ""取消"" 按钮后,账号信息、主题颜色信息等不会被导入,其它安全的信息仍会导入。" & vbCrLf &
"这是最后的警告!", Title:="警告",
Button1:="取消", Button2:="我知道我在做什么!", Button3:="取消", IsWarn:=True) <> 2 Then
Return 1 '不重启
End If
End If
ShellAndGetOutput("cmd", Arguments:=$"/C reg import ""{PathTemp}\ImportTemp.reg""")
Return 0 '重启
End If
Return 1 '不重启
Catch ex As Exception
Log(ex, "导入设置失败", Level:=LogLevel.Msgbox)
Return 2 '失败
End Try
End Function
#End Region

End Class
10 changes: 10 additions & 0 deletions Plain Craft Launcher 2/Pages/PageSetup/PageSetupSystem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
<RowDefinition Height="9" />
<RowDefinition Height="28" />
<RowDefinition Height="9" />
<RowDefinition Height="40" />
<RowDefinition Height="9" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Text="启动器更新" Margin="0,0,25,0" />
Expand Down Expand Up @@ -126,6 +128,14 @@
<local:MyButton Grid.Column="1" x:Name="BtnSystemIdentify" MinWidth="140" Text="复制识别码" Padding="13,0" Margin="0,0,20,0" />
<local:MyButton Grid.Column="2" x:Name="BtnSystemUnlock" MinWidth="140" Text="输入解锁码" Padding="13,0" Margin="0,0,20,0" />
</Grid>
<Grid Height="35" Grid.Row="10" Grid.ColumnSpan="2" Margin="0,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<local:MyButton Grid.Column="0" x:Name="BtnSystemSettingExp" MinWidth="140" Text="导出设置" Padding="13,0" Margin="0,0,20,0" />
<local:MyButton Grid.Column="1" x:Name="BtnSystemSettingImp" MinWidth="140" Text="导入设置" Padding="13,0" Margin="0,0,20,0" />
</Grid>
</Grid>
</local:MyCard>

Expand Down
44 changes: 43 additions & 1 deletion Plain Craft Launcher 2/Pages/PageSetup/PageSetupSystem.xaml.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Class PageSetupSystem
Imports Microsoft.Win32

Class PageSetupSystem

#Region "语言"
'Private Sub PageSetupUI_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Expand Down Expand Up @@ -212,4 +214,44 @@
End Try
End Function

#Region "导入/导出设置"
'导出设置
Private Sub BtnSystemSettingExp_Click(sender As Object, e As MouseButtonEventArgs) Handles BtnSystemSettingExp.Click
Dim regExport As Boolean = False
Select Case MyMsgBox("是否需要导出账号密码、主题颜色等个人设置?" & vbCrLf &
"如果确定,则应妥善保存该设置,避免被他人窃取。这部分敏感设置仅对这台电脑有效。",
Button1:="否", Button2:="是", Button3:="取消")
Case 1
regExport = False
Case 2
regExport = True
Case 3
Exit Sub
End Select
Dim savePath As String = SelectAs("选择保存位置", "PCL 导出配置.ini", "PCL 配置文件(*.ini)|*.ini", Path).Replace("/", "\")
If Setup.SetupExport(savePath, regExport) Then
Hint("配置导出成功!", HintType.Finish)
OpenExplorer($"/select,""{savePath}""")
End If
End Sub

'导入
Private Sub BtnSystemSettingImp_Click(sender As Object, e As MouseButtonEventArgs) Handles BtnSystemSettingImp.Click
If MyMsgBox("导入设置后,现有的设置将会被覆盖,建议先导出现有设置。" & vbCrLf &
"导入完成后,启动器可能需要重启。" & vbCrLf &
"是否继续?", Button1:="继续", Button2:="取消") = 1 Then
Select Case Setup.SetupImport(SelectFile("PCL 配置文件(*.ini)|*.ini", "选择配置文件"))
Case 0 '重启
MyMsgBox("配置导入成功!PCL 即将重启……", ForceWait:=True)
ShellOnly(PathWithName)
Environment.Exit(0)
Case 1 '不重启
Hint("配置导入成功!", HintType.Finish)
Case 2 '炸了,在 SetupImport 函数中显示错误信息
End Select
End If
End Sub

#End Region

End Class

0 comments on commit bb85173

Please sign in to comment.