ModDownload.vb 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. Public Module ModDownload
  2. #Region "DlClient* | Minecraft 客户端"
  3. ''' <summary>
  4. ''' 返回某 Minecraft 版本对应的原版主 Jar 文件的下载信息,要求对应依赖版本已存在。
  5. ''' 失败则抛出异常,不需要下载则返回 Nothing。
  6. ''' </summary>
  7. Public Function DlClientJarGet(Instance As McInstance, ReturnNothingOnFileUseable As Boolean) As NetFile
  8. '获取底层继承版本
  9. Try
  10. Do While Not String.IsNullOrEmpty(Instance.InheritName)
  11. Instance = New McInstance(Instance.InheritName)
  12. Loop
  13. Catch ex As Exception
  14. Logger.Warn(ex, "获取底层继承版本失败")
  15. End Try
  16. '检查 Json 是否标准
  17. If Instance.JsonObject("downloads") Is Nothing OrElse Instance.JsonObject("downloads")("client") Is Nothing OrElse Instance.JsonObject("downloads")("client")("url") Is Nothing Then
  18. Throw New Exception("底层版本 " & Instance.Name & " 中无 jar 文件下载信息")
  19. End If
  20. '检查文件
  21. Dim Checker As New FileChecker With {.MinSize = 1024, .ActualSize = If(Instance.JsonObject("downloads")("client")("size"), -1), .Hash = Instance.JsonObject("downloads")("client")("sha1")}
  22. If ReturnNothingOnFileUseable AndAlso Checker.Check(Instance.PathVersion & Instance.Name & ".jar") Is Nothing Then Return Nothing '通过校验
  23. '返回下载信息
  24. Dim JarUrl As String = Instance.JsonObject("downloads")("client")("url")
  25. Return New NetFile(DlSourceLauncherOrMetaGet(JarUrl), Instance.PathVersion & Instance.Name & ".jar", Checker)
  26. End Function
  27. ''' <summary>
  28. ''' 返回某 Minecraft 版本对应的原版主 AssetIndex 文件的下载信息,要求对应依赖版本已存在。
  29. ''' 若未找到,则会返回 Legacy 资源文件或 Nothing。
  30. ''' </summary>
  31. Public Function DlClientAssetIndexGet(Instance As McInstance) As NetFile
  32. '获取底层继承版本
  33. Do While Not String.IsNullOrEmpty(Instance.InheritName)
  34. Instance = New McInstance(Instance.InheritName)
  35. Loop
  36. '获取信息
  37. Dim IndexInfo = McAssetsGetIndex(Instance, True, True)
  38. Dim IndexAddress As String = McFolderSelected & "assets\indexes\" & IndexInfo("id").ToString & ".json"
  39. Logger.Info($"版本 {Instance.Name} 对应的资源文件索引为 {IndexInfo("id")}")
  40. Dim IndexUrl As String = If(IndexInfo("url"), "")
  41. If IndexUrl = "" Then
  42. Return Nothing
  43. Else
  44. Return New NetFile(DlSourceLauncherOrMetaGet(IndexUrl), IndexAddress, New FileChecker With {.IsJson = True})
  45. End If
  46. End Function
  47. ''' <summary>
  48. ''' 构造补全某 Minecraft 版本的所有文件的加载器列表。失败会抛出异常。
  49. ''' </summary>
  50. Public Function DlClientFix(Instance As McInstance, CheckAssetsHash As Boolean, DownloadAssetIndexInBackground As Boolean) As List(Of LoaderBase)
  51. Dim Loaders As New List(Of LoaderBase)
  52. #Region "下载支持库文件"
  53. Dim LoadersLib As New List(Of LoaderBase) From {
  54. New LoaderTask(Of String, List(Of NetFile))("分析缺失支持库文件", Sub(Task As LoaderTask(Of String, List(Of NetFile))) Task.Output = McLibNetFilesFromInstance(Instance)) With {.ProgressWeight = 1},
  55. New LoaderDownload("下载支持库文件", New List(Of NetFile)) With {.ProgressWeight = 15}
  56. }
  57. '构造加载器
  58. Loaders.Add(New LoaderCombo(Of String)("下载支持库文件(主加载器)", LoadersLib) With {.Block = False, .Show = False, .ProgressWeight = 16})
  59. #End Region
  60. #Region "下载资源文件"
  61. If ShouldIgnoreFileCheck(Instance) Then
  62. Logger.Warn("设置要求跳过所有 Assets 检查,文件可能不是最新的", LogBehavior.None)
  63. Else
  64. Dim LoadersAssets As New List(Of LoaderBase)
  65. '获取资源文件索引地址
  66. Dim TempAddressMain As String = Nothing
  67. Dim RealAddressMain As String = Nothing
  68. LoadersAssets.Add(New LoaderTask(Of String, List(Of NetFile))("分析资源文件索引地址",
  69. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  70. Try
  71. Dim IndexFile = DlClientAssetIndexGet(Instance)
  72. If IndexFile Is Nothing Then
  73. Task.Output = New List(Of NetFile)
  74. Logger.Warn($"未找到版本 {Instance.Name} 的合适的资源索引下载地址,游戏 assets 可能缺失")
  75. Return
  76. End If
  77. Dim IndexBackgroundFile = PathTemp & "Cache\" & IndexFile.LocalName & "_background"
  78. If FileUtils.Exists(IndexBackgroundFile) AndAlso
  79. FileUtils.GetInfo(IndexBackgroundFile).LastWriteTime.Date > FileUtils.GetInfo(IndexFile.LocalPath).LastWriteTime.Date Then
  80. Logger.Info($"发现后台更新的资源索引缓存文件:{IndexBackgroundFile}")
  81. FileUtils.Move(IndexBackgroundFile, IndexFile.LocalPath)
  82. End If
  83. If DownloadAssetIndexInBackground Then
  84. '允许后台更新索引
  85. If IndexFile.Check.Check(IndexFile.LocalPath) Is Nothing Then
  86. Task.Output = New List(Of NetFile)
  87. Else
  88. Task.Output = New List(Of NetFile) From {IndexFile}
  89. End If
  90. Else
  91. '强制要求重新下载
  92. RealAddressMain = IndexFile.LocalPath
  93. TempAddressMain = PathTemp & "Cache\" & IndexFile.LocalName
  94. IndexFile.LocalPath = TempAddressMain
  95. Task.Output = New List(Of NetFile) From {IndexFile}
  96. End If
  97. Catch ex As Exception
  98. Throw New Exception("分析资源文件索引地址失败", ex)
  99. End Try
  100. End Sub) With {.ProgressWeight = 0.5, .Show = False})
  101. '下载资源文件索引
  102. LoadersAssets.Add(New LoaderDownload("下载资源文件索引", New List(Of NetFile)) With {.ProgressWeight = 2})
  103. '获取资源文件地址
  104. LoadersAssets.Add(New LoaderTask(Of String, List(Of NetFile))("获取资源文件列表",
  105. Sub(Task)
  106. If TempAddressMain IsNot Nothing Then FileUtils.Move(TempAddressMain, RealAddressMain)
  107. Task.Output = McAssetsFixList(Instance, CheckAssetsHash, Task)
  108. End Sub) With {.ProgressWeight = 0.01})
  109. '下载资源文件
  110. LoadersAssets.Add(New LoaderDownload("下载资源文件", New List(Of NetFile)) With {.ProgressWeight = 25})
  111. '构造加载器
  112. Loaders.Add(New LoaderCombo(Of String)("下载资源文件(主加载器)", LoadersAssets) With {.Block = False, .Show = False, .ProgressWeight = 27})
  113. '后台独立更新索引
  114. If DownloadAssetIndexInBackground Then
  115. Dim RealAddressBg As String = Nothing
  116. Dim TempAddressBg As String = Nothing
  117. Dim LoadersAssetsUpdate As New List(Of LoaderBase)
  118. LoadersAssetsUpdate.Add(New LoaderTask(Of String, List(Of NetFile))("后台分析资源文件索引地址",
  119. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  120. Dim IndexFile As NetFile = DlClientAssetIndexGet(Instance)
  121. If IndexFile Is Nothing Then
  122. Logger.Warn($"未找到版本 {Instance.Name} 的合适的资源索引下载地址,游戏 assets 可能缺失")
  123. Task.Cancel()
  124. Throw New OperationCanceledException
  125. End If
  126. RealAddressBg = IndexFile.LocalPath
  127. TempAddressBg = PathTemp & "Cache\" & IndexFile.LocalName & "_background"
  128. IndexFile.LocalPath = TempAddressBg
  129. Task.Output = New List(Of NetFile) From {IndexFile}
  130. '检查是否需要更新:每天只更新一次
  131. Dim LastUpdate As Date = FileUtils.GetInfo(RealAddressBg).LastWriteTime.Date
  132. If FileUtils.Exists(RealAddressBg) AndAlso Math.Abs((LastUpdate - Now.Date).TotalDays) < 1 Then
  133. Logger.Info($"无需更新资源文件索引,上次更新于 {LastUpdate}")
  134. Task.Cancel()
  135. End If
  136. End Sub))
  137. LoadersAssetsUpdate.Add(New LoaderDownload("后台下载资源文件索引", New List(Of NetFile)))
  138. Dim Updater As New LoaderCombo(Of String)("后台更新资源文件索引", LoadersAssetsUpdate)
  139. Logger.Info("开始后台检查资源文件索引")
  140. Updater.Start()
  141. End If
  142. End If
  143. #End Region
  144. Return Loaders
  145. End Function
  146. #End Region
  147. #Region "DlClientList | Minecraft 客户端 版本列表"
  148. ''' <summary>
  149. ''' 所有正式版的 Minecraft Drop 序数。
  150. ''' 若从未完成过获取,返回 Nothing;否则必定存在元素,且从高到低排列。
  151. ''' </summary>
  152. Public Property AllDrops As List(Of Integer)
  153. Get
  154. If _AllDrops Is Nothing Then
  155. _AllDrops = Settings.Get(Of String)("CacheDrops").ToString.Split(",", True).Select(Function(d) CInt(Val(d))).ToList()
  156. End If
  157. Return If(_AllDrops.Any, _AllDrops, Nothing) '不要将 _AllDrops 再设为 Nothing,以防止反复获取设置尝试初始化
  158. End Get
  159. Set(value As List(Of Integer))
  160. _AllDrops = value
  161. Settings.Set("CacheDrops", value.Join(","c))
  162. End Set
  163. End Property
  164. Private _AllDrops As List(Of Integer) = Nothing
  165. '主加载器
  166. Public Structure DlClientListResult
  167. ''' <summary>
  168. ''' 数据来源名称,如“Mojang”,“BMCLAPI”。
  169. ''' </summary>
  170. Public SourceName As String
  171. ''' <summary>
  172. ''' 是否为官方的实时数据。
  173. ''' </summary>
  174. Public IsOfficial As Boolean
  175. ''' <summary>
  176. ''' 获取到的 Json 数据。
  177. ''' </summary>
  178. Public Value As JObject
  179. End Structure
  180. ''' <summary>
  181. ''' Minecraft 客户端 版本列表,主加载器。
  182. ''' 若要求镜像源必须包含某个版本,则将该版本 ID 作为输入(#5195)。
  183. ''' </summary>
  184. Public DlClientListLoader As New LoaderTask(Of String, DlClientListResult)("DlClientList Main", AddressOf DlClientListMain)
  185. Private Sub DlClientListMain(Loader As LoaderTask(Of String, DlClientListResult))
  186. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  187. Case 0
  188. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)) From {
  189. New KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)(DlClientListBmclapiLoader, 30),
  190. New KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)(DlClientListMojangLoader, 30 + 60)
  191. }, Loader.IsForceRestarting)
  192. Case 1
  193. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)) From {
  194. New KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)(DlClientListMojangLoader, 5),
  195. New KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)(DlClientListBmclapiLoader, 5 + 30)
  196. }, Loader.IsForceRestarting)
  197. Case Else
  198. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)) From {
  199. New KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)(DlClientListMojangLoader, 60),
  200. New KeyValuePair(Of LoaderTask(Of String, DlClientListResult), Integer)(DlClientListBmclapiLoader, 60 + 60)
  201. }, Loader.IsForceRestarting)
  202. End Select
  203. '提取所有 Drop 序数
  204. Dim Drops As New List(Of Integer)
  205. For Each Version As JObject In Loader.Output.Value("versions")
  206. Dim Id As String = Version("id").ToString
  207. If Id.Contains("-") Then Continue For
  208. Dim Drop As Integer = McVersion.VersionToDrop(Id)
  209. If Drop = 209 Then Continue For
  210. Drops.Add(Drop)
  211. Next
  212. AllDrops = Drops.Distinct.OrderByDescending(Function(d) d).ToList()
  213. End Sub
  214. '各个下载源的分加载器
  215. ''' <summary>
  216. ''' Minecraft 客户端 版本列表,Mojang 官方源加载器。
  217. ''' </summary>
  218. Public DlClientListMojangLoader As New LoaderTask(Of String, DlClientListResult)("DlClientList Mojang", AddressOf DlClientListMojangMain)
  219. Private Sub DlClientListMojangMain(Loader As LoaderTask(Of String, DlClientListResult))
  220. Dim StartTime As Long = GetTimeMs()
  221. Dim Json As JObject = NetRequestByClientRetry("https://launchermeta.mojang.com/mc/game/version_manifest.json", RequireJson:=True).DeserializeJson()
  222. Try
  223. Dim Versions As JArray = Json("versions")
  224. If Versions.Count < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Json.ToString & ")")
  225. '确定官方源是否可用
  226. If Not DlPreferMojang Then
  227. Dim DeltaTime = GetTimeMs() - StartTime
  228. DlPreferMojang = DeltaTime < 4000
  229. Logger.Info($"Mojang 官方源加载耗时:{DeltaTime}ms,{If(DlPreferMojang, "可优先使用官方源", "不优先使用官方源")}")
  230. End If
  231. '返回
  232. Loader.Output = New DlClientListResult With {.IsOfficial = True, .SourceName = "Mojang 官方源", .Value = Json}
  233. 'MC 更新提示
  234. Static IsHinted As Boolean = False
  235. Dim Version As String
  236. '快照版
  237. Version = Json("latest")("snapshot")
  238. If Settings.Get(Of Boolean)("ToolUpdateSnapshot") AndAlso Not Settings.Get(Of String)("ToolUpdateSnapshotLast") = "" AndAlso
  239. Settings.Get(Of String)("ToolUpdateSnapshotLast") <> Version AndAlso Not IsHinted Then
  240. IsHinted = True
  241. McDownloadClientUpdateHint(Version, Json)
  242. End If
  243. Settings.Set("ToolUpdateSnapshotLast", If(Version, "Nothing"))
  244. '正式版
  245. Version = Json("latest")("release")
  246. If Settings.Get(Of Boolean)("ToolUpdateRelease") AndAlso Not Settings.Get(Of String)("ToolUpdateReleaseLast") = "" AndAlso
  247. Settings.Get(Of String)("ToolUpdateReleaseLast") <> Version AndAlso Not IsHinted Then
  248. IsHinted = True
  249. McDownloadClientUpdateHint(Version, Json)
  250. End If
  251. Settings.Set("ToolUpdateReleaseLast", Version)
  252. Catch ex As Exception
  253. Throw New Exception("Minecraft 官方源版本列表解析失败", ex)
  254. End Try
  255. End Sub
  256. ''' <summary>
  257. ''' Minecraft 客户端 版本列表,BMCLAPI 源加载器。
  258. ''' </summary>
  259. Public DlClientListBmclapiLoader As New LoaderTask(Of String, DlClientListResult)("DlClientList Bmclapi", AddressOf DlClientListBmclapiMain)
  260. Private Sub DlClientListBmclapiMain(Loader As LoaderTask(Of String, DlClientListResult))
  261. Dim Json As JObject = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/mc/game/version_manifest.json", RequireJson:=True).DeserializeJson()
  262. Try
  263. Dim Versions As JArray = Json("versions")
  264. If Versions.Count < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Json.ToString & ")")
  265. '添加 PCL 特供项
  266. If FileUtils.Exists(PathTemp & "Cache\download.json") Then Versions.Merge(FileUtils.ReadAsJson(PathTemp & "Cache\download.json"))
  267. '检查是否有要求的版本(#5195)
  268. If Not String.IsNullOrEmpty(Loader.Input) Then
  269. Dim Id = Loader.Input
  270. If DlClientListLoader.Output.Value IsNot Nothing AndAlso Not DlClientListLoader.Output.Value("versions").Any(Function(v) v("id") = Id) Then
  271. Throw New Exception("BMCLAPI 源未包含目标版本 " & Id)
  272. End If
  273. End If
  274. '返回
  275. Loader.Output = New DlClientListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Json}
  276. Catch ex As Exception
  277. Throw New Exception("Minecraft BMCLAPI 版本列表解析失败(" & Json.ToString & ")", ex)
  278. End Try
  279. End Sub
  280. ''' <summary>
  281. ''' 获取某个版本的 Json 下载地址,若失败则返回 Nothing。必须在工作线程执行。
  282. ''' </summary>
  283. Public Function DlClientListGet(Id As String)
  284. Try
  285. '确认版本格式标准
  286. Id = Id.Replace("_", "-") '1.7.10_pre4 在版本列表中显示为 1.7.10-pre4
  287. If Id <> "1.0" AndAlso Id.EndsWithF(".0") Then Id = Left(Id, Id.Length - 2) 'OptiFine 1.8 的下载会触发此问题,显示版本为 1.8.0
  288. '获取 Minecraft 版本列表
  289. Select Case DlClientListLoader.State
  290. Case LoadState.Finished
  291. '从当前的结果获取目标版本…
  292. For Each Version As JObject In DlClientListLoader.Output.Value("versions")
  293. If Version("id") = Id Then Return Version("url").ToString
  294. Next
  295. '…如果没有,则重新尝试获取(在版本刚更新时可能出现这种情况,#5195)
  296. DlClientListLoader.WaitForExit(Id, IsForceRestart:=True)
  297. Case LoadState.Loading
  298. DlClientListLoader.WaitForExit(Id)
  299. Case LoadState.Failed, LoadState.Canceled, LoadState.Waiting
  300. DlClientListLoader.WaitForExit(Id, IsForceRestart:=True)
  301. End Select
  302. '重新查找版本
  303. For Each Version As JObject In DlClientListLoader.Output.Value("versions")
  304. If Version("id") = Id Then Return Version("url").ToString
  305. Next
  306. Logger.Warn($"未发现版本 {Id} 的 json 下载地址,版本列表返回为:{vbCrLf}{DlClientListLoader.Output.Value}")
  307. Return Nothing
  308. Catch ex As Exception
  309. Logger.Warn(ex, $"获取版本 {Id} 的 json 下载地址失败")
  310. Return Nothing
  311. End Try
  312. End Function
  313. #End Region
  314. #Region "DlOptiFineList | OptiFine 版本列表"
  315. Public Structure DlOptiFineListResult
  316. ''' <summary>
  317. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  318. ''' </summary>
  319. Public SourceName As String
  320. ''' <summary>
  321. ''' 是否为官方的实时数据。
  322. ''' </summary>
  323. Public IsOfficial As Boolean
  324. ''' <summary>
  325. ''' 获取到的数据。
  326. ''' </summary>
  327. Public Value As List(Of DlOptiFineListEntry)
  328. End Structure
  329. Public Class DlOptiFineListEntry
  330. ''' <summary>
  331. ''' 显示名称,已去除 HD_U 字样,如“1.12.2 C8”。
  332. ''' </summary>
  333. Public DisplayName As String
  334. ''' <summary>
  335. ''' 原始文件名称,如“preview_OptiFine_1.11_HD_U_E1_pre.jar”。
  336. ''' </summary>
  337. Public FileName As String
  338. ''' <summary>
  339. ''' 对应的版本名称,如“1.13.2-OptiFine_HD_U_E6”。
  340. ''' </summary>
  341. Public InstanceName As String
  342. ''' <summary>
  343. ''' 是否为测试版。
  344. ''' </summary>
  345. Public IsPreview As Boolean
  346. ''' <summary>
  347. ''' 对应的 Minecraft 版本,如“1.12.2”。
  348. ''' </summary>
  349. Public Property Inherit As String
  350. Get
  351. Return _inherit
  352. End Get
  353. Set(value As String)
  354. If value.EndsWithF(".0") Then value = Left(value, value.Length - 2)
  355. _inherit = value
  356. End Set
  357. End Property
  358. Private _inherit As String
  359. ''' <summary>
  360. ''' 发布时间,格式为“yyyy/mm/dd”。OptiFine 源无此数据。
  361. ''' </summary>
  362. Public ReleaseTime As String
  363. ''' <summary>
  364. ''' 需要的最低 Forge 版本。空字符串为无限制,Nothing 为不兼容,“28.1.56” 表示版本号,“1161” 表示版本号的最后一位。
  365. ''' </summary>
  366. Public RequiredForgeVersion As String
  367. End Class
  368. ''' <summary>
  369. ''' OptiFine 版本列表,主加载器。
  370. ''' </summary>
  371. Public DlOptiFineListLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Main", AddressOf DlOptiFineListMain)
  372. Private Sub DlOptiFineListMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  373. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  374. Case 0
  375. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  376. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 30),
  377. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 30 + 60)
  378. }, Loader.IsForceRestarting)
  379. Case 1
  380. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  381. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 5),
  382. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 5 + 30)
  383. }, Loader.IsForceRestarting)
  384. Case Else
  385. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  386. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 60),
  387. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 60 + 60)
  388. }, Loader.IsForceRestarting)
  389. End Select
  390. End Sub
  391. ''' <summary>
  392. ''' OptiFine 版本列表,官方源。
  393. ''' </summary>
  394. Public DlOptiFineListOfficialLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Official", AddressOf DlOptiFineListOfficialMain)
  395. Private Sub DlOptiFineListOfficialMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  396. Dim Result As String = NetRequestByClientRetry("https://optifine.net/downloads", Encoding:=Encoding.Default)
  397. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  398. Try
  399. '获取所有版本信息
  400. Dim Forge As List(Of String) = Result.RegexSearch("(?<=colForge'>)[^<]*").ToList
  401. Dim ReleaseTime As List(Of String) = Result.RegexSearch("(?<=colDate'>)[^<]+").ToList
  402. Dim Name As List(Of String) = Result.RegexSearch("(?<=OptiFine_)[0-9A-Za-z_.]+(?=.jar"")").ToList
  403. If Not ReleaseTime.Count = Name.Count Then Throw New Exception("版本与发布时间数据无法对应")
  404. If Not Forge.Count = Name.Count Then Throw New Exception("版本与 Forge 兼容数据无法对应")
  405. If ReleaseTime.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  406. '转化为列表输出
  407. Dim Versions As New List(Of DlOptiFineListEntry)
  408. For i = 0 To ReleaseTime.Count - 1
  409. Name(i) = Name(i).Replace("_", " ")
  410. Dim Entry As New DlOptiFineListEntry With {
  411. .DisplayName = Name(i).Replace("HD U ", "").Replace(".0 ", " "),
  412. .ReleaseTime = {ReleaseTime(i).Split(".")(2), ReleaseTime(i).Split(".")(1), ReleaseTime(i).Split(".")(0)}.Join("/"c),
  413. .IsPreview = Name(i).ContainsIgnoreCase("pre"),
  414. .Inherit = Name(i).ToString.Split(" ")(0),
  415. .FileName = If(Name(i).ContainsIgnoreCase("pre"), "preview_", "") & "OptiFine_" & Name(i).Replace(" ", "_") & ".jar",
  416. .RequiredForgeVersion = Forge(i).Replace("Forge ", "").Replace("#", "")}
  417. If Entry.RequiredForgeVersion.Contains("N/A") Then Entry.RequiredForgeVersion = Nothing
  418. Entry.InstanceName = Entry.Inherit & "-OptiFine_" & Name(i).AfterFirst(" ").Replace(" ", "_")
  419. Versions.Add(Entry)
  420. Next
  421. Loader.Output = New DlOptiFineListResult With {.IsOfficial = True, .SourceName = "OptiFine 官方源", .Value = Versions}
  422. Catch ex As Exception
  423. Throw New Exception("OptiFine 官方源版本列表解析失败(" & Result & ")", ex)
  424. End Try
  425. End Sub
  426. ''' <summary>
  427. ''' OptiFine 版本列表,BMCLAPI。
  428. ''' </summary>
  429. Public DlOptiFineListBmclapiLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Bmclapi", AddressOf DlOptiFineListBmclapiMain)
  430. Private Sub DlOptiFineListBmclapiMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  431. Dim Json As JArray = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/optifine/versionList", RequireJson:=True).DeserializeJson()
  432. Try
  433. Dim Versions As New List(Of DlOptiFineListEntry)
  434. For Each Token As JObject In Json
  435. Dim Entry As New DlOptiFineListEntry With {
  436. .DisplayName = (Token("mcversion").ToString & Token("type").ToString.Replace("HD_U", "").Replace("_", " ") & " " & Token("patch").ToString).Replace(".0 ", " "),
  437. .ReleaseTime = "",
  438. .IsPreview = Token("patch").ToString.ContainsIgnoreCase("pre"),
  439. .Inherit = Token("mcversion").ToString,
  440. .FileName = Token("filename").ToString,
  441. .RequiredForgeVersion = If(Token("forge"), "").ToString.Replace("Forge ", "").Replace("#", "")
  442. }
  443. If Entry.RequiredForgeVersion.Contains("N/A") Then Entry.RequiredForgeVersion = Nothing
  444. Entry.InstanceName = Entry.Inherit & "-OptiFine_" & (Token("type").ToString & " " & Token("patch").ToString).Replace(".0 ", " ").Replace(" ", "_").Replace(Entry.Inherit & "_", "")
  445. Versions.Add(Entry)
  446. Next
  447. Loader.Output = New DlOptiFineListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Versions}
  448. Catch ex As Exception
  449. Throw New Exception("OptiFine BMCLAPI 版本列表解析失败(" & Json.ToString & ")", ex)
  450. End Try
  451. End Sub
  452. #End Region
  453. #Region "DlForgeList | Forge Minecraft 版本列表"
  454. Public Structure DlForgeListResult
  455. ''' <summary>
  456. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  457. ''' </summary>
  458. Public SourceName As String
  459. ''' <summary>
  460. ''' 是否为官方的实时数据。
  461. ''' </summary>
  462. Public IsOfficial As Boolean
  463. ''' <summary>
  464. ''' 获取到的数据。
  465. ''' </summary>
  466. Public Value As List(Of String)
  467. End Structure
  468. ''' <summary>
  469. ''' Forge 版本列表,主加载器。
  470. ''' </summary>
  471. Public DlForgeListLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Main", AddressOf DlForgeListMain)
  472. Private Sub DlForgeListMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  473. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  474. Case 0
  475. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  476. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 30),
  477. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 30 + 60)
  478. }, Loader.IsForceRestarting)
  479. Case 1
  480. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  481. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 5),
  482. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 5 + 30)
  483. }, Loader.IsForceRestarting)
  484. Case Else
  485. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  486. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 60),
  487. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 60 + 60)
  488. }, Loader.IsForceRestarting)
  489. End Select
  490. End Sub
  491. ''' <summary>
  492. ''' Forge 版本列表,官方源。
  493. ''' </summary>
  494. Public DlForgeListOfficialLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Official", AddressOf DlForgeListOfficialMain)
  495. Private Sub DlForgeListOfficialMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  496. Dim Result As String = NetRequestByClientRetry("https://files.minecraftforge.net/maven/net/minecraftforge/forge/index_1.2.4.html", Encoding:=Encoding.Default, Accept:="text/html", SimulateBrowserHeaders:=True)
  497. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  498. '获取所有版本信息
  499. Dim Names As List(Of String) = Result.RegexSearch("(?<=a href=""index_)[0-9.]+((_pre|[_-]snapshot[_-]?)[0-9]?)?(?=.html)").ToList
  500. Names.Add("1.2.4") '1.2.4 不会被匹配上
  501. If Names.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  502. Loader.Output = New DlForgeListResult With {.IsOfficial = True, .SourceName = "Forge 官方源", .Value = Names}
  503. End Sub
  504. ''' <summary>
  505. ''' Forge 版本列表,BMCLAPI。
  506. ''' </summary>
  507. Public DlForgeListBmclapiLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Bmclapi", AddressOf DlForgeListBmclapiMain)
  508. Private Sub DlForgeListBmclapiMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  509. Dim Result As JArray = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/forge/minecraft",
  510. Encoding:=Encoding.Default, RequireJson:=True).DeserializeJson()
  511. Dim Names As List(Of String) = Result.Select(Function(v) v.ToString).ToList
  512. If Names.Count < 10 Then Throw New Exception("获取到的版本数量不足")
  513. Loader.Output = New DlForgeListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Names}
  514. End Sub
  515. #End Region
  516. #Region "DlForgeVersion | Forge 版本列表"
  517. Public MustInherit Class DlForgelikeEntry
  518. Implements IComparable(Of DlForgelikeEntry)
  519. Public IsNeoForge As Boolean
  520. ''' <summary>
  521. ''' 加载器名称。Forge 或 NeoForge。
  522. ''' </summary>
  523. Public MustOverride ReadOnly Property LoaderName As String
  524. ''' <summary>
  525. ''' 文件扩展名。不以小数点开头。
  526. ''' </summary>
  527. Public MustOverride ReadOnly Property FileExtension As String
  528. ''' <summary>
  529. ''' 标准化后的版本号,仅可用于比较与排序,且 NeoForge 项不包含 alpha/beta 编号!
  530. ''' 格式:Major.Minor.Build.Revision
  531. ''' Forge:如 “50.1.9.0”(最后一位固定为 0)、“14.22.1.2478”(Legacy)。
  532. ''' NeoForge:如 “20.4.30.0”(最后一位固定为 0)、“19.47.1.99”(Legacy:第一位固定为 19)。
  533. ''' </summary>
  534. Public Version As Version
  535. ''' <summary>
  536. ''' 可读的非格式化版本名。
  537. ''' Forge:如 “50.1.9”、“14.22.1.2478”(Legacy)。
  538. ''' NeoForge:如 “20.4.30-beta”、“47.1.99”(Legacy)。
  539. ''' </summary>
  540. Public VersionName As String
  541. ''' <summary>
  542. ''' 对应的 Minecraft 版本,如 1.12.2、26.1-snapshot-1。
  543. ''' </summary>
  544. Public Inherit As String
  545. Public Function CompareTo(other As DlForgelikeEntry) As Integer Implements IComparable(Of DlForgelikeEntry).CompareTo
  546. If Version <> other.Version Then
  547. Return Version.CompareTo(other.Version)
  548. Else
  549. Return McUtils.CompareVersion(VersionName, other.VersionName)
  550. End If
  551. End Function
  552. End Class
  553. Public Class DlForgeVersionEntry
  554. Inherits DlForgelikeEntry
  555. ''' <summary>
  556. ''' 发布时间,格式为“yyyy/MM/dd HH:mm”。
  557. ''' </summary>
  558. Public ReleaseTime As String
  559. ''' <summary>
  560. ''' 文件的 MD5 或 SHA1(BMCLAPI 的老版本是 MD5,新版本是 SHA1;官方源总是 MD5)。
  561. ''' </summary>
  562. Public Hash As String = Nothing
  563. ''' <summary>
  564. ''' 是否为推荐版本。
  565. ''' </summary>
  566. Public IsRecommended As Boolean
  567. ''' <summary>
  568. ''' 安装类型。有 installer、client、universal 三种。
  569. ''' </summary>
  570. Public Category As String
  571. ''' <summary>
  572. ''' 用于下载的文件版本名。可能在 Version 的基础上添加了分支。
  573. ''' </summary>
  574. Public FileVersion As String
  575. ''' <summary>
  576. ''' 加载器名称。Forge 或 NeoForge。
  577. ''' </summary>
  578. Public Overrides ReadOnly Property LoaderName As String
  579. Get
  580. Return "Forge"
  581. End Get
  582. End Property
  583. ''' <summary>
  584. ''' 文件扩展名。不以小数点开头。
  585. ''' </summary>
  586. Public Overrides ReadOnly Property FileExtension As String
  587. Get
  588. Return If(Category = "installer", "jar", "zip")
  589. End Get
  590. End Property
  591. Public Sub New(Version As String, Branch As String, Inherit As String)
  592. '司马版本的特殊处理
  593. If Version = "11.15.1.2318" OrElse Version = "11.15.1.1902" OrElse Version = "11.15.1.1890" Then Branch = "1.8.9"
  594. If Branch Is Nothing AndAlso Inherit = "1.7.10" AndAlso Version.Split(".")(3) >= 1300 Then Branch = "1.7.10"
  595. '为 DlForgelikeEntry 提供所有信息
  596. IsNeoForge = False
  597. VersionName = Version
  598. Me.Version = New Version(Version)
  599. Me.Inherit = Inherit
  600. FileVersion = Version & If(Branch Is Nothing, "", "-" & Branch)
  601. End Sub
  602. End Class
  603. ''' <summary>
  604. ''' Forge 版本列表,主加载器。
  605. ''' </summary>
  606. Public Sub DlForgeVersionMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  607. Dim DlForgeVersionOfficialLoader As New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Official", AddressOf DlForgeVersionOfficialMain)
  608. Dim DlForgeVersionBmclapiLoader As New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Bmclapi", AddressOf DlForgeVersionBmclapiMain)
  609. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  610. Case 0
  611. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  612. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 30),
  613. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 30 + 60)
  614. }, Loader.IsForceRestarting)
  615. Case 1
  616. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  617. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 5),
  618. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 5 + 30)
  619. }, Loader.IsForceRestarting)
  620. Case Else
  621. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  622. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 60),
  623. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 60 + 60)
  624. }, Loader.IsForceRestarting)
  625. End Select
  626. End Sub
  627. ''' <summary>
  628. ''' Forge 版本列表,官方源。
  629. ''' </summary>
  630. Public Sub DlForgeVersionOfficialMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  631. Dim Result As String
  632. Try
  633. Result = NetRequestByLoader("https://files.minecraftforge.net/maven/net/minecraftforge/forge/index_" &
  634. Loader.Input.Replace("-", "_") & '兼容 Forge 1.7.10-pre4,#4057
  635. ".html", SimulateBrowserHeaders:=True)
  636. Catch ex As Exception
  637. If ex.GetDisplay(False).Contains("(404)") Then
  638. Throw New Exception("无")
  639. Else
  640. Throw
  641. End If
  642. End Try
  643. If Result.Length < 1000 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  644. Dim Versions As New List(Of DlForgeVersionEntry)
  645. Try
  646. '分割版本信息
  647. Dim VersionCodes = Result.Substring(0, Result.LastIndexOfF("</table>")).Split("<td class=""download-version")
  648. '获取所有版本信息
  649. For i = 1 To VersionCodes.Count - 1
  650. Dim VersionCode = VersionCodes(i)
  651. Try
  652. '基础信息获取
  653. Dim Name As String = VersionCode.RegexSeek("(?<=[^(0-9)]+)[0-9\.]+")
  654. Dim IsRecommended As Boolean = VersionCode.Contains("fa promo-recommended")
  655. Dim Inherit As String = Loader.Input
  656. '分支获取
  657. Dim Branch As String = VersionCode.RegexSeek($"(?<=-{Name}-)[^-""]+(?=-[a-z]+.[a-z]{{3}})")
  658. If String.IsNullOrWhiteSpace(Branch) Then Branch = Nothing
  659. '发布时间获取
  660. Dim ReleaseTimeOriginal = VersionCode.RegexSeek("(?<=""download-time"" title="")[^""]+")
  661. Dim ReleaseTimeSplit = ReleaseTimeOriginal.Split(" -:".ToCharArray) '原格式:"2021-02-15 03:24:02"
  662. Dim ReleaseDate As New Date(ReleaseTimeSplit(0), ReleaseTimeSplit(1), ReleaseTimeSplit(2), '年月日
  663. ReleaseTimeSplit(3), ReleaseTimeSplit(4), ReleaseTimeSplit(5), '时分秒
  664. 0, DateTimeKind.Utc) '以 UTC 时间作为标准
  665. Dim ReleaseTime As String = ReleaseDate.ToLocalTime.ToString("yyyy'/'MM'/'dd HH':'mm") '时区与格式转换
  666. '分类与 MD5 获取
  667. Dim MD5 As String, Category As String
  668. If VersionCode.Contains("classifier-installer""") Then
  669. '类型为 installer.jar,支持范围 ~753 (~ 1.6.1 部分), 738~684 (1.5.2 全部)
  670. VersionCode = VersionCode.Substring(VersionCode.IndexOfF("installer.jar"))
  671. MD5 = VersionCode.RegexSeek("(?<=MD5:</strong> )[^<]+")
  672. Category = "installer"
  673. ElseIf VersionCode.Contains("classifier-universal""") Then
  674. '类型为 universal.zip,支持范围 751~449 (1.6.1 部分), 682~183 (1.5.1 ~ 1.3.2 部分)
  675. VersionCode = VersionCode.Substring(VersionCode.IndexOfF("universal.zip"))
  676. MD5 = VersionCode.RegexSeek("(?<=MD5:</strong> )[^<]+")
  677. Category = "universal"
  678. ElseIf VersionCode.Contains("client.zip") Then
  679. '类型为 client.zip,支持范围 182~ (1.3.2 部分 ~)
  680. VersionCode = VersionCode.Substring(VersionCode.IndexOfF("client.zip"))
  681. MD5 = VersionCode.RegexSeek("(?<=MD5:</strong> )[^<]+")
  682. Category = "client"
  683. Else
  684. '没有任何下载(1.6.4 有一部分这种情况)
  685. Continue For
  686. End If
  687. '添加进列表
  688. Versions.Add(New DlForgeVersionEntry(Name, Branch, Inherit) With {.Category = Category, .IsRecommended = IsRecommended, .Hash = MD5.ReplaceLineEndings(""), .ReleaseTime = ReleaseTime})
  689. Catch ex As Exception
  690. Throw New Exception("Forge 官方源版本信息提取失败(" & VersionCode & ")", ex)
  691. End Try
  692. Next
  693. Catch ex As Exception
  694. Throw New Exception("Forge 官方源版本列表解析失败(" & Result & ")", ex)
  695. End Try
  696. If Not Versions.Any() Then Throw New Exception("无")
  697. Loader.Output = Versions
  698. End Sub
  699. ''' <summary>
  700. ''' Forge 版本列表,BMCLAPI。
  701. ''' </summary>
  702. Public Sub DlForgeVersionBmclapiMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  703. Dim Json As JArray = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/forge/minecraft/" &
  704. Loader.Input.Replace("-", "_"), RequireJson:=True).DeserializeJson() '兼容 Forge 1.7.10-pre4,#4057
  705. Dim Versions As New List(Of DlForgeVersionEntry)
  706. Try
  707. Dim Recommended As String = McDownloadForgeRecommendedGet(Loader.Input)
  708. For Each Token As JObject In Json
  709. '分类与 Hash 获取
  710. Dim Hash As String = Nothing, Category As String = "unknown", Proi As Integer = -1
  711. For Each File As JObject In Token("files")
  712. Select Case File("category").ToString
  713. Case "installer"
  714. If File("format").ToString = "jar" Then
  715. '类型为 installer.jar,支持范围 ~753 (~ 1.6.1 部分), 738~684 (1.5.2 全部)
  716. Hash = File("hash")
  717. Category = "installer"
  718. Proi = 2
  719. End If
  720. Case "universal"
  721. If Proi <= 1 AndAlso File("format").ToString = "zip" Then
  722. '类型为 universal.zip,支持范围 751~449 (1.6.1 部分), 682~183 (1.5.1 ~ 1.3.2 部分)
  723. Hash = File("hash")
  724. Category = "universal"
  725. Proi = 1
  726. End If
  727. Case "client"
  728. If Proi <= 0 AndAlso File("format").ToString = "zip" Then
  729. '类型为 client.zip,支持范围 182~ (1.3.2 部分 ~)
  730. Hash = File("hash")
  731. Category = "client"
  732. Proi = 0
  733. End If
  734. End Select
  735. Next
  736. '获取 Entry
  737. Dim Branch As String = Token("branch")
  738. Dim Name As String = Token("version")
  739. '基础信息获取
  740. Dim Entry = New DlForgeVersionEntry(Name, Branch, Loader.Input) With {.Hash = Hash, .Category = Category, .IsRecommended = Recommended = Name}
  741. Dim TimeSplit = Token("modified").ToString.Split("-"c, "T"c, ":"c, "."c, " "c, "/"c)
  742. Entry.ReleaseTime = Token("modified").ToObject(Of Date).ToLocalTime.ToString("yyyy'/'MM'/'dd HH':'mm")
  743. '添加项
  744. Versions.Add(Entry)
  745. Next
  746. Catch ex As Exception
  747. Throw New Exception("Forge BMCLAPI 版本列表解析失败(" & Json.ToString & ")", ex)
  748. End Try
  749. If Not Versions.Any() Then Throw New Exception("无")
  750. Loader.Output = Versions
  751. End Sub
  752. #End Region
  753. #Region "DlNeoForgeList | NeoForge 版本列表"
  754. Public Structure DlNeoForgeListResult
  755. ''' <summary>
  756. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  757. ''' </summary>
  758. Public SourceName As String
  759. ''' <summary>
  760. ''' 是否为官方的实时数据。
  761. ''' </summary>
  762. Public IsOfficial As Boolean
  763. ''' <summary>
  764. ''' 所有版本的列表。已经按从新到老排序。
  765. ''' </summary>
  766. Public Value As List(Of DlNeoForgeListEntry)
  767. End Structure
  768. Public Class DlNeoForgeListEntry
  769. Inherits DlForgelikeEntry
  770. ''' <summary>
  771. ''' 是否是 Beta 版。
  772. ''' </summary>
  773. Public IsBeta As Boolean
  774. ''' <summary>
  775. ''' API 使用的原始版本字符串,如 “20.4.30-beta”、“1.20.1-47.1.99”(Legacy)。
  776. ''' </summary>
  777. Public ApiName As String
  778. ''' <summary>
  779. ''' 文件在官网的基础地址,不包含后缀。
  780. ''' </summary>
  781. Public ReadOnly Property UrlBase As String
  782. Get
  783. Dim PackageName As String = If(Inherit = "1.20.1", "forge", "neoforge")
  784. Return $"https://maven.neoforged.net/releases/net/neoforged/{PackageName}/{ApiName}/{PackageName}-{ApiName}"
  785. End Get
  786. End Property
  787. ''' <summary>
  788. ''' 加载器名称。Forge 或 NeoForge。
  789. ''' </summary>
  790. Public Overrides ReadOnly Property LoaderName As String
  791. Get
  792. Return "NeoForge"
  793. End Get
  794. End Property
  795. ''' <summary>
  796. ''' 文件扩展名。不以小数点开头。
  797. ''' </summary>
  798. Public Overrides ReadOnly Property FileExtension As String
  799. Get
  800. Return "jar"
  801. End Get
  802. End Property
  803. Public Sub New(ApiName As String)
  804. IsNeoForge = True
  805. Me.ApiName = ApiName
  806. IsBeta = ApiName.ContainsIgnoreCase("beta") OrElse ApiName.ContainsIgnoreCase("alpha")
  807. If ApiName.Contains("1.20.1") Then '1.20.1-47.1.99
  808. VersionName = ApiName.Replace("1.20.1-", "")
  809. Version = New Version("19." & VersionName)
  810. Inherit = "1.20.1"
  811. ElseIf ApiName.StartsWithF("0.") Then '0.25w14craftmine.3-beta
  812. VersionName = ApiName
  813. Dim Segments = ApiName.BeforeFirst("-").Split("."c)
  814. Version = New Version(0, 0, Segments.Last)
  815. Inherit = Segments(1)
  816. Else '20.4.30-beta;26.1.0.0-alpha.1+snapshot-1
  817. VersionName = ApiName
  818. Version = New Version(ApiName.BeforeFirst("-"))
  819. If Version.Major >= 24 Then
  820. Inherit = $"{Version.Major}.{Version.Minor}.{Version.Build}"
  821. Else
  822. Inherit = $"1.{Version.Major}.{Version.Minor}"
  823. End If
  824. If Inherit.EndsWithF(".0") Then Inherit = Left(Inherit, Inherit.Length - 2)
  825. If VersionName.Contains("+") Then Inherit &= "-" & VersionName.AfterFirst("+")
  826. End If
  827. End Sub
  828. End Class
  829. ''' <summary>
  830. ''' NeoForge 版本列表,主加载器。
  831. ''' </summary>
  832. Public DlNeoForgeListLoader As New LoaderTask(Of Integer, DlNeoForgeListResult)("DlNeoForgeList Main", AddressOf DlNeoForgeListMain)
  833. Private Sub DlNeoForgeListMain(Loader As LoaderTask(Of Integer, DlNeoForgeListResult))
  834. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  835. Case 0
  836. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)) From {
  837. New KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)(DlNeoForgeListBmclapiLoader, 30),
  838. New KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)(DlNeoForgeListOfficialLoader, 30 + 60)
  839. }, Loader.IsForceRestarting)
  840. Case 1
  841. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)) From {
  842. New KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)(DlNeoForgeListOfficialLoader, 5),
  843. New KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)(DlNeoForgeListBmclapiLoader, 5 + 30)
  844. }, Loader.IsForceRestarting)
  845. Case Else
  846. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)) From {
  847. New KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)(DlNeoForgeListOfficialLoader, 60),
  848. New KeyValuePair(Of LoaderTask(Of Integer, DlNeoForgeListResult), Integer)(DlNeoForgeListBmclapiLoader, 60 + 60)
  849. }, Loader.IsForceRestarting)
  850. End Select
  851. End Sub
  852. ''' <summary>
  853. ''' NeoForge 版本列表,官方源。
  854. ''' </summary>
  855. Public DlNeoForgeListOfficialLoader As New LoaderTask(Of Integer, DlNeoForgeListResult)("DlNeoForgeList Official", AddressOf DlNeoForgeListOfficialMain)
  856. Private Sub DlNeoForgeListOfficialMain(Loader As LoaderTask(Of Integer, DlNeoForgeListResult))
  857. '获取版本列表 JSON
  858. Dim ResultLatest As String = NetRequestByClientRetry("https://maven.neoforged.net/api/maven/versions/releases/net/neoforged/neoforge", RequireJson:=True)
  859. Dim ResultLegacy As String = NetRequestByClientRetry("https://maven.neoforged.net/api/maven/versions/releases/net/neoforged/forge", RequireJson:=True)
  860. If ResultLatest.Length < 100 OrElse ResultLegacy.Length < 100 Then Throw New Exception("获取到的版本列表长度不足(" & ResultLatest & ")")
  861. '解析
  862. Try
  863. Loader.Output = New DlNeoForgeListResult With {.IsOfficial = True, .SourceName = "NeoForge 官方源",
  864. .Value = GetNeoForgeEntries(ResultLatest, ResultLegacy)}
  865. Catch ex As Exception
  866. Throw New Exception("NeoForge 官方源版本列表解析失败(" & ResultLatest & vbCrLf & vbCrLf & ResultLegacy & ")", ex)
  867. End Try
  868. End Sub
  869. ''' <summary>
  870. ''' NeoForge 版本列表,BMCLAPI。
  871. ''' </summary>
  872. Public DlNeoForgeListBmclapiLoader As New LoaderTask(Of Integer, DlNeoForgeListResult)("DlNeoForgeList Bmclapi", AddressOf DlNeoForgeListBmclapiMain)
  873. Public Sub DlNeoForgeListBmclapiMain(Loader As LoaderTask(Of Integer, DlNeoForgeListResult))
  874. '获取版本列表 JSON
  875. Dim ResultLatest As String = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/neoforge/meta/api/maven/details/releases/net/neoforged/neoforge", RequireJson:=True)
  876. Dim ResultLegacy As String = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/neoforge/meta/api/maven/details/releases/net/neoforged/forge", RequireJson:=True)
  877. If ResultLatest.Length < 100 OrElse ResultLegacy.Length < 100 Then Throw New Exception("获取到的版本列表长度不足(" & ResultLatest & ")")
  878. '解析
  879. Try
  880. Loader.Output = New DlNeoForgeListResult With {.IsOfficial = True, .SourceName = "BMCLAPI",
  881. .Value = GetNeoForgeEntries(ResultLatest, ResultLegacy)}
  882. Catch ex As Exception
  883. Throw New Exception("NeoForge BMCLAPI 版本列表解析失败(" & ResultLatest & vbCrLf & vbCrLf & ResultLegacy & ")", ex)
  884. End Try
  885. End Sub
  886. Private Function GetNeoForgeEntries(LatestJson As String, LatestLegacyJson As String) As List(Of DlNeoForgeListEntry)
  887. Dim VersionNames = (LatestLegacyJson & LatestJson).RegexSearch(
  888. "(?<="")(1\.20\.1-)?\d+\.[^\.]+\.\d+(\.\d+)?(-(beta|alpha)(\.\d+)?)?(\+snapshot-\d+)?(?="")")
  889. Dim Versions = VersionNames.
  890. Where(Function(name) name <> "47.1.82"). '这个版本虽然在版本列表中,但不能下载
  891. Select(Function(name) New DlNeoForgeListEntry(name)).
  892. OrderByDescending(Function(a) a).ToList
  893. If Not Versions.Any() Then Throw New Exception("无")
  894. Return Versions
  895. End Function
  896. #End Region
  897. #Region "DlLiteLoaderList | LiteLoader 版本列表"
  898. Public Structure DlLiteLoaderListResult
  899. ''' <summary>
  900. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  901. ''' </summary>
  902. Public SourceName As String
  903. ''' <summary>
  904. ''' 是否为官方的实时数据。
  905. ''' </summary>
  906. Public IsOfficial As Boolean
  907. ''' <summary>
  908. ''' 获取到的数据。
  909. ''' </summary>
  910. Public Value As List(Of DlLiteLoaderListEntry)
  911. ''' <summary>
  912. ''' 官方源的失败原因。若没有则为 Nothing。
  913. ''' </summary>
  914. Public OfficialError As Exception
  915. End Structure
  916. Public Class DlLiteLoaderListEntry
  917. ''' <summary>
  918. ''' 实际的文件名,如“liteloader-installer-1.12-00-SNAPSHOT.jar”。
  919. ''' </summary>
  920. Public FileName As String
  921. ''' <summary>
  922. ''' 是否为测试版。
  923. ''' </summary>
  924. Public IsPreview As Boolean
  925. ''' <summary>
  926. ''' 对应的 Minecraft 版本,如“1.12.2”。
  927. ''' </summary>
  928. Public Inherit As String
  929. ''' <summary>
  930. ''' 是否为 1.7 及更早的远古版。
  931. ''' </summary>
  932. Public IsLegacy As Boolean
  933. ''' <summary>
  934. ''' 发布时间,格式为“yyyy/mm/dd HH:mm”。
  935. ''' </summary>
  936. Public ReleaseTime As String
  937. ''' <summary>
  938. ''' 文件的 MD5。
  939. ''' </summary>
  940. Public MD5 As String
  941. ''' <summary>
  942. ''' 对应的 Json 项。
  943. ''' </summary>
  944. Public JsonToken As JToken
  945. End Class
  946. ''' <summary>
  947. ''' LiteLoader 版本列表,主加载器。
  948. ''' </summary>
  949. Public DlLiteLoaderListLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Main", AddressOf DlLiteLoaderListMain)
  950. Private Sub DlLiteLoaderListMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  951. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  952. Case 0
  953. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  954. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 30),
  955. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 30 + 60)
  956. }, Loader.IsForceRestarting)
  957. Case 1
  958. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  959. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 5),
  960. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 5 + 30)
  961. }, Loader.IsForceRestarting)
  962. Case Else
  963. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  964. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 60),
  965. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 60 + 60)
  966. }, Loader.IsForceRestarting)
  967. End Select
  968. End Sub
  969. ''' <summary>
  970. ''' LiteLoader 版本列表,官方源。
  971. ''' </summary>
  972. Public DlLiteLoaderListOfficialLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Official", AddressOf DlLiteLoaderListOfficialMain)
  973. Private Sub DlLiteLoaderListOfficialMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  974. Dim Result As JObject = NetRequestByClientRetry("https://dl.liteloader.com/versions/versions.json", RequireJson:=True).DeserializeJson()
  975. Try
  976. Dim Json As JObject = Result("versions")
  977. Dim Versions As New List(Of DlLiteLoaderListEntry)
  978. For Each Pair As KeyValuePair(Of String, JToken) In Json
  979. If Pair.Key.StartsWithF("1.6") OrElse Pair.Key.StartsWithF("1.5") Then Continue For
  980. Dim RealEntry As JToken = If(Pair.Value("artefacts"), Pair.Value("snapshots"))("com.mumfrey:liteloader")("latest")
  981. Versions.Add(New DlLiteLoaderListEntry With {
  982. .Inherit = Pair.Key,
  983. .IsLegacy = Pair.Key.Split(".")(1) < 8,
  984. .IsPreview = RealEntry("stream").ToString.Lower = "snapshot",
  985. .FileName = "liteloader-installer-" & Pair.Key & If(Pair.Key = "1.8" OrElse Pair.Key = "1.9", ".0", "") & "-00-SNAPSHOT.jar",
  986. .MD5 = RealEntry("md5"),
  987. .ReleaseTime = GetLocalTime(GetDate(RealEntry("timestamp"))).ToString("yyyy'/'MM'/'dd HH':'mm"),
  988. .JsonToken = RealEntry
  989. })
  990. Next
  991. Loader.Output = New DlLiteLoaderListResult With {.IsOfficial = True, .SourceName = "LiteLoader 官方源", .Value = Versions}
  992. Catch ex As Exception
  993. Throw New Exception("LiteLoader 官方源版本列表解析失败(" & Result.ToString & ")", ex)
  994. End Try
  995. End Sub
  996. ''' <summary>
  997. ''' LiteLoader 版本列表,BMCLAPI。
  998. ''' </summary>
  999. Public DlLiteLoaderListBmclapiLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Bmclapi", AddressOf DlLiteLoaderListBmclapiMain)
  1000. Private Sub DlLiteLoaderListBmclapiMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  1001. Dim Result As JObject = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/maven/com/mumfrey/liteloader/versions.json", RequireJson:=True).DeserializeJson()
  1002. Try
  1003. Dim Json As JObject = Result("versions")
  1004. Dim Versions As New List(Of DlLiteLoaderListEntry)
  1005. For Each Pair As KeyValuePair(Of String, JToken) In Json
  1006. If Pair.Key.StartsWithF("1.6") OrElse Pair.Key.StartsWithF("1.5") Then Continue For
  1007. Dim RealEntry As JToken = If(Pair.Value("artefacts"), Pair.Value("snapshots"))("com.mumfrey:liteloader")("latest")
  1008. Versions.Add(New DlLiteLoaderListEntry With {
  1009. .Inherit = Pair.Key,
  1010. .IsLegacy = Pair.Key.Split(".")(1) < 8,
  1011. .IsPreview = RealEntry("stream").ToString.Lower = "snapshot",
  1012. .FileName = "liteloader-installer-" & Pair.Key & If(Pair.Key = "1.8" OrElse Pair.Key = "1.9", ".0", "") & "-00-SNAPSHOT.jar",
  1013. .MD5 = RealEntry("md5"),
  1014. .ReleaseTime = GetLocalTime(GetDate(RealEntry("timestamp"))).ToString("yyyy'/'MM'/'dd HH':'mm"),
  1015. .JsonToken = RealEntry
  1016. })
  1017. Next
  1018. Loader.Output = New DlLiteLoaderListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Versions}
  1019. Catch ex As Exception
  1020. Throw New Exception("LiteLoader BMCLAPI 版本列表解析失败(" & Result.ToString & ")", ex)
  1021. End Try
  1022. End Sub
  1023. #End Region
  1024. #Region "DlFabricList | Fabric 列表"
  1025. Public Structure DlFabricListResult
  1026. ''' <summary>
  1027. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  1028. ''' </summary>
  1029. Public SourceName As String
  1030. ''' <summary>
  1031. ''' 是否为官方的实时数据。
  1032. ''' </summary>
  1033. Public IsOfficial As Boolean
  1034. ''' <summary>
  1035. ''' 获取到的数据。
  1036. ''' </summary>
  1037. Public Value As JObject
  1038. End Structure
  1039. ''' <summary>
  1040. ''' Fabric 列表,主加载器。
  1041. ''' </summary>
  1042. Public DlFabricListLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Main", AddressOf DlFabricListMain)
  1043. Private Sub DlFabricListMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  1044. Select Case Settings.Get(Of Integer)("ToolDownloadVersion")
  1045. Case 0
  1046. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  1047. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 30),
  1048. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 30 + 60)
  1049. }, Loader.IsForceRestarting)
  1050. Case 1
  1051. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  1052. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 5),
  1053. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 5 + 30)
  1054. }, Loader.IsForceRestarting)
  1055. Case Else
  1056. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  1057. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 60),
  1058. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 60 + 60)
  1059. }, Loader.IsForceRestarting)
  1060. End Select
  1061. End Sub
  1062. ''' <summary>
  1063. ''' Fabric 列表,官方源。
  1064. ''' </summary>
  1065. Public DlFabricListOfficialLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Official", AddressOf DlFabricListOfficialMain)
  1066. Private Sub DlFabricListOfficialMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  1067. Dim Result As JObject = NetRequestByClientRetry("https://meta.fabricmc.net/v2/versions", RequireJson:=True).DeserializeJson()
  1068. Try
  1069. Dim Output = New DlFabricListResult With {.IsOfficial = True, .SourceName = "Fabric 官方源", .Value = Result}
  1070. If Output.Value("game") Is Nothing OrElse Output.Value("loader") Is Nothing OrElse Output.Value("installer") Is Nothing Then Throw New Exception("获取到的列表缺乏必要项")
  1071. Loader.Output = Output
  1072. Catch ex As Exception
  1073. Throw New Exception("Fabric 官方源版本列表解析失败(" & Result.ToString & ")", ex)
  1074. End Try
  1075. End Sub
  1076. ''' <summary>
  1077. ''' Fabric 列表,BMCLAPI。
  1078. ''' </summary>
  1079. Public DlFabricListBmclapiLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Bmclapi", AddressOf DlFabricListBmclapiMain)
  1080. Private Sub DlFabricListBmclapiMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  1081. Dim Result As JObject = NetRequestByClientRetry("https://bmclapi2.bangbang93.com/fabric-meta/v2/versions", RequireJson:=True).DeserializeJson()
  1082. Try
  1083. Dim Output = New DlFabricListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Result}
  1084. If Output.Value("game") Is Nothing OrElse Output.Value("loader") Is Nothing OrElse Output.Value("installer") Is Nothing Then Throw New Exception("获取到的列表缺乏必要项")
  1085. Loader.Output = Output
  1086. Catch ex As Exception
  1087. Throw New Exception("Fabric BMCLAPI 版本列表解析失败(" & Result.ToString & ")", ex)
  1088. End Try
  1089. End Sub
  1090. ''' <summary>
  1091. ''' Fabric API 列表,官方源。
  1092. ''' </summary>
  1093. Public DlFabricApiLoader As New LoaderTask(Of Integer, List(Of ResourceVersion))("Fabric API List Loader",
  1094. Sub(Task As LoaderTask(Of Integer, List(Of ResourceVersion))) Task.Output = ResourceVersion.FromProjectId("fabric-api", ResourcePlatforms.Modrinth, LoadDependencies:=False))
  1095. ''' <summary>
  1096. ''' OptiFabric 列表,官方源。
  1097. ''' </summary>
  1098. Public DlOptiFabricLoader As New LoaderTask(Of Integer, List(Of ResourceVersion))("OptiFabric List Loader",
  1099. Sub(Task As LoaderTask(Of Integer, List(Of ResourceVersion))) Task.Output = ResourceVersion.FromProjectId("322385", ResourcePlatforms.CurseForge, LoadDependencies:=False))
  1100. #End Region
  1101. #Region "DlMod | Mod 镜像源请求"
  1102. ''' <summary>
  1103. ''' 对可能涉及 Mod 镜像源的请求进行处理,返回 JToken。
  1104. ''' 调用 NetRequest,会进行重试。
  1105. ''' </summary>
  1106. Public Function DlModRequest(Url As String, Optional Method As HttpMethod = Nothing,
  1107. Optional Content As String = Nothing, Optional ContentType As String = Nothing) As JToken
  1108. Dim Urls As New List(Of (Url As String, Timeout As Integer))
  1109. Dim McimUrl As String = DlSourceModGet(Url)
  1110. If McimUrl <> Url Then
  1111. Select Case Settings.Get(Of Integer)("ToolDownloadMod")
  1112. Case 0
  1113. Urls.Add((McimUrl, 10))
  1114. Urls.Add((McimUrl, 10))
  1115. Urls.Add((Url, 30))
  1116. Case 1
  1117. Urls.Add((Url, If(Url.Contains("modrinth"), 20, 10))) '至少 20s,要不然 Modrinth 返回不过来
  1118. Urls.Add((McimUrl, 10))
  1119. Urls.Add((Url, 30))
  1120. Urls.Add((McimUrl, 30))
  1121. Case Else
  1122. Urls.Add((Url, If(Url.Contains("modrinth"), 20, 10)))
  1123. Urls.Add((Url, 30))
  1124. Urls.Add((McimUrl, 30))
  1125. End Select
  1126. Else
  1127. Urls.Add((Url, If(Url.Contains("modrinth"), 20, 10)))
  1128. Urls.Add((Url, 30))
  1129. End If
  1130. Dim Errors As New List(Of Exception)
  1131. For Each Source In Urls
  1132. Try
  1133. Return NetRequestByClient(Source.Url, Method, Content, ContentType, Timeout:=Source.Timeout * 1000, Encoding:=Encoding.UTF8, RequireJson:=True).DeserializeJson()
  1134. Catch ex As Exception
  1135. Errors.Add(ex)
  1136. End Try
  1137. Next
  1138. Throw New AggregateException(Errors)
  1139. End Function
  1140. #End Region
  1141. #Region "DlSource | 镜像下载源"
  1142. Private DlPreferMojang As Boolean = False
  1143. ''' <summary>
  1144. ''' 下载文件(而非获取版本列表)的时候,是否优先使用官方源。
  1145. ''' </summary>
  1146. Public ReadOnly Property DlSourcePreferMojang As Boolean
  1147. Get
  1148. Return Settings.Get(Of Integer)("ToolDownloadSource") = 2 OrElse (Settings.Get(Of Integer)("ToolDownloadSource") = 1 AndAlso DlPreferMojang)
  1149. End Get
  1150. End Property
  1151. ''' <summary>
  1152. ''' 下载文件(而非获取版本列表)的时候,根据是否优先使用官方源决定使用 Url 的顺序。
  1153. ''' </summary>
  1154. Public Function DlSourceOrder(OfficialUrls As IEnumerable(Of String), MirrorUrls As IEnumerable(Of String)) As IEnumerable(Of String)
  1155. Return If(DlSourcePreferMojang, OfficialUrls.Union(MirrorUrls), MirrorUrls.Union(OfficialUrls))
  1156. End Function
  1157. ''' <summary>
  1158. ''' 获取版本列表(而非下载文件)的时候,是否优先使用官方源。
  1159. ''' </summary>
  1160. Public ReadOnly Property DlVersionListPreferMojang As Boolean
  1161. Get
  1162. Return Settings.Get(Of Integer)("ToolDownloadVersion") = 2 OrElse (Settings.Get(Of Integer)("ToolDownloadVersion") = 1 AndAlso DlPreferMojang)
  1163. End Get
  1164. End Property
  1165. ''' <summary>
  1166. ''' 获取版本列表(而非下载文件)的时候,根据是否优先使用官方源决定使用 Url 的顺序。
  1167. ''' </summary>
  1168. Public Function DlVersionListOrder(OfficialUrls As IEnumerable(Of String), MirrorUrls As IEnumerable(Of String)) As IEnumerable(Of String)
  1169. Return If(DlVersionListPreferMojang, OfficialUrls.Union(MirrorUrls), MirrorUrls.Union(OfficialUrls))
  1170. End Function
  1171. ''' <summary>
  1172. ''' 下载 Assets 文件。
  1173. ''' </summary>
  1174. Public Function DlSourceAssetsGet(Original As String) As IEnumerable(Of String)
  1175. Original = Original.Replace("http://resources.download.minecraft.net", "https://resources.download.minecraft.net")
  1176. Return DlSourceOrder(
  1177. {Original},
  1178. {Original.
  1179. Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/assets").
  1180. Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/assets").
  1181. Replace("https://resources.download.minecraft.net", "https://bmclapi2.bangbang93.com/assets")
  1182. })
  1183. End Function
  1184. ''' <summary>
  1185. ''' 下载 Libraries 文件。
  1186. ''' </summary>
  1187. Public Function DlSourceLibraryGet(Original As String) As IEnumerable(Of String)
  1188. If {"minecraftforge", "fabricmc", "neoforged"}.Any(Function(k) Original.Contains(k)) Then '不添加原版源
  1189. Return {
  1190. Original.
  1191. Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/maven").
  1192. Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/maven").
  1193. Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/maven"),
  1194. Original.
  1195. Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/libraries").
  1196. Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/libraries").
  1197. Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/libraries")
  1198. }
  1199. Else
  1200. Return DlSourceOrder(
  1201. {Original},
  1202. {Original.
  1203. Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/maven").
  1204. Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/maven").
  1205. Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/maven"),
  1206. Original.
  1207. Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/libraries").
  1208. Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/libraries").
  1209. Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/libraries")
  1210. })
  1211. End If
  1212. End Function
  1213. ''' <summary>
  1214. ''' 下载 Launcher 或 Meta 文件。
  1215. ''' 不应使用它来获取版本列表(因为它只使用文件下载源设置来决定源顺序)。
  1216. ''' </summary>
  1217. Public Function DlSourceLauncherOrMetaGet(Original As String) As IEnumerable(Of String)
  1218. If Original Is Nothing Then Throw New Exception("无对应的 json 下载地址")
  1219. Return DlSourceOrder(
  1220. {Original},
  1221. {Original.
  1222. Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com").
  1223. Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com").
  1224. Replace("https://launcher.mojang.com", "https://bmclapi2.bangbang93.com").
  1225. Replace("https://launchermeta.mojang.com", "https://bmclapi2.bangbang93.com")
  1226. })
  1227. End Function
  1228. 'Mod 下载源
  1229. Public Function DlSourceModGet(Original As String) As String
  1230. Return Original.
  1231. Replace("api.modrinth.com", "mod.mcimirror.top/modrinth").
  1232. Replace("staging-api.modrinth.com", "mod.mcimirror.top/modrinth").
  1233. Replace("cdn.modrinth.com", "mod.mcimirror.top").
  1234. Replace("api.curseforge.com", "mod.mcimirror.top/curseforge").
  1235. Replace("edge.forgecdn.net", "mod.mcimirror.top").
  1236. Replace("mediafilez.forgecdn.net", "mod.mcimirror.top").
  1237. Replace("media.forgecdn.net", "mod.mcimirror.top")
  1238. End Function
  1239. 'Loader 自动切换
  1240. Private Sub DlSourceLoader(Of InputType, OutputType)(MainLoader As LoaderTask(Of InputType, OutputType),
  1241. LoaderList As List(Of KeyValuePair(Of LoaderTask(Of InputType, OutputType), Integer)),
  1242. Optional IsForceRestart As Boolean = False)
  1243. Dim WaitCycle As Integer = 0
  1244. Do While True
  1245. '检查状态
  1246. Dim BeforeLoadersAllFailed As Boolean = True
  1247. For Each SubLoader In LoaderList
  1248. If WaitCycle = 0 Then '判断是否可以不加载,直接使用已经加载好的结果
  1249. If IsForceRestart Then Continue For '强制刷新,不行
  1250. If (SubLoader.Key.Input Is Nothing Xor MainLoader.Input Is Nothing) OrElse
  1251. (SubLoader.Key.Input IsNot Nothing AndAlso Not SubLoader.Key.Input.Equals(MainLoader.Input)) Then Continue For '父子加载器的输入不一样,也不行
  1252. End If
  1253. If SubLoader.Key.State <> LoadState.Failed Then BeforeLoadersAllFailed = False
  1254. If SubLoader.Key.State = LoadState.Finished Then
  1255. '检查加载器成功
  1256. MainLoader.Output = SubLoader.Key.Output
  1257. DlSourceLoaderCancel(LoaderList)
  1258. Return
  1259. ElseIf BeforeLoadersAllFailed Then
  1260. '此前的加载器全部失败,直接启动后续加载器
  1261. If WaitCycle < SubLoader.Value * 100 Then WaitCycle = SubLoader.Value * 100
  1262. End If
  1263. Next
  1264. '第一轮时:既然不直接使用已经加载好的结果,那就启动第一个加载器
  1265. If WaitCycle = 0 Then
  1266. LoaderList.First.Key.Start(MainLoader.Input, IsForceRestart)
  1267. For Each Loader In LoaderList.Skip(1)
  1268. Loader.Key.State = LoadState.Waiting '将其他源标记为未启动,以确保可以切换下载源(#184)
  1269. Next
  1270. End If
  1271. '检查加载器失败或超时
  1272. For i = 0 To LoaderList.Count - 1
  1273. If WaitCycle <> LoaderList(i).Value * 100 Then Continue For
  1274. If i < LoaderList.Count - 1 AndAlso Not LoaderList.All(Function(l) l.Key.State = LoadState.Failed) Then
  1275. '若还有下一个源,则启动下一个源
  1276. LoaderList(i + 1).Key.Start(MainLoader.Input, IsForceRestart)
  1277. Else
  1278. '若没有,则失败
  1279. Dim ErrorInfo As Exception = Nothing
  1280. For ii = 0 To LoaderList.Count - 1
  1281. LoaderList(ii).Key.Input = Nothing '重置输入,以免以同样的输入“重试加载”时直接失败
  1282. If LoaderList(ii).Key.Error IsNot Nothing Then
  1283. If ErrorInfo Is Nothing OrElse LoaderList(ii).Key.Error.Message = "无" Then
  1284. ErrorInfo = LoaderList(ii).Key.Error
  1285. End If
  1286. End If
  1287. Next
  1288. If ErrorInfo Is Nothing Then ErrorInfo = New TimeoutException("下载源连接超时")
  1289. DlSourceLoaderCancel(LoaderList)
  1290. Throw ErrorInfo
  1291. End If
  1292. Exit For
  1293. Next
  1294. '计时
  1295. Thread.Sleep(10)
  1296. WaitCycle += 1
  1297. '检查父加载器中断
  1298. If MainLoader.IsCanceled Then
  1299. DlSourceLoaderCancel(LoaderList)
  1300. Return
  1301. End If
  1302. Loop
  1303. End Sub
  1304. Private Sub DlSourceLoaderCancel(Of InputType, OutputType)(LoaderList As List(Of KeyValuePair(Of LoaderTask(Of InputType, OutputType), Integer)))
  1305. For Each Loader In LoaderList
  1306. If Loader.Key.State = LoadState.Loading Then Loader.Key.Cancel()
  1307. Next
  1308. End Sub
  1309. #End Region
  1310. End Module