Skip to content

Commit 76cb299

Browse files
committed
simplify
1 parent 6a81250 commit 76cb299

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

TACTLib/Client/ClientHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public ClientHandler(string? basePath, ClientCreateArgs createArgs) {
242242

243243
public IProductHandler? CreateProductHandler() {
244244
using var _ = new PerfCounter("ProductHandlerFactory::GetHandler`TACTProduct`ClientHandler`Stream");
245-
return ProductHandlerFactory.GetHandler(Product, this, ConfigHandler.BuildConfig.Root.ContentKey == default ? null : OpenCKey(ConfigHandler.BuildConfig.Root.ContentKey)!);
245+
return ProductHandlerFactory.GetHandler(Product, this, OpenCKey(ConfigHandler.BuildConfig.Root.ContentKey));
246246
}
247247

248248
private bool CanShareCDNData([NotNullWhen(true)] ClientHandler? other) {

TACTLib/Core/Key/FullKey.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ public static int FullKeyCompare(FullKey left, FullKey right) {
7676
}
7777

7878
public bool Equals(FullKey other) {
79-
var span = MemoryMarshal.Cast<byte, ulong>(this);
80-
var otherSpan = MemoryMarshal.Cast<byte, ulong>(other);
81-
return span[0] == otherSpan[0] && span[1] == otherSpan[1];
79+
return FullKeyCompare(this, other) == 0;
8280
}
8381

8482
public override bool Equals(object? obj) => obj is FullKey other && Equals(other);

TACTLib/Core/Key/TruncatedKey.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace TACTLib.Core.Key {
1515
[InlineArray(CASC_TRUNCATED_KEY_SIZE)]
1616
[DebuggerDisplay("{ToHexString()}")]
1717
[SuppressMessage("ReSharper", "UseSymbolAlias")]
18-
public struct TruncatedKey : IComparable<TruncatedKey> {
18+
public struct TruncatedKey : IComparable<TruncatedKey>, IEquatable<TruncatedKey> {
1919
// ReSharper disable once InconsistentNaming
2020
/// <summary>Encoding Key size, in bytes</summary>
2121
public const int CASC_TRUNCATED_KEY_SIZE = 9;
@@ -70,5 +70,20 @@ public static int TruncatedKeyCompare(TruncatedKey left, TruncatedKey right) {
7070
var rightU1 = MemoryMarshal.Read<byte>(rightSpan.Slice(8));
7171
return leftU1.CompareTo(rightU1);
7272
}
73+
74+
public bool Equals(TruncatedKey other) {
75+
return TruncatedKeyCompare(this, other) == 0;
76+
}
77+
78+
public override bool Equals(object? obj) => obj is TruncatedKey other && Equals(other);
79+
80+
public static bool operator ==(TruncatedKey left, TruncatedKey right) => left.Equals(right);
81+
public static bool operator !=(TruncatedKey left, TruncatedKey right) => !(left == right);
82+
83+
public override int GetHashCode() {
84+
var h = new HashCode();
85+
h.AddBytes(this);
86+
return h.ToHashCode();
87+
}
7388
}
7489
}

TACTLib/Protocol/CDNIndexHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private void ParseIndex(Stream stream, int archiveIndex)
225225
if (archiveIndex == ARCHIVE_ID_LOOSE) {
226226
ReadOnlySpan<LooseFileEntry> pageEntries = MemoryMarshal.Cast<byte, LooseFileEntry>(page);
227227
for (var entryIdx = 0; entryIdx < pageEntries.Length; entryIdx++) {
228-
if (pageEntries[entryIdx].m_eKey.CompareTo(default) != 0) {
228+
if (pageEntries[entryIdx].m_eKey != default) {
229229
// has value
230230
continue;
231231
}
@@ -252,7 +252,7 @@ private void ParseIndex(Stream stream, int archiveIndex)
252252

253253
for (var entryIdx = 0; entryIdx < maxEntryCount; entryIdx++) {
254254
var key = SpanHelper.ReadStruct<FullKey>(ref pageSpan);
255-
if (key.CompareTo(default) == 0) {
255+
if (key == default) {
256256
// has no value, end of the list
257257
intermediateEntries = intermediateEntries.AsSpan(0, entryIdx).ToArray();
258258
break;

0 commit comments

Comments
 (0)