ModDownload.vb 74 KB

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