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

导入 / 导出启动器设置 复活 #4707

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
158 changes: 157 additions & 1 deletion Plain Craft Launcher 2/Pages/PageSetup/ModSetup.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Public Class ModSetup
Imports System.Windows.Forms.AxHost

Public Class ModSetup

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

#End Region

#Region "导入/导出"
Private Function MachineID() As String
Return "" 'TODO:改为实际的机器码。
End Function
#Region "导出"
''' <summary>
''' 导出全局设置。
''' </summary>
''' <param name="Target">目标文件。</param>
''' <param name="ExportEncoded">是否导出注册表中被加密的设置。</param>
''' <returns>是否成功。</returns>
Public Function SetupExport(Target As String, Optional ExportEncoded As Boolean = False) As Boolean
Try
Log($"[Setup] 导出全局设置:到 {Target},{If(ExportEncoded, "含注册表", "不含注册表")}")
File.Create(Target).Dispose() '选文件的时候已经确认要给他替换掉了
IniClearCache(Target)
For Each entry In SetupDict
If entry.Value.Source <> SetupSource.Version Then
If ExportEncoded OrElse (Not (entry.Value.Encoded OrElse entry.Key = "Identify")) Then
WriteIni(Target, entry.Key, entry.Value.Value)
End If
End If
Next
WriteIni(Target, "MachineID", MachineID)
WriteIni(Target, "ExportType", "Global")
WriteIni(Target, "PCLVersion", VersionCode)
Return True
Catch ex As Exception
Log(ex, "导出全局设置失败", Level:=LogLevel.Hint)
Return False
End Try
End Function
''' <summary>
''' 导出版本设置。
''' </summary>
''' <param name="Target">目标文件。</param>
''' <param name="Version">要导出设置的版本。</param>
''' <returns>是否成功。</returns>
Public Function SetupExport(Target As String, Version As McVersion) As Boolean
Try
Log($"[Setup] 导出版本设置:{Version.Path},到 {Target}")
File.Create(Target).Dispose() '选文件的时候已经确认要给他替换掉了
IniClearCache(Target)
For Each entry In SetupDict
If entry.Value.Source = SetupSource.Version Then
WriteIni(Target, entry.Key, Setup.Get(entry.Key, Version))
End If
Next
For Each key In {"State", "Info", "Logo"}
WriteIni(Target, key, ReadIni($"{Version.Path}PCL\Setup.ini", key))
Next
WriteIni(Target, "ExportType", "Version")
WriteIni(Target, "PCLVersion", VersionCode)
Return True
Catch ex As Exception
Log(ex, "导出版本设置失败", Level:=LogLevel.Hint)
Return False
End Try
End Function
#End Region

