-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDownloadableAssetCollection.cs
64 lines (59 loc) · 2.1 KB
/
DownloadableAssetCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using AssetRipper.Primitives;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace UnityDataMiner
{
internal class DownloadableAssetCollection
{
// url -> path
private readonly Dictionary<string, string> assets = new();
// returns actual asset path
public string AddAsset(string url, string destPath)
{
if (!assets.TryGetValue(url, out var realPath))
{
assets.Add(url, realPath = destPath);
}
return realPath;
}
public async Task DownloadAssetsAsync(Func<string, string, CancellationToken, Task> downloadFunc, UnityVersion version,
SemaphoreSlim? downloadLock = null, CancellationToken cancellationToken = default)
{
while (true)
{
try
{
if (downloadLock is not null)
{
await downloadLock.WaitAsync(cancellationToken);
}
try
{
foreach (var (url, dest) in assets)
{
await downloadFunc(url, dest, cancellationToken);
}
}
finally
{
if (!cancellationToken.IsCancellationRequested && downloadLock is not null)
{
downloadLock.Release();
}
}
break;
}
catch (IOException e) when (e.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
{
Log.Warning("Failed to download {Version}, waiting 5 seconds before retrying...", version);
await Task.Delay(5000, cancellationToken);
}
}
}
}
}