PageDownloadCompDetail.xaml.vb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. Public Class PageDownloadCompDetail
  2. Private CompItem As MyCompItem = Nothing
  3. Private Project As CompProject
  4. #Region "加载器"
  5. Private CompFileLoader As New LoaderTask(Of Integer, List(Of CompFile))(
  6. "Comp File", Sub(Task As LoaderTask(Of Integer, List(Of CompFile))) Task.Output = CompFilesGet(Project.Id, Project.FromCurseForge))
  7. '初始化加载器信息
  8. Private Sub PageDownloadCompDetail_Inited(sender As Object, e As EventArgs) Handles Me.Initialized
  9. Project = FrmMain.PageCurrent.Additional(0)
  10. PageLoaderInit(Load, PanLoad, PanMain, CardIntro, CompFileLoader, AddressOf Load_OnFinish)
  11. End Sub
  12. '自动重试
  13. Private Sub Load_State(sender As Object, state As MyLoading.MyLoadingState, oldState As MyLoading.MyLoadingState) Handles Load.StateChanged
  14. Select Case CompFileLoader.State
  15. Case LoadState.Failed
  16. Dim ErrorMessage As String = ""
  17. If CompFileLoader.Error IsNot Nothing Then ErrorMessage = CompFileLoader.Error.Message
  18. If ErrorMessage.Contains("不是有效的 json 文件") Then
  19. Log("[Comp] 下载的文件 json 列表损坏,已自动重试", LogLevel.Debug)
  20. PageLoaderRestart()
  21. End If
  22. End Select
  23. End Sub
  24. '结果 UI 化
  25. Private Class VersionSorterWithSelect
  26. Implements IComparer(Of String)
  27. Public Top As String = ""
  28. Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
  29. If x = y Then Return 0
  30. If x = Top Then Return -1
  31. If y = Top Then Return 1
  32. Return -VersionSortInteger(x, y)
  33. End Function
  34. Public Sub New(Optional Top As String = "")
  35. Me.Top = If(Top, "")
  36. End Sub
  37. End Class
  38. Private Sub Load_OnFinish()
  39. '获取目标版本与 Mod 加载器
  40. Dim TargetVersion As String
  41. Select Case Project.Type
  42. Case CompType.Mod
  43. TargetVersion = If(PageDownloadMod.Loader.Input.GameVersion, "")
  44. Case CompType.ModPack
  45. TargetVersion = If(PageDownloadPack.Loader.Input.GameVersion, "")
  46. Case Else 'CompType.ResourcePack
  47. 'FUTURE: Res
  48. TargetVersion = "" 'If(PageDownloadResource.Loader.Input.GameVersion, "")
  49. End Select
  50. Dim TargetLoader = CompModLoaderType.Any
  51. If Project.Type = CompType.Mod AndAlso PageDownloadMod.Loader.Input.ModLoader <> CompModLoaderType.Any Then
  52. TargetLoader = PageDownloadMod.Loader.Input.ModLoader
  53. End If
  54. Dim TargetCardName As String = If(TargetVersion <> "" OrElse TargetLoader <> CompModLoaderType.Any,
  55. $"目标版本:{TargetVersion} {If(TargetLoader <> CompModLoaderType.Any, TargetLoader, "")}", "")
  56. '初始化字典
  57. Dim Dict As New SortedDictionary(Of String, List(Of CompFile))(New VersionSorterWithSelect(TargetCardName))
  58. Dict.Add("未知版本", New List(Of CompFile))
  59. For Each Version As CompFile In CompFileLoader.Output
  60. For Each GameVersion In Version.GameVersions
  61. '决定添加到哪个版本
  62. Dim TargetCard As String
  63. If GameVersion Is Nothing Then
  64. TargetCard = "未知版本"
  65. ElseIf GameVersion.Contains("w") OrElse GameVersion.Contains("pre") OrElse GameVersion.Contains("rc") Then
  66. TargetCard = "快照版本"
  67. ElseIf GameVersion.Split(".").Count >= 2 Then
  68. TargetCard = GameVersion
  69. Else
  70. TargetCard = "未知版本"
  71. End If
  72. '实际进行添加
  73. If Not Dict.ContainsKey(TargetCard) Then Dict.Add(TargetCard, New List(Of CompFile))
  74. If Not Dict(TargetCard).Contains(Version) Then Dict(TargetCard).Add(Version)
  75. Next
  76. Next
  77. '添加筛选的版本的卡片
  78. If TargetCardName <> "" Then
  79. Dict.Add(TargetCardName, New List(Of CompFile))
  80. For Each Version As CompFile In CompFileLoader.Output
  81. If Version.GameVersions.Contains(TargetVersion) AndAlso
  82. (TargetLoader = CompModLoaderType.Any OrElse Version.ModLoaders.Contains(TargetLoader)) Then
  83. If Not Dict(TargetCardName).Contains(Version) Then Dict(TargetCardName).Add(Version)
  84. End If
  85. Next
  86. End If
  87. #Region "转化为 UI"
  88. Try
  89. '清空当前
  90. PanMain.Children.Clear()
  91. '转化为 UI
  92. For Each Pair As KeyValuePair(Of String, List(Of CompFile)) In Dict
  93. If Pair.Value.Count = 0 Then Continue For
  94. '增加卡片
  95. Dim NewCard As New MyCard With {.Title = Pair.Key, .Margin = New Thickness(0, 0, 0, 15), .SwapType = If(Project.Type = CompType.ModPack, 9, 8)} 'FUTURE: Res
  96. Dim NewStack As New StackPanel With {.Margin = New Thickness(20, MyCard.SwapedHeight, 18, 0), .VerticalAlignment = VerticalAlignment.Top, .RenderTransform = New TranslateTransform(0, 0), .Tag = Pair.Value}
  97. NewCard.Children.Add(NewStack)
  98. NewCard.SwapControl = NewStack
  99. PanMain.Children.Add(NewCard)
  100. '确定卡片是否展开
  101. If Pair.Key = TargetCardName OrElse
  102. (FrmMain.PageCurrent.Additional IsNot Nothing AndAlso '#2761
  103. CType(FrmMain.PageCurrent.Additional(1), List(Of String)).Contains(NewCard.Title)) Then
  104. MyCard.StackInstall(NewStack, If(Project.Type = CompType.ModPack, 9, 8), Pair.Key) 'FUTURE: Res
  105. Else
  106. NewCard.IsSwaped = True
  107. End If
  108. '增加提示
  109. If Pair.Key = "未知版本" Then
  110. NewStack.Children.Add(New MyHint With {.Text = "由于 API 的版本信息更新缓慢,可能无法识别刚更新不久的 MC 版本,只需等待几天即可自动恢复正常。", .IsWarn = False, .Margin = New Thickness(0, 0, 0, 7)})
  111. End If
  112. Next
  113. '如果只有一张卡片,展开第一张卡片
  114. If PanMain.Children.Count = 1 Then
  115. CType(PanMain.Children(0), MyCard).IsSwaped = False
  116. End If
  117. Catch ex As Exception
  118. Log(ex, "可视化工程下载列表出错", LogLevel.Feedback)
  119. End Try
  120. #End Region
  121. End Sub
  122. #End Region
  123. Private IsFirstInit As Boolean = True
  124. Public Sub Init() Handles Me.PageEnter
  125. AniControlEnabled += 1
  126. Project = FrmMain.PageCurrent.Additional(0)
  127. PanBack.ScrollToHome()
  128. '重启加载器
  129. If IsFirstInit Then
  130. '在 Me.Initialized 已经初始化了加载器,不再重复初始化
  131. IsFirstInit = False
  132. Else
  133. PageLoaderRestart(IsForceRestart:=True)
  134. End If
  135. '放置当前工程
  136. If CompItem IsNot Nothing Then PanIntro.Children.Remove(CompItem)
  137. CompItem = Project.ToCompItem(True, True)
  138. CompItem.IsHitTestVisible = False
  139. CompItem.HasAnimation = False
  140. CompItem.Margin = New Thickness(-7, -7, 0, 8)
  141. PanIntro.Children.Insert(0, CompItem)
  142. '决定按钮显示
  143. BtnIntroWeb.Text = If(Project.FromCurseForge, "转到 CurseForge", "转到 Modrinth")
  144. BtnIntroWiki.Visibility = If(Project.WikiId = 0, Visibility.Collapsed, Visibility.Visible)
  145. BtnIntroMCBBS.Visibility = If(Project.MCBBS = 0, Visibility.Collapsed, Visibility.Visible)
  146. AniControlEnabled -= 1
  147. End Sub
  148. '整合包下载(安装)
  149. Public Sub Install_Click(sender As MyListItem, e As EventArgs)
  150. Try
  151. '获取基本信息
  152. Dim File As CompFile = sender.Tag
  153. Dim LoaderName As String = $"{If(Project.FromCurseForge, "CurseForge", "Modrinth")} 整合包下载:{Project.TranslatedName} "
  154. '获取版本名
  155. Dim PackName As String = Project.TranslatedName.Replace(".zip", "").Replace(".rar", "").Replace(".mrpack", "").Replace("\", "\").Replace("/", "/").Replace("|", "|").Replace(":", ":").Replace("<", "<").Replace(">", ">").Replace("*", "*").Replace("?", "?").Replace("""", "").Replace(": ", ":")
  156. Dim Validate As New ValidateFolderName(PathMcFolder & "versions")
  157. If Validate.Validate(PackName) <> "" Then PackName = ""
  158. Dim VersionName As String = MyMsgBoxInput("输入版本名称", "", PackName, New ObjectModel.Collection(Of Validate) From {Validate})
  159. If String.IsNullOrEmpty(VersionName) Then Exit Sub
  160. '构造步骤加载器
  161. Dim Loaders As New List(Of LoaderBase)
  162. Dim Target As String = $"{PathMcFolder}versions\{VersionName}\原始整合包.{If(Project.FromCurseForge, "zip", "mrpack")}"
  163. Dim LogoFileAddress As String = PathTemp & "CompLogo\" & GetHash(CompItem.Logo) & ".png"
  164. Loaders.Add(New LoaderDownload("下载整合包文件", New List(Of NetFile) From {File.ToNetFile(Target)}) With {.ProgressWeight = 10, .Block = True})
  165. Loaders.Add(New LoaderTask(Of Integer, Integer)("准备安装整合包",
  166. Sub()
  167. If Not ModpackInstall(Target, VersionName, Logo:=If(IO.File.Exists(LogoFileAddress), LogoFileAddress, Nothing)) Then
  168. Throw New Exception("整合包安装出现异常!")
  169. End If
  170. End Sub) With {.ProgressWeight = 0.1})
  171. '启动
  172. Dim Loader As New LoaderCombo(Of String)(LoaderName, Loaders) With {.OnStateChanged = Sub(MyLoader)
  173. Select Case MyLoader.State
  174. Case LoadState.Failed
  175. Hint(MyLoader.Name & "失败:" & GetExceptionSummary(MyLoader.Error), HintType.Critical)
  176. Case LoadState.Aborted
  177. Hint(MyLoader.Name & "已取消!", HintType.Info)
  178. Case LoadState.Loading
  179. Exit Sub '不重新加载版本列表
  180. End Select
  181. McInstallFailedClearFolder(MyLoader)
  182. End Sub}
  183. Loader.Start(PathMcFolder & "versions\" & VersionName & "\")
  184. LoaderTaskbarAdd(Loader)
  185. FrmMain.BtnExtraDownload.ShowRefresh()
  186. FrmMain.BtnExtraDownload.Ribble()
  187. Catch ex As Exception
  188. Log(ex, "下载资源整合包失败", LogLevel.Feedback)
  189. End Try
  190. End Sub
  191. 'Mod 下载;整合包另存为
  192. Public Shared CachedFolder As String = Nothing '仅在本次缓存的下载文件夹
  193. Public Sub Save_Click(sender As Object, e As EventArgs)
  194. Dim File As CompFile = If(TypeOf sender Is MyListItem, sender, sender.Parent).Tag
  195. RunInNewThread(
  196. Sub()
  197. Try
  198. Dim Desc As String = If(Project.Type = CompType.ModPack, "整合包", If(Project.Type = CompType.Mod, "Mod ", "资源包"))
  199. '确认默认保存位置
  200. Dim DefaultFolder As String = Nothing
  201. If Project.Type = CompType.Mod Then
  202. '获取 Mod 所需的加载器种类
  203. Dim AllowForge As Boolean? = Nothing, AllowFabric As Boolean? = Nothing
  204. If File.ModLoaders.Count > 0 Then '从文件中获取
  205. AllowForge = File.ModLoaders.Contains(CompModLoaderType.Forge)
  206. AllowFabric = File.ModLoaders.Contains(CompModLoaderType.Fabric)
  207. ElseIf Project.ModLoaders.Count > 0 Then '从工程中获取
  208. AllowForge = Project.ModLoaders.Contains(CompModLoaderType.Forge)
  209. AllowFabric = Project.ModLoaders.Contains(CompModLoaderType.Fabric)
  210. End If
  211. If AllowForge IsNot Nothing AndAlso Not AllowForge AndAlso
  212. AllowFabric IsNot Nothing AndAlso Not AllowFabric Then
  213. AllowForge = Nothing : AllowFabric = Nothing
  214. End If
  215. Log("[Comp] 允许 Forge:" & If(AllowForge, "未知") & ",允许 Fabric:" & If(AllowFabric, "未知"))
  216. '判断某个版本是否符合要求
  217. Dim IsVersionSuitable As Func(Of McVersion, Boolean) =
  218. Function(Version)
  219. If Not Version.IsLoaded Then Version.Load()
  220. If Not Version.Modable Then Return False
  221. If File.GameVersions.Any(Function(v) v.Contains(".")) AndAlso
  222. Not File.GameVersions.Any(Function(v) v.Contains(".") AndAlso v.Split(".")(1) = Version.Version.McCodeMain.ToString) Then Return False
  223. If AllowForge Is Nothing OrElse AllowFabric Is Nothing Then Return True
  224. If AllowForge AndAlso Version.Version.HasForge Then Return True
  225. If AllowFabric AndAlso Version.Version.HasFabric Then Return True
  226. Return False
  227. End Function
  228. '获取 Mod 默认下载位置
  229. If CachedFolder IsNot Nothing Then
  230. DefaultFolder = CachedFolder
  231. Log("[Comp] 使用上次下载时的文件夹作为默认下载位置")
  232. ElseIf McVersionCurrent IsNot Nothing AndAlso IsVersionSuitable(McVersionCurrent) Then
  233. DefaultFolder = McVersionCurrent.PathIndie & "mods\"
  234. Directory.CreateDirectory(DefaultFolder)
  235. Log("[Comp] 使用当前版本的 mods 文件夹作为默认下载位置(" & McVersionCurrent.Name & ")")
  236. Else
  237. Dim NeedLoad As Boolean = McVersionListLoader.State <> LoadState.Finished
  238. If NeedLoad Then
  239. Hint("正在查找适合的游戏版本……")
  240. LoaderFolderRun(McVersionListLoader, PathMcFolder, LoaderFolderRunType.ForceRun, MaxDepth:=1, ExtraPath:="versions\", WaitForExit:=True)
  241. End If
  242. Dim SuitableVersions As New List(Of McVersion)
  243. For Each Version As McVersion In McVersionList.Values.SelectMany(Function(l) l)
  244. If IsVersionSuitable(Version) Then SuitableVersions.Add(Version)
  245. Next
  246. If SuitableVersions.Count = 0 Then
  247. DefaultFolder = PathMcFolder
  248. If NeedLoad Then
  249. Hint("当前 MC 文件夹中没有找到适合这个 Mod 的版本!")
  250. Else
  251. Log("[Comp] 由于当前版本不兼容,使用当前的 MC 文件夹作为默认下载位置")
  252. End If
  253. Else '选择 Mod 数量最多的版本
  254. Dim SelectedVersion = SuitableVersions.OrderBy(Function(v)
  255. Dim Info As New DirectoryInfo(v.PathIndie & "mods\")
  256. Return If(Info.Exists, Info.GetFiles().Length, -1)
  257. End Function).LastOrDefault()
  258. DefaultFolder = SelectedVersion.PathIndie & "mods\"
  259. Directory.CreateDirectory(DefaultFolder)
  260. Log("[Comp] 使用适合的游戏版本作为默认下载位置(" & SelectedVersion.Name & ")")
  261. End If
  262. End If
  263. End If
  264. '获取基本信息
  265. Dim FileName As String
  266. If Project.TranslatedName = Project.RawName Then
  267. FileName = File.FileName
  268. Else
  269. Dim ChineseName As String = Project.TranslatedName.Split(" (").First.Split(" - ").First.
  270. Replace("\", "\").Replace("/", "/").Replace("|", "|").Replace(":", ":").Replace("<", "<").Replace(">", ">").Replace("*", "*").Replace("?", "?").Replace("""", "").Replace(": ", ":")
  271. Select Case Setup.Get("ToolDownloadTranslate")
  272. Case 0
  273. FileName = $"[{ChineseName}] {File.FileName}"
  274. Case 1
  275. FileName = $"{ChineseName}-{File.FileName}"
  276. Case 2
  277. FileName = $"{File.FileName}-{ChineseName}"
  278. Case Else
  279. FileName = File.FileName
  280. End Select
  281. End If
  282. RunInUi(
  283. Sub()
  284. '弹窗要求选择保存位置
  285. Dim Target As String
  286. Target = SelectAs("选择保存位置", FileName,
  287. Desc & "文件|" &
  288. If(Project.Type = CompType.Mod,
  289. If(File.FileName.EndsWith(".litemod"), "*.litemod", "*.jar"),
  290. If(File.FileName.EndsWith(".mrpack"), "*.mrpack", "*.zip")), DefaultFolder)
  291. If Not Target.Contains("\") Then Exit Sub
  292. '构造步骤加载器
  293. Dim LoaderName As String = Desc & "下载:" & File.DisplayName & " "
  294. If Target <> DefaultFolder AndAlso Project.Type = CompType.Mod Then CachedFolder = GetPathFromFullPath(Target)
  295. Dim Loaders As New List(Of LoaderBase)
  296. Loaders.Add(New LoaderDownload("下载文件", New List(Of NetFile) From {File.ToNetFile(Target)}) With {.ProgressWeight = 6, .Block = True})
  297. '启动
  298. Dim Loader As New LoaderCombo(Of Integer)(LoaderName, Loaders) With {.OnStateChanged = AddressOf DownloadStateSave}
  299. Loader.Start(1)
  300. LoaderTaskbarAdd(Loader)
  301. FrmMain.BtnExtraDownload.ShowRefresh()
  302. FrmMain.BtnExtraDownload.Ribble()
  303. End Sub)
  304. Catch ex As Exception
  305. Log(ex, "保存资源文件失败", LogLevel.Feedback)
  306. End Try
  307. End Sub, "Download CompDetail Save")
  308. End Sub
  309. Private Sub BtnIntroWeb_Click(sender As Object, e As EventArgs) Handles BtnIntroWeb.Click
  310. OpenWebsite(Project.Website)
  311. End Sub
  312. Private Sub BtnIntroWiki_Click(sender As Object, e As EventArgs) Handles BtnIntroWiki.Click
  313. OpenWebsite("https://www.mcmod.cn/class/" & Project.WikiId & ".html")
  314. End Sub
  315. Private Sub BtnIntroMCBBS_Click(sender As Object, e As EventArgs) Handles BtnIntroMCBBS.Click
  316. OpenWebsite("https://www.mcbbs.net/thread-" & Project.MCBBS & "-1-1.html")
  317. End Sub
  318. Private Sub BtnIntroCopy_Click(sender As Object, e As EventArgs) Handles BtnIntroCopy.Click
  319. ClipboardSet(CompItem.LabTitle.Text)
  320. End Sub
  321. End Class