Refactor ModListPage and InstallerListPage

This commit is contained in:
huanghongxun
2018-09-08 00:36:57 +08:00
parent d8a34aac1c
commit daa1a38a63
10 changed files with 168 additions and 141 deletions

View File

@@ -0,0 +1,61 @@
package org.jackhuang.hmcl.game;
import org.jackhuang.hmcl.util.CompressingUtils;
import org.jackhuang.hmcl.util.FileUtils;
import org.jackhuang.hmcl.util.Unzipper;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
public class World {
private final Path file;
private final String name;
public World(Path file) throws IOException {
this.file = file;
if (Files.isDirectory(file))
name = loadFromDirectory();
else if (Files.isRegularFile(file))
name = loadFromZip();
else
throw new IOException("Path " + file + " cannot be recognized as a Minecraft world");
}
private String loadFromDirectory() {
return FileUtils.getName(file);
}
private String loadFromZip() throws IOException {
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(file)) {
Path root = Files.list(fs.getPath("/")).filter(Files::isDirectory).findAny()
.orElseThrow(() -> new IOException("Not a valid world zip file"));
Path levelDat = root.resolve("level.dat");
if (!Files.exists(levelDat))
throw new FileNotFoundException("Not a valid world zip file since level.dat cannot be found.");
return FileUtils.getName(root);
}
}
public void install(Path savesDir) throws IOException {
Path worldDir = savesDir.resolve(name);
if (Files.isDirectory(worldDir)) {
throw new FileAlreadyExistsException("World already exists");
}
if (Files.isRegularFile(file)) {
new Unzipper(file, savesDir)
.setSubDirectory("/" + name + "/")
.unzip();
} else if (Files.isDirectory(file)) {
FileUtils.copyDirectory(file, worldDir);
}
}
}

View File

@@ -22,8 +22,8 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@@ -54,6 +54,10 @@ public final class FileUtils {
return StringUtils.addPrefix(StringUtils.removeSuffix(path, "/", "\\"), "/");
}
public static String getName(Path path) {
return StringUtils.removeSuffix(path.getFileName().toString(), "/", "\\");
}
public static String readText(File file) throws IOException {
return readText(file, UTF_8);
}
@@ -97,6 +101,26 @@ public final class FileUtils {
return Lang.test(() -> deleteDirectory(directory));
}
public static void copyDirectory(Path src, Path dest) throws IOException {
Files.walkFileTree(src, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path destFile = dest.resolve(src.relativize(file));
Files.copy(file, destFile);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path destDir = dest.resolve(src.relativize(dir));
Files.createDirectory(destDir);
return FileVisitResult.CONTINUE;
}
});
}
public static boolean moveToTrash(File file) {
try {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();