This commit is contained in:
龙腾猫跃
2023-08-15 16:38:52 +08:00
parent e1e084aea3
commit 79778490d5
21 changed files with 753 additions and 231 deletions
@@ -3,100 +3,206 @@
Private Sub Init() Handles Me.Loaded
PanBack.ScrollToHome()
PanLog.Visibility = If(ModeDebug, Visibility.Visible, Visibility.Collapsed)
'刷新主页
If ShouldRefresh Then
ShouldRefresh = False
RefreshCustom()
End If
'快照版提示
#If BETA Then
PanHint.Visibility = Visibility.Collapsed
#Else
PanHint.Title = "快照版提示"
PanHint.Visibility = If(ThemeCheckGold(), Visibility.Collapsed, Visibility.Visible)
LabHint1.Text = "快照版包含尚未在正式版发布的测试性功能,仅用于赞助者本人尝鲜。所以请不要发给其他人或者用于制作整合包哦!如果发现了 Bug,可以在 更多 → 反馈 中提交!"
LabHint2.Text = "你可以通过赞助¥23.33 档位换取解锁码来隐藏这个提示。"
LabHint1.Text = "快照版包含尚未正式发布的测试功能,仅用于赞助者本人尝鲜。请不要发给其他人或者用来制作整合包哦!"
LabHint2.Text = $"若已累积赞助¥23.33,在爱发电私信发送 {vbLQ}解锁码{vbRQ} 即可永久隐藏此提示。"
#End If
End Sub
Private LatestFileContent As String = ""
Private LatestFileLink As String = Nothing
Private IsRefreshing As Boolean = False
Public ShouldRefresh As Boolean = True
'暂时关闭快照版提示
#If Not BETA Then
Private Sub BtnHintClose_Click(sender As Object, e As EventArgs) Handles BtnHintClose.Click
AniDispose(PanHint, True)
End Sub
#End If
#Region "自定义主页"
''' <summary>
''' 刷新自定义的主页。返回是否成功。
''' 刷新自定义主页。
''' </summary>
Private Sub RefreshCustom()
If IsRefreshing Then
ShouldRefresh = True
Exit Sub
End If
Private Sub Refresh() Handles Me.Loaded
RunInNewThread(Sub()
Try
SyncLock RefreshLock
RefreshReal()
End SyncLock
Catch ex As Exception
Log(ex, "加载 PCL 主页自定义信息失败", LogLevel.Msgbox)
End Try
End Sub, $"刷新自定义主页 #{GetUuid()}")
End Sub
Private Sub RefreshReal()
Dim Content As String = ""
Dim Url As String
Select Case Setup.Get("UiCustomType")
Case 1
'加载本地文件
Log("[Page] 主页自定义数据来源:本地文件")
Content = ReadFile(Path & "PCL\Custom.xaml") 'ReadFile 会进行存在检测
Case 2
Url = Setup.Get("UiCustomNet")
Download:
'加载联网文件
If String.IsNullOrWhiteSpace(Url) Then Exit Select
If Url = Setup.Get("CacheSavedPageUrl") AndAlso File.Exists(PathTemp & "Cache\Custom.xaml") Then
'缓存可用
Log("[Page] 主页自定义数据来源:联网缓存文件")
Content = ReadFile(PathTemp & "Cache\Custom.xaml")
'后台更新缓存
OnlineLoader.Start(Url)
Else
'缓存不可用
Log("[Page] 主页自定义数据来源:联网全新下载")
Hint("正在加载主页……")
RunInUiWait(Sub() LoadContent("")) '在加载结束前清空页面
Setup.Set("CacheSavedPageVersion", "")
OnlineLoader.Start(Url) '下载完成后将会再次触发更新
Exit Sub
End If
Case 3
Select Case Setup.Get("UiCustomPreset")
Case 0 '你知道吗
Log("[Page] 主页预设:你知道吗")
Content = "
<local:MyCard Title=""你知道吗?"" Margin=""0,0,0,15"">
<TextBlock Margin=""25,38,23,15"" FontSize=""13.5"" IsHitTestVisible=""False"" Text=""{hint}"" TextWrapping=""Wrap"" Foreground=""{DynamicResource ColorBrush1}"" />
<local:MyIconButton Height=""22"" Width=""22"" Margin=""9"" VerticalAlignment=""Top"" HorizontalAlignment=""Right""
EventType=""刷新主页"" EventData=""/""
Logo=""M875.52 148.48C783.36 56.32 655.36 0 512 0 291.84 0 107.52 138.24 30.72 332.8l122.88 46.08C204.8 230.4 348.16 128 512 128c107.52 0 199.68 40.96 271.36 112.64L640 384h384V0L875.52 148.48zM512 896c-107.52 0-199.68-40.96-271.36-112.64L384 640H0v384l148.48-148.48C240.64 967.68 368.64 1024 512 1024c220.16 0 404.48-138.24 481.28-332.8L870.4 645.12C819.2 793.6 675.84 896 512 896z"" />
</local:MyCard>"
Case 1 '回声洞
Log("[Page] 主页预设:回声洞")
Content = "
<local:MyCard Title=""回声洞"" Margin=""0,0,0,15"">
<TextBlock Margin=""25,38,23,15"" FontSize=""13.5"" IsHitTestVisible=""False"" Text=""{cave}"" TextWrapping=""Wrap"" Foreground=""{DynamicResource ColorBrush1}"" />
<local:MyIconButton Height=""22"" Width=""22"" Margin=""9"" VerticalAlignment=""Top"" HorizontalAlignment=""Right""
EventType=""刷新主页"" EventData=""/""
Logo=""M875.52 148.48C783.36 56.32 655.36 0 512 0 291.84 0 107.52 138.24 30.72 332.8l122.88 46.08C204.8 230.4 348.16 128 512 128c107.52 0 199.68 40.96 271.36 112.64L640 384h384V0L875.52 148.48zM512 896c-107.52 0-199.68-40.96-271.36-112.64L384 640H0v384l148.48-148.48C240.64 967.68 368.64 1024 512 1024c220.16 0 404.48-138.24 481.28-332.8L870.4 645.12C819.2 793.6 675.84 896 512 896z"" />
</local:MyCard>"
Case 2 '新闻主页
Log("[Page] 主页预设:Minecraft 新闻")
Url = "https://news.bugjump.net"
GoTo Download
End Select
End Select
RunInUi(Sub() LoadContent(Content))
End Sub
Private RefreshLock As New Object
'联网获取自定义主页文件
Private OnlineLoader As New LoaderTask(Of String, Integer)("自定义主页获取", AddressOf OnlineLoaderSub) With {.ReloadTimeout = 10 * 60 * 1000}
Private Sub OnlineLoaderSub(Task As LoaderTask(Of String, Integer))
Try
IsRefreshing = True
PanCustom.Children.Clear()
RunInNewThread(Sub()
Dim FileContent As String = ""
Select Case Setup.Get("UiCustomType")
Case 0
'啥也不干
Case 1
'加载本地文件
FileContent = ReadFile(Path & "PCL\Custom.xaml") 'ReadFile 会进行存在检测
Log("[System] 尝试从本地文件读取主页自定义数据(" & FileContent.Length & ")")
Case 2
'加载联网文件
Try
Dim Link As String = Setup.Get("UiCustomNet")
If Link = LatestFileLink Then
FileContent = LatestFileContent
Log("[System] 尝试缓存加载主页自定义数据(" & FileContent.Length & ")")
ElseIf Not String.IsNullOrWhiteSpace(Link) Then
Log("[System] 开始从网络读取主页自定义数据(" & Link & ")")
FileContent = NetGetCodeByRequestRetry(Link)
Log("[System] 尝试从网络读取主页自定义数据(" & FileContent.Length & ")")
End If
LatestFileLink = Link
LatestFileContent = FileContent
Catch ex As Exception
Log(ex, "获取 PCL 主页自定义信息失败", LogLevel.Msgbox)
LatestFileLink = ""
End Try
End Select
If FileContent = "" Then
IsRefreshing = False
Exit Sub
End If
FileContent = "<StackPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:local=""clr-namespace:PCL;assembly=Plain Craft Launcher 2"">" & FileContent & "</StackPanel>"
FileContent = FileContent.Replace("{path}", Path)
RunInUi(Sub()
Try
Log("[System] 加载主页自定义数据")
PanCustom.Children.Add(GetObjectFromXML(FileContent))
IsRefreshing = False
Catch ex As Exception
IsRefreshing = False
Log("[System] 自定义信息内容:" & vbCrLf & FileContent)
If MyMsgBox(ex.Message, "加载自定义主页失败", "重试", "取消") = 1 Then
LatestFileLink = Nothing
LatestFileContent = ""
RefreshCustom()
End If
End Try
End Sub)
End Sub, "主页自定义刷新")
'获取版本校验地址
Dim VersionAddress As String
If Task.Input.Contains(".xaml") Then
VersionAddress = Task.Input.Replace(".xaml", ".xaml.ini")
Else
VersionAddress = Task.Input.Split("?").First
If Not VersionAddress.EndsWith("/") Then VersionAddress += "/"
VersionAddress += "version"
If Task.Input.Contains("?") Then VersionAddress += Task.Input.Split("?").Last
End If
'校验版本
Dim Version As String = ""
Dim NeedDownload As Boolean = True
Try
Version = NetGetCodeByRequestOnce(VersionAddress, Timeout:=10000)
If Version.Length > 100 Then Throw New Exception($"获取的自定义主页版本过长({Version.Length} 字符)")
Dim CurrentVersion As String = Setup.Get("CacheSavedPageVersion")
If Version <> "" AndAlso CurrentVersion <> "" AndAlso Version = CurrentVersion Then
Log($"[Page] 当前缓存的自定义主页已为最新,当前版本:{Version},检查源:{VersionAddress}")
NeedDownload = False
Else
Log($"[Page] 需要下载联网自定义主页,当前版本:{Version},检查源:{VersionAddress}")
End If
Catch exx As Exception
Log(exx, $"联网获取自定义主页版本失败", LogLevel.Developer)
Log($"[Page] 无法检查联网自定义主页版本,将直接下载,检查源:{VersionAddress}")
End Try
'实际下载
If NeedDownload Then
Dim FileContent As String = NetGetCodeByRequestRetry(Task.Input)
Log($"[Page] 已联网下载自定义主页,内容长度:{FileContent.Length},来源:{Task.Input}")
Setup.Set("CacheSavedPageUrl", Task.Input)
Setup.Set("CacheSavedPageVersion", Version)
WriteFile(PathTemp & "Cache\Custom.xaml", FileContent)
End If
'要求刷新
Refresh()
Catch ex As Exception
Log(ex, "加载 PCL 主页自定义信息失败", LogLevel.Msgbox)
IsRefreshing = False
Log(ex, $"联网下载自定义主页失败({Task.Input})", LogLevel.Hint)
Task.Input = ""
End Try
End Sub
Public Sub ForceRefresh(Optional ShowHint As Boolean = True)
ShouldRefresh = True
LatestFileContent = ""
LatestFileLink = Nothing
If ShowHint Then Hint("已刷新主页!", HintType.Finish)
FrmMain.PageChange(FormMain.PageType.Launch)
Init()
''' <summary>
''' 立即强制刷新自定义主页。
''' 必须在 UI 线程调用。
''' </summary>
Public Sub ForceRefresh()
Log("[Page] 要求强制刷新自定义主页")
ClearCache()
'实际的刷新
If FrmMain.PageCurrent.Page = FormMain.PageType.Launch Then
PanBack.ScrollToHome()
Refresh()
Else
FrmMain.PageChange(FormMain.PageType.Launch)
End If
End Sub
''' <summary>
''' 清空自定义主页缓存信息。
''' </summary>
Private Sub ClearCache()
LoadedContentHash = -1
OnlineLoader.Input = ""
Setup.Set("CacheSavedPageUrl", "")
Setup.Set("CacheSavedPageVersion", "")
Log("[Page] 已清空自定义主页缓存")
End Sub
''' <summary>
''' 从文本内容中加载自定义主页。
''' 必须在 UI 线程调用。
''' </summary>
Private Sub LoadContent(Content As String)
SyncLock LoadContentLock
'如果加载目标内容一致则不加载
Dim Hash = Content.GetHashCode()
If Hash = LoadedContentHash Then Exit Sub
LoadedContentHash = Hash
'实际加载内容
PanCustom.Children.Clear()
If String.IsNullOrWhiteSpace(Content) Then
Log($"[Page] 实例化:清空自定义主页 UI,来源为空")
Return
End If
Content = "<StackPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:local=""clr-namespace:PCL;assembly=Plain Craft Launcher 2"">" & Content & "</StackPanel>"
Content = Content.Replace("{path}", Path).Replace("{hint}", PageOtherTest.GetRandomHint()).Replace("{cave}", PageOtherTest.GetRandomCave())
Log($"[Page] 实例化:加载自定义主页 UI,最终内容长度:{Content.Count}")
Try
PanCustom.Children.Add(GetObjectFromXML(Content))
Catch ex As Exception
Log("[Page] 加载失败的自定义主页内容:" & vbCrLf & Content)
If MyMsgBox($"自定义主页内容编写有误,请根据下列错误信息进行检查:{vbCrLf}{ex.Message}", "加载自定义主页失败", "重试", "取消") = 1 Then
GoTo Refresh '防止 SyncLock 死锁
End If
End Try
End SyncLock
Return
Refresh:
ForceRefresh()
End Sub
Private LoadedContentHash As Integer = -1
Private LoadContentLock As New Object
#End Region
End Class