Commit 55541fa1 authored by Melledy's avatar Melledy
Browse files

Add a `loadReader` helper function to DataLoader

Also minor name changing on a few DataLoader methods
parent f22b92b0
......@@ -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");
......
......@@ -312,9 +312,9 @@ public class ResourceLoader {
for (String name : spawnDataNames) {
// Load spawn entries from file
try (InputStream spawnDataEntries = DataLoader.load(name)) {
try (InputStreamReader reader = DataLoader.loadReader(name)) {
Type type = TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType();
List<SpawnGroupEntry> list = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(spawnDataEntries), type);
List<SpawnGroupEntry> list = Grasscutter.getGsonFactory().fromJson(reader, type);
// Add spawns to group if it already exists in our spawn group map
spawnEntryMap.addAll(list);
......
......@@ -16,6 +16,7 @@ import org.reflections.Reflections;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
......@@ -44,9 +45,9 @@ public class ActivityManager extends BasePlayerManager {
activityWatcherTypeMap.put(typeName.value(), ConstructorAccess.get(item));
});
try(InputStream is = DataLoader.load("ActivityConfig.json"); InputStreamReader isr = new InputStreamReader(is)) {
try(Reader reader = DataLoader.loadReader("ActivityConfig.json")) {
List<ActivityConfigItem> activities = Grasscutter.getGsonFactory().fromJson(
isr,
reader,
TypeToken.getParameterized(List.class, ActivityConfigItem.class).getType());
......
......@@ -39,7 +39,7 @@ public class CombineManger extends BaseGameSystem {
public static void initialize() {
// Read the data we need for strongbox.
try (Reader fileReader = new InputStreamReader(DataLoader.load("ReliquaryDecompose.json"))) {
try (Reader fileReader = DataLoader.loadReader("ReliquaryDecompose.json")) {
List<ReliquaryDecomposeEntry> decomposeEntries = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ReliquaryDecomposeEntry.class).getType());
for (ReliquaryDecomposeEntry entry : decomposeEntries) {
......
......@@ -38,7 +38,7 @@ public class DropSystem extends BaseGameSystem {
}
public synchronized void load() {
try (Reader fileReader = new InputStreamReader(DataLoader.load("Drop.json"))) {
try (Reader fileReader = DataLoader.loadReader("Drop.json")) {
getDropData().clear();
List<DropInfo> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DropInfo.class).getType());
if(banners.size() > 0) {
......
......@@ -46,7 +46,7 @@ public class DungeonChallenge extends WorldChallenge {
public static void initialize() {
// Read the data we need for dungeon rewards drops.
try (Reader fileReader = new InputStreamReader(DataLoader.load("DungeonDrop.json"))) {
try (Reader fileReader = DataLoader.loadReader("DungeonDrop.json")) {
List<DungeonDrop> dungeonDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DungeonDrop.class).getType());
for (DungeonDrop entry : dungeonDropList) {
......
......@@ -30,7 +30,7 @@ public class ExpeditionSystem extends BaseGameSystem {
}
public synchronized void load() {
try (Reader fileReader = new InputStreamReader(DataLoader.load("ExpeditionReward.json"))) {
try (Reader fileReader = DataLoader.loadReader("ExpeditionReward.json")) {
getExpeditionRewardDataList().clear();
List<ExpeditionRewardInfo> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ExpeditionRewardInfo.class).getType());
if(banners.size() > 0) {
......
......@@ -76,7 +76,7 @@ public class GachaSystem extends BaseGameSystem {
}
public synchronized void load() {
try (Reader fileReader = new InputStreamReader(DataLoader.load("Banners.json"))) {
try (Reader fileReader = DataLoader.loadReader("Banners.json")) {
getGachaBanners().clear();
List<GachaBanner> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, GachaBanner.class).getType());
if(banners.size() > 0) {
......
......@@ -30,7 +30,7 @@ public class AnnouncementSystem extends BaseGameSystem {
}
private int loadConfig() {
try (var fileReader = new InputStreamReader(DataLoader.load("Announcement.json"))) {
try (var fileReader = DataLoader.loadReader("Announcement.json")) {
List<AnnounceConfigItem> announceConfigItems = Grasscutter.getGsonFactory().fromJson(fileReader,
TypeToken.getParameterized(List.class, AnnounceConfigItem.class).getType());
......
......@@ -62,7 +62,7 @@ public class EnergyManager extends BasePlayerManager {
public static void initialize() {
// Read the data we need for monster energy drops.
try (Reader fileReader = new InputStreamReader(DataLoader.load("EnergyDrop.json"))) {
try (Reader fileReader = DataLoader.loadReader("EnergyDrop.json")) {
List<EnergyDropEntry> energyDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, EnergyDropEntry.class).getType());
for (EnergyDropEntry entry : energyDropList) {
......
......@@ -60,7 +60,7 @@ public class ShopSystem extends BaseGameSystem {
}
private void loadShop() {
try (Reader fileReader = new InputStreamReader(DataLoader.load("Shop.json"))) {
try (Reader fileReader = DataLoader.loadReader("Shop.json")) {
getShopData().clear();
List<ShopTable> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopTable.class).getType());
if(banners.size() > 0) {
......@@ -104,7 +104,7 @@ public class ShopSystem extends BaseGameSystem {
}
private void loadShopChest() {
try (Reader fileReader = new InputStreamReader(DataLoader.load("ShopChest.json"))) {
try (Reader fileReader = DataLoader.loadReader("ShopChest.json")) {
getShopChestData().clear();
List<ShopChestTable> shopChestTableList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopChestTable.class).getType());
if (shopChestTableList.size() > 0) {
......@@ -119,7 +119,7 @@ public class ShopSystem extends BaseGameSystem {
}
private void loadShopChestBatchUse() {
try (Reader fileReader = new InputStreamReader(DataLoader.load("ShopChestBatchUse.json"))) {
try (Reader fileReader = DataLoader.loadReader("ShopChestBatchUse.json")) {
getShopChestBatchUseData().clear();
List<ShopChestBatchUseTable> shopChestBatchUseTableList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopChestBatchUseTable.class).getType());
if (shopChestBatchUseTableList.size() > 0) {
......
......@@ -25,7 +25,7 @@ public class TowerSystem extends BaseGameSystem {
private TowerScheduleConfig towerScheduleConfig;
public synchronized void load(){
try (Reader fileReader = new InputStreamReader(DataLoader.load("TowerSchedule.json"))) {
try (Reader fileReader = DataLoader.loadReader("TowerSchedule.json")) {
towerScheduleConfig = Grasscutter.getGsonFactory().fromJson(fileReader, TowerScheduleConfig.class);
} catch (Exception e) {
Grasscutter.getLogger().error("Unable to load tower schedule config.", e);
......
......@@ -19,6 +19,7 @@ import emu.grasscutter.server.game.GameServer;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -41,9 +42,9 @@ public class WorldDataSystem extends BaseGameSystem {
// set the special chest first
chestInteractHandlerMap.put("SceneObj_Chest_Flora", new BossChestInteractHandler());
try(InputStream is = DataLoader.load("ChestReward.json"); InputStreamReader isr = new InputStreamReader(is)) {
try(Reader reader = DataLoader.loadReader("ChestReward.json")) {
List<ChestReward> chestReward = Grasscutter.getGsonFactory().fromJson(
isr,
reader,
TypeToken.getParameterized(List.class, ChestReward.class).getType());
chestReward.forEach(reward ->
......
......@@ -191,7 +191,7 @@ public final class Utils {
createFolder(dataFolder);
// Make sure the data folder is populated, if there are any missing files copy them from resources
DataLoader.CheckAllFiles();
DataLoader.checkAllFiles();
if(exit) System.exit(1);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment