Skip to content

Commit 32d4bf4

Browse files
committed
Attempt at dealing with bad CDNs that disconnect during DL
1 parent 9c39840 commit 32d4bf4

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

TACTSharp/CDN.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private void LoadCASCIndices()
111111
var dataDir = Path.Combine(Settings.BaseDir, "Data", "data");
112112
if (Directory.Exists(dataDir))
113113
{
114-
if(CASCIndexInstances.Count > 0)
114+
if (CASCIndexInstances.Count > 0)
115115
return;
116116

117117
var indexFiles = Directory.GetFiles(dataDir, "*.idx");
@@ -205,9 +205,18 @@ public async Task<string> GetPatchServiceFile(string product, string file = "ver
205205
{
206206
Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!);
207207

208-
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write))
208+
try
209+
{
210+
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write))
211+
{
212+
response.Content.ReadAsStream(token).CopyTo(fileStream);
213+
}
214+
}
215+
catch (Exception e)
209216
{
210-
response.Content.ReadAsStream(token).CopyTo(fileStream);
217+
Console.WriteLine("Failed to download file: " + e.Message);
218+
File.Delete(cachePath);
219+
continue;
211220
}
212221
}
213222

@@ -331,9 +340,18 @@ public unsafe bool TryGetLocalFile(string eKey, out ReadOnlySpan<byte> data)
331340
{
332341
Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!);
333342

334-
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write))
343+
try
344+
{
345+
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write))
346+
{
347+
response.Content.ReadAsStream(token).CopyTo(fileStream);
348+
}
349+
}
350+
catch (Exception e)
335351
{
336-
response.Content.ReadAsStream(token).CopyTo(fileStream);
352+
Console.WriteLine("Failed to download file: " + e.Message);
353+
File.Delete(cachePath);
354+
continue;
337355
}
338356
}
339357

TACTSharp/Config.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public Config(CDN cdn, string path, bool isFile)
99
{
1010
if (!isFile)
1111
{
12-
foreach (var line in Encoding.UTF8.GetString(cdn.GetFile("config", path)).Split('\n'))
12+
var file = Encoding.UTF8.GetString(cdn.GetFile("config", path));
13+
if (file[0] != '#')
14+
throw new IOException("Config file is unreadable");
15+
16+
foreach (var line in file.Split('\n'))
1317
{
1418
var splitLine = line.Split('=');
1519
if (splitLine.Length > 1)
@@ -18,7 +22,11 @@ public Config(CDN cdn, string path, bool isFile)
1822
}
1923
else
2024
{
21-
foreach (var line in File.ReadAllLines(path))
25+
var file = File.ReadAllLines(path);
26+
if (file[0][0] != '#')
27+
throw new IOException("Config file is unreadable");
28+
29+
foreach (var line in file)
2230
{
2331
var splitLine = line.Split('=');
2432
if (splitLine.Length > 1)

0 commit comments

Comments
 (0)