Skip to content

Commit 0b048a7

Browse files
committed
使用更先进的方式加载mirai核心
代价是日志输出变多和无法直接使用mcl下载的mirai
1 parent d5986a6 commit 0b048a7

File tree

12 files changed

+216
-263
lines changed

12 files changed

+216
-263
lines changed

MiraiMC-Base/pom.xml

+48
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<maven.compiler.target>8</maven.compiler.target>
1717
<kotlin.version>1.9.24</kotlin.version>
1818
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
19+
<aetherVersion>1.0.0.v20140518</aetherVersion>
20+
<mavenVersion>3.1.0</mavenVersion>
21+
<wagonVersion>1.0</wagonVersion>
1922
</properties>
2023

2124
<dependencies>
@@ -54,6 +57,51 @@
5457
<version>${kotlin.version}</version>
5558
<scope>provided</scope>
5659
</dependency>
60+
<dependency>
61+
<groupId>org.eclipse.aether</groupId>
62+
<artifactId>aether-api</artifactId>
63+
<version>${aetherVersion}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.eclipse.aether</groupId>
67+
<artifactId>aether-util</artifactId>
68+
<version>${aetherVersion}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.eclipse.aether</groupId>
72+
<artifactId>aether-impl</artifactId>
73+
<version>${aetherVersion}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.eclipse.aether</groupId>
77+
<artifactId>aether-connector-basic</artifactId>
78+
<version>${aetherVersion}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.eclipse.aether</groupId>
82+
<artifactId>aether-transport-file</artifactId>
83+
<version>${aetherVersion}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.eclipse.aether</groupId>
87+
<artifactId>aether-transport-http</artifactId>
88+
<version>${aetherVersion}</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.eclipse.aether</groupId>
92+
<artifactId>aether-transport-wagon</artifactId>
93+
<version>${aetherVersion}</version>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.apache.maven</groupId>
97+
<artifactId>maven-aether-provider</artifactId>
98+
<version>${mavenVersion}</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.apache.maven.wagon</groupId>
102+
<artifactId>wagon-ssh</artifactId>
103+
<version>${wagonVersion}</version>
104+
</dependency>
57105
</dependencies>
58106

59107
<build>

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/Platform.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.dreamvoid.miraimc;
22

