This commit is contained in:
1 parent
5d549ff66a
commit
e831d0dfe0
28 files changed
+1026
-286
No files matched your search
@@ -9,13 +9,28 @@ Friend Module ModSecret
|
||||
Public Const VersionBranchMain As String = "OpenSource"
|
||||
'在开源版的注册表与常规版的注册表隔离,以防数据冲突
|
||||
Public Const RegFolder As String = "PCLDebug"
|
||||
'用于微软登录的 ClientId
|
||||
Public OAuthClientId As String = If(Environment.GetEnvironmentVariable("PCL_MS_CLIENT_ID"), "")
|
||||
'CurseForge API Key
|
||||
Public CurseForgeAPIKey As String = If(Environment.GetEnvironmentVariable("PCL_CURSEFORGE_API_KEY"), "")
|
||||
'发布构建可内嵌配置,运行时环境变量优先。
|
||||
Private ReadOnly BuildCredentials As JObject = LoadBuildCredentials()
|
||||
Public OAuthClientId As String = GetBuildCredential("PCL_MS_CLIENT_ID")
|
||||
Public CurseForgeAPIKey As String = GetBuildCredential("PCL_CURSEFORGE_API_KEY")
|
||||
'用于匿名数据收集的腾讯云日志服务上报 URL,形如 https://{region}.cls.tencentcs.com/track?topic_id={topic_id}
|
||||
Public Const ClsBaseUrl As String = ""
|
||||
|
||||
Private Function LoadBuildCredentials() As JObject
|
||||
Using Stream = GetType(ModSecret).Assembly.GetManifestResourceStream("PCL.BuildCredentials.json")
|
||||
If Stream Is Nothing Then Return New JObject()
|
||||
Using Reader As New StreamReader(Stream)
|
||||
Return JObject.Parse(Reader.ReadToEnd())
|
||||
End Using
|
||||
End Using
|
||||
End Function
|
||||
|
||||
Private Function GetBuildCredential(Name As String) As String
|
||||
Dim OverrideValue As String = Environment.GetEnvironmentVariable(Name)
|
||||
If Not String.IsNullOrWhiteSpace(OverrideValue) Then Return OverrideValue
|
||||
Return If(BuildCredentials(Name)?.ToString(), "")
|
||||
End Function
|
||||
|
||||
#Region "网络鉴权"
|
||||
|
||||
'PCL-ISE 提供 Mojang 官方版本清单以及现有的 Minecraft 下载链路。
|
||||
@@ -65,15 +80,15 @@ Friend Module ModSecret
|
||||
Public ColorGray8 As New MyColor(245, 245, 245)
|
||||
Public ColorSemiTransparent As New MyColor(1, Color8)
|
||||
|
||||
Public ThemeNow As Integer = -1
|
||||
Public ColorHue As Integer = 210, ColorSat As Integer = 85, ColorLightAdjust As Integer = 0, ColorHueTopbarDelta As OneOf(Of Integer, Integer()) = 0
|
||||
Public ThemeDontClick As Integer = 0
|
||||
'PCL-ISE 仅提供龙猫蓝;旧主题编号和自定义调色均不参与渲染。
|
||||
Public Const ThemeNow As Integer = 0
|
||||
Public Const ColorHue As Integer = 210, ColorSat As Integer = 85, ColorLightAdjust As Integer = 0
|
||||
Public ReadOnly ColorHueTopbarDelta As OneOf(Of Integer, Integer()) = 0
|
||||
Public Const ThemeDontClick As Integer = 0
|
||||
|
||||
Public Sub ThemeRefresh(Optional NewTheme As Integer = -1)
|
||||
Try
|
||||
If ThemeNow = NewTheme AndAlso NewTheme >= 0 Then Return
|
||||
If NewTheme >= 0 Then ThemeNow = NewTheme
|
||||
|
||||
'保留旧调用签名以兼容设置回调,始终使用固定的龙猫蓝配色。
|
||||
Color1 = New MyColor().FromHSL2(ColorHue, ColorSat * 0.2, 25 + ColorLightAdjust * 0.3)
|
||||
Color2 = New MyColor().FromHSL2(ColorHue, ColorSat, 45 + ColorLightAdjust)
|
||||
Color3 = New MyColor().FromHSL2(ColorHue, ColorSat, 55 + ColorLightAdjust)
|
||||
@@ -114,7 +129,7 @@ Friend Module ModSecret
|
||||
Public Sub ThemeRefreshMain()
|
||||
RunInUi(
|
||||
Sub()
|
||||
If Not FrmMain.IsLoaded Then Return
|
||||
If FrmMain Is Nothing OrElse Not FrmMain.IsLoaded Then Return
|
||||
'顶部条背景
|
||||
Dim Brush = New LinearGradientBrush With {.EndPoint = New Point(1, 0), .StartPoint = New Point(0, 0)}
|
||||
Dim Deltas = ColorHueTopbarDelta.Switch(Function(d) New Integer() {-d, 0, d}, Function(d) d)
|
||||
@@ -137,9 +152,13 @@ Friend Module ModSecret
|
||||
End Sub)
|
||||
End Sub
|
||||
Friend Sub ThemeCheckAll(EffectSetup As Boolean)
|
||||
'仅重置旧主题选择和调色配置,不影响背景、透明度、音乐等个性化设置。
|
||||
For Each Key In {"UiLauncherTheme", "UiLauncherHue", "UiLauncherSat", "UiLauncherDelta", "UiLauncherLight"}
|
||||
Settings.Reset(Key)
|
||||
Next
|
||||
End Sub
|
||||
Friend Function ThemeCheckOne(Id As Integer) As Boolean
|
||||
Return True
|
||||
Return Id = 0
|
||||
End Function
|
||||
Friend Function ThemeUnlock(Id As Integer, Optional ShowDoubleHint As Boolean = True, Optional UnlockHint As String = Nothing) As Boolean
|
||||
Return False
|
||||
@@ -151,8 +170,10 @@ Friend Module ModSecret
|
||||
|
||||
'更新源使用 InfstarMC 自己的 Gitea Release API,不会从上游 PCL 下载启动器。
|
||||
Public Const UpdateApiUrl As String = "https://git.infstar.cn/api/v1/repos/InfStarMC/PCL-ISE/releases/latest"
|
||||
Private Const UpdateReleasesApiUrl As String = "https://git.infstar.cn/api/v1/repos/InfStarMC/PCL-ISE/releases"
|
||||
Public Const UpdateRepositoryUrl As String = "https://git.infstar.cn/InfStarMC/PCL-ISE/releases"
|
||||
Private Const UpdateCacheFile As String = "Cache\PclIseUpdate.json"
|
||||
Private Const SnapshotUpdateCacheFile As String = "Cache\PclIseSnapshotUpdate.json"
|
||||
Private Const UpdateAssetFileName As String = "PCL-ISE.exe"
|
||||
Private Const UpdateAssetMinSize As Long = 64 * 1024
|
||||
|
||||
@@ -169,10 +190,49 @@ Friend Module ModSecret
|
||||
|
||||
Private UpdateFilePath As String = Nothing
|
||||
Private UpdateCheckRunning As Integer = 0
|
||||
Private UpdateRestartRunning As Integer = 0
|
||||
Friend IsUpdateWaitingRestart As Boolean = False
|
||||
|
||||
Private Function GetLatestLauncherUpdate() As LauncherUpdateInfo
|
||||
Dim Json As JObject = NetRequestByClientRetry(UpdateApiUrl, RequireJson:=True).DeserializeJson()
|
||||
Return ParseLauncherUpdate(Json, Snapshot:=False)
|
||||
End Function
|
||||
|
||||
Private Function GetLatestSnapshotLauncherUpdate() As LauncherUpdateInfo
|
||||
Dim Json = GetLatestSnapshotRelease()
|
||||
If Json Is Nothing Then Return Nothing
|
||||
Return ParseLauncherUpdate(Json, Snapshot:=True)
|
||||
End Function
|
||||
|
||||
'按三位版本号选择最新的公开快照发布,不依赖 Gitea 返回的创建时间顺序。
|
||||
'分页请求可替换为隔离的测试数据,实际调用始终访问 InfstarMC Release API。
|
||||
Private Function GetLatestSnapshotRelease(Optional FetchPage As Func(Of Integer, String) = Nothing) As JObject
|
||||
If FetchPage Is Nothing Then
|
||||
FetchPage = Function(PageNumber) NetRequestByClientRetry(UpdateReleasesApiUrl & "?limit=50&page=" & PageNumber, RequireJson:=True).ToString()
|
||||
End If
|
||||
Dim Latest As JObject = Nothing
|
||||
Dim LatestVersion As Version = Nothing
|
||||
For PageNumber As Integer = 1 To 100
|
||||
Dim Releases = JArray.Parse(FetchPage(PageNumber))
|
||||
'即使服务器限制每页条数,也继续读取到空页,避免遗漏较早发布的高版本。
|
||||
If Releases.Count = 0 Then Return Latest
|
||||
For Each Candidate As JObject In Releases.OfType(Of JObject)()
|
||||
If Candidate.Value(Of Boolean)("draft") OrElse Not Candidate.Value(Of Boolean)("prerelease") Then Continue For
|
||||
Dim CandidateVersion = ParseLauncherVersion(If(Candidate("tag_name"), "").ToString())
|
||||
If CandidateVersion Is Nothing Then Continue For
|
||||
If LatestVersion Is Nothing OrElse CandidateVersion > LatestVersion Then
|
||||
Latest = Candidate
|
||||
LatestVersion = CandidateVersion
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
Throw New IOException("发布列表过长,未能完成快照版本检查,请前往发布页查看。")
|
||||
End Function
|
||||
|
||||
Private Function ParseLauncherUpdate(Json As JObject, Snapshot As Boolean) As LauncherUpdateInfo
|
||||
If Json.Value(Of Boolean)("draft") OrElse Json.Value(Of Boolean)("prerelease") <> Snapshot Then
|
||||
Throw New FormatException("PCL-ISE 发布类型与本次检查不符")
|
||||
End If
|
||||
Dim TagName = If(Json("tag_name"), "").ToString
|
||||
Dim VersionNumber = ParseLauncherVersion(TagName)
|
||||
If VersionNumber Is Nothing Then Throw New FormatException("PCL-ISE 发布信息中的版本号无效")
|
||||
@@ -215,7 +275,7 @@ Friend Module ModSecret
|
||||
.VersionNumber = VersionNumber
|
||||
}
|
||||
DirectoryUtils.Create(PathTemp & "Cache\")
|
||||
FileUtils.Write(PathTemp & UpdateCacheFile, Json.ToString)
|
||||
FileUtils.Write(PathTemp & If(Snapshot, SnapshotUpdateCacheFile, UpdateCacheFile), Json.ToString)
|
||||
Return Info
|
||||
End Function
|
||||
|
||||
@@ -237,7 +297,7 @@ Friend Module ModSecret
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Friend Sub UpdateCheckByButton()
|
||||
Friend Sub UpdateCheckByButton(Optional CheckSnapshot As Boolean = False)
|
||||
If Interlocked.Exchange(UpdateCheckRunning, 1) <> 0 Then
|
||||
Hint("正在检查更新,请稍候……")
|
||||
Return
|
||||
@@ -245,28 +305,35 @@ Friend Module ModSecret
|
||||
RunInNewThread(
|
||||
Sub()
|
||||
Try
|
||||
RunInUi(Sub() Hint("正在检查 PCL-ISE 更新……", Log:=False))
|
||||
Dim Info = GetLatestLauncherUpdate()
|
||||
Dim ChannelName = If(CheckSnapshot, "快照版", "正式版")
|
||||
RunInUi(Sub() Hint("正在检查 PCL-ISE " & ChannelName & "更新……", Log:=False))
|
||||
Dim Info = If(CheckSnapshot, GetLatestSnapshotLauncherUpdate(), GetLatestLauncherUpdate())
|
||||
If Info Is Nothing Then
|
||||
RunInUi(Sub() Hint("目前没有可用的 PCL-ISE " & ChannelName & "发布。"))
|
||||
Return
|
||||
End If
|
||||
If Not IsUpdateNewer(Info) Then
|
||||
RunInUi(Sub() Hint("当前已经是最新版本!", HintType.Green))
|
||||
RunInUi(Sub() Hint("当前版本不低于最新" & ChannelName & ",无需更新。", HintType.Green))
|
||||
Return
|
||||
End If
|
||||
|
||||
Dim Summary = If(String.IsNullOrWhiteSpace(Info.ReleaseBody), "该版本没有提供更新说明。", Info.ReleaseBody)
|
||||
If Summary.Length > 1200 Then Summary = Summary.Substring(0, 1200) & "……"
|
||||
Dim Confirm = RunInUiWait(Function() MyMsgBox($"发现 PCL-ISE {Info.TagName}。" & vbCrLf & vbCrLf & Summary,
|
||||
Dim SnapshotNotice = If(CheckSnapshot, "这是开发测试版本,可能包含尚未稳定的功能。" & vbCrLf & vbCrLf, "")
|
||||
Dim Confirm = RunInUiWait(Function() MyMsgBox($"发现 PCL-ISE {ChannelName} {Info.TagName}。" & vbCrLf & vbCrLf & SnapshotNotice & Summary,
|
||||
"发现新版本", "下载并更新", "查看发布页", "取消",
|
||||
Button2Action:=Sub() OpenWebsite(UpdateRepositoryUrl)))
|
||||
If Confirm = 2 Then Return
|
||||
If Confirm <> 1 Then Return
|
||||
|
||||
Dim SafeVersion = Info.VersionNumber.ToString(3)
|
||||
UpdateFilePath = PathTemp & "Update\PCL-ISE-" & SafeVersion & ".exe"
|
||||
Dim DownloadPath = PathTemp & "Update\PCL-ISE-" & SafeVersion & ".exe"
|
||||
Dim Checker As New FileChecker With {.MinSize = UpdateAssetMinSize, .ActualSize = Info.AssetSize, .Hash = If(String.IsNullOrWhiteSpace(Info.AssetHash), Nothing, Info.AssetHash)}
|
||||
RunInUi(Sub() Hint("正在下载更新……", Log:=False))
|
||||
NetDownloadByLoader(Info.DownloadUrl, UpdateFilePath, Check:=Checker)
|
||||
Dim DownloadedAssembly = Reflection.AssemblyName.GetAssemblyName(UpdateFilePath)
|
||||
NetDownloadByLoader(Info.DownloadUrl, DownloadPath, Check:=Checker)
|
||||
Dim DownloadedAssembly = Reflection.AssemblyName.GetAssemblyName(DownloadPath)
|
||||
If DownloadedAssembly.Name <> "Plain Craft Launcher 2" OrElse DownloadedAssembly.Version <> Info.VersionNumber Then Throw New FormatException("更新文件的程序集版本与发布版本不符")
|
||||
UpdateFilePath = DownloadPath
|
||||
IsUpdateWaitingRestart = True
|
||||
RunInUi(
|
||||
Sub()
|
||||
@@ -290,29 +357,110 @@ Friend Module ModSecret
|
||||
IsUpdateWaitingRestart = False
|
||||
Return
|
||||
End If
|
||||
If Interlocked.Exchange(UpdateRestartRunning, 1) <> 0 Then Return
|
||||
Try
|
||||
Dim Arguments = $"--update {Process.GetCurrentProcess.Id} ""{PathExe}"" ""{NewFile}"" {TriggerRestartAndByEnd.ToString.ToLowerInvariant()}"
|
||||
StartProcess(New ProcessStartInfo With {.FileName = NewFile, .Arguments = Arguments, .WorkingDirectory = Paths.Base, .UseShellExecute = True})
|
||||
StartProcess(New ProcessStartInfo With {.FileName = NewFile, .Arguments = Arguments, .WorkingDirectory = Paths.Base,
|
||||
.UseShellExecute = True, .WindowStyle = ProcessWindowStyle.Hidden})
|
||||
IsUpdateWaitingRestart = False
|
||||
If TriggerRestartAndByEnd Then FormMain.EndProgramForce()
|
||||
Catch ex As Exception
|
||||
Interlocked.Exchange(UpdateRestartRunning, 0)
|
||||
Logger.Error(ex, "启动更新替换进程失败", LogBehavior.Toast)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Public Function UpdateReplace(ProcessId As Integer, OldFileName As String, NewFileName As String, TriggerRestart As Boolean) As Boolean
|
||||
Dim Result As New LauncherUpdateResult With {
|
||||
.TargetPath = OldFileName, .DownloadPath = NewFileName, .VersionName = VersionBaseName,
|
||||
.HelperProcessId = Process.GetCurrentProcess().Id
|
||||
}
|
||||
Try
|
||||
ReplaceLauncherFile(ProcessId, OldFileName, NewFileName)
|
||||
Result.TargetPath = Path.GetFullPath(OldFileName)
|
||||
Result.DownloadPath = Path.GetFullPath(NewFileName)
|
||||
Result.BackupPath = Result.TargetPath & ".bak"
|
||||
ReplaceLauncherFile(ProcessId, Result.TargetPath, Result.DownloadPath)
|
||||
Result.Succeeded = True
|
||||
Logger.Info("PCL-ISE 更新文件替换完成")
|
||||
If TriggerRestart Then StartProcess(OldFileName)
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Result.ErrorMessage = ex.GetDisplay(False)
|
||||
Logger.Error(ex, "替换 PCL-ISE 更新文件失败")
|
||||
MsgBox("PCL-ISE 更新失败:" & ex.GetDisplay(False), MsgBoxStyle.Critical, "更新失败")
|
||||
Return False
|
||||
End Try
|
||||
Result.CompletedUtc = Date.UtcNow
|
||||
Try
|
||||
Dim ResultPath = GetLauncherUpdateResultPath(Result.TargetPath)
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(ResultPath))
|
||||
File.WriteAllText(ResultPath, Newtonsoft.Json.JsonConvert.SerializeObject(Result), System.Text.Encoding.UTF8)
|
||||
Catch ex As Exception
|
||||
Logger.Warn(ex, "保存 PCL-ISE 更新结果失败")
|
||||
End Try
|
||||
|
||||
If TriggerRestart Then
|
||||
If Result.Succeeded Then
|
||||
Try
|
||||
StartProcess(New ProcessStartInfo With {
|
||||
.FileName = Result.TargetPath, .Arguments = "--update-result",
|
||||
.WorkingDirectory = Path.GetDirectoryName(Result.TargetPath), .UseShellExecute = True
|
||||
})
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex, "更新完成,但自动重启失败")
|
||||
MsgBox("文件已更新,请手动启动 PCL-ISE。" & vbCrLf & ex.GetDisplay(False), MsgBoxStyle.Exclamation, "自动重启失败")
|
||||
End Try
|
||||
Else
|
||||
MsgBox("PCL-ISE 更新失败:" & Result.ErrorMessage & vbCrLf & "可查看旧版备份(如有):" & Result.BackupPath,
|
||||
MsgBoxStyle.Critical, "更新失败")
|
||||
End If
|
||||
End If
|
||||
Return Result.Succeeded
|
||||
End Function
|
||||
|
||||
'在主窗口加载后的后台初始化线程调用,显示上一次更新结果。
|
||||
Friend Sub ShowLauncherUpdateResult()
|
||||
Try
|
||||
Dim ResultPath = GetLauncherUpdateResultPath(PathExe)
|
||||
If Not File.Exists(ResultPath) Then Return
|
||||
Dim Result = JObject.Parse(File.ReadAllText(ResultPath)).ToObject(Of LauncherUpdateResult)()
|
||||
If Result Is Nothing OrElse Not String.Equals(Path.GetFullPath(Result.TargetPath), Path.GetFullPath(PathExe), StringComparison.OrdinalIgnoreCase) Then Return
|
||||
If Result.Succeeded AndAlso Result.VersionName <> VersionBaseName Then Return
|
||||
If Result.HelperProcessId > 0 AndAlso Result.HelperProcessId <> Process.GetCurrentProcess().Id Then
|
||||
Dim Helper As Process = Nothing
|
||||
Try
|
||||
Helper = Process.GetProcessById(Result.HelperProcessId)
|
||||
Catch ex As ArgumentException
|
||||
'更新进程已退出。
|
||||
End Try
|
||||
If Helper IsNot Nothing Then
|
||||
Using Helper
|
||||
If Not Helper.WaitForExit(5000) Then Return
|
||||
End Using
|
||||
End If
|
||||
End If
|
||||
|
||||
If Result.Succeeded Then
|
||||
'只清理此启动器管理的当前版本下载文件,其他路径保持原样。
|
||||
Dim ManagedDownload = Path.GetFullPath(PathTemp & "Update\PCL-ISE-" & VersionBaseName & ".exe")
|
||||
If Not String.IsNullOrWhiteSpace(Result.DownloadPath) AndAlso
|
||||
String.Equals(Path.GetFullPath(Result.DownloadPath), ManagedDownload, StringComparison.OrdinalIgnoreCase) AndAlso
|
||||
Not String.Equals(ManagedDownload, Path.GetFullPath(PathExe), StringComparison.OrdinalIgnoreCase) Then
|
||||
Try
|
||||
File.Delete(ManagedDownload)
|
||||
Catch ex As Exception
|
||||
Logger.Warn(ex, "清理已安装的更新下载文件失败")
|
||||
End Try
|
||||
End If
|
||||
Logger.Info("PCL-ISE 已更新至 " & Result.VersionName & ",旧版本备份:" & Result.BackupPath)
|
||||
Dim BackupHint = If(File.Exists(Result.BackupPath), ",旧版本备份已保留", "")
|
||||
RunInUi(Sub() Hint("PCL-ISE 已更新至 " & VersionDisplay & BackupHint & "。", HintType.Green))
|
||||
Else
|
||||
Logger.Warn("上次 PCL-ISE 更新未完成:" & Result.ErrorMessage & ",备份位置:" & Result.BackupPath)
|
||||
RunInUi(Sub() Hint("上次更新未完成,请查看日志后重试或手动安装。", HintType.Red))
|
||||
End If
|
||||
File.Delete(ResultPath)
|
||||
Catch ex As Exception
|
||||
Logger.Warn(ex, "处理 PCL-ISE 更新结果失败")
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 下载 InfstarMC 发布的启动器,用于整合包打包。
|
||||
''' 如果不是,则下载一个。
|
||||
|
||||
Reference in new issue
Block a user