Skip to content

Commit e689584

Browse files
committed
Rework RemoteResourceDownloader
1 parent 848e286 commit e689584

12 files changed

+224
-175
lines changed

Assets/Editor/Tests/RemoteResourceDownloaderTest.cs

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

1717
private ChunksData CreateTestChunksData()
1818
{
19-
return new ChunksData()
19+
return new ChunksData
2020
{
2121
Chunks = new[]
2222
{
@@ -98,13 +98,12 @@ public void UseTorrentDownloaderFirst()
9898
var torrentDownloader = Substitute.For<ITorrentDownloader>();
9999

100100
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
101-
(path, remoteResource, size) => httpDownloader,
102-
(path, remoteResource) => chunkedHttpDownloader,
103-
(path, remoteResource, timeout) => torrentDownloader);
101+
(path, urls) => httpDownloader,
102+
(path, urls, data, size) => chunkedHttpDownloader,
103+
(path, filePath, bytes) => torrentDownloader);
104104

105105
downloader.Download(CancellationToken.Empty);
106106

107-
httpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
108107
chunkedHttpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
109108
torrentDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
110109
}
@@ -118,16 +117,15 @@ public void UseChunkedHttpDownloaderIfTorrentFails()
118117
var chunkedHttpDownloader = Substitute.For<IChunkedHttpDownloader>();
119118
var torrentDownloader = Substitute.For<ITorrentDownloader>();
120119
torrentDownloader.When(t => t.Download(CancellationToken.Empty)).Do(
121-
info => { throw new DownloaderException("Test.", DownloaderExceptionStatus.Other); });
120+
info => { throw new DownloadFailureException("Test."); });
122121

123122
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
124-
(path, remoteResource, size) => httpDownloader,
125-
(path, remoteResource) => chunkedHttpDownloader,
126-
(path, remoteResource, timeout) => torrentDownloader);
123+
(path, urls) => httpDownloader,
124+
(path, urls, data, size) => chunkedHttpDownloader,
125+
(path, filePath, bytes) => torrentDownloader);
127126

128127
downloader.Download(CancellationToken.Empty);
129128

130-
httpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
131129
chunkedHttpDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
132130
torrentDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
133131
}
@@ -142,9 +140,9 @@ public void UseChunkedHttpDownloaderIfTorrentIsNotUsed()
142140
var torrentDownloader = Substitute.For<ITorrentDownloader>();
143141

144142
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, false,
145-
(path, remoteResource, size) => httpDownloader,
146-
(path, remoteResource) => chunkedHttpDownloader,
147-
(path, remoteResource, timeout) => torrentDownloader);
143+
(path, urls) => httpDownloader,
144+
(path, urls, data, size) => chunkedHttpDownloader,
145+
(path, filePath, bytes) => torrentDownloader);
148146

149147
downloader.Download(CancellationToken.Empty);
150148

@@ -164,9 +162,9 @@ public void UseHttpDownloaderIfChunksAreNotAvailable()
164162
var torrentDownloader = Substitute.For<ITorrentDownloader>();
165163

166164
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, false,
167-
(path, remoteResource, size) => httpDownloader,
168-
(path, remoteResource) => chunkedHttpDownloader,
169-
(path, remoteResource, timeout) => torrentDownloader);
165+
(path, urls) => httpDownloader,
166+
(path, urls, data, size) => chunkedHttpDownloader,
167+
(path, filePath, bytes) => torrentDownloader);
170168

171169
downloader.Download(CancellationToken.Empty);
172170

Assets/PatchKit Patcher/Scripts/AppData/Remote/Downloaders/DownloaderException.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Assets/PatchKit Patcher/Scripts/AppData/Remote/Downloaders/DownloaderException.cs.meta

Lines changed: 0 additions & 12 deletions
This file was deleted.

Assets/PatchKit Patcher/Scripts/AppData/Remote/Downloaders/ITorrentDownloader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using PatchKit.Unity.Patcher.Cancellation;
1+
using PatchKit.Unity.Patcher.Cancellation;
32

43
namespace PatchKit.Unity.Patcher.AppData.Remote.Downloaders
54
{

Assets/PatchKit Patcher/Scripts/AppData/Remote/Downloaders/TorrentDownloader.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public sealed class TorrentDownloader : ITorrentDownloader
2121

2222
private const int ConnectionTimeout = 10000;
2323

24-
private ILogger _logger;
24+
private readonly ILogger _logger;
2525

2626
private readonly string _destinationFilePath;
2727
private readonly string _torrentFilePath;
28+
private readonly long _totalBytes;
2829

2930
private bool _downloadHasBeenCalled;
3031

@@ -35,14 +36,17 @@ private string DestinationDirectoryPath
3536
get { return _destinationFilePath + ".torrent_dir"; }
3637
}
3738

