ModDownload.vb 75 KB

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