Commit 9e7c9f46 authored by Benjamin Elsdon's avatar Benjamin Elsdon
Browse files

Merge remote-tracking branch 'upstream/development' into dev-mail

parents cb03b654 d612530c
name: "Build"
on:
push:
branches:
- "stable"
jobs:
Build-Server-Jar:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: '8'
- name: Run Gradle
run: .\gradlew.bat && .\gradlew jar
- name: Upload build
uses: actions/upload-artifact@v3
with:
name: Grasscutter
path: grasscutter.jar
......@@ -47,12 +47,13 @@ tmp/
# Grasscutter
resources/*
logs/*
data/AbilityEmbryos.json
data/OpenConfig.json
proto/auto/
proto/protoc.exe
GM Handbook.txt
config.json
mitmdump.exe
grasscutter.jar
config.json
mitmdump.exe
grasscutter.jar
mongod.exe
Subproject commit ba0eab7d629b5adeb145bc430e14ffcb4bdf3d6a
Subproject commit dd17415b71dfcff049e72dbe8a76173611f4b0ae
......@@ -12,8 +12,11 @@ A WIP server reimplementation for *some anime game* 2.3-2.6
* Friends list
* Co-op *partially* work
# Quick setup guide
### Note
* If you update from an older version, delete `config.json` for regeneration
### Prerequisites
* JDK-8u202 ([mirror link](https://mirrors.huaweicloud.com/java/jdk/8u202-b08/) since Oracle required an account to download old builds)
* Java 16
* Mongodb (recommended 4.0+)
* Proxy daemon: mitmproxy (mitmdump, recommended), Fiddler Classic, etc.
......@@ -26,7 +29,7 @@ A WIP server reimplementation for *some anime game* 2.3-2.6
### Connecting with the client
½. Create an account using *server console command* below
1. Run a proxy daemon: (choose either one)
- mitmdump: `mitmdump -s proxy.py --ssl-insecure`
- mitmdump: `mitmdump -s proxy.py -k`
- Fiddler Classic: Run Fiddler Classic, turn on `Decrypt https traffic` in setting and change the default port there (Tools -> Options -> Connections) to anything other than `8888`, and load [this script](https://github.lunatic.moe/fiddlerscript).
- [Hosts file](https://github.com/Melledy/Grasscutter/wiki/Running#traffic-route-map)
2. Trust CA certificate:
......@@ -34,39 +37,36 @@ A WIP server reimplementation for *some anime game* 2.3-2.6
2. Set network proxy to `127.0.0.1:8080` or the proxy port you specified.
4. *yoink*
* or you can use `run.cmd` to start Server & Proxy daemon with one click
* or you can use `start.cmd` to start Server & Proxy daemon with one click
# Grasscutter commands
### Server console commands
There is a dummy user named "Server" in every player's friends list that you can message to use commands. Commands also work in other chat rooms, such as private/team chats.
`account create [username] {playerid}` - Creates an account with the specified username and the in-game uid for that account. The playerid parameter is optional and will be auto generated if not set.
### In-Game commands
There is a dummy user named "Server" in every player's friends list that you can message to use commands. Commands also work in other chat rooms, such as private/team chats.
`!spawn [monster id] [level] [amount]`
`spawn [monster id] [level] [amount]`
`!give [item id] [amount]`
`give [item id] [amount]`
`!givechar [avatar id] [level]`
`givechar [avatar id] [level]`
`!drop [item id] [amount]`
`drop [item id] [amount]`
`!killall`
`killall`
`!setworldlevel [level]` - Relog to see effects properly
`setworldlevel [level]` - Relog to see effects properly
`!godmode` - Prevents you from taking damage
`godmode` - Prevents you from taking damage
`!resetconst` - Resets the constellation level on your current active character, will need to relog after using the command to see any changes.
`resetconst` - Resets the constellation level on your current active character, will need to relog after using the command to see any changes.
`!setstats [stats] [amount]` - Changes the current character's specified stat.
`setstats [stats] [amount]` - Changes the current character's specified stat.
`!clearartifacts` - Deletes all unequipped and unlocked level 0 artifacts, **including yellow rarity ones** from your inventory
`clearartifacts` - Deletes all unequipped and unlocked level 0 artifacts, **including yellow rarity ones** from your inventory
`!pos` - Gets your current coordinate.
`pos` - Gets your current coordinate.
`!weather [weather id] [climate id]` - Changes the current weather.
`weather [weather id] [climate id]` - Changes the current weather.
*More commands will be updated in the [wiki](https://github.com/Melledy/Grasscutter/wiki/).*
......
......@@ -14,17 +14,16 @@ plugins {
id 'application'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceCompatibility = 16
targetCompatibility = 16
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation fileTree(dir: 'lib', include: ['*.jar'])
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.32'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.6'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.6'
......@@ -33,9 +32,9 @@ dependencies {
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.8'
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.18.1'
implementation group: 'org.reflections', name: 'reflections', version: '0.9.12'
implementation group: 'org.reflections', name: 'reflections', version: '0.10.2'
implementation group: 'dev.morphia.morphia', name: 'core', version: '1.6.1'
implementation group: 'dev.morphia.morphia', name: 'morphia-core', version: '2.2.6'
implementation group: 'org.greenrobot', name: 'eventbus-java', version: '3.3.1'
}
......
No preview for this file type
......@@ -57,7 +57,9 @@ class MlgmXyysd_Genshin_Impact_Proxy:
"minor-api.mihoyo.com",
"public-data-api.mihoyo.com",
"uspider.yuanshen.com",
"sdk-static.mihoyo.com"
"sdk-static.mihoyo.com",
"abtest-api-data-sg.hoyoverse.com",
"log-upload-os.hoyoverse.com"
]
def request(self, flow: http.HTTPFlow) -> None:
......
......@@ -27,9 +27,11 @@ public final class Config {
public String Ip = "0.0.0.0";
public String PublicIp = "127.0.0.1";
public int Port = 443;
public int PublicPort = 0;
public String KeystorePath = "./keystore.p12";
public String KeystorePassword = "";
public String KeystorePassword = "123456";
public Boolean UseSSL = true;
public Boolean FrontHTTPS = true;
public boolean AutomaticallyCreateAccounts = false;
......@@ -52,6 +54,7 @@ public final class Config {
public String Ip = "0.0.0.0";
public String PublicIp = "127.0.0.1";
public int Port = 22102;
public int PublicPort = 0;
public String DispatchServerDatabaseUrl = "mongodb://localhost:27017";
public String DispatchServerDatabaseCollection = "grasscutter";
......
......@@ -34,7 +34,7 @@ public final class Grasscutter {
private static DispatchServer dispatchServer;
private static GameServer gameServer;
public static final Reflections reflector = new Reflections();
public static final Reflections reflector = new Reflections("emu.grasscutter");
static {
// Declare logback configuration.
......@@ -118,6 +118,7 @@ public final class Grasscutter {
public static void startConsole() {
String input;
getLogger().info("Done! For help, type \"help\"");
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
while ((input = br.readLine()) != null) {
try {
......
......@@ -11,6 +11,7 @@ import java.util.*;
public final class CommandMap {
private final Map<String, CommandHandler> commands = new HashMap<>();
private final Map<String, Command> annotations = new HashMap<>();
public CommandMap() {
this(false);
}
......
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.inventory.Inventory;
import emu.grasscutter.game.inventory.ItemType;
import java.util.List;
@Command(label = "clearweapons", usage = "clearweapons",
description = "Deletes all unequipped and unlocked weapons, including yellow rarity ones from your inventory",
aliases = {"clearwpns"}, permission = "player.clearweapons")
public final class ClearWeaponsCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
return; // TODO: clear player's weapons from console or other players
}
Inventory playerInventory = sender.getInventory();
playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_WEAPON)
.filter(item -> !item.isLocked() && !item.isEquipped())
.forEach(item -> playerInventory.removeItem(item, item.getCount()));
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.def.AvatarSkillDepotData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.server.packet.send.PacketAvatarSkillChangeNotify;
import emu.grasscutter.server.packet.send.PacketAvatarSkillUpgradeRsp;
import java.util.List;
@Command(label = "talent", usage = "talent <talentID> <value>",
description = "Set talent level for your current active character", permission = "player.settalent")
public class TalentCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
return;
}
if (args.size() < 1){
CommandHandler.sendMessage(sender, "To set talent level: /talent set <talentID> <value>");
CommandHandler.sendMessage(sender, "Another way to set talent level: /talent <n or e or q> <value>");
CommandHandler.sendMessage(sender, "To get talent ID: /talent getid");
return;
}
String cmdSwitch = args.get(0);
switch (cmdSwitch) {
default:
CommandHandler.sendMessage(sender, "To set talent level: /talent set <talentID> <value>");
CommandHandler.sendMessage(sender, "Another way to set talent level: /talent <n or e or q> <value>");
CommandHandler.sendMessage(sender, "To get talent ID: /talent getid");
return;
case "set":
try {
int skillId = Integer.parseInt(args.get(1));
int nextLevel = Integer.parseInt(args.get(2));
EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity();
GenshinAvatar avatar = entity.getAvatar();
int skillIdNorAtk = avatar.getData().getSkillDepot().getSkills().get(0);
int skillIdE = avatar.getData().getSkillDepot().getSkills().get(1);
int skillIdQ = avatar.getData().getSkillDepot().getEnergySkill();
int currentLevelNorAtk = avatar.getSkillLevelMap().get(skillIdNorAtk);
int currentLevelE = avatar.getSkillLevelMap().get(skillIdE);
int currentLevelQ = avatar.getSkillLevelMap().get(skillIdQ);
if (args.size() < 2){
CommandHandler.sendMessage(sender, "To set talent level: /talent set <talentID> <value>");
CommandHandler.sendMessage(sender, "To get talent ID: /talent getid");
return;
}
if (nextLevel > 16){
CommandHandler.sendMessage(sender, "Invalid talent level. Level should be lower than 16");
return;
}
if (skillId == skillIdNorAtk){
// Upgrade skill
avatar.getSkillLevelMap().put(skillIdNorAtk, nextLevel);
avatar.save();
// Packet
sender.sendPacket(new PacketAvatarSkillChangeNotify(avatar, skillIdNorAtk, currentLevelNorAtk, nextLevel));
sender.sendPacket(new PacketAvatarSkillUpgradeRsp(avatar, skillIdNorAtk, currentLevelNorAtk, nextLevel));
CommandHandler.sendMessage(sender, "Set talent Normal ATK to " + nextLevel + ".");
}
if (skillId == skillIdE){
// Upgrade skill
avatar.getSkillLevelMap().put(skillIdE, nextLevel);
avatar.save();
// Packet
sender.sendPacket(new PacketAvatarSkillChangeNotify(avatar, skillIdE, currentLevelE, nextLevel));
sender.sendPacket(new PacketAvatarSkillUpgradeRsp(avatar, skillIdE, currentLevelE, nextLevel));
CommandHandler.sendMessage(sender, "Set talent E to " + nextLevel + ".");
}
if (skillId == skillIdQ){
// Upgrade skill
avatar.getSkillLevelMap().put(skillIdQ, nextLevel);
avatar.save();
// Packet
sender.sendPacket(new PacketAvatarSkillChangeNotify(avatar, skillIdQ, currentLevelQ, nextLevel));
sender.sendPacket(new PacketAvatarSkillUpgradeRsp(avatar, skillIdQ, currentLevelQ, nextLevel));
CommandHandler.sendMessage(sender, "Set talent Q to " + nextLevel + ".");
}
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, "Invalid skill ID.");
return;
}
break;
case "n": case "e": case "q":
try {
EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity();
GenshinAvatar avatar = entity.getAvatar();
AvatarSkillDepotData SkillDepot = avatar.getData().getSkillDepot();
int skillId;
switch (cmdSwitch) {
default:
skillId = SkillDepot.getSkills().get(0);
break;
case "e":
skillId = SkillDepot.getSkills().get(1);
break;
case "q":
skillId = SkillDepot.getEnergySkill();
break;
}
int nextLevel = Integer.parseInt(args.get(1));
int currentLevel = avatar.getSkillLevelMap().get(skillId);
if (args.size() < 1){
CommandHandler.sendMessage(sender, "To set talent level: /talent <n or e or q> <value>");
return;
}
if (nextLevel > 16){
CommandHandler.sendMessage(sender, "Invalid talent level. Level should be lower than 16");
return;
}
// Upgrade skill
avatar.getSkillLevelMap().put(skillId, nextLevel);
avatar.save();
// Packet
sender.sendPacket(new PacketAvatarSkillChangeNotify(avatar, skillId, currentLevel, nextLevel));
sender.sendPacket(new PacketAvatarSkillUpgradeRsp(avatar, skillId, currentLevel, nextLevel));
CommandHandler.sendMessage(sender, "Set this talent to " + nextLevel + ".");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, "Invalid talent level.");
return;
}
break;
case "getid":
EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity();
GenshinAvatar avatar = entity.getAvatar();
int skillIdNorAtk = avatar.getData().getSkillDepot().getSkills().get(0);
int skillIdE = avatar.getData().getSkillDepot().getSkills().get(1);
int skillIdQ = avatar.getData().getSkillDepot().getEnergySkill();
CommandHandler.sendMessage(sender, "Normal Attack ID " + skillIdNorAtk + ".");
CommandHandler.sendMessage(sender, "E skill ID " + skillIdE + ".");
CommandHandler.sendMessage(sender, "Q skill ID " + skillIdQ + ".");
break;
}
}
}
......@@ -3,7 +3,7 @@ package emu.grasscutter.database;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
@Entity(value = "counters", noClassnameStored = true)
@Entity(value = "counters", useDiscriminator = false)
public class DatabaseCounter {
@Id
private String id;
......
......@@ -2,41 +2,33 @@ package emu.grasscutter.database;
import java.util.List;
import com.mongodb.WriteResult;
import dev.morphia.query.FindOptions;
import dev.morphia.query.Query;
import dev.morphia.query.internal.MorphiaCursor;
import com.mongodb.client.result.DeleteResult;
import dev.morphia.query.experimental.filters.Filters;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.inventory.GenshinItem;
public class DatabaseHelper {
protected static FindOptions FIND_ONE = new FindOptions().limit(1);
public final class DatabaseHelper {
public static Account createAccount(String username) {
return createAccountWithId(username, 0);
}
public static Account createAccountWithId(String username, int reservedId) {
// Unique names only
Account exists = DatabaseHelper.getAccountByName(username);
if (exists != null) {
return null;
}
// Make sure there are no id collisions
if (reservedId > 0) {
// Cannot make account with the same uid as the server console
if (reservedId == GenshinConstants.SERVER_CONSOLE_UID) {
return null;
}
exists = DatabaseHelper.getAccountByPlayerId(reservedId);
if (exists != null) {
return null;
......@@ -47,10 +39,10 @@ public class DatabaseHelper {
Account account = new Account();
account.setUsername(username);
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
if (reservedId > 0) {
account.setPlayerId(reservedId);
}
}
DatabaseHelper.saveAccount(account);
return account;
......@@ -63,65 +55,52 @@ public class DatabaseHelper {
if (exists != null) {
return null;
}
// Account
Account account = new Account();
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
account.setUsername(username);
account.setPassword(password);
DatabaseHelper.saveAccount(account);
DatabaseHelper.saveAccount(account);
return account;
}
public static void saveAccount(Account account) {
DatabaseManager.getAccountDatastore().save(account);
}
public static Account getAccountByName(String username) {
MorphiaCursor<Account> cursor = DatabaseManager.getAccountDatastore().createQuery(Account.class).field("username").equalIgnoreCase(username).find(FIND_ONE);
if (!cursor.hasNext()) return null;
return cursor.next();
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
}
public static Account getAccountByToken(String token) {
if (token == null) return null;
MorphiaCursor<Account> cursor = DatabaseManager.getAccountDatastore().createQuery(Account.class).field("token").equal(token).find(FIND_ONE);
if (!cursor.hasNext()) return null;
return cursor.next();
if(token == null) return null;
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
}
public static Account getAccountById(String uid) {
MorphiaCursor<Account> cursor = DatabaseManager.getAccountDatastore().createQuery(Account.class).field("_id").equal(uid).find(FIND_ONE);
if (!cursor.hasNext()) return null;
return cursor.next();
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
}
public static Account getAccountByPlayerId(int playerId) {
MorphiaCursor<Account> cursor = DatabaseManager.getAccountDatastore().createQuery(Account.class).field("playerId").equal(playerId).find(FIND_ONE);
if (!cursor.hasNext()) return null;
return cursor.next();
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
}
public static boolean deleteAccount(String username) {
Query<Account> q = DatabaseManager.getAccountDatastore().createQuery(Account.class).field("username").equalIgnoreCase(username);
return DatabaseManager.getDatastore().findAndDelete(q) != null;
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).delete().getDeletedCount() > 0;
}
public static GenshinPlayer getPlayerById(int id) {
Query<GenshinPlayer> query = DatabaseManager.getDatastore().createQuery(GenshinPlayer.class).field("_id").equal(id);
MorphiaCursor<GenshinPlayer> cursor = query.find(FIND_ONE);
if (!cursor.hasNext()) return null;
return cursor.next();
return DatabaseManager.getDatastore().find(GenshinPlayer.class).filter(Filters.eq("_id", id)).first();
}
public static boolean checkPlayerExists(int id) {
MorphiaCursor<GenshinPlayer> query = DatabaseManager.getDatastore().createQuery(GenshinPlayer.class).field("_id").equal(id).find(FIND_ONE);
return query.hasNext();
return DatabaseManager.getDatastore().find(GenshinPlayer.class).filter(Filters.eq("_id", id)).first() != null;
}
public static synchronized GenshinPlayer createPlayer(GenshinPlayer character, int reservedId) {
// Check if reserved id
int id = 0;
int id;
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId;
character.setUid(id);
......@@ -136,10 +115,10 @@ public class DatabaseHelper {
DatabaseManager.getDatastore().save(character);
return character;
}
public static synchronized int getNextPlayerId(int reservedId) {
// Check if reserved id
int id = 0;
int id;
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId;
} else {
......@@ -150,41 +129,37 @@ public class DatabaseHelper {
}
return id;
}
public static void savePlayer(GenshinPlayer character) {
DatabaseManager.getDatastore().save(character);
}
public static void saveAvatar(GenshinAvatar avatar) {
DatabaseManager.getDatastore().save(avatar);
}
public static List<GenshinAvatar> getAvatars(GenshinPlayer player) {
Query<GenshinAvatar> query = DatabaseManager.getDatastore().createQuery(GenshinAvatar.class).filter("ownerId", player.getUid());
return query.find().toList();
return DatabaseManager.getDatastore().find(GenshinAvatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
}
public static void saveItem(GenshinItem item) {
DatabaseManager.getDatastore().save(item);
}
public static boolean deleteItem(GenshinItem item) {
WriteResult result = DatabaseManager.getDatastore().delete(item);
DeleteResult result = DatabaseManager.getDatastore().delete(item);
return result.wasAcknowledged();
}
public static List<GenshinItem> getInventoryItems(GenshinPlayer player) {
Query<GenshinItem> query = DatabaseManager.getDatastore().createQuery(GenshinItem.class).filter("ownerId", player.getUid());
return query.find().toList();
return DatabaseManager.getDatastore().find(GenshinItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
}
public static List<Friendship> getFriends(GenshinPlayer player) {
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class).filter("ownerId", player.getUid());
return query.find().toList();
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
}
public static List<Friendship> getReverseFriends(GenshinPlayer player) {
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class).filter("friendId", player.getUid());
return query.find().toList();
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
}
public static void saveFriendship(Friendship friendship) {
......@@ -196,13 +171,9 @@ public class DatabaseHelper {
}
public static Friendship getReverseFriendship(Friendship friendship) {
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class);
query.and(
query.criteria("ownerId").equal(friendship.getFriendId()),
query.criteria("friendId").equal(friendship.getOwnerId())
);
MorphiaCursor<Friendship> reverseFriendship = query.find(FIND_ONE);
if (!reverseFriendship.hasNext()) return null;
return reverseFriendship.next();
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.and(
Filters.eq("ownerId", friendship.getFriendId()),
Filters.eq("friendId", friendship.getOwnerId())
)).first();
}
}
package emu.grasscutter.database;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCommandException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import dev.morphia.mapping.MapperOptions;
import dev.morphia.query.experimental.filters.Filters;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
......@@ -16,6 +19,7 @@ import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.inventory.GenshinItem;
public final class DatabaseManager {
private static MongoClient mongoClient;
private static MongoClient dispatchMongoClient;
......@@ -26,15 +30,11 @@ public final class DatabaseManager {
DatabaseCounter.class, Account.class, GenshinPlayer.class, GenshinAvatar.class, GenshinItem.class, Friendship.class
};
public static MongoClient getMongoClient() {
return mongoClient;
public static Datastore getDatastore() {
return datastore;
}
public static Datastore getDatastore() {
return datastore;
}
public static MongoDatabase getDatabase() {
public static MongoDatabase getDatabase() {
return getDatastore().getDatabase();
}
......@@ -50,27 +50,23 @@ public final class DatabaseManager {
public static void initialize() {
// Initialize
mongoClient = new MongoClient(new MongoClientURI(Grasscutter.getConfig().DatabaseUrl));
Morphia morphia = new Morphia();
// TODO Update when migrating to Morphia 2.0
morphia.getMapper().getOptions().setStoreEmpties(true);
morphia.getMapper().getOptions().setStoreNulls(false);
morphia.getMapper().getOptions().setDisableEmbeddedIndexes(true);
MongoClient mongoClient = MongoClients.create(Grasscutter.getConfig().DatabaseUrl);
// Map
morphia.map(mappedClasses);
// Build datastore
datastore = morphia.createDatastore(mongoClient, Grasscutter.getConfig().DatabaseCollection);
// Set mapper options.
MapperOptions mapperOptions = MapperOptions.builder()
.storeEmpties(true).storeNulls(false).build();
// Create data store.
datastore = Morphia.createDatastore(mongoClient, Grasscutter.getConfig().DatabaseCollection, mapperOptions);
// Map classes.
datastore.getMapper().map(mappedClasses);
// Ensure indexes
try {
datastore.ensureIndexes();
} catch (MongoCommandException e) {
Grasscutter.getLogger().info("Mongo index error: ", e);
} catch (MongoCommandException exception) {
Grasscutter.getLogger().info("Mongo index error: ", exception);
// Duplicate index error
if (e.getCode() == 85) {
if (exception.getCode() == 85) {
// Drop all indexes and re add them
MongoIterable<String> collections = datastore.getDatabase().listCollectionNames();
for (String name : collections) {
......@@ -82,8 +78,8 @@ public final class DatabaseManager {
}
if(Grasscutter.getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
dispatchMongoClient = new MongoClient(new MongoClientURI(Grasscutter.getConfig().getGameServerOptions().DispatchServerDatabaseUrl));
dispatchDatastore = morphia.createDatastore(dispatchMongoClient, Grasscutter.getConfig().getGameServerOptions().DispatchServerDatabaseCollection);
dispatchMongoClient = MongoClients.create(Grasscutter.getConfig().getGameServerOptions().DispatchServerDatabaseUrl);
dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, Grasscutter.getConfig().getGameServerOptions().DispatchServerDatabaseCollection);
// Ensure indexes for dispatch server
try {
......@@ -103,9 +99,9 @@ public final class DatabaseManager {
}
}
}
public static synchronized int getNextId(Class<?> c) {
DatabaseCounter counter = getDatastore().createQuery(DatabaseCounter.class).field("_id").equal(c.getSimpleName()).find().tryNext();
DatabaseCounter counter = getDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getName())).first();
if (counter == null) {
counter = new DatabaseCounter(c.getSimpleName());
}
......@@ -115,7 +111,7 @@ public final class DatabaseManager {
getDatastore().save(counter);
}
}
public static synchronized int getNextId(Object o) {
return getNextId(o.getClass());
}
......
package emu.grasscutter.game;
import dev.morphia.annotations.AlsoLoad;
import dev.morphia.annotations.Collation;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.PreLoad;
import dev.morphia.annotations.*;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils;
import dev.morphia.annotations.IndexOptions;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.DBObject;
@Entity(value = "accounts", noClassnameStored = true)
@Entity(value = "accounts", useDiscriminator = false)
public class Account {
@Id private String id;
......@@ -31,7 +27,7 @@ public class Account {
private String token;
private String sessionKey; // Session token for dispatch server
private List<String> permissions;
@Deprecated
public Account() {
this.permissions = new ArrayList<>();
......@@ -122,15 +118,15 @@ public class Account {
return this.token;
}
public void save() {
DatabaseHelper.saveAccount(this);
}
@PreLoad
public void onLoad(DBObject dbObj) {
public void onLoad(Document document) {
// Grant the superuser permissions to accounts created before the permissions update
if (!dbObj.containsField("permissions")) {
if (!document.containsKey("permissions")) {
this.addPermission("*");
}
}
public void save() {
DatabaseHelper.saveAccount(this);
}
}
......@@ -18,6 +18,7 @@ import emu.grasscutter.game.friends.PlayerProfile;
import emu.grasscutter.game.gacha.PlayerGachaInfo;
import emu.grasscutter.game.inventory.GenshinItem;
import emu.grasscutter.game.inventory.Inventory;
import emu.grasscutter.game.player.PlayerBirthday;
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.net.packet.GenshinPacket;
......@@ -40,7 +41,7 @@ import emu.grasscutter.utils.Position;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@Entity(value = "players", noClassnameStored = true)
@Entity(value = "players", useDiscriminator = false)
public class GenshinPlayer {
@Id private int id;
@Indexed(options = @IndexOptions(unique = true)) private String accountId;
......@@ -52,6 +53,7 @@ public class GenshinPlayer {
private int nameCardId = 210001;
private Position pos;
private Position rotation;
private PlayerBirthday birthday;
private Map<Integer, Integer> properties;
private Set<Integer> nameCardList;
......@@ -121,6 +123,8 @@ public class GenshinPlayer {
this.combatInvokeHandler = new InvokeHandler(PacketCombatInvocationsNotify.class);
this.abilityInvokeHandler = new InvokeHandler(PacketAbilityInvocationsNotify.class);
this.clientAbilityInitFinishHandler = new InvokeHandler(PacketClientAbilityInitFinishNotify.class);
this.birthday = new PlayerBirthday();
}
// On player creation
......@@ -132,6 +136,7 @@ public class GenshinPlayer {
this.nickname = "Traveler";
this.signature = "";
this.teamManager = new TeamManager(this);
this.birthday = new PlayerBirthday();
this.setProperty(PlayerProperty.PROP_PLAYER_LEVEL, 1);
this.setProperty(PlayerProperty.PROP_IS_SPRING_AUTO_USE, 1);
this.setProperty(PlayerProperty.PROP_SPRING_AUTO_USE_PERCENT, 50);
......@@ -634,6 +639,15 @@ public class GenshinPlayer {
return onlineInfo.build();
}
public PlayerBirthday getBirthday(){
return this.birthday;
}
public void setBirthday(int d, int m) {
this.birthday = new PlayerBirthday(d, m);
this.updateProfile();
}
public SocialDetail.Builder getSocialDetail() {
SocialDetail.Builder social = SocialDetail.newBuilder()
.setUid(this.getUid())
......@@ -641,7 +655,7 @@ public class GenshinPlayer {
.setNickname(this.getNickname())
.setSignature(this.getSignature())
.setLevel(this.getLevel())
.setBirthday(Birthday.newBuilder())
.setBirthday(this.getBirthday().getFilledProtoWhenNotEmpty())
.setWorldLevel(this.getWorldLevel())
.setUnk1(1)
.setUnk3(1)
......
......@@ -3,10 +3,12 @@ package emu.grasscutter.game;
import java.util.ArrayList;
import java.util.List;
import dev.morphia.annotations.Entity;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.avatar.GenshinAvatar;
@Entity
public class TeamInfo {
private String name;
private List<Integer> avatars;
......
......@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
......@@ -41,6 +42,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
@Entity
public class TeamManager {
@Transient private GenshinPlayer player;
......
package emu.grasscutter.game.avatar;
import dev.morphia.annotations.Entity;
@Entity
public class AvatarProfileData {
private int avatarId;
private int level;
......
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