mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 01:15:52 +01:00
Allow loading Resources from zip files
Move Resources loading from String filenames to Paths Add zip support
This commit is contained in:
@@ -9,7 +9,6 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -17,7 +16,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
public final class FileUtils {
|
||||
public static void write(String dest, byte[] bytes) {
|
||||
Path path = Paths.get(dest);
|
||||
Path path = Path.of(dest);
|
||||
|
||||
try {
|
||||
Files.write(path, bytes);
|
||||
@@ -27,7 +26,7 @@ public final class FileUtils {
|
||||
}
|
||||
|
||||
public static byte[] read(String dest) {
|
||||
return read(Paths.get(dest));
|
||||
return read(Path.of(dest));
|
||||
}
|
||||
|
||||
public static byte[] read(Path path) {
|
||||
|
||||
@@ -3,7 +3,10 @@ package emu.grasscutter.utils;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -32,36 +35,57 @@ public final class JsonUtils {
|
||||
return gson.fromJson(jsonElement, classType);
|
||||
}
|
||||
|
||||
public static <T> T loadToClass(InputStreamReader fileReader, Class<T> classType) throws IOException {
|
||||
public static <T> T loadToClass(Reader fileReader, Class<T> classType) throws IOException {
|
||||
return gson.fromJson(fileReader, classType);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T> T loadToClass(String filename, Class<T> classType) throws IOException {
|
||||
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
|
||||
return loadToClass(fileReader, classType);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> loadToList(InputStreamReader fileReader, Class<T> classType) throws IOException {
|
||||
public static <T> T loadToClass(Path filename, Class<T> classType) throws IOException {
|
||||
try (var fileReader = Files.newBufferedReader(filename, StandardCharsets.UTF_8)) {
|
||||
return loadToClass(fileReader, classType);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> loadToList(Reader fileReader, Class<T> classType) throws IOException {
|
||||
return gson.fromJson(fileReader, TypeToken.getParameterized(List.class, classType).getType());
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T> List<T> loadToList(String filename, Class<T> classType) throws IOException {
|
||||
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
|
||||
return loadToList(fileReader, classType);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T1,T2> Map<T1,T2> loadToMap(InputStreamReader fileReader, Class<T1> keyType, Class<T2> valueType) throws IOException {
|
||||
public static <T> List<T> loadToList(Path filename, Class<T> classType) throws IOException {
|
||||
try (var fileReader = Files.newBufferedReader(filename, StandardCharsets.UTF_8)) {
|
||||
return loadToList(fileReader, classType);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T1,T2> Map<T1,T2> loadToMap(Reader fileReader, Class<T1> keyType, Class<T2> valueType) throws IOException {
|
||||
return gson.fromJson(fileReader, TypeToken.getParameterized(Map.class, keyType, valueType).getType());
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T1,T2> Map<T1,T2> loadToMap(String filename, Class<T1> keyType, Class<T2> valueType) throws IOException {
|
||||
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
|
||||
return loadToMap(fileReader, keyType, valueType);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T1,T2> Map<T1,T2> loadToMap(Path filename, Class<T1> keyType, Class<T2> valueType) throws IOException {
|
||||
try (var fileReader = Files.newBufferedReader(filename, StandardCharsets.UTF_8)) {
|
||||
return loadToMap(fileReader, keyType, valueType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely JSON decodes a given string.
|
||||
* @param jsonData The JSON-encoded data.
|
||||
|
||||
@@ -325,7 +325,7 @@ public final class Language {
|
||||
|
||||
private static Int2ObjectMap<String> loadTextMapFile(String language, IntSet nameHashes) {
|
||||
Int2ObjectMap<String> output = new Int2ObjectOpenHashMap<>();
|
||||
try (BufferedReader file = new BufferedReader(new FileReader(Utils.toFilePath(RESOURCE("TextMap/TextMap"+language+".json")), StandardCharsets.UTF_8))) {
|
||||
try (BufferedReader file = Files.newBufferedReader(getResourcePath("TextMap/TextMap"+language+".json"), StandardCharsets.UTF_8)) {
|
||||
Matcher matcher = textMapKeyValueRegex.matcher("");
|
||||
return new Int2ObjectOpenHashMap<>(
|
||||
file.lines()
|
||||
@@ -406,7 +406,7 @@ public final class Language {
|
||||
try {
|
||||
long cacheModified = Files.getLastModifiedTime(TEXTMAP_CACHE_PATH).toMillis();
|
||||
|
||||
long textmapsModified = Files.list(Path.of(RESOURCE("TextMap")))
|
||||
long textmapsModified = Files.list(getResourcePath("TextMap"))
|
||||
.filter(path -> path.toString().endsWith(".json"))
|
||||
.map(path -> {
|
||||
try {
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.slf4j.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.getResourcePath;
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
@SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"})
|
||||
@@ -169,19 +170,18 @@ public final class Utils {
|
||||
Logger logger = Grasscutter.getLogger();
|
||||
boolean exit = false;
|
||||
|
||||
String resourcesFolder = config.folderStructure.resources;
|
||||
String dataFolder = config.folderStructure.data;
|
||||
|
||||
// Check for resources folder.
|
||||
if (!fileExists(resourcesFolder)) {
|
||||
if (!Files.exists(getResourcePath(""))) {
|
||||
logger.info(translate("messages.status.create_resources"));
|
||||
logger.info(translate("messages.status.resources_error"));
|
||||
createFolder(resourcesFolder); exit = true;
|
||||
createFolder(config.folderStructure.resources); exit = true;
|
||||
}
|
||||
|
||||
// Check for BinOutput + ExcelBinOutput.
|
||||
if (!fileExists(resourcesFolder + "BinOutput") ||
|
||||
!fileExists(resourcesFolder + "ExcelBinOutput")) {
|
||||
if (!Files.exists(getResourcePath("BinOutput")) ||
|
||||
!Files.exists(getResourcePath("ExcelBinOutput"))) {
|
||||
logger.info(translate("messages.status.resources_error"));
|
||||
exit = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user