diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt index 3b83606e2..c76647687 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt @@ -14,13 +14,12 @@ object ConsoleUpdator{ private object Links:HashMap>() { init { put(CONSOLE_PURE, mapOf( - "version" to "/net/mamoe/mirai-console/", - "jcenter" to "https://jcenter.bintray.com/net/mamoe/mirai-console/{version}/:mirai-console-{version}.jar", - "aliyun" to "https://maven.aliyun.com/nexus/content/repositories/jcenter/net/mamoe/mirai-console/{version}/mirai-console-{version}.jar" + "version" to "/net/mamoe/mirai-console/" )) } } + var consoleType = CONSOLE_PURE fun getFile():File?{ @@ -102,7 +101,8 @@ object ConsoleUpdator{ Http.downloadMavenArchive("net/mamoe",getProjectName(),version) .saveToContent("${getProjectName()}-$version.jar") } - + LibManager.clearLibs() + LibManager.addDependencyRequest("net/mamoe",getProjectName(),version) } diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt index 7929b4a7d..f2481e28d 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/CoreUpdator.kt @@ -108,19 +108,6 @@ object CoreUpdator { } - @Suppress("SpellCheckingInspection") - private object Links { - internal const val libJcenter = - "https://jcenter.bintray.com/net/mamoe/mirai-core-qqandroid-jvm/{version}/:mirai-core-qqandroid-jvm-{version}.jar" - internal const val libAliyun = - "https://maven.aliyun.com/nexus/content/repositories/jcenter/net/mamoe/mirai-core-qqandroid-jvm/{version}/mirai-core-qqandroid-jvm-{version}.jar" - - internal const val coreJcenter = - "https://jcenter.bintray.com/net/mamoe/mirai-core-jvm/{version}/:mirai-core-jvm-{version}.jar" - internal const val coreAliyun = - "https://maven.aliyun.com/nexus/content/repositories/jcenter/net/mamoe/mirai-core-jvm/{version}/mirai-core-jvm-{version}.jar" - } - private suspend fun downloadCoreAndLib(version: String) { coroutineScope { launch { @@ -136,7 +123,14 @@ object CoreUpdator { .saveToContent("mirai-core-jvm-$version.jar") } } + + launch { + LibManager.clearLibs() + LibManager.addDependencyRequest("net/mamoe","mirai-core-jvm",version) + LibManager.addDependencyRequest("net/mamoe","mirai-core-qqandroid-jvm",version) + } } + } diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt index 61507944c..c28cfe738 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt @@ -3,6 +3,7 @@ package net.mamoe.mirai.console.wrapper import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO +import io.ktor.client.features.ClientRequestException import io.ktor.client.request.get import io.ktor.client.statement.HttpResponse import io.ktor.utils.io.ByteReadChannel @@ -43,7 +44,26 @@ suspend inline fun HttpClient.downloadRequest(url: String): ByteReadChannel { private val jcenterPath = "https://jcenter.bintray.com/{group}/{project}/{version}/:{project}-{version}.{extension}" private val aliyunPath = "https://maven.aliyun.com/nexus/content/repositories/jcenter/{group}/{project}/{version}/{project}-{version}.{extension}" - +private fun String.buildPath( + groupName: String, + projectName: String, + version: String, + extension: String +):String{ + return this + .replace( + "{group}",groupName + ) + .replace( + "{project}",projectName + ) + .replace( + "{extension}",extension + ) + .replace( + "{version}",version + ) +} suspend fun HttpClient.downloadMaven( groupName: String, @@ -53,35 +73,11 @@ suspend fun HttpClient.downloadMaven( ):ByteReadChannel{ return kotlin.runCatching { downloadRequest( - aliyunPath - .replace( - "{group}",groupName - ) - .replace( - "{project}",projectName - ) - .replace( - "{extension}",extension - ) - .replace( - "{version}",version - ) + aliyunPath.buildPath(groupName,projectName,version,extension) ) }.getOrElse { downloadRequest( - jcenterPath - .replace( - "{group}",groupName - ) - .replace( - "{project}",projectName - ) - .replace( - "{extension}",extension - ) - .replace( - "{version}",version - ) + aliyunPath.buildPath(groupName,projectName,version,extension) ) } } @@ -102,6 +98,29 @@ suspend inline fun HttpClient.downloadMavenPom( return downloadMaven(groupName,projectName,version,"pom") } +suspend fun HttpClient.downloadMavenPomAsString( + groupName: String, + projectName: String, + version: String +):String{ + return kotlin.runCatching { + Http.get( + aliyunPath.buildPath(groupName,projectName,version,"pom") + ) + }.getOrElse { + try { + Http.get( + aliyunPath.buildPath(groupName, projectName, version, "pom") + ) + }catch (e:Exception){ + if(e.message?.contains("404 Not Found") == true) { + return "" + } + throw e + } + } +} + /** diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/LibManager.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/LibManager.kt index 8b3071d1b..7d4d250d7 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/LibManager.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/LibManager.kt @@ -1,6 +1,7 @@ package net.mamoe.mirai.console.wrapper import java.io.File +import java.util.* object LibManager{ @@ -12,6 +13,12 @@ object LibManager{ } } + /** + * 开关 + * 当前版本不写dynamic加载lib + */ + val dynamic = false; + fun clearLibs(){ libPath.listFiles()?.forEach { it.delete() @@ -24,36 +31,88 @@ object LibManager{ * 全部完成后使用 @link downloadIfNeeded()开始下载 */ - - /** - * 由Pom content提供必要依赖 - * LibManager会检查所有dependency的dependency - */ - internal fun addDependencyByPom(pomContent:String){ - - } - - /** * 由Pom Path提供必要依赖 * LibManager会进行下载和递归处理 */ - fun addDependencyRequest(link:String){ - val stream = kotlin.runCatching { - val jcenterPath = "https://jcenter.bintray.com/{group}/{project}/{version}/:{project}-{version}.pom" - val aliyunPath = "https://maven.aliyun.com/nexus/content/repositories/jcenter/{group}/{project}/{version}/{project}-{version}.pom" + suspend fun addDependencyRequest( + group: String, + project: String, + version: String + ){ + if(!dynamic){ + return; + } + var pom:String? = null + if(project.contains("maven") && project.contains("plugin")){ + return + } + if(project.contains("toplink-essentials") || project.contains("ejb") ||project.contains("glassfish-embedded-all") || project.contains("maven-bundle-plugin") || project.contains("jetty") || project.contains("plexus-component-annotations") || project.contains("slf4j") || project.contains("sisu-inject-plexus") || project.contains("maven-remote-resources-plugin") || project.contains("easymock") || project.contains("junit") || project.contains("log4j") || project.contains("doxia-logging-api") || project.contains("maven-enforcer-plugin") || project.contains("maven-plugin") || project.contains("maven-artifact") || project.contains("maven-core") || project.contains("cglib") || project.contains("spring-core")){ + return + } + tryNTimesOrQuit(3, "Failed to find dependency for $project") { + pom = Http.downloadMavenPomAsString( + group, project, version + ) + } + addDependency(group,project,version) + if(pom == null){ + println("Failed to load dependency POM") + return + } + pom!!.replace("\n","").split("").forEach { + if(it.contains("")) { + val dependencyInfo = it.replace("","") + if(dependencyInfo.contains("") && dependencyInfo.contains("") && dependencyInfo.contains("")) { + val groupName = + dependencyInfo.substringAfter("").substringBefore("").replace(".", "/") + .removeSuffix("/") + val projectName = dependencyInfo.substringAfter("").substringBefore("") + val versionName = dependencyInfo.substringAfter("").substringBefore("") + if (!versionName.contains("{")) { + if (addDependency(groupName, projectName, versionName)) { + addDependencyRequest(groupName, projectName, versionName) + } + } + } + } } } /** * 普通的增加一个dependency */ - fun addDependency(){ - + private val dependency = HashSet() + fun addDependency( + group: String, + project: String, + version: String + ):Boolean{ + if(!dynamic){ + return false; + } + if(project.contains("maven") && project.contains("plugin")){ + return true + } + if(project.contains("toplink-essentials") ||project.contains("ejb") || project.contains("glassfish-embedded-all") || project.contains("maven-bundle-plugin") || project.contains("jetty") || project.contains("slf4j") || project.contains("sisu-inject-plexus") || project.contains("maven-remote-resources-plugin") || project.contains("easymock") || project.contains("junit") || project.contains("log4j") || project.contains("doxia-logging-api") || project.contains("maven-enforcer-plugin") || project.contains("maven-plugin") || project.contains("maven-artifact") || project.contains("maven-core") || project.contains("cglib") || project.contains("spring-core")){ + return true + } + val id = "${group + .replace(".","/") + .removeSuffix("/") + }-$project:$version" + if(dependency.contains(id)){ + return false + } + println(id) + dependency.add(id) + return true } suspend fun downloadIfNeeded(){ - + this.dependency.forEach { + println(it) + } } } \ No newline at end of file diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt index dbb54b2b4..b6752b389 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt @@ -46,6 +46,10 @@ object WrapperMain { ConsoleUpdator.versionCheck(type) } } + println("Dependency needed:") + runBlocking { + LibManager.downloadIfNeeded() + } println("Version check complete, starting Mirai") } }