Skip to content

Commit

Permalink
Merge pull request #57 from textGamex/feat
Browse files Browse the repository at this point in the history
Feat: 错误修复和性能优化
  • Loading branch information
JusterZhu authored Jul 16, 2024
2 parents be74622 + 1ab11fd commit 417012f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
21 changes: 17 additions & 4 deletions src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ namespace GeneralUpdate.Core.ContentProvider
{
public partial class FileProvider
{
private static List<string> _blackFiles, _blackFileFormats;
private static List<string> _blackFiles,
_blackFileFormats;
private static readonly List<string> DefaultBlackFileFormats = new List<string>(6)
{
".patch",
".7z",
".zip",
".rar",
".tar",
".json"
};
private static readonly List<string> DefaultBlackFiles = new List<string>(1) { "Newtonsoft.Json.dll" };

/// <summary>
/// Set a blacklist.
Expand All @@ -21,12 +32,14 @@ public static void SetBlacklist(List<string> blackFiles, List<string> blackFileF
/// These files will be skipped when updating.
/// </summary>
/// <returns></returns>
public static List<string> GetBlackFiles() => _blackFiles ?? new List<string>() { "Newtonsoft.Json.dll" };
public static List<string> GetBlackFiles() =>
_blackFiles ?? DefaultBlackFiles;

/// <summary>
/// These files that contain the file suffix will be skipped when updating.
/// </summary>
/// <returns></returns>
public static List<string> GetBlackFileFormats() => _blackFileFormats ?? new List<string>() { ".patch", ".7z", ".zip", ".rar", ".tar", ".json" };
public static List<string> GetBlackFileFormats() =>
_blackFileFormats ?? DefaultBlackFileFormats;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ public partial class FileProvider
{
public static void CreateJson<T>(string targetPath, T obj)
{
if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath);
if (File.Exists(targetPath)) File.Delete(targetPath);
var folderPath = Path.GetDirectoryName(targetPath) ??
throw new ArgumentException("invalid path", nameof(targetPath));
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}

var jsonString = JsonConvert.SerializeObject(obj);
File.WriteAllText(targetPath, jsonString);
}
Expand All @@ -20,10 +25,7 @@ public static T GetJson<T>(string path)
if (File.Exists(path))
{
var json = File.ReadAllText(path);
if (json != null)
{
return JsonConvert.DeserializeObject<T>(json);
}
return JsonConvert.DeserializeObject<T>(json);
}
return default(T);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public string ComputeHash(string fileName)
{
using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var dataArray = GetHashAlgorithm().ComputeHash(file);
var dataArray = hashAlgorithm.ComputeHash(file);
var stringBuilder = new StringBuilder();
for (int i = 0; i < dataArray.Length; i++)
{
Expand Down
46 changes: 33 additions & 13 deletions src/c#/GeneralUpdate.Differential/DifferentialCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath =
{
try
{
if (string.IsNullOrWhiteSpace(patchPath)) patchPath = Path.Combine(Environment.CurrentDirectory, PATCHS);
if (!Directory.Exists(patchPath)) Directory.CreateDirectory(patchPath);
if (string.IsNullOrWhiteSpace(patchPath))
patchPath = Path.Combine(Environment.CurrentDirectory, PATCHS);
if (!Directory.Exists(patchPath))
Directory.CreateDirectory(patchPath);

//Take the left tree as the center to match the files that are not in the right tree .
var fileProvider = new FileProvider();
var nodes = await fileProvider.Compare(sourcePath, targetPath);
var hashAlgorithm = new Sha256HashAlgorithm();

//Binary differencing of like terms .
foreach (var file in nodes.Item3)
Expand All @@ -93,25 +96,35 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath =
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
tempPath0 = Path.Combine(tempDir, $"{file.Name}{PATCH_FORMAT}");
}

var finOldFile = nodes.Item1.FirstOrDefault(i => i.Name.Equals(file.Name));
var oldfile = finOldFile == null ? "" : finOldFile.FullName;
var newfile = file.FullName;
var oldFile = finOldFile == null ? "" : finOldFile.FullName;
var newFile = file.FullName;
var extensionName = Path.GetExtension(file.FullName);
if (File.Exists(oldfile) && File.Exists(newfile) && !FileProvider.GetBlackFileFormats().Contains(extensionName))
if (File.Exists(oldFile) && File.Exists(newFile) && !FileProvider.GetBlackFileFormats().Contains(extensionName))
{
if (hashAlgorithm.ComputeHash(oldFile)
.Equals(hashAlgorithm.ComputeHash(newFile), StringComparison.OrdinalIgnoreCase))
{
continue;
}

//Generate the difference file to the difference directory .
await new BinaryHandle().Clean(oldfile, newfile, tempPath0);
await new BinaryHandle().Clean(oldFile, newFile, tempPath0);
}
else
{
File.Copy(newfile, Path.Combine(tempDir, Path.GetFileName(newfile)), true);
File.Copy(newFile, Path.Combine(tempDir, Path.GetFileName(newFile)), true);
}
}

//If a file is found that needs to be deleted, a list of files is written to the update package.
var exceptFiles = await fileProvider.Except(sourcePath, targetPath);
if (exceptFiles != null && exceptFiles.Count() > 0)
FileProvider.CreateJson(Path.Combine(patchPath, DELETE_FILES_NAME), exceptFiles);
var exceptFiles = (await fileProvider.Except(sourcePath, targetPath)).ToList();
if (exceptFiles.Count != 0)
{
var path = Path.Combine(patchPath, DELETE_FILES_NAME);
FileProvider.CreateJson(path, exceptFiles);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -142,9 +155,16 @@ public async Task Dirty(string appPath, string patchPath)
var hashAlgorithm = new Sha256HashAlgorithm();
foreach (var file in deleteFiles)
{
var resultFile = oldFiles.FirstOrDefault(i => string.Equals(hashAlgorithm.ComputeHash(i.FullName), file.Hash, StringComparison.OrdinalIgnoreCase));
if (resultFile == null) continue;
if (File.Exists(resultFile.FullName)) File.Delete(resultFile.FullName);
var resultFile = oldFiles.FirstOrDefault(i =>
string.Equals(hashAlgorithm.ComputeHash(i.FullName), file.Hash, StringComparison.OrdinalIgnoreCase));
if (resultFile == null)
{
continue;
}
if (File.Exists(resultFile.FullName))
{
File.Delete(resultFile.FullName);
}
}
}

Expand Down

0 comments on commit 417012f

Please sign in to comment.