ModDownload.vb 75 KB

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