ModDownload.vb 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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 = Setup.Get("LaunchAdvanceAssets")
  21. Select Case Setup.Get("VersionAdvanceAssets", Version:=Version)
  22. Case 0 '使用全局设置
  23. Case 1 '开启
  24. IsSetupSkip = False
  25. Case 2 '关闭
  26. IsSetupSkip = True
  27. End Select
  28. If IsSetupSkip AndAlso File.Exists(Version.Path & Version.Name & ".jar") Then Return Nothing '跳过校验
  29. If Checker.Check(Version.Path & Version.Name & ".jar") Is Nothing Then Return Nothing '通过校验
  30. End If
  31. '返回下载信息
  32. Dim JarUrl As String = Version.JsonObject("downloads")("client")("url")
  33. Return New NetFile(DlSourceLauncherOrMetaGet(JarUrl), Version.Path & Version.Name & ".jar", Checker)
  34. End Function
  35. ''' <summary>
  36. ''' 返回某 Minecraft 版本对应的原版主 AssetIndex 文件的下载信息,要求对应依赖版本已存在。
  37. ''' 若未找到,则会返回 Legacy 资源文件或 Nothing。
  38. ''' </summary>
  39. Public Function DlClientAssetIndexGet(Version As McVersion) As NetFile
  40. '获取底层继承版本
  41. Do While Not String.IsNullOrEmpty(Version.InheritVersion)
  42. Version = New McVersion(Version.InheritVersion)
  43. Loop
  44. '获取信息
  45. Dim IndexInfo = McAssetsGetIndex(Version, True, True)
  46. Dim IndexAddress As String = PathMcFolder & "assets\indexes\" & IndexInfo("id").ToString & ".json"
  47. Log("[Download] 版本 " & Version.Name & " 对应的资源文件索引为 " & IndexInfo("id").ToString)
  48. Dim IndexUrl As String = If(IndexInfo("url"), "")
  49. If IndexUrl = "" Then
  50. Return Nothing
  51. Else
  52. Return New NetFile(DlSourceLauncherOrMetaGet(IndexUrl, False), IndexAddress, New FileChecker(CanUseExistsFile:=False, IsJson:=True))
  53. End If
  54. End Function
  55. ''' <summary>
  56. ''' 构造补全某 Minecraft 版本的所有文件的加载器列表。失败会抛出异常。
  57. ''' </summary>
  58. Public Function DlClientFix(Version As McVersion, CheckAssetsHash As Boolean, AssetsIndexBehaviour As AssetsIndexExistsBehaviour, SkipAssetsDownloadWhileSetupRequired As Boolean) As List(Of LoaderBase)
  59. Dim Loaders As New List(Of LoaderBase)
  60. #Region "下载支持库文件"
  61. Dim LoadersLib As New List(Of LoaderBase) From {
  62. New LoaderTask(Of String, List(Of NetFile))("分析缺失支持库文件", Sub(Task As LoaderTask(Of String, List(Of NetFile))) Task.Output = McLibFix(Version)) With {.ProgressWeight = 1},
  63. New LoaderDownload("下载支持库文件", New List(Of NetFile)) With {.ProgressWeight = 15}
  64. }
  65. '构造加载器
  66. Loaders.Add(New LoaderCombo(Of String)("下载支持库文件(主加载器)", LoadersLib) With {.Block = False, .Show = False, .ProgressWeight = 16})
  67. #End Region
  68. #Region "下载资源文件"
  69. Dim IsSetupSkip As Boolean = Setup.Get("LaunchAdvanceAssets")
  70. Select Case Setup.Get("VersionAdvanceAssets", Version:=Version)
  71. Case 0
  72. '使用全局设置
  73. Case 1
  74. '开启
  75. IsSetupSkip = False
  76. Case 2
  77. '关闭
  78. IsSetupSkip = True
  79. End Select
  80. If IsSetupSkip Then Log("[Download] 已跳过 Assets 下载")
  81. If (Not SkipAssetsDownloadWhileSetupRequired) OrElse Not IsSetupSkip Then
  82. Dim LoadersAssets As New List(Of LoaderBase)
  83. '获取资源文件索引地址
  84. LoadersAssets.Add(New LoaderTask(Of String, List(Of NetFile))("分析资源文件索引地址",
  85. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  86. Try
  87. Dim IndexFile = DlClientAssetIndexGet(Version)
  88. Dim IndexFileInfo As New FileInfo(IndexFile.LocalPath)
  89. If AssetsIndexBehaviour <> AssetsIndexExistsBehaviour.AlwaysDownload AndAlso IndexFile.Check.Check(IndexFile.LocalPath) Is Nothing Then
  90. Task.Output = New List(Of NetFile)
  91. Else
  92. Task.Output = New List(Of NetFile) From {IndexFile}
  93. End If
  94. Catch ex As Exception
  95. Throw New Exception("分析资源文件索引地址失败", ex)
  96. End Try
  97. End Sub) With {.ProgressWeight = 0.5, .Show = False})
  98. '下载资源文件索引
  99. LoadersAssets.Add(New LoaderDownload("下载资源文件索引", New List(Of NetFile)) With {.ProgressWeight = 2})
  100. '要求独立更新索引
  101. If AssetsIndexBehaviour = AssetsIndexExistsBehaviour.DownloadInBackground Then
  102. Dim LoadersAssetsUpdate As New List(Of LoaderBase)
  103. Dim TempAddress As String = Nothing
  104. Dim RealAddress As String = Nothing
  105. LoadersAssetsUpdate.Add(New LoaderTask(Of String, List(Of NetFile))("后台分析资源文件索引地址", Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  106. Dim BackAssetsFile As NetFile = DlClientAssetIndexGet(Version)
  107. RealAddress = BackAssetsFile.LocalPath
  108. TempAddress = PathTemp & "Cache\" & BackAssetsFile.LocalName
  109. BackAssetsFile.LocalPath = TempAddress
  110. Task.Output = New List(Of NetFile) From {BackAssetsFile}
  111. End Sub))
  112. LoadersAssetsUpdate.Add(New LoaderDownload("后台下载资源文件索引", New List(Of NetFile)))
  113. LoadersAssetsUpdate.Add(New LoaderTask(Of List(Of NetFile), String)("后台复制资源文件索引", Sub(Task As LoaderTask(Of List(Of NetFile), String))
  114. CopyFile(TempAddress, RealAddress)
  115. McLaunchLog("后台更新资源文件索引成功:" & TempAddress)
  116. End Sub))
  117. Dim Updater As New LoaderCombo(Of String)("后台更新资源文件索引", LoadersAssetsUpdate)
  118. Log("[Download] 开始后台更新资源文件索引")
  119. Updater.Start()
  120. End If
  121. '获取资源文件地址
  122. LoadersAssets.Add(New LoaderTask(Of String, List(Of NetFile))("分析缺失资源文件",
  123. Sub(Task As LoaderTask(Of String, List(Of NetFile)))
  124. Task.Output = McAssetsFixList(McAssetsGetIndexName(Version), CheckAssetsHash, Task)
  125. End Sub) With {.ProgressWeight = 3})
  126. '下载资源文件
  127. LoadersAssets.Add(New LoaderDownload("下载资源文件", New List(Of NetFile)) With {.ProgressWeight = 25})
  128. '构造加载器
  129. Loaders.Add(New LoaderCombo(Of String)("下载资源文件(主加载器)", LoadersAssets) With {.Block = False, .Show = False, .ProgressWeight = 30.5})
  130. End If
  131. #End Region
  132. Return Loaders
  133. End Function
  134. Public Enum AssetsIndexExistsBehaviour
  135. ''' <summary>
  136. ''' 如果文件存在,则不进行下载。
  137. ''' </summary>
  138. DontDownload
  139. ''' <summary>
  140. ''' 如果文件存在,则启动新的下载加载器进行独立的更新。
  141. ''' </summary>
  142. DownloadInBackground
  143. ''' <summary>
  144. ''' 如果文件存在,也同样进行下载。
  145. ''' </summary>
  146. AlwaysDownload
  147. End Enum
  148. #End Region
  149. #Region "DlClientList | Minecraft 客户端 版本列表"
  150. '主加载器
  151. Public Structure DlClientListResult
  152. ''' <summary>
  153. ''' 数据来源名称,如“Mojang”,“BMCLAPI”。
  154. ''' </summary>
  155. Public SourceName As String
  156. ''' <summary>
  157. ''' 是否为官方的实时数据。
  158. ''' </summary>
  159. Public IsOfficial As Boolean
  160. ''' <summary>
  161. ''' 获取到的 Json 数据。
  162. ''' </summary>
  163. Public Value As JObject
  164. '''' <summary>
  165. '''' 官方源的失败原因。若没有则为 Nothing。
  166. '''' </summary>
  167. 'Public OfficialError As Exception
  168. End Structure
  169. ''' <summary>
  170. ''' Minecraft 客户端 版本列表,主加载器。
  171. ''' </summary>
  172. Public DlClientListLoader As New LoaderTask(Of Integer, DlClientListResult)("DlClientList Main", AddressOf DlClientListMain)
  173. Private Sub DlClientListMain(Loader As LoaderTask(Of Integer, DlClientListResult))
  174. Select Case Setup.Get("ToolDownloadVersion")
  175. Case 0
  176. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)) From {
  177. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListBmclapiLoader, 30),
  178. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListMojangLoader, 60)
  179. }, Loader.IsForceRestarting)
  180. Case 1
  181. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)) From {
  182. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListMojangLoader, 5),
  183. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListBmclapiLoader, 35)
  184. }, Loader.IsForceRestarting)
  185. Case Else
  186. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)) From {
  187. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListMojangLoader, 60),
  188. New KeyValuePair(Of LoaderTask(Of Integer, DlClientListResult), Integer)(DlClientListBmclapiLoader, 60)
  189. }, Loader.IsForceRestarting)
  190. End Select
  191. End Sub
  192. '各个下载源的分加载器
  193. ''' <summary>
  194. ''' Minecraft 客户端 版本列表,Mojang 官方源加载器。
  195. ''' </summary>
  196. Public DlClientListMojangLoader As New LoaderTask(Of Integer, DlClientListResult)("DlClientList Mojang", AddressOf DlClientListMojangMain)
  197. Private IsNewClientVersionHinted As Boolean = False
  198. Private Sub DlClientListMojangMain(Loader As LoaderTask(Of Integer, DlClientListResult))
  199. Dim Json As JObject = NetGetCodeByRequestRetry("https://launchermeta.mojang.com/mc/game/version_manifest.json", IsJson:=True)
  200. Try
  201. Dim Versions As JArray = Json("versions")
  202. If Versions.Count < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Json.ToString & ")")
  203. '添加 PCL2 特供项
  204. If File.Exists(PathTemp & "Cache\download.json") Then Versions.Merge(GetJson(ReadFile(PathTemp & "Cache\download.json")))
  205. '返回
  206. Loader.Output = New DlClientListResult With {.IsOfficial = True, .SourceName = "Mojang 官方源", .Value = Json}
  207. '解析更新提示(Release)
  208. Dim Version As String = Json("latest")("release")
  209. If Setup.Get("ToolUpdateRelease") AndAlso Not Setup.Get("ToolUpdateReleaseLast") = "" AndAlso Version IsNot Nothing AndAlso Not Setup.Get("ToolUpdateReleaseLast") = Version Then
  210. McDownloadClientUpdateHint(Version, Json)
  211. IsNewClientVersionHinted = True
  212. End If
  213. Setup.Set("ToolUpdateReleaseLast", Version)
  214. '解析更新提示(Snapshot)
  215. Version = Json("latest")("snapshot")
  216. If Setup.Get("ToolUpdateSnapshot") AndAlso Not Setup.Get("ToolUpdateSnapshotLast") = "" AndAlso Version IsNot Nothing AndAlso Not Setup.Get("ToolUpdateSnapshotLast") = Version AndAlso Not IsNewClientVersionHinted Then
  217. McDownloadClientUpdateHint(Version, Json)
  218. End If
  219. Setup.Set("ToolUpdateSnapshotLast", If(Version, "Nothing"))
  220. Catch ex As Exception
  221. Throw New Exception("版本列表解析失败", ex)
  222. End Try
  223. End Sub
  224. ''' <summary>
  225. ''' Minecraft 客户端 版本列表,BMCLAPI 源加载器。
  226. ''' </summary>
  227. Public DlClientListBmclapiLoader As New LoaderTask(Of Integer, DlClientListResult)("DlClientList Bmclapi", AddressOf DlClientListBmclapiMain)
  228. Private Sub DlClientListBmclapiMain(Loader As LoaderTask(Of Integer, DlClientListResult))
  229. Dim Json As JObject = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/mc/game/version_manifest.json",
  230. BackupUrl:="https://download.mcbbs.net/mc/game/version_manifest.json",
  231. IsJson:=True)
  232. Try
  233. Dim Versions As JArray = Json("versions")
  234. If Versions.Count < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Json.ToString & ")")
  235. '添加 PCL2 特供项
  236. If File.Exists(PathTemp & "Cache\download.json") Then Versions.Merge(GetJson(ReadFile(PathTemp & "Cache\download.json")))
  237. '返回
  238. Loader.Output = New DlClientListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Json}
  239. Catch ex As Exception
  240. Throw New Exception("版本列表解析失败(" & Json.ToString & ")", ex)
  241. End Try
  242. End Sub
  243. ''' <summary>
  244. ''' 获取某个版本的 Json 下载地址,若失败则返回 Nothing。必须在工作线程执行。
  245. ''' </summary>
  246. Public Function DlClientListGet(Id As String)
  247. Try
  248. '确认 Minecraft 版本列表已完成获取
  249. Select Case DlClientListLoader.State
  250. Case LoadState.Loading
  251. DlClientListLoader.WaitForExit()
  252. Case LoadState.Failed, LoadState.Aborted, LoadState.Waiting
  253. DlClientListLoader.WaitForExit(IsForceRestart:=True)
  254. End Select
  255. '确认版本格式标准
  256. Id = Id.Replace("_", "-") '1.7.10_pre4 在版本列表中显示为 1.7.10-pre4
  257. If Id <> "1.0" AndAlso Id.EndsWith(".0") Then Id = Left(Id, Id.Length - 2) 'OptiFine 1.8 的下载会触发此问题,显示版本为 1.8.0
  258. '查找版本并开始
  259. For Each Version As JObject In DlClientListLoader.Output.Value("versions")
  260. If Version("id") = Id Then
  261. Return Version("url").ToString
  262. End If
  263. Next
  264. Log("未发现版本 " & Id & " 的 Json 下载地址,版本列表返回为:" & vbCrLf & DlClientListLoader.Output.Value.ToString, LogLevel.Debug)
  265. Return Nothing
  266. Catch ex As Exception
  267. Log(ex, "获取版本 " & Id & " 的 Json 下载地址失败")
  268. Return Nothing
  269. End Try
  270. End Function
  271. #End Region
  272. #Region "DlOptiFineList | OptiFine 版本列表"
  273. Public Structure DlOptiFineListResult
  274. ''' <summary>
  275. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  276. ''' </summary>
  277. Public SourceName As String
  278. ''' <summary>
  279. ''' 是否为官方的实时数据。
  280. ''' </summary>
  281. Public IsOfficial As Boolean
  282. ''' <summary>
  283. ''' 获取到的数据。
  284. ''' </summary>
  285. Public Value As List(Of DlOptiFineListEntry)
  286. End Structure
  287. Public Class DlOptiFineListEntry
  288. ''' <summary>
  289. ''' 显示名称,已去除 HD_U 字样,如“1.12.2 C8”。
  290. ''' </summary>
  291. Public NameDisplay As String
  292. ''' <summary>
  293. ''' 原始文件名称,如“preview_OptiFine_1.11_HD_U_E1_pre.jar”。
  294. ''' </summary>
  295. Public NameFile As String
  296. ''' <summary>
  297. ''' 对应的版本名称,如“1.13.2-OptiFine_HD_U_E6”。
  298. ''' </summary>
  299. Public NameVersion As String
  300. ''' <summary>
  301. ''' 是否为测试版。
  302. ''' </summary>
  303. Public IsPreview As Boolean
  304. ''' <summary>
  305. ''' 对应的 Minecraft 版本,如“1.12.2”。
  306. ''' </summary>
  307. Public Property Inherit As String
  308. Get
  309. Return _inherit
  310. End Get
  311. Set(value As String)
  312. If value.EndsWith(".0") Then value = Left(value, value.Length - 2)
  313. _inherit = value
  314. End Set
  315. End Property
  316. Private _inherit As String
  317. ''' <summary>
  318. ''' 发布时间,格式为“yyyy/mm/dd”。OptiFine 源无此数据。
  319. ''' </summary>
  320. Public ReleaseTime As String
  321. ''' <summary>
  322. ''' 需要的最低 Forge 版本。空字符串为无限制,“28.1.56” 表示版本号,“1161” 表示版本号的最后一位。
  323. ''' </summary>
  324. Public RequiredForgeVersion As String
  325. End Class
  326. ''' <summary>
  327. ''' OptiFine 版本列表,主加载器。
  328. ''' </summary>
  329. Public DlOptiFineListLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Main", AddressOf DlOptiFineListMain)
  330. Private Sub DlOptiFineListMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  331. Select Case Setup.Get("ToolDownloadVersion")
  332. Case 0
  333. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  334. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 30),
  335. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 60)
  336. }, Loader.IsForceRestarting)
  337. Case 1
  338. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  339. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 5),
  340. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 35)
  341. }, Loader.IsForceRestarting)
  342. Case Else
  343. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)) From {
  344. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListOfficialLoader, 60),
  345. New KeyValuePair(Of LoaderTask(Of Integer, DlOptiFineListResult), Integer)(DlOptiFineListBmclapiLoader, 60)
  346. }, Loader.IsForceRestarting)
  347. End Select
  348. End Sub
  349. ''' <summary>
  350. ''' OptiFine 版本列表,官方源。
  351. ''' </summary>
  352. Public DlOptiFineListOfficialLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Official", AddressOf DlOptiFineListOfficialMain)
  353. Private Sub DlOptiFineListOfficialMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  354. Dim Result As String = NetGetCodeByClient("https://optifine.net/downloads", Encoding.Default)
  355. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  356. '获取所有版本信息
  357. Dim Forge As List(Of String) = RegexSearch(Result, "(?<=colForge'>)[^<]*")
  358. Dim ReleaseTime As List(Of String) = RegexSearch(Result, "(?<=colDate'>)[^<]+")
  359. Dim Name As List(Of String) = RegexSearch(Result, "(?<=OptiFine_)[0-9A-Za-z_.]+(?=.jar"")")
  360. If Not ReleaseTime.Count = Name.Count Then Throw New Exception("版本与发布时间数据无法对应")
  361. If Not Forge.Count = Name.Count Then Throw New Exception("版本与 Forge 兼容数据无法对应")
  362. If ReleaseTime.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  363. '转化为列表输出
  364. Dim Versions As New List(Of DlOptiFineListEntry)
  365. For i = 0 To ReleaseTime.Count - 1
  366. Name(i) = Name(i).Replace("_", " ")
  367. Dim Entry As New DlOptiFineListEntry With {
  368. .NameDisplay = Name(i).Replace("HD U ", "").Replace(".0 ", " "),
  369. .ReleaseTime = Join({ReleaseTime(i).Split(".")(2), ReleaseTime(i).Split(".")(1), ReleaseTime(i).Split(".")(0)}, "/"),
  370. .IsPreview = Name(i).ToLower.Contains("pre"),
  371. .Inherit = Name(i).ToString.Split(" ")(0),
  372. .NameFile = If(Name(i).ToLower.Contains("pre"), "preview_", "") & "OptiFine_" & Name(i).Replace(" ", "_") & ".jar",
  373. .RequiredForgeVersion = Forge(i).Replace("Forge ", "").Replace("#", "")}
  374. Entry.NameVersion = Entry.Inherit & "-OptiFine_" & Name(i).ToString.Replace(" ", "_").Replace(Entry.Inherit & "_", "")
  375. Versions.Add(Entry)
  376. Next
  377. Loader.Output = New DlOptiFineListResult With {.IsOfficial = True, .SourceName = "OptiFine 官方源", .Value = Versions}
  378. End Sub
  379. ''' <summary>
  380. ''' OptiFine 版本列表,BMCLAPI。
  381. ''' </summary>
  382. Public DlOptiFineListBmclapiLoader As New LoaderTask(Of Integer, DlOptiFineListResult)("DlOptiFineList Bmclapi", AddressOf DlOptiFineListBmclapiMain)
  383. Private Sub DlOptiFineListBmclapiMain(Loader As LoaderTask(Of Integer, DlOptiFineListResult))
  384. Dim Json As JArray = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/optifine/versionList",
  385. BackupUrl:="https://download.mcbbs.net/optifine/versionList", IsJson:=True)
  386. Try
  387. Dim Versions As New List(Of DlOptiFineListEntry)
  388. For Each Token As JObject In Json
  389. Dim Entry As New DlOptiFineListEntry With {
  390. .NameDisplay = (Token("mcversion").ToString & Token("type").ToString.Replace("HD_U", "").Replace("_", " ") & " " & Token("patch").ToString).Replace(".0 ", " "),
  391. .ReleaseTime = "",
  392. .IsPreview = Token("patch").ToString.ToLower.Contains("pre"),
  393. .Inherit = Token("mcversion").ToString,
  394. .NameFile = Token("filename").ToString,
  395. .RequiredForgeVersion = If(Token("forge"), "").ToString.Replace("Forge ", "").Replace("#", "")
  396. }
  397. Entry.NameVersion = Entry.Inherit & "-OptiFine_" & (Token("type").ToString & " " & Token("patch").ToString).Replace(".0 ", " ").Replace(" ", "_").Replace(Entry.Inherit & "_", "")
  398. Versions.Add(Entry)
  399. Next
  400. Loader.Output = New DlOptiFineListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Versions}
  401. Catch ex As Exception
  402. Throw New Exception("版本列表解析失败(" & Json.ToString & ")", ex)
  403. End Try
  404. End Sub
  405. #End Region
  406. #Region "DlForgeList | Forge Minecraft 版本列表"
  407. Public Structure DlForgeListResult
  408. ''' <summary>
  409. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  410. ''' </summary>
  411. Public SourceName As String
  412. ''' <summary>
  413. ''' 是否为官方的实时数据。
  414. ''' </summary>
  415. Public IsOfficial As Boolean
  416. ''' <summary>
  417. ''' 获取到的数据。
  418. ''' </summary>
  419. Public Value As List(Of String)
  420. End Structure
  421. ''' <summary>
  422. ''' Forge 版本列表,主加载器。
  423. ''' </summary>
  424. Public DlForgeListLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Main", AddressOf DlForgeListMain)
  425. Private Sub DlForgeListMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  426. Select Case Setup.Get("ToolDownloadVersion")
  427. Case 0
  428. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  429. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 30),
  430. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 60)
  431. }, Loader.IsForceRestarting)
  432. Case 1
  433. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  434. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 5),
  435. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 35)
  436. }, Loader.IsForceRestarting)
  437. Case Else
  438. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)) From {
  439. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListOfficialLoader, 60),
  440. New KeyValuePair(Of LoaderTask(Of Integer, DlForgeListResult), Integer)(DlForgeListBmclapiLoader, 60)
  441. }, Loader.IsForceRestarting)
  442. End Select
  443. End Sub
  444. ''' <summary>
  445. ''' Forge 版本列表,官方源。
  446. ''' </summary>
  447. Public DlForgeListOfficialLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Official", AddressOf DlForgeListOfficialMain)
  448. Private Sub DlForgeListOfficialMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  449. Dim Result As String = NetGetCodeByRequestRetry("http://files.minecraftforge.net/maven/net/minecraftforge/forge/index_1.2.4.html", Encoding.Default, "text/html")
  450. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  451. '获取所有版本信息
  452. Dim Names As List(Of String) = RegexSearch(Result, "(?<=a href=""index_)[0-9.]+(_pre[0-9]?)?(?=.html)")
  453. Names.Add("1.2.4") '1.2.4 不会被匹配上
  454. If Names.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  455. Loader.Output = New DlForgeListResult With {.IsOfficial = True, .SourceName = "Forge 官方源", .Value = Names}
  456. End Sub
  457. ''' <summary>
  458. ''' Forge 版本列表,BMCLAPI。
  459. ''' </summary>
  460. Public DlForgeListBmclapiLoader As New LoaderTask(Of Integer, DlForgeListResult)("DlForgeList Bmclapi", AddressOf DlForgeListBmclapiMain)
  461. Private Sub DlForgeListBmclapiMain(Loader As LoaderTask(Of Integer, DlForgeListResult))
  462. Dim Result As String = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/forge/minecraft", Encoding.Default,
  463. BackupUrl:="https://download.mcbbs.net/forge/minecraft")
  464. If Result.Length < 200 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  465. '获取所有版本信息
  466. Dim Names As List(Of String) = RegexSearch(Result, "[0-9.]+(_pre[0-9]?)?")
  467. If Names.Count < 10 Then Throw New Exception("获取到的版本数量不足(" & Result & ")")
  468. Loader.Output = New DlForgeListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Names}
  469. End Sub
  470. #End Region
  471. #Region "DlForgeVersion | Forge 版本列表"
  472. Public Class DlForgeVersionEntry
  473. ''' <summary>
  474. ''' 完整的版本名,如 “14.22.1.2478”。
  475. ''' </summary>
  476. Public Version As String
  477. ''' <summary>
  478. ''' 对应的 Minecraft 版本,如“1.12.2”。
  479. ''' </summary>
  480. Public Inherit As String
  481. ''' <summary>
  482. ''' 发布时间,格式为“yyyy/MM/dd HH:mm”。
  483. ''' </summary>
  484. Public ReleaseTime As String
  485. ''' <summary>
  486. ''' 文件的 MD5 或 SHA1(BMCLAPI 的老版本是 MD5,新版本是 SHA1;官方源总是 MD5)。
  487. ''' </summary>
  488. Public Hash As String = Nothing
  489. ''' <summary>
  490. ''' 是否为推荐版本。
  491. ''' </summary>
  492. Public IsRecommended As Boolean
  493. ''' <summary>
  494. ''' 安装类型。有 installer、client、universal 三种。
  495. ''' </summary>
  496. Public Category As String
  497. ''' <summary>
  498. ''' 版本分支。若无分支则为 Nothing。
  499. ''' </summary>
  500. Public Branch As String = Nothing
  501. ''' <summary>
  502. ''' 是否为新版 Forge。(即为 Minecraft 1.13+)
  503. ''' </summary>
  504. Public ReadOnly Property IsNewType As Boolean
  505. Get
  506. Return Version.Split(".")(0) >= 20
  507. End Get
  508. End Property
  509. ''' <summary>
  510. ''' 构建数。
  511. ''' </summary>
  512. Public ReadOnly Property Build As Integer
  513. Get
  514. Dim Version = Me.Version.Split(".")
  515. If Version(0) < 15 Then
  516. Return Version(Version.Count - 1)
  517. Else
  518. Return Version(Version.Count - 1) + 10000
  519. End If
  520. End Get
  521. End Property
  522. ''' <summary>
  523. ''' 用于下载的文件版本名。可能在 Version 的基础上添加了分支。
  524. ''' </summary>
  525. Public ReadOnly Property FileVersion As String
  526. Get
  527. Return Version & If(Branch Is Nothing, "", "-" & Branch)
  528. End Get
  529. End Property
  530. ''' <summary>
  531. ''' 即将下载的文件全名。
  532. ''' </summary>
  533. Public ReadOnly Property FileName As String
  534. Get
  535. Return "forge-" & Inherit & "-" & FileVersion & "-" & Category & "." & FileSuffix
  536. End Get
  537. End Property
  538. ''' <summary>
  539. ''' 文件扩展名。
  540. ''' </summary>
  541. Public ReadOnly Property FileSuffix As String
  542. Get
  543. If Category = "installer" Then
  544. Return "jar"
  545. Else
  546. Return "zip"
  547. End If
  548. End Get
  549. End Property
  550. End Class
  551. ''' <summary>
  552. ''' Forge 版本列表,主加载器。
  553. ''' </summary>
  554. Public Sub DlForgeVersionMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  555. Dim DlForgeVersionOfficialLoader As New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Official", AddressOf DlForgeVersionOfficialMain)
  556. Dim DlForgeVersionBmclapiLoader As New LoaderTask(Of String, List(Of DlForgeVersionEntry))("DlForgeVersion Bmclapi", AddressOf DlForgeVersionBmclapiMain)
  557. Select Case Setup.Get("ToolDownloadVersion")
  558. Case 0
  559. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  560. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 30),
  561. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 60)
  562. }, Loader.IsForceRestarting)
  563. Case 1
  564. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  565. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 5),
  566. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 35)
  567. }, Loader.IsForceRestarting)
  568. Case Else
  569. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)) From {
  570. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionOfficialLoader, 60),
  571. New KeyValuePair(Of LoaderTask(Of String, List(Of DlForgeVersionEntry)), Integer)(DlForgeVersionBmclapiLoader, 60)
  572. }, Loader.IsForceRestarting)
  573. End Select
  574. End Sub
  575. ''' <summary>
  576. ''' Forge 版本列表,官方源。
  577. ''' </summary>
  578. Public Sub DlForgeVersionOfficialMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  579. Dim Result As String
  580. Try
  581. Result = NetGetCodeByDownload("http://files.minecraftforge.net/maven/net/minecraftforge/forge/index_" & Loader.Input & ".html")
  582. Catch ex As Exception
  583. If GetString(ex).Contains("(404)") Then
  584. Throw New Exception("没有可用版本")
  585. Else
  586. Throw
  587. End If
  588. End Try
  589. If Result.Length < 1000 Then Throw New Exception("获取到的版本列表长度不足(" & Result & ")")
  590. Dim Versions As New List(Of DlForgeVersionEntry)
  591. Try
  592. '分割版本信息
  593. Dim VersionCodes = Mid(Result, 1, Result.LastIndexOf("</table>")).Replace("<td class=""download-version", "¨").Split("¨")
  594. '获取所有版本信息
  595. For i = 1 To VersionCodes.Count - 1
  596. Dim VersionCode = VersionCodes(i)
  597. Try
  598. '基础信息获取
  599. Dim Name As String = RegexSeek(VersionCode, "(?<=[^(0-9)]+)[0-9\.]+")
  600. Dim IsRecommended As Boolean = VersionCode.Contains("fa promo-recommended")
  601. Dim Inherit As String = Loader.Input
  602. '分支获取
  603. Dim Branch As String = RegexSeek(VersionCode, "(?<=Branch:</strong>[\s]*)[^<]+")
  604. Branch = RegexSeek(If(Branch, ""), "[^\s]+")
  605. If String.IsNullOrWhiteSpace(Branch) Then Branch = Nothing
  606. If Name = "11.15.1.2318" OrElse Name = "11.15.1.1902" OrElse Name = "11.15.1.1890" Then Branch = "1.8.9"
  607. If Branch Is Nothing AndAlso Inherit = "1.7.10" AndAlso Name.Split(".")(3) >= 1300 Then Branch = "1.7.10"
  608. '发布时间获取
  609. Dim ReleaseTimeOriginal = RegexSeek(VersionCode, "(?<=""download-time"" title="")[^""]+")
  610. Dim ReleaseTimeSplit = ReleaseTimeOriginal.Split(" -:".ToCharArray) '原格式:"2021-02-15 03:24:02"
  611. Dim ReleaseDate As New Date(ReleaseTimeSplit(0), ReleaseTimeSplit(1), ReleaseTimeSplit(2), '年月日
  612. ReleaseTimeSplit(3), ReleaseTimeSplit(4), ReleaseTimeSplit(5), '时分秒
  613. 0, DateTimeKind.Utc) '以 UTC 时间作为标准
  614. Dim ReleaseTime As String = ReleaseDate.ToLocalTime.ToString("yyyy/MM/dd HH:mm") '时区与格式转换
  615. '分类与 MD5 获取
  616. Dim MD5 As String, Category As String
  617. If VersionCode.Contains("classifier-installer""") Then
  618. '类型为 installer.jar,支持范围 ~753 (~ 1.6.1 部分), 738~684 (1.5.2 全部)
  619. VersionCode = VersionCode.Substring(VersionCode.IndexOf("installer.jar"))
  620. MD5 = RegexSeek(VersionCode, "(?<=MD5:</strong> )[^<]+")
  621. Category = "installer"
  622. ElseIf VersionCode.Contains("classifier-universal""") Then
  623. '类型为 universal.zip,支持范围 751~449 (1.6.1 部分), 682~183 (1.5.1 ~ 1.3.2 部分)
  624. VersionCode = VersionCode.Substring(VersionCode.IndexOf("universal.zip"))
  625. MD5 = RegexSeek(VersionCode, "(?<=MD5:</strong> )[^<]+")
  626. Category = "universal"
  627. Else
  628. '类型为 client.zip,支持范围 182~ (1.3.2 部分 ~)
  629. VersionCode = VersionCode.Substring(VersionCode.IndexOf("client.zip"))
  630. MD5 = RegexSeek(VersionCode, "(?<=MD5:</strong> )[^<]+")
  631. Category = "client"
  632. End If
  633. '添加进列表
  634. Versions.Add(New DlForgeVersionEntry With {.Category = Category, .Version = Name, .IsRecommended = IsRecommended, .Hash = MD5.Trim(vbCr, vbLf), .Inherit = Inherit, .ReleaseTime = ReleaseTime, .Branch = Branch})
  635. Catch ex As Exception
  636. Throw New Exception("版本信息提取失败(" & VersionCode & ")", ex)
  637. End Try
  638. Next
  639. Catch ex As Exception
  640. Throw New Exception("版本列表解析失败(" & Result & ")", ex)
  641. End Try
  642. If Versions.Count = 0 Then Throw New Exception("没有可用版本")
  643. Loader.Output = Versions
  644. End Sub
  645. ''' <summary>
  646. ''' Forge 版本列表,BMCLAPI。
  647. ''' </summary>
  648. Public Sub DlForgeVersionBmclapiMain(Loader As LoaderTask(Of String, List(Of DlForgeVersionEntry)))
  649. Dim Json As JArray = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/forge/minecraft/" & Loader.Input,
  650. BackupUrl:="https://download.mcbbs.net/forge/minecraft/" & Loader.Input, IsJson:=True)
  651. Dim Versions As New List(Of DlForgeVersionEntry)
  652. Try
  653. Dim Recommended As String = McDownloadForgeRecommendedGet(Loader.Input)
  654. For Each Token As JObject In Json
  655. '分类与 Hash 获取
  656. Dim Hash As String = Nothing, Category As String = "unknown", Proi As Integer = -1
  657. For Each File As JObject In Token("files")
  658. Select Case File("category").ToString
  659. Case "installer"
  660. If File("format").ToString = "jar" Then
  661. '类型为 installer.jar,支持范围 ~753 (~ 1.6.1 部分), 738~684 (1.5.2 全部)
  662. Hash = File("hash")
  663. Category = "installer"
  664. Proi = 2
  665. End If
  666. Case "universal"
  667. If Proi <= 1 AndAlso File("format").ToString = "zip" Then
  668. '类型为 universal.zip,支持范围 751~449 (1.6.1 部分), 682~183 (1.5.1 ~ 1.3.2 部分)
  669. Hash = File("hash")
  670. Category = "universal"
  671. Proi = 1
  672. End If
  673. Case "client"
  674. If Proi <= 0 AndAlso File("format").ToString = "zip" Then
  675. '类型为 client.zip,支持范围 182~ (1.3.2 部分 ~)
  676. Hash = File("hash")
  677. Category = "client"
  678. Proi = 0
  679. End If
  680. End Select
  681. Next
  682. '获取 Entry
  683. Dim Inherit As String = Loader.Input
  684. Dim Branch As String = Token("branch")
  685. Dim Name As String = Token("version")
  686. If Name = "11.15.1.2318" OrElse Name = "11.15.1.1902" OrElse Name = "11.15.1.1890" Then Branch = "1.8.9"
  687. If Branch Is Nothing AndAlso Inherit = "1.7.10" AndAlso Name.Split(".")(3) >= 1300 Then Branch = "1.7.10"
  688. '基础信息获取
  689. Dim Entry = New DlForgeVersionEntry With {.Hash = Hash, .Category = Category, .Version = Name, .Branch = Branch, .Inherit = Inherit, .IsRecommended = Recommended = Name}
  690. Dim TimeSplit = Token("modified").ToString.Split("-"c, "T"c, ":"c, "."c, " "c, "/"c)
  691. Entry.ReleaseTime = Token("modified").ToObject(Of Date).ToLocalTime.ToString("yyyy/MM/dd HH:mm")
  692. '添加项
  693. Versions.Add(Entry)
  694. Next
  695. Catch ex As Exception
  696. Throw New Exception("版本列表解析失败(" & Json.ToString & ")", ex)
  697. End Try
  698. If Versions.Count = 0 Then Throw New Exception("没有可用版本")
  699. Loader.Output = Versions
  700. End Sub
  701. #End Region
  702. #Region "DlLiteLoaderList | LiteLoader 版本列表"
  703. Public Structure DlLiteLoaderListResult
  704. ''' <summary>
  705. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  706. ''' </summary>
  707. Public SourceName As String
  708. ''' <summary>
  709. ''' 是否为官方的实时数据。
  710. ''' </summary>
  711. Public IsOfficial As Boolean
  712. ''' <summary>
  713. ''' 获取到的数据。
  714. ''' </summary>
  715. Public Value As List(Of DlLiteLoaderListEntry)
  716. ''' <summary>
  717. ''' 官方源的失败原因。若没有则为 Nothing。
  718. ''' </summary>
  719. Public OfficialError As Exception
  720. End Structure
  721. Public Class DlLiteLoaderListEntry
  722. ''' <summary>
  723. ''' 实际的文件名,如“liteloader-installer-1.12-00-SNAPSHOT.jar”。
  724. ''' </summary>
  725. Public FileName As String
  726. ''' <summary>
  727. ''' 是否为测试版。
  728. ''' </summary>
  729. Public IsPreview As Boolean
  730. ''' <summary>
  731. ''' 对应的 Minecraft 版本,如“1.12.2”。
  732. ''' </summary>
  733. Public Inherit As String
  734. ''' <summary>
  735. ''' 是否为 1.7 及更早的远古版。
  736. ''' </summary>
  737. Public IsLegacy As Boolean
  738. ''' <summary>
  739. ''' 发布时间,格式为“yyyy/mm/dd HH:mm:ss”。
  740. ''' </summary>
  741. Public ReleaseTime As String
  742. ''' <summary>
  743. ''' 文件的 MD5。
  744. ''' </summary>
  745. Public MD5 As String
  746. ''' <summary>
  747. ''' 对应的 Json 项。
  748. ''' </summary>
  749. Public JsonToken As JToken
  750. End Class
  751. ''' <summary>
  752. ''' LiteLoader 版本列表,主加载器。
  753. ''' </summary>
  754. Public DlLiteLoaderListLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Main", AddressOf DlLiteLoaderListMain)
  755. Private Sub DlLiteLoaderListMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  756. Select Case Setup.Get("ToolDownloadVersion")
  757. Case 0
  758. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  759. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 30),
  760. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 60)
  761. }, Loader.IsForceRestarting)
  762. Case 1
  763. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  764. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 5),
  765. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 35)
  766. }, Loader.IsForceRestarting)
  767. Case Else
  768. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)) From {
  769. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListOfficialLoader, 60),
  770. New KeyValuePair(Of LoaderTask(Of Integer, DlLiteLoaderListResult), Integer)(DlLiteLoaderListBmclapiLoader, 60)
  771. }, Loader.IsForceRestarting)
  772. End Select
  773. End Sub
  774. ''' <summary>
  775. ''' LiteLoader 版本列表,官方源。
  776. ''' </summary>
  777. Public DlLiteLoaderListOfficialLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Official", AddressOf DlLiteLoaderListOfficialMain)
  778. Private Sub DlLiteLoaderListOfficialMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  779. Dim Result As JObject = NetGetCodeByRequestRetry("http://dl.liteloader.com/versions/versions.json", IsJson:=True)
  780. Try
  781. Dim Json As JObject = Result("versions")
  782. Dim Versions As New List(Of DlLiteLoaderListEntry)
  783. For Each Pair As KeyValuePair(Of String, JToken) In Json
  784. If Pair.Key.StartsWith("1.6") OrElse Pair.Key.StartsWith("1.5") Then Continue For
  785. Dim RealEntry As JToken = If(Pair.Value("artefacts"), Pair.Value("snapshots"))("com.mumfrey:liteloader")("latest")
  786. Versions.Add(New DlLiteLoaderListEntry With {
  787. .Inherit = Pair.Key,
  788. .IsLegacy = Pair.Key.Split(".")(1) < 8,
  789. .IsPreview = RealEntry("stream").ToString.ToLower = "snapshot",
  790. .FileName = "liteloader-installer-" & Pair.Key & If(Pair.Key = "1.8" OrElse Pair.Key = "1.9", ".0", "") & "-00-SNAPSHOT.jar",
  791. .MD5 = RealEntry("md5"),
  792. .ReleaseTime = GetLocalTime(GetDate(RealEntry("timestamp"))).ToString("yyyy/MM/dd HH:mm"),
  793. .JsonToken = RealEntry
  794. })
  795. Next
  796. Loader.Output = New DlLiteLoaderListResult With {.IsOfficial = True, .SourceName = "LiteLoader 官方源", .Value = Versions}
  797. Catch ex As Exception
  798. Throw New Exception("版本列表解析失败(" & Result.ToString & ")", ex)
  799. End Try
  800. End Sub
  801. ''' <summary>
  802. ''' LiteLoader 版本列表,BMCLAPI。
  803. ''' </summary>
  804. Public DlLiteLoaderListBmclapiLoader As New LoaderTask(Of Integer, DlLiteLoaderListResult)("DlLiteLoaderList Bmclapi", AddressOf DlLiteLoaderListBmclapiMain)
  805. Private Sub DlLiteLoaderListBmclapiMain(Loader As LoaderTask(Of Integer, DlLiteLoaderListResult))
  806. Dim Result As JObject = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/maven/com/mumfrey/liteloader/versions.json",
  807. BackupUrl:="https://download.mcbbs.net/maven/com/mumfrey/liteloader/versions.json", IsJson:=True)
  808. Try
  809. Dim Json As JObject = Result("versions")
  810. Dim Versions As New List(Of DlLiteLoaderListEntry)
  811. For Each Pair As KeyValuePair(Of String, JToken) In Json
  812. If Pair.Key.StartsWith("1.6") OrElse Pair.Key.StartsWith("1.5") Then Continue For
  813. Dim RealEntry As JToken = If(Pair.Value("artefacts"), Pair.Value("snapshots"))("com.mumfrey:liteloader")("latest")
  814. Versions.Add(New DlLiteLoaderListEntry With {
  815. .Inherit = Pair.Key,
  816. .IsLegacy = Pair.Key.Split(".")(1) < 8,
  817. .IsPreview = RealEntry("stream").ToString.ToLower = "snapshot",
  818. .FileName = "liteloader-installer-" & Pair.Key & If(Pair.Key = "1.8" OrElse Pair.Key = "1.9", ".0", "") & "-00-SNAPSHOT.jar",
  819. .MD5 = RealEntry("md5"),
  820. .ReleaseTime = GetLocalTime(GetDate(RealEntry("timestamp"))).ToString("yyyy/MM/dd HH:mm"),
  821. .JsonToken = RealEntry
  822. })
  823. Next
  824. Loader.Output = New DlLiteLoaderListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Versions}
  825. Catch ex As Exception
  826. Throw New Exception("版本列表解析失败(" & Result.ToString & ")", ex)
  827. End Try
  828. End Sub
  829. #End Region
  830. #Region "DlFabricList | Fabric 列表"
  831. Public Structure DlFabricListResult
  832. ''' <summary>
  833. ''' 数据来源名称,如“Official”,“BMCLAPI”。
  834. ''' </summary>
  835. Public SourceName As String
  836. ''' <summary>
  837. ''' 是否为官方的实时数据。
  838. ''' </summary>
  839. Public IsOfficial As Boolean
  840. ''' <summary>
  841. ''' 获取到的数据。
  842. ''' </summary>
  843. Public Value As JObject
  844. End Structure
  845. ''' <summary>
  846. ''' Fabric 列表,主加载器。
  847. ''' </summary>
  848. Public DlFabricListLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Main", AddressOf DlFabricListMain)
  849. Private Sub DlFabricListMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  850. Select Case Setup.Get("ToolDownloadVersion")
  851. Case 0
  852. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  853. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 30),
  854. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 60)
  855. }, Loader.IsForceRestarting)
  856. Case 1
  857. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  858. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 5),
  859. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 35)
  860. }, Loader.IsForceRestarting)
  861. Case Else
  862. DlSourceLoader(Loader, New List(Of KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)) From {
  863. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListOfficialLoader, 60),
  864. New KeyValuePair(Of LoaderTask(Of Integer, DlFabricListResult), Integer)(DlFabricListBmclapiLoader, 60)
  865. }, Loader.IsForceRestarting)
  866. End Select
  867. End Sub
  868. ''' <summary>
  869. ''' Fabric 列表,官方源。
  870. ''' </summary>
  871. Public DlFabricListOfficialLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Official", AddressOf DlFabricListOfficialMain)
  872. Private Sub DlFabricListOfficialMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  873. Dim Result As JObject = NetGetCodeByRequestRetry("https://meta.fabricmc.net/v2/versions", IsJson:=True)
  874. Try
  875. Dim Output = New DlFabricListResult With {.IsOfficial = True, .SourceName = "Fabric 官方源", .Value = Result}
  876. If Output.Value("game") Is Nothing OrElse Output.Value("loader") Is Nothing OrElse Output.Value("installer") Is Nothing Then Throw New Exception("获取到的列表缺乏必要项")
  877. Loader.Output = Output
  878. Catch ex As Exception
  879. Throw New Exception("列表解析失败(" & Result.ToString & ")", ex)
  880. End Try
  881. End Sub
  882. ''' <summary>
  883. ''' Fabric 列表,BMCLAPI。
  884. ''' </summary>
  885. Public DlFabricListBmclapiLoader As New LoaderTask(Of Integer, DlFabricListResult)("DlFabricList Bmclapi", AddressOf DlFabricListBmclapiMain)
  886. Private Sub DlFabricListBmclapiMain(Loader As LoaderTask(Of Integer, DlFabricListResult))
  887. Dim Result As JObject = NetGetCodeByRequestRetry("https://bmclapi2.bangbang93.com/fabric-meta/v2/versions",
  888. BackupUrl:="https://download.mcbbs.net/fabric-meta/v2/versions", IsJson:=True)
  889. Try
  890. Dim Output = New DlFabricListResult With {.IsOfficial = False, .SourceName = "BMCLAPI", .Value = Result}
  891. If Output.Value("game") Is Nothing OrElse Output.Value("loader") Is Nothing OrElse Output.Value("installer") Is Nothing Then Throw New Exception("获取到的列表缺乏必要项")
  892. Loader.Output = Output
  893. Catch ex As Exception
  894. Throw New Exception("列表解析失败(" & Result.ToString & ")", ex)
  895. End Try
  896. End Sub
  897. ''' <summary>
  898. ''' Fabric API 列表,官方源。
  899. ''' </summary>
  900. Public DlFabricApiLoader As New LoaderTask(Of Integer, List(Of DlCfFile))("Fabric API List Loader",
  901. Sub(Task As LoaderTask(Of Integer, List(Of DlCfFile))) Task.Output = DlCfGetFiles(306612, False))
  902. ''' <summary>
  903. ''' OptiFabric 列表,官方源。
  904. ''' </summary>
  905. Public DlOptiFabricLoader As New LoaderTask(Of Integer, List(Of DlCfFile))("OptiFabric List Loader",
  906. Sub(Task As LoaderTask(Of Integer, List(Of DlCfFile))) Task.Output = DlCfGetFiles(322385, False))
  907. #End Region
  908. #Region "DlCfProject | CurseForge 工程"
  909. Private DlCfProjectDb As Dictionary(Of String, DlCfDbEntry) = Nothing
  910. Private DlCfProjectCache As New Dictionary(Of Integer, DlCfProject)
  911. ''' <summary>
  912. ''' CurseForge 工程列表获取、搜索请求。
  913. ''' </summary>
  914. Public Class DlCfProjectRequest
  915. Public CategoryId As Integer = 0 '421: Mod(?), 4780: Fabric
  916. Public IsModPack As Boolean = False
  917. Public GameVersion As String = Nothing
  918. Public PageSize As String = 50
  919. Public SearchFilter As String = Nothing
  920. Public Index As Integer? = Nothing
  921. Public ModLoader As Integer? = Nothing
  922. Public Function GetAddress() As String
  923. GetAddress = "https://api.curseforge.com/v1/mods/search?gameId=432&sortField=Featured&sortOrder=desc" &
  924. "&pageSize=" & PageSize &
  925. "&categoryId=" & CategoryId &
  926. If(IsModPack, "&classId=4471", "&classId=6") &
  927. If(String.IsNullOrEmpty(GameVersion), "", "&gameVersion=" & GameVersion) &
  928. If(String.IsNullOrEmpty(SearchFilter), "", "&searchFilter=" & Net.WebUtility.UrlEncode(SearchFilter)) &
  929. If(Index IsNot Nothing, "&index=" & Index, "") &
  930. If(ModLoader IsNot Nothing AndAlso ModLoader > 0, "&modLoaderType=" & ModLoader, "")
  931. End Function
  932. #Region "相同判断"
  933. Public Overrides Function Equals(obj As Object) As Boolean
  934. Dim request = TryCast(obj, DlCfProjectRequest)
  935. Return request IsNot Nothing AndAlso request.GetAddress = GetAddress()
  936. End Function
  937. Public Shared Operator =(left As DlCfProjectRequest, right As DlCfProjectRequest) As Boolean
  938. Return EqualityComparer(Of DlCfProjectRequest).Default.Equals(left, right)
  939. End Operator
  940. Public Shared Operator <>(left As DlCfProjectRequest, right As DlCfProjectRequest) As Boolean
  941. Return Not left = right
  942. End Operator
  943. #End Region
  944. End Class
  945. ''' <summary>
  946. ''' CurseForge 工程信息。
  947. ''' </summary>
  948. Public Class DlCfProject
  949. Public Id As Integer
  950. Public Name As String
  951. Public Description As String
  952. Public Website As String
  953. Public DateUpdate As Date
  954. Public DownloadCount As Integer
  955. Public ModLoaders As New List(Of String)
  956. Public GameVersionDesc As String
  957. Private CategoryDesc As List(Of String)
  958. Private Thumb As String
  959. Public IsModPack As Boolean
  960. Public FileIndexes As New List(Of Integer)
  961. Public Files As List(Of DlCfFile)
  962. Private _MCBBS As Integer?
  963. Public Property MCBBS As Integer?
  964. Get
  965. ReleaseCfDatabase()
  966. If _MCBBS Is Nothing Then
  967. If Website IsNot Nothing Then
  968. Dim Keyword As String = Website.TrimEnd("/").Split("/").Last
  969. If DlCfProjectDb.ContainsKey(Keyword) Then _MCBBS = DlCfProjectDb(Keyword).MCBBS
  970. End If
  971. End If
  972. Return _MCBBS
  973. End Get
  974. Set(value As Integer?)
  975. _MCBBS = value
  976. End Set
  977. End Property
  978. Private _ChineseName As String
  979. Public Property ChineseName As String
  980. Get
  981. ReleaseCfDatabase()
  982. If _ChineseName Is Nothing Then
  983. _ChineseName = Name
  984. If Website IsNot Nothing Then
  985. Dim Keyword As String = Website.TrimEnd("/").Split("/").Last
  986. If DlCfProjectDb.ContainsKey(Keyword) Then
  987. Dim Result = DlCfProjectDb(Keyword)
  988. If Not String.IsNullOrEmpty(Result.ChineseName) Then '使用原名
  989. _ChineseName = Result.ChineseName
  990. End If
  991. End If
  992. End If
  993. End If
  994. Return _ChineseName
  995. End Get
  996. Set(value As String)
  997. _ChineseName = value
  998. End Set
  999. End Property
  1000. Private _WikiId As Integer = -1
  1001. ''' <summary>
  1002. ''' MCMOD 的页面 ID。若为 0 则代表没有对应页面。
  1003. ''' </summary>
  1004. Public Property WikiId As Integer
  1005. Get
  1006. ReleaseCfDatabase()
  1007. If _WikiId = -1 Then
  1008. If Website IsNot Nothing Then
  1009. Dim Keyword As String = Website.TrimEnd("/").Split("/").Last
  1010. If DlCfProjectDb.ContainsKey(Keyword) Then
  1011. _WikiId = DlCfProjectDb(Keyword).WikiId
  1012. Else
  1013. _WikiId = 0
  1014. End If
  1015. End If
  1016. End If
  1017. Return _WikiId
  1018. End Get
  1019. Set(value As Integer)
  1020. _WikiId = value
  1021. End Set
  1022. End Property
  1023. ''' <summary>
  1024. ''' 从 CurseForge 工程 Json 项中初始化实例。若出错会抛出异常。
  1025. ''' </summary>
  1026. Public Sub New(Data As JObject)
  1027. 'https://api.curseforge.com/v1/mods/search?gameId=432&pageSize=5&categoryID=421
  1028. Id = Data("id")
  1029. Name = Data("name")
  1030. Description = Data("summary")
  1031. Website = Data("links")("websiteUrl")
  1032. DateUpdate = Data("dateModified")
  1033. DownloadCount = Data("downloadCount")
  1034. IsModPack = Not Website.Contains("/mc-mods/")
  1035. '获取常见文件
  1036. For Each File In If(Data("latestFiles"), {})
  1037. Dim NewFile As New DlCfFile(File, IsModPack)
  1038. If Not NewFile.IsAvailable Then Continue For
  1039. Next
  1040. '获取游戏版本、Mod Loader
  1041. Dim GameVersions As New List(Of Integer)
  1042. ModLoaders = New List(Of String)
  1043. For Each File In If(Data("latestFilesIndexes"), {})
  1044. Dim Version As String = File("gameVersion")
  1045. If Not Version.Contains("1.") Then Continue For
  1046. GameVersions.Add(Version.Split(".")(1).Split("-").First)
  1047. Select Case If(File("modLoader"), "0").ToString
  1048. Case 1
  1049. ModLoaders.Add("Forge")
  1050. Case 2
  1051. ModLoaders.Add("Cauldron")
  1052. Case 3
  1053. ModLoaders.Add("LiteLoader")
  1054. Case 4
  1055. ModLoaders.Add("Fabric")
  1056. End Select
  1057. FileIndexes.Add(File("fileId"))
  1058. Next
  1059. GameVersions = Sort(GameVersions.Distinct.ToList, AddressOf VersionSortBoolean)
  1060. ModLoaders = ModLoaders.Distinct.ToList
  1061. ModLoaders.Sort()
  1062. If GameVersions.Count = 0 Then
  1063. GameVersionDesc = ""
  1064. Else
  1065. Dim SpaVersions As New List(Of String)
  1066. For i = 0 To GameVersions.Count - 1
  1067. Dim StartVersion As Integer = GameVersions(i), EndVersion As Integer = GameVersions(i)
  1068. For ii = i + 1 To GameVersions.Count - 1
  1069. If GameVersions(ii) = EndVersion - 1 Then
  1070. EndVersion = GameVersions(ii)
  1071. i = ii
  1072. Else
  1073. Exit For
  1074. End If
  1075. Next
  1076. If StartVersion = EndVersion Then
  1077. SpaVersions.Add("1." & StartVersion)
  1078. Else
  1079. SpaVersions.Add("1." & StartVersion & "-1." & EndVersion)
  1080. End If
  1081. Next
  1082. GameVersionDesc = "[" & Join(SpaVersions, ", ") & "] "
  1083. End If
  1084. '获取分类
  1085. Dim Categories = Data("categories")
  1086. Dim CategoryId As New List(Of Integer)
  1087. For Each CategoryToken In Categories
  1088. CategoryId.Add(CategoryToken("id"))
  1089. Next
  1090. CategoryId = ArrayNoDouble(CategoryId)
  1091. CategoryId.Sort()
  1092. CategoryDesc = New List(Of String)
  1093. For Each Category In CategoryId
  1094. Dim Desc As String
  1095. Select Case Category
  1096. Case 406 : Desc = "世界生成"
  1097. Case 407 : Desc = "生物群系"
  1098. Case 409 : Desc = "天然结构"
  1099. Case 410 : Desc = "维度"
  1100. Case 411 : Desc = "生物"
  1101. Case 412 : Desc = "科技"
  1102. Case 414 : Desc = "交通与移动"
  1103. Case 415 : Desc = "管道与物流"
  1104. Case 417 : Desc = "能源"
  1105. Case 4558 : Desc = "红石"
  1106. Case 420 : Desc = "仓储"
  1107. Case 416 : Desc = "农业"
  1108. Case 436 : Desc = "食物"
  1109. Case 419 : Desc = "魔法"
  1110. Case 434 : Desc = "装备与工具"
  1111. Case 422 : Desc = "冒险与探索"
  1112. Case 424 : Desc = "建筑与装饰"
  1113. Case 423 : Desc = "信息显示"
  1114. Case 425 : Desc = "杂项"
  1115. Case 421 : Desc = "支持库"
  1116. Case 435 : Desc = "服务器"
  1117. Case 4471 : Desc = "科幻"
  1118. Case 4483 : Desc = "战斗"
  1119. Case 4477 : Desc = "小游戏"
  1120. Case 4478 : Desc = "任务"
  1121. Case 4484 : Desc = "多人"
  1122. Case 4476 : Desc = "探索"
  1123. Case 4736 : Desc = "空岛"
  1124. Case 4475 : Desc = "冒险"
  1125. Case 4487 : Desc = "FTB"
  1126. Case 4480 : Desc = "基于地图"
  1127. Case 4479 : Desc = "硬核"
  1128. Case 4472 : Desc = "科技"
  1129. Case 4473 : Desc = "魔法"
  1130. Case 4481 : Desc = "小型整合"
  1131. Case 4482 : Desc = "大型整合"
  1132. Case Else : Continue For
  1133. End Select
  1134. CategoryDesc.Add(Desc)
  1135. Next
  1136. If CategoryDesc.Count = 0 Then CategoryDesc.Add("杂类")
  1137. '获取图片
  1138. If Data("logo").Count > 0 Then
  1139. If GetPixelSize(1) > 1.25 Then
  1140. Thumb = Data("logo")("thumbnailUrl").ToString '使用 256x256 图标
  1141. Else
  1142. Thumb = Data("logo")("thumbnailUrl").ToString.Replace("/256/256/", "/64/64/") '使用 64x64 图标
  1143. End If
  1144. End If
  1145. '保存缓存
  1146. If Not DlCfProjectCache.ContainsKey(Id) OrElse DlCfProjectCache(Id).Files Is Nothing Then DictionaryAdd(DlCfProjectCache, Id, Me)
  1147. End Sub
  1148. Public Function ToCfItem(ShowVersionDesc As Boolean, ShowLoaderDesc As Boolean, Optional OnClick As MyCfItem.ClickEventHandler = Nothing) As MyCfItem
  1149. Dim NewItem As New MyCfItem With {.Tag = Me}
  1150. NewItem.LabTitle.Text = ChineseName
  1151. NewItem.LabInfo.Text = Description.Replace(vbCr, "").Replace(vbLf, "")
  1152. NewItem.LabLeft.Text = If(ModLoaders.Count > 0 AndAlso ShowLoaderDesc, "[" & Join(ModLoaders, " & ") & "] ", "") &
  1153. If(ShowVersionDesc, GameVersionDesc, "") &
  1154. Join(CategoryDesc, ",") & " (" &
  1155. GetTimeSpanString(DateUpdate - Date.Now) & "更新" &
  1156. If(DownloadCount > 0,
  1157. "," & If(DownloadCount > 100000, Math.Round(DownloadCount / 10000) & " 万次下载", DownloadCount & " 次下载"), "") & ")"
  1158. If Thumb Is Nothing Then
  1159. NewItem.Logo = "pack://application:,,,/images/Icons/NoIcon.png"
  1160. Else
  1161. NewItem.Logo = Thumb
  1162. End If
  1163. If OnClick IsNot Nothing Then
  1164. AddHandler NewItem.Click, OnClick
  1165. NewItem.HasAnimation = True
  1166. Else
  1167. NewItem.HasAnimation = False
  1168. End If
  1169. '结束
  1170. Return NewItem
  1171. End Function
  1172. Public Overrides Function Equals(obj As Object) As Boolean
  1173. Dim project = TryCast(obj, DlCfProject)
  1174. Return project IsNot Nothing AndAlso Id = project.Id
  1175. End Function
  1176. Public Shared Operator =(left As DlCfProject, right As DlCfProject) As Boolean
  1177. Return EqualityComparer(Of DlCfProject).Default.Equals(left, right)
  1178. End Operator
  1179. Public Shared Operator <>(left As DlCfProject, right As DlCfProject) As Boolean
  1180. Return Not left = right
  1181. End Operator
  1182. End Class
  1183. Public Class DlCfProjectResult
  1184. Public Projects As List(Of DlCfProject)
  1185. Public Index As Integer
  1186. Public RealCount As Long
  1187. End Class
  1188. ''' <summary>
  1189. ''' CurseForge 工程列表获取事件。
  1190. ''' </summary>
  1191. Public Sub DlCfProjectSub(Task As LoaderTask(Of DlCfProjectRequest, DlCfProjectResult))
  1192. Dim RawFilter As String = If(Task.Input.SearchFilter, "").Trim
  1193. Task.Input.SearchFilter = RawFilter
  1194. RawFilter = RawFilter.ToLower
  1195. Log("[Download] CurseForge 工程列表搜索原始文本:" & RawFilter)
  1196. '中文请求关键字处理
  1197. Dim IsChineseSearch As Boolean = RegexCheck(RawFilter, "[\u4e00-\u9fbb]")
  1198. If IsChineseSearch AndAlso Not String.IsNullOrEmpty(RawFilter) Then
  1199. If Task.Input.IsModPack Then Throw New Exception("整合包搜索仅支持英文")
  1200. ReleaseCfDatabase()
  1201. '构造搜索请求
  1202. Dim SearchEntries As New List(Of SearchEntry(Of String))
  1203. For Each Entry In DlCfProjectDb.Values
  1204. If If(Entry.ChineseName, "").Contains("动态的树") Then Continue For '傻逼 Mod 的附属太多了
  1205. SearchEntries.Add(New SearchEntry(Of String) With {
  1206. .Item = Entry.ChineseName,
  1207. .SearchSource = New List(Of KeyValuePair(Of String, Double)) From {
  1208. New KeyValuePair(Of String, Double)(Entry.ChineseName & Entry.CurseForgeId, 1)
  1209. }
  1210. })
  1211. Next
  1212. '获取搜索结果
  1213. Dim SearchResults = Search(SearchEntries, Task.Input.SearchFilter, 3)
  1214. If SearchResults.Count = 0 Then Throw New Exception("无搜索结果,请尝试搜索英文名称")
  1215. Dim SearchResult As String = ""
  1216. For i = 0 To SearchResults.Count - 1
  1217. If Not SearchResults(i).AbsoluteRight AndAlso i >= Math.Min(2, SearchResults.Count - 1) Then Exit For '把 3 个结果拼合以提高准确度
  1218. SearchResult += SearchResults(i).Item.Replace(" (", "|").Split("|").Last.TrimEnd(")") & " "
  1219. Next
  1220. Log("[Download] CurseForge 工程列表中文搜索原始关键词:" & SearchResult, LogLevel.Developer)
  1221. '去除常见连接词
  1222. Dim RealFilter As String = ""
  1223. For Each Word In SearchResult.Split(" ")
  1224. If {"the", "of", "a", "mod"}.Contains(Word.ToLower) Then Continue For
  1225. If SearchResult.Split(" ").Count > 3 AndAlso {"ftb"}.Contains(Word.ToLower) Then Continue For
  1226. RealFilter += Word & " "
  1227. Next
  1228. Task.Input.SearchFilter = RealFilter
  1229. Log("[Download] CurseForge 工程列表中文搜索最终关键词:" & RealFilter, LogLevel.Developer)
  1230. End If
  1231. '驼峰英文请求关键字处理
  1232. Dim SpacedKeywords = RegexReplace(Task.Input.SearchFilter, "$& ", "([A-Z]+|[a-z]+?)(?=[A-Z]+[a-z]+[a-z ]*)")
  1233. Dim ConnectedKeywords = Task.Input.SearchFilter.Replace(" ", "")
  1234. Dim AllPossibleKeywords = (SpacedKeywords & " " & If(IsChineseSearch, Task.Input.SearchFilter, ConnectedKeywords & " " & RawFilter)).ToLower
  1235. '最终处理关键字:分割、去重
  1236. Dim RightKeywords As New List(Of String)
  1237. For Each Keyword In AllPossibleKeywords.Split(" ")
  1238. If Keyword.Trim = "" Then Continue For
  1239. If Keyword = "forge" OrElse Keyword = "fabric" OrElse Keyword = "for" OrElse Keyword = "mod" Then 'https://github.com/Hex-Dragon/PCL2/issues/208
  1240. Log("[Download] 已跳过搜索关键词 " & Keyword, LogLevel.Developer)
  1241. Continue For
  1242. End If
  1243. RightKeywords.Add(Keyword)
  1244. Next
  1245. If RawFilter.Length > 0 AndAlso RightKeywords.Count = 0 Then
  1246. Task.Input.SearchFilter = RawFilter '全都被过滤掉了
  1247. Else
  1248. Task.Input.SearchFilter = Join(ArrayNoDouble(RightKeywords), " ").ToLower
  1249. End If
  1250. '例外项:OptiForge、OptiFabric(拆词后因为包含 Forge/Fabric 导致无法搜到实际的 Mod)
  1251. If RawFilter.Replace(" ", "").ToLower.Contains("optiforge") Then Task.Input.SearchFilter = "optiforge"
  1252. If RawFilter.Replace(" ", "").ToLower.Contains("optifabric") Then Task.Input.SearchFilter = "optifabric"
  1253. Log("[Download] CurseForge 工程列表搜索最终文本:" & Task.Input.SearchFilter, LogLevel.Developer)
  1254. '正式获取
  1255. Dim Url = Task.Input.GetAddress()
  1256. Log("[Download] 开始获取 CurseForge 工程列表:" & Url)
  1257. Dim RequestResult As JObject = NetGetCodeByRequestRetry(Url, IsJson:=True, Encode:=Encoding.UTF8)
  1258. Dim FileList = GetCfProjectListFromJson(RequestResult("data"), Task.Input.IsModPack)
  1259. If FileList.Count = 0 Then Throw New Exception("无搜索结果")
  1260. '重新排序
  1261. If Not String.IsNullOrEmpty(Task.Input.SearchFilter) Then
  1262. '排序分 = 搜索相对相似度 + 默认排序相对活跃度
  1263. Dim Scores As New Dictionary(Of DlCfProject, Double)
  1264. Dim Entry As New List(Of SearchEntry(Of DlCfProject))
  1265. For i = 1 To FileList.Count
  1266. Dim File = FileList(i - 1)
  1267. Scores.Add(File, (1 - (i / FileList.Count)))
  1268. Entry.Add(New SearchEntry(Of DlCfProject) With {.Item = File, .SearchSource = New List(Of KeyValuePair(Of String, Double)) From {
  1269. New KeyValuePair(Of String, Double)(If(IsChineseSearch, File.ChineseName, File.Name), 1),
  1270. New KeyValuePair(Of String, Double)(File.Description, 0.05)}})
  1271. Next
  1272. Dim SearchResult = Search(Entry, RawFilter, 101, -1)
  1273. For Each OneResult In SearchResult
  1274. Scores(OneResult.Item) += OneResult.Similarity / SearchResult(0).Similarity
  1275. Next
  1276. '根据排序分得出结果
  1277. Dim ResultList = Sort(Scores.ToList, Function(Left As KeyValuePair(Of DlCfProject, Double), Right As KeyValuePair(Of DlCfProject, Double))
  1278. Return Left.Value > Right.Value
  1279. End Function)
  1280. FileList = New List(Of DlCfProject)
  1281. For Each Result In ResultList
  1282. FileList.Add(Result.Key)
  1283. Next
  1284. End If
  1285. Task.Output = New DlCfProjectResult With {.Projects = FileList, .Index = RequestResult("pagination")("index"), .RealCount = RequestResult("pagination")("totalCount")}
  1286. End Sub
  1287. ''' <summary>
  1288. ''' 从 API 返回的 Json 数组中获取工程信息数组。
  1289. ''' </summary>
  1290. Private Function GetCfProjectListFromJson(Json As JArray, IsModPack As Boolean) As List(Of DlCfProject)
  1291. Dim FileList As New List(Of DlCfProject)
  1292. For Each JsonEntry As JObject In Json
  1293. Dim Result = GetCfProjectFromJson(JsonEntry, IsModPack)
  1294. If Result IsNot Nothing Then FileList.Add(Result)
  1295. Next
  1296. Return FileList
  1297. End Function
  1298. ''' <summary>
  1299. ''' 从 API 返回的 Json 中获取工程信息。
  1300. ''' </summary>
  1301. Private Function GetCfProjectFromJson(Json As JObject, IsModPack As Boolean) As DlCfProject
  1302. If Json("links")("websiteUrl") Is Nothing Then
  1303. Log("[Download] 发现关键项为 Nothing 的工程:" & If(Json, "").ToString, LogLevel.Debug)
  1304. Return Nothing
  1305. End If
  1306. If IsModPack = Json("links")("websiteUrl").ToString.Contains("curseforge.com/minecraft/mc-mods") Then
  1307. Log("[Download] 返回的工程与要求的类别不一致:" & Json("links")("websiteUrl").ToString, LogLevel.Debug)
  1308. Return Nothing
  1309. End If
  1310. Return New DlCfProject(Json)
  1311. End Function
  1312. ''' <summary>
  1313. ''' 读取 Mod 数据库。
  1314. ''' </summary>
  1315. Private Sub ReleaseCfDatabase()
  1316. If DlCfProjectDb IsNot Nothing Then Exit Sub
  1317. DlCfProjectDb = New Dictionary(Of String, DlCfDbEntry)
  1318. For Each Line In DecodeBytes(GetResources("ModData")).Split(vbLf)
  1319. Dim Entry = New DlCfDbEntry
  1320. Dim SplitedLine = Line.Split("|")
  1321. Entry.CurseForgeId = SplitedLine(0)
  1322. Entry.WikiId = SplitedLine(1)
  1323. Entry.ChineseName = SplitedLine(2)
  1324. Entry.MCBBS = If(SplitedLine.Count = 4, SplitedLine(3), Nothing)
  1325. If Entry.ChineseName.Contains("*") Then '处理 *
  1326. Entry.ChineseName = Entry.ChineseName.Replace("*", " (" &
  1327. String.Join(" ", Entry.CurseForgeId.Split("-").Select(Function(w) w.Substring(0, 1).ToUpper & w.Substring(1, w.Length - 1))) & ")")
  1328. End If
  1329. DlCfProjectDb.Add(Entry.CurseForgeId, Entry)
  1330. Next
  1331. End Sub
  1332. Public Class DlCfDbEntry
  1333. ''' <summary>
  1334. ''' McMod 的 ID。
  1335. ''' </summary>
  1336. Public WikiId As Integer
  1337. ''' <summary>
  1338. ''' 中文译名。若为 Nothing 或空字符串则使用英文名。
  1339. ''' </summary>
  1340. Public ChineseName As String
  1341. ''' <summary>
  1342. ''' MCBBS 发布帖编号(例如 195107)。若为 Nothing 则没有发布帖。
  1343. ''' </summary>
  1344. Public MCBBS As Integer?
  1345. ''' <summary>
  1346. ''' CurseForge ID(例如 advanced-solar-panels)。
  1347. ''' </summary>
  1348. Public CurseForgeId As String
  1349. Public Overrides Function ToString() As String
  1350. Return CurseForgeId & "|" & WikiId & "|" & ChineseName & If(MCBBS Is Nothing, "", "|" & MCBBS)
  1351. End Function
  1352. End Class
  1353. #End Region
  1354. #Region "DlCfFile | CurseForge Files"
  1355. Public DlCfFileLoader As New LoaderTask(Of KeyValuePair(Of Integer, Boolean), List(Of DlCfFile))("DlCfFile Main", Sub(Task As LoaderTask(Of KeyValuePair(Of Integer, Boolean), List(Of DlCfFile))) Task.Output = DlCfGetFiles(Task.Input.Key, Task.Input.Value))
  1356. ''' <summary>
  1357. ''' CurseForge 工程下的单一版本文件。
  1358. ''' </summary>
  1359. Public Class DlCfFile
  1360. Public IsModPack As Boolean
  1361. Public FileId As Integer
  1362. Public DisplayName As String
  1363. Public [Date] As Date
  1364. Public GameVersion As String()
  1365. Public ReleaseType As Integer
  1366. Public FileName As String
  1367. Public DownloadCount As Integer
  1368. Public Dependencies As New List(Of Integer)
  1369. Private DownloadAddress As String()
  1370. Public ReadOnly Property ReleaseTypeString As String
  1371. Get
  1372. Select Case ReleaseType
  1373. Case 1
  1374. Return "正式版"
  1375. Case 2
  1376. Return If(ModeDebug, "Beta 测试版", "测试版")
  1377. Case Else
  1378. Return If(ModeDebug, "Alpha 测试版", "测试版")
  1379. End Select
  1380. End Get
  1381. End Property
  1382. Public ReadOnly Property IsAvailable As Boolean
  1383. Get
  1384. Return DisplayName IsNot Nothing AndAlso GameVersion IsNot Nothing AndAlso FileName IsNot Nothing AndAlso DownloadAddress(0).StartsWith("http")
  1385. End Get
  1386. End Property
  1387. ''' <summary>
  1388. ''' 从 CurseForge 文件 Json 项中初始化实例。若出错会抛出异常。
  1389. ''' </summary>
  1390. Public Sub New(Data As JObject, IsModPack As Boolean)
  1391. FileId = Data("id")
  1392. DisplayName = Data("displayName").ToString.Replace(" ", "").Trim(" ")
  1393. [Date] = Data("fileDate")
  1394. ReleaseType = Data("releaseType")
  1395. DownloadCount = Data("downloadCount")
  1396. FileName = Data("fileName")
  1397. Me.IsModPack = IsModPack
  1398. '获取下载地址
  1399. Dim Url = Data("downloadUrl").ToString
  1400. If Url = "" Then Url = "https://media.forgecdn.net/files/" & FileId.ToString.Substring(0, 4) & "/" & FileId.ToString.Substring(4).TrimStart("0") & "/" & FileName
  1401. Url = Url.Replace(FileName, Net.WebUtility.UrlEncode(FileName)) '对文件名进行编码
  1402. DownloadAddress = ArrayNoDouble({Url.Replace("-service.overwolf.wtf", ".forgecdn.net").Replace("://edge", "://media"), Url.Replace("-service.overwolf.wtf", ".forgecdn.net"), Url.Replace("://edge", "://media"), Url})
  1403. '获取前置 Mod 的 Addon ID
  1404. If Not IsModPack Then
  1405. For Each Dep In Data("dependencies")
  1406. If Dep("relationType").ToObject(Of Integer) = 3 AndAlso '是一个依赖
  1407. Dep("modId").ToObject(Of Integer) <> 306612 Then '排除 Fabric API
  1408. Dependencies.Add(Dep("modId"))
  1409. End If
  1410. Next
  1411. End If
  1412. '获取游戏版本
  1413. Dim Versions As New List(Of String)
  1414. For Each Version In Data("gameVersions")
  1415. If Version.ToString.StartsWith("1.") OrElse Version.ToString.Contains("w") Then
  1416. Versions.Add(Version.ToString.Trim.ToLower)
  1417. End If
  1418. Next
  1419. If Versions.Count > 1 Then
  1420. GameVersion = Sort(Versions, AddressOf VersionSortBoolean).ToArray
  1421. If IsModPack Then GameVersion = {GameVersion(0)}
  1422. ElseIf Versions.Count = 1 Then
  1423. GameVersion = Versions.ToArray
  1424. 'ElseIf Data("gameVersion").Count = 1 AndAlso Not Data("gameVersion")(0).ToString.Contains("1.") Then
  1425. ' GameVersion = {"1.16.4"}
  1426. Else
  1427. GameVersion = {"未知版本"}
  1428. End If
  1429. End Sub
  1430. ''' <summary>
  1431. ''' 获取对应的下载文件信息。
  1432. ''' </summary>
  1433. ''' <param name="LocalAddress">本地文件夹或文件地址。</param>
  1434. Public Function GetDownloadFile(LocalAddress As String, IsFullPath As Boolean) As NetFile
  1435. Return New NetFile(DownloadAddress, LocalAddress & If(IsFullPath, "", FileName))
  1436. End Function
  1437. Public Function ToListItem(OnClick As MyListItem.ClickEventHandler, Optional OnSaveClick As MyIconButton.ClickEventHandler = Nothing) As MyListItem
  1438. '获取描述信息
  1439. Dim Info As String = ""
  1440. If Not IsModPack Then
  1441. Info += "适用于 " & Join(GameVersion, "、").Replace("-snapshot", " 快照") &
  1442. If(ModeDebug AndAlso Dependencies.Count > 0, "," & Dependencies.Count & " 个前置,", ",")
  1443. End If
  1444. Info += If(DownloadCount > 0, If(DownloadCount > 100000, Math.Round(DownloadCount / 10000) & " 万次下载,", DownloadCount & " 次下载,"), "")
  1445. Info += GetTimeSpanString([Date] - Date.Now) & "更新"
  1446. Info += If(ReleaseType <> 1, "," & ReleaseTypeString, "")
  1447. '建立控件
  1448. Dim NewItem As New MyListItem With {
  1449. .Title = DisplayName, .SnapsToDevicePixels = True, .Height = 42, .Type = MyListItem.CheckType.Clickable, .Tag = Me,
  1450. .Info = Info
  1451. }
  1452. Select Case ReleaseType
  1453. Case 1 'R
  1454. NewItem.Logo = "pack://application:,,,/images/Icons/R.png"
  1455. Case 2 'B
  1456. NewItem.Logo = "pack://application:,,,/images/Icons/B.png"
  1457. Case Else 'A
  1458. NewItem.Logo = "pack://application:,,,/images/Icons/A.png"
  1459. End Select
  1460. AddHandler NewItem.Click, OnClick
  1461. '建立另存为按钮
  1462. If OnSaveClick IsNot Nothing Then
  1463. Dim BtnSave As New MyIconButton With {.Logo = Logo.IconButtonSave, .ToolTip = "另存为"}
  1464. ToolTipService.SetPlacement(BtnSave, Primitives.PlacementMode.Center)
  1465. ToolTipService.SetVerticalOffset(BtnSave, 30)
  1466. ToolTipService.SetHorizontalOffset(BtnSave, 2)
  1467. AddHandler BtnSave.Click, OnSaveClick
  1468. NewItem.Buttons = {BtnSave}
  1469. NewItem.PaddingRight = 35
  1470. End If
  1471. '结束
  1472. Return NewItem
  1473. End Function
  1474. Public Overrides Function ToString() As String
  1475. Return DisplayName
  1476. End Function
  1477. End Class
  1478. ''' <summary>
  1479. ''' 获取某个 CurseForge 工程下的全部文件列表。
  1480. ''' 必须在工作线程执行,失败会抛出异常。
  1481. ''' </summary>
  1482. Public Function DlCfGetFiles(ProjectId As Integer, IsModPack As Boolean) As List(Of DlCfFile)
  1483. '获取工程对象
  1484. Dim TargetProject As DlCfProject
  1485. If DlCfProjectCache.ContainsKey(ProjectId) Then
  1486. TargetProject = DlCfProjectCache(ProjectId)
  1487. Else
  1488. TargetProject = GetCfProjectFromJson(NetGetCodeByRequestRetry("https://api.curseforge.com/v1/mods/" & ProjectId, IsJson:=True, Encode:=Encoding.UTF8)("data"), IsModPack)
  1489. End If
  1490. '获取工程对象的文件列表
  1491. If TargetProject.Files Is Nothing Then
  1492. Log("[Download] 开始获取 CurseForge 工程 ID 为 " & ProjectId & " 的文件列表")
  1493. Dim Json As JArray
  1494. If TargetProject.IsModPack Then
  1495. '整合包使用全部文件
  1496. Json = NetGetCodeByRequestRetry("https://api.curseforge.com/v1/mods/" & ProjectId & "/files?pageSize=999", Accept:="application/json", IsJson:=True)("data")
  1497. Else
  1498. 'Mod 使用每个版本最新的文件
  1499. Json = GetJson(NetRequestRetry("https://api.curseforge.com/v1/mods/files", "POST", "{""fileIds"": [" & Join(TargetProject.FileIndexes, ",") & "]}", "application/json"))("data")
  1500. End If
  1501. Dim Files As New Dictionary(Of Integer, DlCfFile)
  1502. For Each JsonEntry As JObject In Json
  1503. Dim NewFile As New DlCfFile(JsonEntry, IsModPack)
  1504. If NewFile.IsAvailable AndAlso Not Files.ContainsKey(NewFile.FileId) Then Files.Add(NewFile.FileId, NewFile)
  1505. Next
  1506. TargetProject.Files = Files.Values.ToList
  1507. End If
  1508. '获取前置 Mod 列表
  1509. If IsModPack Then GoTo ExitSub
  1510. Dim Deps As New List(Of Integer)
  1511. For Each File In TargetProject.Files
  1512. For Each AddonId In File.Dependencies
  1513. If Not Deps.Contains(AddonId) AndAlso Not DlCfProjectCache.ContainsKey(AddonId) Then Deps.Add(AddonId)
  1514. Next
  1515. Next
  1516. If Deps.Count > 0 Then
  1517. '获取前置 Mod 工程信息
  1518. Log("[Download] 文件列表中需要获取的前置 Mod:" & Join(Deps, ","))
  1519. GetCfProjectListFromJson(GetJson(NetRequestRetry("https://api.curseforge.com/v1/mods", "POST", "{""modIds"": [" & Join(Deps, ",") & "]}", "application/json"))("data"), False)
  1520. End If
  1521. '返回
  1522. ExitSub:
  1523. Return TargetProject.Files
  1524. End Function
  1525. Public Sub DlCfFilesPreload(Stack As StackPanel, Entrys As List(Of DlCfFile), OnClick As MyListItem.ClickEventHandler)
  1526. '获取卡片对应的前置 ID
  1527. '如果为整合包就不会有 Dependencies 信息,所以不用管
  1528. Dim Deps As New List(Of Integer)
  1529. For Each File In Entrys
  1530. For Each AddonId In If(File.Dependencies, New List(Of Integer))
  1531. If Not Deps.Contains(AddonId) Then
  1532. If Not DlCfProjectCache.ContainsKey(AddonId) Then
  1533. Log("[Download] 未找到 ID 为 " & AddonId & " 的前置 Mod 信息", LogLevel.Developer)
  1534. Continue For
  1535. End If
  1536. Deps.Add(AddonId)
  1537. End If
  1538. Next
  1539. Next
  1540. Deps.Remove(306612) '移除 Fabric API
  1541. If Deps.Count = 0 Then Exit Sub
  1542. Deps.Sort()
  1543. '添加开头间隔
  1544. Stack.Children.Add(New TextBlock With {.Text = "前置 Mod", .FontSize = 14, .HorizontalAlignment = HorizontalAlignment.Left, .Margin = New Thickness(6, 2, 0, 5)})
  1545. '添加前置 Mod 列表
  1546. For Each Dep In Deps
  1547. Dim Item = DlCfProjectCache(Dep).ToCfItem(False, True, AddressOf FrmDownloadMod.ProjectClick)
  1548. 'Item.Margin = New Thickness(5, 0, 0, 0)
  1549. Stack.Children.Add(Item)
  1550. Next
  1551. '添加结尾间隔
  1552. Stack.Children.Add(New TextBlock With {.Text = "可选版本", .FontSize = 14, .HorizontalAlignment = HorizontalAlignment.Left, .Margin = New Thickness(6, 12, 0, 5)})
  1553. End Sub
  1554. #End Region
  1555. #Region "DlSource | 镜像下载源"
  1556. Public Function DlSourceResourceGet(MojangBase As String) As String()
  1557. Return {MojangBase.Replace("http://resources.download.minecraft.net", "https://download.mcbbs.net/assets"),
  1558. MojangBase,
  1559. MojangBase.Replace("http://resources.download.minecraft.net", "https://bmclapi2.bangbang93.com/assets")
  1560. }
  1561. End Function
  1562. Public Function DlSourceLibraryGet(MojangBase As String) As String()
  1563. Return {MojangBase.Replace("https://libraries.minecraft.net", "https://download.mcbbs.net/maven"),
  1564. MojangBase.Replace("https://libraries.minecraft.net", "https://download.mcbbs.net/libraries"),
  1565. MojangBase,
  1566. MojangBase.Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/maven"),
  1567. MojangBase.Replace("https://libraries.minecraft.net", "https://bmclapi2.bangbang93.com/libraries")
  1568. }
  1569. End Function
  1570. Public Function DlSourceLauncherOrMetaGet(MojangBase As String, Optional IsStatic As Boolean = True) As String()
  1571. If MojangBase Is Nothing Then Throw New Exception("无对应的 Json 下载地址")
  1572. If IsStatic Then
  1573. Return {MojangBase.Replace("https://launcher.mojang.com", "https://download.mcbbs.net").Replace("https://launchermeta.mojang.com", "https://download.mcbbs.net"),
  1574. MojangBase,
  1575. MojangBase.Replace("https://launcher.mojang.com", "https://bmclapi2.bangbang93.com").Replace("https://launchermeta.mojang.com", "https://bmclapi2.bangbang93.com")
  1576. }
  1577. Else
  1578. Return {MojangBase.Replace("https://launcher.mojang.com", "https://download.mcbbs.net").Replace("https://launchermeta.mojang.com", "https://download.mcbbs.net"),
  1579. MojangBase,
  1580. MojangBase.Replace("https://launcher.mojang.com", "https://bmclapi2.bangbang93.com").Replace("https://launchermeta.mojang.com", "https://bmclapi2.bangbang93.com")
  1581. }
  1582. End If
  1583. End Function
  1584. Private Sub DlSourceLoader(Of InputType, OutputType)(MainLoader As LoaderTask(Of InputType, OutputType),
  1585. LoaderList As List(Of KeyValuePair(Of LoaderTask(Of InputType, OutputType), Integer)),
  1586. Optional IsForceRestart As Boolean = False)
  1587. Dim WaitCycle As Integer = 0
  1588. Do While True
  1589. '检查加载结束
  1590. Dim IsAllFailed As Boolean = True
  1591. For Each SubLoader In LoaderList
  1592. If WaitCycle = 0 Then
  1593. '如果要强制刷新,就不使用已经加载好的值
  1594. If IsForceRestart Then Exit For
  1595. '如果输入不一样,就不使用已经加载好的值
  1596. If (SubLoader.Key.Input Is Nothing Xor MainLoader.Input Is Nothing) OrElse
  1597. (SubLoader.Key.Input IsNot Nothing AndAlso Not SubLoader.Key.Input.Equals(MainLoader.Input)) Then Continue For
  1598. End If
  1599. If SubLoader.Key.State <> LoadState.Failed Then IsAllFailed = False
  1600. If SubLoader.Key.State = LoadState.Finished Then
  1601. MainLoader.Output = SubLoader.Key.Output
  1602. DlSourceLoaderAbort(LoaderList)
  1603. Exit Sub
  1604. ElseIf IsAllFailed Then
  1605. If WaitCycle < SubLoader.Value * 100 Then WaitCycle = SubLoader.Value * 100
  1606. End If
  1607. '由于 Forge BMCLAPI 没有可用版本导致强制失败
  1608. '在没有可用版本时,官方源会一直卡住,直接使用 BMCLAPI 判定失败即可
  1609. If SubLoader.Key.Error IsNot Nothing AndAlso SubLoader.Key.Error.Message.Contains("没有可用版本") Then
  1610. For Each SubLoader2 In LoaderList
  1611. If WaitCycle < SubLoader2.Value * 100 Then WaitCycle = SubLoader2.Value * 100
  1612. Next
  1613. End If
  1614. Next
  1615. '启动加载源
  1616. If WaitCycle = 0 Then
  1617. '启动第一个源
  1618. LoaderList(0).Key.Start(MainLoader.Input, IsForceRestart)
  1619. '将其他源标记为未启动,以确保可以切换下载源(#184)
  1620. For i = 1 To LoaderList.Count - 1
  1621. LoaderList(i).Key.State = LoadState.Waiting
  1622. Next
  1623. End If
  1624. For i = 0 To LoaderList.Count - 1
  1625. If WaitCycle = LoaderList(i).Value * 100 Then
  1626. If i < LoaderList.Count - 1 Then
  1627. '启动下一个源
  1628. LoaderList(i + 1).Key.Start(MainLoader.Input, IsForceRestart)
  1629. Else
  1630. '失败
  1631. Dim ErrorInfo As Exception = Nothing
  1632. For ii = 0 To LoaderList.Count - 1
  1633. LoaderList(ii).Key.Input = Nothing '重置输入,以免以同样的输入“重试加载”时直接失败
  1634. If LoaderList(ii).Key.Error IsNot Nothing Then
  1635. If ErrorInfo Is Nothing OrElse LoaderList(ii).Key.Error.Message.Contains("没有可用版本") Then
  1636. ErrorInfo = LoaderList(ii).Key.Error
  1637. End If
  1638. End If
  1639. Next
  1640. If ErrorInfo Is Nothing Then ErrorInfo = New TimeoutException("下载源连接超时")
  1641. DlSourceLoaderAbort(LoaderList)
  1642. Throw ErrorInfo
  1643. End If
  1644. Exit For
  1645. End If
  1646. Next
  1647. '计时
  1648. If MainLoader.IsAborted Then
  1649. DlSourceLoaderAbort(LoaderList)
  1650. Exit Sub
  1651. End If
  1652. Thread.Sleep(10)
  1653. WaitCycle += 1
  1654. Loop
  1655. End Sub
  1656. Private Sub DlSourceLoaderAbort(Of InputType, OutputType)(LoaderList As List(Of KeyValuePair(Of LoaderTask(Of InputType, OutputType), Integer)))
  1657. For Each Loader In LoaderList
  1658. If Loader.Key.State = LoadState.Loading Then Loader.Key.Abort()
  1659. Next
  1660. End Sub
  1661. #End Region
  1662. End Module