Allow loading Resources from zip files

Move Resources loading from String filenames to Paths
Add zip support
This commit is contained in:
AnimeGitB
2022-09-23 18:10:46 +09:30
parent a90455a7a4
commit fbc0219cba
9 changed files with 305 additions and 286 deletions

View File

@@ -1,7 +1,15 @@
package emu.grasscutter.config;
import java.util.Locale;
import java.util.stream.Stream;
import emu.grasscutter.Grasscutter;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static emu.grasscutter.Grasscutter.config;
@@ -30,6 +38,42 @@ public final class Configuration extends ConfigContainer {
private static final String PLUGINS_FOLDER = config.folderStructure.plugins;
private static final String SCRIPTS_FOLDER = config.folderStructure.scripts;
private static final String PACKETS_FOLDER = config.folderStructure.packets;
private static final FileSystem RESOURCES_FILE_SYSTEM; // Not sure about lifetime rules on this one, might be safe to remove
private static final Path RESOURCES_PATH;
static {
FileSystem fs = null;
Path path = Path.of(RESOURCES_FOLDER);
if (RESOURCES_FOLDER.endsWith(".zip")) { // Would be nice to support .tar.gz too at some point, but it doesn't come for free in Java
try {
fs = FileSystems.newFileSystem(path);
} catch (IOException e) {
Grasscutter.getLogger().error("Failed to load resources zip \"" + RESOURCES_FOLDER + "\"");
}
}
if (fs != null) {
var root = fs.getPath("");
try (Stream<Path> pathStream = java.nio.file.Files.find(root, 3, (p, a) -> {
var filename = p.getFileName();
if (filename == null) return false;
return filename.toString().equals("ExcelBinOutput");
})) {
var excelBinOutput = pathStream.findFirst();
if (excelBinOutput.isPresent()) {
path = excelBinOutput.get().getParent();
if (path == null)
path = root;
Grasscutter.getLogger().debug("Resources will be loaded from \"" + RESOURCES_FOLDER + "/" + path.toString() + "\"");
} else {
Grasscutter.getLogger().error("Failed to find ExcelBinOutput in resources zip \"" + RESOURCES_FOLDER + "\"");
}
} catch (IOException e) {
Grasscutter.getLogger().error("Failed to scan resources zip \"" + RESOURCES_FOLDER + "\"");
}
}
RESOURCES_FILE_SYSTEM = fs;
RESOURCES_PATH = path;
};
public static final Server SERVER = config.server;
public static final Database DATABASE = config.databaseInfo;
@@ -54,11 +98,15 @@ public final class Configuration extends ConfigContainer {
}
public static String DATA(String path) {
return Paths.get(DATA_FOLDER, path).toString();
return Path.of(DATA_FOLDER, path).toString();
}
public static Path getResourcePath(String path) {
return RESOURCES_PATH.resolve(path);
}
public static String RESOURCE(String path) {
return Paths.get(RESOURCES_FOLDER, path).toString();
return getResourcePath(path).toString();
}
public static String PLUGIN() {
@@ -66,15 +114,15 @@ public final class Configuration extends ConfigContainer {
}
public static String PLUGIN(String path) {
return Paths.get(PLUGINS_FOLDER, path).toString();
return Path.of(PLUGINS_FOLDER, path).toString();
}
public static String SCRIPT(String path) {
return Paths.get(SCRIPTS_FOLDER, path).toString();
return Path.of(SCRIPTS_FOLDER, path).toString();
}
public static String PACKET(String path) {
return Paths.get(PACKETS_FOLDER, path).toString();
return Path.of(PACKETS_FOLDER, path).toString();
}
/**