Skip to content

Commit 187a3df

Browse files
committed
Merge branch 'pack1'
2 parents 5926d8d + d47a75f commit 187a3df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+768
-136
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.IO;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
using NUnit.Framework;
5+
using PatchKit.Unity.Patcher.AppData.Local;
6+
using PatchKit.Unity.Patcher.Cancellation;
7+
8+
public class Pack1UnarchiverTest
9+
{
10+
private const string Key = "test123";
11+
private string _tempDir;
12+
13+
[SetUp]
14+
public void SetUp()
15+
{
16+
_tempDir = TestHelpers.CreateTemporaryDirectory();
17+
}
18+
19+
[TearDown]
20+
public void TearDown()
21+
{
22+
TestHelpers.DeleteTemporaryDirectory(_tempDir);
23+
}
24+
25+
[Test]
26+
public void Unpack()
27+
{
28+
string archivePath = TestFixtures.GetFilePath("pack1/test.pack1");
29+
string metaPath = TestFixtures.GetFilePath("pack1/test.pack1.meta");
30+
string metaString = File.ReadAllText(metaPath);
31+
Pack1Meta meta = Pack1Meta.Parse(metaString);
32+
33+
var pack1Unarchiver = new Pack1Unarchiver(archivePath, meta, _tempDir, Key);
34+
pack1Unarchiver.Unarchive(new CancellationToken());
35+
36+
Assert.True(Directory.Exists(Path.Combine(_tempDir, "dir")));
37+
38+
var rakefile = Path.Combine(_tempDir, "dir/Rakefile");
39+
Assert.True(File.Exists(rakefile));
40+
Assert.AreEqual("d2974b45f816b3ddaca7a984a9101707", Md5File(rakefile));
41+
42+
var rubocopFile = Path.Combine(_tempDir, ".rubocop.yml");
43+
Assert.True(File.Exists(rubocopFile));
44+
Assert.AreEqual("379cc2261c048e4763969cca74974237", Md5File(rubocopFile));
45+
}
46+
47+
private string Md5File(string path)
48+
{
49+
using (MD5 md5 = MD5.Create())
50+
{
51+
using (var stream = File.OpenRead(path))
52+
{
53+
byte[] bytes = md5.ComputeHash(stream);
54+
var sb = new StringBuilder();
55+
foreach (byte b in bytes)
56+
{
57+
sb.Append(b.ToString("x2"));
58+
}
59+
60+
return sb.ToString();
61+
}
62+
}
63+
}
64+
}

