Support single datapack zip
This commit is contained in:
@@ -17,6 +17,7 @@ import java.util.*;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class Datapack {
|
public class Datapack {
|
||||||
|
private boolean isMultiple;
|
||||||
private final Path path;
|
private final Path path;
|
||||||
private final ObservableList<Pack> info = FXCollections.observableArrayList();
|
private final ObservableList<Pack> info = FXCollections.observableArrayList();
|
||||||
|
|
||||||
@@ -44,7 +45,11 @@ public class Datapack {
|
|||||||
FileUtils.deleteDirectory(datapack.toFile());
|
FileUtils.deleteDirectory(datapack.toFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
new Unzipper(path, worldPath).setReplaceExistentFile(true).unzip();
|
if (isMultiple) {
|
||||||
|
new Unzipper(path, worldPath).setReplaceExistentFile(true).unzip();
|
||||||
|
} else {
|
||||||
|
new Unzipper(path, worldPath.resolve("datapacks").resolve(FileUtils.getNameWithoutExtension(path))).unzip();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deletePack(String pack) throws IOException {
|
public void deletePack(String pack) throws IOException {
|
||||||
@@ -54,7 +59,22 @@ public class Datapack {
|
|||||||
|
|
||||||
public void loadFromZip() throws IOException {
|
public void loadFromZip() throws IOException {
|
||||||
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(path)) {
|
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(path)) {
|
||||||
loadFromDir(fs.getPath("/datapacks/"));
|
Path datapacks = fs.getPath("/datapacks/");
|
||||||
|
Path mcmeta = fs.getPath("pack.mcmeta");
|
||||||
|
if (Files.exists(datapacks)) { // multiple datapacks
|
||||||
|
isMultiple = true;
|
||||||
|
loadFromDir(datapacks);
|
||||||
|
} else if (Files.exists(mcmeta)) { // single datapack
|
||||||
|
isMultiple = false;
|
||||||
|
try {
|
||||||
|
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
|
||||||
|
info.add(new Pack(mcmeta, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this));
|
||||||
|
} catch (IOException e) {
|
||||||
|
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new IOException("Malformed datapack zip");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,13 +92,17 @@ public class Datapack {
|
|||||||
if (Files.isDirectory(dir))
|
if (Files.isDirectory(dir))
|
||||||
for (Path subDir : Files.newDirectoryStream(dir)) {
|
for (Path subDir : Files.newDirectoryStream(dir)) {
|
||||||
Path mcmeta = subDir.resolve("pack.mcmeta");
|
Path mcmeta = subDir.resolve("pack.mcmeta");
|
||||||
|
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled");
|
||||||
|
|
||||||
if (!Files.exists(mcmeta))
|
if (!Files.exists(mcmeta) && !Files.exists(mcmetaDisabled))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
boolean enabled = Files.exists(mcmeta);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
|
PackMcMeta pack = enabled ? JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class)
|
||||||
info.add(new Pack(mcmeta, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this));
|
: JsonUtils.fromNonNullJson(FileUtils.readText(mcmetaDisabled), PackMcMeta.class);
|
||||||
|
info.add(new Pack(enabled ? mcmeta : mcmetaDisabled, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
|
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
|
||||||
}
|
}
|
||||||
@@ -107,7 +131,7 @@ public class Datapack {
|
|||||||
if (DISABLED_EXT.equals(FileUtils.getExtension(f)))
|
if (DISABLED_EXT.equals(FileUtils.getExtension(f)))
|
||||||
newF = f.getParent().resolve(FileUtils.getNameWithoutExtension(f));
|
newF = f.getParent().resolve(FileUtils.getNameWithoutExtension(f));
|
||||||
else
|
else
|
||||||
newF = f.getParent().resolve(FileUtils.getName(f) + DISABLED_EXT);
|
newF = f.getParent().resolve(FileUtils.getName(f) + "." + DISABLED_EXT);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Files.move(f, newF);
|
Files.move(f, newF);
|
||||||
@@ -194,5 +218,5 @@ public class Datapack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String DISABLED_EXT = ".disabled";
|
private static final String DISABLED_EXT = "disabled";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public final class ModInfo implements Comparable<ModInfo> {
|
|||||||
if (DISABLED_EXTENSION.equals(FileUtils.getExtension(f)))
|
if (DISABLED_EXTENSION.equals(FileUtils.getExtension(f)))
|
||||||
newF = new File(f.getParentFile(), FileUtils.getNameWithoutExtension(f));
|
newF = new File(f.getParentFile(), FileUtils.getNameWithoutExtension(f));
|
||||||
else
|
else
|
||||||
newF = new File(f.getParentFile(), f.getName() + ".disabled");
|
newF = new File(f.getParentFile(), f.getName() + "." + DISABLED_EXTENSION);
|
||||||
|
|
||||||
if (f.renameTo(newF))
|
if (f.renameTo(newF))
|
||||||
ModInfo.this.file = newF;
|
ModInfo.this.file = newF;
|
||||||
|
|||||||
Reference in New Issue
Block a user