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>
/// Blocks contained into the chunk
/// </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>
/// Read, or set the specified block
@ -43,22 +34,22 @@ namespace MinecraftClient.Mapping
get
{
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)
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)
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];
}
set
{
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)
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)
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;
}

View file

@ -134,10 +134,10 @@ namespace MinecraftClient.Mapping
/// <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>
/// <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();
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);
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="ct">Token for stopping computation after a certain time</param>
/// <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
if (minOffset > maxOffset)

View file

@ -55,7 +55,7 @@ namespace MinecraftClient
private bool locationReceived = false;
private World world = new();
private Queue<Location> steps;
private Queue<Location> path;
private Queue<Location>? path;
private Location location;
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

View file

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

View file

@ -148,7 +148,7 @@ namespace MinecraftClient.Protocol
if (this.precedingSignature != null && !this.precedingSignature.SequenceEqual(precedingSignature!))
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.precedingSignature = headerSignature;

View file

@ -88,9 +88,12 @@ namespace MinecraftClient.Protocol.Keys
/// <param name="bodyDigest">Message body hash</param>
/// <param name="signature">Message signature</param>
/// <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);
}
}