PageLaunchRight.xaml.vb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. Imports System.Windows.Threading
  2. Public Class PageLaunchRight
  3. Implements IRefreshable, IDispatcherUnhandledException
  4. Private Sub Init() Handles Me.Loaded
  5. PanBack.ScrollToHome()
  6. PanLog.Visibility = If(ModeDebug, Visibility.Visible, Visibility.Collapsed)
  7. '快照版提示
  8. #If BETA Then
  9. PanHint.Visibility = Visibility.Collapsed
  10. #Else
  11. PanHint.Visibility = If(ThemeCheckGold(), Visibility.Collapsed, Visibility.Visible)
  12. LabHint1.Text = "快照版包含尚未正式发布的测试功能,仅用于赞助者本人尝鲜。请不要发给其他人或者用来制作整合包哦!"
  13. LabHint2.Text = $"若已累积赞助¥23.33,在爱发电私信发送 {vbLQ}解锁码{vbRQ} 即可永久隐藏此提示。"
  14. #End If
  15. End Sub
  16. '暂时关闭快照版提示
  17. #If Not BETA Then
  18. Private Sub BtnHintClose_Click(sender As Object, e As EventArgs) Handles BtnHintClose.Click
  19. AniDispose(PanHint, True)
  20. End Sub
  21. #End If
  22. #Region "主页"
  23. ''' <summary>
  24. ''' 刷新主页。
  25. ''' </summary>
  26. Private Sub Refresh() Handles Me.Loaded
  27. RunInThread(
  28. Sub()
  29. Try
  30. SyncLock RefreshLock
  31. RefreshReal()
  32. End SyncLock
  33. Catch ex As Exception
  34. Log(ex, "加载 PCL 主页自定义信息失败", If(ModeDebug, LogLevel.Msgbox, LogLevel.Hint))
  35. End Try
  36. End Sub)
  37. End Sub
  38. Private Sub RefreshReal()
  39. Dim Content As String = Nothing, Url As String = Nothing
  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. '联网下载
  47. Url = Setup.Get("UiCustomNet")
  48. Case 3
  49. '预设
  50. Select Case Setup.Get("UiCustomPreset")
  51. Case 0
  52. Log("[Page] 主页预设:你知道吗")
  53. Content = "
  54. <local:MyCard Title=""你知道吗?"" Margin=""0,0,0,15"">
  55. <TextBlock Margin=""25,38,23,15"" FontSize=""13.5"" IsHitTestVisible=""False"" Text=""{hint}"" TextWrapping=""Wrap"" Foreground=""{DynamicResource ColorBrush1}"" />
  56. <local:MyIconButton Height=""22"" Width=""22"" Margin=""9"" VerticalAlignment=""Top"" HorizontalAlignment=""Right""
  57. EventType=""刷新主页"" EventData=""/""
  58. 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"" />
  59. </local:MyCard>"
  60. Case 1
  61. Log("[Page] 主页预设:回声洞")
  62. Content = "
  63. <local:MyCard Title=""回声洞"" Margin=""0,0,0,15"">
  64. <TextBlock Margin=""25,38,23,15"" FontSize=""13.5"" IsHitTestVisible=""False"" Text=""{cave}"" TextWrapping=""Wrap"" Foreground=""{DynamicResource ColorBrush1}"" />
  65. <local:MyIconButton Height=""22"" Width=""22"" Margin=""9"" VerticalAlignment=""Top"" HorizontalAlignment=""Right""
  66. EventType=""刷新主页"" EventData=""/""
  67. 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"" />
  68. </local:MyCard>"
  69. Case 2
  70. Log("[Page] 主页预设:Minecraft 新闻")
  71. Url = "https://mcnews.meloong.com"
  72. Case 3
  73. Log("[Page] 主页预设:简单主页")
  74. Url = "https://pclhomeplazaoss.lingyunawa.top:26994/d/Homepages/MFn233/Custom.xaml"
  75. Case 4
  76. Log("[Page] 主页预设:每日整合包推荐")
  77. Url = "https://pclsub.sodamc.com/"
  78. Case 5
  79. Log("[Page] 主页预设:Minecraft 皮肤推荐")
  80. Url = "https://forgepixel.com/pcl_sub_file"
  81. Case 6
  82. Log("[Page] 主页预设:OpenBMCLAPI 仪表盘 Lite")
  83. Url = "https://pcl-bmcl.milu.ink/"
  84. Case 7
  85. Log("[Page] 主页预设:主页市场")
  86. Url = "https://pclhomeplazaoss.lingyunawa.top:26994/d/Homepages/JingHai-Lingyun/Custom.xaml"
  87. Case 8
  88. Log("[Page] 主页预设:更新日志")
  89. Url = "https://pclhomeplazaoss.lingyunawa.top:26994/d/Homepages/Joker2184/UpdateHomepage.xaml"
  90. Case 9
  91. Log("[Page] 主页预设:PCL 新功能说明书")
  92. Url = "https://raw.gitcode.com/WForst-Breeze/WhatsNewPCL/raw/main/Custom.xaml"
  93. Case 10
  94. Log("[Page] 主页预设:OpenMCIM Dashboard")
  95. Url = "https://files.mcimirror.top/PCL"
  96. Case 11
  97. Log("[Page] 主页预设:杂志主页")
  98. Url = "https://pclhomeplazaoss.lingyunawa.top:26994/d/Homepages/Ext1nguisher/Custom.xaml"
  99. Case 12
  100. Log("[Page] 主页预设:PCL GitHub 仪表盘")
  101. Url = "https://ddf.pcl-community.org/Custom.xaml"
  102. End Select
  103. End Select
  104. '联网下载
  105. If Not String.IsNullOrWhiteSpace(Url) Then
  106. If Url = Setup.Get("CacheSavedPageUrl") AndAlso File.Exists(PathTemp & "Cache\Custom.xaml") Then
  107. '缓存可用
  108. Log("[Page] 主页自定义数据来源:联网缓存文件")
  109. Content = ReadFile(PathTemp & "Cache\Custom.xaml")
  110. '后台更新缓存
  111. OnlineLoader.Start(New Tuple(Of String, Boolean)(Url, False))
  112. Else
  113. '缓存不可用
  114. Log("[Page] 主页自定义数据来源:联网全新下载")
  115. Hint("正在加载主页……")
  116. RunInUiWait(Sub() LoadContent(Nothing)) '在加载结束前清空页面
  117. Setup.Set("CacheSavedPageVersion", "")
  118. OnlineLoader.Start(New Tuple(Of String, Boolean)(Url, True)) '下载完成后将会再次触发更新
  119. Return
  120. End If
  121. End If
  122. '同步到 UI
  123. RunInUi(Sub() LoadContent(Content))
  124. End Sub
  125. Private RefreshLock As New Object
  126. '联网获取主页文件
  127. Private OnlineLoader As New LoaderTask(Of Tuple(Of String, Boolean), Integer)("下载主页", AddressOf OnlineLoaderSub) With {.ReloadTimeout = 10 * 60 * 1000}
  128. Private Sub OnlineLoaderSub(Task As LoaderTask(Of Tuple(Of String, Boolean), Integer))
  129. Dim Address As String = Task.Input.Item1 '#3721 中连续触发两次导致内容变化
  130. Dim ShouldRefresh As Boolean = Task.Input.Item2
  131. Try
  132. '替换自定义变量与设置
  133. Address = ArgumentReplace(Address, AddressOf WebUtility.HtmlEncode)
  134. '获取版本校验地址
  135. Dim VersionAddress As String
  136. If Address.Contains(".xaml") Then
  137. VersionAddress = Address.Replace(".xaml", ".xaml.ini")
  138. Else
  139. VersionAddress = Address.BeforeFirst("?")
  140. If Not VersionAddress.EndsWith("/") Then VersionAddress += "/"
  141. VersionAddress += "version"
  142. If Address.Contains("?") Then VersionAddress += "?" & Address.AfterFirst("?")
  143. End If
  144. '校验版本
  145. Dim Version As String = ""
  146. Try
  147. Version = NetRequestByClientRetry(VersionAddress)
  148. If Version.Length > 1000 Then Throw New Exception($"获取的主页版本过长({Version.Length} 字符)")
  149. Dim CurrentVersion As String = Setup.Get("CacheSavedPageVersion")
  150. If Version <> "" AndAlso CurrentVersion <> "" AndAlso Version = CurrentVersion Then
  151. Log($"[Page] 当前缓存的主页已为最新,当前版本:{Version},检查源:{VersionAddress}")
  152. Return
  153. End If
  154. Log($"[Page] 需要下载联网主页,当前版本:{Version},检查源:{VersionAddress}")
  155. Catch exx As Exception
  156. Log(exx, $"联网获取主页版本失败", LogLevel.Developer)
  157. Log($"[Page] 无法检查联网主页版本,将直接下载,检查源:{VersionAddress}")
  158. End Try
  159. '实际下载
  160. Dim FileContent As String = NetRequestByClientRetry(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. '若内容变更则要求刷新
  166. If LoadedContentHash <> FileContent.GetHashCode() AndAlso ShouldRefresh Then Refresh()
  167. Catch ex As Exception
  168. Log(ex, $"下载主页失败({Address})", If(ModeDebug, LogLevel.Msgbox, LogLevel.Hint))
  169. End Try
  170. End Sub
  171. ''' <summary>
  172. ''' 立即强制刷新主页。
  173. ''' 必须在 UI 线程调用。
  174. ''' </summary>
  175. Public Sub ForceRefresh() Implements IRefreshable.Refresh
  176. Log("[Page] 要求强制刷新主页")
  177. ClearCache()
  178. '实际的刷新
  179. If FrmMain.PageCurrent.Page = FormMain.PageType.Launch Then
  180. PanBack.ScrollToHome()
  181. Refresh()
  182. Else
  183. FrmMain.PageChange(FormMain.PageType.Launch)
  184. End If
  185. End Sub
  186. ''' <summary>
  187. ''' 清空主页缓存信息。
  188. ''' </summary>
  189. Private Sub ClearCache()
  190. LoadedContentHash = -1
  191. OnlineLoader.Input = New Tuple(Of String, Boolean)("", True)
  192. Setup.Set("CacheSavedPageUrl", "")
  193. Setup.Set("CacheSavedPageVersion", "")
  194. Log("[Page] 已清空主页缓存")
  195. End Sub
  196. ''' <summary>
  197. ''' 从文本内容中加载主页。
  198. ''' 必须在 UI 线程调用。
  199. ''' </summary>
  200. Private Sub LoadContent(Content As String)
  201. Try
  202. SyncLock LoadContentLock
  203. '如果加载目标内容一致则不加载
  204. Dim Hash = If(Content, "").GetHashCode()
  205. If Hash = LoadedContentHash Then Return
  206. LoadedContentHash = Hash
  207. '实际加载内容
  208. PanCustom.Children.Clear()
  209. If String.IsNullOrWhiteSpace(Content) Then
  210. Log($"[Page] 实例化:清空主页 UI,来源为空")
  211. Return
  212. End If
  213. Dim LoadStartTime As Date = Date.Now
  214. '修改时应同时修改 PageOtherHelpDetail.Init
  215. Content = ArgumentReplace(Content, AddressOf EscapeXML)
  216. Do While Content.Contains("xmlns")
  217. Content = Content.RegexReplace("xmlns[^""']*(""|')[^""']*(""|')", "").Replace("xmlns", "") '禁止声明命名空间
  218. Loop
  219. Content = "<StackPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:sys=""clr-namespace:System;assembly=mscorlib"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:local=""clr-namespace:PCL;assembly=Plain Craft Launcher 2"">" & Content & "</StackPanel>"
  220. Log($"[Page] 实例化:加载主页 UI 开始,最终内容长度:{Content.Count}")
  221. PanCustom.Children.Add(GetObjectFromXML(Content))
  222. '加载计时
  223. Dim LoadCostTime = (Date.Now - LoadStartTime).Milliseconds
  224. Log($"[Page] 实例化:加载主页 UI 完成,耗时 {LoadCostTime}ms")
  225. If LoadCostTime > 3000 Then Hint($"主页加载过于缓慢(花费了 {Math.Round(LoadCostTime / 1000, 1)} 秒),请向主页作者反馈此问题,或暂时停止使用该主页")
  226. End SyncLock
  227. Catch ex As Exception
  228. Log(ex, "加载失败的主页内容:" & vbCrLf & Content)
  229. OnLoadContentFailed(ex)
  230. End Try
  231. End Sub
  232. ''' <summary>
  233. ''' 加载主页失败时调用。
  234. ''' </summary>
  235. Private Sub OnLoadContentFailed(ex As Exception)
  236. If ModeDebug OrElse Setup.Get("UiCustomType") = 1 Then
  237. Log(ex, "加载主页失败")
  238. If MyMsgBox(If(TypeOf ex Is UnauthorizedAccessException, ex.Message, $"主页内容编写有误,请根据下列错误信息进行检查:{vbCrLf}{ex.GetBrief}"),
  239. "加载主页失败", "重试", "取消") = 1 Then ForceRefresh()
  240. Else
  241. Log(ex, "加载主页失败", LogLevel.Hint)
  242. End If
  243. End Sub
  244. ''' <summary>
  245. ''' 捕获主页在 Measure 和 Arrange 阶段抛出的异常。
  246. ''' </summary>
  247. Private Sub DispatcherUnhandledException(sender As Object, e As DispatcherUnhandledExceptionEventArgs) Implements IDispatcherUnhandledException.DispatcherUnhandledException
  248. If TypeOf e.Exception IsNot Markup.XamlParseException Then Return
  249. e.Handled = True
  250. LoadContent(Nothing)
  251. OnLoadContentFailed(e.Exception)
  252. End Sub
  253. Private LoadedContentHash As Integer = -1
  254. Private LoadContentLock As New Object
  255. #End Region
  256. End Class