Commit d215035f authored by KingRainbow44's avatar KingRainbow44
Browse files

Refactor config database settings

parent 3612c6af
...@@ -77,25 +77,25 @@ public final class DatabaseHelper { ...@@ -77,25 +77,25 @@ public final class DatabaseHelper {
} }
public static Account getAccountByName(String username) { public static Account getAccountByName(String username) {
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).first(); return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
} }
public static Account getAccountByToken(String token) { public static Account getAccountByToken(String token) {
if(token == null) return null; if(token == null) return null;
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("token", token)).first(); return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
} }
public static Account getAccountBySessionKey(String sessionKey) { public static Account getAccountBySessionKey(String sessionKey) {
if(sessionKey == null) return null; if(sessionKey == null) return null;
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first(); return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first();
} }
public static Account getAccountById(String uid) { public static Account getAccountById(String uid) {
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first(); return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
} }
public static Account getAccountByPlayerId(int playerId) { public static Account getAccountByPlayerId(int playerId) {
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first(); return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
} }
public static void deleteAccount(Account target) { public static void deleteAccount(Account target) {
...@@ -104,37 +104,37 @@ public final class DatabaseHelper { ...@@ -104,37 +104,37 @@ public final class DatabaseHelper {
// database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ... // database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ...
// Delete Mail.class data // Delete Mail.class data
DatabaseManager.getDatabase().getCollection("mail").deleteMany(eq("ownerUid", target.getPlayerUid())); DatabaseManager.getGameDatabase().getCollection("mail").deleteMany(eq("ownerUid", target.getPlayerUid()));
// Delete Avatar.class data // Delete Avatar.class data
DatabaseManager.getDatabase().getCollection("avatars").deleteMany(eq("ownerId", target.getPlayerUid())); DatabaseManager.getGameDatabase().getCollection("avatars").deleteMany(eq("ownerId", target.getPlayerUid()));
// Delete GachaRecord.class data // Delete GachaRecord.class data
DatabaseManager.getDatabase().getCollection("gachas").deleteMany(eq("ownerId", target.getPlayerUid())); DatabaseManager.getGameDatabase().getCollection("gachas").deleteMany(eq("ownerId", target.getPlayerUid()));
// Delete GameItem.class data // Delete GameItem.class data
DatabaseManager.getDatabase().getCollection("items").deleteMany(eq("ownerId", target.getPlayerUid())); DatabaseManager.getGameDatabase().getCollection("items").deleteMany(eq("ownerId", target.getPlayerUid()));
// Delete friendships. // Delete friendships.
// Here, we need to make sure to not only delete the deleted account's friendships, // Here, we need to make sure to not only delete the deleted account's friendships,
// but also all friendship entries for that account's friends. // but also all friendship entries for that account's friends.
DatabaseManager.getDatabase().getCollection("friendships").deleteMany(eq("ownerId", target.getPlayerUid())); DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("ownerId", target.getPlayerUid()));
DatabaseManager.getDatabase().getCollection("friendships").deleteMany(eq("friendId", target.getPlayerUid())); DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("friendId", target.getPlayerUid()));
// Delete the player. // Delete the player.
DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("id", target.getPlayerUid())).delete(); DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("id", target.getPlayerUid())).delete();
// Finally, delete the account itself. // Finally, delete the account itself.
DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete(); DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
} }
public static List<Player> getAllPlayers() { public static List<Player> getAllPlayers() {
return DatabaseManager.getDatastore().find(Player.class).stream().toList(); return DatabaseManager.getGameDatastore().find(Player.class).stream().toList();
} }
public static Player getPlayerById(int id) { public static Player getPlayerById(int id) {
return DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("_id", id)).first(); return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first();
} }
public static boolean checkPlayerExists(int id) { public static boolean checkPlayerExists(int id) {
return DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("_id", id)).first() != null; return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first() != null;
} }
public static synchronized Player createPlayer(Player character, int reservedId) { public static synchronized Player createPlayer(Player character, int reservedId) {
...@@ -151,7 +151,7 @@ public final class DatabaseHelper { ...@@ -151,7 +151,7 @@ public final class DatabaseHelper {
character.setUid(id); character.setUid(id);
} }
// Save to database // Save to database
DatabaseManager.getDatastore().save(character); DatabaseManager.getGameDatastore().save(character);
return character; return character;
} }
...@@ -170,48 +170,48 @@ public final class DatabaseHelper { ...@@ -170,48 +170,48 @@ public final class DatabaseHelper {
} }
public static void savePlayer(Player character) { public static void savePlayer(Player character) {
DatabaseManager.getDatastore().save(character); DatabaseManager.getGameDatastore().save(character);
} }
public static void saveAvatar(Avatar avatar) { public static void saveAvatar(Avatar avatar) {
DatabaseManager.getDatastore().save(avatar); DatabaseManager.getGameDatastore().save(avatar);
} }
public static List<Avatar> getAvatars(Player player) { public static List<Avatar> getAvatars(Player player) {
return DatabaseManager.getDatastore().find(Avatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList(); return DatabaseManager.getGameDatastore().find(Avatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
} }
public static void saveItem(GameItem item) { public static void saveItem(GameItem item) {
DatabaseManager.getDatastore().save(item); DatabaseManager.getGameDatastore().save(item);
} }
public static boolean deleteItem(GameItem item) { public static boolean deleteItem(GameItem item) {
DeleteResult result = DatabaseManager.getDatastore().delete(item); DeleteResult result = DatabaseManager.getGameDatastore().delete(item);
return result.wasAcknowledged(); return result.wasAcknowledged();
} }
public static List<GameItem> getInventoryItems(Player player) { public static List<GameItem> getInventoryItems(Player player) {
return DatabaseManager.getDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList(); return DatabaseManager.getGameDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
} }
public static List<Friendship> getFriends(Player player) { public static List<Friendship> getFriends(Player player) {
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList(); return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
} }
public static List<Friendship> getReverseFriends(Player player) { public static List<Friendship> getReverseFriends(Player player) {
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList(); return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
} }
public static void saveFriendship(Friendship friendship) { public static void saveFriendship(Friendship friendship) {
DatabaseManager.getDatastore().save(friendship); DatabaseManager.getGameDatastore().save(friendship);
} }
public static void deleteFriendship(Friendship friendship) { public static void deleteFriendship(Friendship friendship) {
DatabaseManager.getDatastore().delete(friendship); DatabaseManager.getGameDatastore().delete(friendship);
} }
public static Friendship getReverseFriendship(Friendship friendship) { public static Friendship getReverseFriendship(Friendship friendship) {
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.and( return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.and(
Filters.eq("ownerId", friendship.getFriendId()), Filters.eq("ownerId", friendship.getFriendId()),
Filters.eq("friendId", friendship.getOwnerId()) Filters.eq("friendId", friendship.getOwnerId())
)).first(); )).first();
...@@ -222,7 +222,7 @@ public final class DatabaseHelper { ...@@ -222,7 +222,7 @@ public final class DatabaseHelper {
} }
public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType, int pageSize){ public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType, int pageSize){
return DatabaseManager.getDatastore().find(GachaRecord.class).filter( return DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
Filters.eq("ownerId", ownerId), Filters.eq("ownerId", ownerId),
Filters.eq("gachaType", gachaType) Filters.eq("gachaType", gachaType)
).iterator(new FindOptions() ).iterator(new FindOptions()
...@@ -237,7 +237,7 @@ public final class DatabaseHelper { ...@@ -237,7 +237,7 @@ public final class DatabaseHelper {
} }
public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType, int pageSize){ public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType, int pageSize){
long count = DatabaseManager.getDatastore().find(GachaRecord.class).filter( long count = DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
Filters.eq("ownerId", ownerId), Filters.eq("ownerId", ownerId),
Filters.eq("gachaType", gachaType) Filters.eq("gachaType", gachaType)
).count(); ).count();
...@@ -245,19 +245,19 @@ public final class DatabaseHelper { ...@@ -245,19 +245,19 @@ public final class DatabaseHelper {
} }
public static void saveGachaRecord(GachaRecord gachaRecord){ public static void saveGachaRecord(GachaRecord gachaRecord){
DatabaseManager.getDatastore().save(gachaRecord); DatabaseManager.getGameDatastore().save(gachaRecord);
} }
public static List<Mail> getAllMail(Player player) { public static List<Mail> getAllMail(Player player) {
return DatabaseManager.getDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList(); return DatabaseManager.getGameDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
} }
public static void saveMail(Mail mail) { public static void saveMail(Mail mail) {
DatabaseManager.getDatastore().save(mail); DatabaseManager.getGameDatastore().save(mail);
} }
public static boolean deleteMail(Mail mail) { public static boolean deleteMail(Mail mail) {
DeleteResult result = DatabaseManager.getDatastore().delete(mail); DeleteResult result = DatabaseManager.getGameDatastore().delete(mail);
return result.wasAcknowledged(); return result.wasAcknowledged();
} }
} }
...@@ -23,19 +23,19 @@ import emu.grasscutter.game.player.Player; ...@@ -23,19 +23,19 @@ import emu.grasscutter.game.player.Player;
import static emu.grasscutter.Configuration.*; import static emu.grasscutter.Configuration.*;
public final class DatabaseManager { public final class DatabaseManager {
private static Datastore datastore; private static Datastore gameDatastore;
private static Datastore dispatchDatastore; private static Datastore dispatchDatastore;
private static final Class<?>[] mappedClasses = new Class<?>[] { private static final Class<?>[] mappedClasses = new Class<?>[] {
DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class, GachaRecord.class, Mail.class DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class, GachaRecord.class, Mail.class
}; };
public static Datastore getDatastore() { public static Datastore getGameDatastore() {
return datastore; return gameDatastore;
} }
public static MongoDatabase getDatabase() { public static MongoDatabase getGameDatabase() {
return getDatastore().getDatabase(); return getGameDatastore().getDatabase();
} }
// Yes. I very dislike this method. However, this will be good for now. // Yes. I very dislike this method. However, this will be good for now.
...@@ -44,42 +44,42 @@ public final class DatabaseManager { ...@@ -44,42 +44,42 @@ public final class DatabaseManager {
if(SERVER.runMode == ServerRunMode.GAME_ONLY) { if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
return dispatchDatastore; return dispatchDatastore;
} else { } else {
return datastore; return gameDatastore;
} }
} }
public static void initialize() { public static void initialize() {
// Initialize // Initialize
MongoClient mongoClient = MongoClients.create(DATABASE.connectionUri); MongoClient gameMongoClient = MongoClients.create(DATABASE.game.connectionUri);
// Set mapper options. // Set mapper options.
MapperOptions mapperOptions = MapperOptions.builder() MapperOptions mapperOptions = MapperOptions.builder()
.storeEmpties(true).storeNulls(false).build(); .storeEmpties(true).storeNulls(false).build();
// Create data store. // Create data store.
datastore = Morphia.createDatastore(mongoClient, DATABASE.collection, mapperOptions); gameDatastore = Morphia.createDatastore(gameMongoClient, DATABASE.game.collection, mapperOptions);
// Map classes. // Map classes.
datastore.getMapper().map(mappedClasses); gameDatastore.getMapper().map(mappedClasses);
// Ensure indexes // Ensure indexes
try { try {
datastore.ensureIndexes(); gameDatastore.ensureIndexes();
} catch (MongoCommandException exception) { } catch (MongoCommandException exception) {
Grasscutter.getLogger().info("Mongo index error: ", exception); Grasscutter.getLogger().info("Mongo index error: ", exception);
// Duplicate index error // Duplicate index error
if (exception.getCode() == 85) { if (exception.getCode() == 85) {
// Drop all indexes and re add them // Drop all indexes and re add them
MongoIterable<String> collections = datastore.getDatabase().listCollectionNames(); MongoIterable<String> collections = gameDatastore.getDatabase().listCollectionNames();
for (String name : collections) { for (String name : collections) {
datastore.getDatabase().getCollection(name).dropIndexes(); gameDatastore.getDatabase().getCollection(name).dropIndexes();
} }
// Add back indexes // Add back indexes
datastore.ensureIndexes(); gameDatastore.ensureIndexes();
} }
} }
if(SERVER.runMode == ServerRunMode.GAME_ONLY) { if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
MongoClient dispatchMongoClient = MongoClients.create(GAME_OPTIONS.databaseInfo.connectionUri); MongoClient dispatchMongoClient = MongoClients.create(DATABASE.server.connectionUri);
dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, GAME_OPTIONS.databaseInfo.collection); dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, DATABASE.server.collection);
// Ensure indexes for dispatch server // Ensure indexes for dispatch server
try { try {
...@@ -101,14 +101,14 @@ public final class DatabaseManager { ...@@ -101,14 +101,14 @@ public final class DatabaseManager {
} }
public static synchronized int getNextId(Class<?> c) { public static synchronized int getNextId(Class<?> c) {
DatabaseCounter counter = getDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getSimpleName())).first(); DatabaseCounter counter = getGameDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getSimpleName())).first();
if (counter == null) { if (counter == null) {
counter = new DatabaseCounter(c.getSimpleName()); counter = new DatabaseCounter(c.getSimpleName());
} }
try { try {
return counter.getNextId(); return counter.getNextId();
} finally { } finally {
getDatastore().save(counter); getGameDatastore().save(counter);
} }
} }
......
...@@ -7,6 +7,7 @@ import emu.grasscutter.server.event.HandlerPriority; ...@@ -7,6 +7,7 @@ import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
...@@ -90,6 +91,8 @@ public final class PluginManager { ...@@ -90,6 +91,8 @@ public final class PluginManager {
fileReader.close(); // Close the file reader. fileReader.close(); // Close the file reader.
} catch (ClassNotFoundException ignored) { } catch (ClassNotFoundException ignored) {
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid main class."); Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid main class.");
} catch (FileNotFoundException ignored) {
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " lacks a valid config file.");
} }
} catch (Exception exception) { } catch (Exception exception) {
Grasscutter.getLogger().error("Failed to load plugin: " + plugin.getName(), exception); Grasscutter.getLogger().error("Failed to load plugin: " + plugin.getName(), exception);
......
...@@ -2,6 +2,8 @@ package emu.grasscutter.utils; ...@@ -2,6 +2,8 @@ package emu.grasscutter.utils;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.Grasscutter.ServerRunMode;
import java.io.FileReader; import java.io.FileReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
...@@ -15,7 +17,7 @@ import static emu.grasscutter.Grasscutter.config; ...@@ -15,7 +17,7 @@ import static emu.grasscutter.Grasscutter.config;
*/ */
public class ConfigContainer { public class ConfigContainer {
private static int version() { private static int version() {
return 1; return 2;
} }
/** /**
...@@ -69,9 +71,14 @@ public class ConfigContainer { ...@@ -69,9 +71,14 @@ public class ConfigContainer {
/* Option containers. */ /* Option containers. */
public static class Database { public static class Database {
public DataStore server = new DataStore();
public DataStore game = new DataStore();
public static class DataStore {
public String connectionUri = "mongodb://localhost:27017"; public String connectionUri = "mongodb://localhost:27017";
public String collection = "grasscutter"; public String collection = "grasscutter";
} }
}
public static class Structure { public static class Structure {
public String resources = "./resources/"; public String resources = "./resources/";
...@@ -86,8 +93,8 @@ public class ConfigContainer { ...@@ -86,8 +93,8 @@ public class ConfigContainer {
} }
public static class Server { public static class Server {
public Grasscutter.ServerDebugMode debugLevel = Grasscutter.ServerDebugMode.NONE; public ServerDebugMode debugLevel = ServerDebugMode.NONE;
public Grasscutter.ServerRunMode runMode = Grasscutter.ServerRunMode.HYBRID; public ServerRunMode runMode = ServerRunMode.HYBRID;
public Dispatch dispatch = new Dispatch(); public Dispatch dispatch = new Dispatch();
public Game game = new Game(); public Game game = new Game();
...@@ -112,7 +119,7 @@ public class ConfigContainer { ...@@ -112,7 +119,7 @@ public class ConfigContainer {
public int bindPort = 443; public int bindPort = 443;
/* This is the port used in URLs. */ /* This is the port used in URLs. */
public int accessPort = 443; public int accessPort = 0;
public Encryption encryption = new Encryption(); public Encryption encryption = new Encryption();
public Policies policies = new Policies(); public Policies policies = new Policies();
...@@ -128,7 +135,7 @@ public class ConfigContainer { ...@@ -128,7 +135,7 @@ public class ConfigContainer {
public int bindPort = 22102; public int bindPort = 22102;
/* This is the port used in the default region. */ /* This is the port used in the default region. */
public int accessPort = 22102; public int accessPort = 0;
public GameOptions gameOptions = new GameOptions(); public GameOptions gameOptions = new GameOptions();
public JoinOptions joinOptions = new JoinOptions(); public JoinOptions joinOptions = new JoinOptions();
...@@ -155,16 +162,14 @@ public class ConfigContainer { ...@@ -155,16 +162,14 @@ public class ConfigContainer {
} }
public static class GameOptions { public static class GameOptions {
public GameOptions.InventoryLimits inventoryLimits = new GameOptions.InventoryLimits(); public InventoryLimits inventoryLimits = new InventoryLimits();
public GameOptions.AvatarLimits avatarLimits = new GameOptions.AvatarLimits(); public AvatarLimits avatarLimits = new AvatarLimits();
public int worldEntityLimit = 1000; // Unenforced. TODO: Implement. public int worldEntityLimit = 1000; // Unenforced. TODO: Implement.
public boolean watchGachaConfig = false; public boolean watchGachaConfig = false;
public boolean enableShopItems = true; public boolean enableShopItems = true;
public boolean staminaUsage = true; public boolean staminaUsage = true;
public GameOptions.Rates rates = new GameOptions.Rates(); public Rates rates = new Rates();
public Database databaseInfo = new Database();
public static class InventoryLimits { public static class InventoryLimits {
public int weapons = 2000; public int weapons = 2000;
......
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