Skip to content

Commit 0ddb145

Browse files
committed
Cleanup
1 parent 78ec6a1 commit 0ddb145

File tree

12 files changed

+113
-118
lines changed

12 files changed

+113
-118
lines changed

TACTBench/BuildBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task LoadBuild()
3131
break;
3232
}
3333

34-
_build.LoadConfigs(_build.Settings.BuildConfig, _build.Settings.CDNConfig);
34+
_build.LoadConfigs(_build.Settings.BuildConfig!, _build.Settings.CDNConfig!);
3535
_build.Load();
3636
}
3737

TACTSharp.Native/TACTSharpNative.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static nint GetBuildString()
4545
if (build == null)
4646
throw new InvalidOperationException("TACTSharpNative is not initialized. Call Initialize first.");
4747

48-
var splitName = build.BuildConfig.Values["build-name"][0].Replace("WOW-", "").Split("patch");
48+
var splitName = build.BuildConfig!.Values["build-name"][0].Replace("WOW-", "").Split("patch");
4949
var buildString = splitName[1].Split("_")[0] + "." + splitName[0];
5050

5151
var buildStringPtr = Marshal.StringToHGlobalAnsi(buildString);

TACTSharp.Tests/ExtractionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void ExtractEXE()
2323
var filename = "WowB.exe";
2424
var expectedMD5 = "923754949b474d581fd9fdd2c1c32912";
2525

26-
var fileEntries = build.Install.Entries.Where(x => x.name.Equals(filename, StringComparison.InvariantCultureIgnoreCase)).ToList();
26+
var fileEntries = build.Install!.Entries.Where(x => x.name.Equals(filename, StringComparison.InvariantCultureIgnoreCase)).ToList();
2727
if (fileEntries.Count == 0)
2828
Assert.Fail("Failed to find " + filename + " in install");
2929

@@ -50,7 +50,7 @@ public void ExtractEXE()
5050
// Check if CKey is the one we expect it to be
5151
Assert.AreEqual(expectedMD5, Convert.ToHexStringLower(targetCKey));
5252

53-
var fileEncodingKeys = build.Encoding.FindContentKey(targetCKey);
53+
var fileEncodingKeys = build.Encoding!.FindContentKey(targetCKey);
5454
if (!fileEncodingKeys)
5555
Assert.Fail("EKey not found in encoding");
5656

TACTSharp/Build.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
public class BuildInstance
44
{
5-
public Config BuildConfig { get; private set; }
6-
public Config CDNConfig { get; private set; }
5+
public Config? BuildConfig { get; private set; }
6+
public Config? CDNConfig { get; private set; }
77

88
public EncodingInstance? Encoding { get; private set; }
99
public RootInstance? Root { get; private set; }
@@ -185,8 +185,6 @@ public byte[] OpenFileByEKey(ReadOnlySpan<byte> eKey, ulong decodedSize = 0)
185185
throw new Exception("Indexes not loaded");
186186

187187
var (offset, size, archiveIndex) = GroupIndex.GetIndexInfo(eKey);
188-
byte[] fileBytes;
189-
190188
if (offset == -1)
191189
{
192190
if (FileIndex != null)
@@ -201,7 +199,7 @@ public byte[] OpenFileByEKey(ReadOnlySpan<byte> eKey, ulong decodedSize = 0)
201199
}
202200
else
203201
{
204-
return cdn.GetFileFromArchive(Convert.ToHexStringLower(eKey), CDNConfig.Values["archives"][archiveIndex], offset, size, decodedSize, true);
202+
return cdn.GetFileFromArchive(Convert.ToHexStringLower(eKey), CDNConfig!.Values["archives"][archiveIndex], offset, size, decodedSize, true);
205203
}
206204
}
207205
}

TACTSharp/BuildInfo.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public struct AvailableBuild
1919

2020
public BuildInfo(string path, Settings Settings, CDN CDN)
2121
{
22+
if(string.IsNullOrEmpty(Settings.BaseDir) || !Directory.Exists(Settings.BaseDir))
23+
throw new DirectoryNotFoundException("Base directory for .build.info/.flavor.info not set or does not exist");
24+
2225
var headerMap = new Dictionary<string, byte>();
2326

2427
var folderMap = new Dictionary<string, string>();
@@ -28,7 +31,7 @@ public BuildInfo(string path, Settings Settings, CDN CDN)
2831
if (flavorLines.Length < 2)
2932
continue;
3033

31-
folderMap.Add(flavorLines[1], Path.GetFileName(Path.GetDirectoryName(flavorFile)));
34+
folderMap.Add(flavorLines[1], Path.GetFileName(Path.GetDirectoryName(flavorFile)!));
3235
}
3336

