Merge remote-tracking branch 'origin/unstable' into unstable

This commit is contained in:
KingRainbow44 2024-07-06 23:04:06 -04:00
commit ea84789c47
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
9 changed files with 50 additions and 60 deletions

View File

@ -78,13 +78,14 @@ public final class Grasscutter {
mongoLogger.setLevel(Level.OFF);
// Configure the reflector.
reflector = new Reflections(
new ConfigurationBuilder()
.forPackage("emu.grasscutter")
.filterInputsBy(new FilterBuilder()
.includePackage("emu.grasscutter")
.excludePackage("emu.grasscutter.net.proto"))
);
reflector =
new Reflections(
new ConfigurationBuilder()
.forPackage("emu.grasscutter")
.filterInputsBy(
new FilterBuilder()
.includePackage("emu.grasscutter")
.excludePackage("emu.grasscutter.net.proto")));
// Load server configuration.
Grasscutter.loadConfig();

View File

@ -1,12 +1,9 @@
package emu.grasscutter.net;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import java.net.InetSocketAddress;
/**
* This is most closely related to the previous `KcpTunnel` interface.
*/
/** This is most closely related to the previous `KcpTunnel` interface. */
public interface IKcpSession {
/**
* @return The session's unique logger.
@ -18,9 +15,7 @@ public interface IKcpSession {
*/
InetSocketAddress getAddress();
/**
* Closes the server's connection to the client.
*/
/** Closes the server's connection to the client. */
void close();
/**

View File

@ -2,13 +2,12 @@ package emu.grasscutter.net;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.game.GameServer;
import java.net.InetSocketAddress;
public interface INetworkTransport {
/**
* Waits for the server to be active.
* This should be used to ensure that the server is ready to accept connections.
* Waits for the server to be active. This should be used to ensure that the server is ready to
* accept connections.
*/
default GameServer waitForServer() throws InterruptedException {
int depth = 0;
@ -32,8 +31,8 @@ public interface INetworkTransport {
void start(InetSocketAddress listening);
/**
* This is invoked when the transport should stop listening for incoming connections.
* This should also close all active connections.
* This is invoked when the transport should stop listening for incoming connections. This should
* also close all active connections.
*/
void shutdown();
}

View File

@ -1,19 +1,16 @@
package emu.grasscutter.net.impl;
import emu.grasscutter.net.IKcpSession;
import emu.grasscutter.net.INetworkTransport;
import io.netty.buffer.Unpooled;
import java.net.InetSocketAddress;
import kcp.highway.Ukcp;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
/**
* This is the default implementation of a KCP session.
* It uses {@link Ukcp} as the underlying wrapper.
* This is the default implementation of a KCP session. It uses {@link Ukcp} as the underlying
* wrapper.
*/
@Getter
public class KcpSessionImpl implements IKcpSession {

View File

@ -1,26 +1,25 @@
package emu.grasscutter.net.impl;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import emu.grasscutter.net.INetworkTransport;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.utils.Utils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoop;
import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import kcp.highway.ChannelConfig;
import kcp.highway.KcpListener;
import kcp.highway.KcpServer;
import kcp.highway.Ukcp;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import static emu.grasscutter.config.Configuration.GAME_INFO;
/**
* The default implementation of a {@link INetworkTransport}.
* Uses {@link KcpServer} as the underlying transport.
* The default implementation of a {@link INetworkTransport}. Uses {@link KcpServer} as the
* underlying transport.
*/
@Slf4j
public class NetworkTransportImpl extends KcpServer implements INetworkTransport {
@ -87,15 +86,16 @@ public class NetworkTransportImpl extends KcpServer implements INetworkTransport
// Copy the buffer to avoid reference issues.
var data = Utils.byteBufToArray(byteBuf);
transport.networkLoop.submit(() -> {
// Fun fact: if we don't catch exceptions here,
// we run the risk of locking the entire network loop.
try {
session.onReceived(data);
} catch (Exception ex) {
session.getLogger().warn("Unable to handle received data.", ex);
}
});
transport.networkLoop.submit(
() -> {
// Fun fact: if we don't catch exceptions here,
// we run the risk of locking the entire network loop.
try {
session.onReceived(data);
} catch (Exception ex) {
session.getLogger().warn("Unable to handle received data.", ex);
}
});
} catch (Exception ex) {
NetworkTransportImpl.log.warn("Unable to handle received data.", ex);
}

View File

@ -508,8 +508,7 @@ public class SceneScriptManager {
block -> {
block.load(sceneId, meta.context);
if (block.groups == null) {
Grasscutter.getLogger()
.error("block.groups null for block {}", block.id);
Grasscutter.getLogger().error("block.groups null for block {}", block.id);
return;
}

View File

@ -56,9 +56,7 @@ import org.jetbrains.annotations.*;
@Getter
@Slf4j
public final class GameServer implements Iterable<Player> {
/**
* This can be set by plugins to change the network transport implementation.
*/
/** This can be set by plugins to change the network transport implementation. */
@Setter private static Class<? extends INetworkTransport> transport = NetworkTransportImpl.class;
// Game server base
@ -145,9 +143,7 @@ public final class GameServer implements Iterable<Player> {
// Create the network transport.
INetworkTransport transport;
try {
transport = GameServer.transport
.getDeclaredConstructor()
.newInstance();
transport = GameServer.transport.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
log.error("Failed to create network transport.", ex);
transport = new NetworkTransportImpl();

View File

@ -74,8 +74,9 @@ public class GameSession implements IGameSession {
}
public void logPacket(String sendOrRecv, int opcode, byte[] payload) {
this.session.getLogger().info("{}: {} ({})",
sendOrRecv, PacketOpcodesUtils.getOpcodeName(opcode), opcode);
this.session
.getLogger()
.info("{}: {} ({})", sendOrRecv, PacketOpcodesUtils.getOpcodeName(opcode), opcode);
if (GAME_INFO.isShowPacketPayload) System.out.println(Utils.bytesToHex(payload));
}
@ -151,7 +152,9 @@ public class GameSession implements IGameSession {
int const1 = packet.readShort();
if (const1 != 17767) {
if (allDebug) {
this.session.getLogger().error("Invalid packet header received: got {}, expected 17767", const1);
this.session
.getLogger()
.error("Invalid packet header received: got {}, expected 17767", const1);
}
return; // Bad packet
}
@ -169,7 +172,9 @@ public class GameSession implements IGameSession {
int const2 = packet.readShort();
if (const2 != -30293) {
if (allDebug) {
this.session.getLogger().error("Invalid packet footer received: got {}, expected -30293", const2);
this.session
.getLogger()
.error("Invalid packet footer received: got {}, expected -30293", const2);
}
return; // Bad packet
}

View File

@ -3,14 +3,12 @@ package emu.grasscutter.server.game;
public interface IGameSession {
/**
* Invoked when the server establishes a connection to the client.
* <p>
* This is invoked after the KCP handshake is completed.
*
* <p>This is invoked after the KCP handshake is completed.
*/
void onConnected();
/**
* Invoked when the server loses connection to the client.
*/
/** Invoked when the server loses connection to the client. */
void onDisconnected();
/**