ModDownload.vb 75 KB

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