Skip to content

Commit 2d656db

Browse files
committed
Rework some things to be more fault tolerant
1 parent 2ccfbb0 commit 2d656db

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

TACTSharp/Build.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void Load()
7575
if (!File.Exists(groupIndexPath))
7676
{
7777
var groupIndex = new GroupIndex();
78-
groupIndex.Generate(cdn, Settings, groupArchiveIndex[0], CDNConfig.Values["archives"]);
78+
groupIndex.Generate(cdn, Settings, groupArchiveIndex[0], CDNConfig.Values["archives"], true);
7979
}
8080
GroupIndex = new IndexInstance(groupIndexPath);
8181
}
@@ -93,8 +93,15 @@ public void Load()
9393
}
9494
else
9595
{
96-
var fileIndexPath = cdn.GetFilePath("data", fileIndex[0] + ".index");
97-
FileIndex = new IndexInstance(fileIndexPath);
96+
try
97+
{
98+
var fileIndexPath = cdn.GetFilePath("data", fileIndex[0] + ".index");
99+
FileIndex = new IndexInstance(fileIndexPath);
100+
101+
}catch(Exception e)
102+
{
103+
Console.WriteLine("Failed to load file index: " + e.Message);
104+
}
98105
}
99106

100107
timer.Stop();
@@ -161,31 +168,28 @@ public byte[] OpenFileByCKey(ReadOnlySpan<byte> cKey)
161168

162169
public byte[] OpenFileByEKey(ReadOnlySpan<byte> eKey, ulong decodedSize = 0)
163170
{
164-
if (GroupIndex == null || FileIndex == null)
171+
if (GroupIndex == null)
165172
throw new Exception("Indexes not loaded");
166173

167174
var (offset, size, archiveIndex) = GroupIndex.GetIndexInfo(eKey);
168175
byte[] fileBytes;
169176

170177
if (offset == -1)
171178
{
172-
var fileIndexEntry = FileIndex.GetIndexInfo(eKey);
173-
if (fileIndexEntry.size == -1)
179+
if(FileIndex != null)
174180
{
175-
Console.WriteLine("Warning: EKey " + Convert.ToHexStringLower(eKey) + " not found in group or file index and might not be available on CDN.");
176-
fileBytes = cdn.GetFile("data", Convert.ToHexStringLower(eKey), 0, decodedSize, true);
177-
}
178-
else
179-
{
180-
fileBytes = cdn.GetFile("data", Convert.ToHexStringLower(eKey), (ulong)fileIndexEntry.size, decodedSize, true);
181+
var fileIndexEntry = FileIndex.GetIndexInfo(eKey);
182+
if (fileIndexEntry.size != -1)
183+
return cdn.GetFile("data", Convert.ToHexStringLower(eKey), (ulong)fileIndexEntry.size, decodedSize, true);
181184
}
185+
186+
Console.WriteLine("Warning: EKey " + Convert.ToHexStringLower(eKey) + " not found in group or file index and might not be available on CDN.");
187+
return cdn.GetFile("data", Convert.ToHexStringLower(eKey), 0, decodedSize, true);
182188
}
183189
else
184190
{
185-
fileBytes = cdn.GetFileFromArchive(Convert.ToHexStringLower(eKey), CDNConfig.Values["archives"][archiveIndex], offset, size, decodedSize, true);
191+
return cdn.GetFileFromArchive(Convert.ToHexStringLower(eKey), CDNConfig.Values["archives"][archiveIndex], offset, size, decodedSize, true);
186192
}
187-
188-
return fileBytes;
189193
}
190194
}
191195
}

TACTSharp/CDN.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ public async Task<string> GetPatchServiceFile(string product, string file = "ver
199199
}
200200
}
201201

202+
throw new FileNotFoundException();
203+
202204
for (var i = 0; i < CDNServers.Count; i++)
203205
{
204206
var url = $"http://{CDNServers[i]}/{ProductDirectory}/{type}/{hash[0]}{hash[1]}/{hash[2]}{hash[3]}/{hash}";

TACTSharp/GroupIndex.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private struct IndexEntry
1818
private readonly List<IndexEntry> Entries = [];
1919
private readonly Lock entryLock = new();
2020

21-
public string Generate(CDN CDN, Settings Settings, string? hash, string[] archives)
21+
public string Generate(CDN CDN, Settings Settings, string? hash, string[] archives, bool allowPartial = false)
2222
{
2323
if (string.IsNullOrEmpty(hash))
2424
Console.WriteLine("Generating group index for unknown group-index");
@@ -41,7 +41,18 @@ public string Generate(CDN CDN, Settings Settings, string? hash, string[] archiv
4141
}
4242
else
4343
{
44-
_ = CDN.GetFile("data", archives[archiveIndex] + ".index");
44+
try
45+
{
46+
_ = CDN.GetFile("data", archives[archiveIndex] + ".index");
47+
}
48+
catch(Exception e)
49+
{
50+
Console.WriteLine("Failed to load index file " + archives[archiveIndex] + ".index: " + e.Message);
51+
if (allowPartial)
52+
return;
53+
else
54+
throw;
55+
}
4556
indexPath = Path.Combine(Settings.CacheDir, CDN.ProductDirectory, "data", archives[archiveIndex] + ".index");
4657
}
4758

@@ -168,7 +179,7 @@ public string Generate(CDN CDN, Settings Settings, string? hash, string[] archiv
168179

169180
if (!string.IsNullOrEmpty(hash))
170181
{
171-
if (fullFooterMD5Hash != hash)
182+
if (fullFooterMD5Hash != hash && !allowPartial)
172183
throw new Exception("Footer MD5 of group index does not match group index filename");
173184

174185
if (!Directory.Exists(Path.Combine(Settings.CacheDir, CDN.ProductDirectory, "data")))

0 commit comments

Comments
 (0)