38-
public TorrentDownloader([NotNull] string destinationFilePath, [NotNull] string torrentFilePath)
39+
public TorrentDownloader([NotNull] string destinationFilePath, [NotNull] string torrentFilePath,
40+
long totalBytes)
3941
{
4042
if (destinationFilePath == null) throw new ArgumentNullException("destinationFilePath");
4143
if (torrentFilePath == null) throw new ArgumentNullException("torrentFilePath");
44+
if (totalBytes <= 0) throw new ArgumentOutOfRangeException("totalBytes");
4245

4346
_logger = PatcherLogManager.DefaultLogger;
4447
_destinationFilePath = destinationFilePath;
4548
_torrentFilePath = torrentFilePath;
49+
_totalBytes = totalBytes;
4650
}
4751

4852
public void Download(CancellationToken cancellationToken)
@@ -56,9 +60,9 @@ public void Download(CancellationToken cancellationToken)
5660

5761
Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download");
5862

59-
using (var torrentClient = new TorrentClient(new UnityTorrentClientProcessStartInfoProvider()))
63+
using (var tempDir = new TemporaryDirectory(DestinationDirectoryPath))
6064
{
61-
using (var tempDir = new TemporaryDirectory(DestinationDirectoryPath))
65+
using (var torrentClient = new TorrentClient(new UnityTorrentClientProcessStartInfoProvider()))
6266
{
6367
torrentClient.AddTorrent(_torrentFilePath, tempDir.Path, cancellationToken);
6468

@@ -67,8 +71,11 @@ public void Download(CancellationToken cancellationToken)
6771

6872
var status = GetAndCheckTorrentStatus(torrentClient, cancellationToken);
6973
double initialProgress = status.Progress;
74+
_logger.LogTrace("initialProgress = " + status.Progress);
7075
var waitHandle = new AutoResetEvent(false);
7176

77+
OnDownloadProgressChanged(0);
78+
7279
using (cancellationToken.Register(() => waitHandle.Set()))
7380
{
7481
bool finished = false;
@@ -79,8 +86,12 @@ public void Download(CancellationToken cancellationToken)
7986

8087
status = GetAndCheckTorrentStatus(torrentClient, cancellationToken);
8188

89+
_logger.LogTrace("progress = " + status.Progress);
90+
8291
CheckTimeout(timeoutWatch, status.Progress, initialProgress);
8392

93+
OnDownloadProgressChanged((long) (_totalBytes * status.Progress));
94+
8495
if (status.IsSeeding)
8596
{
8697
finished = true;
@@ -114,10 +125,13 @@ public void Download(CancellationToken cancellationToken)
114125
}
115126
}
116127

117-
private TorrentStatus GetAndCheckTorrentStatus(TorrentClient torrentClient, CancellationToken cancellationToken)
128+
private TorrentStatus GetAndCheckTorrentStatus(TorrentClient torrentClient,
129+
CancellationToken cancellationToken)
118130
{
119131
var torrentClientStatus = torrentClient.GetStatus(cancellationToken);
120132

133+
_logger.LogTrace("status = " + torrentClientStatus.Status);
134+
121135
if (torrentClientStatus.Status != "ok")
122136
{
123137
throw new DownloadFailureException("Torrent client failure.");
@@ -168,14 +182,10 @@ private string GetDownloadedFilePath()
168182
return dirFiles[0].FullName;
169183
}
170184

171-
//private void UpdateTorrentProgress(double progress)
172-
//{
173-
// OnDownloadProgressChanged((long) (_resource.Size * progress));
174-
//}
175-
176-
private void OnDownloadProgressChanged(long downloadedBytes)
185+
private void OnDownloadProgressChanged(long downloadedbytes)
177186
{
178-
if (DownloadProgressChanged != null) DownloadProgressChanged(downloadedBytes);
187+
var handler = DownloadProgressChanged;
188+
if (handler != null) handler(downloadedbytes);
179189
}
180190
}
181191
}

Assets/PatchKit Patcher/Scripts/AppData/Remote/Downloaders/Torrents/Protocol/TorrentStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class TorrentStatus
2323
public long Peers { get; set; }
2424

2525
[JsonProperty("progress")]
26-
public long Progress { get; set; }
26+
public double Progress { get; set; }
2727

2828
[JsonProperty("seed_rank")]
2929
public long SeedRank { get; set; }

Assets/PatchKit Patcher/Scripts/AppData/Remote/RemoteResource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public override string ToString()
2424
"hashcode: " + HashCode;
2525
}
2626

27+
[Pure]
2728
public bool HasMetaUrls()
2829
{
2930
return ResourceUrls != null && ResourceUrls.Length > 0 && !string.IsNullOrEmpty(ResourceUrls[0].MetaUrl);

0 commit comments

Comments
 (0)