Skip to content

Commit

Permalink
fix: use original file name (#133)
Browse files Browse the repository at this point in the history
* fix: original file name

* use: substring

* fix: dir

* fix: delete

* fix: download default name
  • Loading branch information
cssxsh authored Sep 4, 2022
1 parent d4ddbd4 commit 356a321
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
5 changes: 1 addition & 4 deletions src/main/java/org/itxtech/mcl/Utility.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.itxtech.mcl;

import org.itxtech.mcl.component.Config;
import org.itxtech.mcl.pkg.MclPackage;

import java.io.File;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/itxtech/mcl/module/builtin/Boot.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public void boot() {
var pkgMap = new HashMap<String, String>();
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];
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/org/itxtech/mcl/module/builtin/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 28 additions & 13 deletions src/main/java/org/itxtech/mcl/pkg/MclPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
Expand Down

0 comments on commit 356a321

Please sign in to comment.