Skip to content

Commit 80c7cc5

Browse files
committed
Merge branch 'dev/v3.17.x.x'
2 parents 7221005 + ba0b484 commit 80c7cc5

30 files changed

+446
-97
lines changed

Assets/Editor/Tests/LocalMetaDataTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void CreateMetaDataFile()
3131

3232
var localMetaData = new LocalMetaData(_filePath, _deprecatedFilePath);
3333

34-
localMetaData.RegisterEntry("test", 1);
34+
localMetaData.RegisterEntry("test", 1, 0, true);
3535

3636
Assert.True(File.Exists(_filePath));
3737
}
@@ -41,8 +41,8 @@ public void SaveValidFileSinglePass()
4141
{
4242
var localMetaData = new LocalMetaData(_filePath, _deprecatedFilePath);
4343

44-
localMetaData.RegisterEntry("a", 1);
45-
localMetaData.RegisterEntry("b", 2);
44+
localMetaData.RegisterEntry("a", 1, 0 , true);
45+
localMetaData.RegisterEntry("b", 2, 0 , true);
4646

4747
var localMetaData2 = new LocalMetaData(_filePath, _deprecatedFilePath);
4848

Assets/Editor/Tests/Pack1UnarchiverTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public void Unpack()
3131
string metaString = File.ReadAllText(metaPath);
3232
Pack1Meta meta = Pack1Meta.Parse(metaString);
3333

34-
var pack1Unarchiver = new Pack1Unarchiver(archivePath, meta, _tempDir, Key);
34+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
35+
var pack1Unarchiver = new Pack1Unarchiver(archivePath, meta, _tempDir, mapHashExtractedFiles, Key);
3536
pack1Unarchiver.Unarchive(new CancellationToken());
3637

3738
Assert.True(Directory.Exists(Path.Combine(_tempDir, "dir")));

Assets/Editor/Tests/UnarchiverTest.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ private void CheckConsistency(string sourceDirPath, string dirPath)
5353
[Test]
5454
public void Unarchive()
5555
{
56-
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
56+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
57+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath, mapHashExtractedFiles);
5758

5859
unarchiver.Unarchive(CancellationToken.Empty);
5960

@@ -63,7 +64,8 @@ public void Unarchive()
6364
[Test]
6465
public void CancelUnarchive()
6566
{
66-
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
67+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
68+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath, mapHashExtractedFiles);
6769

6870
CancellationTokenSource source = new CancellationTokenSource();
6971
source.Cancel();
@@ -74,7 +76,8 @@ public void CancelUnarchive()
7476
[Test]
7577
public void UnarchiveCorruptedArchive()
7678
{
77-
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/corrupted-zip.zip"), _dirPath);
79+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
80+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/corrupted-zip.zip"), _dirPath, mapHashExtractedFiles);
7881

7982
Assert.That(() => unarchiver.Unarchive(CancellationToken.Empty), Throws.Exception);
8083
}
@@ -84,7 +87,8 @@ public void UnarchiveWithPassword()
8487
{
8588
string password = "\x08\x07\x18\x24" + "123==";
8689

87-
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/password-zip.zip"), _dirPath, password);
90+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
91+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/password-zip.zip"), _dirPath, mapHashExtractedFiles, password);
8892

8993
unarchiver.Unarchive(CancellationToken.Empty);
9094

@@ -94,7 +98,8 @@ public void UnarchiveWithPassword()
9498
[Test]
9599
public void ProgressReporting()
96100
{
97-
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath);
101+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
102+
var unarchiver = new ZipUnarchiver(TestFixtures.GetFilePath("unarchiver-test/zip.zip"), _dirPath, mapHashExtractedFiles);
98103

99104
int? lastAmount = null;
100105
int? lastEntry = null;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public interface ILocalMetaData
1616
/// </summary>
1717
/// <param name="entryName">Name of the entry.</param>
1818
/// <param name="versionId">The version id.</param>
19-
void RegisterEntry(string entryName, int versionId);
19+
/// <param name="entrySize">Size of entry.</param>
20+
/// <param name="isLastEntry">If set to true, it is last entry.</param>
21+
void RegisterEntry(string entryName, int versionId, long entrySize, bool isLastEntry);
2022

2123
/// <summary>
2224
/// Unregisters the entry.

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ namespace PatchKit.Unity.Patcher.AppData.Local
2020
public class LocalMetaData : ILocalMetaData
2121
{
2222
private readonly ILogger _logger;
23+
private long _unsavedEntriesSize = 0;
2324
private const string DeprecatedCachePatchKitKey = "patchkit-key";
25+
private const long UnsavedEntriesSizeLimit = 104857600; //100MiB
2426

2527
/// <summary>
2628
/// Data structure stored in file.
@@ -79,7 +81,7 @@ public string[] GetRegisteredEntries()
7981
return _data.FileVersionIds.Select(pair => pair.Key).ToArray();
8082
}
8183

82-
public void RegisterEntry([NotNull] string entryName, int versionId)
84+
public void RegisterEntry([NotNull] string entryName, int versionId, long entrySize, bool isLastEntry)
8385
{
8486
if (entryName == null)
8587
{
@@ -103,7 +105,10 @@ public void RegisterEntry([NotNull] string entryName, int versionId)
103105

104106
_data.FileVersionIds[entryName] = versionId;
105107

106-
SaveData();
108+
if (ShouldSaveEntry(entrySize, isLastEntry))
109+
{
110+
SaveData();
111+
}
107112

108113
_logger.LogDebug("Entry registered.");
109114
}
@@ -114,6 +119,22 @@ public void RegisterEntry([NotNull] string entryName, int versionId)
114119
}
115120
}
116121

122+
private bool ShouldSaveEntry(long entrySize, bool isLastEntry)
123+
{
124+
if (isLastEntry)
125+
{
126+
return true;
127+
}
128+
129+
_unsavedEntriesSize += entrySize;
130+
if (_unsavedEntriesSize > UnsavedEntriesSizeLimit)
131+
{
132+
_unsavedEntriesSize = 0;
133+
return true;
134+
}
135+
return false;
136+
}
137+
117138
public void UnregisterEntry([NotNull] string entryName)
118139
{
119140
if (entryName == null)
@@ -183,10 +204,14 @@ public string GetProductKey()
183204

184205
public void SetMainExecutableAndArgs(string mainExecutable, string mainExecutableArgs)
185206
{
186-
_data.MainExecutable = mainExecutable;
187-
_data.MainExecutableArgs = mainExecutableArgs;
207+
if (_data.MainExecutable != mainExecutable ||
208+
_data.MainExecutableArgs != mainExecutableArgs)
209+
{
210+
_data.MainExecutable = mainExecutable;
211+
_data.MainExecutableArgs = mainExecutableArgs;
188212

189-
SaveData();
213+
SaveData();
214+
}
190215
}
191216

192217
public string GetMainExecutable()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
3+
namespace PatchKit.Unity.Patcher.AppData.Local
4+
{
5+
public class MapHashExtractedFiles
6+
{
7+
private Dictionary<string, string> MapHash;
8+
9+
public MapHashExtractedFiles()
10+
{
11+
MapHash = new Dictionary<string, string>();
12+
}
13+
14+
public string Add(string path)
15+
{
16+
string nameHash = HashCalculator.ComputeMD5Hash(path);
17+
MapHash.Add(path, nameHash);
18+
return nameHash;
19+
}
20+
21+
public bool TryGetHash(string path,out string nameHash)
22+
{
23+
if (MapHash.TryGetValue(path, out nameHash))
24+
{
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
}
31+
}

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

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static Pack1Meta Parse(string content)
3636
DebugLogger.Log(content);
3737
return JsonConvert.DeserializeObject<Pack1Meta>(content);
3838
}
39-
4039
#endregion
4140

4241

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class Pack1Unarchiver : IUnarchiver
4141
/// </summary>
4242
private readonly BytesRange _range;
4343

44+
private MapHashExtractedFiles _mapHashExtractedFiles;
45+
4446
public event UnarchiveProgressChangedHandler UnarchiveProgressChanged;
4547

4648
// set to true to continue unpacking on error. Check HasErrors later to see if there are any
@@ -49,23 +51,24 @@ public class Pack1Unarchiver : IUnarchiver
4951
// After Unarchive() finishes if this set to true, there were unpacking errors.
5052
public bool HasErrors { get; private set; }
5153

52-
public Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinationDirPath, string key, string suffix = "")
53-
: this(packagePath, metaData, destinationDirPath, Encoding.ASCII.GetBytes(key), suffix, new BytesRange(0, -1))
54+
public Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinationDirPath, MapHashExtractedFiles mapHashExtractedFiles, string key, string suffix = "")
55+
: this(packagePath, metaData, destinationDirPath, mapHashExtractedFiles, Encoding.ASCII.GetBytes(key), suffix, new BytesRange(0, -1))
5456
{
5557
// do nothing
5658
}
5759

58-
public Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinationDirPath, string key, string suffix, BytesRange range)
59-
: this(packagePath, metaData, destinationDirPath, Encoding.ASCII.GetBytes(key), suffix, range)
60+
public Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinationDirPath, MapHashExtractedFiles mapHashExtractedFiles, string key, string suffix, BytesRange range)
61+
: this(packagePath, metaData, destinationDirPath, mapHashExtractedFiles, Encoding.ASCII.GetBytes(key), suffix, range)
6062
{
6163
// do nothing
6264
}
6365

