Skip to content

Commit 31e9ee5

Browse files
authored
Merge pull request #159 from patchkit-net/bugfix/v3.17.x.x/shorten-file-path-unpacking
Shortening the file path for unpacking
2 parents 1dade67 + 70e0863 commit 31e9ee5

24 files changed

+333
-61
lines changed

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;
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+
Assert.IsNotNull(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: 13 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+
Assert.IsNotNull(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,13 @@ 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+
MemoryStream memoryStream = new MemoryStream();
77+
string destPath = Path.Combine(_destinationDirPath, _mapHashExtractedFiles.Add(zipEntry.FileName));
78+
zipEntry.Extract(memoryStream);
79+
using (var target = new FileStream(destPath, FileMode.Create))
80+
{
81+
memoryStream.WriteTo(target);
82+
}
7483
}
7584

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

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterContentStrategy.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public void Update(CancellationToken cancellationToken)
5353

5454
DebugLogger.LogVariable(latestVersionId, "latestVersionId");
5555

56+
#if UNITY_STANDALONE_WIN
57+
var checkPathLengthCommand = commandFactory.CreateCheckPathLengthCommand(latestVersionId, _context, cancellationToken);
58+
checkPathLengthCommand.Prepare(_status, cancellationToken);
59+
checkPathLengthCommand.Execute(cancellationToken);
60+
#endif
61+
5662
var checkDiskSpaceCommand = commandFactory.CreateCheckDiskSpaceCommandForContent(latestVersionId, _context, cancellationToken);
5763
checkDiskSpaceCommand.Prepare(_status, cancellationToken);
5864
checkDiskSpaceCommand.Execute(cancellationToken);

Assets/PatchKit Patcher/Scripts/AppUpdater/AppUpdaterDiffStrategy.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public void Update(CancellationToken cancellationToken)
6868
geolocateCommand.Prepare(_status, cancellationToken);
6969
geolocateCommand.Execute(cancellationToken);
7070

71+
#if UNITY_STANDALONE_WIN
72+
var checkPathLengthCommand = commandFactory.CreateCheckPathLengthCommand(latestVersionId, _context, cancellationToken);
73+
checkPathLengthCommand.Prepare(_status, cancellationToken);
74+
checkPathLengthCommand.Execute(cancellationToken);
75+
#endif
76+
7177
var checkDiskSpaceCommand = commandFactory.CreateCheckDiskSpaceCommandForDiff(latestVersionId, _context, cancellationToken);
7278
checkDiskSpaceCommand.Prepare(_status, cancellationToken);
7379
checkDiskSpaceCommand.Execute(cancellationToken);

Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/AppUpdaterCommandFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,11 @@ public IGeolocateCommand CreateGeolocateCommand()
133133
var geolocateCommand = new GeolocateCommand();
134134
return geolocateCommand;
135135
}
136+
137+
public ICheckPathLengthCommand CreateCheckPathLengthCommand(int versionId, AppUpdaterContext context, CancellationToken cancellationToken)
138+
{
139+
AppContentSummary contentSummary = context.App.RemoteMetaData.GetContentSummary(versionId, cancellationToken);
140+
return new CheckPathLengthCommand(contentSummary, context.App.LocalDirectory.Path);
141+
}
136142
}
137143
}

0 commit comments

Comments
 (0)