ModDownload.vb 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. Public Module ModDownload
  2. #Region "DlClient* | Minecraft 客户端"
  3. ''' <summary>
  4. ''' 返回某 Minecraft 版本对应的原版主 Jar 文件的下载信息,要求对应依赖版本已存在。
  5. ''' 失败则抛出异常,不需要下载则返回 Nothing。
  6. ''' </summary>
  7. Public Function DlClientJarGet(Version As McVersion, ReturnNothingOnFileUseable As Boolean) As NetFile
  8. '获取底层继承版本
  9. Do While Not String.IsNullOrEmpty(Version.InheritVersion)
  10. Version = New McVersion(Version.InheritVersion)
  11. Loop
  12. '检查 Json 是否标准
  13. If Version.JsonObject("downloads") Is Nothing OrElse Version.JsonObject("downloads")("client") Is Nothing OrElse Version.JsonObject("downloads")("client")("url") Is Nothing Then
  14. Throw New Exception("底层版本 " & Version.Name & " 中无 jar 文件下载信息")
  15. End If
  16. '检查文件
  17. Dim Checker As New FileChecker(MinSize:=1024, ActualSize:=If(Version.JsonObject("downloads")("client")("size"), -1), Hash:=Version.JsonObject("downloads")("client")("sha1"))
  18. If ReturnNothingOnFileUseable Then
  19. '是否跳过
  20. Dim IsSetupSkip As Boolean = ShouldIgnoreFileCheck(Version)
  21. If IsSetupSkip AndAlso File.Exists(Version.Path & Version.Name & ".jar") Then Return Nothing '跳过校验
  22. If Checker.Check(Version.Path & Version.Name & ".jar") Is Nothing Then Return Nothing '通过校验
  23. End If
  24. '返回下载信息
  25. Dim JarUrl As String = Version.JsonObject("downloads")("client")("url")
  26. Return New NetFile(DlSourceLauncherOrMetaGet(JarUrl), Version.Path & Version.Name & ".jar", Checker)
  27. End Function
  28. ''' <summary>
  29. ''' 返回某 Minecraft 版本对应的原版主 AssetIndex 文件的下载信息,要求对应依赖版本已存在。
  30. ''' 若未找到,则会返回 Legacy 资源文件或 Nothing。
  31. ''' </summary>
  32. Public Function DlClientAssetIndexGet(Version As McVersion) As NetFile
  33. '获取底层继承版本
  34. Do While Not String.IsNullOrEmpty(Version.InheritVersion)
  35. Version = New McVersion(Version.InheritVersion)
  36. Loop
  37. '获取信息
  38. Dim IndexInfo = McAssetsGetIndex(Version, True, True)
  39. Dim IndexAddress As String = PathMcFolder & "assets\indexes\" & IndexInfo("id").ToString & ".json"
  40. Log("[Download] 版本 " & Version.Name & " 对应的资源文件索引为 " & IndexInfo("id").ToString)
  41. Dim IndexUrl As String = If(IndexInfo("url"), "")
  42. If IndexUrl = "" Then
  43. Return Nothing
  44. Else
  45. Return New NetFile(DlSourceLauncherOrMetaGet(IndexUrl), IndexAddress, New FileChecker(CanUseExistsFile:=False, IsJson:=True))
  46. End If
  47. End Function
  48. ''' <summary>
  49. ''' 构造补全某 Minecraft 版本的所有文件的加载器列表。失败会抛出异常。
  50. ''' </summary>
  51. Public Function DlClientFix(Version As McVersion, CheckAssetsHash As Boolean, AssetsIndexBehaviour As AssetsIndexExistsBehaviour, SkipAssetsDownloadWhileSetupRequired As Boolean) As List(Of LoaderBase)
  52. Dim Loaders As New List(Of LoaderBase)
  53. #Region "下载支持库文件"
  54. Dim LoadersLib As New List(Of LoaderBase) From {
  55. New LoaderTask(Of String, List(Of NetFile))("分析缺失支持库文件", Sub(Task As LoaderTask(Of String, List(Of NetFile))) Task.Output = McLibFix(Version)) With {.ProgressWeight = 1},
  56. New LoaderDownload("下载支持库文件", New List(Of NetFile)) With {.ProgressWeight = 15}
  57. }
  58. '构造加载器
  59. Loaders.Add(New LoaderCombo(Of String)("下载支持库文件(主加载器)", LoadersLib) With {.Block = False, .Show = False, .ProgressWeight = 16})
  60. #End Region
  61. #Region "下载资源文件"
  62. Dim IsSetupSkip As Boolean = ShouldIgnoreFileCheck(Version)
  63. If IsSetupSkip Then Log("[Download] 已跳过 Assets 下载")
  64. If (Not SkipAssetsDownloadWhileSetupRequired) OrElse Not IsSetupSkip Then
  65. Dim LoadersAssets As New List(Of LoaderBase)
  66. '获取资源文件索引地址
  67. LoadersAssets.Add(New LoaderTask(Of String, List(Of NetFile))("分析资源文件索引地址",
  68. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  69. Try
  70. Dim IndexFile = DlClientAssetIndexGet(Version)
  71. Dim IndexFileInfo As New FileInfo(IndexFile.LocalPath)
  72. If AssetsIndexBehaviour <> AssetsIndexExistsBehaviour.AlwaysDownload AndAlso IndexFile.Check.Check(IndexFile.LocalPath) Is Nothing Then
  73. Task.Output = New List(Of NetFile)
  74. Else
  75. Task.Output = New List(Of NetFile) From {IndexFile}
  76. End If
  77. Catch ex As Exception
  78. Throw New Exception("分析资源文件索引地址失败", ex)
  79. End Try
  80. End Sub) With {.ProgressWeight = 0.5, .Show = False})
  81. '下载资源文件索引
  82. LoadersAssets.Add(New LoaderDownload("下载资源文件索引", New List(Of NetFile)) With {.ProgressWeight = 2})
  83. '要求独立更新索引
  84. If AssetsIndexBehaviour = AssetsIndexExistsBehaviour.DownloadInBackground Then
  85. Dim LoadersAssetsUpdate As New List(Of LoaderBase)
  86. Dim TempAddress As String = Nothing
  87. Dim RealAddress As String = Nothing
  88. LoadersAssetsUpdate.Add(New LoaderTask(Of String, List(Of NetFile))("后台分析资源文件索引地址",
  89. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  90. Dim BackAssetsFile As NetFile = DlClientAssetIndexGet(Version)
  91. RealAddress = BackAssetsFile.LocalPath
  92. TempAddress = PathTemp & "Cache\" & BackAssetsFile.LocalName
  93. BackAssetsFile.LocalPath = TempAddress
  94. Task.Output = New List(Of NetFile) From {BackAssetsFile}
  95. End Sub))
  96. LoadersAssetsUpdate.Add(New LoaderDownload("后台下载资源文件索引", New List(Of NetFile)))
  97. LoadersAssetsUpdate.Add(New LoaderTask(Of List(Of NetFile), String)("后台复制资源文件索引",
  98. Sub(Task As LoaderTask(Of List(Of NetFile), String))
  99. CopyFile(TempAddress, RealAddress)
  100. McLaunchLog("后台更新资源文件索引成功:" & TempAddress)
  101. End Sub))
  102. Dim Updater As New LoaderCombo(Of String)("后台更新资源文件索引", LoadersAssetsUpdate)
  103. Log("[Download] 开始后台更新资源文件索引")
  104. Updater.Start()
  105. End If
  106. '获取资源文件地址
  107. LoadersAssets.Add(New LoaderTask(Of String, List(Of NetFile))("分析缺失资源文件",
  108. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  109. Task.Output = McAssetsFixList(McAssetsGetIndexName(Version), CheckAssetsHash, Task)
  110. End Sub) With {.ProgressWeight = 3})
  111. '下载资源文件
  112. LoadersAssets.Add(New LoaderDownload("下载资源文件", New List(Of NetFile)) With {.ProgressWeight = 25})
  113. '构造加载器
  114. Loaders.Add(New LoaderCombo(Of String)("下载资源文件(主加载器)", LoadersAssets) With {.Block = False, .Show = False, .ProgressWeight = 30.5})
  115. End If
  116. #End Region
  117. Return Loaders
  118. End Function
  119. Public Enum AssetsIndexExistsBehaviour
  120. ''' <summary>
  121. ''' 如果文件存在,则不进行下载。
  122. ''' </summary>
  123. DontDownload
  124. ''' <summary>
  125. ''' 如果文件存在,则启动新的下载加载器进行独立的更新。
  126. ''' </summary>
  127. DownloadInBackground
  128. ''' <summary>
  129. ''' 如果文件存在,也同样进行下载。
  130. ''' </summary>
  131. AlwaysDownload
  132. End Enum
  133. #End Region
  134. #Region "DlClientList | Minecraft 客户端 版本列表"
  135. '主加载器
  136. Public Structure DlClientListResult
  137. ''' <summary>
  138. ''' 数据来源名称,如“Mojang”,“BMCLAPI”。
  139. ''' </summary>
  140. Public SourceName As String
  141. ''' <summary>
  142. ''' 是否为官方的实时数据。
  143. ''' </summary>
  144. Public IsOfficial As Boolean
  145. ''' <summary>
  146. ''' 获取到的 Json 数据。
  147. ''' </summary>
  148. Public Value As JObject
  149. '''' <summary>
  150. '''' 官方源的失败原因。若没有则为 Nothing。
  151. '''' </summary>
  152. 'Public OfficialError As Exception
  153. End Structure
  154. ''' <summary>
  155. ''' Minecraft 客户端 版本列表,主加载器。
  156. ''' </summary>
  157. Public DlClientListLoader As New LoaderTask(Of Integer, DlClientListResult)("DlClientList Main", AddressOf DlClientListMain)
  158. Private Sub DlClientListMain(Loader As LoaderTask(Of Integer, DlClientListResult))
  159. Select Case Setup.Get("ToolDownloadVersion")
  160. Case 0
  161. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)) From {
  162. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListBmclapiLoader, 30),
  163. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListMojangLoader, 60)
  164. }, Loader.IsForceRestarting)
  165. Case 1
  166. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)) From {
  167. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListMojangLoader, 5),
  168. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListBmclapiLoader, 35)
  169. }, Loader.IsForceRestarting)
  170. Case Else
  171. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)) From {
  172. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListMojangLoader, 60),
  173. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListBmclapiLoader, 60)
  174. }, Loader.IsForceRestarting)
  175. End Select
  176. End Sub
  177. '各个下载源的分加载器
  178. ''' <summary>
  179. ''' Minecraft 客户端 版本列表,Mojang 官方源加载器。
  180. ''' </summary>
  181. Public DlClientListMojangLoader As New LoaderTask(Of Integer, DlClientListResult)("DlClientList Mojang", AddressOf DlClientListMojangMain)
  182. Private IsNewClientVersionHinted As Boolean = False
  183. Private Sub DlClientListMojangMain(Loader As LoaderTask(Of Integer, DlClientListResult))
  184. Dim Json As JObject = NetGetCodeByRequestRetry("https://launchermeta.mojang.com/mc/game/version_manifest.json", IsJson:=True)
  185. Try
  186. Dim Versions As JArray = Json("versions")
  187. If Versions.Count < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Json.ToString & ")")
  188. '添加 PCL 特供项
  189. If File.Exists(PathTemp & "Cache\download.json") Then Versions.Merge(GetJson(ReadFile(PathTemp & "Cache\download.json")))
  190. '返回
  191. Loader.Output = New DlClientListResult With {.IsOfficial = True, .SourceName = "Mojang 官方源", .Value = Json}
  192. '解析更新提示(Release)
  193. Dim Version As String = Json("latest")("release")
  194. If Setup.Get("ToolUpdateRelease") AndAlso Not Setup.Get("ToolUpdateReleaseLast") = "" AndAlso Version IsNot Nothing AndAlso Not Setup.Get("ToolUpdateReleaseLast") = Version Then
  195. McDownloadClientUpdateHint(Version, Json)
  196. IsNewClientVersionHinted = True
  197. End If
  198. McVersionHighest = Version.Split(".")(1)
  199. Setup.Set("ToolUpdateReleaseLast", Version)
  200. '解析更新提示(Snapshot)
  201. Version = Json("latest")("snapshot")
  202. If Setup.Get("ToolUpdateSnapshot") AndAlso Not Setup.Get("ToolUpdateSnapshotLast") = "" AndAlso Version IsNot Nothing AndAlso Not Setup.Get("ToolUpdateSnapshotLast") = Version AndAlso Not IsNewClientVersionHinted Then
  203. McDownloadClientUpdateHint(Version, Json)
  204. End If
  205. Setup.Set("ToolUpdateSnapshotLast", If(Version, "Nothing"))
  206. Catch ex As Exception
  207. Throw New Exception("版本列表解析失败", ex)
  208. End Try
  209. End Sub
  210. ''' <summary>
  211. ''' Minecraft 客户端 版本列表,BMCLAPI 源加载器。
  212. ''' </summary>
  213. Public DlClientListBmclapiLoader As New LoaderTask(Of Integer, DlClientListResult)("DlClientList Bmclapi", AddressOf DlClientListBmclapiMain)
  214. Private Sub DlClientListBmclapiMain(Loader As LoaderTask(Of Integer, DlClientListResult))
  215. Dim Json As JObject = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/mc/game/version_manifest.json", IsJson:=True)
  216. Try
  217. Dim Versions As JArray = Json("versions")
  218. If Versions.Count < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Json.ToString & ")")
  219. '添加 PCL 特供项
  220. If File.Exists(PathTemp & "Cache\download.json") Then Versions.Merge(GetJson(ReadFile(PathTemp & "Cache\download.json")))
  221. '返回
  222. Loader.Output = New DlClientListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Json}
  223. Catch ex As Exception
  224. Throw New Exception("版本列表解析失败(" & Json.ToString & ")", ex)
  225. End Try
  226. End Sub
  227. ''' <summary>
  228. ''' 获取某个版本的 Json 下载地址,若失败则返回 Nothing。必须在工作线程执行。
  229. ''' </summary>
  230. Public Function DlClientListGet(Id As String)
  231. Try
  232. '确认 Minecraft 版本列表已完成获取
  233. Select Case DlClientListLoader.State
  234. Case LoadState.Loading
  235. DlClientListLoader.WaitForExit()
  236. Case LoadState.Failed, LoadState.Aborted, LoadState.Waiting
  237. DlClientListLoader.WaitForExit(IsForceRestart:=True)
  238. End Select
  239. '确认版本格式标准
  240. Id = Id.Replace("_", "-") '1.7.10_pre4 在版本列表中显示为 1.7.10-pre4
  241. If Id <> "1.0" AndAlso Id.EndsWithF(".0") Then Id = Left(Id, Id.Length - 2) 'OptiFine 1.8 的下载会触发此问题,显示版本为 1.8.0
  242. '查找版本并开始
  243. For Each Version As JObject In DlClientListLoader.Output.Value("versions")
  244. If Version("id") = Id Then
  245. Return Version("url").ToString
  246. End If
  247. Next
  248. Log("未发现版本 " & Id & " 的 json 下载地址,版本列表返回为:" & vbCrLf & DlClientListLoader.Output.Value.ToString, LogLevel.Debug)
  249. Return Nothing
  250. Catch ex As Exception
  251. Log(ex, "获取版本 " & Id & " 的 json 下载地址失败")
  252. Return Nothing
  253. End Try
  254. End Function
  255. #End Region
  256. #Region "DlOptiFineList | OptiFine 版本列表"
  257. Public Structure DlOptiFineListResult
  258. ''' <summary>
  259. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  260. ''' </summary>
  261. Public SourceName As String
  262. ''' <summary>
  263. ''' 是否为官方的实时数据。
  264. ''' </summary>
  265. Public IsOfficial As Boolean
  266. ''' <summary>
  267. ''' 获取到的数据。
  268. ''' </summary>
  269. Public Value As List(Of DlOptiFineListEntry)
  270. End Structure
  271. Public Class DlOptiFineListEntry
  272. ''' <summary>
  273. ''' 显示名称,已去除 HD_U 字样,如“1.12.2 C8”。
  274. ''' </summary>
  275. Public NameDisplay As String
  276. ''' <summary>
  277. ''' 原始文件名称,如“preview_OptiFine_1.11_HD_U_E1_pre.jar”。
  278. ''' </summary>
  279. Public NameFile As String
  280. ''' <summary>
  281. ''' 对应的版本名称,如“1.13.2-OptiFine_HD_U_E6”。
  282. ''' </summary>
  283. Public NameVersion As String
  284. ''' <summary>
  285. ''' 是否为测试版。
  286. ''' </summary>
  287. Public IsPreview As Boolean
  288. ''' <summary>
  289. ''' 对应的 Minecraft 版本,如“1.12.2”。
  290. ''' </summary>
  291. Public Property Inherit As String
  292. Get
  293. Return _inherit
  294. End Get
  295. Set(value As String)
  296. If value.EndsWithF(".0") Then value = Left(value, value.Length - 2)
  297. _inherit = value
  298. End Set
  299. End Property
  300. Private _inherit As String
  301. ''' <summary>
  302. ''' 发布时间,格式为“yyyy/mm/dd”。OptiFine 源无此数据。
  303. ''' </summary>
  304. Public ReleaseTime As String
  305. ''' <summary>
  306. ''' 需要的最低 Forge 版本。空字符串为无限制,Nothing 为不兼容,“28.1.56” 表示版本号,“1161” 表示版本号的最后一位。
  307. ''' </summary>
  308. Public RequiredForgeVersion As String
  309. End Class
  310. ''' <summary>
  311. ''' OptiFine 版本列表,主加载器。
  312. ''' </summary>
  313. Public DlOptiFineListLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Main", AddressOf DlOptiFineListMain)
  314. Private Sub DlOptiFineListMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  315. Select Case Setup.Get("ToolDownloadVersion")
  316. Case 0
  317. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  318. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 30),
  319. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 60)
  320. }, Loader.IsForceRestarting)
  321. Case 1
  322. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  323. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 5),
  324. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 35)
  325. }, Loader.IsForceRestarting)
  326. Case Else
  327. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  328. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 60),
  329. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 60)
  330. }, Loader.IsForceRestarting)
  331. End Select
  332. End Sub
  333. ''' <summary>
  334. ''' OptiFine 版本列表,官方源。
  335. ''' </summary>
  336. Public DlOptiFineListOfficialLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Official", AddressOf DlOptiFineListOfficialMain)
  337. Private Sub DlOptiFineListOfficialMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  338. Dim Result As String = NetGetCodeByClient("https://optifine.net/downloads", Encoding.Default)
  339. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  340. '获取所有版本信息
  341. Dim Forge As List(Of String) = RegexSearch(Result, "(?<=colForge'>)[^<]*")
  342. Dim ReleaseTime As List(Of String) = RegexSearch(Result, "(?<=colDate'>)[^<]+")
  343. Dim Name As List(Of String) = RegexSearch(Result, "(?<=OptiFine_)[0-9A-Za-z_.]+(?=.jar"")")
  344. If Not ReleaseTime.Count = Name.Count Then Throw New Exception("版本与发布时间数据无法对应")
  345. If Not Forge.Count = Name.Count Then Throw New Exception("版本与 Forge 兼容数据无法对应")
  346. If ReleaseTime.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  347. '转化为列表输出
  348. Dim Versions As New List(Of DlOptiFineListEntry)
  349. For i = 0 To ReleaseTime.Count - 1
  350. Name(i) = Name(i).Replace("_", " ")
  351. Dim Entry As New DlOptiFineListEntry With {
  352. .NameDisplay = Name(i).Replace("HD U ", "").Replace(".0 ", " "),
  353. .ReleaseTime = Join({ReleaseTime(i).Split(".")(2), ReleaseTime(i).Split(".")(1), ReleaseTime(i).Split(".")(0)}, "/"),
  354. .IsPreview = Name(i).ContainsF("pre", True),
  355. .Inherit = Name(i).ToString.Split(" ")(0),
  356. .NameFile = If(Name(i).ContainsF("pre", True), "preview_", "") & "OptiFine_" & Name(i).Replace(" ", "_") & ".jar",
  357. .RequiredForgeVersion = Forge(i).Replace("Forge ", "").Replace("#", "")}
  358. If Entry.RequiredForgeVersion.Contains("N/A") Then Entry.RequiredForgeVersion = Nothing
  359. Entry.NameVersion = Entry.Inherit & "-OptiFine_" & Name(i).ToString.Replace(" ", "_").Replace(Entry.Inherit & "_", "")
  360. Versions.Add(Entry)
  361. Next
  362. Loader.Output = New DlOptiFineListResult With {.IsOfficial = True, .SourceName = "OptiFine 官方源", .Value = Versions}
  363. End Sub
  364. ''' <summary>
  365. ''' OptiFine 版本列表,BMCLAPI。
  366. ''' </summary>
  367. Public DlOptiFineListBmclapiLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Bmclapi", AddressOf DlOptiFineListBmclapiMain)
  368. Private Sub DlOptiFineListBmclapiMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  369. Dim Json As JArray = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/optifine/versionList", IsJson:=True)
  370. Try
  371. Dim Versions As New List(Of DlOptiFineListEntry)
  372. For Each Token As JObject In Json
  373. Dim Entry As New DlOptiFineListEntry With {
  374. .NameDisplay = (Token("mcversion").ToString & Token("type").ToString.Replace("HD_U", "").Replace("_", " ") & " " & Token("patch").ToString).Replace(".0 ", " "),
  375. .ReleaseTime = "",
  376. .IsPreview = Token("patch").ToString.ContainsF("pre", True),
  377. .Inherit = Token("mcversion").ToString,
  378. .NameFile = Token("filename").ToString,
  379. .RequiredForgeVersion = If(Token("forge"), "").ToString.Replace("Forge ", "").Replace("#", "")
  380. }
  381. If Entry.RequiredForgeVersion.Contains("N/A") Then Entry.RequiredForgeVersion = Nothing
  382. Entry.NameVersion = Entry.Inherit & "-OptiFine_" & (Token("type").ToString & " " & Token("patch").ToString).Replace(".0 ", " ").Replace(" ", "_").Replace(Entry.Inherit & "_", "")
  383. Versions.Add(Entry)
  384. Next
  385. Loader.Output = New DlOptiFineListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Versions}
  386. Catch ex As Exception
  387. Throw New Exception("版本列表解析失败(" & Json.ToString & ")", ex)
  388. End Try
  389. End Sub
  390. #End Region
  391. #Region "DlForgeList | Forge Minecraft 版本列表"
  392. Public Structure DlForgeListResult
  393. ''' <summary>
  394. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  395. ''' </summary>
  396. Public SourceName As String
  397. ''' <summary>
  398. ''' 是否为官方的实时数据。
  399. ''' </summary>
  400. Public IsOfficial As Boolean
  401. ''' <summary>
  402. ''' 获取到的数据。
  403. ''' </summary>
  404. Public Value As List(Of String)
  405. End Structure
  406. ''' <summary>
  407. ''' Forge 版本列表,主加载器。
  408. ''' </summary>
  409. Public DlForgeListLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Main", AddressOf DlForgeListMain)
  410. Private Sub DlForgeListMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  411. Select Case Setup.Get("ToolDownloadVersion")
  412. Case 0
  413. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  414. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 30),
  415. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 60)
  416. }, Loader.IsForceRestarting)
  417. Case 1
  418. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  419. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 5),
  420. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 35)
  421. }, Loader.IsForceRestarting)
  422. Case Else
  423. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  424. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 60),
  425. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 60)
  426. }, Loader.IsForceRestarting)
  427. End Select
  428. End Sub
  429. ''' <summary>
  430. ''' Forge 版本列表,官方源。
  431. ''' </summary>
  432. Public DlForgeListOfficialLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Official", AddressOf DlForgeListOfficialMain)
  433. Private Sub DlForgeListOfficialMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  434. Dim Result As String = NetGetCodeByRequestRetry("https://files.minecraftforge.net/maven/net/minecraftforge/forge/index_1.2.4.html", Encoding.Default, "text/html", UseBrowserUserAgent:=True)
  435. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  436. '获取所有版本信息
  437. Dim Names As List(Of String) = RegexSearch(Result, "(?<=a href=""index_)[0-9.]+(_pre[0-9]?)?(?=.html)")
  438. Names.Add("1.2.4") '1.2.4 不会被匹配上
  439. If Names.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  440. Loader.Output = New DlForgeListResult With {.IsOfficial = True, .SourceName = "Forge 官方源", .Value = Names}
  441. End Sub
  442. ''' <summary>
  443. ''' Forge 版本列表,BMCLAPI。
  444. ''' </summary>
  445. Public DlForgeListBmclapiLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Bmclapi", AddressOf DlForgeListBmclapiMain)
  446. Private Sub DlForgeListBmclapiMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  447. Dim Result As String = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/forge/minecraft", Encoding.Default)
  448. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  449. '获取所有版本信息
  450. Dim Names As List(Of String) = RegexSearch(Result, "[0-9.]+(_pre[0-9]?)?")
  451. If Names.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  452. Loader.Output = New DlForgeListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Names}
  453. End Sub
  454. #End Region
  455. #Region "DlForgeVersion | Forge 版本列表"
  456. Public Class DlForgeVersionEntry
  457. ''' <summary>
  458. ''' 完整的版本名,如 “14.22.1.2478”。
  459. ''' </summary>
  460. Public Version As String
  461. ''' <summary>
  462. ''' 对应的 Minecraft 版本,如“1.12.2”。
  463. ''' </summary>
  464. Public Inherit As String
  465. ''' <summary>
  466. ''' 发布时间,格式为“yyyy/MM/dd HH:mm”。
  467. ''' </summary>
  468. Public ReleaseTime As String
  469. ''' <summary>
  470. ''' 文件的 MD5 或 SHA1(BMCLAPI 的老版本是 MD5,新版本是 SHA1;官方源总是 MD5)。
  471. ''' </summary>
  472. Public Hash As String = Nothing
  473. ''' <summary>
  474. ''' 是否为推荐版本。
  475. ''' </summary>
  476. Public IsRecommended As Boolean
  477. ''' <summary>
  478. ''' 安装类型。有 installer、client、universal 三种。
  479. ''' </summary>
  480. Public Category As String
  481. ''' <summary>
  482. ''' 版本分支。若无分支则为 Nothing。
  483. ''' </summary>
  484. Public Branch As String = Nothing
  485. ''' <summary>
  486. ''' 是否为新版 Forge。(即为 Minecraft 1.13+)
  487. ''' </summary>
  488. Public ReadOnly Property IsNewType As Boolean
  489. Get
  490. Return Version.Split(".")(0) >= 20
  491. End Get
  492. End Property
  493. ''' <summary>
  494. ''' 构建数。
  495. ''' </summary>
  496. Public ReadOnly Property Build As Integer
  497. Get
  498. Dim Version = Me.Version.Split(".")
  499. If Version(0) < 15 Then
  500. Return Version(Version.Count - 1)
  501. Else
  502. Return Version(Version.Count - 1) + 10000
  503. End If
  504. End Get
  505. End Property
  506. ''' <summary>
  507. ''' 用于下载的文件版本名。可能在 Version 的基础上添加了分支。
  508. ''' </summary>
  509. Public ReadOnly Property FileVersion As String
  510. Get
  511. Return Version & If(Branch Is Nothing, "", "-" & Branch)
  512. End Get
  513. End Property
  514. ''' <summary>
  515. ''' 即将下载的文件全名。
  516. ''' </summary>
  517. Public ReadOnly Property FileName As String
  518. Get
  519. Return "forge-" & Inherit & "-" & FileVersion & "-" & Category & "." & FileSuffix
  520. End Get
  521. End Property
  522. ''' <summary>
  523. ''' 文件扩展名。
  524. ''' </summary>
  525. Public ReadOnly Property FileSuffix As String
  526. Get
  527. If Category = "installer" Then
  528. Return "jar"
  529. Else
  530. Return "zip"
  531. End If
  532. End Get
  533. End Property
  534. End Class
  535. ''' <summary>
  536. ''' Forge 版本列表,主加载器。
  537. ''' </summary>
  538. Public Sub DlForgeVersionMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  539. Dim DlForgeVersionOfficialLoader As New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Official", AddressOf DlForgeVersionOfficialMain)
  540. Dim DlForgeVersionBmclapiLoader As New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Bmclapi", AddressOf DlForgeVersionBmclapiMain)
  541. Select Case Setup.Get("ToolDownloadVersion")
  542. Case 0
  543. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  544. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 30),
  545. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 60)
  546. }, Loader.IsForceRestarting)
  547. Case 1
  548. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  549. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 5),
  550. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 35)
  551. }, Loader.IsForceRestarting)
  552. Case Else
  553. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  554. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 60),
  555. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 60)
  556. }, Loader.IsForceRestarting)
  557. End Select
  558. End Sub
  559. ''' <summary>
  560. ''' Forge 版本列表,官方源。
  561. ''' </summary>
  562. Public Sub DlForgeVersionOfficialMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  563. Dim Result As String
  564. Try
  565. Result = NetGetCodeByDownload("https://files.minecraftforge.net/maven/net/minecraftforge/forge/index_" & Loader.Input & ".html", UseBrowserUserAgent:=True)
  566. Catch ex As Exception
  567. If GetExceptionSummary(ex).Contains("(404)") Then
  568. Throw New Exception("没有可用版本")
  569. Else
  570. Throw
  571. End If
  572. End Try
  573. If Result.Length < 1000 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  574. Dim Versions As New List(Of DlForgeVersionEntry)
  575. Try
  576. '分割版本信息
  577. Dim VersionCodes = Mid(Result, 1, Result.LastIndexOfF("</table>")).Split("<td class=""download-version")
  578. '获取所有版本信息
  579. For i = 1 To VersionCodes.Count - 1
  580. Dim VersionCode = VersionCodes(i)
  581. Try
  582. '基础信息获取
  583. Dim Name As String = RegexSeek(VersionCode, "(?<=[^(0-9)]+)[0-9\.]+")
  584. Dim IsRecommended As Boolean = VersionCode.Contains("fa promo-recommended")
  585. Dim Inherit As String = Loader.Input
  586. '分支获取
  587. Dim Branch As String = RegexSeek(VersionCode, $"(?<=-{Name}-)[^-""]+(?=-[a-z]+.[a-z]{{3}})")
  588. If String.IsNullOrWhiteSpace(Branch) Then Branch = Nothing
  589. If Name = "11.15.1.2318" OrElse Name = "11.15.1.1902" OrElse Name = "11.15.1.1890" Then Branch = "1.8.9"
  590. If Branch Is Nothing AndAlso Inherit = "1.7.10" AndAlso Name.Split(".")(3) >= 1300 Then Branch = "1.7.10"
  591. '发布时间获取
  592. Dim ReleaseTimeOriginal = RegexSeek(VersionCode, "(?<=""download-time"" title="")[^""]+")
  593. Dim ReleaseTimeSplit = ReleaseTimeOriginal.Split(" -:".ToCharArray) '原格式:"2021-02-15 03:24:02"
  594. Dim ReleaseDate As New Date(ReleaseTimeSplit(0), ReleaseTimeSplit(1), ReleaseTimeSplit(2), '年月日
  595. ReleaseTimeSplit(3), ReleaseTimeSplit(4), ReleaseTimeSplit(5), '时分秒
  596. 0, DateTimeKind.Utc) '以 UTC 时间作为标准
  597. Dim ReleaseTime As String = ReleaseDate.ToLocalTime.ToString("yyyy'/'MM'/'dd HH':'mm") '时区与格式转换
  598. '分类与 MD5 获取
  599. Dim MD5 As String, Category As String
  600. If VersionCode.Contains("classifier-installer""") Then
  601. '类型为 installer.jar,支持范围 ~753 (~ 1.6.1 部分), 738~684 (1.5.2 全部)
  602. VersionCode = VersionCode.Substring(VersionCode.IndexOfF("installer.jar"))
  603. MD5 = RegexSeek(VersionCode, "(?<=MD5:</strong> )[^<]+")
  604. Category = "installer"
  605. ElseIf VersionCode.Contains("classifier-universal""") Then
  606. '类型为 universal.zip,支持范围 751~449 (1.6.1 部分), 682~183 (1.5.1 ~ 1.3.2 部分)
  607. VersionCode = VersionCode.Substring(VersionCode.IndexOfF("universal.zip"))
  608. MD5 = RegexSeek(VersionCode, "(?<=MD5:</strong> )[^<]+")
  609. Category = "universal"
  610. ElseIf VersionCode.Contains("client.zip") Then
  611. '类型为 client.zip,支持范围 182~ (1.3.2 部分 ~)
  612. VersionCode = VersionCode.Substring(VersionCode.IndexOfF("client.zip"))
  613. MD5 = RegexSeek(VersionCode, "(?<=MD5:</strong> )[^<]+")
  614. Category = "client"
  615. Else
  616. '没有任何下载(1.6.4 有一部分这种情况)
  617. Continue For
  618. End If
  619. '添加进列表
  620. Versions.Add(New DlForgeVersionEntry With {.Category = Category, .Version = Name, .IsRecommended = IsRecommended, .Hash = MD5.Trim(vbCr, vbLf), .Inherit = Inherit, .ReleaseTime = ReleaseTime, .Branch = Branch})
  621. Catch ex As Exception
  622. Throw New Exception("版本信息提取失败(" & VersionCode & ")", ex)
  623. End Try
  624. Next
  625. Catch ex As Exception
  626. Throw New Exception("版本列表解析失败(" & Result & ")", ex)
  627. End Try
  628. If Not Versions.Any() Then Throw New Exception("没有可用版本")
  629. Loader.Output = Versions
  630. End Sub
  631. ''' <summary>
  632. ''' Forge 版本列表,BMCLAPI。
  633. ''' </summary>
  634. Public Sub DlForgeVersionBmclapiMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  635. Dim Json As JArray = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/forge/minecraft/" & Loader.Input, IsJson:=True)
  636. Dim Versions As New List(Of DlForgeVersionEntry)
  637. Try
  638. Dim Recommended As String = McDownloadForgeRecommendedGet(Loader.Input)
  639. For Each Token As JObject In Json
  640. '分类与 Hash 获取
  641. Dim Hash As String = Nothing, Category As String = "unknown", Proi As Integer = -1
  642. For Each File As JObject In Token("files")
  643. Select Case File("category").ToString
  644. Case "installer"
  645. If File("format").ToString = "jar" Then
  646. '类型为 installer.jar,支持范围 ~753 (~ 1.6.1 部分), 738~684 (1.5.2 全部)
  647. Hash = File("hash")
  648. Category = "installer"
  649. Proi = 2
  650. End If
  651. Case "universal"
  652. If Proi <= 1 AndAlso File("format").ToString = "zip" Then
  653. '类型为 universal.zip,支持范围 751~449 (1.6.1 部分), 682~183 (1.5.1 ~ 1.3.2 部分)
  654. Hash = File("hash")
  655. Category = "universal"
  656. Proi = 1
  657. End If
  658. Case "client"
  659. If Proi <= 0 AndAlso File("format").ToString = "zip" Then
  660. '类型为 client.zip,支持范围 182~ (1.3.2 部分 ~)
  661. Hash = File("hash")
  662. Category = "client"
  663. Proi = 0
  664. End If
  665. End Select
  666. Next
  667. '获取 Entry
  668. Dim Inherit As String = Loader.Input
  669. Dim Branch As String = Token("branch")
  670. Dim Name As String = Token("version")
  671. If Name = "11.15.1.2318" OrElse Name = "11.15.1.1902" OrElse Name = "11.15.1.1890" Then Branch = "1.8.9"
  672. If Branch Is Nothing AndAlso Inherit = "1.7.10" AndAlso Name.Split(".")(3) >= 1300 Then Branch = "1.7.10"
  673. '基础信息获取
  674. Dim Entry = New DlForgeVersionEntry With {.Hash = Hash, .Category = Category, .Version = Name, .Branch = Branch, .Inherit = Inherit, .IsRecommended = Recommended = Name}
  675. Dim TimeSplit = Token("modified").ToString.Split("-"c, "T"c, ":"c, "."c, " "c, "/"c)
  676. Entry.ReleaseTime = Token("modified").ToObject(Of Date).ToLocalTime.ToString("yyyy'/'MM'/'dd HH':'mm")
  677. '添加项
  678. Versions.Add(Entry)
  679. Next
  680. Catch ex As Exception
  681. Throw New Exception("版本列表解析失败(" & Json.ToString & ")", ex)
  682. End Try
  683. If Not Versions.Any() Then Throw New Exception("没有可用版本")
  684. Loader.Output = Versions
  685. End Sub
  686. #End Region
  687. #Region "DlLiteLoaderList | LiteLoader 版本列表"
  688. Public Structure DlLiteLoaderListResult
  689. ''' <summary>
  690. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  691. ''' </summary>
  692. Public SourceName As String
  693. ''' <summary>
  694. ''' 是否为官方的实时数据。
  695. ''' </summary>
  696. Public IsOfficial As Boolean
  697. ''' <summary>
  698. ''' 获取到的数据。
  699. ''' </summary>
  700. Public Value As List(Of DlLiteLoaderListEntry)
  701. ''' <summary>
  702. ''' 官方源的失败原因。若没有则为 Nothing。
  703. ''' </summary>
  704. Public OfficialError As Exception
  705. End Structure
  706. Public Class DlLiteLoaderListEntry
  707. ''' <summary>
  708. ''' 实际的文件名,如“liteloader-installer-1.12-00-SNAPSHOT.jar”。
  709. ''' </summary>
  710. Public FileName As String
  711. ''' <summary>
  712. ''' 是否为测试版。
  713. ''' </summary>
  714. Public IsPreview As Boolean
  715. ''' <summary>
  716. ''' 对应的 Minecraft 版本,如“1.12.2”。
  717. ''' </summary>
  718. Public Inherit As String
  719. ''' <summary>
  720. ''' 是否为 1.7 及更早的远古版。
  721. ''' </summary>
  722. Public IsLegacy As Boolean
  723. ''' <summary>
  724. ''' 发布时间,格式为“yyyy/mm/dd HH:mm”。
  725. ''' </summary>
  726. Public ReleaseTime As String
  727. ''' <summary>
  728. ''' 文件的 MD5。
  729. ''' </summary>
  730. Public MD5 As String
  731. ''' <summary>
  732. ''' 对应的 Json 项。
  733. ''' </summary>
  734. Public JsonToken As JToken
  735. End Class
  736. ''' <summary>
  737. ''' LiteLoader 版本列表,主加载器。
  738. ''' </summary>
  739. Public DlLiteLoaderListLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Main", AddressOf DlLiteLoaderListMain)
  740. Private Sub DlLiteLoaderListMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  741. Select Case Setup.Get("ToolDownloadVersion")
  742. Case 0
  743. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  744. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 30),
  745. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 60)
  746. }, Loader.IsForceRestarting)
  747. Case 1
  748. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  749. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 5),
  750. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 35)
  751. }, Loader.IsForceRestarting)
  752. Case Else
  753. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  754. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 60),
  755. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 60)
  756. }, Loader.IsForceRestarting)
  757. End Select
  758. End Sub
  759. ''' <summary>
  760. ''' LiteLoader 版本列表,官方源。
  761. ''' </summary>
  762. Public DlLiteLoaderListOfficialLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Official", AddressOf DlLiteLoaderListOfficialMain)
  763. Private Sub DlLiteLoaderListOfficialMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  764. Dim Result As JObject = NetGetCodeByRequestRetry("https://dl.liteloader.com/versions/versions.json", IsJson:=True)
  765. Try
  766. Dim Json As JObject = Result("versions")
  767. Dim Versions As New List(Of DlLiteLoaderListEntry)
  768. For Each Pair As KeyValuePair(Of String, JToken) In Json
  769. If Pair.Key.StartsWithF("1.6") OrElse Pair.Key.StartsWithF("1.5") Then Continue For
  770. Dim RealEntry As JToken = If(Pair.Value("artefacts"), Pair.Value("snapshots"))("com.mumfrey:liteloader")("latest")
  771. Versions.Add(New DlLiteLoaderListEntry With {
  772. .Inherit = Pair.Key,
  773. .IsLegacy = Pair.Key.Split(".")(1) < 8,
  774. .IsPreview = RealEntry("stream").ToString.ToLower = "snapshot",
  775. .FileName = "liteloader-installer-" & Pair.Key & If(Pair.Key = "1.8" OrElse Pair.Key = "1.9", ".0", "") & "-00-SNAPSHOT.jar",
  776. .MD5 = RealEntry("md5"),
  777. .ReleaseTime = GetLocalTime(GetDate(RealEntry("timestamp"))).ToString("yyyy'/'MM'/'dd HH':'mm"),
  778. .JsonToken = RealEntry
  779. })
  780. Next
  781. Loader.Output = New DlLiteLoaderListResult With {.IsOfficial = True, .SourceName = "LiteLoader 官方源", .Value = Versions}
  782. Catch ex As Exception
  783. Throw New Exception("版本列表解析失败(" & Result.ToString & ")", ex)
  784. End Try
  785. End Sub
  786. ''' <summary>
  787. ''' LiteLoader 版本列表,BMCLAPI。
  788. ''' </summary>
  789. Public DlLiteLoaderListBmclapiLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Bmclapi", AddressOf DlLiteLoaderListBmclapiMain)
  790. Private Sub DlLiteLoaderListBmclapiMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  791. Dim Result As JObject = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/maven/com/mumfrey/liteloader/versions.json", IsJson:=True)
  792. Try
  793. Dim Json As JObject = Result("versions")
  794. Dim Versions As New List(Of DlLiteLoaderListEntry)
  795. For Each Pair As KeyValuePair(Of String, JToken) In Json
  796. If Pair.Key.StartsWithF("1.6") OrElse Pair.Key.StartsWithF("1.5") Then Continue For
  797. Dim RealEntry As JToken = If(Pair.Value("artefacts"), Pair.Value("snapshots"))("com.mumfrey:liteloader")("latest")
  798. Versions.Add(New DlLiteLoaderListEntry With {
  799. .Inherit = Pair.Key,
  800. .IsLegacy = Pair.Key.Split(".")(1) < 8,
  801. .IsPreview = RealEntry("stream").ToString.ToLower = "snapshot",
  802. .FileName = "liteloader-installer-" & Pair.Key & If(Pair.Key = "1.8" OrElse Pair.Key = "1.9", ".0", "") & "-00-SNAPSHOT.jar",
  803. .MD5 = RealEntry("md5"),
  804. .ReleaseTime = GetLocalTime(GetDate(RealEntry("timestamp"))).ToString("yyyy'/'MM'/'dd HH':'mm"),
  805. .JsonToken = RealEntry
  806. })
  807. Next
  808. Loader.Output = New DlLiteLoaderListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Versions}
  809. Catch ex As Exception
  810. Throw New Exception("版本列表解析失败(" & Result.ToString & ")", ex)
  811. End Try
  812. End Sub
  813. #End Region
  814. #Region "DlFabricList | Fabric 列表"
  815. Public Structure DlFabricListResult
  816. ''' <summary>
  817. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  818. ''' </summary>
  819. Public SourceName As String
  820. ''' <summary>
  821. ''' 是否为官方的实时数据。
  822. ''' </summary>
  823. Public IsOfficial As Boolean
  824. ''' <summary>
  825. ''' 获取到的数据。
  826. ''' </summary>
  827. Public Value As JObject
  828. End Structure
  829. ''' <summary>
  830. ''' Fabric 列表,主加载器。
  831. ''' </summary>
  832. Public DlFabricListLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Main", AddressOf DlFabricListMain)
  833. Private Sub DlFabricListMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  834. Select Case Setup.Get("ToolDownloadVersion")
  835. Case 0
  836. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  837. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 30),
  838. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 60)
  839. }, Loader.IsForceRestarting)
  840. Case 1
  841. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  842. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 5),
  843. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 35)
  844. }, Loader.IsForceRestarting)
  845. Case Else
  846. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  847. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 60),
  848. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 60)
  849. }, Loader.IsForceRestarting)
  850. End Select
  851. End Sub
  852. ''' <summary>
  853. ''' Fabric 列表,官方源。
  854. ''' </summary>
  855. Public DlFabricListOfficialLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Official", AddressOf DlFabricListOfficialMain)
  856. Private Sub DlFabricListOfficialMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  857. Dim Result As JObject = NetGetCodeByRequestRetry("https://meta.fabricmc.net/v2/versions", IsJson:=True)
  858. Try
  859. Dim Output = New DlFabricListResult With {.IsOfficial = True, .SourceName = "Fabric 官方源", .Value = Result}
  860. If Output.Value("game") Is Nothing OrElse Output.Value("loader") Is Nothing OrElse Output.Value("installer") Is Nothing Then Throw New Exception("获取到的列表缺乏必要项")
  861. Loader.Output = Output
  862. Catch ex As Exception
  863. Throw New Exception("列表解析失败(" & Result.ToString & ")", ex)
  864. End Try
  865. End Sub
  866. ''' <summary>
  867. ''' Fabric 列表,BMCLAPI。
  868. ''' </summary>
  869. Public DlFabricListBmclapiLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Bmclapi", AddressOf DlFabricListBmclapiMain)
  870. Private Sub DlFabricListBmclapiMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  871. Dim Result As JObject = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/fabric-meta/v2/versions", IsJson:=True)
  872. Try
  873. Dim Output = New DlFabricListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Result}
  874. If Output.Value("game") Is Nothing OrElse Output.Value("loader") Is Nothing OrElse Output.Value("installer") Is Nothing Then Throw New Exception("获取到的列表缺乏必要项")
  875. Loader.Output = Output
  876. Catch ex As Exception
  877. Throw New Exception("列表解析失败(" & Result.ToString & ")", ex)
  878. End Try
  879. End Sub
  880. ''' <summary>
  881. ''' Fabric API 列表,官方源。
  882. ''' </summary>
  883. Public DlFabricApiLoader As New LoaderTask(Of Integer, List(Of CompFile))("Fabric API List Loader",
  884. Sub(Task As LoaderTask(Of Integer, List(Of CompFile))) Task.Output = CompFilesGet("fabric-api", False))
  885. ''' <summary>
  886. ''' OptiFabric 列表,官方源。
  887. ''' </summary>
  888. Public DlOptiFabricLoader As New LoaderTask(Of Integer, List(Of CompFile))("OptiFabric List Loader",
  889. Sub(Task As LoaderTask(Of Integer, List(Of CompFile))) Task.Output = CompFilesGet("322385", True))
  890. #End Region
  891. #Region "DlSource | 镜像下载源"
  892. Public Function DlSourceResourceGet(MojangBase As String) As String()
  893. MojangBase = MojangBase.Replace("http://resources.download.minecraft.net", "https://resources.download.minecraft.net")
  894. Return {
  895. MojangBase.Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/assets").Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/assets").Replace("https://resources.download.minecraft.net", "https://bmclapi2.bangbang93.com/assets"),
  896. MojangBase
  897. }
  898. End Function
  899. Public Function DlSourceLibraryGet(MojangBase As String) As String()
  900. Return {
  901. MojangBase.Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/maven").Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/maven").Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/maven"),
  902. MojangBase.Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com/libraries").Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com/libraries").Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/libraries"),
  903. MojangBase
  904. }
  905. End Function
  906. Public Function DlSourceLauncherOrMetaGet(MojangBase As String) As String()
  907. If MojangBase Is Nothing Then Throw New Exception("无对应的 json 下载地址")
  908. Return {
  909. MojangBase.Replace("https://piston-data.mojang.com", "https://bmclapi2.bangbang93.com").Replace("https://piston-meta.mojang.com", "https://bmclapi2.bangbang93.com").Replace("https://launcher.mojang.com", "https://bmclapi2.bangbang93.com").Replace("https://launchermeta.mojang.com", "https://bmclapi2.bangbang93.com"),
  910. MojangBase
  911. }
  912. End Function
  913. Private Sub DlSourceLoader(Of InputType, OutputType)(MainLoader As LoaderTask(Of InputType, OutputType),
  914. LoaderList As List(Of KeyValuePair(Of LoaderTask(Of InputType, OutputType), Integer)),
  915. Optional IsForceRestart As Boolean = False)
  916. Dim WaitCycle As Integer = 0
  917. Do While True
  918. '检查加载结束
  919. Dim IsAllFailed As Boolean = True
  920. For Each SubLoader In LoaderList
  921. If WaitCycle = 0 Then
  922. '如果要强制刷新,就不使用已经加载好的值
  923. If IsForceRestart Then Exit For
  924. '如果输入不一样,就不使用已经加载好的值
  925. If (SubLoader.Key.Input Is Nothing Xor MainLoader.Input Is Nothing) OrElse
  926. (SubLoader.Key.Input IsNot Nothing AndAlso Not SubLoader.Key.Input.Equals(MainLoader.Input)) Then Continue For
  927. End If
  928. If SubLoader.Key.State <> LoadState.Failed Then IsAllFailed = False
  929. If SubLoader.Key.State = LoadState.Finished Then
  930. MainLoader.Output = SubLoader.Key.Output
  931. DlSourceLoaderAbort(LoaderList)
  932. Exit Sub
  933. ElseIf IsAllFailed Then
  934. If WaitCycle < SubLoader.Value * 100 Then WaitCycle = SubLoader.Value * 100
  935. End If
  936. '由于 Forge BMCLAPI 没有可用版本导致强制失败
  937. '在没有可用版本时,官方源会一直卡住,直接使用 BMCLAPI 判定失败即可
  938. If SubLoader.Key.Error IsNot Nothing AndAlso SubLoader.Key.Error.Message.Contains("没有可用版本") Then
  939. For Each SubLoader2 In LoaderList
  940. If WaitCycle < SubLoader2.Value * 100 Then WaitCycle = SubLoader2.Value * 100
  941. Next
  942. End If
  943. Next
  944. '启动加载源
  945. If WaitCycle = 0 Then
  946. '启动第一个源
  947. LoaderList(0).Key.Start(MainLoader.Input, IsForceRestart)
  948. '将其他源标记为未启动,以确保可以切换下载源(#184)
  949. For i = 1 To LoaderList.Count - 1
  950. LoaderList(i).Key.State = LoadState.Waiting
  951. Next
  952. End If
  953. For i = 0 To LoaderList.Count - 1
  954. If WaitCycle = LoaderList(i).Value * 100 Then
  955. If i < LoaderList.Count - 1 Then
  956. '启动下一个源
  957. LoaderList(i + 1).Key.Start(MainLoader.Input, IsForceRestart)
  958. Else
  959. '失败
  960. Dim ErrorInfo As Exception = Nothing
  961. For ii = 0 To LoaderList.Count - 1
  962. LoaderList(ii).Key.Input = Nothing '重置输入,以免以同样的输入“重试加载”时直接失败
  963. If LoaderList(ii).Key.Error IsNot Nothing Then
  964. If ErrorInfo Is Nothing OrElse LoaderList(ii).Key.Error.Message.Contains("没有可用版本") Then
  965. ErrorInfo = LoaderList(ii).Key.Error
  966. End If
  967. End If
  968. Next
  969. If ErrorInfo Is Nothing Then ErrorInfo = New TimeoutException("下载源连接超时")
  970. DlSourceLoaderAbort(LoaderList)
  971. Throw ErrorInfo
  972. End If
  973. Exit For
  974. End If
  975. Next
  976. '计时
  977. If MainLoader.IsAborted Then
  978. DlSourceLoaderAbort(LoaderList)
  979. Exit Sub
  980. End If
  981. Thread.Sleep(10)
  982. WaitCycle += 1
  983. Loop
  984. End Sub
  985. Private Sub DlSourceLoaderAbort(Of InputType, OutputType)(LoaderList As List(Of KeyValuePair(Of LoaderTask(Of InputType, OutputType), Integer)))
  986. For Each Loader In LoaderList
  987. If Loader.Key.State = LoadState.Loading Then Loader.Key.Abort()
  988. Next
  989. End Sub
  990. #End Region
  991. End Module