More parts switched to async

This commit is contained in:
Anon 2026-04-04 21:31:02 +02:00
parent 297d7dbc30
commit 4d800a31fc
6 changed files with 295 additions and 73 deletions

View file

@ -3974,6 +3974,11 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
/// <returns>True if login successful</returns>
public bool Login(PlayerKeyPair? playerKeyPair, SessionToken session, bool isTransfer = false)
{
return LoginAsync(playerKeyPair, session, isTransfer).GetAwaiter().GetResult();
}
private async Task<bool> LoginAsync(PlayerKeyPair? playerKeyPair, SessionToken session, bool isTransfer = false)
{
int nextState = isTransfer && protocolVersion >= MC_1_20_6_Version ? 3 : 2;
@ -4052,7 +4057,7 @@ namespace MinecraftClient.Protocol.Handlers
// 3. Encryption Request - 9. Login Acknowledged
while (true)
{
var (packetId, packetData) = ReadNextPacket();
var (packetId, packetData) = await ReadNextPacketAsync(CancellationToken.None);
switch (packetId)
{
@ -4075,7 +4080,7 @@ namespace MinecraftClient.Protocol.Handlers
if (protocolVersion >= MC_1_20_6_Version)
shouldAuthetnicate = dataTypes.ReadNextBool(packetData);
return StartEncryption(handler.GetUserUuidStr(), handler.GetSessionID(),
return await StartEncryptionAsync(handler.GetUserUuidStr(), handler.GetSessionID(),
Config.Main.General.AccountType, token, serverId,
serverPublicKey, playerKeyPair, session, shouldAuthetnicate);
}
@ -4091,7 +4096,7 @@ namespace MinecraftClient.Protocol.Handlers
if (protocolVersion >= MC_1_20_2_Version)
SendPacket(0x03, new List<byte>());
if (!pForge.CompleteForgeHandshake())
if (!await pForge.CompleteForgeHandshakeAsync())
{
log.Error($"§8{Translations.error_forge}");
return false;
@ -4112,7 +4117,7 @@ namespace MinecraftClient.Protocol.Handlers
/// Start network encryption. Automatically called by Login() if the server requests encryption.
/// </summary>
/// <returns>True if encryption was successful</returns>
private bool StartEncryption(string uuid, string sessionID, LoginType type, byte[] token, string serverIDhash,
private async Task<bool> StartEncryptionAsync(string uuid, string sessionID, LoginType type, byte[] token, string serverIDhash,
byte[] serverPublicKey, PlayerKeyPair? playerKeyPair, SessionToken session, bool shouldAuthetnicate)
{
var RSAService = CryptoHandler.DecodeRSAPublicKey(serverPublicKey)!;
@ -4140,7 +4145,7 @@ namespace MinecraftClient.Protocol.Handlers
if (needCheckSession)
{
var serverHash = CryptoHandler.GetServerHash(serverIDhash, serverPublicKey, secretKey);
if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash, type))
if (await ProtocolHandler.SessionCheckAsync(uuid, sessionID, serverHash, type))
{
session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey;
@ -4190,7 +4195,7 @@ namespace MinecraftClient.Protocol.Handlers
int loopPrevention = ushort.MaxValue;
while (true)
{
var (packetId, packetData) = ReadNextPacket();
var (packetId, packetData) = await ReadNextPacketAsync(CancellationToken.None);
if (packetId < 0 || loopPrevention-- < 0) // Failed to read packet or too many iterations (issue #1150)
{
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost,
@ -4241,7 +4246,7 @@ namespace MinecraftClient.Protocol.Handlers
handler.OnLoginSuccess(uuidReceived, userName, playerProperty);
if (!pForge.CompleteForgeHandshake())
if (!await pForge.CompleteForgeHandshakeAsync())
{
log.Error($"§8{Translations.error_forge_encrypt}");
return false;

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MinecraftClient.Protocol.Handlers.Forge;
using MinecraftClient.Protocol.Message;
using MinecraftClient.Scripting;
@ -21,6 +22,7 @@ namespace MinecraftClient.Protocol.Handlers
private readonly ForgeInfo? forgeInfo = forgeInfo;
private FMLHandshakeClientState fmlHandshakeState = FMLHandshakeClientState.START;
private DateTime? pendingServerDataAckAt;
private bool ForgeEnabled() { return forgeInfo is not null; }
/// <summary>
@ -40,12 +42,17 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
/// <returns>Whether the handshake was successful.</returns>
public bool CompleteForgeHandshake()
{
return CompleteForgeHandshakeAsync().GetAwaiter().GetResult();
}
public async Task<bool> CompleteForgeHandshakeAsync(CancellationToken cancellationToken = default)
{
if (ForgeEnabled() && forgeInfo!.Version == FMLVersion.FML)
{
while (fmlHandshakeState != FMLHandshakeClientState.DONE)
{
(int packetID, Queue<byte> packetData) = protocol18.ReadNextPacket();
(int packetID, Queue<byte> packetData) = await protocol18.ReadNextPacketAsync(cancellationToken);
if (packetID == 0x40) // Disconnect
{
@ -56,12 +63,31 @@ namespace MinecraftClient.Protocol.Handlers
{
// Send back regular packet to the vanilla protocol handler
protocol18.HandlePacket(packetID, packetData);
await FlushPendingHandshakeActionsAsync(cancellationToken);
}
}
}
return true;
}
private async Task FlushPendingHandshakeActionsAsync(CancellationToken cancellationToken)
{
if (!pendingServerDataAckAt.HasValue)
return;
TimeSpan delay = pendingServerDataAckAt.Value - DateTime.UtcNow;
if (delay > TimeSpan.Zero)
await Task.Delay(delay, cancellationToken);
if (Settings.Config.Logging.DebugMessages)
ConsoleIO.WriteLineFormatted("§8" + Translations.forge_accept, acceptnewlines: true);
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
new byte[] { (byte)FMLHandshakeClientState.WAITINGSERVERDATA });
pendingServerDataAckAt = null;
}
/// <summary>
/// Read Forge VarShort field
/// </summary>
@ -145,16 +171,9 @@ namespace MinecraftClient.Protocol.Handlers
if (discriminator != FMLHandshakeDiscriminator.ModList)
return false;
Thread.Sleep(2000);
if (Settings.Config.Logging.DebugMessages)
ConsoleIO.WriteLineFormatted("§8" + Translations.forge_accept, acceptnewlines: true);
// Tell the server that yes, we are OK with the mods it has
// even though we don't actually care what mods it has.
SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck,
new byte[] { (byte)FMLHandshakeClientState.WAITINGSERVERDATA });
pendingServerDataAckAt = DateTime.UtcNow.AddSeconds(2);
fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERCOMPLETE;
return false;
case FMLHandshakeClientState.WAITINGSERVERCOMPLETE: