mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
Working ZIP file loading
This commit is contained in:
@@ -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());
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user