3437
foreach (var line in File.ReadAllLines(path))
@@ -58,7 +61,7 @@ public BuildInfo(string path, Settings Settings, CDN CDN)
5861
if (headerMap.TryGetValue("CDN Hosts", out byte cdnHosts))
5962
CDN.SetCDNs(splitLine[cdnHosts].Split(' '));
6063

61-
if (folderMap.TryGetValue(availableBuild.Product, out string folder))
64+
if (folderMap.TryGetValue(availableBuild.Product, out string? folder))
6265
availableBuild.Folder = folder;
6366
else
6467
Console.WriteLine("No flavor found matching " + availableBuild.Product);

TACTSharp/CASCIndexInstance.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ namespace TACTSharp
55
{
66
public sealed class CASCIndexInstance
77
{
8-
private readonly long indexSize;
9-
10-
private readonly short archiveIndex = -1;
11-
12-
private readonly string path;
138
private readonly IndexHeader header;
149

1510
private readonly MemoryMappedFile indexFile;
@@ -23,9 +18,6 @@ public sealed class CASCIndexInstance
2318

2419
public CASCIndexInstance(string path)
2520
{
26-
this.path = path;
27-
this.indexSize = new FileInfo(path).Length;
28-
2921
// create from filestream instead so battle.net doesn't freak out
3022
this.indexFile = MemoryMappedFile.CreateFromFile(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, false);
3123
this.accessor = indexFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
@@ -104,6 +96,7 @@ unsafe public (int offset, int size, int archiveIndex) GetIndexInfo(Span<byte> e
10496
}
10597
}
10698

99+
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
107100
private unsafe struct IndexHeader
108101
{
109102
public uint headerHashSize;
@@ -120,5 +113,6 @@ private unsafe struct IndexHeader
120113
public uint entriesSize;
121114
public uint entriesHash;
122115
}
116+
#pragma warning restore CS0649
123117
}
124118
}

TACTSharp/CDN.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class CDN
1616

1717
// TODO: The implementation around this needs improving. For local installations, this comes from .build.info. For remote this is set by the first CDN server it retrieves.
1818
// However, if ProductDirectory is accessed before the first CDN is loaded (or if not set through .build.info loading) it'll be null.
19-
public string ProductDirectory;
19+
public string ProductDirectory = string.Empty;
2020

2121
// TODO: Memory mapped cache file access?
2222
public CDN(Settings settings)
@@ -26,23 +26,23 @@ public CDN(Settings settings)
2626

