ModDownload.vb 75 KB

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