PageDownloadCompDetail.xaml.vb 19 KB

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