Skip to content

Commit 8b0d9cf

Browse files
committed
Merge branch 'torrent-downloader-rework'
2 parents b9ee3a0 + e689584 commit 8b0d9cf

30 files changed

+590
-477
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/Local/BaseWritableDirectory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public string Path
3131

3232
protected BaseWritableDirectory(string path)
3333
{
34-
Assert.IsFalse(UsedPaths.Contains(path),
35-
string.Format("You cannot create two instances of {0} pointing to the same path.", typeof(T)));
34+
// This makes no sense.
35+
//Assert.IsFalse(UsedPaths.Contains(path),
36+
// string.Format("You cannot create two instances of {0} pointing to the same path.", typeof(T)));
3637
Checks.ArgumentNotNullOrEmpty(path, "path");
3738

3839
DebugLogger.LogConstructor();

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,29 @@ private struct DownloadJob
3535

3636
private readonly string _destinationFilePath;
3737

38-
private readonly RemoteResource _resource;
38+
private readonly ResourceUrl[] _urls;
39+
40+
private readonly ChunksData _chunksData;
41+
42+
private readonly long _size;
3943

4044
private bool _downloadHasBeenCalled;
4145

4246
public event DownloadProgressChangedHandler DownloadProgressChanged;
4347

44-
public ChunkedHttpDownloader([NotNull] string destinationFilePath, RemoteResource resource)
48+
public ChunkedHttpDownloader([NotNull] string destinationFilePath, [NotNull] ResourceUrl[] urls, ChunksData chunksData,
49+
long size)
4550
{
4651
if (string.IsNullOrEmpty(destinationFilePath))
4752
throw new ArgumentException("Value cannot be null or empty.", "destinationFilePath");
53+
if (urls == null) throw new ArgumentNullException("urls");
54+
if (size <= 0) throw new ArgumentOutOfRangeException("size");
4855

4956
_logger = PatcherLogManager.DefaultLogger;
5057
_destinationFilePath = destinationFilePath;
51-
_resource = resource;
58+
_urls = urls;
59+
_chunksData = chunksData;
60+
_size = size;
5261
}
5362

5463
private ChunkedFileStream OpenFileStream()
@@ -59,7 +68,7 @@ private ChunkedFileStream OpenFileStream()
5968
Directory.CreateDirectory(parentDirectory);
6069
}
6170

62-
return new ChunkedFileStream(_destinationFilePath, _resource.Size, _resource.ChunksData,
71+
return new ChunkedFileStream(_destinationFilePath, _size, _chunksData,
6372
HashFunction, ChunkedFileStream.WorkFlags.PreservePreviousFile);
6473
}
6574

@@ -68,17 +77,16 @@ public void Download(CancellationToken cancellationToken)
6877
try
6978
{
7079
_logger.LogDebug("Downloading...");
71-
_logger.LogTrace("resource.Size = " + _resource.Size);
72-
for (int i = 0; i < _resource.ResourceUrls.Length; i++)
80+
_logger.LogTrace("size = " + _size);
81+
for (int i = 0; i < _urls.Length; i++)
7382
{
74-
_logger.LogTrace("resource.ResourceUrls[" + i + "].Url = " + _resource.ResourceUrls[i].Url);
75-
_logger.LogTrace("resource.ResourceUrls[" + i + "].Country = " + _resource.ResourceUrls[i].Country);
76-
_logger.LogTrace(
77-
"resource.ResourceUrls[" + i + "].PartSize = " + _resource.ResourceUrls[i].PartSize);
83+
_logger.LogTrace("urls[" + i + "].Url = " + _urls[i].Url);
84+
_logger.LogTrace("urls[" + i + "].Country = " + _urls[i].Country);
85+
_logger.LogTrace("urls[" + i + "].PartSize = " + _urls[i].PartSize);
7886
}
7987

80-
_logger.LogTrace("resource.ChunksData.ChunkSize = " + _resource.ChunksData.ChunkSize);
81-
_logger.LogTrace("resource.ChunksData.Chunks.Length = " + _resource.ChunksData.Chunks.Length);
88+
_logger.LogTrace("chunksData.ChunkSize = " + _chunksData.ChunkSize);
89+
_logger.LogTrace("chunksData.Chunks.Length = " + _chunksData.Chunks.Length);
8290

8391
Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download");
8492

@@ -89,7 +97,7 @@ public void Download(CancellationToken cancellationToken)
8997
do
9098
{
9199
bool success =
92-
_resource.ResourceUrls.Any(url => TryDownload(url, fileStream, cancellationToken));
100+
_urls.Any(url => TryDownload(url, fileStream, cancellationToken));
93101

94102
if (success)
95103
{
@@ -202,11 +210,10 @@ private bool TryDownload(ResourceUrl url, ChunkedFileStream fileStream, Cancella
202210

203211
private IEnumerable<DownloadJob> BuildDownloadJobQueue(ResourceUrl resourceUrl, long currentOffset)
204212
{
205-
long totalSize = _resource.Size;
206-
long partSize = resourceUrl.PartSize == 0 ? totalSize : resourceUrl.PartSize;
213+
long partSize = resourceUrl.PartSize == 0 ? _size : resourceUrl.PartSize;
207214

208-
int partCount = (int) (totalSize / partSize);
209-
partCount += totalSize % partSize != 0 ? 1 : 0;
215+
int partCount = (int) (_size / partSize);
216+
partCount += _size % partSize != 0 ? 1 : 0;
210217

211218
for (int i = 0; i < partCount; i++)
212219
{

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/HttpDownloader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ public sealed class HttpDownloader : IHttpDownloader
3030

3131
public event DownloadProgressChangedHandler DownloadProgressChanged;
3232

33-
public HttpDownloader([NotNull] string destinationFilePath, [NotNull] string[] urls, long size)
33+
public HttpDownloader([NotNull] string destinationFilePath, [NotNull] string[] urls)
3434
{
3535
if (destinationFilePath == null) throw new ArgumentNullException("destinationFilePath");
3636
if (urls == null) throw new ArgumentNullException("urls");
37-
if (size <= 0) throw new ArgumentOutOfRangeException("size");
3837

3938
_logger = PatcherLogManager.DefaultLogger;
4039
_destinationFilePath = destinationFilePath;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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
{
6-
public interface ITorrentDownloader : IDisposable
5+
public interface ITorrentDownloader
76
{
87
event DownloadProgressChangedHandler DownloadProgressChanged;
98

0 commit comments

Comments
 (0)