Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Share模块目录共享机制
Browse files Browse the repository at this point in the history
  • Loading branch information
wohaopa committed May 5, 2023
1 parent a0c52a8 commit 05038ca
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 37 deletions.
111 changes: 99 additions & 12 deletions Wrapper/src/main/java/com/github/wohaopa/zeropoint/wrapper/Main.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,115 @@
package com.github.wohaopa.zeropoint.wrapper;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static String versionUrl = "http://127.0.0.1/version";
public static String zpLaunch = "http://127.0.0.1/ZeroPointLaunch/ZeroPointLaunch-Core.jar";
private static String zpLaunch = "http://127.0.0.1/ZeroPointLaunch/Library/";
private static File rootDir;

public static List<String> lib = new ArrayList<>();

static {
// 懒得找了,写死得了
lib.add("hutool-all-5.8.17.jar");
lib.add("log4j-core-2.20.0.jar");
lib.add("log4j-api-2.20.0.jar");
lib.add("commons-compress-1.21.jar");
lib.add("xz-1.9.jar");
lib.add("junit-platform-engine-1.9.2.jar");
lib.add("junit-platform-commons-1.9.2.jar");
lib.add("junit-vintage-engine-5.9.2.jar");
lib.add("junit-4.13.2.jar");
lib.add("opentest4j-1.2.0.jar");
lib.add("hamcrest-core-1.3.jar");
lib.add("ZeroPointLaunch-Core.jar");

String userDir = System.getProperty("user.dit");
rootDir = new File(userDir, "lib");
}

public static void main(String[] args) {

lib.forEach(s -> {
File libFile = new File(rootDir, s);
if (!libFile.exists()) {
System.out.println("missing...: " + s);
try {
downloadFile(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});


try {
Class<?> clazz = Class.forName("com.github.wohaopa.zeropointlanuch.main.Main");
Method method = clazz.getMethod("main", String[].class);
method.setAccessible(true);
method.invoke(null, (Object) args);
} catch (ClassNotFoundException e) {
System.out.println("无法加载核心类:com.github.wohaopa.zeropointlanuch.main.Main,可能缺少ZeroPointLaunch-Core.jar");
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
System.out.println("无法加载main方法,请重新下载!");
throw new RuntimeException(e);
} catch (InvocationTargetException | IllegalAccessException e) {
System.out.println("main方法执行错误");
throw new RuntimeException(e);
}
}

private static boolean camp(String version1, String version2) {
String[] a = version1.split("\\.");
String[] b = version2.split("\\.");
for (int i = 0; i < 3; i++)
if (Integer.getInteger(a[i]) < Integer.getInteger(b[i]))
return true;
return false;

private static void downloadFile(String s) throws IOException {


URL url = new URL(zpLaunch + s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);

//文件保存位置
rootDir.mkdirs();

File file = new File(rootDir + File.separator + s);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);

if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}

System.out.println("info:" + url + " download success");

}
}


private static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}


}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.github.wohaopa'
version '1.0-SNAPSHOT'
version 'Core'

