Commit c6323e97 authored by AnimeGitB's avatar AnimeGitB Committed by Luke H-W
Browse files

Funnel all gson calls into helper functions

Add deprecated getGsonFactory for plugin compat until 3.0
parent 76fcbb47
...@@ -3,7 +3,6 @@ package emu.grasscutter; ...@@ -3,7 +3,6 @@ package emu.grasscutter;
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.auth.DefaultAuthentication;
...@@ -56,7 +55,6 @@ public final class Grasscutter { ...@@ -56,7 +55,6 @@ public final class Grasscutter {
private static Language language; private static Language language;
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static final File configFile = new File("./config.json"); public static final File configFile = new File("./config.json");
private static int day; // Current day of week. private static int day; // Current day of week.
...@@ -203,8 +201,8 @@ public final class Grasscutter { ...@@ -203,8 +201,8 @@ public final class Grasscutter {
} }
// If the file already exists, we attempt to load it. // If the file already exists, we attempt to load it.
try (FileReader file = new FileReader(configFile)) { try {
config = gson.fromJson(file, ConfigContainer.class); config = Utils.loadJsonToClass(configFile.getPath(), ConfigContainer.class);
} catch (Exception exception) { } catch (Exception exception) {
getLogger().error("There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json."); getLogger().error("There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
System.exit(1); System.exit(1);
...@@ -220,7 +218,7 @@ public final class Grasscutter { ...@@ -220,7 +218,7 @@ public final class Grasscutter {
if (config == null) config = new ConfigContainer(); if (config == null) config = new ConfigContainer();
try (FileWriter file = new FileWriter(configFile)) { try (FileWriter file = new FileWriter(configFile)) {
file.write(gson.toJson(config)); file.write(Utils.jsonEncode(config));
} catch (IOException ignored) { } catch (IOException ignored) {
Grasscutter.getLogger().error("Unable to write to config file."); Grasscutter.getLogger().error("Unable to write to config file.");
} catch (Exception e) { } catch (Exception e) {
...@@ -272,8 +270,9 @@ public final class Grasscutter { ...@@ -272,8 +270,9 @@ public final class Grasscutter {
return consoleLineReader; return consoleLineReader;
} }
@Deprecated(forRemoval = true)
public static Gson getGsonFactory() { public static Gson getGsonFactory() {
return gson; return Utils.getGsonFactory();
} }
public static HttpServer getHttpServer() { public static HttpServer getHttpServer() {
......
...@@ -4,10 +4,9 @@ import com.google.gson.JsonObject; ...@@ -4,10 +4,9 @@ import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerDebugMode; import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.Grasscutter.ServerRunMode; import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.utils.Utils;
import java.nio.charset.StandardCharsets;
import java.util.Set; import java.util.Set;
import java.io.FileReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
...@@ -27,8 +26,7 @@ public class ConfigContainer { ...@@ -27,8 +26,7 @@ public class ConfigContainer {
*/ */
public static void updateConfig() { public static void updateConfig() {
try { // Check if the server is using a legacy config. try { // Check if the server is using a legacy config.
JsonObject configObject = Grasscutter.getGsonFactory() JsonObject configObject = Utils.loadJsonToClass(Grasscutter.configFile.getPath(), JsonObject.class);
.fromJson(new FileReader(Grasscutter.configFile, StandardCharsets.UTF_8), JsonObject.class);
if (!configObject.has("version")) { if (!configObject.has("version")) {
Grasscutter.getLogger().info("Updating legacy .."); Grasscutter.getLogger().info("Updating legacy ..");
Grasscutter.saveConfig(null); Grasscutter.saveConfig(null);
......
...@@ -15,6 +15,7 @@ import java.io.InputStream; ...@@ -15,6 +15,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Map;
public class DataLoader { public class DataLoader {
...@@ -69,6 +70,24 @@ public class DataLoader { ...@@ -69,6 +70,24 @@ public class DataLoader {
return null; return null;
} }
public static <T> T loadClass(String resourcePath, Class<T> classType) throws IOException {
try (InputStreamReader reader = loadReader(resourcePath)) {
return Utils.loadJsonToClass(reader, classType);
}
}
public static <T> List<T> loadList(String resourcePath, Class<T> classType) throws IOException {
try (InputStreamReader reader = loadReader(resourcePath)) {
return Utils.loadJsonToList(reader, classType);
}
}
public static <T1,T2> Map<T1,T2> loadMap(String resourcePath, Class<T1> keyType, Class<T2> valueType) throws IOException {
try (InputStreamReader reader = loadReader(resourcePath)) {
return Utils.loadJsonToMap(reader, keyType, valueType);
}
}
public static void checkAllFiles() { public static void checkAllFiles() {
try { try {
List<Path> filenames = FileUtils.getPathsFromResource("/defaults/data/"); List<Path> filenames = FileUtils.getPathsFromResource("/defaults/data/");
......
...@@ -2,7 +2,6 @@ package emu.grasscutter.data; ...@@ -2,7 +2,6 @@ package emu.grasscutter.data;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.binout.*; import emu.grasscutter.data.binout.*;
import emu.grasscutter.data.binout.AbilityModifier.AbilityConfigData; import emu.grasscutter.data.binout.AbilityModifier.AbilityConfigData;
...@@ -21,7 +20,6 @@ import lombok.SneakyThrows; ...@@ -21,7 +20,6 @@ import lombok.SneakyThrows;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.io.*; import java.io.*;
import java.lang.reflect.Type;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
...@@ -29,6 +27,7 @@ import java.util.Map.Entry; ...@@ -29,6 +27,7 @@ import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static emu.grasscutter.config.Configuration.DATA;
import static emu.grasscutter.config.Configuration.RESOURCE; import static emu.grasscutter.config.Configuration.RESOURCE;
import static emu.grasscutter.utils.Language.translate; import static emu.grasscutter.utils.Language.translate;
...@@ -119,15 +118,13 @@ public class ResourceLoader { ...@@ -119,15 +118,13 @@ public class ResourceLoader {
} }
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
protected static void loadFromResource(Class<?> c, String fileName, Int2ObjectMap map) throws Exception { protected static <T> void loadFromResource(Class<T> c, String fileName, Int2ObjectMap map) throws Exception {
try (FileReader fileReader = new FileReader(RESOURCE("ExcelBinOutput/" + fileName))) { List<T> list = Utils.loadJsonToList(RESOURCE("ExcelBinOutput/" + fileName), c);
List list = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, c).getType());
for (T o : list) {
for (Object o : list) { GameResource res = (GameResource) o;
GameResource res = (GameResource) o; res.onLoad();
res.onLoad(); map.put(res.getId(), res);
map.put(res.getId(), res);
}
} }
} }
...@@ -140,7 +137,6 @@ public class ResourceLoader { ...@@ -140,7 +137,6 @@ public class ResourceLoader {
return; return;
} }
List<ScenePointEntry> scenePointList = new ArrayList<>();
for (File file : Objects.requireNonNull(folder.listFiles())) { for (File file : Objects.requireNonNull(folder.listFiles())) {
ScenePointConfig config; ScenePointConfig config;
Integer sceneId; Integer sceneId;
...@@ -152,8 +148,8 @@ public class ResourceLoader { ...@@ -152,8 +148,8 @@ public class ResourceLoader {
continue; continue;
} }
try (FileReader fileReader = new FileReader(file)) { try {
config = Grasscutter.getGsonFactory().fromJson(fileReader, ScenePointConfig.class); config = Utils.loadJsonToClass(file.getPath(), ScenePointConfig.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
...@@ -163,22 +159,20 @@ public class ResourceLoader { ...@@ -163,22 +159,20 @@ public class ResourceLoader {
continue; continue;
} }
List<Integer> scenePoints = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : config.points.entrySet()) { for (Map.Entry<String, JsonElement> entry : config.points.entrySet()) {
PointData pointData = Grasscutter.getGsonFactory().fromJson(entry.getValue(), PointData.class); int id = Integer.parseInt(entry.getKey());
pointData.setId(Integer.parseInt(entry.getKey())); String name = sceneId + "_" + entry.getKey();
PointData pointData = Utils.jsonDecode(entry.getValue(), PointData.class);
pointData.setId(id);
ScenePointEntry sl = new ScenePointEntry(sceneId + "_" + entry.getKey(), pointData); GameData.getScenePointIdList().add(id);
scenePointList.add(sl); GameData.getScenePointEntries().put(name, new ScenePointEntry(name, pointData));
GameData.getScenePointIdList().add(pointData.getId()); scenePoints.add(id);
pointData.updateDailyDungeon(); pointData.updateDailyDungeon();
} }
GameData.getScenePointsPerScene().put(sceneId, scenePoints);
GameData.getScenePointsPerScene().put(sceneId, new ArrayList<>());
for (ScenePointEntry entry : scenePointList) {
GameData.getScenePointEntries().put(entry.getName(), entry);
GameData.getScenePointsPerScene().get(sceneId).add(entry.getPointData().getId());
}
} }
} }
...@@ -186,15 +180,15 @@ public class ResourceLoader { ...@@ -186,15 +180,15 @@ public class ResourceLoader {
List<AbilityEmbryoEntry> embryoList = null; List<AbilityEmbryoEntry> embryoList = null;
// Read from cached file if exists // Read from cached file if exists
try (InputStream embryoCache = DataLoader.load("AbilityEmbryos.json", false)) { try {
embryoList = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(embryoCache), TypeToken.getParameterized(Collection.class, AbilityEmbryoEntry.class).getType()); embryoList = Utils.loadJsonToList(DATA("AbilityEmbryos.json"), AbilityEmbryoEntry.class);
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (embryoList == null) { if (embryoList == null) {
// Load from BinOutput // Load from BinOutput
Pattern pattern = Pattern.compile("(?<=ConfigAvatar_)(.*?)(?=.json)"); Pattern pattern = Pattern.compile("(?<=ConfigAvatar_)(.*?)(?=.json)");
embryoList = new LinkedList<>(); embryoList = new ArrayList<>();
File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Avatar/"))); File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Avatar/")));
File[] files = folder.listFiles(); File[] files = folder.listFiles();
if (files == null) { if (files == null) {
...@@ -213,8 +207,8 @@ public class ResourceLoader { ...@@ -213,8 +207,8 @@ public class ResourceLoader {
continue; continue;
} }
try (FileReader fileReader = new FileReader(file)) { try {
config = Grasscutter.getGsonFactory().fromJson(fileReader, AvatarConfig.class); config = Utils.loadJsonToClass(file.getPath(), AvatarConfig.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
...@@ -229,14 +223,10 @@ public class ResourceLoader { ...@@ -229,14 +223,10 @@ public class ResourceLoader {
embryoList.add(al); embryoList.add(al);
} }
File playerElementsFile = new File(Utils.toFilePath(RESOURCE("BinOutput/AbilityGroup/AbilityGroup_Other_PlayerElementAbility.json"))); try {
GameDepot.setPlayerAbilities(Utils.loadJsonToMap(RESOURCE("BinOutput/AbilityGroup/AbilityGroup_Other_PlayerElementAbility.json"), String.class, AvatarConfig.class));
if (playerElementsFile.exists()) { } catch (Exception e) {
try (FileReader fileReader = new FileReader(playerElementsFile)) { e.printStackTrace();
GameDepot.setPlayerAbilities(Grasscutter.getGsonFactory().fromJson(fileReader, new TypeToken<Map<String, AvatarConfig>>(){}.getType()));
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
...@@ -262,8 +252,8 @@ public class ResourceLoader { ...@@ -262,8 +252,8 @@ public class ResourceLoader {
for (File file : files) { for (File file : files) {
List<AbilityConfigData> abilityConfigList; List<AbilityConfigData> abilityConfigList;
try (FileReader fileReader = new FileReader(file)) { try {
abilityConfigList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, AbilityConfigData.class).getType()); abilityConfigList = Utils.loadJsonToList(file.getPath(), AbilityConfigData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
...@@ -320,11 +310,8 @@ public class ResourceLoader { ...@@ -320,11 +310,8 @@ public class ResourceLoader {
for (String name : spawnDataNames) { for (String name : spawnDataNames) {
// Load spawn entries from file // Load spawn entries from file
try (InputStreamReader reader = DataLoader.loadReader(name)) { try (InputStreamReader reader = DataLoader.loadReader(name)) {
Type type = TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType();
List<SpawnGroupEntry> list = Grasscutter.getGsonFactory().fromJson(reader, type);
// Add spawns to group if it already exists in our spawn group map // Add spawns to group if it already exists in our spawn group map
spawnEntryMap.addAll(list); spawnEntryMap.addAll(Utils.loadJsonToList(reader, SpawnGroupEntry.class));
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
...@@ -354,13 +341,12 @@ public class ResourceLoader { ...@@ -354,13 +341,12 @@ public class ResourceLoader {
// Read from cached file if exists // Read from cached file if exists
List<OpenConfigEntry> list = null; List<OpenConfigEntry> list = null;
try (InputStream openConfigCache = DataLoader.load("OpenConfig.json", false)) { try {
list = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(openConfigCache), TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType()); list = Utils.loadJsonToList(DATA("OpenConfig.json"), OpenConfigEntry.class);
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (list == null) { if (list == null) {
Map<String, OpenConfigEntry> map = new TreeMap<>(); Map<String, OpenConfigEntry> map = new TreeMap<>();
java.lang.reflect.Type type = new TypeToken<Map<String, OpenConfigData[]>>() {}.getType();
String[] folderNames = {"BinOutput/Talent/EquipTalents/", "BinOutput/Talent/AvatarTalents/"}; String[] folderNames = {"BinOutput/Talent/EquipTalents/", "BinOutput/Talent/AvatarTalents/"};
for (String name : folderNames) { for (String name : folderNames) {
...@@ -374,11 +360,10 @@ public class ResourceLoader { ...@@ -374,11 +360,10 @@ public class ResourceLoader {
if (!file.getName().endsWith(".json")) { if (!file.getName().endsWith(".json")) {
continue; continue;
} }
Map<String, OpenConfigData[]> config; Map<String, OpenConfigData[]> config;
try (FileReader fileReader = new FileReader(file)) { try {
config = Grasscutter.getGsonFactory().fromJson(fileReader, type); config = Utils.loadJsonToMap(file.getPath(), String.class, OpenConfigData[].class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
...@@ -414,8 +399,8 @@ public class ResourceLoader { ...@@ -414,8 +399,8 @@ public class ResourceLoader {
for (File file : folder.listFiles()) { for (File file : folder.listFiles()) {
MainQuestData mainQuest = null; MainQuestData mainQuest = null;
try (FileReader fileReader = new FileReader(file)) { try {
mainQuest = Grasscutter.getGsonFactory().fromJson(fileReader, MainQuestData.class); mainQuest = Utils.loadJsonToClass(file.getPath(), MainQuestData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
...@@ -424,16 +409,14 @@ public class ResourceLoader { ...@@ -424,16 +409,14 @@ public class ResourceLoader {
GameData.getMainQuestDataMap().put(mainQuest.getId(), mainQuest); GameData.getMainQuestDataMap().put(mainQuest.getId(), mainQuest);
} }
try (Reader reader = new FileReader(RESOURCE("QuestEncryptionKeys.json"))) { try {
List<QuestEncryptionKey> keys = Grasscutter.getGsonFactory().fromJson( List<QuestEncryptionKey> keys = DataLoader.loadList("QuestEncryptionKeys.json", QuestEncryptionKey.class);
reader,
TypeToken.getParameterized(List.class, QuestEncryptionKey.class).getType());
Int2ObjectMap<QuestEncryptionKey> questEncryptionMap = GameData.getMainQuestEncryptionMap(); Int2ObjectMap<QuestEncryptionKey> questEncryptionMap = GameData.getMainQuestEncryptionMap();
keys.forEach(key -> questEncryptionMap.put(key.getMainQuestId(), key)); keys.forEach(key -> questEncryptionMap.put(key.getMainQuestId(), key));
Grasscutter.getLogger().debug("Loaded {} quest keys.", questEncryptionMap.size()); Grasscutter.getLogger().debug("Loaded {} quest keys.", questEncryptionMap.size());
} catch (FileNotFoundException ignored) { } catch (FileNotFoundException | NullPointerException ignored) {
Grasscutter.getLogger().error("Unable to load quest keys - ./resources/QuestEncryptionKeys.json not found."); Grasscutter.getLogger().warn("Unable to load quest keys - ./resources/QuestEncryptionKeys.json not found.");
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().error("Unable to load quest keys.", e); Grasscutter.getLogger().error("Unable to load quest keys.", e);
} }
...@@ -450,8 +433,8 @@ public class ResourceLoader { ...@@ -450,8 +433,8 @@ public class ResourceLoader {
for (File file : folder.listFiles()) { for (File file : folder.listFiles()) {
ScriptSceneData sceneData; ScriptSceneData sceneData;
try (FileReader fileReader = new FileReader(file)) { try {
sceneData = Grasscutter.getGsonFactory().fromJson(fileReader, ScriptSceneData.class); sceneData = Utils.loadJsonToClass(file.getPath(), ScriptSceneData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
...@@ -463,43 +446,41 @@ public class ResourceLoader { ...@@ -463,43 +446,41 @@ public class ResourceLoader {
Grasscutter.getLogger().debug("Loaded " + GameData.getScriptSceneDataMap().size() + " ScriptSceneDatas."); Grasscutter.getLogger().debug("Loaded " + GameData.getScriptSceneDataMap().size() + " ScriptSceneDatas.");
} }
@SneakyThrows @SneakyThrows
private static void loadHomeworldDefaultSaveData(){ private static void loadHomeworldDefaultSaveData() {
var folder = Files.list(Path.of(RESOURCE("BinOutput/HomeworldDefaultSave"))).toList(); var pattern = Pattern.compile("scene(.*)_home_config.json");
var pattern = Pattern.compile("scene(.*)_home_config.json"); Files.list(Path.of(RESOURCE("BinOutput/HomeworldDefaultSave"))).forEach(file -> {
String filename = file.getFileName().toString();
for (var file : folder) { var matcher = pattern.matcher(filename);
var matcher = pattern.matcher(file.getFileName().toString());
if (!matcher.find()) { if (!matcher.find()) {
continue; return;
} }
var sceneId = matcher.group(1); try {
var sceneId = Integer.parseInt(matcher.group(1));
var data = Grasscutter.getGsonFactory().fromJson(Files.readString(file), HomeworldDefaultSaveData.class); var data = Utils.loadJsonToClass(filename, HomeworldDefaultSaveData.class);
GameData.getHomeworldDefaultSaveData().put(sceneId, data);
GameData.getHomeworldDefaultSaveData().put(Integer.parseInt(sceneId), data); } catch (Exception ignored) {}
} });
Grasscutter.getLogger().debug("Loaded " + GameData.getHomeworldDefaultSaveData().size() + " HomeworldDefaultSaveDatas."); Grasscutter.getLogger().debug("Loaded " + GameData.getHomeworldDefaultSaveData().size() + " HomeworldDefaultSaveDatas.");
} }
@SneakyThrows @SneakyThrows
private static void loadNpcBornData() { private static void loadNpcBornData() {
var folder = Files.list(Path.of(RESOURCE("BinOutput/Scene/SceneNpcBorn"))).toList(); Files.list(Path.of(RESOURCE("BinOutput/Scene/SceneNpcBorn"))).forEach(file -> {
for (var file : folder) {
if (file.toFile().isDirectory()) { if (file.toFile().isDirectory()) {
continue; return;
}
var data = Grasscutter.getGsonFactory().fromJson(Files.readString(file), SceneNpcBornData.class);
if (data.getBornPosList() == null || data.getBornPosList().size() == 0) {
continue;
} }
try {
var data = Utils.loadJsonToClass(file.getFileName().toString(), SceneNpcBornData.class);
if (data.getBornPosList() == null || data.getBornPosList().size() == 0) {
return;
}
data.setIndex(SceneIndexManager.buildIndex(3, data.getBornPosList(), item -> item.getPos().toPoint())); data.setIndex(SceneIndexManager.buildIndex(3, data.getBornPosList(), item -> item.getPos().toPoint()));
GameData.getSceneNpcBornData().put(data.getSceneId(), data); GameData.getSceneNpcBornData().put(data.getSceneId(), data);
} } catch (Exception ignored) {}
});
Grasscutter.getLogger().debug("Loaded " + GameData.getSceneNpcBornData().size() + " SceneNpcBornDatas."); Grasscutter.getLogger().debug("Loaded " + GameData.getSceneNpcBornData().size() + " SceneNpcBornDatas.");
} }
......
package emu.grasscutter.game.activity; package emu.grasscutter.game.activity;
import com.esotericsoftware.reflectasm.ConstructorAccess; import com.esotericsoftware.reflectasm.ConstructorAccess;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
...@@ -14,9 +13,6 @@ import emu.grasscutter.server.packet.send.PacketActivityScheduleInfoNotify; ...@@ -14,9 +13,6 @@ import emu.grasscutter.server.packet.send.PacketActivityScheduleInfoNotify;
import lombok.Getter; import lombok.Getter;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -45,13 +41,8 @@ public class ActivityManager extends BasePlayerManager { ...@@ -45,13 +41,8 @@ public class ActivityManager extends BasePlayerManager {
activityWatcherTypeMap.put(typeName.value(), ConstructorAccess.get(item)); activityWatcherTypeMap.put(typeName.value(), ConstructorAccess.get(item));
}); });
try (Reader reader = DataLoader.loadReader("ActivityConfig.json")) { try {
List<ActivityConfigItem> activities = Grasscutter.getGsonFactory().fromJson( DataLoader.loadList("ActivityConfig.json", ActivityConfigItem.class).forEach(item -> {
reader,
TypeToken.getParameterized(List.class, ActivityConfigItem.class).getType());
activities.forEach(item -> {
var activityData = GameData.getActivityDataMap().get(item.getActivityId()); var activityData = GameData.getActivityDataMap().get(item.getActivityId());
if (activityData == null) { if (activityData == null) {
Grasscutter.getLogger().warn("activity {} not exist.", item.getActivityId()); Grasscutter.getLogger().warn("activity {} not exist.", item.getActivityId());
......
...@@ -13,6 +13,7 @@ import emu.grasscutter.game.player.Player; ...@@ -13,6 +13,7 @@ import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.net.proto.ActivityWatcherInfoOuterClass; import emu.grasscutter.net.proto.ActivityWatcherInfoOuterClass;
import emu.grasscutter.server.packet.send.PacketActivityUpdateWatcherNotify; import emu.grasscutter.server.packet.send.PacketActivityUpdateWatcherNotify;
import emu.grasscutter.utils.Utils;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
...@@ -68,7 +69,7 @@ public class PlayerActivityData { ...@@ -68,7 +69,7 @@ public class PlayerActivityData {
} }
public void setDetail(Object detail){ public void setDetail(Object detail){
this.detail = Grasscutter.getGsonFactory().toJson(detail); this.detail = Utils.jsonEncode(detail);
} }
public void takeWatcherReward(int watcherId) { public void takeWatcherReward(int watcherId) {
......
...@@ -8,6 +8,7 @@ import emu.grasscutter.game.props.ActivityType; ...@@ -8,6 +8,7 @@ import emu.grasscutter.game.props.ActivityType;
import emu.grasscutter.net.proto.ActivityInfoOuterClass; import emu.grasscutter.net.proto.ActivityInfoOuterClass;
import emu.grasscutter.net.proto.MusicBriefInfoOuterClass; import emu.grasscutter.net.proto.MusicBriefInfoOuterClass;
import emu.grasscutter.net.proto.MusicGameActivityDetailInfoOuterClass; import emu.grasscutter.net.proto.MusicGameActivityDetailInfoOuterClass;
import emu.grasscutter.utils.Utils;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -47,8 +48,7 @@ public class MusicGameActivityHandler extends ActivityHandler { ...@@ -47,8 +48,7 @@ public class MusicGameActivityHandler extends ActivityHandler {
playerActivityData.save(); playerActivityData.save();
} }
return Grasscutter.getGsonFactory().fromJson(playerActivityData.getDetail(), return Utils.jsonDecode(playerActivityData.getDetail(), MusicGamePlayerData.class);
MusicGamePlayerData.class);
} }
public boolean setMusicGameRecord(PlayerActivityData playerActivityData, MusicGamePlayerData.MusicGameRecord newRecord) { public boolean setMusicGameRecord(PlayerActivityData playerActivityData, MusicGamePlayerData.MusicGameRecord newRecord) {
......
...@@ -6,8 +6,6 @@ import emu.grasscutter.data.GameData; ...@@ -6,8 +6,6 @@ import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData; import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.excels.CombineData; import emu.grasscutter.data.excels.CombineData;
import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.inventory.Inventory;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.ItemUseOp; import emu.grasscutter.game.props.ItemUseOp;
...@@ -19,18 +17,12 @@ import emu.grasscutter.server.packet.send.PacketCombineFormulaDataNotify; ...@@ -19,18 +17,12 @@ import emu.grasscutter.server.packet.send.PacketCombineFormulaDataNotify;
import emu.grasscutter.server.packet.send.PacketCombineRsp; import emu.grasscutter.server.packet.send.PacketCombineRsp;
import emu.grasscutter.server.packet.send.PacketReliquaryDecomposeRsp; import emu.grasscutter.server.packet.send.PacketReliquaryDecomposeRsp;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.google.gson.reflect.TypeToken;
public class CombineManger extends BaseGameSystem { public class CombineManger extends BaseGameSystem {
private final static Int2ObjectMap<List<Integer>> reliquaryDecomposeData = new Int2ObjectOpenHashMap<>(); private final static Int2ObjectMap<List<Integer>> reliquaryDecomposeData = new Int2ObjectOpenHashMap<>();
...@@ -40,13 +32,10 @@ public class CombineManger extends BaseGameSystem { ...@@ -40,13 +32,10 @@ public class CombineManger extends BaseGameSystem {
public static void initialize() { public static void initialize() {
// Read the data we need for strongbox. // Read the data we need for strongbox.
try (Reader fileReader = DataLoader.loadReader("ReliquaryDecompose.json")) { try {
List<ReliquaryDecomposeEntry> decomposeEntries = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ReliquaryDecomposeEntry.class).getType()); DataLoader.loadList("ReliquaryDecompose.json", ReliquaryDecomposeEntry.class).forEach(entry -> {
for (ReliquaryDecomposeEntry entry : decomposeEntries) {
reliquaryDecomposeData.put(entry.getConfigId(), entry.getItems()); reliquaryDecomposeData.put(entry.getConfigId(), entry.getItems());
} });
Grasscutter.getLogger().debug("Loaded {} reliquary decompose entries.", reliquaryDecomposeData.size()); Grasscutter.getLogger().debug("Loaded {} reliquary decompose entries.", reliquaryDecomposeData.size());
} }
catch (Exception ex) { catch (Exception ex) {
......
package emu.grasscutter.game.drop; package emu.grasscutter.game.drop;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
...@@ -19,9 +18,6 @@ import emu.grasscutter.utils.Utils; ...@@ -19,9 +18,6 @@ import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.List; import java.util.List;
public class DropSystem extends BaseGameSystem { public class DropSystem extends BaseGameSystem {
...@@ -38,9 +34,9 @@ public class DropSystem extends BaseGameSystem { ...@@ -38,9 +34,9 @@ public class DropSystem extends BaseGameSystem {
} }
public synchronized void load() { public synchronized void load() {
try (Reader fileReader = DataLoader.loadReader("Drop.json")) { getDropData().clear();
getDropData().clear(); try {
List<DropInfo> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DropInfo.class).getType()); List<DropInfo> banners = DataLoader.loadList("Drop.json", DropInfo.class);
if (banners.size() > 0) { if (banners.size() > 0) {
for (DropInfo di : banners) { for (DropInfo di : banners) {
getDropData().put(di.getMonsterId(), di.getDropDataList()); getDropData().put(di.getMonsterId(), di.getDropDataList());
......
...@@ -24,16 +24,11 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; ...@@ -24,16 +24,11 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.ints.IntSet;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import com.google.gson.reflect.TypeToken;
public class DungeonChallenge extends WorldChallenge { public class DungeonChallenge extends WorldChallenge {
/** /**
...@@ -46,13 +41,10 @@ public class DungeonChallenge extends WorldChallenge { ...@@ -46,13 +41,10 @@ public class DungeonChallenge extends WorldChallenge {
public static void initialize() { public static void initialize() {
// Read the data we need for dungeon rewards drops. // Read the data we need for dungeon rewards drops.
try (Reader fileReader = DataLoader.loadReader("DungeonDrop.json")) { try {
List<DungeonDrop> dungeonDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DungeonDrop.class).getType()); DataLoader.loadList("DungeonDrop.json", DungeonDrop.class).forEach(entry -> {
for (DungeonDrop entry : dungeonDropList) {
dungeonDropData.put(entry.getDungeonId(), entry.getDrops()); dungeonDropData.put(entry.getDungeonId(), entry.getDrops());
} });
Grasscutter.getLogger().debug("Loaded {} dungeon drop data entries.", dungeonDropData.size()); Grasscutter.getLogger().debug("Loaded {} dungeon drop data entries.", dungeonDropData.size());
} }
catch (Exception ex) { catch (Exception ex) {
......
package emu.grasscutter.game.expedition; package emu.grasscutter.game.expedition;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.server.game.BaseGameSystem; import emu.grasscutter.server.game.BaseGameSystem;
...@@ -8,12 +7,6 @@ import emu.grasscutter.server.game.GameServer; ...@@ -8,12 +7,6 @@ import emu.grasscutter.server.game.GameServer;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import static emu.grasscutter.config.Configuration.*;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.List; import java.util.List;
public class ExpeditionSystem extends BaseGameSystem { public class ExpeditionSystem extends BaseGameSystem {
...@@ -30,9 +23,9 @@ public class ExpeditionSystem extends BaseGameSystem { ...@@ -30,9 +23,9 @@ public class ExpeditionSystem extends BaseGameSystem {
} }
public synchronized void load() { public synchronized void load() {
try (Reader fileReader = DataLoader.loadReader("ExpeditionReward.json")) { getExpeditionRewardDataList().clear();
getExpeditionRewardDataList().clear(); try {
List<ExpeditionRewardInfo> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ExpeditionRewardInfo.class).getType()); List<ExpeditionRewardInfo> banners = DataLoader.loadList("ExpeditionReward.json", ExpeditionRewardInfo.class);
if (banners.size() > 0) { if (banners.size() > 0) {
for (ExpeditionRewardInfo di : banners) { for (ExpeditionRewardInfo di : banners) {
getExpeditionRewardDataList().put(di.getExpId(), di.getExpeditionRewardDataList()); getExpeditionRewardDataList().put(di.getExpId(), di.getExpeditionRewardDataList());
......
...@@ -3,18 +3,11 @@ package emu.grasscutter.game.gacha; ...@@ -3,18 +3,11 @@ package emu.grasscutter.game.gacha;
import static emu.grasscutter.config.Configuration.*; import static emu.grasscutter.config.Configuration.*;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.*; import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import com.google.gson.reflect.TypeToken;
import com.sun.nio.file.SensitivityWatchEventModifier; import com.sun.nio.file.SensitivityWatchEventModifier;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
...@@ -76,9 +69,9 @@ public class GachaSystem extends BaseGameSystem { ...@@ -76,9 +69,9 @@ public class GachaSystem extends BaseGameSystem {
} }
public synchronized void load() { public synchronized void load() {
try (Reader fileReader = DataLoader.loadReader("Banners.json")) { getGachaBanners().clear();
getGachaBanners().clear(); try {
List<GachaBanner> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, GachaBanner.class).getType()); List<GachaBanner> banners = DataLoader.loadList("Banners.json", GachaBanner.class);
if (banners.size() > 0) { if (banners.size() > 0) {
for (GachaBanner banner : banners) { for (GachaBanner banner : banners) {
getGachaBanners().put(banner.getScheduleId(), banner); getGachaBanners().put(banner.getScheduleId(), banner);
......
...@@ -31,9 +31,6 @@ import emu.grasscutter.utils.Position; ...@@ -31,9 +31,6 @@ import emu.grasscutter.utils.Position;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -42,7 +39,6 @@ import java.util.concurrent.ThreadLocalRandom; ...@@ -42,7 +39,6 @@ import java.util.concurrent.ThreadLocalRandom;
import static emu.grasscutter.config.Configuration.GAME_OPTIONS; import static emu.grasscutter.config.Configuration.GAME_OPTIONS;
import com.google.gson.reflect.TypeToken;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
public class EnergyManager extends BasePlayerManager { public class EnergyManager extends BasePlayerManager {
...@@ -60,12 +56,10 @@ public class EnergyManager extends BasePlayerManager { ...@@ -60,12 +56,10 @@ public class EnergyManager extends BasePlayerManager {
public static void initialize() { public static void initialize() {
// Read the data we need for monster energy drops. // Read the data we need for monster energy drops.
try (Reader fileReader = DataLoader.loadReader("EnergyDrop.json")) { try {
List<EnergyDropEntry> energyDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, EnergyDropEntry.class).getType()); DataLoader.loadList("EnergyDrop.json", EnergyDropEntry.class).forEach(entry -> {
for (EnergyDropEntry entry : energyDropList) {
energyDropData.put(entry.getDropId(), entry.getDropList()); energyDropData.put(entry.getDropId(), entry.getDropList());
} });
Grasscutter.getLogger().debug("Energy drop data successfully loaded."); Grasscutter.getLogger().debug("Energy drop data successfully loaded.");
} }
...@@ -74,12 +68,10 @@ public class EnergyManager extends BasePlayerManager { ...@@ -74,12 +68,10 @@ public class EnergyManager extends BasePlayerManager {
} }
// Read the data for particle generation from skills // Read the data for particle generation from skills
try (Reader fileReader = new InputStreamReader(DataLoader.load("SkillParticleGeneration.json"))) { try {
List<SkillParticleGenerationEntry> skillParticleGenerationList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, SkillParticleGenerationEntry.class).getType()); DataLoader.loadList("SkillParticleGeneration.json", SkillParticleGenerationEntry.class).forEach(entry -> {
for (SkillParticleGenerationEntry entry : skillParticleGenerationList) {
skillParticleGenerationData.put(entry.getAvatarId(), entry.getAmountList()); skillParticleGenerationData.put(entry.getAvatarId(), entry.getAmountList());
} });
Grasscutter.getLogger().debug("Skill particle generation data successfully loaded."); Grasscutter.getLogger().debug("Skill particle generation data successfully loaded.");
} }
......
package emu.grasscutter.game.shop; package emu.grasscutter.game.shop;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
...@@ -14,11 +13,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; ...@@ -14,11 +13,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import static emu.grasscutter.config.Configuration.*; import static emu.grasscutter.config.Configuration.*;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
...@@ -60,9 +55,9 @@ public class ShopSystem extends BaseGameSystem { ...@@ -60,9 +55,9 @@ public class ShopSystem extends BaseGameSystem {
} }
private void loadShop() { private void loadShop() {
try (Reader fileReader = DataLoader.loadReader("Shop.json")) { getShopData().clear();
getShopData().clear(); try {
List<ShopTable> banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopTable.class).getType()); List<ShopTable> banners = DataLoader.loadList("Shop.json", ShopTable.class);
if (banners.size() > 0) { if (banners.size() > 0) {
for (ShopTable shopTable : banners) { for (ShopTable shopTable : banners) {
for (ShopInfo cost : shopTable.getItems()) { for (ShopInfo cost : shopTable.getItems()) {
...@@ -104,9 +99,9 @@ public class ShopSystem extends BaseGameSystem { ...@@ -104,9 +99,9 @@ public class ShopSystem extends BaseGameSystem {
} }
private void loadShopChest() { private void loadShopChest() {
try (Reader fileReader = DataLoader.loadReader("ShopChest.json")) { getShopChestData().clear();
getShopChestData().clear(); try {
List<ShopChestTable> shopChestTableList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopChestTable.class).getType()); List<ShopChestTable> shopChestTableList = DataLoader.loadList("ShopChest.json", ShopChestTable.class);
if (shopChestTableList.size() > 0) { if (shopChestTableList.size() > 0) {
getShopChestData().addAll(shopChestTableList); getShopChestData().addAll(shopChestTableList);
Grasscutter.getLogger().debug("ShopChest data successfully loaded."); Grasscutter.getLogger().debug("ShopChest data successfully loaded.");
...@@ -119,9 +114,9 @@ public class ShopSystem extends BaseGameSystem { ...@@ -119,9 +114,9 @@ public class ShopSystem extends BaseGameSystem {
} }
private void loadShopChestBatchUse() { private void loadShopChestBatchUse() {
try (Reader fileReader = DataLoader.loadReader("ShopChestBatchUse.json")) { getShopChestBatchUseData().clear();
getShopChestBatchUseData().clear(); try {
List<ShopChestBatchUseTable> shopChestBatchUseTableList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, ShopChestBatchUseTable.class).getType()); List<ShopChestBatchUseTable> shopChestBatchUseTableList = DataLoader.loadList("ShopChestBatchUse.json", ShopChestBatchUseTable.class);
if (shopChestBatchUseTableList.size() > 0) { if (shopChestBatchUseTableList.size() > 0) {
getShopChestBatchUseData().addAll(shopChestBatchUseTableList); getShopChestBatchUseData().addAll(shopChestBatchUseTableList);
Grasscutter.getLogger().debug("ShopChestBatchUse data successfully loaded."); Grasscutter.getLogger().debug("ShopChestBatchUse data successfully loaded.");
......
package emu.grasscutter.game.systems; package emu.grasscutter.game.systems;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
...@@ -16,7 +15,6 @@ import lombok.Data; ...@@ -16,7 +15,6 @@ import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.experimental.FieldDefaults; import lombok.experimental.FieldDefaults;
import java.io.InputStreamReader;
import java.util.*; import java.util.*;
@Getter @Getter
...@@ -30,9 +28,8 @@ public class AnnouncementSystem extends BaseGameSystem { ...@@ -30,9 +28,8 @@ public class AnnouncementSystem extends BaseGameSystem {
} }
private int loadConfig() { private int loadConfig() {
try (var fileReader = DataLoader.loadReader("Announcement.json")) { try {
List<AnnounceConfigItem> announceConfigItems = Grasscutter.getGsonFactory().fromJson(fileReader, List<AnnounceConfigItem> announceConfigItems = DataLoader.loadList("Announcement.json", AnnounceConfigItem.class);
TypeToken.getParameterized(List.class, AnnounceConfigItem.class).getType());
announceConfigItemMap.clear(); announceConfigItemMap.clear();
announceConfigItems.forEach(i -> announceConfigItemMap.put(i.getTemplateId(), i)); announceConfigItems.forEach(i -> announceConfigItemMap.put(i.getTemplateId(), i));
......
...@@ -7,11 +7,6 @@ import emu.grasscutter.data.excels.TowerScheduleData; ...@@ -7,11 +7,6 @@ import emu.grasscutter.data.excels.TowerScheduleData;
import emu.grasscutter.server.game.BaseGameSystem; import emu.grasscutter.server.game.BaseGameSystem;
import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameServer;
import static emu.grasscutter.config.Configuration.*;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -25,8 +20,8 @@ public class TowerSystem extends BaseGameSystem { ...@@ -25,8 +20,8 @@ public class TowerSystem extends BaseGameSystem {
private TowerScheduleConfig towerScheduleConfig; private TowerScheduleConfig towerScheduleConfig;
public synchronized void load() { public synchronized void load() {
try (Reader fileReader = DataLoader.loadReader("TowerSchedule.json")) { try {
towerScheduleConfig = Grasscutter.getGsonFactory().fromJson(fileReader, TowerScheduleConfig.class); towerScheduleConfig = DataLoader.loadClass("TowerSchedule.json", TowerScheduleConfig.class);
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().error("Unable to load tower schedule config.", e); Grasscutter.getLogger().error("Unable to load tower schedule config.", e);
} }
......
package emu.grasscutter.game.world; package emu.grasscutter.game.world;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
...@@ -17,9 +16,6 @@ import emu.grasscutter.scripts.data.SceneMonster; ...@@ -17,9 +16,6 @@ import emu.grasscutter.scripts.data.SceneMonster;
import emu.grasscutter.server.game.BaseGameSystem; import emu.grasscutter.server.game.BaseGameSystem;
import emu.grasscutter.server.game.GameServer; 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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -42,15 +38,11 @@ public class WorldDataSystem extends BaseGameSystem { ...@@ -42,15 +38,11 @@ public class WorldDataSystem extends BaseGameSystem {
// set the special chest first // set the special chest first
chestInteractHandlerMap.put("SceneObj_Chest_Flora", new BossChestInteractHandler()); chestInteractHandlerMap.put("SceneObj_Chest_Flora", new BossChestInteractHandler());
try (Reader reader = DataLoader.loadReader("ChestReward.json")) { try {
List<ChestReward> chestReward = Grasscutter.getGsonFactory().fromJson( DataLoader.loadList("ChestReward.json", ChestReward.class)
reader, .forEach(reward ->
TypeToken.getParameterized(List.class, ChestReward.class).getType()); reward.getObjNames().forEach(name ->
chestInteractHandlerMap.putIfAbsent(name, new NormalChestInteractHandler(reward))));
chestReward.forEach(reward ->
reward.getObjNames().forEach(
name -> chestInteractHandlerMap.putIfAbsent(name, new NormalChestInteractHandler(reward))));
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().error("Unable to load chest reward config.", e); Grasscutter.getLogger().error("Unable to load chest reward config.", e);
} }
......
...@@ -12,6 +12,7 @@ import java.util.stream.Collectors; ...@@ -12,6 +12,7 @@ import java.util.stream.Collectors;
import emu.grasscutter.GameConstants; import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
...@@ -59,7 +60,7 @@ public class PacketOpcodesUtils { ...@@ -59,7 +60,7 @@ public class PacketOpcodesUtils {
.filter(e -> e.getIntKey() > 0) .filter(e -> e.getIntKey() > 0)
.collect(Collectors.toMap(Int2ObjectMap.Entry::getIntKey, Int2ObjectMap.Entry::getValue, (k, v) -> v, TreeMap::new)); .collect(Collectors.toMap(Int2ObjectMap.Entry::getIntKey, Int2ObjectMap.Entry::getValue, (k, v) -> v, TreeMap::new));
// Write to file // Write to file
writer.write(Grasscutter.getGsonFactory().toJson(packetIds)); writer.write(Utils.jsonEncode(packetIds));
Grasscutter.getLogger().info("Dumped packet ids."); Grasscutter.getLogger().info("Dumped packet ids.");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -82,7 +82,7 @@ public final class PluginManager { ...@@ -82,7 +82,7 @@ public final class PluginManager {
InputStreamReader fileReader = new InputStreamReader(configFile.openStream()); InputStreamReader fileReader = new InputStreamReader(configFile.openStream());
// Create a plugin config instance from the config file. // Create a plugin config instance from the config file.
PluginConfig pluginConfig = Grasscutter.getGsonFactory().fromJson(fileReader, PluginConfig.class); PluginConfig pluginConfig = Utils.loadJsonToClass(fileReader, PluginConfig.class);
// Check if the plugin config is valid. // Check if the plugin config is valid.
if (!pluginConfig.validate()) { if (!pluginConfig.validate()) {
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid config file."); Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid config file.");
......
...@@ -130,7 +130,7 @@ public final class Language { ...@@ -130,7 +130,7 @@ public final class Language {
languageCode = description.getLanguageCode(); languageCode = description.getLanguageCode();
try { try {
languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class); languageData = Utils.jsonDecode(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class);
} catch (Exception exception) { } catch (Exception exception) {
Grasscutter.getLogger().warn("Failed to load language file: " + description.getLanguageCode(), exception); Grasscutter.getLogger().warn("Failed to load language file: " + description.getLanguageCode(), exception);
} }
......
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