diff --git a/Wrapper/src/main/java/com/github/wohaopa/zeropoint/wrapper/Main.java b/Wrapper/src/main/java/com/github/wohaopa/zeropoint/wrapper/Main.java index 9abd2b7..d340a6f 100644 --- a/Wrapper/src/main/java/com/github/wohaopa/zeropoint/wrapper/Main.java +++ b/Wrapper/src/main/java/com/github/wohaopa/zeropoint/wrapper/Main.java @@ -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 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"); + } -} \ No newline at end of file + + + 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(); + } + + +} diff --git a/build.gradle b/build.gradle index 2cc1032..45f1a75 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'com.github.wohaopa' -version '1.0-SNAPSHOT' +version 'Core' repositories { mavenCentral() @@ -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 ) } } @@ -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) { diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java b/src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java index c55f5a2..931987b 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java @@ -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 { @@ -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; } /* diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Instance.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Instance.java index e300ff7..48ae454 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Instance.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Instance.java @@ -87,7 +87,7 @@ 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); @@ -95,7 +95,7 @@ public void genRuntimeDir() { } 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); // 本实例的共享文件 diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/InstanceInstaller.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/InstanceInstaller.java index d04674f..820d954 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/InstanceInstaller.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/InstanceInstaller.java @@ -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); // 加载文件校验 diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Mapper.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Mapper.java index 436a37a..badb6f4 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Mapper.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Mapper.java @@ -34,19 +34,19 @@ public class Mapper { private final Map> exclude = new HashMap<>(); private final Map> include = new HashMap<>(); - // private List all_include = new ArrayList<>(); + private final List all_exclude; private final File targetDir; - private final File config; + private final File mainConfig; public Mapper(File config, File targetDir) { List 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__"); } @@ -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__"; @@ -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 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); } } @@ -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; } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Sharer.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Sharer.java index 7f0844b..9a8a885 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Sharer.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Sharer.java @@ -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) { @@ -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; diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java b/src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java index 9cb0b6b..693f47c 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java @@ -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 { @@ -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()) { @@ -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; } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/main/Main.java b/src/main/java/com/github/wohaopa/zeropointlanuch/main/Main.java index 7e2f409..21453f7 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/main/Main.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/main/Main.java @@ -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"; }