From 55541fa1da064b3be773e040c963e4d5f40c8d46 Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Wed, 20 Jul 2022 02:26:02 -0700 Subject: [PATCH] Add a `loadReader` helper function to DataLoader Also minor name changing on a few DataLoader methods --- .../java/emu/grasscutter/data/DataLoader.java | 30 +++++++++++++++---- .../emu/grasscutter/data/ResourceLoader.java | 4 +-- .../game/activity/ActivityManager.java | 5 ++-- .../game/combine/CombineManger.java | 2 +- .../emu/grasscutter/game/drop/DropSystem.java | 2 +- .../dungeons/challenge/DungeonChallenge.java | 2 +- .../game/expedition/ExpeditionSystem.java | 2 +- .../grasscutter/game/gacha/GachaSystem.java | 2 +- .../game/managers/AnnouncementSystem.java | 2 +- .../game/managers/energy/EnergyManager.java | 2 +- .../emu/grasscutter/game/shop/ShopSystem.java | 6 ++-- .../grasscutter/game/tower/TowerSystem.java | 2 +- .../game/world/WorldDataSystem.java | 5 ++-- .../java/emu/grasscutter/utils/Utils.java | 2 +- 14 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/main/java/emu/grasscutter/data/DataLoader.java b/src/main/java/emu/grasscutter/data/DataLoader.java index d03d3a612..5dee449ef 100644 --- a/src/main/java/emu/grasscutter/data/DataLoader.java +++ b/src/main/java/emu/grasscutter/data/DataLoader.java @@ -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 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"); diff --git a/src/main/java/emu/grasscutter/data/ResourceLoader.java b/src/main/java/emu/grasscutter/data/ResourceLoader.java index 5d93b7245..763fb7281 100644 --- a/src/main/java/emu/grasscutter/data/ResourceLoader.java +++ b/src/main/java/emu/grasscutter/data/ResourceLoader.java @@ -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 list = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(spawnDataEntries), type); + List list = Grasscutter.getGsonFactory().fromJson(reader, type); // Add spawns to group if it already exists in our spawn group map spawnEntryMap.addAll(list); diff --git a/src/main/java/emu/grasscutter/game/activity/ActivityManager.java b/src/main/java/emu/grasscutter/game/activity/ActivityManager.java index b3cdb024a..f1c86b812 100644 --- a/src/main/java/emu/grasscutter/game/activity/ActivityManager.java +++ b/src/main/java/emu/grasscutter/game/activity/ActivityManager.java @@ -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 activities = Grasscutter.getGsonFactory().fromJson( - isr, + reader, TypeToken.getParameterized(List.class, ActivityConfigItem.class).getType()); diff --git a/src/main/java/emu/grasscutter/game/combine/CombineManger.java b/src/main/java/emu/grasscutter/game/combine/CombineManger.java index 555440a85..0ec9750dc 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineManger.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineManger.java @@ -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 decomposeEntries = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ReliquaryDecomposeEntry.class).getType()); for (ReliquaryDecomposeEntry entry : decomposeEntries) { diff --git a/src/main/java/emu/grasscutter/game/drop/DropSystem.java b/src/main/java/emu/grasscutter/game/drop/DropSystem.java index 4e94bfe98..2917df436 100644 --- a/src/main/java/emu/grasscutter/game/drop/DropSystem.java +++ b/src/main/java/emu/grasscutter/game/drop/DropSystem.java @@ -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 banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DropInfo.class).getType()); if(banners.size() > 0) { diff --git a/src/main/java/emu/grasscutter/game/dungeons/challenge/DungeonChallenge.java b/src/main/java/emu/grasscutter/game/dungeons/challenge/DungeonChallenge.java index 392d40455..5093b8665 100644 --- a/src/main/java/emu/grasscutter/game/dungeons/challenge/DungeonChallenge.java +++ b/src/main/java/emu/grasscutter/game/dungeons/challenge/DungeonChallenge.java @@ -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 dungeonDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DungeonDrop.class).getType()); for (DungeonDrop entry : dungeonDropList) { diff --git a/src/main/java/emu/grasscutter/game/expedition/ExpeditionSystem.java b/src/main/java/emu/grasscutter/game/expedition/ExpeditionSystem.java index 9dba624a6..01c18cca0 100644 --- a/src/main/java/emu/grasscutter/game/expedition/ExpeditionSystem.java +++ b/src/main/java/emu/grasscutter/game/expedition/ExpeditionSystem.java @@ -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 banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ExpeditionRewardInfo.class).getType()); if(banners.size() > 0) { diff --git a/src/main/java/emu/grasscutter/game/gacha/GachaSystem.java b/src/main/java/emu/grasscutter/game/gacha/GachaSystem.java index 6365561eb..ac513a1e7 100644 --- a/src/main/java/emu/grasscutter/game/gacha/GachaSystem.java +++ b/src/main/java/emu/grasscutter/game/gacha/GachaSystem.java @@ -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 banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, GachaBanner.class).getType()); if(banners.size() > 0) { diff --git a/src/main/java/emu/grasscutter/game/managers/AnnouncementSystem.java b/src/main/java/emu/grasscutter/game/managers/AnnouncementSystem.java index 201079fa3..22f23ffcf 100644 --- a/src/main/java/emu/grasscutter/game/managers/AnnouncementSystem.java +++ b/src/main/java/emu/grasscutter/game/managers/AnnouncementSystem.java @@ -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 announceConfigItems = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(List.class, AnnounceConfigItem.class).getType()); diff --git a/src/main/java/emu/grasscutter/game/managers/energy/EnergyManager.java b/src/main/java/emu/grasscutter/game/managers/energy/EnergyManager.java index e688267d9..c509d85d1 100644 --- a/src/main/java/emu/grasscutter/game/managers/energy/EnergyManager.java +++ b/src/main/java/emu/grasscutter/game/managers/energy/EnergyManager.java @@ -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 energyDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, EnergyDropEntry.class).getType()); for (EnergyDropEntry entry : energyDropList) { diff --git a/src/main/java/emu/grasscutter/game/shop/ShopSystem.java b/src/main/java/emu/grasscutter/game/shop/ShopSystem.java index d6afb07ab..2169a06eb 100644 --- a/src/main/java/emu/grasscutter/game/shop/ShopSystem.java +++ b/src/main/java/emu/grasscutter/game/shop/ShopSystem.java @@ -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 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 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 shopChestBatchUseTableList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopChestBatchUseTable.class).getType()); if (shopChestBatchUseTableList.size() > 0) { diff --git a/src/main/java/emu/grasscutter/game/tower/TowerSystem.java b/src/main/java/emu/grasscutter/game/tower/TowerSystem.java index 206bab462..2b9b9e5af 100644 --- a/src/main/java/emu/grasscutter/game/tower/TowerSystem.java +++ b/src/main/java/emu/grasscutter/game/tower/TowerSystem.java @@ -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); diff --git a/src/main/java/emu/grasscutter/game/world/WorldDataSystem.java b/src/main/java/emu/grasscutter/game/world/WorldDataSystem.java index a4be78686..5ec06de79 100644 --- a/src/main/java/emu/grasscutter/game/world/WorldDataSystem.java +++ b/src/main/java/emu/grasscutter/game/world/WorldDataSystem.java @@ -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 = Grasscutter.getGsonFactory().fromJson( - isr, + reader, TypeToken.getParameterized(List.class, ChestReward.class).getType()); chestReward.forEach(reward -> diff --git a/src/main/java/emu/grasscutter/utils/Utils.java b/src/main/java/emu/grasscutter/utils/Utils.java index 1c86b3d12..f379def58 100644 --- a/src/main/java/emu/grasscutter/utils/Utils.java +++ b/src/main/java/emu/grasscutter/utils/Utils.java @@ -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); }