Skip to content

Commit

Permalink
从 JarClient 中分离出, 作为 JarClient 的 Loader 实现.
Browse files Browse the repository at this point in the history
  • Loading branch information
KasumiNova committed Oct 2, 2022
1 parent 0c3a1e7 commit 5ce059f
Show file tree
Hide file tree
Showing 45 changed files with 128 additions and 3,301 deletions.
28 changes: 8 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

plugins {
id 'net.minecraftforge.gradle' version '5.1.+'
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
}


group = 'github'
version = '1.0.6-Stable_4.1.17'
version = '1.1.0-Stable'

java {
archivesBaseName = '!BalloonUpdateModClient-1.12.2'
archivesBaseName = '!BalloonUpdateModLoader-1.12.2'
toolchain.languageVersion = JavaLanguageVersion.of(8)
}

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

Expand All @@ -119,8 +118,7 @@ dependencies {
jar {
manifest {
attributes([
"Version" : "4.1.17_ModVer",
"ModVerion" : "1.0.6-Stable",
"ModVerion" : "1.1.0-Stable",
"FMLCorePlugin" : "com.github.kasuminova.BalloonUpdate",
"FMLCorePluginContainsFMLMod" : false
])
Expand All @@ -132,13 +130,3 @@ jar.finalizedBy('reobfJar')
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
124 changes: 120 additions & 4 deletions src/main/java/com/github/kasuminova/BalloonUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,130 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Map;

@IFMLLoadingPlugin.MCVersion("1.12.2")
@IFMLLoadingPlugin.Name("BalloonUpdatePlugin")
@IFMLLoadingPlugin.TransformerExclusions("github.kasuminova")
public class BalloonUpdate implements IFMLLoadingPlugin {
public static String mcLocation = null;
static Logger logger = LogManager.getLogger("BalloonUpdateMod");
// static 代码块, 超前调用外部 JAR 包
static {
Logger logger = LogManager.getLogger("BalloonUpdateModLoader");
File directory = new File(".");
File[] files = directory.listFiles();
assert files != null;

String path = null;

//根据自定义文件名 BalloonUpdateFileName 内的文件名搜索文件, 如果文件不存在则不做任何修改
File fileNameTXT = new File("./BalloonUpdateFileName.txt");
if (fileNameTXT.exists() && fileNameTXT.isFile()) {
try {
Reader reader = new InputStreamReader(Files.newInputStream(fileNameTXT.toPath()), StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
reader.close();

File target = new File("./" + sb);
if (target.exists() && target.isFile()) {
path = target.getAbsoluteFile().toURI().toString();
}
} catch (Exception e) {
e.printStackTrace();
}
}

//根据自定义文件名 OldBalloonUpdateFileName 内的文件名搜索文件, 如果存在则删除指定文件
File oldFileNamesTXT = new File("./OldBalloonUpdateFileName.txt");
if (oldFileNamesTXT.exists() && oldFileNamesTXT.isFile()) {
try {
Reader reader = new InputStreamReader(Files.newInputStream(oldFileNamesTXT.toPath()), StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
reader.close();

//读取旧文件名 txt 文件后, 将每行分割成多个文件名然后循环搜索
String[] oldFileNames = sb.toString().split("\n");
for (String oldFileName : oldFileNames) {
File target = new File("./" + oldFileName);
if (target.exists() && target.isFile()) {
if (target.delete()) {
logger.info("Successfully Delete Old JarClient File: " + target.getName());
} else {
logger.warn("Failed Delete Old JarClient File: " + target.getName());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

//如果自定义 JarClient 文件未找到, 则搜索当前文件夹所有文件, 寻找对应的 JarClient
if (path == null) {
for (File target : files) {
if (target.isFile() && target.getName().contains("JarClient") && target.getName().endsWith(".jar")) {
path = target.getAbsoluteFile().toURI().toString();
break;
}
}
}

if (path == null) {
logger.error("Cannot Find JarClient JAR File!");
} else {
//开始载入外部 Jar
URLClassLoader urlClassLoader = null;
Class<?> BalloonUpdateMain;
Class<?> SetupSwing;
try {
logger.info("Loading JAR: " + path);
//通过URLClassLoader加载外部jar
urlClassLoader = new URLClassLoader(new URL[]{new URL(path)});

//获取外部jar里面的具体类对象
logger.info("Loading Class: " + "com.github.balloonupdate.BalloonUpdateMain");

BalloonUpdateMain = urlClassLoader.loadClass("com.github.balloonupdate.BalloonUpdateMain");

if (Desktop.isDesktopSupported()) {
SetupSwing = urlClassLoader.loadClass("com.github.kasuminova.GUI.SetupSwing");
Method init = SetupSwing.getMethod("init");
init.invoke(null);
}

Method main = BalloonUpdateMain.getMethod("main", String[].class);

logger.info("Method Load Successfully, invoke Method main()...");

main.invoke(null, (Object) new String[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
//卸载关闭外部 JAR
try {
if (urlClassLoader != null) urlClassLoader.close();
System.out.println("Finished!");
} catch (IOException e) {
System.out.println("Close JAR Failed:" + e.getMessage());
}
}
}
}

@Override
public String[] getASMTransformerClass() {
return new String[0];
Expand All @@ -24,12 +140,12 @@ public String getModContainerClass() {

@Override
public String getSetupClass() {
return "com.github.kasuminova.StartUpdate";
return null;
}

@Override
public void injectData(Map<String, Object> data) {
mcLocation = data.get("mcLocation").toString();

}

@Override
Expand Down
65 changes: 0 additions & 65 deletions src/main/java/com/github/kasuminova/GUI/SetupSwing.java

This file was deleted.

Loading

0 comments on commit 5ce059f

Please sign in to comment.