Support zip file in datapacks folder

This commit is contained in:
huanghongxun 2018-09-19 23:02:27 +08:00
parent bafe17107c
commit aa8c386cdd
5 changed files with 59 additions and 25 deletions

View File

@ -38,7 +38,7 @@ public class DatapackListPage extends ListPage<DatapackListItem> implements Deco
list = MappedObservableList.create(datapack.getInfo(), pack -> new DatapackListItem(pack, item -> { list = MappedObservableList.create(datapack.getInfo(), pack -> new DatapackListItem(pack, item -> {
try { try {
datapack.deletePack(pack.getId()); datapack.deletePack(pack);
} catch (IOException e) { } catch (IOException e) {
Logging.LOG.warning("Failed to delete datapack"); Logging.LOG.warning("Failed to delete datapack");
} }
@ -84,7 +84,7 @@ public class DatapackListPage extends ListPage<DatapackListItem> implements Deco
public void add() { public void add() {
FileChooser chooser = new FileChooser(); FileChooser chooser = new FileChooser();
chooser.setTitle(i18n("datapack.choose_datapack")); chooser.setTitle(i18n("datapack.choose_datapack"));
chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter(i18n("extension.datapack"), "*.zip")); chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter(i18n("datapack.extension"), "*.zip"));
List<File> res = chooser.showOpenMultipleDialog(Controllers.getStage()); List<File> res = chooser.showOpenMultipleDialog(Controllers.getStage());
if (res != null) if (res != null)

View File

@ -247,6 +247,7 @@ mods.remove=Remove
datapack=Data packs datapack=Data packs
datapack.add=Add data pack datapack.add=Add data pack
datapack.choose_datapack=Choose the datapack zip to be imported datapack.choose_datapack=Choose the datapack zip to be imported
datapack.extension=Datapack
datapack.title=World %s - Datapacks datapack.title=World %s - Datapacks
datapack.remove=Remove datapack.remove=Remove

View File

@ -247,6 +247,7 @@ mods.remove=刪除
datapack=資料包 datapack=資料包
datapack.add=添加資料包 datapack.add=添加資料包
datapack.choose_datapack=選擇要導入的資料包壓縮檔 datapack.choose_datapack=選擇要導入的資料包壓縮檔
datapack.extension=資料包
datapack.title=世界 %s - 資料包 datapack.title=世界 %s - 資料包
datapack.remove=刪除 datapack.remove=刪除

View File

@ -247,6 +247,7 @@ mods.remove=删除
datapack=数据包 datapack=数据包
datapack.add=添加数据包 datapack.add=添加数据包
datapack.choose_datapack=选择要导入的数据包压缩包 datapack.choose_datapack=选择要导入的数据包压缩包
datapack.extension=数据包
datapack.title=世界 %s - 数据包 datapack.title=世界 %s - 数据包
datapack.remove=删除 datapack.remove=删除

View File

@ -41,20 +41,27 @@ public class Datapack {
if (Files.isDirectory(datapacks)) if (Files.isDirectory(datapacks))
for (Path datapack : Files.newDirectoryStream(datapacks)) { for (Path datapack : Files.newDirectoryStream(datapacks)) {
if (packs.contains(FileUtils.getName(datapack))) if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack)))
FileUtils.deleteDirectory(datapack.toFile()); FileUtils.deleteDirectory(datapack.toFile());
else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack)))
Files.delete(datapack);
} }
if (isMultiple) { if (isMultiple) {
new Unzipper(path, worldPath).setReplaceExistentFile(true).unzip(); new Unzipper(path, worldPath).setReplaceExistentFile(true).unzip();
} else { } else {
new Unzipper(path, worldPath.resolve("datapacks").resolve(FileUtils.getNameWithoutExtension(path))).unzip(); FileUtils.copyFile(path.toFile(), datapacks.resolve(FileUtils.getName(path)).toFile());
} }
} }
public void deletePack(String pack) throws IOException { public void deletePack(Pack pack) throws IOException {
FileUtils.deleteDirectory(path.resolve(pack).toFile()); Path subPath = pack.file;
Platform.runLater(() -> info.removeIf(p -> p.getId().equals(pack))); if (Files.isDirectory(subPath))
FileUtils.deleteDirectory(subPath.toFile());
else if (Files.isRegularFile(subPath))
Files.delete(subPath);
Platform.runLater(() -> info.removeIf(p -> p.getId().equals(pack.getId())));
} }
public void loadFromZip() throws IOException { public void loadFromZip() throws IOException {
@ -68,7 +75,7 @@ public class Datapack {
isMultiple = false; isMultiple = false;
try { try {
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class); PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(mcmeta, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this)); info.add(new Pack(path, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this));
} catch (IOException e) { } catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e); Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e);
} }
@ -91,6 +98,7 @@ public class Datapack {
if (Files.isDirectory(dir)) if (Files.isDirectory(dir))
for (Path subDir : Files.newDirectoryStream(dir)) { for (Path subDir : Files.newDirectoryStream(dir)) {
if (Files.isDirectory(subDir)) {
Path mcmeta = subDir.resolve("pack.mcmeta"); Path mcmeta = subDir.resolve("pack.mcmeta");
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled"); Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled");
@ -106,28 +114,51 @@ public class Datapack {
} 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);
} }
} else if (Files.isRegularFile(subDir)) {
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(subDir)) {
Path mcmeta = fs.getPath("pack.mcmeta");
if (!Files.exists(mcmeta))
continue;
String name = FileUtils.getName(subDir);
boolean enabled = true;
if (name.endsWith(".disabled")) {
name = name.substring(0, name.length() - ".disabled".length());
enabled = false;
}
if (!name.endsWith(".zip"))
continue;
name = StringUtils.substringBeforeLast(name, ".zip");
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(subDir, name, pack.getPackInfo().getDescription(), this));
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
}
}
} }
this.info.setAll(info); this.info.setAll(info);
} }
public static class Pack { public static class Pack {
private Path packMcMeta; private Path file;
private final BooleanProperty active; private final BooleanProperty active;
private final String id; private final String id;
private final String description; private final String description;
private final Datapack datapack; private final Datapack datapack;
public Pack(Path packMcMeta, String id, String description, Datapack datapack) { public Pack(Path file, String id, String description, Datapack datapack) {
this.packMcMeta = packMcMeta; this.file = file;
this.id = id; this.id = id;
this.description = description; this.description = description;
this.datapack = datapack; this.datapack = datapack;
active = new SimpleBooleanProperty(this, "active", !DISABLED_EXT.equals(FileUtils.getExtension(packMcMeta))) { active = new SimpleBooleanProperty(this, "active", !DISABLED_EXT.equals(FileUtils.getExtension(file))) {
@Override @Override
protected void invalidated() { protected void invalidated() {
Path f = Pack.this.packMcMeta.toAbsolutePath(), newF; Path f = Pack.this.file.toAbsolutePath(), newF;
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
@ -135,7 +166,7 @@ public class Datapack {
try { try {
Files.move(f, newF); Files.move(f, newF);
Pack.this.packMcMeta = newF; Pack.this.file = newF;
} catch (IOException e) { } catch (IOException e) {
// Mod file is occupied. // Mod file is occupied.
Logging.LOG.warning("Unable to rename file " + f + " to " + newF); Logging.LOG.warning("Unable to rename file " + f + " to " + newF);