Skip to content

Commit 092c521

Browse files
committed
Hopefully make archive downloads slightly more robust
1 parent 0ddb145 commit 092c521

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

TACTSharp/CDN.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ 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))
274+
if (string.IsNullOrEmpty(Settings.BaseDir))
275275
throw new DirectoryNotFoundException("Base directory not set");
276276

277277
var eKeyBytes = Convert.FromHexString(eKey);
@@ -387,7 +387,7 @@ public unsafe bool TryGetLocalFile(string eKey, out ReadOnlySpan<byte> data)
387387
{
388388
var url = $"http://{CDNServers[i]}/{ProductDirectory}/data/{archive[0]}{archive[1]}/{archive[2]}{archive[3]}/{archive}";
389389

390-
Console.WriteLine("Downloading file " + eKey + " from archive " + archive + " at offset " + offset + " with size " + size);
390+
Console.WriteLine("Downloading file " + eKey + " from archive " + archive + " at offset " + offset + " with size " + size + " from " + CDNServers[i]);
391391

392392
var request = new HttpRequestMessage(HttpMethod.Get, url)
393393
{
@@ -397,31 +397,39 @@ public unsafe bool TryGetLocalFile(string eKey, out ReadOnlySpan<byte> data)
397397
}
398398
};
399399

400-
var response = Client.Send(request, token);
401-
402-
if (!response.IsSuccessStatusCode)
400+
try
403401
{
404-
Console.WriteLine("Encountered HTTP " + response.StatusCode + " downloading " + eKey + " (archive " + archive + ") from " + CDNServers[i]);
405-
continue;
406-
}
402+
var response = Client.Send(request, token);
407403

408-
lock (FileLocks[cachePath])
409-
{
410-
Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!);
404+
if (!response.IsSuccessStatusCode)
405+
{
406+
Console.WriteLine("Encountered HTTP " + response.StatusCode + " downloading " + eKey + " (archive " + archive + ") from " + CDNServers[i]);
407+
continue;
408+
}
411409

412-
try
410+
lock (FileLocks[cachePath])
413411
{
414-
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write))
412+
Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!);
413+
414+
try
415415
{
416-
response.Content.ReadAsStream(token).CopyTo(fileStream);
416+
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write))
417+
{
418+
response.Content.ReadAsStream(token).CopyTo(fileStream);
419+
}
420+
}
421+
catch (Exception ex)
422+
{
423+
Console.WriteLine("Failed to download file: " + ex.Message);
424+
File.Delete(cachePath);
425+
continue;
417426
}
418427
}
419-
catch (Exception e)
420-
{
421-
Console.WriteLine("Failed to download file: " + e.Message);
422-
File.Delete(cachePath);
423-
continue;
424-
}
428+
}
429+
catch (Exception e)
430+
{
431+
Console.WriteLine("Encountered exception " + e.Message + " downloading " + eKey + " (archive " + archive + ") from " + CDNServers[i]);
432+
continue;
425433
}
426434

427435
return File.ReadAllBytes(cachePath);

0 commit comments

Comments
 (0)