3+
import me.dreamvoid.miraimc.internal.loader.LibraryLoader;
4+
35
import java.util.List;
46
import java.util.UUID;
57
import java.util.logging.Logger;
@@ -32,4 +34,6 @@ public interface Platform {
3234
IMiraiEvent getMiraiEvent();
3335

3436
MiraiMCConfig getPluginConfig();
37+
38+
LibraryLoader getLibraryLoader();
3539
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
package me.dreamvoid.miraimc.internal;
22

33
import me.dreamvoid.miraimc.MiraiMCConfig;
4+
import me.dreamvoid.miraimc.MiraiMCPlugin;
45
import me.dreamvoid.miraimc.internal.webapi.Info;
56
import me.dreamvoid.miraimc.internal.webapi.Version;
7+
import org.eclipse.aether.artifact.DefaultArtifact;
8+
import org.eclipse.aether.graph.Dependency;
9+
import org.eclipse.aether.repository.RemoteRepository;
610
import org.xml.sax.SAXException;
711

812
import javax.xml.parsers.ParserConfigurationException;
9-
import java.io.BufferedWriter;
10-
import java.io.File;
11-
import java.io.FileWriter;
1213
import java.io.IOException;
13-
import java.nio.charset.StandardCharsets;
14-
import java.nio.file.Files;
15-
16-
import static me.dreamvoid.miraimc.internal.loader.LibraryLoader.*;
1714

1815
public class MiraiLoader {
16+
private static final RemoteRepository mavenCentral = new RemoteRepository.Builder("central","default", MiraiMCConfig.General.MavenRepoUrl).build();
1917
/**
2018
* 加载最新版Mirai Core
2119
*/
2220
public static void loadMiraiCore() throws RuntimeException, IOException, ParserConfigurationException, SAXException {
23-
loadMiraiCore(getLibraryVersionMaven("net.mamoe", "mirai-core-all", MiraiMCConfig.General.MavenRepoUrl.replace("http://","https://"),"release"));
21+
loadMiraiCore("latest");
2422
}
2523

2624
public static String getStableVersion() {
@@ -67,43 +65,6 @@ public static String getStableVersion(String PluginVersion) {
6765
* @param version 版本
6866
*/
6967
public static void loadMiraiCore(String version) throws RuntimeException, IOException, ParserConfigurationException, SAXException {
70-
if(version.equalsIgnoreCase("latest")){
71-
version = getLibraryVersionMaven("net.mamoe", "mirai-core-all", MiraiMCConfig.General.MavenRepoUrl.replace("http://","https://"),"release");
72-
}
73-
74-
// 文件夹
75-
File LibrariesDir = new File(Utils.getMiraiDir(),"libs");
76-
if(!LibrariesDir.exists() && !LibrariesDir.mkdirs()) {
77-
throw new RuntimeException("Failed to create " + LibrariesDir.getPath());
78-
}
79-
80-
File writeName = new File(new File(MiraiMCConfig.PluginDir, "cache"), "core-ver");
81-
if(!writeName.getParentFile().exists() && !writeName.getParentFile().mkdirs()) {
82-
throw new RuntimeException("Failed to create " + writeName.getParentFile().getPath());
83-
}
84-
85-
try {
86-
loadJarMaven("net.mamoe", "mirai-core-all", version, "-all", MiraiMCConfig.General.MavenRepoUrl.replace("http://","https://"), LibrariesDir);
87-
try (FileWriter writer = new FileWriter(writeName);
88-
BufferedWriter out = new BufferedWriter(writer)
89-
) {
90-
out.write(version);
91-
out.flush();
92-
}
93-
} catch (Exception e) {
94-
Utils.getLogger().warning("Unable to download mirai core from remote server, try to use local core. ("+e+")");
95-
if(writeName.exists()) {
96-
String content = new String(Files.readAllBytes(writeName.toPath()), StandardCharsets.UTF_8);
97-
if(!content.equals("")){
98-
String name = "mirai-core-all" + "-" + content + ".jar"; // 文件名
99-
File coreFile = new File(LibrariesDir, name);
100-
loadJarLocal(coreFile);
101-
} else {
102-
Utils.getLogger().warning("Unable to use local core.");
103-
}
104-
} else {
105-
Utils.getLogger().warning("No local core found.");
106-
}
107-
}
68+
MiraiMCPlugin.getPlatform().getLibraryLoader().loadLibraryMaven(mavenCentral, new Dependency(new DefaultArtifact("net.mamoe:mirai-core-all:" + (version.equalsIgnoreCase("latest") ? MiraiMCPlugin.getPlatform().getLibraryLoader().getLibraryVersion(mavenCentral, "net.mamoe", "mirai-core-all") : version)), null));
10869
}
10970
}

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/internal/database/SQLite.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.zaxxer.hikari.HikariConfig;
44
import com.zaxxer.hikari.HikariDataSource;
55
import me.dreamvoid.miraimc.MiraiMCConfig;
6+
import me.dreamvoid.miraimc.MiraiMCPlugin;
67
import me.dreamvoid.miraimc.internal.Utils;
7-
import me.dreamvoid.miraimc.internal.loader.LibraryLoader;
8+
import org.eclipse.aether.artifact.DefaultArtifact;
9+
import org.eclipse.aether.graph.Dependency;
10+
import org.eclipse.aether.repository.RemoteRepository;
811

912
import java.io.File;
10-
import java.io.IOException;
1113
import java.sql.Connection;
1214
import java.sql.SQLException;
1315

@@ -36,13 +38,9 @@ public void initialize() throws ClassNotFoundException {
3638
if(Utils.findClass("org.sqlite.JDBC")){
3739
driver = "org.sqlite.JDBC";
3840
} else {
39-
try {
40-
LibraryLoader.loadJarMaven("org.xerial", "sqlite-jdbc", "3.36.0.3");
41-
initialize();
42-
return;
43-
} catch (IOException e) {
44-
throw new ClassNotFoundException("Local library not found and maven download failed: " + e);
45-
}
41+
MiraiMCPlugin.getPlatform().getLibraryLoader().loadLibraryMaven(new RemoteRepository.Builder("central","default", MiraiMCConfig.General.MavenRepoUrl).build(), new Dependency(new DefaultArtifact("org.xerial:sqlite-jdbc:3.36.0.3"), null));
42+
initialize();
43+
return;
4644
}
4745

4846
HikariConfig config = new HikariConfig();

0 commit comments

Comments
 (0)