PageLaunchRight.xaml.vb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. Public Class PageLaunchRight
  2. Private Sub Init() Handles Me.Loaded
  3. PanBack.ScrollToHome()
  4. PanScroll = PanBack '不知道为啥不能在 XAML 设置
  5. PanLog.Visibility = If(ModeDebug, Visibility.Visible, Visibility.Collapsed)
  6. '快照版提示
  7. #If BETA Then
  8. PanHint.Visibility = Visibility.Collapsed
  9. #Else
  10. PanHint.Visibility = If(ThemeCheckGold(), Visibility.Collapsed, Visibility.Visible)
  11. LabHint1.Text = "快照版包含尚未正式发布的测试功能,仅用于赞助者本人尝鲜。请不要发给其他人或者用来制作整合包哦!"
  12. LabHint2.Text = $"若已累积赞助¥23.33,在爱发电私信发送 {vbLQ}解锁码{vbRQ} 即可永久隐藏此提示。"
  13. #End If
  14. End Sub
  15. '暂时关闭快照版提示
  16. #If Not BETA Then
  17. Private Sub BtnHintClose_Click(sender As Object, e As EventArgs) Handles BtnHintClose.Click
  18. AniDispose(PanHint, True)
  19. End Sub
  20. #End If
  21. #Region "自定义主页"
  22. ''' <summary>
  23. ''' 刷新自定义主页。
  24. ''' </summary>
  25. Private Sub Refresh() Handles Me.Loaded
  26. RunInNewThread(
  27. Sub()
  28. Try
  29. SyncLock RefreshLock
  30. RefreshReal()
  31. End SyncLock
  32. Catch ex As Exception
  33. Log(ex, "加载 PCL 主页自定义信息失败", LogLevel.Msgbox)
  34. End Try
  35. End Sub, $"刷新自定义主页 #{GetUuid()}")
  36. End Sub
  37. Private Sub RefreshReal()
  38. Dim Content As String = ""
  39. Dim Url As String
  40. Select Case Setup.Get("UiCustomType")
  41. Case 1
  42. '加载本地文件
  43. Log("[Page] 主页自定义数据来源:本地文件")
  44. Content = ReadFile(Path & "PCL\Custom.xaml") 'ReadFile 会进行存在检测
  45. Case 2
  46. Url = Setup.Get("UiCustomNet")
  47. Download:
  48. '加载联网文件
  49. If String.IsNullOrWhiteSpace(Url) Then Exit Select
  50. If Url = Setup.Get("CacheSavedPageUrl") AndAlso File.Exists(PathTemp & "Cache\Custom.xaml") Then
  51. '缓存可用
  52. Log("[Page] 主页自定义数据来源:联网缓存文件")
  53. Content = ReadFile(PathTemp & "Cache\Custom.xaml")
  54. '后台更新缓存
  55. OnlineLoader.Start(Url)
  56. Else
  57. '缓存不可用
  58. Log("[Page] 主页自定义数据来源:联网全新下载")
  59. Hint("正在加载主页……")
  60. RunInUiWait(Sub() LoadContent("")) '在加载结束前清空页面
  61. Setup.Set("CacheSavedPageVersion", "")
  62. OnlineLoader.Start(Url) '下载完成后将会再次触发更新
  63. Exit Sub
  64. End If
  65. Case 3
  66. Select Case Setup.Get("UiCustomPreset")
  67. Case 0
  68. Log("[Page] 主页预设:你知道吗")
  69. Content = "
  70. <local:MyCard Title=""你知道吗?"" Margin=""0,0,0,15"">
  71. <TextBlock Margin=""25,38,23,15"" FontSize=""13.5"" IsHitTestVisible=""False"" Text=""{hint}"" TextWrapping=""Wrap"" Foreground=""{DynamicResource ColorBrush1}"" />
  72. <local:MyIconButton Height=""22"" Width=""22"" Margin=""9"" VerticalAlignment=""Top"" HorizontalAlignment=""Right""
  73. EventType=""刷新主页"" EventData=""/""
  74. 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"" />
  75. </local:MyCard>"
  76. Case 1
  77. Log("[Page] 主页预设:回声洞")
  78. Content = "
  79. <local:MyCard Title=""回声洞"" Margin=""0,0,0,15"">
  80. <TextBlock Margin=""25,38,23,15"" FontSize=""13.5"" IsHitTestVisible=""False"" Text=""{cave}"" TextWrapping=""Wrap"" Foreground=""{DynamicResource ColorBrush1}"" />
  81. <local:MyIconButton Height=""22"" Width=""22"" Margin=""9"" VerticalAlignment=""Top"" HorizontalAlignment=""Right""
  82. EventType=""刷新主页"" EventData=""/""
  83. 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"" />
  84. </local:MyCard>"
  85. Case 2
  86. Log("[Page] 主页预设:Minecraft 新闻")
  87. Url = "http://pcl.mcnews.thestack.top"
  88. GoTo Download
  89. Case 3
  90. Log("[Page] 主页预设:简单主页")
  91. Url = "https://gitee.com/mfn233/PCL-Mainpage/raw/main/Custom.xaml"
  92. GoTo Download
  93. Case 4
  94. Log("[Page] 主页预设:每日整合包推荐")
  95. Url = "https://pclsub.sodamc.com/"
  96. GoTo Download
  97. Case 5
  98. Log("[Page] 主页预设:Minecraft 皮肤推荐")
  99. Url = "https://forgepixel.com/pcl_sub_file"
  100. GoTo Download
  101. Case 6
  102. Log("[Page] 主页预设:OpenBMCLAPI 仪表盘 Lite")
  103. Url = "https://pcl-bmcl.milulu.xyz/"
  104. GoTo Download
  105. Case 7
  106. Log("[Page] 主页预设:主页市场")
  107. Url = "https://homepage-market.pages.dev/Custom.xaml"
  108. GoTo Download
  109. Case 8
  110. Log("[Page] 主页预设:更新日志")
  111. Url = "https://updatehomepage.pages.dev/UpdateHomepage.xaml"
  112. GoTo Download
  113. Case 9
  114. Log("[Page] 主页预设:PCL 新功能说明书")
  115. Url = "https://gitee.com/wforstbreeze/whats-new-pcl/raw/main/Custom.xaml"
  116. GoTo Download
  117. Case 10
  118. Log("[Page] 主页预设:OpenMCIM Dashboard")
  119. Url = "https://files.mcimirror.top/PCL"
  120. GoTo Download
  121. End Select
  122. End Select
  123. RunInUi(Sub() LoadContent(Content))
  124. End Sub
  125. Private RefreshLock As New Object
  126. '联网获取自定义主页文件
  127. Private OnlineLoader As New LoaderTask(Of String, Integer)("自定义主页获取", AddressOf OnlineLoaderSub) With {.ReloadTimeout = 10 * 60 * 1000}
  128. Private Sub OnlineLoaderSub(Task As LoaderTask(Of String, Integer))
  129. Dim Address As String = Task.Input '#3721 中连续触发两次导致内容变化
  130. Try
  131. '获取版本校验地址
  132. Dim VersionAddress As String
  133. If Address.Contains(".xaml") Then
  134. VersionAddress = Address.Replace(".xaml", ".xaml.ini")
  135. Else
  136. VersionAddress = Address.Before("?")
  137. If Not VersionAddress.EndsWith("/") Then VersionAddress += "/"
  138. VersionAddress += "version"
  139. If Address.Contains("?") Then VersionAddress += Address.After("?")
  140. End If
  141. '校验版本
  142. Dim Version As String = ""
  143. Dim NeedDownload As Boolean = True
  144. Try
  145. Version = NetGetCodeByRequestOnce(VersionAddress, Timeout:=10000)
  146. If Version.Length > 1000 Then Throw New Exception($"获取的自定义主页版本过长({Version.Length} 字符)")
  147. Dim CurrentVersion As String = Setup.Get("CacheSavedPageVersion")
  148. If Version <> "" AndAlso CurrentVersion <> "" AndAlso Version = CurrentVersion Then
  149. Log($"[Page] 当前缓存的自定义主页已为最新,当前版本:{Version},检查源:{VersionAddress}")
  150. NeedDownload = False
  151. Else
  152. Log($"[Page] 需要下载联网自定义主页,当前版本:{Version},检查源:{VersionAddress}")
  153. End If
  154. Catch exx As Exception
  155. Log(exx, $"联网获取自定义主页版本失败", LogLevel.Developer)
  156. Log($"[Page] 无法检查联网自定义主页版本,将直接下载,检查源:{VersionAddress}")
  157. End Try
  158. '实际下载
  159. If NeedDownload Then
  160. Dim FileContent As String = NetGetCodeByRequestRetry(Address)
  161. Log($"[Page] 已联网下载自定义主页,内容长度:{FileContent.Length},来源:{Address}")
  162. Setup.Set("CacheSavedPageUrl", Address)
  163. Setup.Set("CacheSavedPageVersion", Version)
  164. WriteFile(PathTemp & "Cache\Custom.xaml", FileContent)
  165. End If
  166. '要求刷新
  167. Refresh()
  168. Catch ex As Exception
  169. If Setup.Get("CacheSavedPageVersion") = "" Then
  170. Log(ex, $"联网下载自定义主页失败({Address})", LogLevel.Msgbox)
  171. Else
  172. Log(ex, $"联网下载自定义主页失败({Address})")
  173. End If
  174. End Try
  175. End Sub
  176. ''' <summary>
  177. ''' 立即强制刷新自定义主页。
  178. ''' 必须在 UI 线程调用。
  179. ''' </summary>
  180. Public Sub ForceRefresh()
  181. Log("[Page] 要求强制刷新自定义主页")
  182. ClearCache()
  183. '实际的刷新
  184. If FrmMain.PageCurrent.Page = FormMain.PageType.Launch Then
  185. PanBack.ScrollToHome()
  186. Refresh()
  187. Else
  188. FrmMain.PageChange(FormMain.PageType.Launch)
  189. End If
  190. End Sub
  191. ''' <summary>
  192. ''' 清空自定义主页缓存信息。
  193. ''' </summary>
  194. Private Sub ClearCache()
  195. LoadedContentHash = -1
  196. OnlineLoader.Input = ""
  197. Setup.Set("CacheSavedPageUrl", "")
  198. Setup.Set("CacheSavedPageVersion", "")
  199. Log("[Page] 已清空自定义主页缓存")
  200. End Sub
  201. ''' <summary>
  202. ''' 从文本内容中加载自定义主页。
  203. ''' 必须在 UI 线程调用。
  204. ''' </summary>
  205. Private Sub LoadContent(Content As String)
  206. SyncLock LoadContentLock
  207. '如果加载目标内容一致则不加载
  208. Dim Hash = Content.GetHashCode()
  209. If Hash = LoadedContentHash Then Exit Sub
  210. LoadedContentHash = Hash
  211. '实际加载内容
  212. PanCustom.Children.Clear()
  213. If String.IsNullOrWhiteSpace(Content) Then
  214. Log($"[Page] 实例化:清空自定义主页 UI,来源为空")
  215. Return
  216. End If
  217. 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>"
  218. Content = HelpArgumentReplace(Content)
  219. Log($"[Page] 实例化:加载自定义主页 UI 开始,最终内容长度:{Content.Count}")
  220. Try
  221. PanCustom.Children.Add(GetObjectFromXML(Content))
  222. Catch ex As Exception
  223. Log("[Page] 加载失败的自定义主页内容:" & vbCrLf & Content)
  224. If MyMsgBox($"自定义主页内容编写有误,请根据下列错误信息进行检查:{vbCrLf}{ex.Message}", "加载自定义主页失败", "重试", "取消") = 1 Then
  225. GoTo Refresh '防止 SyncLock 死锁
  226. End If
  227. End Try
  228. Log($"[Page] 实例化:加载自定义主页 UI 完成")
  229. End SyncLock
  230. Return
  231. Refresh:
  232. ForceRefresh()
  233. End Sub
  234. Private LoadedContentHash As Integer = -1
  235. Private LoadContentLock As New Object
  236. #End Region
  237. End Class