PageLaunchRight.xaml.vb 13 KB

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