repositories {
mavenCentral()
Expand Down Expand Up @@ -36,14 +36,14 @@ test {

def allJars = (configurations.runtimeClasspath)
.findAll{it.isFile()}
.collect { 'lib/' + it.name }
.collect { it.name }
.join(' ')

jar {
manifest {
attributes(
'Main-Class': 'com.github.wohaopa.zeropointlanuch.main.Main',
'Class-Path': allJars + " ."
'Class-Path': allJars
)
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ spotless {
tasks.register('copyLib', Copy) {
dependsOn 'copyCmd'
from configurations.compileClasspath
into "$buildDir/libs/lib"
into "$buildDir/libs"
}

tasks.register('copyCmd', Copy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import java.util.Objects;
import java.util.concurrent.ExecutionException;

import com.github.wohaopa.zeropointlanuch.core.DirTools;
import com.github.wohaopa.zeropointlanuch.core.Instance;
import com.github.wohaopa.zeropointlanuch.core.InstanceInstaller;
import com.github.wohaopa.zeropointlanuch.core.Log;
import com.github.wohaopa.zeropointlanuch.core.*;
import com.github.wohaopa.zeropointlanuch.core.utils.DownloadUtil;

public class Core {
Expand Down Expand Up @@ -115,8 +112,8 @@ public static void downloadFile(String url) {
}
}

public static String genRuntimeDir(Instance inst) {
inst.genRuntimeDir();
public static String genRuntimeDir(Instance inst, Sharer sharer) {
inst.genRuntimeDir(sharer);
return inst.information.runDir;
}
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public void savaInformation() {
}

/** 生成实例的运行目录(.minecraft) 可被其他启动器直接启动 */
public void genRuntimeDir() {
public void genRuntimeDir(Sharer sharer) {
// 删除目录中的系统链接文件
for (File file : Objects.requireNonNull(this.runDir.listFiles())) {
if (file.isDirectory()) for (File file1 : Objects.requireNonNull(file.listFiles())) FileUtil.delLink(file1);
else FileUtil.delLink(file);
}
Mapper mapper = new Mapper(new File(this.imageDir, "zpl_margi_config.json"), this.runDir);

Sharer.execute(this.information.sharer, mapper); // 优先共享文件
Sharer.execute(sharer == null ? this.information.sharer : sharer.name, mapper); // 优先共享文件

this.copyFileAsLink(mapper, this.information.excludeMods); // 本实例的共享文件

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private static Instance install(File dir, String name, String version, String de
inst.information.insDir = inst.insDir.toString();
inst.information.imageDir = inst.imageDir.toString();
inst.information.runDir = inst.runDir.toString();
inst.information.sharer = "Common"; // 使用默认共享器

Log.LOGGER.debug("[实例安装]正在生成校验文件");
inst.information.checksum = FileUtil.genChecksum(inst.imageDir); // 加载文件校验
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/com/github/wohaopa/zeropointlanuch/core/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public class Mapper {

private final Map<String, List<String>> exclude = new HashMap<>();
private final Map<String, List<String>> include = new HashMap<>();
// private List<String> all_include = new ArrayList<>();

private final List<String> all_exclude;

private final File targetDir;
private final File config;
private final File mainConfig;

public Mapper(File config, File targetDir) {
List<String> all = new ArrayList<>();
all.add("zpl_margi_config.json");
exclude.put("__zpl_all__", all);
this.targetDir = targetDir;
this.config = config;
this.analyticConfig();
this.mainConfig = config;
this.analyticConfig(config);
// this.all_include = include.getOrDefault("__zpl_all__", new ArrayList<>());
this.all_exclude = exclude.get("__zpl_all__");
}
Expand All @@ -56,8 +56,8 @@ public File getTargetDir() {
}

/** 用于解析json文件 */
private void analyticConfig() {
JSONObject json = (JSONObject) JsonUtil.fromJson(config);
public void analyticConfig(File configFile) {
JSONObject json = (JSONObject) JsonUtil.fromJson(configFile);
List<_Config> exclude1 = json.getBeanList("exclude", _Config.class);
exclude1.forEach(config -> {
if (config.name == null) config.name = "__zpl_all__";
Expand All @@ -73,23 +73,28 @@ private void analyticConfig() {
});
}

public void execute(String name, File dir) {

File file = new File(dir, "zpl_margi_config.json");
List list = ((JSONObject) JsonUtil.fromJson(file)).get("shareDir", List.class);
execute0(name, dir, list);
}

/**
* 执行映射用
*
* @param name 提供image的实例名
* @param dir image目录
*/
public void execute(String name, File dir) {
private void execute0(String name, File dir, List<String> shareDir) {
for (File file : Objects.requireNonNull(dir.listFiles())) {
if (file.isDirectory()) {
if (file.isDirectory() && !shareDir.contains(file.getName())) {
for (File file1 : Objects.requireNonNull(file.listFiles())) {
File tmp = new File(targetDir, file.getName() + "/" + file1.getName());
// doLink("__zpl_all__", tmp, file1);
doLink(name, tmp, file1);
}
} else {
File tmp = new File(targetDir, file.getName());;
// doLink("__zpl_all__", tmp, file);
doLink(name, tmp, file);
}
}
Expand All @@ -99,6 +104,7 @@ public static JSONObject defaultJson() {
JSONObject object = new JSONObject();
object.putOpt("include", new JSONArray());
object.putOpt("exclude", new JSONArray());
object.putOpt("shareDir", new ArrayList<>());
return object;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Sharer {
inst.put("Common", new Sharer("Common", FileUtil.initAndMkDir(DirTools.shareDir, "Common"), null));
inst.put("Java8", new Sharer("Java8", FileUtil.initAndMkDir(DirTools.shareDir, "Java8"), "Common"));
inst.put("Java17", new Sharer("Java17", FileUtil.initAndMkDir(DirTools.shareDir, "Java17"), "Common"));
inst.put("HMCL", new Sharer("HMCL", FileUtil.initAndMkDir(DirTools.shareDir, "HMCL"), "Common"));
}

public static Sharer get(String name) {
Expand All @@ -44,6 +45,7 @@ public static Sharer get(String name) {
public String name;
public String parent;


private Sharer(String name, File rootDir, String parent) {
this.rootDir = rootDir;
this.name = name;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github.wohaopa.zeropointlanuch.api.Core;
import com.github.wohaopa.zeropointlanuch.core.DirTools;
import com.github.wohaopa.zeropointlanuch.core.Instance;
import com.github.wohaopa.zeropointlanuch.core.Sharer;

@SuppressWarnings("unused") // 内部所有字段都会被反射注册
public class Command {
Expand Down Expand Up @@ -124,7 +125,7 @@ public boolean execute(String[] args) throws IOException {
System.out.println("错误:目录重复:" + instDir.getPath() + "\n");
return false;
}
String zip = args[2];
String zip = args[2].replace("\"", "");
String ver = args[3];
File zipFile = new File(zip);
if (!zipFile.exists()) {
Expand Down Expand Up @@ -195,7 +196,15 @@ public boolean execute(String[] args) {
System.out.println("错误:已安装的实例中不包含:\"" + args[1] + "\" 可以使用list指令查看已安装实例");
return false;
}
System.out.println("dir=" + Core.genRuntimeDir(inst));
Sharer sharer = null;
if (args.length == 3) {
sharer = Sharer.get(args[2]);
if (sharer == null) {
System.out.println("错误:共享器不存在:" + args[2] + " 可以使用默认共享器:Java8、Java17、Common");
return false;
}
}
System.out.println("dir=" + Core.genRuntimeDir(inst, sharer));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static void main(String[] args) {

private static void init() {

String rootDirStr = System.getProperty("zpl.rootdir");
// String rootDirStr = System.getProperty("zpl.rootdir");
String rootDirStr = "D:\\DevProject\\JavaProject\\ZeroPointLaunch\\Wrapper\\build\\libs\\.GTNH";
if (rootDirStr == null) {
rootDirStr = System.getProperty("user.dir") + "/.GTNH";
}
Expand Down

0 comments on commit 05038ca

Please sign in to comment.