Skip to content

Commit ee38591

Browse files
authored
Merge pull request #6933 from pynickle/fix/issue-6876
imp(VersionOverall): 优化版本重命名处理逻辑
2 parents 47c3c16 + fc910b1 commit ee38591

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

Plain Craft Launcher 2/Pages/PageVersion/PageVersionOverall.xaml.vb

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,18 @@
116116
Dim TempName As String = NewName & "_temp"
117117
Dim TempPath As String = PathMcFolder & "versions\" & TempName & "\"
118118
Dim IsCaseChangedOnly As Boolean = NewName.ToLower = OldName.ToLower
119-
'重新读取版本 JSON 信息,避免 JsonObject 中已被合并的项被重新存储
119+
120+
'读取版本 JSON 信息
120121
Dim JsonObject As JObject
122+
Dim OldJsonPath As String = ""
121123
Try
122-
Dim JsonPath As String = PageVersionLeft.Version.GetJsonPath()
123-
JsonObject = GetJson(ReadFile(JsonPath))
124-
File.Delete(JsonPath)
124+
OldJsonPath = PageVersionLeft.Version.GetJsonPath()
125+
JsonObject = GetJson(ReadFile(OldJsonPath))
125126
Catch ex As Exception
126127
Log(ex, "在重命名读取 json 时失败")
127128
JsonObject = PageVersionLeft.Version.JsonObject
128129
End Try
130+
129131
'重命名主文件夹
130132
My.Computer.FileSystem.RenameDirectory(OldPath, TempName)
131133
My.Computer.FileSystem.RenameDirectory(TempPath, NewName)
@@ -160,13 +162,48 @@
160162
If ReadIni(PathMcFolder & "PCL.ini", "Version") = OldName Then
161163
WriteIni(PathMcFolder & "PCL.ini", "Version", NewName)
162164
End If
163-
'写入版本 Json
165+
166+
'处理JSON文件:先写入新文件,再删除旧文件
164167
Try
168+
'更新JSON内容中的id
165169
JsonObject("id") = NewName
170+
'写入新的JSON文件
166171
WriteFile(NewPath & NewName & ".json", JsonObject.ToString)
172+
173+
'删除旧的JSON文件(现在在新路径下)
174+
Dim OldJsonInNewPath As String = NewPath & OldName & ".json"
175+
If File.Exists(OldJsonInNewPath) Then
176+
File.Delete(OldJsonInNewPath)
177+
End If
167178
Catch ex As Exception
168179
Log(ex, "重命名版本 json 失败")
180+
'如果JSON处理失败,尝试回滚整个操作
181+
If Directory.Exists(NewPath) AndAlso Not Directory.Exists(OldPath) Then
182+
Try
183+
'还原jar文件名
184+
If File.Exists($"{NewPath}{NewName}.jar") Then
185+
My.Computer.FileSystem.RenameFile($"{NewPath}{NewName}.jar", $"{OldName}.jar")
186+
End If
187+
'还原natives文件夹名
188+
If Directory.Exists($"{NewPath}{NewName}-natives") Then
189+
My.Computer.FileSystem.RenameDirectory($"{NewPath}{NewName}-natives", $"{OldName}-natives")
190+
End If
191+
Catch
192+
' 忽略 jar 文件, natives 文件夹还原失败
193+
End Try
194+
End If
195+
Try
196+
'还原文件夹名
197+
My.Computer.FileSystem.RenameDirectory(NewPath, OldName)
198+
Hint("重命名失败:无法处理版本配置文件,已回滚更改", HintType.Critical)
199+
Return
200+
Catch rollbackEx As Exception
201+
Log(rollbackEx, "重命名失败后尝试回滚也失败")
202+
Hint("重命名失败:版本文件可能已损坏,请检查版本文件夹", HintType.Critical)
203+
Return
204+
End Try
169205
End Try
206+
170207
'刷新与提示
171208
Hint("重命名成功!", HintType.Finish)
172209
PageVersionLeft.Version = New McVersion(NewName).Load()

0 commit comments

Comments
 (0)