Assets/Editor/Tests/Pack1UnarchiverTest.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/Tests/RemoteResourceDownloaderTest.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class RemoteResourceDownloaderTest
1010
{
1111
private string _dirPath;
1212
private string _filePath;
13+
private string _metaFilePath;
1314
private byte[] _fileData;
1415

1516
private ChunksData CreateTestChunksData()
@@ -44,6 +45,7 @@ public void Setup()
4445
{
4546
_dirPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
4647
_filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
48+
_metaFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
4749
_fileData = new byte[1024];
4850

4951
new Random().NextBytes(_fileData);
@@ -58,6 +60,10 @@ public void TearDown()
5860
{
5961
File.Delete(_filePath);
6062
}
63+
if (File.Exists(_metaFilePath))
64+
{
65+
File.Delete(_metaFilePath);
66+
}
6167
if (Directory.Exists(_dirPath))
6268
{
6369
Directory.Delete(_dirPath, true);
@@ -73,7 +79,7 @@ public void UseTorrentDownloaderFirst()
7379
var chunkedHttpDownloader = Substitute.For<IChunkedHttpDownloader>();
7480
var torrentDownloader = Substitute.For<ITorrentDownloader>();
7581

76-
var downloader = new RemoteResourceDownloader(_filePath, resource, true,
82+
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
7783
(path, remoteResource, timeout) => httpDownloader,
7884
(path, remoteResource, timeout) => chunkedHttpDownloader,
7985
(path, remoteResource, timeout) => torrentDownloader);
@@ -96,7 +102,7 @@ public void UseChunkedHttpDownloaderIfTorrentFails()
96102
torrentDownloader.When(t => t.Download(CancellationToken.Empty)).Do(
97103
info => { throw new DownloaderException("Test.", DownloaderExceptionStatus.Other); });
98104

99-
var downloader = new RemoteResourceDownloader(_filePath, resource, true,
105+
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
100106
(path, remoteResource, timeout) => httpDownloader,
101107
(path, remoteResource, timeout) => chunkedHttpDownloader,
102108
(path, remoteResource, timeout) => torrentDownloader);
@@ -117,7 +123,7 @@ public void UseChunkedHttpDownloaderIfTorrentIsNotUsed()
117123
var chunkedHttpDownloader = Substitute.For<IChunkedHttpDownloader>();
118124
var torrentDownloader = Substitute.For<ITorrentDownloader>();
119125

120-
var downloader = new RemoteResourceDownloader(_filePath, resource, false,
126+
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, false,
121127
(path, remoteResource, timeout) => httpDownloader,
122128
(path, remoteResource, timeout) => chunkedHttpDownloader,
123129
(path, remoteResource, timeout) => torrentDownloader);
@@ -139,7 +145,7 @@ public void UseHttpDownloaderIfChunksAreNotAvailable()
139145
var chunkedHttpDownloader = Substitute.For<IChunkedHttpDownloader>();
140146
var torrentDownloader = Substitute.For<ITorrentDownloader>();
141147

142-
var downloader = new RemoteResourceDownloader(_filePath, resource, false,
148+
var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, false,
143149
(path, remoteResource, timeout) => httpDownloader,
144150
(path, remoteResource, timeout) => chunkedHttpDownloader,
145151
(path, remoteResource, timeout) => torrentDownloader);

Assets/Editor/Tests/UnarchiverTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void CheckConsistency(string sourceDirPath, string dirPath)
5555
[Test]
5656
public void Unarchive()
5757
{
58-
var unarchiver = new Unarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
58+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
5959

6060
unarchiver.Unarchive(CancellationToken.Empty);
6161

@@ -65,7 +65,7 @@ public void Unarchive()
6565
[Test]
6666
public void CancelUnarchive()
6767
{
68-
var unarchiver = new Unarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
68+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
6969

7070
CancellationTokenSource source = new CancellationTokenSource();
7171
source.Cancel();
@@ -76,7 +76,7 @@ public void CancelUnarchive()
7676
[Test]
7777
public void UnarchiveCorruptedArchive()
7878
{
79-
var unarchiver = new Unarchiver(TestFixtures.GetFilePath("unarchiver-test/corrupted-zip.zip"), _dirPath);
79+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/corrupted-zip.zip"), _dirPath);
8080

8181
Assert.Catch<Exception>(() => unarchiver.Unarchive(CancellationToken.Empty));
8282
}
@@ -86,7 +86,7 @@ public void UnarchiveWithPassword()
8686
{
8787
string password = "\x08\x07\x18\x24" + "123==";
8888

89-
var unarchiver = new Unarchiver(TestFixtures.GetFilePath("unarchiver-test/password-zip.zip"), _dirPath, password);
89+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/password-zip.zip"), _dirPath, password);
9090

9191
unarchiver.Unarchive(CancellationToken.Empty);
9292

@@ -96,7 +96,7 @@ public void UnarchiveWithPassword()
9696
[Test]
9797
public void ProgressReporting()
9898
{
99-
var unarchiver = new Unarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
99+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
100100

101101
int? lastAmount = null;
102102
int? lastEntry = null;

Assets/PatchKit Patcher/Scripts/AppData/Local/DownloadDirectory.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,25 @@ public string GetContentPackagePath(int versionId)
2121
return Path.PathCombine("-content-" + versionId);
2222
}
2323

24+
public string GetContentPackageMetaPath(int versionId)
25+
{
26+
Checks.ArgumentValidVersionId(versionId, "versionId");
27+
return Path.PathCombine("-content-meta-" + versionId);
28+
}
29+
2430
public string GetDiffPackagePath(int versionId)
2531
{
2632
Checks.ArgumentValidVersionId(versionId, "versionId");
2733

2834
return Path.PathCombine("-diff-" + versionId);
2935
}
3036

37+
public string GetDiffPackageMetaPath(int versionId)
38+
{
39+
Checks.ArgumentValidVersionId(versionId, "versionId");
40+
return Path.PathCombine("-diff-meta" + versionId);
41+
}
42+
3143
public void Clear()
3244
{
3345
DebugLogger.Log("Clearing download data.");

Assets/PatchKit Patcher/Scripts/AppData/Local/IDownloadDirectory.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,26 @@ public interface IDownloadDirectory : IWritableDirectory
1212
/// <param name="versionId">The version id.</param>
1313
string GetContentPackagePath(int versionId);
1414

15+
/// <summary>
16+
/// Return path to the version content package meta file.
17+
/// </summary>
18+
/// <param name="versionId"></param>
19+
/// <returns></returns>
20+
string GetContentPackageMetaPath(int versionId);
21+
1522
/// <summary>
1623
/// Returns path to the version diff package located in download directory.
1724
/// </summary>
1825
/// <param name="versionId">The version id.</param>
1926
string GetDiffPackagePath(int versionId);
2027

28+
/// <summary>
29+
/// Return path to the version diff package meta file.
30+
/// </summary>
31+
/// <param name="versionId"></param>
32+
/// <returns></returns>
33+
string GetDiffPackageMetaPath(int versionId);
34+
2135
/// <summary>
2236
/// Clears the download data.
2337
/// </summary>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using PatchKit.Unity.Patcher.Cancellation;
2+
3+
namespace PatchKit.Unity.Patcher.AppData.Local
4+
{
5+
public interface IUnarchiver
6+
{
7+
event UnarchiveProgressChangedHandler UnarchiveProgressChanged;
8+
9+
void Unarchive(CancellationToken cancellationToken);
10+
}
11+
}

Assets/PatchKit Patcher/Scripts/AppData/Local/IUnarchiver.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace PatchKit.Unity.Patcher.AppData.Local
5+
{
6+
/// <summary>
7+
/// Limits the stream to given size in bytes.
8+
/// </summary>
9+
public class LimitedStream : Stream
10+
{
11+
private readonly Stream _orig;
12+
private long _bytesLeft;
13+
14+
public LimitedStream(Stream orig, long bytesLimit)
15+
{
16+
_orig = orig;
17+
_bytesLeft = bytesLimit;
18+
}
19+
20+
public override void Flush()
21+
{
22+
_orig.Flush();
23+
}
24+
25+
public override long Seek(long offset, SeekOrigin origin)
26+
{
27+
return _orig.Seek(offset, origin);
28+
}
29+
30+
public override void SetLength(long value)
31+
{
32+
_orig.SetLength(value);
33+
}
34+
35+
public override int Read(byte[] buffer, int offset, int count)
36+
{
37+
if (_bytesLeft == 0)
38+
{
39+
return 0;
40+
}
41+
42+
var read = _orig.Read(buffer, offset, (int) Math.Min(_bytesLeft, count));
43+
_bytesLeft -= read;
44+
return read;
45+
}
46+
47+
public override void Write(byte[] buffer, int offset, int count)
48+
{
49+
_orig.Write(buffer, offset, count);
50+
}
51+
52+
public override bool CanRead
53+
{
54+
get { return _orig.CanRead; }
55+
}
56+
57+
public override bool CanSeek
58+
{
59+
get { return _orig.CanSeek; }
60+
}
61+
62+
public override bool CanWrite
63+
{
64+
get { return _orig.CanWrite; }
65+
}
66+
67+
public override long Length
68+
{
69+
get { return _orig.Length; }
70+
}
71+
72+
public override long Position
73+
{
74+
get { return _orig.Position; }
75+
set { _orig.Position = value; }
76+
77+
}
78+
}
79+
}

Assets/PatchKit Patcher/Scripts/AppData/Local/LimitedStream.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)