#Region "导入"
Private CannotImport As String() = {"MachineID", "ExportType", "PCLVersion", "HintNotice", "SystemHelpVersion"}
''' <summary>
''' 导入全局设置。
''' </summary>
''' <param name="Source">源文件。</param>
''' <returns>是否成功。</returns>
Public Function SetupImport(Source As String) As Boolean
Try
Log($"[Setup] 导入全局设置:从 {Source}")
Select Case ReadIni(Source, "ExportType")
Case "Version"
Hint("导入的配置是版本设置,请到 版本设置 → 导入版本设置 页面导入!", HintType.Critical)
Return False
Case "Global" '正常
Case Else
Hint("请确认导入的配置文件有效!", HintType.Critical)
Return False
End Select
If ReadIni(Source, "PCLVersion", "10000000") > VersionCode Then
Hint("导入的配置来自高版本 PCL,可能导致问题。请到 设置 → 启动器 → 检查更新 更新启动器再试!", HintType.Critical)
Return False
End If
CopyFile(Path & "PCL\Setup.ini", Path & "PCL\Setup.ini.old") '备份现有

Dim importEncoded As Boolean = ReadIni(Source, "MachineID", "null") = MachineID()

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 CannotImport.Contains(key) Then
Log($"[Setup] 设置项 {key} 忽略")
Continue For
ElseIf (Not importEncoded) AndAlso SetupDict(key).Encoded Then
Log($"[Setup] 设置项 {key} 被加密,忽略")
Continue For
End If
Log($"[Setup] 设置项 {key} 导入,数据 {val}")
Setup.Set(key, val, ForceReload:=True)
Next
Return True
Catch ex As Exception
Log(ex, "导入全局设置失败", Level:=LogLevel.Msgbox)
Return False
End Try
End Function
''' <summary>
''' 导入版本设置。
''' </summary>
''' <param name="Source">源文件。</param>
''' <param name="Version">要导入设置的版本。</param>
''' <returns>是否成功。</returns>
Public Function SetupImport(Source As String, Version As McVersion) As Boolean
Try
Log($"[Setup] 导入版本设置:{Version.Path},从 {Source}")
Select Case ReadIni(Source, "ExportType")
Case "Global"
Hint("导入的配置是全局设置,请到 设置 → 启动器 页面导入!", HintType.Critical)
Return False
Case "Version" '正常
Case Else
Hint("请确认导入的配置文件有效!", HintType.Critical)
Return False
End Select
If ReadIni(Source, "PCLVersion", "10000000") > VersionCode Then
Hint("导入的配置来自高版本 PCL,可能导致问题。请到 设置 → 启动器 → 检查更新 更新启动器再试!", HintType.Critical)
Return False
End If

CopyFile(Version.Path & "PCL\Setup.ini", Version.Path & "PCL\Setup.ini.old") '备份现有

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 CannotImport.Contains(key) Then
Log($"[Setup] 设置项 {key} 忽略")
Continue For
End If
Log($"[Setup] 设置项 {key} 导入,数据 {val}")
If SetupDict.ContainsKey(key) Then Setup.Set(key, val, ForceReload:=True, Version:=Version) Else WriteIni(Version.Path & "PCL\Setup.ini", key, val)
Next
Return True
Catch ex As Exception
Log(ex, "导入全局设置失败", Level:=LogLevel.Msgbox)
Return False
End Try
End Function
#End Region

#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
41 changes: 41 additions & 0 deletions Plain Craft Launcher 2/Pages/PageSetup/PageSetupSystem.xaml.vb
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,45 @@
End Try
End Function

#Region "导入/导出设置"
'导出设置
Private Sub BtnSystemSettingExp_Click(sender As Object, e As MouseButtonEventArgs) Handles BtnSystemSettingExp.Click
Dim encodedExport As Boolean = False
Select Case MyMsgBox("是否需要导出账号密码、主题颜色等个人设置?" & vbCrLf &
"如果确定,则应妥善保存该设置,避免被他人窃取。这部分设置仅对这台电脑有效。",
Button1:="否", Button2:="是", Button3:="取消")
Case 1
encodedExport = False
Case 2
encodedExport = True
Case 3
Exit Sub
End Select
Dim savePath As String = SelectAs("选择保存位置", "PCL 全局配置.ini", "PCL 配置文件(*.ini)|*.ini", Path).Replace("/", "\")
If savePath = "" Then Exit Sub
If Setup.SetupExport(savePath, ExportEncoded:=encodedExport) 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 &
"当前设置将会被备份到 PCL 文件夹下的 Setup.ini.old 文件,如有需要可以自行还原。" & vbCrLf &
"是否继续?", Button1:="继续", Button2:="取消") = 1 Then
Dim sourcePath As String = SelectFile("PCL 配置文件(*.ini)|*.ini", "选择配置文件")
If sourcePath = "" Then Exit Sub
If Setup.SetupImport(sourcePath) Then
'把导入的设置 UI 化
If FrmSetupLaunch IsNot Nothing Then FrmSetupLaunch.Reload()
If FrmSetupUI IsNot Nothing Then FrmSetupUI.Reload()
If FrmSetupSystem IsNot Nothing Then FrmSetupSystem.Reload()
Hint("配置导入成功!", HintType.Finish)
End If
End If
End Sub

#End Region

End Class
12 changes: 12 additions & 0 deletions Plain Craft Launcher 2/Pages/PageVersion/PageVersionOverall.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@
</Grid>
</StackPanel>
</local:MyCard>
<local:MyCard Margin="0,0,0,15" Title="导入 / 导出设置" x:Name="PanSettingExpImp">
<StackPanel Margin="25,40,25,15">
<Grid Margin="0,2,0,7" Height="35">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
</Grid.ColumnDefinitions>
<local:MyButton x:Name="BtnSettingExport" Text="导出版本设置" MinWidth="140" Padding="13,0" Margin="0,0,20,0" />
<local:MyButton x:Name="BtnSettingImport" Text="导入版本设置" MinWidth="140" Padding="13,0" Margin="0,0,20,0" Grid.Column="1" />
</Grid>
</StackPanel>
</local:MyCard>
</StackPanel>
</local:MyScrollViewer>
</local:MyPageRight>
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,31 @@

#End Region

#Region "卡片:导入 / 导出设置"
Private Sub BtnSettingExport_Click(sender As Object, e As MouseButtonEventArgs) Handles BtnSettingExport.Click
Dim savePath As String = SelectAs("选择保存位置", $"PCL 版本配置 - {PageVersionLeft.Version.Name}.ini", "PCL 配置文件(*.ini)|*.ini", Path).Replace("/", "\")
If savePath = "" Then Exit Sub
If Setup.SetupExport(savePath, Version:=PageVersionLeft.Version) Then
Hint("配置导出成功!", HintType.Finish)
OpenExplorer($"/select,""{savePath}""")
End If
End Sub

Private Sub BtnSettingImport_Click(sender As Object, e As MouseButtonEventArgs) Handles BtnSettingImport.Click
If MyMsgBox("导入设置后,现有的设置将会被覆盖,建议先导出现有设置。" & vbCrLf &
"当前设置将会被备份到:版本文件夹下的 PCL 文件夹下的 Setup.ini.old 文件,如有需要可以自行还原。" & vbCrLf &
"是否继续?", Button1:="继续", Button2:="取消") = 1 Then
Dim sourcePath As String = SelectFile("PCL 配置文件(*.ini)|*.ini", "选择配置文件")
If sourcePath = "" Then Exit Sub
If Setup.SetupImport(sourcePath, PageVersionLeft.Version) Then
'把导入的设置 UI 化
If FrmVersionOverall IsNot Nothing Then FrmVersionOverall.Reload()
If FrmVersionSetup IsNot Nothing Then FrmVersionSetup.Reload()
Hint("配置导入成功!", HintType.Finish)
End If
End If
End Sub

#End Region

End Class