This commit is contained in:
BruceChen 2022-09-04 17:34:12 +08:00
parent bcded40476
commit db17babe58
6 changed files with 23 additions and 26 deletions

View file

@ -19,17 +19,8 @@ namespace MinecraftClient.Mapping
/// <summary> /// <summary>
/// Blocks contained into the chunk /// Blocks contained into the chunk
/// </summary> /// </summary>
private readonly Block[] blocks; private readonly Block[] blocks = new Block[SizeY * SizeZ * SizeX];
public Chunk()
{
this.blocks = new Block[SizeY * SizeZ * SizeX];
}
public Chunk(Block[] blocks)
{
this.blocks = blocks;
}
/// <summary> /// <summary>
/// Read, or set the specified block /// Read, or set the specified block
@ -43,22 +34,22 @@ namespace MinecraftClient.Mapping
get get
{ {
if (blockX < 0 || blockX >= SizeX) if (blockX < 0 || blockX >= SizeX)
throw new ArgumentOutOfRangeException("blockX", "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); throw new ArgumentOutOfRangeException(nameof(blockX), "Must be between 0 and " + (SizeX - 1) + " (inclusive)");
if (blockY < 0 || blockY >= SizeY) if (blockY < 0 || blockY >= SizeY)
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); throw new ArgumentOutOfRangeException(nameof(blockY), "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
if (blockZ < 0 || blockZ >= SizeZ) if (blockZ < 0 || blockZ >= SizeZ)
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); throw new ArgumentOutOfRangeException(nameof(blockZ), "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
return blocks[(blockY << 8) | (blockZ << 4) | blockX]; return blocks[(blockY << 8) | (blockZ << 4) | blockX];
} }
set set
{ {
if (blockX < 0 || blockX >= SizeX) if (blockX < 0 || blockX >= SizeX)
throw new ArgumentOutOfRangeException("blockX", "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); throw new ArgumentOutOfRangeException(nameof(blockX), "Must be between 0 and " + (SizeX - 1) + " (inclusive)");
if (blockY < 0 || blockY >= SizeY) if (blockY < 0 || blockY >= SizeY)
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); throw new ArgumentOutOfRangeException(nameof(blockY), "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
if (blockZ < 0 || blockZ >= SizeZ) if (blockZ < 0 || blockZ >= SizeZ)
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); throw new ArgumentOutOfRangeException(nameof(blockZ), "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
blocks[(blockY << 8) | (blockZ << 4) | blockX] = value; blocks[(blockY << 8) | (blockZ << 4) | blockX] = value;
} }

View file

@ -134,10 +134,10 @@ namespace MinecraftClient.Mapping
/// <param name="timeout">How long to wait before stopping computation</param> /// <param name="timeout">How long to wait before stopping computation</param>
/// <remarks>When location is unreachable, computation will reach timeout, then optionally fallback to a close location within maxOffset</remarks> /// <remarks>When location is unreachable, computation will reach timeout, then optionally fallback to a close location within maxOffset</remarks>
/// <returns>A list of locations, or null if calculation failed</returns> /// <returns>A list of locations, or null if calculation failed</returns>
public static Queue<Location> CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout) public static Queue<Location>? CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout)
{ {
CancellationTokenSource cts = new CancellationTokenSource(); CancellationTokenSource cts = new CancellationTokenSource();
Task<Queue<Location>> pathfindingTask = Task.Factory.StartNew(() => Movement.CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token)); Task<Queue<Location>?> pathfindingTask = Task.Factory.StartNew(() => Movement.CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token));
pathfindingTask.Wait(timeout); pathfindingTask.Wait(timeout);
if (!pathfindingTask.IsCompleted) if (!pathfindingTask.IsCompleted)
{ {
@ -161,7 +161,7 @@ namespace MinecraftClient.Mapping
/// <param name="minOffset">Do not get closer of destination than specified distance</param> /// <param name="minOffset">Do not get closer of destination than specified distance</param>
/// <param name="ct">Token for stopping computation after a certain time</param> /// <param name="ct">Token for stopping computation after a certain time</param>
/// <returns>A list of locations, or null if calculation failed</returns> /// <returns>A list of locations, or null if calculation failed</returns>
public static Queue<Location> CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, CancellationToken ct) public static Queue<Location>? CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, CancellationToken ct)
{ {
// This is a bad configuration // This is a bad configuration
if (minOffset > maxOffset) if (minOffset > maxOffset)

View file

@ -55,7 +55,7 @@ namespace MinecraftClient
private bool locationReceived = false; private bool locationReceived = false;
private World world = new(); private World world = new();
private Queue<Location> steps; private Queue<Location> steps;
private Queue<Location> path; private Queue<Location>? path;
private Location location; private Location location;
private float? _yaw; // Used for calculation ONLY!!! Doesn't reflect the client yaw private float? _yaw; // Used for calculation ONLY!!! Doesn't reflect the client yaw
private float? _pitch; // Used for calculation ONLY!!! Doesn't reflect the client pitch private float? _pitch; // Used for calculation ONLY!!! Doesn't reflect the client pitch

View file

@ -572,6 +572,7 @@ namespace MinecraftClient.Protocol.Handlers
byte[] bodyDigest = dataTypes.ReadNextByteArray(packetData); byte[] bodyDigest = dataTypes.ReadNextByteArray(packetData);
bool verifyResult; bool verifyResult;
if (!isOnlineMode) if (!isOnlineMode)
verifyResult = false; verifyResult = false;
else if (senderUUID == handler.GetUserUuid()) else if (senderUUID == handler.GetUserUuid())
@ -579,6 +580,7 @@ namespace MinecraftClient.Protocol.Handlers
else else
{ {
PlayerInfo? player = handler.GetPlayerInfo(senderUUID); PlayerInfo? player = handler.GetPlayerInfo(senderUUID);
if (player == null || !player.IsMessageChainLegal()) if (player == null || !player.IsMessageChainLegal())
verifyResult = false; verifyResult = false;
else else
@ -586,7 +588,7 @@ namespace MinecraftClient.Protocol.Handlers
bool lastVerifyResult = player.IsMessageChainLegal(); bool lastVerifyResult = player.IsMessageChainLegal();
verifyResult = player.VerifyMessageHead(ref precedingSignature, ref headerSignature, ref bodyDigest); verifyResult = player.VerifyMessageHead(ref precedingSignature, ref headerSignature, ref bodyDigest);
if (lastVerifyResult && !verifyResult) if (lastVerifyResult && !verifyResult)
log.Warn("Player " + player.DisplayName + "'s message chain is broken!"); log.Warn("Player " + player.Name + "'s message chain is broken!");
} }
} }
} }
@ -1705,7 +1707,7 @@ namespace MinecraftClient.Protocol.Handlers
if (protocolVersion >= MC_1_19_2_Version) if (protocolVersion >= MC_1_19_2_Version)
fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.SignatureV2!)); // Public key signature received from Microsoft API fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.SignatureV2!)); // Public key signature received from Microsoft API
else else
fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.Signature!)); // Public key signature received from Microsoft API fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.Signature!)); // Public key signature received from Microsoft API
} }
} }
if (protocolVersion >= MC_1_19_2_Version) if (protocolVersion >= MC_1_19_2_Version)
@ -1769,7 +1771,6 @@ namespace MinecraftClient.Protocol.Handlers
if (serverIDhash != "-") if (serverIDhash != "-")
{ {
log.Info(Translations.Get("mcc.session")); log.Info(Translations.Get("mcc.session"));
string serverHash = CryptoHandler.getServerHash(serverIDhash, serverPublicKey, secretKey);
bool needCheckSession = true; bool needCheckSession = true;
if (session.ServerPublicKey != null && session.SessionPreCheckTask != null if (session.ServerPublicKey != null && session.SessionPreCheckTask != null
@ -1782,6 +1783,8 @@ namespace MinecraftClient.Protocol.Handlers
if (needCheckSession) if (needCheckSession)
{ {
string serverHash = CryptoHandler.getServerHash(serverIDhash, serverPublicKey, secretKey);
if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash)) if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash))
{ {
session.ServerIDhash = serverIDhash; session.ServerIDhash = serverIDhash;

View file

@ -148,7 +148,7 @@ namespace MinecraftClient.Protocol
if (this.precedingSignature != null && !this.precedingSignature.SequenceEqual(precedingSignature!)) if (this.precedingSignature != null && !this.precedingSignature.SequenceEqual(precedingSignature!))
return false; return false;
bool res = PublicKey.VerifyHeader(ref bodyDigest, ref headerSignature); bool res = PublicKey.VerifyHeader(Uuid, ref bodyDigest, ref headerSignature, ref precedingSignature);
this.lastMessageVerified = res; this.lastMessageVerified = res;
this.precedingSignature = headerSignature; this.precedingSignature = headerSignature;

View file

@ -88,9 +88,12 @@ namespace MinecraftClient.Protocol.Keys
/// <param name="bodyDigest">Message body hash</param> /// <param name="bodyDigest">Message body hash</param>
/// <param name="signature">Message signature</param> /// <param name="signature">Message signature</param>
/// <returns>Is this message header vaild</returns> /// <returns>Is this message header vaild</returns>
public bool VerifyHeader(ref byte[] bodyDigest, ref byte[] signature) public bool VerifyHeader(Guid uuid, ref byte[] bodyDigest, ref byte[] signature, ref byte[]? precedingSignature)
{ {
return VerifyData(bodyDigest, signature);
byte[] msgSignData = KeyUtils.GetSignatureData(precedingSignature, uuid, bodyDigest);
return VerifyData(msgSignData, signature);
} }
} }