mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-16 00:44:46 +01:00
"Autogenerate" data files with data fallbacks and moved keys folder into jar resources (#927)
* Autogenerate keys and data files * Update gacha html files Accidentally pushed with old html files * Keys no longer copied. No more manually retrieving listing files. Recursive directory creation Removed unused code from old GC as well. * Moved somethings and better errors * Fixed resources from loading twice * Data files fallback
This commit is contained in:
@@ -4,9 +4,14 @@ import emu.grasscutter.Grasscutter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class FileUtils {
|
||||
public static void write(String dest, byte[] bytes) {
|
||||
@@ -32,10 +37,34 @@ public final class FileUtils {
|
||||
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
public static InputStream readResourceAsStream(String resourcePath) {
|
||||
return Grasscutter.class.getResourceAsStream(resourcePath);
|
||||
}
|
||||
|
||||
public static byte[] readResource(String resourcePath) {
|
||||
try (InputStream is = Grasscutter.class.getResourceAsStream(resourcePath)) {
|
||||
return is.readAllBytes();
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().warn("Failed to read resource: " + resourcePath);
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
public static byte[] read(File file) {
|
||||
return read(file.getPath());
|
||||
}
|
||||
|
||||
public static void copyResource(String resourcePath, String destination) {
|
||||
try {
|
||||
byte[] resource = FileUtils.readResource(resourcePath);
|
||||
FileUtils.write(destination, resource);
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().warn("Failed to copy resource: " + resourcePath + "\n" + exception);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFilenameWithoutPath(String fileName) {
|
||||
if (fileName.indexOf(".") > 0) {
|
||||
@@ -44,4 +73,33 @@ public final class FileUtils {
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
|
||||
// From https://mkyong.com/java/java-read-a-file-from-resources-folder/
|
||||
public static List<Path> getPathsFromResource(String folder) throws URISyntaxException, IOException {
|
||||
List<Path> result;
|
||||
|
||||
// get path of the current running JAR
|
||||
String jarPath = Grasscutter.class.getProtectionDomain()
|
||||
.getCodeSource()
|
||||
.getLocation()
|
||||
.toURI()
|
||||
.getPath();
|
||||
|
||||
// file walks JAR
|
||||
URI uri = URI.create("jar:file:" + jarPath);
|
||||
try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
|
||||
result = Files.walk(fs.getPath(folder))
|
||||
.filter(Files::isRegularFile)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public static String readToString(InputStream file) throws IOException {
|
||||
byte[] content = file.readAllBytes();
|
||||
|
||||
return new String(content, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user