Add a loadReader helper function to DataLoader

Also minor name changing on a few DataLoader methods
This commit is contained in:
Melledy
2022-07-20 02:26:02 -07:00
parent f22b92b08f
commit 55541fa1da
14 changed files with 45 additions and 23 deletions

View File

@@ -8,7 +8,9 @@ import emu.grasscutter.utils.Utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.List;
@@ -27,6 +29,24 @@ public class DataLoader {
public static InputStream load(String resourcePath) throws FileNotFoundException {
return load(resourcePath, true);
}
/**
* Creates an input stream reader for a data file. If the file isn't found within the /data directory then it will fallback to the default within the jar resources
*
* @param resourcePath The path to the data file to be loaded.
* @return InputStreamReader of the data file.
* @throws IOException
* @throws FileNotFoundException
* @see #load(String, boolean)
*/
public static InputStreamReader loadReader(String resourcePath) throws IOException, FileNotFoundException {
try {
InputStream is = load(resourcePath, true);
return new InputStreamReader(is);
} catch (FileNotFoundException exception) {
throw exception;
}
}
/**
* Load a data file by its name.
@@ -49,7 +69,7 @@ public class DataLoader {
return null;
}
public static void CheckAllFiles() {
public static void checkAllFiles() {
try {
List<Path> filenames = FileUtils.getPathsFromResource("/defaults/data/");
@@ -58,16 +78,16 @@ public class DataLoader {
} else for (Path file : filenames) {
String relativePath = String.valueOf(file).split("defaults[\\\\\\/]data[\\\\\\/]")[1];
CheckAndCopyData(relativePath);
checkAndCopyData(relativePath);
}
} catch (Exception e) {
Grasscutter.getLogger().error("An error occurred while trying to check the data folder.", e);
}
GenerateGachaMappings();
generateGachaMappings();
}
private static void CheckAndCopyData(String name) {
private static void checkAndCopyData(String name) {
String filePath = Utils.toFilePath(DATA(name));
if (!Utils.fileExists(filePath)) {
@@ -93,7 +113,7 @@ public class DataLoader {
}
}
private static void GenerateGachaMappings() {
private static void generateGachaMappings() {
if (!Utils.fileExists(GachaHandler.gachaMappings)) {
try {
Grasscutter.getLogger().info("Creating default '" + GachaHandler.gachaMappings + "' data");