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 d340a6f..4e80fa5 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 @@ -7,12 +7,15 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Properties; public class Main { - private static String zpLaunch = "http://127.0.0.1/ZeroPointLaunch/Library/"; - private static File rootDir; + private static String zpLaunch; + private static final File libDir; - public static List lib = new ArrayList<>(); + private static final List lib = new ArrayList<>(); + + private static final Properties config = new Properties(); static { // 懒得找了,写死得了 @@ -29,23 +32,56 @@ public class Main { lib.add("hamcrest-core-1.3.jar"); lib.add("ZeroPointLaunch-Core.jar"); - String userDir = System.getProperty("user.dit"); - rootDir = new File(userDir, "lib"); + String userDir = System.getProperty("user.dir"); + libDir = new File(userDir, "lib"); + File configFile = new File(userDir + "/config.properties"); + if (!configFile.exists()) { + config.setProperty("download-url", "http://127.0.0.1/ZeroPointLaunch/Library/"); + config.setProperty("check-libraries", "true"); + config.setProperty("check-update", "true"); + try { + config.store(new FileOutputStream(configFile), null); + } catch (IOException e) { + throw new RuntimeException(e); + } + } else { + try { + config.load(new FileInputStream(configFile)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } 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); - } + zpLaunch=config.getProperty("download-url","http://127.0.0.1/ZeroPointLaunch/Library/"); + + if (config.getProperty("check-update", "false").equals("true")) { + File newCore = new File(libDir, "ZeroPointLaunch-Core-new.jar"); + if (newCore.exists()) { + File curCore = new File(libDir, "ZeroPointLaunch-Core.jar"); + File oldCore = new File(libDir, "ZeroPointLaunch-Core-old.jar"); + + if (oldCore.exists()) oldCore.delete(); + if (curCore.exists()) curCore.renameTo(oldCore); + newCore.renameTo(curCore); } - }); + } + + if (config.getProperty("check-libraries", "false").equals("true")) { + lib.forEach(s -> { + File libFile = new File(libDir, s); + if (!libFile.exists()) { + System.out.println("missing...: " + s); + try { + downloadFile(s); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }); + } try { @@ -82,9 +118,9 @@ private static void downloadFile(String s) throws IOException { byte[] getData = readInputStream(inputStream); //文件保存位置 - rootDir.mkdirs(); + libDir.mkdirs(); - File file = new File(rootDir + File.separator + s); + File file = new File(libDir + File.separator + s); FileOutputStream fos = new FileOutputStream(file); fos.write(getData); 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 931987b..8e2579c 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java @@ -28,6 +28,7 @@ import com.github.wohaopa.zeropointlanuch.core.*; import com.github.wohaopa.zeropointlanuch.core.utils.DownloadUtil; +import com.github.wohaopa.zeropointlanuch.core.utils.FileUtil; public class Core { @@ -53,19 +54,19 @@ public static void installStandard(File zipFile, File dir, String name, String v /** 检查实例文件夹 */ public static void lookup() { - Log.LOGGER.debug("[文件初始化]实例搜寻:开始"); + Log.start("文件初始化"); Instance.clear(); for (File file : Objects.requireNonNull(DirTools.instancesDir.listFiles())) { if (file.isDirectory()) { File version = new File(file, "version.json"); if (version.exists()) { - Log.LOGGER.debug("[文件初始化]实例搜寻:发现实例" + file.getName()); + Log.debug("发现实例" + file.getName()); InstanceInstaller.addInst(version); } } } - Log.LOGGER.debug("[文件初始化]实例搜寻:结束"); - Log.LOGGER.debug("[文件初始化]安装包搜寻:开始"); + Log.end(); + Log.start("文件初始化"); for (File file : Objects.requireNonNull(DirTools.zipDir.listFiles())) { if (file.isFile() && file.getName() .endsWith(".zip")) { @@ -73,12 +74,12 @@ public static void lookup() { name = name.replace("GT_New_Horizons_", ""); name = name.replace("_Client.zip", ""); if (!Instance.containsKey(name)) { - Log.LOGGER.debug("[文件初始化]实例搜寻:发现安装包 {},名为 {}", file.getName(), name); + Log.debug("发现安装包 {},名为 {}", file.getName(), name); InstanceInstaller.installStandard(file, new File(DirTools.instancesDir, name), name, name); } } } - Log.LOGGER.debug("[文件初始化]安装包搜寻:结束"); + Log.end(); } /** @@ -108,7 +109,7 @@ public static void downloadFile(String url) { try { DownloadUtil.takeDownloadResult(); } catch (ExecutionException | InterruptedException e) { - Log.LOGGER.error("文件下载失败:{}", url); + Log.error("文件下载失败:{}", url); } } @@ -116,14 +117,38 @@ public static String genRuntimeDir(Instance inst, Sharer sharer) { inst.genRuntimeDir(sharer); return inst.information.runDir; } - /* - * public static String genHMCLDir(String name) { - * Instance inst = Instance.get(name); - * inst.genRuntimeDir(); - * FileUtil.genLink(new File(inst.runDir, "assets"), DirTools.assetsDir); - * FileUtil.genLink(new File(inst.runDir, "libraries"), DirTools.librariesDir); - * FileUtil.genLink(new File(inst.runDir, "versions"), DirTools.versionsDir); - * return inst.information.runDir; - * } - */ + + public static void genTransferOld(Instance instance, File file) { + + List exclude = new ArrayList<>(); + for (File file1 : Objects.requireNonNull(file.listFiles())) { + if (file1.getName() + .startsWith(".")) exclude.add(file1.getPath()); + } + exclude.add(file.getPath() + "\\assets"); + exclude.add(file.getPath() + "\\libraries"); + exclude.add(file.getPath() + "\\logs"); + exclude.add(file.getPath() + "\\versions"); + + long a = System.currentTimeMillis(); + Identification identification = new Identification(instance.information.checksum); + Differ differ = Differ.diff(file, identification, exclude); + long b = System.currentTimeMillis(); + Log.debug("对比用时:{}s", (b - a) / 1000.0); + + File outDir = new File(instance.insDir, "private-outDir"); + if (outDir.exists()) { + File oldOutDir = new File(instance.insDir, "private-outDir-old"); + FileUtil.delete(oldOutDir); + if (!outDir.renameTo(oldOutDir)) FileUtil.delete(outDir); + } else { + outDir.mkdir(); + } + differ.addition.forEach(s -> { + File f1 = new File(file, s); + File f2 = new File(outDir, s).getParentFile(); + if (!f2.exists()) f2.mkdirs(); + FileUtil.copyDir(f1, f2); + }); + } } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Differ.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Differ.java index 50ebdf5..81d7bbe 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Differ.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Differ.java @@ -21,35 +21,54 @@ package com.github.wohaopa.zeropointlanuch.core; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.List; import java.util.zip.Checksum; import com.github.wohaopa.zeropointlanuch.core.utils.FileUtil; public class Differ { - public static Differ diff(File dir, Identification identification) { + public void setExclude(List exclude) { + this.exclude = exclude; + } + + public static Differ diff(File dir, Identification identification, List exclude) { - Differ differ = new Differ(dir.getPath() + "\\"); + Differ differ = new Differ(dir.getPath() + "\\", exclude); differ.search(dir, identification.item); return differ; } public final List removed = new ArrayList<>(); public final List addition = new ArrayList<>(); - private String rootStr; + private final String rootStr; + private List exclude; - public Differ(String rootStr) { + public Differ(String rootStr, List exclude) { this.rootStr = rootStr; + this.exclude = exclude; } - public void search(File dir, Identification.IdentificationDirectoryItem directoryItem) { + private void search(File dir, Identification.IdentificationDirectoryItem directoryItem) { + File[] rootDir = dir.listFiles(); if (rootDir == null || rootDir.length < 1) return; List loaded = new ArrayList<>(); for (File file : rootDir) { String fileName = file.getName(); - if (fileName.equals("Quests")) continue; // 跳过任务书检测 + + if (exclude != null) { + boolean flag = false; + for (String s : exclude) if (file.getPath() + .endsWith(s)) { + flag = true; + exclude.remove(s); + break; + } + if (flag) continue; + } + if (directoryItem.contains(fileName)) { // 镜像内存在相应名字的文件或者目录 loaded.add(fileName); Identification.IdentificationItem identificationItem = directoryItem.getFile(fileName); diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/DirTools.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/DirTools.java index 4991550..3d7d0a3 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/DirTools.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/DirTools.java @@ -39,7 +39,7 @@ public class DirTools { public static File versionsDir; public static void init(File workDir) { - Log.LOGGER.info("目录管理初始化开始"); + Log.start("目录管理"); DirTools.workDir = workDir; DirTools.instancesDir = FileUtil.initAndMkDir(workDir, "instances"); DirTools.librariesDir = FileUtil.initAndMkDir(workDir, "libraries"); @@ -49,6 +49,6 @@ public static void init(File workDir) { DirTools.shareDir = FileUtil.initAndMkDir(workDir, "share"); DirTools.tmpDir = FileUtil.initAndMkDir(workDir, "tmpDir"); DirTools.versionsDir = FileUtil.initAndMkDir(workDir, "versions"); - Log.LOGGER.info("目录管理初始化结束"); + Log.end(); } } 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 48ae454..97b205c 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Instance.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Instance.java @@ -76,13 +76,13 @@ private Instance() {} /** 加载实例信息,仅在初始化阶段使用本方法来加载持久化的实例 */ public Information loadInformation() { - Log.LOGGER.debug("[实例加载]正在加载:{}", versionFile); + Log.debug("正在加载:{}", versionFile); return JsonUtil.fromJson(FileUtil.fileRead(versionFile), Information.class); } /** 保存实例信息 */ public void savaInformation() { - Log.LOGGER.debug("[实例安装]正在保存:{}", versionFile); + Log.debug("正在保存:{}", versionFile); FileUtil.fileWrite(versionFile, JsonUtil.toJson(information)); } @@ -108,7 +108,7 @@ public void genRuntimeDir(Sharer sharer) { depVersion = instance.information.depVersion; } else { - Log.LOGGER.error("未知版本:" + depVersion); + Log.error("未知版本:" + depVersion); throw new RuntimeException("未知版本:" + depVersion); } } @@ -127,7 +127,7 @@ private void copyFileAsLink(Mapper mapper, List excludeModsList) { if (excludeModsList.contains(mods)) continue; // 排除 File modsFile = new File(modsDir, mods); if (!modsFile.exists()) { - Log.LOGGER.fatal("缺失mod:{}", modsFile); + Log.error("缺失mod:{}", modsFile); throw new RuntimeException("缺失mod:" + modsFile); } FileUtil.genLink(new File(modsRun, modsFile.getName()), modsFile); 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 820d954..c2eb372 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/InstanceInstaller.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/InstanceInstaller.java @@ -64,7 +64,7 @@ public static void addInst(File versionFile) { throw new RuntimeException("版本加载错误:" + e); } if (!inst.information.insDir.equals(versionFile.getParent())) { - Log.LOGGER.info("实例文件被移动:{}", inst.information.insDir); + Log.info("实例文件被移动:{}", inst.information.insDir); inst.information.insDir = versionFile.getParent(); inst.information.imageDir = inst.information.insDir + "\\image"; inst.information.runDir = inst.information.insDir + "\\.minecraft"; @@ -108,7 +108,7 @@ private static Instance install(File dir, String name, String version, String de inst.information.runDir = inst.runDir.toString(); inst.information.sharer = "Common"; // 使用默认共享器 - Log.LOGGER.debug("[实例安装]正在生成校验文件"); + Log.debug("正在生成校验文件"); inst.information.checksum = FileUtil.genChecksum(inst.imageDir); // 加载文件校验 inst.information.includeMods = genModList(new File(inst.imageDir, "mods")); // 加载mods信息 @@ -129,14 +129,16 @@ private static Instance install(File dir, String name, String version, String de * @param version 实例版本(不起作用) */ public static void installStandard(File zip, File dir, String name, String version) { + + Log.start("标准实例安装"); // 准备好目录 File image = FileUtil.initAndMkDir(dir, "image"); - Log.LOGGER.debug("[实例安装]开始解压:{}", zip); + Log.debug("开始解压:{}", zip); long time1 = System.currentTimeMillis(); ZipUtil.unCompress(zip, image); // 解压 long time2 = System.currentTimeMillis(); - Log.LOGGER.debug("[实例安装]解压完成!用时:{}s", (time2 - time1) / 1000); + Log.debug("解压完成!用时:{}s", (time2 - time1) / 1000); install(dir, name, version, "null"); // 创建实例 @@ -159,7 +161,8 @@ public static void installStandard(File zip, File dir, String name, String versi File config = new File(image, "zpl_margi_config.json"); FileUtil.fileWrite(config, JsonUtil.toJson(json)); - Log.LOGGER.info("[实例安装]实例 {} 安装完成!", name); + Log.info("实例 {} 安装完成!", name); + Log.end(); } /** @@ -183,14 +186,15 @@ public static void installUpdate(File updaterFile, File dir, String name, String public static void installTranslation(File translationFile, File dir, String name, String version, Instance targetVersion) { + Log.start("汉化实例安装"); File image = FileUtil.initAndMkDir(dir, "image"); - Log.LOGGER.debug("[实例安装]开始解压:{}", translationFile); + Log.debug("开始解压:{}", translationFile); long time1 = System.currentTimeMillis(); ZipUtil.unCompress(translationFile, image); // 解压 long time2 = System.currentTimeMillis(); - Log.LOGGER.debug("[实例安装]解压完成!用时:{}s", (time2 - time1) / 1000); + Log.debug("解压完成!用时:{}s", (time2 - time1) / 1000); Instance inst = install(dir, name, version, targetVersion.information.name); File depImageDir = targetVersion.imageDir; @@ -200,12 +204,13 @@ public static void installTranslation(File translationFile, File dir, String nam for (File file1 : Objects.requireNonNull(file.listFiles())) { if (file.isDirectory()) { File depFile = new File(depImageDir, file.getName() + "/" + file1.getName()); - Log.LOGGER.debug("[实例安装]复制目录:{} 到:{}", depFile, file1); + Log.debug("复制目录:{} 到:{}", depFile, file1); FileUtil.copyDir(depFile, file1.getParentFile()); } } } } + Log.end(); } private static List genModList(File modsDir) { @@ -224,8 +229,8 @@ private static List genModList(File modsDir) { String path = modRepo + "/" + modFileName; mods.add(path); if (!FileUtil.moveFile(mod, new File(DirTools.modsDir, path))) { - Log.LOGGER.info("[实例安装]已在mod库中发现:{} 即将删除临时文件", modFileName); - if (mod.delete()) Log.LOGGER.warn("[实例安装]文件:{} 删除失败,可能被占用,请手动删除", mod.getPath()); + Log.info("已在mod库中发现:{} 即将删除临时文件", modFileName); + if (mod.delete()) Log.warn("文件:{} 删除失败,可能被占用,请手动删除", mod.getPath()); } } } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Log.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Log.java index d635530..d59f672 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Log.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Log.java @@ -20,10 +20,106 @@ package com.github.wohaopa.zeropointlanuch.core; +import java.util.Stack; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; public class Log { - public static final Logger LOGGER = LogManager.getLogger("ZPW"); + private static final Logger myLogger = LogManager.getLogger("Main"); + + private static Marker marker = MarkerManager.getMarker("默认");; + private static final Stack old_marker = new Stack<>(); + + public static void start(String session) { + Log.marker = MarkerManager.getMarker(session); + Log.old_marker.push(Log.marker); + } + + public static void end() { + Log.marker = Log.old_marker.pop(); + } + + public static void debug(String s) { + myLogger.debug(marker, s); + } + + public static void debug(String s, Object o1) { + myLogger.debug(marker, s, o1); + } + + public static void debug(String s, Object o1, Object o2) { + myLogger.debug(marker, s, o1, o2); + } + + public static void debug(String s, Object o1, Object o2, Object o3) { + myLogger.debug(marker, s, o1, o2, o3); + } + + public static void debug(String s, Object o1, Object o2, Object o3, Object o4) { + myLogger.debug(marker, s, o1, o2, o3, o4); + } + + public static void info(String s) { + myLogger.info(marker, s); + } + + public static void info(String s, Object o1) { + myLogger.info(marker, s, o1); + } + + public static void info(String s, Object o1, Object o2) { + myLogger.info(marker, s, o1, o2); + } + + public static void info(String s, Object o1, Object o2, Object o3) { + myLogger.info(marker, s, o1, o2, o3); + } + + public static void info(String s, Object o1, Object o2, Object o3, Object o4) { + myLogger.info(marker, s, o1, o2, o3, o4); + } + + public static void warn(String s) { + myLogger.warn(marker, s); + } + + public static void warn(String s, Object o1) { + myLogger.warn(marker, s, o1); + } + + public static void warn(String s, Object o1, Object o2) { + myLogger.warn(marker, s, o1, o2); + } + + public static void warn(String s, Object o1, Object o2, Object o3) { + myLogger.warn(marker, s, o1, o2, o3); + } + + public static void warn(String s, Object o1, Object o2, Object o3, Object o4) { + myLogger.error(marker, s, o1, o2, o3, o4); + } + + public static void error(String s) { + myLogger.error(marker, s); + } + + public static void error(String s, Object o1) { + myLogger.error(marker, s, o1); + } + + public static void error(String s, Object o1, Object o2) { + myLogger.error(marker, s, o1, o2); + } + + public static void error(String s, Object o1, Object o2, Object o3) { + myLogger.error(marker, s, o1, o2, o3); + } + + public static void error(String s, Object o1, Object o2, Object o3, Object o4) { + myLogger.error(marker, s, o1, o2, o3, o4); + } } 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 9a8a885..4e0849a 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/Sharer.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/Sharer.java @@ -45,7 +45,6 @@ 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; @@ -69,7 +68,7 @@ public static void execute(String name, Mapper mapper) { mapper.execute(sharerP.name, sharerP.rootDir); } } catch (Exception e) { - Log.LOGGER.error("未找到Sharer或其依赖:{}", name); + Log.error("未找到Sharer或其依赖:{}", name); throw new RuntimeException(e); } } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/download/AssetsDownloader.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/download/AssetsDownloader.java index a939ee7..42d16dd 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/download/AssetsDownloader.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/download/AssetsDownloader.java @@ -36,13 +36,13 @@ public class AssetsDownloader { public static void verifyAssets(File assetsDir) throws ExecutionException, InterruptedException { - Log.LOGGER.debug("[assets文件夹校验]开始"); + Log.start("assets文件夹校验"); List fail = new ArrayList<>(); File mcAssetsFile = new File(assetsDir, "indexes/1.7.10.json"); File objectsDir = FileUtil.initAndMkDir(assetsDir, "objects"); File tmpDir = DirTools.tmpDir; if (!mcAssetsFile.exists()) { - Log.LOGGER.debug("[assets文件夹校验]正在下载indexes/1.7.10.json文件"); + Log.debug("正在下载indexes/1.7.10.json文件"); String mcAssetsFileUrl = DownloadProvider.getProvider() .getAssetsIndexJsonUrl(); DownloadUtil.submitDownloadTasks(mcAssetsFileUrl, tmpDir); @@ -67,7 +67,7 @@ public static void verifyAssets(File assetsDir) throws ExecutionException, Inter for (File file : fail) { String name = file.getName(); if (!FileUtil.checkSha1OfFile(new File(tmpDir, name), name)) { - Log.LOGGER.debug("[assets文件夹校验]正在将{}}文件加入下载队列", name); + Log.debug("正在将{}文件加入下载队列", name); downloads.add( DownloadProvider.getProvider() .getAssetsObjUrl(name)); @@ -75,12 +75,12 @@ public static void verifyAssets(File assetsDir) throws ExecutionException, Inter } DownloadUtil.submitDownloadTasks(downloads, tmpDir); - Log.LOGGER.debug("[assets文件夹校验]正在补全缺失的{}个文件", downloads.size()); + Log.debug("正在补全缺失的{}个文件", downloads.size()); DownloadUtil.takeDownloadResult(); for (File file : fail) { FileUtil.moveFile(new File(tmpDir, file.getName()), file, true); } - Log.LOGGER.debug("[assets文件夹校验]结束"); + Log.end(); } } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/DownloadUtil.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/DownloadUtil.java index 7112df3..d88bb2b 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/DownloadUtil.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/DownloadUtil.java @@ -28,13 +28,16 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import cn.hutool.core.thread.ThreadUtil; import cn.hutool.http.HttpUtil; -import com.github.wohaopa.zeropointlanuch.core.Log; - public class DownloadUtil { + private static final Logger downloadLog = LogManager.getLogger("Download"); + public static int tasksCount = 0; static CompletionService cs = ThreadUtil.newCompletionService(ThreadUtil.newExecutor(5)); @@ -74,32 +77,32 @@ public static List takeDownloadResult() throws ExecutionException, Interru int count = tasksCount; while (tasksCount-- != 0) { - Log.LOGGER.debug("正在下载:{} 剩余:{}", count - tasksCount, tasksCount); + downloadLog.debug("正在下载:{} 剩余:{}", count - tasksCount, tasksCount); result = cs.take(); list.add(result.get()); } return list; } -} -class DownloadTask implements Callable { + static class DownloadTask implements Callable { - String url; - File file; + String url; + File file; - public DownloadTask(String url, File file) { - this.url = url; - this.file = file; - } + public DownloadTask(String url, File file) { + this.url = url; + this.file = file; + } - @Override - public File call() throws Exception { - try { - return HttpUtil.downloadFileFromUrl(url, file); - } catch (Exception e) { - Log.LOGGER.error("[多线程下载器]无法下载链接:{}", url); + @Override + public File call() throws Exception { + try { + return HttpUtil.downloadFileFromUrl(url, file); + } catch (Exception e) { + downloadLog.error("无法下载链接:{}", url); + } + return null; } - return null; } } diff --git a/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/FileUtil.java b/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/FileUtil.java index 0f6add6..5556ed0 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/FileUtil.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/core/utils/FileUtil.java @@ -133,7 +133,7 @@ public static void genLink(File link, File target) { try { if (link.exists()) { - Log.LOGGER.info("跳过文件:{} 文件已存在", link); + Log.info("跳过文件:{} 文件已存在", link); return; } link.getParentFile() @@ -141,7 +141,7 @@ public static void genLink(File link, File target) { Files.createSymbolicLink(link.toPath(), target.toPath()); } catch (IOException e) { adminFlag = true; - Log.LOGGER.info("无法创建文件链接,可能是没有管理员权限,文件:{} 目标:{}", link, target); + Log.info("无法创建文件链接,可能是没有管理员权限,文件:{} 目标:{}", link, target); throw new RuntimeException("缺少管理员权限,无法创建系统链接"); } } @@ -187,7 +187,8 @@ public static void delLink(File file) { try { if (!path.equals(path.toRealPath())) delete(file); } catch (IOException e) { - Log.LOGGER.error("无法判断文件链接:{} 错误:{}", file.getPath(), e); + delete(file); // 一般无法找到源文件,即源文件失效 + Log.error("无法判断文件链接:{} 错误:{}", file.getPath(), e); } } 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 693f47c..21ba88c 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/main/Command.java @@ -67,7 +67,7 @@ public boolean execute(String[] args) { @Override public String usage() { - return "lookup - 安装zip文件夹压缩包并刷新实例文件夹"; + return "lookup - 刷新实例文件夹并安装zip文件夹压缩包"; } }; @@ -210,29 +210,38 @@ public boolean execute(String[] args) { @Override public String usage() { - return "genRunDir <实例名> - 生成指定版本的运行目录,在使用其他启动器时使用次命令"; + return "genRunDir <实例名> [共享器] - 生成指定版本的运行目录,在使用其他启动器时使用此命令"; } }; - /* - * public static ICommand genHMCLDir = new ICommand() { - * @Override - * public boolean execute(String[] args) throws IOException { - * if (args.length < 2) { - * System.out.println("用法" + usage()); - * return false; - * } - * if (!Instance.containsKey(args[1])) { - * System.out.println("错误:已安装的实例中不包含:\"" + args[1] + "\" 可以使用list指令查看已安装实例"); - * return false; - * } - * System.out.println("dir=" + Function.genHMCLDir(args[1])); - * return true; - * } - * @Override - * public String usage() { - * return "genHMCLDir <实例名> - 生成hmcl支持的.miencraft文件夹"; - * } - * }; - */ + public static ICommand genTransferOld = new ICommand() { + + @Override + public boolean execute(String[] args) throws IOException { + if (args.length < 3) { + System.out.println("用法:" + usage()); + return false; + } + + Instance instance = Instance.get(args[1]); + if (instance == null) { + System.out.println("错误:已安装的实例中不包含:\"" + args[1] + "\" 可以使用list指令查看已安装实例"); + return false; + } + + File file = new File(args[2].replace("\"", "")); + if (!file.exists() || file.isFile()) { + System.out.println("错误:" + file + "不存在/不是目录。"); + return false; + } + + Core.genTransferOld(instance, file); + return true; + } + + @Override + public String usage() { + return "genTransferOld <实例名> <.minecraft路径> - 将原来的GTNH迁移至ZPL对应版本的实例"; + } + }; } 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 21453f7..cd80989 100644 --- a/src/main/java/com/github/wohaopa/zeropointlanuch/main/Main.java +++ b/src/main/java/com/github/wohaopa/zeropointlanuch/main/Main.java @@ -23,23 +23,34 @@ import java.io.File; import java.io.IOException; import java.lang.reflect.Field; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; +import java.util.concurrent.ExecutionException; + +import cn.hutool.json.JSONObject; import com.github.wohaopa.zeropointlanuch.api.Core; +import com.github.wohaopa.zeropointlanuch.core.DirTools; import com.github.wohaopa.zeropointlanuch.core.Log; +import com.github.wohaopa.zeropointlanuch.core.utils.DownloadUtil; +import com.github.wohaopa.zeropointlanuch.core.utils.JsonUtil; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class Main { + private static final String DOWNLOAD_VERSION_URL = "http://127.0.0.1//ZeroPointLaunch//"; public static Map commands = new HashMap<>(); public static File workDir; public static void main(String[] args) { long a = System.currentTimeMillis(); - Log.LOGGER.debug("ZeroPoint启动器核心正在启动..."); + Log.info("ZeroPoint启动器核心正在启动..."); Main.init(); long b = System.currentTimeMillis(); - Log.LOGGER.debug("ZeroPoint启动器核心启动成功,用时:{}ms", b - a); + Log.debug("ZeroPoint启动器核心启动成功,用时:{}ms", b - a); if (args.length == 0) { appProcess(); @@ -47,7 +58,7 @@ public static void main(String[] args) { commandProcess(args); } - Log.LOGGER.info("ZeroPoint启动器核心关闭"); + Log.info("ZeroPoint启动器核心关闭"); System.exit(0); // 关闭用于下载的线程池 } @@ -59,12 +70,54 @@ private static void init() { rootDirStr = System.getProperty("user.dir") + "/.GTNH"; } workDir = new File(rootDirStr); - Core.initDirTools(workDir); // 目录工具初始化 + + if (!Core.launcherVersion.equals("内部测试版本")) { + check_update(); + } + Main.registerCommand(GenCommand.class); // 注册指令 Main.registerCommand(Command.class); } + private static void check_update() { + Logger updateLog = LogManager.getLogger("Update"); + Runnable runnable = () -> { + File versionFile = new File(DirTools.tmpDir, "version.json"); + updateLog.info("开始检查更新"); + DownloadUtil.submitDownloadTasks(DOWNLOAD_VERSION_URL + "version.json", versionFile); + try { + DownloadUtil.takeDownloadResult(); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + JSONObject versionJson = (JSONObject) JsonUtil.fromJson(versionFile); + String latestVersion = (String) versionJson.get("latest"); + boolean flag = false; + { + String[] a = Core.launcherVersion.split("\\."); + String[] b = latestVersion.split("\\."); + for (int i = 0; i < a.length; i++) { + if (Integer.parseInt(b[i]) > Integer.parseInt(a[i])) { + flag = true; + break; + } + } + } + if (flag) { + updateLog.info("发现更新,正在下载更新"); + String downloadUrl = DOWNLOAD_VERSION_URL + versionJson.get("download-url"); + DownloadUtil.submitDownloadTasks( + downloadUrl, + new File(workDir.getParentFile(), "lib\\ZeroPointLaunch-Core.jar")); + } + versionFile.delete(); + }; + Thread thread = new Thread(runnable); + thread.setName("update"); + thread.start(); + } + private static void registerCommand(Class commandClass) { Field[] fields = commandClass.getFields(); try { @@ -72,7 +125,7 @@ private static void registerCommand(Class commandClass) { commands.put(field.getName(), (ICommand) field.get(null)); } } catch (IllegalAccessException e) { - Log.LOGGER.error("无法注册指令,错误:{}", e.getMessage()); + Log.error("无法注册指令,错误:{}", e.getMessage()); throw new RuntimeException(e); } } diff --git a/src/main/resources/log4j2-test.xml b/src/main/resources/log4j2-test.xml index 579c0ef..310edd9 100644 --- a/src/main/resources/log4j2-test.xml +++ b/src/main/resources/log4j2-test.xml @@ -2,7 +2,7 @@ - + diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 8a242d1..a2eb6aa 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - +