From 356a321faa74ba84ce59d68df30d671148985354 Mon Sep 17 00:00:00 2001 From: cssxsh Date: Sun, 4 Sep 2022 13:13:08 +0800 Subject: [PATCH] fix: use original file name (#133) * fix: original file name * use: substring * fix: dir * fix: delete * fix: download default name --- src/main/java/org/itxtech/mcl/Utility.java | 5 +-- .../org/itxtech/mcl/module/builtin/Boot.java | 8 ++-- .../itxtech/mcl/module/builtin/Updater.java | 28 +++++++++---- .../java/org/itxtech/mcl/pkg/MclPackage.java | 41 +++++++++++++------ 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/itxtech/mcl/Utility.java b/src/main/java/org/itxtech/mcl/Utility.java index 39776b2..e8b0eb6 100644 --- a/src/main/java/org/itxtech/mcl/Utility.java +++ b/src/main/java/org/itxtech/mcl/Utility.java @@ -1,6 +1,5 @@ package org.itxtech.mcl; -import org.itxtech.mcl.component.Config; import org.itxtech.mcl.pkg.MclPackage; import java.io.File; @@ -66,9 +65,7 @@ public static boolean check(File baseFile, File checksumFile) throws Exception { } public static boolean checkLocalFile(MclPackage pkg) throws Exception { - var dir = new File(pkg.type); - dir.mkdirs(); - return Utility.check(pkg.getJarFile(), new File(dir, pkg.getBasename() + ".sha1")); + return Utility.check(pkg.getJarFile(), pkg.getSha1File()); } public interface GetMain { diff --git a/src/main/java/org/itxtech/mcl/module/builtin/Boot.java b/src/main/java/org/itxtech/mcl/module/builtin/Boot.java index 65cc4e1..c92b772 100644 --- a/src/main/java/org/itxtech/mcl/module/builtin/Boot.java +++ b/src/main/java/org/itxtech/mcl/module/builtin/Boot.java @@ -85,13 +85,13 @@ public void boot() { var pkgMap = new HashMap(); for (var pkg : loader.packageManager.getPackages()) { if (pkg.type.equals(MclPackage.TYPE_CORE)) { - files.add(new File(new File(pkg.type), pkg.getBasename() + ".jar")); + files.add(pkg.getJarFile()); pkgMap.put(pkg.id, pkg.version); } if (pkg.type.equals(MclPackage.TYPE_PLUGIN)) { - var file = new File(new File(pkg.type), pkg.getBasename() + ".metadata"); - if (file.exists()) { - for (var s : loader.repo.getMetadataFromFile(file).dependencies) { + var metadata = pkg.getMetadataFile(); + if (metadata.exists()) { + for (var s : loader.repo.getMetadataFromFile(metadata).dependencies) { var dep = s.split(":"); var name = dep[0] + ":" + dep[1]; var version = dep[2]; diff --git a/src/main/java/org/itxtech/mcl/module/builtin/Updater.java b/src/main/java/org/itxtech/mcl/module/builtin/Updater.java index 0181ac5..bc6e8a0 100644 --- a/src/main/java/org/itxtech/mcl/module/builtin/Updater.java +++ b/src/main/java/org/itxtech/mcl/module/builtin/Updater.java @@ -166,21 +166,31 @@ public void check(MclPackage pack) throws Exception { public void downloadFile(MclPackage pack, Repository.PackageInfo info) { var dir = new File(pack.type); dir.mkdirs(); - var ver = pack.version; + var name = pack.getName(); + var jar = name + "-" + pack.version + ".jar"; + var metadata = name + "-" + pack.version + ".mirai.metadata"; + var jarUrl = loader.repo.getJarUrl(pack, info); - if (!jarUrl.isEmpty()) { - down(jarUrl, new File(dir, pack.getName() + "-" + ver + ".jar")); - down(loader.repo.getSha1Url(pack, info, jarUrl), new File(dir, pack.getName() + "-" + ver + ".sha1")); - var metadata = loader.repo.getMetadataUrl(pack, info); - if (!metadata.equals("")) { - down(metadata, new File(dir, pack.getName() + "-" + ver + ".metadata")); - } - } else { + if (jarUrl.isEmpty()) { loader.logger.error(Ansi.ansi() .a("Cannot download package ") .fgBrightYellow().a("\"").a(pack.id).a("\"") ); + return; } + var index = jarUrl.lastIndexOf(name); + if (index != -1) { + jar = jarUrl.substring(index); + } + down(jarUrl, new File(dir, jar)); + + var sha1Url = loader.repo.getSha1Url(pack, info, jarUrl); + var sha1 = jar + ".sha1"; + down(sha1Url, new File(dir, sha1)); + + var metadataUrl = loader.repo.getMetadataUrl(pack, info); + if (metadataUrl.isEmpty()) return; + down(metadataUrl, new File(dir, metadata)); } public String alignRight(String current, String total) { diff --git a/src/main/java/org/itxtech/mcl/pkg/MclPackage.java b/src/main/java/org/itxtech/mcl/pkg/MclPackage.java index 7e03cad..9354aaf 100644 --- a/src/main/java/org/itxtech/mcl/pkg/MclPackage.java +++ b/src/main/java/org/itxtech/mcl/pkg/MclPackage.java @@ -89,28 +89,43 @@ public String getName() { return id.split(":", 2)[1]; } - public String getBasename() { - return getName() + "-" + version; + public File getJarFile() { + var name = getName(); + var suffix = Loader.getInstance().config.archiveSuffix; + for (String end : suffix) { + var file = new File(type, name + "-" + version + end); + if (file.exists()) return file; + } + return new File(type, name + "-" + version + suffix.get(0)); } - public File getJarFile() { - return new File(new File(type), getBasename() + ".jar"); + public File getSha1File() { + var jar = getJarFile(); + return new File(jar.getParent(), jar.getName() + ".sha1"); + } + + public File getMetadataFile() { + var name = getName(); + return new File(type, name + "-" + version + ".mirai.metadata"); } public void removeFiles() { var dir = new File(type); - deleteFile(dir, "jar"); - deleteFile(dir, "sha1"); - deleteFile(dir, "metadata"); + var name = getName(); + deleteFile(dir, name, "jar"); + deleteFile(dir, name, "zip"); + deleteFile(dir, name, "sha1"); + deleteFile(dir, name, "metadata"); } - public void deleteFile(File dir, String type) { - var f = new File(dir, getBasename() + "." + type); - if (f.exists()) { - if (f.delete()) { - Loader.getInstance().logger.info("File \"" + f.getName() + "\" has been deleted."); + public void deleteFile(File dir, String name, String type) { + var list = dir.listFiles((d, f) -> f.startsWith(name) && f.endsWith(type)); + if (list == null) return; + for (File source : list) { + if (source.delete()) { + Loader.getInstance().logger.info("File \"" + source.getName() + "\" has been deleted."); } else { - Loader.getInstance().logger.error("Failed to delete \"" + f.getName() + "\". Please delete it manually."); + Loader.getInstance().logger.error("Failed to delete \"" + source.getName() + "\". Please delete it manually."); } } }