366 lines
19 KiB
VB.net
366 lines
19 KiB
VB.net
'由于包含加解密等安全信息,本文件中的部分代码已被删除
|
|
|
|
Imports System.Diagnostics
|
|
Imports System.IO
|
|
|
|
Friend Module ModSecret
|
|
|
|
'标注 PCL 的不同分支,仅用于替换标记
|
|
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"), "")
|
|
'用于匿名数据收集的腾讯云日志服务上报 URL,形如 https://{region}.cls.tencentcs.com/track?topic_id={topic_id}
|
|
Public Const ClsBaseUrl As String = ""
|
|
|
|
#Region "网络鉴权"
|
|
|
|
'PCL-ISE 提供 Mojang 官方版本清单以及现有的 Minecraft 下载链路。
|
|
Public Const RestrictMcDownload As Boolean = False
|
|
Friend Function SecretCdnSign(UrlWithMark As String) As String
|
|
If Not UrlWithMark.EndsWithF("{CDN}") Then Return UrlWithMark
|
|
Return UrlWithMark.Replace("{CDN}", "").Replace(" ", "%20")
|
|
End Function
|
|
''' <summary>
|
|
''' 设置 Headers 的 UA、Referer。
|
|
''' </summary>
|
|
Friend Sub SecretHeadersSign(Url As String, ByRef Req As HttpRequestMessage, Optional SimulateBrowserHeaders As Boolean = False)
|
|
If Not Req.Headers.UserAgent.Any Then
|
|
If Url.Contains("baidupcs.com") OrElse Url.Contains("baidu.com") Then
|
|
Req.Headers.Add("User-Agent", "LogStatistic") '#4951
|
|
ElseIf SimulateBrowserHeaders Then
|
|
Req.Headers.Add("User-Agent", $"PCL2/{VersionBaseName}.{CInt(BuildType)} Mozilla/5.0 AppleWebKit/537.36 Chrome/63.0.3239.132 Safari/537.36")
|
|
Else
|
|
Req.Headers.Add("User-Agent", $"PCL2/{VersionBaseName}.{CInt(BuildType)}")
|
|
End If
|
|
End If
|
|
If Not SimulateBrowserHeaders Then Req.Headers.Add("Referer", $"http://{VersionCode}.open.pcl2.server/")
|
|
If Url.Contains("api.curseforge.com") OrElse Url.Contains("forgecdn.net") Then Req.Headers.Add("x-api-key", CurseForgeAPIKey)
|
|
End Sub
|
|
|
|
#End Region
|
|
|
|
#Region "主题"
|
|
|
|
Public Color1 As New MyColor(52, 61, 74)
|
|
Public Color2 As New MyColor(11, 91, 203)
|
|
Public Color3 As New MyColor(19, 112, 243)
|
|
Public Color4 As New MyColor(72, 144, 245)
|
|
Public Color5 As New MyColor(150, 192, 249)
|
|
Public Color6 As New MyColor(213, 230, 253)
|
|
Public Color7 As New MyColor(222, 236, 253)
|
|
Public Color8 As New MyColor(234, 242, 254)
|
|
Public ColorBg0 As New MyColor(150, 192, 249)
|
|
Public ColorBg1 As New MyColor(190, Color7)
|
|
Public ColorGray1 As New MyColor(64, 64, 64)
|
|
Public ColorGray2 As New MyColor(115, 115, 115)
|
|
Public ColorGray3 As New MyColor(140, 140, 140)
|
|
Public ColorGray4 As New MyColor(166, 166, 166)
|
|
Public ColorGray5 As New MyColor(204, 204, 204)
|
|
Public ColorGray6 As New MyColor(235, 235, 235)
|
|
Public ColorGray7 As New MyColor(240, 240, 240)
|
|
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
|
|
|
|
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)
|
|
Color4 = New MyColor().FromHSL2(ColorHue, ColorSat, 65 + ColorLightAdjust)
|
|
Color5 = New MyColor().FromHSL2(ColorHue, ColorSat, 80 + ColorLightAdjust * 0.4)
|
|
Color6 = New MyColor().FromHSL2(ColorHue, ColorSat, 91 + ColorLightAdjust * 0.1)
|
|
Color7 = New MyColor().FromHSL2(ColorHue, ColorSat, 95)
|
|
Color8 = New MyColor().FromHSL2(ColorHue, ColorSat, 97)
|
|
ColorBg0 = Color4 * 0.4 + Color5 * 0.4 + ColorGray4 * 0.2
|
|
ColorBg1 = New MyColor(190, Color7)
|
|
|
|
ColorSemiTransparent = New MyColor(1, Color8)
|
|
Application.Current.Resources("ColorBrush1") = New SolidColorBrush(Color1)
|
|
Application.Current.Resources("ColorBrush2") = New SolidColorBrush(Color2)
|
|
Application.Current.Resources("ColorBrush3") = New SolidColorBrush(Color3)
|
|
Application.Current.Resources("ColorBrush4") = New SolidColorBrush(Color4)
|
|
Application.Current.Resources("ColorBrush5") = New SolidColorBrush(Color5)
|
|
Application.Current.Resources("ColorBrush6") = New SolidColorBrush(Color6)
|
|
Application.Current.Resources("ColorBrush7") = New SolidColorBrush(Color7)
|
|
Application.Current.Resources("ColorBrush8") = New SolidColorBrush(Color8)
|
|
Application.Current.Resources("ColorBrushBg0") = New SolidColorBrush(ColorBg0)
|
|
Application.Current.Resources("ColorBrushBg1") = New SolidColorBrush(ColorBg1)
|
|
Application.Current.Resources("ColorObject1") = CType(Color1, Color)
|
|
Application.Current.Resources("ColorObject2") = CType(Color2, Color)
|
|
Application.Current.Resources("ColorObject3") = CType(Color3, Color)
|
|
Application.Current.Resources("ColorObject4") = CType(Color4, Color)
|
|
Application.Current.Resources("ColorObject5") = CType(Color5, Color)
|
|
Application.Current.Resources("ColorObject6") = CType(Color6, Color)
|
|
Application.Current.Resources("ColorObject7") = CType(Color7, Color)
|
|
Application.Current.Resources("ColorObject8") = CType(Color8, Color)
|
|
Application.Current.Resources("ColorObjectBg0") = CType(ColorBg0, Color)
|
|
Application.Current.Resources("ColorObjectBg1") = CType(ColorBg1, Color)
|
|
ThemeRefreshMain()
|
|
Catch ex As Exception
|
|
Logger.Error(ex, "刷新主题颜色失败", LogBehavior.Toast)
|
|
End Try
|
|
End Sub
|
|
Public Sub ThemeRefreshMain()
|
|
RunInUi(
|
|
Sub()
|
|
If 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)
|
|
Brush.GradientStops.Add(New GradientStop With {.Offset = 0, .Color = New MyColor().FromHSL2(ColorHue + Deltas(0), ColorSat, 48 + ColorLightAdjust)})
|
|
Brush.GradientStops.Add(New GradientStop With {.Offset = 0.5, .Color = New MyColor().FromHSL2(ColorHue + Deltas(1), ColorSat, 54 + ColorLightAdjust)})
|
|
Brush.GradientStops.Add(New GradientStop With {.Offset = 1, .Color = New MyColor().FromHSL2(ColorHue + Deltas(2), ColorSat, 48 + ColorLightAdjust)})
|
|
FrmMain.PanTitle.Background = Brush
|
|
FrmMain.PanTitle.Background.Freeze()
|
|
'主页面背景
|
|
If Settings.Get(Of Boolean)("UiBackgroundColorful") Then
|
|
Brush = New LinearGradientBrush With {.EndPoint = New Point(0.1, 1), .StartPoint = New Point(0.9, 0)}
|
|
Brush.GradientStops.Add(New GradientStop With {.Offset = -0.1, .Color = New MyColor().FromHSL2(ColorHue - 15, ColorSat * 0.8, 91)})
|
|
Brush.GradientStops.Add(New GradientStop With {.Offset = 0.4, .Color = New MyColor().FromHSL2(ColorHue, ColorSat * 0.8, 91)})
|
|
Brush.GradientStops.Add(New GradientStop With {.Offset = 1.1, .Color = New MyColor().FromHSL2(ColorHue + 15, ColorSat * 0.8, 91)})
|
|
FrmMain.PanForm.Background = Brush
|
|
Else
|
|
FrmMain.PanForm.Background = New MyColor(245, 245, 245)
|
|
End If
|
|
FrmMain.PanForm.Background.Freeze()
|
|
End Sub)
|
|
End Sub
|
|
Friend Sub ThemeCheckAll(EffectSetup As Boolean)
|
|
End Sub
|
|
Friend Function ThemeCheckOne(Id As Integer) As Boolean
|
|
Return True
|
|
End Function
|
|
Friend Function ThemeUnlock(Id As Integer, Optional ShowDoubleHint As Boolean = True, Optional UnlockHint As String = Nothing) As Boolean
|
|
Return False
|
|
End Function
|
|
|
|
#End Region
|
|
|
|
#Region "更新"
|
|
|
|
'更新源使用 InfstarMC 自己的 Gitea Release API,不会从上游 PCL 下载启动器。
|
|
Public Const UpdateApiUrl As String = "https://git.infstar.cn/api/v1/repos/InfStarMC/PCL-ISE/releases/latest"
|
|
Public Const UpdateRepositoryUrl As String = "https://git.infstar.cn/InfStarMC/PCL-ISE/releases"
|
|
Private Const UpdateCacheFile As String = "Cache\PclIseUpdate.json"
|
|
Private Const UpdateAssetFileName As String = "PCL-ISE.exe"
|
|
Private Const UpdateAssetMinSize As Long = 64 * 1024
|
|
|
|
Private Class LauncherUpdateInfo
|
|
Public TagName As String
|
|
Public ReleaseName As String
|
|
Public ReleaseBody As String
|
|
Public DownloadUrl As String
|
|
Public AssetName As String
|
|
Public AssetSize As Long
|
|
Public AssetHash As String
|
|
Public VersionNumber As Version
|
|
End Class
|
|
|
|
Private UpdateFilePath As String = Nothing
|
|
Private UpdateCheckRunning As Integer = 0
|
|
Friend IsUpdateWaitingRestart As Boolean = False
|
|
|
|
Private Function GetLatestLauncherUpdate() As LauncherUpdateInfo
|
|
Dim Json As JObject = NetRequestByClientRetry(UpdateApiUrl, RequireJson:=True).DeserializeJson()
|
|
Dim TagName = If(Json("tag_name"), "").ToString
|
|
Dim VersionNumber = ParseLauncherVersion(TagName)
|
|
If VersionNumber Is Nothing Then Throw New FormatException("PCL-ISE 发布信息中的版本号无效")
|
|
|
|
Dim SelectedAsset As JObject = Nothing
|
|
For Each Candidate As JObject In If(Json("assets"), New JArray)
|
|
Dim CandidateName = If(Candidate("name"), "").ToString
|
|
If CandidateName.Equals(UpdateAssetFileName, StringComparison.OrdinalIgnoreCase) Then
|
|
SelectedAsset = Candidate
|
|
Exit For
|
|
End If
|
|
Next
|
|
If SelectedAsset Is Nothing Then Throw New FileNotFoundException("发布中没有找到 PCL-ISE 的 exe 更新文件")
|
|
|
|
Dim DownloadUrl = If(SelectedAsset("browser_download_url"), If(SelectedAsset("download_url"), "")).ToString
|
|
If String.IsNullOrWhiteSpace(DownloadUrl) Then Throw New FormatException("PCL-ISE 发布文件缺少下载地址")
|
|
Dim AssetSize As Long = -1
|
|
If SelectedAsset("size") IsNot Nothing Then AssetSize = SelectedAsset("size").ToObject(Of Long)()
|
|
Dim AssetHash = If(SelectedAsset("digest"), If(SelectedAsset("sha256"), If(SelectedAsset("checksum"), ""))).ToString
|
|
If AssetHash.StartsWith("sha256:", StringComparison.OrdinalIgnoreCase) Then AssetHash = AssetHash.Substring(7)
|
|
If Not System.Text.RegularExpressions.Regex.IsMatch(AssetHash, "\A[0-9a-fA-F]{64}\z") Then
|
|
Dim ChecksumAsset = DirectCast(Json("assets"), JArray).OfType(Of JObject)().FirstOrDefault(
|
|
Function(a) String.Equals(If(a("name"), "").ToString, UpdateAssetFileName & ".sha256", StringComparison.OrdinalIgnoreCase))
|
|
If ChecksumAsset Is Nothing Then Throw New FormatException("发布文件缺少 SHA256 校验信息")
|
|
Dim ChecksumUrl = If(ChecksumAsset("browser_download_url"), If(ChecksumAsset("download_url"), "")).ToString
|
|
Dim ChecksumText = NetRequestByClientRetry(ChecksumUrl).ToString.Trim()
|
|
Dim Match = System.Text.RegularExpressions.Regex.Match(ChecksumText, "\A([0-9a-fA-F]{64})(?:\s+\*?PCL-ISE\.exe)?\s*\z")
|
|
If Not Match.Success Then Throw New FormatException("发布文件的 SHA256 校验信息无效")
|
|
AssetHash = Match.Groups(1).Value
|
|
End If
|
|
|
|
Dim Info As New LauncherUpdateInfo With {
|
|
.TagName = TagName,
|
|
.ReleaseName = If(Json("name"), TagName).ToString,
|
|
.ReleaseBody = If(Json("body"), "").ToString,
|
|
.DownloadUrl = DownloadUrl,
|
|
.AssetName = If(SelectedAsset("name"), UpdateAssetFileName).ToString,
|
|
.AssetSize = AssetSize,
|
|
.AssetHash = AssetHash,
|
|
.VersionNumber = VersionNumber
|
|
}
|
|
DirectoryUtils.Create(PathTemp & "Cache\")
|
|
FileUtils.Write(PathTemp & UpdateCacheFile, Json.ToString)
|
|
Return Info
|
|
End Function
|
|
|
|
Private Function IsUpdateNewer(Info As LauncherUpdateInfo) As Boolean
|
|
Return Info IsNot Nothing AndAlso Info.VersionNumber IsNot Nothing AndAlso Info.VersionNumber > ParseLauncherVersion(VersionBaseName)
|
|
End Function
|
|
|
|
Friend Function IsCachedLauncherNewest() As Boolean?
|
|
Try
|
|
Dim CacheContent = FileUtils.TryReadAsString(PathTemp & UpdateCacheFile)
|
|
If String.IsNullOrWhiteSpace(CacheContent) Then Return Nothing
|
|
Dim Json As JObject = CacheContent.DeserializeJson()
|
|
Dim CachedVersion = ParseLauncherVersion(If(Json("tag_name"), "").ToString)
|
|
If CachedVersion Is Nothing Then Return Nothing
|
|
Return CachedVersion <= ParseLauncherVersion(VersionBaseName)
|
|
Catch ex As Exception
|
|
Logger.Warn(ex, "读取 PCL-ISE 更新缓存失败")
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Friend Sub UpdateCheckByButton()
|
|
If Interlocked.Exchange(UpdateCheckRunning, 1) <> 0 Then
|
|
Hint("正在检查更新,请稍候……")
|
|
Return
|
|
End If
|
|
RunInNewThread(
|
|
Sub()
|
|
Try
|
|
RunInUi(Sub() Hint("正在检查 PCL-ISE 更新……", Log:=False))
|
|
Dim Info = GetLatestLauncherUpdate()
|
|
If Not IsUpdateNewer(Info) Then
|
|
RunInUi(Sub() Hint("当前已经是最新版本!", 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,
|
|
"发现新版本", "下载并更新", "查看发布页", "取消",
|
|
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 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)
|
|
If DownloadedAssembly.Name <> "Plain Craft Launcher 2" OrElse DownloadedAssembly.Version <> Info.VersionNumber Then Throw New FormatException("更新文件的程序集版本与发布版本不符")
|
|
IsUpdateWaitingRestart = True
|
|
RunInUi(
|
|
Sub()
|
|
MyMsgBox("更新文件已下载并通过完整性校验。点击立即重启完成更新。", "准备更新", "立即重启", "稍后再说",
|
|
Button1Action:=Sub()
|
|
UpdateRestart(True)
|
|
End Sub)
|
|
End Sub)
|
|
Catch ex As Exception
|
|
Logger.Warn(ex, "检查 PCL-ISE 更新失败")
|
|
RunInUi(Sub() Hint("检查更新失败:" & ex.GetDisplay(False), HintType.Red))
|
|
Finally
|
|
Interlocked.Exchange(UpdateCheckRunning, 0)
|
|
End Try
|
|
End Sub, "PCL-ISE Update Check", ThreadPriority.BelowNormal)
|
|
End Sub
|
|
|
|
Public Sub UpdateRestart(TriggerRestartAndByEnd As Boolean)
|
|
Dim NewFile = UpdateFilePath
|
|
If String.IsNullOrWhiteSpace(NewFile) OrElse Not FileUtils.Exists(NewFile) Then
|
|
IsUpdateWaitingRestart = False
|
|
Return
|
|
End If
|
|
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})
|
|
IsUpdateWaitingRestart = False
|
|
If TriggerRestartAndByEnd Then FormMain.EndProgramForce()
|
|
Catch ex As Exception
|
|
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
|
|
Try
|
|
ReplaceLauncherFile(ProcessId, OldFileName, NewFileName)
|
|
Logger.Info("PCL-ISE 更新文件替换完成")
|
|
If TriggerRestart Then StartProcess(OldFileName)
|
|
Return True
|
|
Catch ex As Exception
|
|
Logger.Error(ex, "替换 PCL-ISE 更新文件失败")
|
|
MsgBox("PCL-ISE 更新失败:" & ex.GetDisplay(False), MsgBoxStyle.Critical, "更新失败")
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' 下载 InfstarMC 发布的启动器,用于整合包打包。
|
|
''' 如果不是,则下载一个。
|
|
''' </summary>
|
|
Friend Sub DownloadLatestPCL(Optional LoaderToSyncProgress As LoaderBase = Nothing)
|
|
Dim Target = PathTemp & "PCL-ISE-Latest.exe"
|
|
Dim Info = GetLatestLauncherUpdate()
|
|
Dim Checker As New FileChecker With {.MinSize = UpdateAssetMinSize, .ActualSize = Info.AssetSize, .Hash = Info.AssetHash}
|
|
NetDownloadByLoader(Info.DownloadUrl, Target, LoaderToSyncProgress, Checker)
|
|
End Sub
|
|
|
|
#End Region
|
|
|
|
#Region "联网配置"
|
|
|
|
''' <summary>
|
|
''' 联网获取的配置信息。
|
|
''' 若获取失败或仍在获取中,可能为 Nothing。
|
|
''' </summary>
|
|
Public ServerConfig As JObject
|
|
|
|
'联机模块仍使用原有配置接口;启动器更新通过“检查更新”按钮单独请求 Release API。
|
|
Public ServerLoader As New LoaderTask(Of Integer, Integer)("PCL 配置更新", Sub() Logger.Info("PCL-ISE 未启用远程联机配置"), Priority:=ThreadPriority.BelowNormal) With
|
|
{.ReloadTimeout = 1000 * 60 * 60} '超时 1 小时
|
|
|
|
#End Region
|
|
|
|
#Region "赞助等级"
|
|
|
|
Public ReadOnly Property CurrentRank As DonationRank
|
|
Get
|
|
Return DonationRank.None
|
|
End Get
|
|
End Property
|
|
|
|
Public Sub InputPotatoCode(IsUpdating As Boolean)
|
|
End Sub
|
|
Friend Sub GeneratePotatoCode()
|
|
End Sub
|
|
|
|
''' <summary>
|
|
''' 获取设备识别码。
|
|
''' </summary>
|
|
Friend Function GetIdentify() As String
|
|
Return "0000-0000-0000-0000"
|
|
End Function
|
|
|
|
#End Region
|
|
|
|
End Module
|