Merge resources.zip to allow multiple datapack zip containing resources.zip
This commit is contained in:
@@ -10,9 +10,7 @@ import javafx.collections.ObservableList;
|
|||||||
import org.jackhuang.hmcl.util.*;
|
import org.jackhuang.hmcl.util.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileSystem;
|
import java.nio.file.*;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@@ -48,7 +46,40 @@ public class Datapack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isMultiple) {
|
if (isMultiple) {
|
||||||
new Unzipper(path, worldPath).setReplaceExistentFile(true).unzip();
|
new Unzipper(path, worldPath)
|
||||||
|
.setReplaceExistentFile(true)
|
||||||
|
.setFilter(new Unzipper.FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(Path destPath, boolean isDirectory, Path zipEntry, String entryPath) {
|
||||||
|
// We will merge resources.zip instead of replacement.
|
||||||
|
return !entryPath.equals("resources.zip");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unzip();
|
||||||
|
|
||||||
|
try (FileSystem dest = CompressingUtils.createWritableZipFileSystem(worldPath.resolve("resources.zip"));
|
||||||
|
FileSystem zip = CompressingUtils.createReadOnlyZipFileSystem(path)) {
|
||||||
|
Path resourcesZip = zip.getPath("resources.zip");
|
||||||
|
if (Files.isRegularFile(resourcesZip)) {
|
||||||
|
Path temp = Files.createTempFile("hmcl", ".zip");
|
||||||
|
Files.copy(resourcesZip, temp, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
try (FileSystem resources = CompressingUtils.createReadOnlyZipFileSystem(temp)) {
|
||||||
|
FileUtils.copyDirectory(resources.getPath("/"), dest.getPath("/"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Path packMcMeta = dest.getPath("pack.mcmeta");
|
||||||
|
Files.write(packMcMeta, Arrays.asList("{",
|
||||||
|
"\t\"pack\": {",
|
||||||
|
"\t\t\"pack_format\": 4,",
|
||||||
|
"\t\t\"description\": \"Modified by HMCL.\"",
|
||||||
|
"\t}",
|
||||||
|
"}"), StandardOpenOption.CREATE);
|
||||||
|
|
||||||
|
|
||||||
|
Path packPng = dest.getPath("pack.png");
|
||||||
|
if (Files.isRegularFile(packPng))
|
||||||
|
Files.delete(packPng);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
FileUtils.copyFile(path.toFile(), datapacks.resolve(FileUtils.getName(path)).toFile());
|
FileUtils.copyFile(path.toFile(), datapacks.resolve(FileUtils.getName(path)).toFile());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public final class CompressingUtils {
|
|||||||
// Since Java 8 throws ZipError stupidly
|
// Since Java 8 throws ZipError stupidly
|
||||||
throw new ZipException(error.getMessage());
|
throw new ZipException(error.getMessage());
|
||||||
} catch (UnsupportedOperationException ex) {
|
} catch (UnsupportedOperationException ex) {
|
||||||
throw new IOException("Not a zip file");
|
throw new IOException("Not a zip file", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ public final class FileUtils {
|
|||||||
@Override
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
Path destFile = dest.resolve(src.relativize(file));
|
Path destFile = dest.resolve(src.relativize(file));
|
||||||
Files.copy(file, destFile);
|
Files.copy(file, destFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
@@ -126,7 +126,7 @@ public final class FileUtils {
|
|||||||
@Override
|
@Override
|
||||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||||
Path destDir = dest.resolve(src.relativize(dir));
|
Path destDir = dest.resolve(src.relativize(dir));
|
||||||
Files.createDirectory(destDir);
|
Files.createDirectories(destDir);
|
||||||
|
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user