Skip to content

Commit 5ce059f

Browse files
committed
从 JarClient 中分离出, 作为 JarClient 的 Loader 实现.
1 parent 0c3a1e7 commit 5ce059f

45 files changed

Lines changed: 128 additions & 3301 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11

22
plugins {
33
id 'net.minecraftforge.gradle' version '5.1.+'
4-
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
54
}
65

76

87
group = 'github'
9-
version = '1.0.6-Stable_4.1.17'
8+
version = '1.1.0-Stable'
109

1110
java {
12-
archivesBaseName = '!BalloonUpdateModClient-1.12.2'
11+
archivesBaseName = '!BalloonUpdateModLoader-1.12.2'
1312
toolchain.languageVersion = JavaLanguageVersion.of(8)
1413
}
1514

@@ -94,11 +93,11 @@ dependencies {
9493
// that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied.
9594
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
9695
minecraft 'net.minecraftforge:forge:1.12.2-14.23.5.2860'
97-
implementation ("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
98-
implementation ('org.yaml:snakeyaml:1.32')
99-
implementation ("com.squareup.okhttp3:okhttp:4.10.0")
100-
implementation ("org.json:json:20220320")
101-
implementation ("com.hrakaroo:glob:0.9.0")
96+
// implementation ("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
97+
// implementation ('org.yaml:snakeyaml:1.32')
98+
// implementation ("com.squareup.okhttp3:okhttp:4.10.0")
99+
// implementation ("org.json:json:20220320")
100+
// implementation ("com.hrakaroo:glob:0.9.0")
102101
implementation ("com.formdev:flatlaf:2.4")
103102
implementation ("com.formdev:flatlaf-intellij-themes:2.4")
104103

@@ -119,8 +118,7 @@ dependencies {
119118
jar {
120119
manifest {
121120
attributes([
122-
"Version" : "4.1.17_ModVer",
123-
"ModVerion" : "1.0.6-Stable",
121+
"ModVerion" : "1.1.0-Stable",
124122
"FMLCorePlugin" : "com.github.kasuminova.BalloonUpdate",
125123
"FMLCorePluginContainsFMLMod" : false
126124
])
@@ -132,13 +130,3 @@ jar.finalizedBy('reobfJar')
132130
tasks.withType(JavaCompile).configureEach {
133131
options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
134132
}
135-
compileKotlin {
136-
kotlinOptions {
137-
jvmTarget = "1.8"
138-
}
139-
}
140-
compileTestKotlin {
141-
kotlinOptions {
142-
jvmTarget = "1.8"
143-
}
144-
}

src/main/java/com/github/kasuminova/BalloonUpdate.java

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,130 @@
44
import org.apache.logging.log4j.LogManager;
55
import org.apache.logging.log4j.Logger;
66

7+
import java.awt.*;
8+
import java.io.*;
9+
import java.lang.reflect.Method;
10+
import java.net.URL;
11+
import java.net.URLClassLoader;
12+
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
714
import java.util.Map;
815

916
@IFMLLoadingPlugin.MCVersion("1.12.2")
1017
@IFMLLoadingPlugin.Name("BalloonUpdatePlugin")
1118
@IFMLLoadingPlugin.TransformerExclusions("github.kasuminova")
1219
public class BalloonUpdate implements IFMLLoadingPlugin {
13-
public static String mcLocation = null;
14-
static Logger logger = LogManager.getLogger("BalloonUpdateMod");
20+
// static 代码块, 超前调用外部 JAR 包
21+
static {
22+
Logger logger = LogManager.getLogger("BalloonUpdateModLoader");
23+
File directory = new File(".");
24+
File[] files = directory.listFiles();
25+
assert files != null;
26+
27+
String path = null;
28+
29+
//根据自定义文件名 BalloonUpdateFileName 内的文件名搜索文件, 如果文件不存在则不做任何修改
30+
File fileNameTXT = new File("./BalloonUpdateFileName.txt");
31+
if (fileNameTXT.exists() && fileNameTXT.isFile()) {
32+
try {
33+
Reader reader = new InputStreamReader(Files.newInputStream(fileNameTXT.toPath()), StandardCharsets.UTF_8);
34+
StringBuilder sb = new StringBuilder();
35+
int ch;
36+
while ((ch = reader.read()) != -1) {
37+
sb.append((char) ch);
38+
}
39+
reader.close();
40+
41+
File target = new File("./" + sb);
42+
if (target.exists() && target.isFile()) {
43+
path = target.getAbsoluteFile().toURI().toString();
44+
}
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
50+
//根据自定义文件名 OldBalloonUpdateFileName 内的文件名搜索文件, 如果存在则删除指定文件
51+
File oldFileNamesTXT = new File("./OldBalloonUpdateFileName.txt");
52+
if (oldFileNamesTXT.exists() && oldFileNamesTXT.isFile()) {
53+
try {
54+
Reader reader = new InputStreamReader(Files.newInputStream(oldFileNamesTXT.toPath()), StandardCharsets.UTF_8);
55+
StringBuilder sb = new StringBuilder();
56+
int ch;
57+
while ((ch = reader.read()) != -1) {
58+
sb.append((char) ch);
59+
}
60+
reader.close();
61+
62+
//读取旧文件名 txt 文件后, 将每行分割成多个文件名然后循环搜索
63+
String[] oldFileNames = sb.toString().split("\n");
64+
for (String oldFileName : oldFileNames) {
65+
File target = new File("./" + oldFileName);
66+
if (target.exists() && target.isFile()) {
67+
if (target.delete()) {
68+
logger.info("Successfully Delete Old JarClient File: " + target.getName());
69+
} else {
70+
logger.warn("Failed Delete Old JarClient File: " + target.getName());
71+
}
72+
}
73+
}
74+
} catch (Exception e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
//如果自定义 JarClient 文件未找到, 则搜索当前文件夹所有文件, 寻找对应的 JarClient
80+
if (path == null) {
81+
for (File target : files) {
82+
if (target.isFile() && target.getName().contains("JarClient") && target.getName().endsWith(".jar")) {
83+
path = target.getAbsoluteFile().toURI().toString();
84+
break;
85+
}
86+
}
87+
}
88+
89+
if (path == null) {
90+
logger.error("Cannot Find JarClient JAR File!");
91+
} else {
92+
//开始载入外部 Jar
93+
URLClassLoader urlClassLoader = null;
94+
Class<?> BalloonUpdateMain;
95+
Class<?> SetupSwing;
96+
try {
97+
logger.info("Loading JAR: " + path);
98+
//通过URLClassLoader加载外部jar
99+
urlClassLoader = new URLClassLoader(new URL[]{new URL(path)});
100+
101+
//获取外部jar里面的具体类对象
102+
logger.info("Loading Class: " + "com.github.balloonupdate.BalloonUpdateMain");
103+
104+
BalloonUpdateMain = urlClassLoader.loadClass("com.github.balloonupdate.BalloonUpdateMain");
105+
106+
if (Desktop.isDesktopSupported()) {
107+
SetupSwing = urlClassLoader.loadClass("com.github.kasuminova.GUI.SetupSwing");
108+
Method init = SetupSwing.getMethod("init");
109+
init.invoke(null);
110+
}
111+
112+
Method main = BalloonUpdateMain.getMethod("main", String[].class);
113+
114+
logger.info("Method Load Successfully, invoke Method main()...");
115+
116+
main.invoke(null, (Object) new String[0]);
117+
} catch (Exception e) {
118+
e.printStackTrace();
119+
} finally {
120+
//卸载关闭外部 JAR
121+
try {
122+
if (urlClassLoader != null) urlClassLoader.close();
123+
System.out.println("Finished!");
124+
} catch (IOException e) {
125+
System.out.println("Close JAR Failed:" + e.getMessage());
126+
}
127+
}
128+
}
129+
}
130+
15131
@Override
16132
public String[] getASMTransformerClass() {
17133
return new String[0];
@@ -24,12 +140,12 @@ public String getModContainerClass() {
24140

25141
@Override
26142
public String getSetupClass() {
27-
return "com.github.kasuminova.StartUpdate";
143+
return null;
28144
}
29145

30146
@Override
31147
public void injectData(Map<String, Object> data) {
32-
mcLocation = data.get("mcLocation").toString();
148+
33149
}
34150

35151
@Override

src/main/java/com/github/kasuminova/GUI/SetupSwing.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)