Skip to content

Commit 70e0863

Browse files
Jakub SzczyrkJakub Szczyrk
authored andcommitted
Refactoring
1 parent 230fa63 commit 70e0863

File tree

9 files changed

+51
-45
lines changed

9 files changed

+51
-45
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;

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ namespace PatchKit.Unity.Patcher.AppData.Local
44
{
55
public class MapHashExtractedFiles
66
{
7-
private Dictionary<string, string> MapHash = new Dictionary<string, string>();
8-
private static MapHashExtractedFiles _instance = new MapHashExtractedFiles();
9-
10-
public static MapHashExtractedFiles Instance
11-
{
12-
get { return _instance; }
13-
}
14-
15-
16-
public void Clear()
7+
private Dictionary<string, string> MapHash;
8+
9+
public MapHashExtractedFiles()
1710
{
18-
MapHash.Clear();
11+
MapHash = new Dictionary<string, string>();
1912
}
2013

2114
public string Add(string path)

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

Lines changed: 12 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, MapHashExtractedFiles.Instance.Add(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, MapHashExtractedFiles.Instance.Add(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,7 @@ 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, MapHashExtractedFiles.Instance.Add(file.Name) + _suffix);
237+
string destPath = Path.Combine(destinationDirPath == null ? _destinationDirPath : destinationDirPath, _mapHashExtractedFiles.Add(file.Name) + _suffix);
234238

235239
DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);
236240

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ZipUnarchiver : IUnarchiver
1717
private readonly string _password;
1818

1919
private bool _unarchiveHasBeenCalled;
20+
private MapHashExtractedFiles _mapHashExtractedFiles;
2021

2122
public event UnarchiveProgressChangedHandler UnarchiveProgressChanged;
2223

@@ -26,10 +27,11 @@ public class ZipUnarchiver : IUnarchiver
2627
// not used
2728
public bool HasErrors { get; private set; }
2829

29-
public ZipUnarchiver(string packagePath, string destinationDirPath, string password = null)
30+
public ZipUnarchiver(string packagePath, string destinationDirPath, MapHashExtractedFiles mapHashExtractedFiles, string password = null)
3031
{
3132
Checks.ArgumentFileExists(packagePath, "packagePath");
3233
Checks.ArgumentDirectoryExists(destinationDirPath, "destinationDirPath");
34+
Assert.IsNotNull(mapHashExtractedFiles);
3335

3436
DebugLogger.LogConstructor();
3537
DebugLogger.LogVariable(packagePath, "packagePath");
@@ -38,6 +40,7 @@ public ZipUnarchiver(string packagePath, string destinationDirPath, string passw
3840
_packagePath = packagePath;
3941
_destinationDirPath = destinationDirPath;
4042
_password = password;
43+
_mapHashExtractedFiles = mapHashExtractedFiles;
4144
}
4245

4346
public void Unarchive(CancellationToken cancellationToken)
@@ -71,7 +74,7 @@ private void UnarchiveEntry(ZipEntry zipEntry)
7174
{
7275
DebugLogger.Log(string.Format("Unarchiving entry {0}", zipEntry.FileName));
7376
MemoryStream memoryStream = new MemoryStream();
74-
string destPath = Path.Combine(_destinationDirPath, MapHashExtractedFiles.Instance.Add(zipEntry.FileName));
77+
string destPath = Path.Combine(_destinationDirPath, _mapHashExtractedFiles.Add(zipEntry.FileName));
7578
zipEntry.Extract(memoryStream);
7679
using (var target = new FileStream(destPath, FileMode.Create))
7780
{

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public override void Execute(CancellationToken cancellationToken)
8383
Checks.FileExists(_packagePath);
8484
Assert.IsTrue(_localMetaData.GetRegisteredEntries().Length == 0,
8585
"Cannot install content if previous version is still present.");
86-
MapHashExtractedFiles.Instance.Clear();
87-
86+
8887
if (_versionContentSummary.CompressionMethod == "pack1")
8988
{
9089
Assert.IsTrue(File.Exists(_packageMetaPath),
@@ -104,7 +103,8 @@ public override void Execute(CancellationToken cancellationToken)
104103
DebugLogger.Log("Unarchiving package.");
105104

106105
string usedSuffix;
107-
IUnarchiver unarchiver = CreateUnrachiver(packageDir.Path, out usedSuffix);
106+
MapHashExtractedFiles mapHashExtractedFiles = new MapHashExtractedFiles();
107+
IUnarchiver unarchiver = CreateUnrachiver(packageDir.Path, mapHashExtractedFiles, out usedSuffix);
108108

109109
_unarchivePackageStatus.IsActive.Value = true;
110110
_unarchivePackageStatus.Description.Value = "Unarchiving package...";
@@ -140,7 +140,7 @@ public override void Execute(CancellationToken cancellationToken)
140140
cancellationToken.ThrowIfCancellationRequested();
141141
string filePath = _versionContentSummary.Files[i].Path;
142142
string nameHash;
143-
if (MapHashExtractedFiles.Instance.TryGetHash(filePath, out nameHash))
143+
if (mapHashExtractedFiles.TryGetHash(filePath, out nameHash))
144144
{
145145
var sourceFile = new SourceFile(filePath, packageDir.Path, usedSuffix, nameHash);
146146

@@ -169,16 +169,16 @@ public override void Execute(CancellationToken cancellationToken)
169169
});
170170
}
171171

172-
private IUnarchiver CreateUnrachiver(string destinationDir, out string usedSuffix)
172+
private IUnarchiver CreateUnrachiver(string destinationDir, MapHashExtractedFiles mapHashExtractedFiles, out string usedSuffix)
173173
{
174174
switch (_versionContentSummary.CompressionMethod)
175175
{
176176
case "zip":
177177
usedSuffix = string.Empty;
178-
return new ZipUnarchiver(_packagePath, destinationDir, _packagePassword);
178+
return new ZipUnarchiver(_packagePath, destinationDir, mapHashExtractedFiles, _packagePassword);
179179
case "pack1":
180180
usedSuffix = Suffix;
181-
return new Pack1Unarchiver(_packagePath, _pack1Meta, destinationDir, _packagePassword, Suffix);
181+
return new Pack1Unarchiver(_packagePath, _pack1Meta, destinationDir, mapHashExtractedFiles, _packagePassword, Suffix);
182182
default:
183183
throw new InstallerException(string.Format("Unknown compression method: {0}",
184184
_versionContentSummary.CompressionMethod));

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class InstallDiffCommand : BaseAppUpdaterCommand, IInstallDiffCommand
3939
private AppContentSummary _contentSummary;
4040
private AppDiffSummary _diffSummary;
4141
private Pack1Meta _pack1Meta;
42+
private MapHashExtractedFiles _mapHashExtractedFiles;
4243

4344
public InstallDiffCommand([NotNull] string packagePath, string packageMetaPath, string packagePassword,
4445
int versionId,
@@ -78,6 +79,7 @@ public InstallDiffCommand([NotNull] string packagePath, string packageMetaPath,
7879
_localData = localData;
7980
_localMetaData = localMetaData;
8081
_remoteMetaData = remoteMetaData;
82+
_mapHashExtractedFiles = new MapHashExtractedFiles();
8183
}
8284

8385
public override void Prepare([NotNull] UpdaterStatus status, CancellationToken cancellationToken)
@@ -147,8 +149,7 @@ public override void Execute(CancellationToken cancellationToken)
147149
_logger.LogDebug("Installing diff...");
148150

149151
base.Execute(cancellationToken);
150-
MapHashExtractedFiles.Instance.Clear();
151-
152+
152153
_logger.LogTrace("diffSummary.compressionMethod = " + _diffSummary.CompressionMethod);
153154

154155
if (_diffSummary.CompressionMethod == "pack1")
@@ -299,10 +300,10 @@ private IUnarchiver CreateUnrachiver(string destinationDir, out string usedSuffi
299300
{
300301
case "zip":
301302
usedSuffix = string.Empty;
302-
return new ZipUnarchiver(_packagePath, destinationDir, _packagePassword);
303+
return new ZipUnarchiver(_packagePath, destinationDir, _mapHashExtractedFiles, _packagePassword);
303304
case "pack1":
304305
usedSuffix = Suffix;
305-
return new Pack1Unarchiver(_packagePath, _pack1Meta, destinationDir, _packagePassword, Suffix);
306+
return new Pack1Unarchiver(_packagePath, _pack1Meta, destinationDir, _mapHashExtractedFiles, _packagePassword, Suffix);
306307
default:
307308
throw new UnknownPackageCompressionModeException(string.Format("Unknown compression method: {0}",
308309
_diffSummary.CompressionMethod));
@@ -471,7 +472,7 @@ private void AddFile(string fileName, string packageDirPath, string suffix, Canc
471472
}
472473
#endif
473474
string nameHash;
474-
if (MapHashExtractedFiles.Instance.TryGetHash(fileName, out nameHash))
475+
if (_mapHashExtractedFiles.TryGetHash(fileName, out nameHash))
475476
{
476477
var sourceFilePath = Path.Combine(packageDirPath, nameHash + suffix);
477478
_logger.LogTrace("sourceFilePath = " + sourceFilePath);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public RepairFilesCommand(
6565
public override void Execute(CancellationToken cancellationToken)
6666
{
6767
base.Execute(cancellationToken);
68-
MapHashExtractedFiles.Instance.Clear();
69-
68+
7069
foreach (var entry in _entries)
7170
{
7271
var tempDirName = _packagePath + "_repair";
@@ -80,7 +79,8 @@ public override void Execute(CancellationToken cancellationToken)
8079
{
8180
DirectoryOperations.CreateDirectory(unarchivePath, cancellationToken);
8281
}
83-
82+
MapHashExtractedFiles mapHashExtracted = new MapHashExtractedFiles();
83+
8484
var downloader = new ChunkedHttpDownloader(packagePath, _resource.ResourceUrls, _resource.ChunksData, _resource.Size);
8585

8686
long start = entry.Offset.GetValueOrDefault();
@@ -116,7 +116,7 @@ public override void Execute(CancellationToken cancellationToken)
116116
repairStatus.Progress.Value = 0.0;
117117

118118
_logger.LogDebug("Unarchiving the package.");
119-
var unarchiver = new Pack1Unarchiver(packagePath, _meta, unarchivePath, _packagePassword, _unpackingSuffix, effectiveRange);
119+
var unarchiver = new Pack1Unarchiver(packagePath, _meta, unarchivePath, mapHashExtracted, _packagePassword, _unpackingSuffix, effectiveRange);
120120
// allow repair to continue on errors, because after the repair process, all the files must be validated again
121121
unarchiver.ContinueOnError = true;
122122

@@ -127,7 +127,7 @@ public override void Execute(CancellationToken cancellationToken)
127127

128128
unarchiver.UnarchiveSingleFile(entry, cancellationToken);
129129
string nameHash;
130-
if (MapHashExtractedFiles.Instance.TryGetHash(entry.Name, out nameHash))
130+
if (mapHashExtracted.TryGetHash(entry.Name, out nameHash))
131131
{
132132
EmplaceFile(Path.Combine(unarchivePath, nameHash + _unpackingSuffix),
133133
Path.Combine(_localData.Path, entry.Name), cancellationToken);

Assets/PatchKit Patcher/Scripts/Patcher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,6 @@ private void ThreadUpdateApp(bool automatically, CancellationToken cancellationT
940940

941941
_updaterStatus.Value = null;
942942
_updateAppCancellationTokenSource = null;
943-
MapHashExtractedFiles.Instance.Clear();
944943
}
945944
}
946945
}

0 commit comments

Comments
 (0)