ModDownload.vb 75 KB

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