Skip to content

Commit 6ed13cf

Browse files
Jakub SzczyrkJakub Szczyrk
authored andcommitted
Refactoring
1 parent 568fd9e commit 6ed13cf

File tree

11 files changed

+41
-71
lines changed

11 files changed

+41
-71
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@ namespace PatchKit.Unity.Patcher.AppData.Local
44
{
55
public class MapHashExtractedFiles
66
{
7-
private static Dictionary<string, string> MapHash = new Dictionary<string, string>();
7+
private Dictionary<string, string> MapHash = new Dictionary<string, string>();
8+
private static MapHashExtractedFiles _instance = new MapHashExtractedFiles();
89

9-
public static void Clear()
10+
public static MapHashExtractedFiles Instance
11+
{
12+
get { return _instance; }
13+
}
14+
15+
16+
public void Clear()
1017
{
1118
MapHash.Clear();
1219
}
1320

14-
public static string Add(string path)
21+
public string Add(string path)
1522
{
1623
string nameHash = HashCalculator.ComputeMD5Hash(path);
1724
MapHash.Add(path, nameHash);
1825
return nameHash;
1926
}
2027

21-
public static bool TryGetHash(string path,out string nameHash)
28+
public bool TryGetHash(string path,out string nameHash)
2229
{
2330
if (MapHash.TryGetValue(path, out nameHash))
2431
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private void Unpack(Pack1Meta.FileEntry file, Action<double> progress, Cancellat
199199

200200
private void UnpackDirectory(Pack1Meta.FileEntry file, CancellationToken cancellationToken)
201201
{
202-
string destPath = Path.Combine(_destinationDirPath, MapHashExtractedFiles.Add(file.Name));
202+
string destPath = Path.Combine(_destinationDirPath, MapHashExtractedFiles.Instance.Add(file.Name));
203203

204204
DebugLogger.Log("Creating directory " + destPath);
205205
DirectoryOperations.CreateDirectory(destPath, cancellationToken);
@@ -208,7 +208,7 @@ private void UnpackDirectory(Pack1Meta.FileEntry file, CancellationToken cancell
208208

209209
private void UnpackSymlink(Pack1Meta.FileEntry file)
210210
{
211-
string destPath = Path.Combine(_destinationDirPath, MapHashExtractedFiles.Add(file.Name));
211+
string destPath = Path.Combine(_destinationDirPath, MapHashExtractedFiles.Instance.Add(file.Name));
212212
DebugLogger.Log("Creating symlink: " + destPath);
213213
// TODO: how to create a symlink?
214214
}
@@ -230,7 +230,7 @@ private DecompressorCreator ResolveDecompressor(Pack1Meta meta)
230230

231231
private void UnpackRegularFile(Pack1Meta.FileEntry file, Action<double> onProgress, CancellationToken cancellationToken, string destinationDirPath = null)
232232
{
233-
string destPath = Path.Combine(destinationDirPath == null ? _destinationDirPath : destinationDirPath, MapHashExtractedFiles.Add(file.Name) + _suffix);
233+
string destPath = Path.Combine(destinationDirPath == null ? _destinationDirPath : destinationDirPath, MapHashExtractedFiles.Instance.Add(file.Name) + _suffix);
234234

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void UnarchiveEntry(ZipEntry zipEntry)
7171
{
7272
DebugLogger.Log(string.Format("Unarchiving entry {0}", zipEntry.FileName));
7373
MemoryStream memoryStream = new MemoryStream();
74-
string destPath = Path.Combine(_destinationDirPath, HashCalculator.ComputeMD5Hash(zipEntry.FileName));
74+
string destPath = Path.Combine(_destinationDirPath, MapHashExtractedFiles.Instance.Add(zipEntry.FileName));
7575
zipEntry.Extract(memoryStream);
7676
using (var target = new FileStream(destPath, FileMode.Create))
7777
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void Update(CancellationToken cancellationToken)
5454
DebugLogger.LogVariable(latestVersionId, "latestVersionId");
5555

5656
#if UNITY_STANDALONE_WIN
57-
var checkPathLengthCommand = commandFactory.CreateCheckPathLengthCommandForContent(latestVersionId, _context, cancellationToken);
57+
var checkPathLengthCommand = commandFactory.CreateCheckPathLengthCommand(latestVersionId, _context, cancellationToken);
5858
checkPathLengthCommand.Prepare(_status, cancellationToken);
5959
checkPathLengthCommand.Execute(cancellationToken);
6060
#endif

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void Update(CancellationToken cancellationToken)
6969
geolocateCommand.Execute(cancellationToken);
7070

7171
#if UNITY_STANDALONE_WIN
72-
var checkPathLengthCommand = commandFactory.CreateCheckPathLengthCommandForDiff(latestVersionId, _context, cancellationToken);
72+
var checkPathLengthCommand = commandFactory.CreateCheckPathLengthCommand(latestVersionId, _context, cancellationToken);
7373
checkPathLengthCommand.Prepare(_status, cancellationToken);
7474
checkPathLengthCommand.Execute(cancellationToken);
7575
#endif

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,7 @@ public IGeolocateCommand CreateGeolocateCommand()
134134
return geolocateCommand;
135135
}
136136

137-
public ICheckPathLengthCommand CreateCheckPathLengthCommandForDiff(int versionId, AppUpdaterContext context, CancellationToken cancellationToken)
138-
{
139-
AppDiffSummary diffSummary = context.App.RemoteMetaData.GetDiffSummary(versionId, cancellationToken);
140-
return new CheckPathLengthCommand(diffSummary, context.App.LocalDirectory.Path);
141-
}
142-
143-
public ICheckPathLengthCommand CreateCheckPathLengthCommandForContent(int versionId, AppUpdaterContext context, CancellationToken cancellationToken)
137+
public ICheckPathLengthCommand CreateCheckPathLengthCommand(int versionId, AppUpdaterContext context, CancellationToken cancellationToken)
144138
{
145139
AppContentSummary contentSummary = context.App.RemoteMetaData.GetContentSummary(versionId, cancellationToken);
146140
return new CheckPathLengthCommand(contentSummary, context.App.LocalDirectory.Path);

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

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class CheckPathLengthCommand : ICheckPathLengthCommand
1212
private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(CheckPathLengthCommand));
1313

1414
private readonly AppContentSummary? _contentSummary;
15-
private readonly AppDiffSummary? _diffSummary;
1615
private readonly string _localDirectoryPath;
1716
private OperationStatus _status;
1817

@@ -24,14 +23,6 @@ public CheckPathLengthCommand(AppContentSummary contentSummary, string localDire
2423
_localDirectoryPath = localDirectoryPath;
2524
}
2625

27-
public CheckPathLengthCommand(AppDiffSummary diffSummary, string localDirectoryPath)
28-
{
29-
Checks.ArgumentNotNull(localDirectoryPath, "localDirectoryPath");
30-
31-
_diffSummary = diffSummary;
32-
_localDirectoryPath = localDirectoryPath;
33-
}
34-
3526
public void Execute(CancellationToken cancellationToken)
3627
{
3728
_status.IsActive.Value = true;
@@ -40,31 +31,15 @@ public void Execute(CancellationToken cancellationToken)
4031
try
4132
{
4233
string pathFile;
43-
if (UseContentSummary())
34+
foreach (AppContentSummaryFile contentSummaryFile in _contentSummary.Value.Files)
4435
{
45-
foreach (AppContentSummaryFile contentSummaryFile in _contentSummary.Value.Files)
46-
{
47-
pathFile = Path.Combine(_localDirectoryPath, contentSummaryFile.Path);
48-
49-
if (pathFile.Length > 259)
50-
{
51-
throw new FilePathTooLongException(string.Format(
52-
"Cannot install file {0}, the destination path length has exceeded Windows path length limit (260).",
53-
pathFile));
54-
}
55-
}
56-
}
57-
else
58-
{
59-
foreach (string contentSummaryFile in _diffSummary.Value.AddedFiles)
36+
pathFile = Path.Combine(_localDirectoryPath, contentSummaryFile.Path);
37+
38+
if (pathFile.Length > 259)
6039
{
61-
pathFile = Path.Combine(_localDirectoryPath, contentSummaryFile);
62-
if (pathFile.Length > 259)
63-
{
64-
throw new FilePathTooLongException(string.Format(
65-
"Cannot install file {0}, the destination path length has exceeded Windows path length limit (260).",
66-
pathFile));
67-
}
40+
throw new FilePathTooLongException(string.Format(
41+
"Cannot install file {0}, the destination path length has exceeded Windows path length limit (260).",
42+
pathFile));
6843
}
6944
}
7045
}
@@ -73,12 +48,7 @@ public void Execute(CancellationToken cancellationToken)
7348
_status.IsActive.Value = false;
7449
}
7550
}
76-
77-
private bool UseContentSummary()
78-
{
79-
return _contentSummary != null;
80-
}
81-
51+
8252
public void Prepare(UpdaterStatus status, CancellationToken cancellationToken)
8353
{
8454
_status = new OperationStatus

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +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.Clear();
86+
MapHashExtractedFiles.Instance.Clear();
8787

8888
if (_versionContentSummary.CompressionMethod == "pack1")
8989
{
@@ -138,14 +138,13 @@ public override void Execute(CancellationToken cancellationToken)
138138
for (int i = 0; i < _versionContentSummary.Files.Length; i++)
139139
{
140140
cancellationToken.ThrowIfCancellationRequested();
141-
string pathFile = _versionContentSummary.Files[i].Path;
141+
string filePath = _versionContentSummary.Files[i].Path;
142142
string nameHash;
143-
if (MapHashExtractedFiles.TryGetHash(pathFile, out nameHash))
143+
if (MapHashExtractedFiles.Instance.TryGetHash(filePath, out nameHash))
144144
{
145-
var sourceFile = new SourceFile(pathFile, packageDir.Path, usedSuffix, nameHash);
145+
var sourceFile = new SourceFile(filePath, packageDir.Path, usedSuffix, nameHash);
146146

147-
if (unarchiver.HasErrors && !sourceFile.Exists()
148-
) // allow unexistent file only if does not have errors
147+
if (unarchiver.HasErrors && !sourceFile.Exists()) // allow unexistent file only if does not have errors
149148
{
150149
DebugLogger.LogWarning(
151150
"Skipping unexisting file because I've been expecting unpacking errors: " +
@@ -216,23 +215,23 @@ private void InstallFile(SourceFile sourceFile, CancellationToken cancellationTo
216215
struct SourceFile
217216
{
218217
public string Name { get; private set; }
219-
public string Hash { get; private set; }
218+
public string HashName { get; private set; }
220219
private string _suffix;
221220
private string _root;
222221

223-
public string FullHashPath { get { return Path.Combine(_root, Hash + _suffix); } }
222+
public string FullHashPath { get { return Path.Combine(_root, HashName + _suffix); } }
224223

225-
public SourceFile(string name, string root, string suffix, string hash)
224+
public SourceFile(string name, string root, string suffix, string hashName)
226225
{
227226
Assert.IsNotNull(name);
228227
Assert.IsNotNull(root);
229228
Assert.IsNotNull(suffix);
230-
Assert.IsNotNull(hash);
229+
Assert.IsNotNull(hashName);
231230

232231
Name = name;
233232
_root = root;
234233
_suffix = suffix;
235-
Hash = hash;
234+
HashName = hashName;
236235
}
237236

238237
public bool Exists()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public override void Execute(CancellationToken cancellationToken)
147147
_logger.LogDebug("Installing diff...");
148148

149149
base.Execute(cancellationToken);
150-
MapHashExtractedFiles.Clear();
150+
MapHashExtractedFiles.Instance.Clear();
151151

152152
_logger.LogTrace("diffSummary.compressionMethod = " + _diffSummary.CompressionMethod);
153153

@@ -471,7 +471,7 @@ private void AddFile(string fileName, string packageDirPath, string suffix, Canc
471471
}
472472
#endif
473473
string nameHash;
474-
if (MapHashExtractedFiles.TryGetHash(fileName, out nameHash))
474+
if (MapHashExtractedFiles.Instance.TryGetHash(fileName, out nameHash))
475475
{
476476
var sourceFilePath = Path.Combine(packageDirPath, nameHash + suffix);
477477
_logger.LogTrace("sourceFilePath = " + sourceFilePath);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public RepairFilesCommand(
6565
public override void Execute(CancellationToken cancellationToken)
6666
{
6767
base.Execute(cancellationToken);
68-
MapHashExtractedFiles.Clear();
68+
MapHashExtractedFiles.Instance.Clear();
6969

7070
foreach (var entry in _entries)
7171
{
@@ -127,7 +127,7 @@ public override void Execute(CancellationToken cancellationToken)
127127

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

0 commit comments

Comments
 (0)