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); mongoLogger.setLevel(Level.OFF);
// Configure the reflector. // Configure the reflector.
reflector = new Reflections( reflector =
new ConfigurationBuilder() new Reflections(
.forPackage("emu.grasscutter") new ConfigurationBuilder()
.filterInputsBy(new FilterBuilder() .forPackage("emu.grasscutter")
.includePackage("emu.grasscutter") .filterInputsBy(
.excludePackage("emu.grasscutter.net.proto")) new FilterBuilder()
); .includePackage("emu.grasscutter")
.excludePackage("emu.grasscutter.net.proto")));
// Load server configuration. // Load server configuration.
Grasscutter.loadConfig(); Grasscutter.loadConfig();

View File

@ -1,12 +1,9 @@
package emu.grasscutter.net; package emu.grasscutter.net;
import java.net.InetSocketAddress;
import org.slf4j.Logger; 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 { public interface IKcpSession {
/** /**
* @return The session's unique logger. * @return The session's unique logger.
@ -18,9 +15,7 @@ public interface IKcpSession {
*/ */
InetSocketAddress getAddress(); InetSocketAddress getAddress();
/** /** Closes the server's connection to the client. */
* Closes the server's connection to the client.
*/
void close(); void close();
/** /**

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,14 +3,12 @@ package emu.grasscutter.server.game;
public interface IGameSession { public interface IGameSession {
/** /**
* Invoked when the server establishes a connection to the client. * 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(); void onConnected();
/** /** Invoked when the server loses connection to the client. */
* Invoked when the server loses connection to the client.
*/
void onDisconnected(); void onDisconnected();
/** /**