dynamic load libs

This commit is contained in:
jiahua.liu 2020-03-06 15:41:04 +08:00
parent daffe2c9d6
commit 35659f750c
5 changed files with 137 additions and 61 deletions

View File

@ -14,13 +14,12 @@ object ConsoleUpdator{
private object Links:HashMap<String,Map<String,String>>() {
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)
}

View File

@ -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,9 +123,16 @@ 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)
}
}
}
private fun loadCoreAndLib() {
try {

View File

@ -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<String>(
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
}
}
}
/**

View File

@ -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("</dependency>").forEach {
if(it.contains("<dependency>")) {
val dependencyInfo = it.replace("<dependency>","")
if(dependencyInfo.contains("<groupId>") && dependencyInfo.contains("<artifactId>") && dependencyInfo.contains("<version>")) {
val groupName =
dependencyInfo.substringAfter("<groupId>").substringBefore("</groupId>").replace(".", "/")
.removeSuffix("/")
val projectName = dependencyInfo.substringAfter("<artifactId>").substringBefore("</artifactId>")
val versionName = dependencyInfo.substringAfter("<version>").substringBefore("</version>")
if (!versionName.contains("{")) {
if (addDependency(groupName, projectName, versionName)) {
addDependencyRequest(groupName, projectName, versionName)
}
}
}
}
}
}
/**
* 普通的增加一个dependency
*/
fun addDependency(){
private val dependency = HashSet<String>()
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)
}
}
}

View File

@ -46,6 +46,10 @@ object WrapperMain {
ConsoleUpdator.versionCheck(type)
}
}
println("Dependency needed:")
runBlocking {
LibManager.downloadIfNeeded()
}
println("Version check complete, starting Mirai")
}
}