64-
private Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinationDirPath, byte[] key, string suffix, BytesRange range)
66+
private Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinationDirPath, MapHashExtractedFiles mapHashExtractedFiles, byte[] key, string suffix, BytesRange range)
6567
{
6668
Checks.ArgumentFileExists(packagePath, "packagePath");
6769
Checks.ArgumentDirectoryExists(destinationDirPath, "destinationDirPath");
6870
Checks.ArgumentNotNull(suffix, "suffix");
71+
Checks.ArgumentNotNull(mapHashExtractedFiles, "mapHashExtractedFiles");
6972

7073
if (range.Start == 0)
7174
{
@@ -82,6 +85,7 @@ private Pack1Unarchiver(string packagePath, Pack1Meta metaData, string destinati
8285
_destinationDirPath = destinationDirPath;
8386
_suffix = suffix;
8487
_range = range;
88+
_mapHashExtractedFiles = mapHashExtractedFiles;
8589

8690
using (var sha256 = SHA256.Create())
8791
{
@@ -199,7 +203,7 @@ private void Unpack(Pack1Meta.FileEntry file, Action<double> progress, Cancellat
199203

200204
private void UnpackDirectory(Pack1Meta.FileEntry file, CancellationToken cancellationToken)
201205
{
202-
string destPath = Path.Combine(_destinationDirPath, file.Name);
206+
string destPath = Path.Combine(_destinationDirPath, _mapHashExtractedFiles.Add(file.Name));
203207

204208
DebugLogger.Log("Creating directory " + destPath);
205209
DirectoryOperations.CreateDirectory(destPath, cancellationToken);
@@ -208,7 +212,7 @@ private void UnpackDirectory(Pack1Meta.FileEntry file, CancellationToken cancell
208212

209213
private void UnpackSymlink(Pack1Meta.FileEntry file)
210214
{
211-
string destPath = Path.Combine(_destinationDirPath, file.Name);
215+
string destPath = Path.Combine(_destinationDirPath, _mapHashExtractedFiles.Add(file.Name));
212216
DebugLogger.Log("Creating symlink: " + destPath);
213217
// TODO: how to create a symlink?
214218
}
@@ -230,7 +234,8 @@ private DecompressorCreator ResolveDecompressor(Pack1Meta meta)
230234

231235
private void UnpackRegularFile(Pack1Meta.FileEntry file, Action<double> onProgress, CancellationToken cancellationToken, string destinationDirPath = null)
232236
{
233-
string destPath = Path.Combine(destinationDirPath == null ? _destinationDirPath : destinationDirPath, file.Name + _suffix);
237+
string destPath = Path.Combine(destinationDirPath == null ? _destinationDirPath : destinationDirPath, _mapHashExtractedFiles.Add(file.Name) + _suffix);
238+
234239
DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);
235240

236241
if (file.Size == null)

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Ionic.Zip;
1+
using System.IO;
2+
using Ionic.Zip;
23
using PatchKit.Unity.Patcher.Debug;
34
using PatchKit.Unity.Patcher.Cancellation;
45

@@ -16,6 +17,7 @@ public class ZipUnarchiver : IUnarchiver
1617
private readonly string _password;
1718

1819
private bool _unarchiveHasBeenCalled;
20+
private MapHashExtractedFiles _mapHashExtractedFiles;
1921

2022
public event UnarchiveProgressChangedHandler UnarchiveProgressChanged;
2123

@@ -25,10 +27,11 @@ public class ZipUnarchiver : IUnarchiver
2527
// not used
2628
public bool HasErrors { get; private set; }
2729

28-
public ZipUnarchiver(string packagePath, string destinationDirPath, string password = null)
30+
public ZipUnarchiver(string packagePath, string destinationDirPath, MapHashExtractedFiles mapHashExtractedFiles, string password = null)
2931
{
3032
Checks.ArgumentFileExists(packagePath, "packagePath");
3133
Checks.ArgumentDirectoryExists(destinationDirPath, "destinationDirPath");
34+
Checks.ArgumentNotNull(mapHashExtractedFiles, "mapHashExtractedFiles");
3235

3336
DebugLogger.LogConstructor();
3437
DebugLogger.LogVariable(packagePath, "packagePath");
@@ -37,6 +40,7 @@ public ZipUnarchiver(string packagePath, string destinationDirPath, string passw
3740
_packagePath = packagePath;
3841
_destinationDirPath = destinationDirPath;
3942
_password = password;
43+
_mapHashExtractedFiles = mapHashExtractedFiles;
4044
}
4145

4246
public void Unarchive(CancellationToken cancellationToken)
@@ -69,8 +73,11 @@ public void Unarchive(CancellationToken cancellationToken)
6973
private void UnarchiveEntry(ZipEntry zipEntry)
7074
{
7175
DebugLogger.Log(string.Format("Unarchiving entry {0}", zipEntry.FileName));
72-
73-
zipEntry.Extract(_destinationDirPath, ExtractExistingFileAction.OverwriteSilently);
76+
string destPath = Path.Combine(_destinationDirPath, _mapHashExtractedFiles.Add(zipEntry.FileName));
77+
using (var target = new FileStream(destPath, FileMode.Create))
78+
{
79+
zipEntry.Extract(target);
80+
}
7481
}
7582

7683
protected virtual void OnUnarchiveProgressChanged(string name, bool isFile, int entry, int amount, double entryProgress)

0 commit comments

Comments
 (0)