2727
public void OpenLocal()
2828
{
29-
if (Settings.BaseDir != null)
30-
{
31-
if (CASCIndexInstances.Count > 0)
32-
return;
29+
if (string.IsNullOrEmpty(Settings.BaseDir))
30+
return;
3331

34-
try
35-
{
36-
var localTimer = new Stopwatch();
37-
localTimer.Start();
38-
LoadCASCIndices();
39-
localTimer.Stop();
40-
Console.WriteLine("Loaded local CASC indices in " + Math.Round(localTimer.Elapsed.TotalMilliseconds) + "ms");
41-
}
42-
catch (Exception e)
43-
{
44-
Console.WriteLine("Failed to load CASC indices: " + e.Message);
45-
}
32+
if (CASCIndexInstances.Count > 0)
33+
return;
34+
35+
try
36+
{
37+
var localTimer = new Stopwatch();
38+
localTimer.Start();
39+
LoadCASCIndices();
40+
localTimer.Stop();
41+
Console.WriteLine("Loaded local CASC indices in " + Math.Round(localTimer.Elapsed.TotalMilliseconds) + "ms");
42+
}
43+
catch (Exception e)
44+
{
45+
Console.WriteLine("Failed to load CASC indices: " + e.Message);
4646
}
4747
}
4848

@@ -168,13 +168,13 @@ public async Task<string> GetPatchServiceFile(string product, string file = "ver
168168
{
169169
if (type == "data" && hash.EndsWith(".index"))
170170
{
171-
var localIndexPath = Path.Combine(Settings.BaseDir, "Data", "indices", hash);
171+
var localIndexPath = Path.Combine(Settings.BaseDir!, "Data", "indices", hash);
172172
if (File.Exists(localIndexPath))
173173
return File.ReadAllBytes(localIndexPath);
174174
}
175175
else if (type == "config")
176176
{
177-
var localConfigPath = Path.Combine(Settings.BaseDir, "Data", "config", hash[0] + "" + hash[1], hash[2] + "" + hash[3], hash);
177+
var localConfigPath = Path.Combine(Settings.BaseDir!, "Data", "config", hash[0] + "" + hash[1], hash[2] + "" + hash[3], hash);
178178
if (File.Exists(localConfigPath))
179179
return File.ReadAllBytes(localConfigPath);
180180
}
@@ -271,6 +271,9 @@ public async Task<string> GetPatchServiceFile(string product, string file = "ver
271271

272272
public unsafe bool TryGetLocalFile(string eKey, out ReadOnlySpan<byte> data)
273273
{
274+
if(string.IsNullOrEmpty(Settings.BaseDir))
275+
throw new DirectoryNotFoundException("Base directory not set");
276+
274277
var eKeyBytes = Convert.FromHexString(eKey);
275278
var i = eKeyBytes[0] ^ eKeyBytes[1] ^ eKeyBytes[2] ^ eKeyBytes[3] ^ eKeyBytes[4] ^ eKeyBytes[5] ^ eKeyBytes[6] ^ eKeyBytes[7] ^ eKeyBytes[8];
276279
var indexBucket = (i & 0xf) ^ (i >> 4);

TACTSharp/IndexInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ unsafe public (int offset, int size, int archiveIndex) GetIndexInfo(ReadOnlySpan
189189
}
190190
}
191191

192+
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
192193
private unsafe struct IndexFooter
193194
{
194195
public byte formatRevision;
@@ -202,5 +203,6 @@ private unsafe struct IndexFooter
202203
public uint numElements;
203204
public fixed byte bytefooterHash[8];
204205
}
206+
#pragma warning restore CS0649
205207
}
206208
}

TACTSharp/Listfile.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ namespace TACTSharp
44
{
55
public class Listfile
66
{
7-
private Lock listfileLock = new();
8-
private HttpClient client = new();
9-
private Dictionary<ulong, uint> nameHashToFDID = new();
10-
private Dictionary<uint, string> fdidToName = new();
11-
private Jenkins96 hasher = new();
12-
13-
private CDN CDN;
14-
private Settings Settings;
7+
private readonly Lock listfileLock = new();
8+
private readonly HttpClient client = new();
9+
private readonly Jenkins96 hasher = new();
10+
private readonly Dictionary<ulong, uint> nameHashToFDID =[];
11+
private readonly Dictionary<uint, string> fdidToName = [];
12+
13+
private CDN? CDN;
14+
private Settings? Settings;
1515
private string ListfilePath = "listfile.csv";
1616

1717
public bool Initialized = false;
@@ -67,7 +67,7 @@ public void Initialize(CDN cdn, Settings settings, string path = "listfile.csv",
6767

6868
private void Download()
6969
{
70-
if (string.IsNullOrEmpty(Settings.ListfileURL))
70+
if (string.IsNullOrEmpty(Settings!.ListfileURL))
7171
throw new Exception("Listfile URL is not set or empty");
7272

7373
using (var client = new HttpClient())

TACTSharp/Utils/KeyService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ static KeyService()
1414

1515
public static bool TryGetKey(ulong keyName, out byte[] key)
1616
{
17+
#pragma warning disable CS8601 // Possible null reference assignment.
1718
return keys.TryGetValue(keyName, out key);
19+
#pragma warning restore CS8601
1820
}
1921

2022
public static void SetKey(ulong keyName, byte[] key)
2123
{
24+
if(key == null || key.Length == 0)
25+
throw new ArgumentException("Key cannot be null or empty", nameof(key));
26+
2227
keys[keyName] = key;
2328
}
2429

0 commit comments

Comments
 (0)