Working ZIP file loading

This commit is contained in:
dfsek
2020-11-29 18:17:29 -07:00
parent 1c0954d0cf
commit 78acf59f98
5 changed files with 33 additions and 19 deletions

View File

@@ -105,17 +105,15 @@ public class ConfigPack {
long l = System.nanoTime();
InputStream stream = null;
Enumeration<? extends ZipEntry> entries = file.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.getName().equals("pack.yml")) {
try {
stream = file.getInputStream(entry);
} catch(IOException e) {
throw new LoadException("Unable to load pack.yml from ZIP file", e);
}
try {
Enumeration<? extends ZipEntry> entries = file.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.getName().equals("pack.yml")) stream = file.getInputStream(entry);
}
} catch(IOException e) {
throw new LoadException("Unable to load pack.yml from ZIP file", e);
}
if(stream == null) throw new FileMissingException("No pack.yml file found in " + file.getName());

View File

@@ -19,8 +19,7 @@ public class FolderLoader extends Loader {
}
@Override
public Loader open(String directory) {
if(streams.size() != 0) throw new IllegalStateException("Attempted to load folder before closing InputStreams");
protected void load(String directory) {
File newPath = new File(path.toFile(), directory);
newPath.mkdirs();
try(Stream<Path> paths = Files.walk(newPath.toPath())) {
@@ -34,6 +33,5 @@ public class FolderLoader extends Loader {
} catch(IOException e) {
e.printStackTrace();
}
return this;
}
}

View File

@@ -23,10 +23,15 @@ public abstract class Loader {
/**
* Open a subdirectory.
*
* @param directory
* @return
* @param directory Directory to open
*/
public abstract Loader open(String directory);
public Loader open(String directory) {
if(streams.size() != 0) throw new IllegalStateException("Attempted to load new directory before closing existing InputStreams");
load(directory);
return this;
}
protected abstract void load(String directory);
/**
* Close all InputStreams opened.

View File

@@ -13,10 +13,8 @@ public class ZIPLoader extends Loader {
}
@Override
public Loader open(String directory) {
if(streams.size() != 0) throw new IllegalStateException("Attempted to load folder before closing InputStreams");
protected void load(String directory) {
Enumeration<? extends ZipEntry> entries = file.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(!entry.isDirectory() && entry.getName().startsWith(directory) && entry.getName().endsWith(".yml")) {
@@ -27,6 +25,5 @@ public class ZIPLoader extends Loader {
}
}
}
return this;
}
}

View File

@@ -1,10 +1,13 @@
package com.dfsek.terra.registry;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.Debug;
import com.dfsek.terra.config.base.ConfigPack;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;
/**
* Class to hold config packs
@@ -35,5 +38,18 @@ public class ConfigRegistry extends TerraRegistry<ConfigPack> {
e.printStackTrace();
}
}
for(File zip : packsFolder.listFiles(file -> file.getName().endsWith(".zip") || file.getName().endsWith(".jar") || file.getName().endsWith(".terra"))) {
try {
Debug.info("Loading ZIP archive: " + zip.getName());
getRegistry().load(new ZipFile(zip));
} catch(IOException | ConfigException e) {
e.printStackTrace();
}
}
}
public void load(ZipFile file) throws ConfigException {
ConfigPack pack = new ConfigPack(file);
add(pack.getTemplate().getID(), pack);
}
}