diff --git a/src/main/java/emu/nebula/game/player/Player.java b/src/main/java/emu/nebula/game/player/Player.java index c0c43e0..a940fce 100644 --- a/src/main/java/emu/nebula/game/player/Player.java +++ b/src/main/java/emu/nebula/game/player/Player.java @@ -2,6 +2,7 @@ package emu.nebula.game.player; import java.util.HashSet; import java.util.Set; +import java.util.Stack; import dev.morphia.annotations.Entity; import dev.morphia.annotations.Id; @@ -21,6 +22,7 @@ import emu.nebula.game.mail.Mailbox; import emu.nebula.game.story.StoryManager; import emu.nebula.game.tower.StarTowerManager; import emu.nebula.net.GameSession; +import emu.nebula.net.NetMsgPacket; import emu.nebula.proto.PlayerData.DictionaryEntry; import emu.nebula.proto.PlayerData.DictionaryTab; import emu.nebula.proto.PlayerData.PlayerInfo; @@ -32,6 +34,7 @@ import emu.nebula.proto.Public.WorldClass; import emu.nebula.proto.Public.Title; import lombok.Getter; +import us.hebi.quickbuf.ProtoMessage; import us.hebi.quickbuf.RepeatedInt; @Getter @@ -72,11 +75,16 @@ public class Player implements GameDatabaseObject { private transient InstanceManager instanceManager; private transient StoryManager storyManager; + // Next packages + private transient Stack nextPackages; + @Deprecated // Morphia only public Player() { this.sessions = new HashSet<>(); this.characters = new CharacterStorage(this); this.gachaManager = new GachaManager(this); + + this.nextPackages = new Stack<>(); } public Player(Account account, String name, boolean gender) { @@ -430,6 +438,12 @@ public class Player implements GameDatabaseObject { this.storyManager = this.loadManagerFromDatabase(StoryManager.class); } + // Next packages + + public void addNextPackage(int msgId, ProtoMessage proto) { + this.getNextPackages().add(new NetMsgPacket(msgId, proto)); + } + // Proto public PlayerInfo toProto() { diff --git a/src/main/java/emu/nebula/net/GameSession.java b/src/main/java/emu/nebula/net/GameSession.java index eb83b34..5b35efe 100644 --- a/src/main/java/emu/nebula/net/GameSession.java +++ b/src/main/java/emu/nebula/net/GameSession.java @@ -167,13 +167,44 @@ public class GameSession { this.getPlayer().getMailbox().clearNewState(); // Send mail state notify - byte[] nextPackage = PacketHelper.encodeMsg( - NetMsgId.mail_state_notify, - MailState.newInstance().setNew(true) + this.getPlayer().addNextPackage( + NetMsgId.mail_state_notify, + MailState.newInstance().setNew(true) ); + } + + // Set next package + if (this.getPlayer().getNextPackages().size() > 0) { + // Set current package + NetMsgPacket curPacket = null; - // Set via reflection - PacketHelper.setNextPackage(proto, nextPackage); + // Chain link next packages + while (getPlayer().getNextPackages().size() > 0) { + // Make sure the current packet has a nextPackage field + if (curPacket != null && !PacketHelper.hasNextPackageMethod(curPacket.getProto())) { + break; + } + + // Get current package + var nextPacket = getPlayer().getNextPackages().pop(); + + // Set cur packet if its null + if (curPacket == null) { + curPacket = nextPacket; + continue; + } + + // Set next package + PacketHelper.setNextPackage(nextPacket.getProto(), curPacket.toByteArray()); + + // Update next packet + curPacket = nextPacket; + } + + // Set next package of current proto via reflection + if (curPacket != null) { + PacketHelper.setNextPackage(proto, curPacket.toByteArray()); + } } } } diff --git a/src/main/java/emu/nebula/net/NetMsgPacket.java b/src/main/java/emu/nebula/net/NetMsgPacket.java new file mode 100644 index 0000000..6ad1c01 --- /dev/null +++ b/src/main/java/emu/nebula/net/NetMsgPacket.java @@ -0,0 +1,19 @@ +package emu.nebula.net; + +import lombok.Getter; +import us.hebi.quickbuf.ProtoMessage; + +@Getter +public class NetMsgPacket { + private int msgId; + private ProtoMessage proto; + + public NetMsgPacket(int msgId, ProtoMessage proto) { + this.msgId = msgId; + this.proto = proto; + } + + public byte[] toByteArray() { + return PacketHelper.encodeMsg(this.getMsgId(), this.getProto()); + } +} diff --git a/src/main/java/emu/nebula/net/PacketHelper.java b/src/main/java/emu/nebula/net/PacketHelper.java index 1f9ddc0..0f0eb1b 100644 --- a/src/main/java/emu/nebula/net/PacketHelper.java +++ b/src/main/java/emu/nebula/net/PacketHelper.java @@ -14,6 +14,8 @@ import us.hebi.quickbuf.ProtoSink; public class PacketHelper { private static Object2IntMap> methodIndexCache = new Object2IntOpenHashMap<>(); + // Next packages + public static void cacheProtos() { var classes = new Reflections(Nebula.class.getPackage().getName()).getSubTypesOf(ProtoMessage.class);