main #2

Merged
waffle.lord merged 3 commits from main into avalonia-port 2021-12-19 11:24:13 -05:00
8 changed files with 541 additions and 267 deletions
Showing only changes of commit 644a2fb4e9 - Show all commits

View File

@ -21,24 +21,15 @@ namespace PatchClient
{ {
Task.Run(() => Task.Run(() =>
{ {
FilePatcher bp = new FilePatcher() PatchHelper patcher = new PatchHelper(Environment.CurrentDirectory, null, LazyOperations.PatchFolder.FromCwd());
{
TargetBase = Environment.CurrentDirectory,
PatchBase = LazyOperations.PatchFolder.FromCwd()
};
bp.ProgressChanged += Bp_ProgressChanged; patcher.ProgressChanged += patcher_ProgressChanged;
try try
{ {
if (bp.Run()) string message = patcher.ApplyPatches();
{
MessageBox.Show("Patch completed without issues", "Patching Successful"); MessageBox.Show(message, "Patcher");
}
else
{
MessageBox.Show("Failed to patch client.", "Patching Failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
} }
catch(Exception ex) catch(Exception ex)
{ {
@ -54,7 +45,7 @@ namespace PatchClient
}); });
} }
private void Bp_ProgressChanged(object Sender, int Progress, int Total, int Percent, string Message = "", params LineItem[] AdditionalLineItems) private void patcher_ProgressChanged(object Sender, int Progress, int Total, int Percent, string Message = "", params LineItem[] AdditionalLineItems)
{ {
string additionalInfo = ""; string additionalInfo = "";
foreach (LineItem item in AdditionalLineItems) foreach (LineItem item in AdditionalLineItems)

View File

@ -120,13 +120,14 @@ namespace PatchGenerator
GenProgressBar.DispatcherSetIndetermination(false); GenProgressBar.DispatcherSetIndetermination(false);
//generate patches //generate patches
FileCompare bc = new FileCompare(targetFolder, compareFolder, patchBase); //TODO - fix these weird variable names (why did I do this?)
PatchHelper patcher = new PatchHelper(compareFolder, targetFolder, patchBase);
bc.ProgressChanged += Bc_ProgressChanged; patcher.ProgressChanged += patcher_ProgressChanged;
if (!bc.CompareAll()) if (!patcher.GeneratePatches())
{ {
MessageBox.Show("Failed to generate diffs.", ":(", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show("One of the provided folder paths doesn't exist.", "Oops :(", MessageBoxButton.OK, MessageBoxImage.Error);
} }
//Copy patch client to output folder //Copy patch client to output folder
@ -142,7 +143,7 @@ namespace PatchGenerator
GenProgressMessageLabel.DispaatcherSetContent("Done"); GenProgressMessageLabel.DispaatcherSetContent("Done");
} }
private void Bc_ProgressChanged(object Sender, int Progress, int Total, int Percent, string Message = "", params LineItem[] AdditionalLineItems) private void patcher_ProgressChanged(object Sender, int Progress, int Total, int Percent, string Message = "", params LineItem[] AdditionalLineItems)
{ {
string additionalInfo = ""; string additionalInfo = "";
foreach (LineItem item in AdditionalLineItems) foreach (LineItem item in AdditionalLineItems)

View File

@ -12,174 +12,174 @@ using System.Linq;
namespace PatcherUtils namespace PatcherUtils
{ {
public class FileCompare //public class FileCompare
{ //{
public string PatchBase; // public string PatchBase;
public string TargetBase; // public string TargetBase;
public string CompareBase; // public string CompareBase;
private int fileCount; // private int fileCount;
private int fileIt; // private int fileIt;
private int diffCount = 0; // private int diffCount = 0;
private int newCount = 0; // private int newCount = 0;
private int delCount = 0; // private int delCount = 0;
private int matchCount = 0; // private int matchCount = 0;
private List<FileInfo> TargetPaths; // private List<FileInfo> TargetPaths;
private List<FileInfo> ComparePaths; // private List<FileInfo> ComparePaths;
private List<LineItem> AdditionalInfo = new List<LineItem>(); // private List<LineItem> AdditionalInfo = new List<LineItem>();
/// <summary> // /// <summary>
/// Provides patch generation progress changes // /// Provides patch generation progress changes
/// </summary> // /// </summary>
public event ProgressChangedHandler ProgressChanged; // public event ProgressChangedHandler ProgressChanged;
protected virtual void RaiseProgressChanged(int progress, int total, string Message = "", params LineItem[] AdditionalLineItems) // protected virtual void RaiseProgressChanged(int progress, int total, string Message = "", params LineItem[] AdditionalLineItems)
{ // {
int percent = (int)Math.Floor((double)progress / total * 100); // int percent = (int)Math.Floor((double)progress / total * 100);
ProgressChanged?.Invoke(this, progress, total, percent, Message, AdditionalLineItems); // ProgressChanged?.Invoke(this, progress, total, percent, Message, AdditionalLineItems);
} // }
/// <summary> // /// <summary>
/// Compare a target file to an assumed compareable file. // /// Compare a target file to an assumed compareable file.
/// </summary> // /// </summary>
/// <param name="targetFile">The known target path</param> // /// <param name="targetFile">The known target path</param>
/// <param name="assumedCompareFile">The assumed comparable file path</param> // /// <param name="assumedCompareFile">The assumed comparable file path</param>
/// <returns>True if a comparison was made | False if a comparison could not be made</returns> // /// <returns>True if a comparison was made | False if a comparison could not be made</returns>
private bool Compare(string targetFile, string assumedCompareFile) // private bool Compare(string targetFile, string assumedCompareFile)
{ // {
string patchFilePath = targetFile.Replace(TargetBase, PatchBase); // string patchFilePath = targetFile.Replace(TargetBase, PatchBase);
//we know our target file exists // //we know our target file exists
byte[] targetData = VFS.ReadFile(targetFile); // byte[] targetData = VFS.ReadFile(targetFile);
if(!File.Exists(assumedCompareFile)) // if(!File.Exists(assumedCompareFile))
{ // {
//save the data we won't have in our target as new // //save the data we won't have in our target as new
VFS.WriteFile($"{patchFilePath}.new", Zlib.Compress(targetData, ZlibCompression.Maximum)); // VFS.WriteFile($"{patchFilePath}.new", Zlib.Compress(targetData, ZlibCompression.Maximum));
newCount++; // newCount++;
return true; // return true;
} // }
//now our compare file is known to exist // //now our compare file is known to exist
byte[] compareData = VFS.ReadFile(assumedCompareFile); // byte[] compareData = VFS.ReadFile(assumedCompareFile);
// get diffs // // get diffs
DiffResult result = PatchUtil.Diff(compareData, targetData); // DiffResult result = PatchUtil.Diff(compareData, targetData);
switch (result.Result) // switch (result.Result)
{ // {
case DiffResultType.Success: // case DiffResultType.Success:
VFS.WriteFile($"{patchFilePath}.bpf", result.PatchInfo.ToBytes()); // VFS.WriteFile($"{patchFilePath}.bpf", result.PatchInfo.ToBytes());
diffCount++; // diffCount++;
return true; // return true;
case DiffResultType.FilesMatch: // case DiffResultType.FilesMatch:
matchCount++; // matchCount++;
return true; // return true;
default: // default:
return false; // return false;
} // }
} // }
/// <summary> // /// <summary>
/// Compares the base folders and generates patch files. // /// Compares the base folders and generates patch files.
/// </summary> // /// </summary>
/// <returns>True if patches were generated successfully | False if patch generation failed</returns> // /// <returns>True if patches were generated successfully | False if patch generation failed</returns>
public bool CompareAll() // public bool CompareAll()
{ // {
DirectoryInfo targetDirInfo = new DirectoryInfo(TargetBase); // DirectoryInfo targetDirInfo = new DirectoryInfo(TargetBase);
DirectoryInfo compareDirInfo = new DirectoryInfo(CompareBase); // DirectoryInfo compareDirInfo = new DirectoryInfo(CompareBase);
AdditionalInfo.Add(new LineItem("Diff Patch", "0")); // AdditionalInfo.Add(new LineItem("Diff Patch", "0"));
AdditionalInfo.Add(new LineItem("New Patch", "0")); // AdditionalInfo.Add(new LineItem("New Patch", "0"));
AdditionalInfo.Add(new LineItem("Del Patch", "0")); // AdditionalInfo.Add(new LineItem("Del Patch", "0"));
AdditionalInfo.Add(new LineItem("Files Match", "0")); // AdditionalInfo.Add(new LineItem("Files Match", "0"));
if (!targetDirInfo.Exists || !compareDirInfo.Exists) // if (!targetDirInfo.Exists || !compareDirInfo.Exists)
{ // {
Console.WriteLine("Target or Compare folder does not exist"); // Console.WriteLine("Target or Compare folder does not exist");
return false; // return false;
} // }
//Get all the files recursively // //Get all the files recursively
TargetPaths = new List<FileInfo>(targetDirInfo.GetFiles("*.*", SearchOption.AllDirectories)); // TargetPaths = new List<FileInfo>(targetDirInfo.GetFiles("*.*", SearchOption.AllDirectories));
ComparePaths = new List<FileInfo>(compareDirInfo.GetFiles("*.*", SearchOption.AllDirectories)); // ComparePaths = new List<FileInfo>(compareDirInfo.GetFiles("*.*", SearchOption.AllDirectories));
RaiseProgressChanged(0, fileCount, "Generating diffs..."); // RaiseProgressChanged(0, fileCount, "Generating diffs...");
/* Comparing Target files -> Compare files // /* Comparing Target files -> Compare files
* - Exists = Diff (.bfd file) // * - Exists = Diff (.bfd file)
* - Doesn't Exist = New (.new file) // * - Doesn't Exist = New (.new file)
* // *
* Once everything has been compared from one side, any remaining paths in our ComparePaths // * Once everything has been compared from one side, any remaining paths in our ComparePaths
* are things that don't exist in our target and can be deleted (.del file) // * are things that don't exist in our target and can be deleted (.del file)
*/ // */
for (int x = 0; x < TargetPaths.Count; x++) // for (int x = 0; x < TargetPaths.Count; x++)
{ // {
FileInfo file = TargetPaths[x]; // FileInfo file = TargetPaths[x];
string assumedComparePath = file.DirectoryName.Replace(TargetBase, CompareBase); // string assumedComparePath = file.DirectoryName.Replace(TargetBase, CompareBase);
if (!Compare(file.FullName, VFS.Combine(assumedComparePath, file.Name))) // if (!Compare(file.FullName, VFS.Combine(assumedComparePath, file.Name)))
{ // {
return false; // return false;
} // }
//remove any existing files from our ComparePaths // //remove any existing files from our ComparePaths
FileInfo assumedFile = new FileInfo(VFS.Combine(assumedComparePath, file.Name)); // FileInfo assumedFile = new FileInfo(VFS.Combine(assumedComparePath, file.Name));
if (assumedFile.Exists && ComparePaths.Exists(x => x.FullName == assumedFile.FullName)) // if (assumedFile.Exists && ComparePaths.Exists(x => x.FullName == assumedFile.FullName))
{ // {
ComparePaths.Remove(ComparePaths.Where(x => x.FullName == assumedFile.FullName).FirstOrDefault()); // ComparePaths.Remove(ComparePaths.Where(x => x.FullName == assumedFile.FullName).FirstOrDefault());
} // }
AdditionalInfo[0].ItemValue = diffCount.ToString(); // AdditionalInfo[0].ItemValue = diffCount.ToString();
AdditionalInfo[1].ItemValue = newCount.ToString(); // AdditionalInfo[1].ItemValue = newCount.ToString();
AdditionalInfo[3].ItemValue = matchCount.ToString(); // AdditionalInfo[3].ItemValue = matchCount.ToString();
fileIt++; // fileIt++;
RaiseProgressChanged(fileIt, fileCount, file.Name, AdditionalInfo.ToArray()); // RaiseProgressChanged(fileIt, fileCount, file.Name, AdditionalInfo.ToArray());
} // }
if (ComparePaths.Count == 0) // if (ComparePaths.Count == 0)
{ // {
//if there are no files to delete, just return true // //if there are no files to delete, just return true
return true; // return true;
} // }
//progress reset for files that need to be deleted // //progress reset for files that need to be deleted
RaiseProgressChanged(0, ComparePaths.Count, "Processing .del files..."); // RaiseProgressChanged(0, ComparePaths.Count, "Processing .del files...");
fileIt = 0; // fileIt = 0;
fileCount = ComparePaths.Count; // fileCount = ComparePaths.Count;
//the paths remaining in ComparePaths don't exist in our target and need to be removed during patching. // //the paths remaining in ComparePaths don't exist in our target and need to be removed during patching.
foreach (FileInfo file in ComparePaths) // foreach (FileInfo file in ComparePaths)
{ // {
//add del files replace root dir with patch base // //add del files replace root dir with patch base
string patchFilePath = file.FullName.Replace(CompareBase, PatchBase); // string patchFilePath = file.FullName.Replace(CompareBase, PatchBase);
VFS.WriteFile($"{patchFilePath}.del", new byte[0]); // VFS.WriteFile($"{patchFilePath}.del", new byte[0]);
delCount++; // delCount++;
AdditionalInfo[2].ItemValue = delCount.ToString(); // AdditionalInfo[2].ItemValue = delCount.ToString();
fileIt++; // fileIt++;
RaiseProgressChanged(fileIt, fileCount, "", AdditionalInfo.ToArray()); // RaiseProgressChanged(fileIt, fileCount, "", AdditionalInfo.ToArray());
} // }
return true; // return true;
} // }
public FileCompare(string TargetBase, string CompareBase, string PatchBase) // public FileCompare(string TargetBase, string CompareBase, string PatchBase)
{ // {
this.TargetBase = TargetBase; // this.TargetBase = TargetBase;
this.CompareBase = CompareBase; // this.CompareBase = CompareBase;
this.PatchBase = PatchBase; // this.PatchBase = PatchBase;
fileCount = VFS.GetFilesCount(TargetBase); // fileCount = VFS.GetFilesCount(TargetBase);
fileIt = 0; // fileIt = 0;
} // }
} //}
} }

View File

@ -7,141 +7,141 @@ using System.Linq;
namespace PatcherUtils namespace PatcherUtils
{ {
public class FilePatcher //public class FilePatcher
{ //{
public string TargetBase; // public string TargetBase;
public string PatchBase; // public string PatchBase;
private int fileCount; // private int fileCount;
private int fileIt; // private int fileIt;
private int diffCount; // private int diffCount;
private int newCount; // private int newCount;
private int delCount; // private int delCount;
private List<LineItem> AdditionalInfo; // private List<LineItem> AdditionalInfo;
public event ProgressChangedHandler ProgressChanged; // public event ProgressChangedHandler ProgressChanged;
protected virtual void RaiseProgressChanged(int progress, int total, string Message = "", params LineItem[] AdditionalLineItems) // protected virtual void RaiseProgressChanged(int progress, int total, string Message = "", params LineItem[] AdditionalLineItems)
{ // {
int percent = (int)Math.Floor((double)progress / total * 100); // int percent = (int)Math.Floor((double)progress / total * 100);
ProgressChanged?.Invoke(this, progress, total, percent, Message, AdditionalLineItems); // ProgressChanged?.Invoke(this, progress, total, percent, Message, AdditionalLineItems);
} // }
public bool Patch(string targetfile, string patchfile) // public bool Patch(string targetfile, string patchfile)
{ // {
byte[] target = VFS.ReadFile(targetfile); // byte[] target = VFS.ReadFile(targetfile);
byte[] patch = VFS.ReadFile(patchfile); // byte[] patch = VFS.ReadFile(patchfile);
PatchResult result = PatchUtil.Patch(target, PatchInfo.FromBytes(patch)); // PatchResult result = PatchUtil.Patch(target, PatchInfo.FromBytes(patch));
switch (result.Result) // switch (result.Result)
{ // {
case PatchResultType.Success: // case PatchResultType.Success:
VFS.WriteFile(targetfile, result.PatchedData); // VFS.WriteFile(targetfile, result.PatchedData);
return true; // return true;
case PatchResultType.AlreadyPatched: // case PatchResultType.AlreadyPatched:
case PatchResultType.InputChecksumMismatch: // case PatchResultType.InputChecksumMismatch:
case PatchResultType.InputLengthMismatch: // case PatchResultType.InputLengthMismatch:
return true; // return true;
case PatchResultType.OutputChecksumMismatch: // case PatchResultType.OutputChecksumMismatch:
default: // default:
return false; // return false;
} // }
} // }
private bool PatchAll(string targetpath, string patchpath) // private bool PatchAll(string targetpath, string patchpath)
{ // {
DirectoryInfo di = new DirectoryInfo(patchpath); // DirectoryInfo di = new DirectoryInfo(patchpath);
foreach (FileInfo file in di.GetFiles()) // foreach (FileInfo file in di.GetFiles())
{ // {
FileInfo target = null; // FileInfo target = null;
switch (file.Extension) // switch (file.Extension)
{ // {
// patch // // patch
case ".bpf": // case ".bpf":
{ // {
target = new FileInfo(VFS.Combine(targetpath, file.Name.Replace(".bpf", ""))); // target = new FileInfo(VFS.Combine(targetpath, file.Name.Replace(".bpf", "")));
if (!Patch(target.FullName, file.FullName)) // if (!Patch(target.FullName, file.FullName))
{ // {
// patch failed // // patch failed
return false; // return false;
} // }
diffCount--; // diffCount--;
} // }
break; // break;
// add new files // // add new files
case ".new": // case ".new":
{ // {
target = new FileInfo(VFS.Combine(targetpath, file.Name.Replace(".new", ""))); // target = new FileInfo(VFS.Combine(targetpath, file.Name.Replace(".new", "")));
VFS.WriteFile(target.FullName, Zlib.Decompress(VFS.ReadFile(file.FullName))); // VFS.WriteFile(target.FullName, Zlib.Decompress(VFS.ReadFile(file.FullName)));
newCount--; // newCount--;
} // }
break; // break;
// delete old files // // delete old files
case ".del": // case ".del":
{ // {
target = new FileInfo(VFS.Combine(targetpath, file.Name.Replace(".del", ""))); // target = new FileInfo(VFS.Combine(targetpath, file.Name.Replace(".del", "")));
target.IsReadOnly = false; // target.IsReadOnly = false;
target.Delete(); // target.Delete();
delCount--; // delCount--;
} // }
break; // break;
} // }
AdditionalInfo[0].ItemValue = diffCount.ToString(); // AdditionalInfo[0].ItemValue = diffCount.ToString();
AdditionalInfo[1].ItemValue = newCount.ToString(); // AdditionalInfo[1].ItemValue = newCount.ToString();
AdditionalInfo[2].ItemValue = delCount.ToString(); // AdditionalInfo[2].ItemValue = delCount.ToString();
++fileIt; // ++fileIt;
RaiseProgressChanged(fileIt, fileCount, target.Name, AdditionalInfo.ToArray()); // RaiseProgressChanged(fileIt, fileCount, target.Name, AdditionalInfo.ToArray());
} // }
foreach (DirectoryInfo directory in di.GetDirectories()) // foreach (DirectoryInfo directory in di.GetDirectories())
{ // {
PatchAll(VFS.Combine(targetpath, directory.Name), directory.FullName); // PatchAll(VFS.Combine(targetpath, directory.Name), directory.FullName);
} // }
di.Refresh(); // di.Refresh();
if (di.GetFiles().Length == 0 && di.GetDirectories().Length == 0) // if (di.GetFiles().Length == 0 && di.GetDirectories().Length == 0)
{ // {
// remove empty folders // // remove empty folders
di.Delete(); // di.Delete();
} // }
return true; // return true;
} // }
public bool Run() // public bool Run()
{ // {
fileCount = VFS.GetFilesCount(PatchBase); // fileCount = VFS.GetFilesCount(PatchBase);
FileInfo[] files = new DirectoryInfo(PatchBase).GetFiles("*.*", SearchOption.AllDirectories); // FileInfo[] files = new DirectoryInfo(PatchBase).GetFiles("*.*", SearchOption.AllDirectories);
diffCount = files.Where(x => x.Extension == ".bpf").Count(); // diffCount = files.Where(x => x.Extension == ".bpf").Count();
newCount = files.Where(x => x.Extension == ".new").Count(); // newCount = files.Where(x => x.Extension == ".new").Count();
delCount = files.Where(x => x.Extension == ".del").Count(); // delCount = files.Where(x => x.Extension == ".del").Count();
AdditionalInfo = new List<LineItem>() // AdditionalInfo = new List<LineItem>()
{ // {
new LineItem("Patches Remaining", diffCount.ToString()), // new LineItem("Patches Remaining", diffCount.ToString()),
new LineItem("New Files to Inflate", newCount.ToString()), // new LineItem("New Files to Inflate", newCount.ToString()),
new LineItem("Files to Delete", delCount.ToString()) // new LineItem("Files to Delete", delCount.ToString())
}; // };
fileIt = 0; // fileIt = 0;
return PatchAll(TargetBase, PatchBase); // return PatchAll(TargetBase, PatchBase);
} // }
} //}
} }

View File

@ -12,12 +12,13 @@ namespace PatcherUtils
/// </summary> /// </summary>
public static string TempDir = "PATCHER_TEMP".FromCwd(); public static string TempDir = "PATCHER_TEMP".FromCwd();
private static string SevenZExe = "7za.exe";
/// <summary> /// <summary>
/// The folder that the patches will be stored in /// The folder that the patches will be stored in
/// </summary> /// </summary>
public static string PatchFolder = "Aki_Data\\Patcher"; public static string PatchFolder = "Aki_Patches";
private static string SevenZExe = "7za.exe";
/// <summary> /// <summary>
/// The path to the 7za.exe file in the <see cref="TempDir"/> /// The path to the 7za.exe file in the <see cref="TempDir"/>
@ -30,6 +31,13 @@ namespace PatcherUtils
/// </summary> /// </summary>
public static string PatcherClientPath = $"{TempDir}\\{PatcherClient}"; public static string PatcherClientPath = $"{TempDir}\\{PatcherClient}";
private static string XDelta3EXE = "xdelta3.exe";
/// <summary>
/// The path to the xdelta3.exe flie in the <see cref="TempDir"/>
/// </summary>
public static string XDelta3Path = $"{TempDir}\\{XDelta3EXE}";
/// <summary> /// <summary>
/// Streams embedded resources out of the assembly /// Streams embedded resources out of the assembly
/// </summary> /// </summary>
@ -73,6 +81,11 @@ namespace PatcherUtils
StreamResourceOut(resource, PatcherClientPath); StreamResourceOut(resource, PatcherClientPath);
break; break;
} }
case string a when a.EndsWith(XDelta3EXE):
{
StreamResourceOut(resource, XDelta3Path);
break;
}
} }
} }
} }

View File

@ -0,0 +1,267 @@
using Aki.Common.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace PatcherUtils
{
public class PatchHelper
{
private string SourceFolder = "";
private string TargetFolder = "";
private string DeltaFolder = "";
private int fileCountTotal;
private int filesProcessed;
private int deltaCount;
private int newCount;
private int delCount;
private List<LineItem> AdditionalInfo;
public event ProgressChangedHandler ProgressChanged;
protected virtual void RaiseProgressChanged(int progress, int total, string Message = "", params LineItem[] AdditionalLineItems)
{
int percent = (int)Math.Floor((double)progress / total * 100);
ProgressChanged?.Invoke(this, progress, total, percent, Message, AdditionalLineItems);
}
public PatchHelper(string SourceFolder, string TargetFolder, string DeltaFolder)
{
this.SourceFolder = SourceFolder;
this.TargetFolder = TargetFolder;
this.DeltaFolder = DeltaFolder;
}
private string GetDeltaPath(string sourceFile, string sourceFolder, string extension)
{
return Path.Join(DeltaFolder, $"{sourceFile.Replace(sourceFolder, "")}.{extension}");
}
private void ApplyDelta(string SourceFilePath, string DeltaFilePath)
{
string decodedPath = SourceFilePath + ".decoded";
Process.Start(new ProcessStartInfo
{
FileName = LazyOperations.XDelta3Path,
Arguments = $"-d -f -s \"{SourceFilePath}\" \"{DeltaFilePath}\" \"{decodedPath}\""
})
.WaitForExit();
if(File.Exists(decodedPath))
{
File.Delete(SourceFilePath);
File.Move(decodedPath, SourceFilePath);
}
}
private void CreateDelta(string SourceFilePath, string TargetFilePath)
{
FileInfo sourceFileInfo = new FileInfo(SourceFilePath);
string deltaPath = GetDeltaPath(SourceFilePath, SourceFolder, "delta");
Directory.CreateDirectory(deltaPath.Replace(sourceFileInfo.Name, ""));
//TODO - don't hardcode FileName
Process.Start(new ProcessStartInfo
{
FileName = LazyOperations.XDelta3Path,
Arguments = $"-0 -e -f -s \"{SourceFilePath}\" \"{TargetFilePath}\" \"{deltaPath}\""
})
.WaitForExit();
}
private void CreateDelFile(string SourceFile)
{
FileInfo sourceFileInfo = new FileInfo(SourceFile);
string deltaPath = GetDeltaPath(SourceFile, SourceFolder, "del");
Directory.CreateDirectory(deltaPath.Replace(sourceFileInfo.Name, ""));
File.Create(deltaPath);
}
private void CreateNewFile(string TargetFile)
{
FileInfo targetSourceInfo = new FileInfo(TargetFile);
string deltaPath = GetDeltaPath(TargetFile, TargetFolder, "new");
Directory.CreateDirectory(deltaPath.Replace(targetSourceInfo.Name, ""));
targetSourceInfo.CopyTo(deltaPath, true);
}
public bool GeneratePatches()
{
//get all directory information needed
DirectoryInfo sourceDir = new DirectoryInfo(SourceFolder);
DirectoryInfo targetDir = new DirectoryInfo(TargetFolder);
DirectoryInfo deltaDir = Directory.CreateDirectory(DeltaFolder);
//make sure all directories exist
if (!sourceDir.Exists || !targetDir.Exists || !deltaDir.Exists)
{
//One of the directories doesn't exist
return false;
}
List<FileInfo> SourceFiles = sourceDir.GetFiles("*", SearchOption.AllDirectories).ToList();
fileCountTotal = SourceFiles.Count;
AdditionalInfo.Clear();
AdditionalInfo.Add(new LineItem("Delta Patch", "0"));
AdditionalInfo.Add(new LineItem("New Patch", "0"));
AdditionalInfo.Add(new LineItem("Del Patch", "0"));
filesProcessed = 0;
RaiseProgressChanged(0, fileCountTotal, "Generating deltas...");
foreach (FileInfo targetFile in targetDir.GetFiles("*", SearchOption.AllDirectories))
{
//find a matching source file based on the relative path of the file
FileInfo sourceFile = SourceFiles.Find(f => f.FullName.Replace(sourceDir.FullName, "") == targetFile.FullName.Replace(targetDir.FullName, ""));
//if the target file doesn't exist in the source files, the target file needs to be added.
if (sourceFile == null)
{
CreateNewFile(targetFile.FullName);
newCount++;
RaiseProgressChanged(filesProcessed, fileCountTotal, targetFile.Name, AdditionalInfo.ToArray());
continue;
}
//if a matching source file was found, get the delta for it.
CreateDelta(sourceFile.FullName, targetFile.FullName);
SourceFiles.Remove(sourceFile);
deltaCount++;
RaiseProgressChanged(filesProcessed, fileCountTotal, targetFile.Name, AdditionalInfo.ToArray());
}
//Any remaining source files do not exist in the target folder and can be removed.
//reset progress info
RaiseProgressChanged(0, SourceFiles.Count, "Processing .del files...");
filesProcessed = 0;
fileCountTotal = SourceFiles.Count;
foreach (FileInfo delFile in SourceFiles)
{
CreateDelFile(delFile.FullName);
delCount++;
AdditionalInfo[2].ItemValue = delCount.ToString();
filesProcessed++;
RaiseProgressChanged(filesProcessed, fileCountTotal, "", AdditionalInfo.ToArray());
}
return true;
}
public string ApplyPatches()
{
//get needed directory information
DirectoryInfo sourceDir = new DirectoryInfo(SourceFolder);
DirectoryInfo deltaDir = new DirectoryInfo(DeltaFolder);
//check directories exist
if (!sourceDir.Exists || !deltaDir.Exists)
{
return "One of the supplied directories doesn't exist";
}
List<FileInfo> SourceFiles = sourceDir.GetFiles("*", SearchOption.AllDirectories).ToList();
List<FileInfo> deltaFiles = deltaDir.GetFiles("*", SearchOption.AllDirectories).ToList();
deltaCount = deltaFiles.Where(x => x.Extension == ".delta").Count();
newCount = deltaFiles.Where(x => x.Extension == ".new").Count();
delCount = deltaFiles.Where(x => x.Extension == ".del").Count();
AdditionalInfo = new List<LineItem>()
{
new LineItem("Patches Remaining", deltaCount.ToString()),
new LineItem("New Files to Add", newCount.ToString()),
new LineItem("Files to Delete", delCount.ToString())
};
filesProcessed = 0;
fileCountTotal = deltaFiles.Count;
foreach (FileInfo deltaFile in deltaDir.GetFiles("*", SearchOption.AllDirectories))
{
switch(deltaFile.Extension)
{
case ".delta":
{
//apply delta
FileInfo sourceFile = SourceFiles.Find(f => f.FullName.Replace(sourceDir.FullName, "") == deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".delta", ""));
if(sourceFile == null)
{
return $"Failed to find matching source file for '{deltaFile.FullName}'";
}
ApplyDelta(sourceFile.FullName, deltaFile.FullName);
deltaCount--;
break;
}
case ".new":
{
//copy new file
string destination = Path.Join(sourceDir.FullName, deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".new", ""));
File.Copy(deltaFile.FullName, destination);
newCount--;
break;
}
case ".del":
{
//remove unneeded file
string delFilePath = Path.Join(sourceDir.FullName, deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".del", ""));
File.Delete(delFilePath);
delCount--;
break;
}
}
AdditionalInfo[0].ItemValue = deltaCount.ToString();
AdditionalInfo[1].ItemValue = newCount.ToString();
AdditionalInfo[2].ItemValue = delCount.ToString();
++filesProcessed;
RaiseProgressChanged(filesProcessed, fileCountTotal, deltaFile.Name, AdditionalInfo.ToArray());
}
return "Patching Complete";
}
}
}

View File

@ -7,11 +7,13 @@
<ItemGroup> <ItemGroup>
<None Remove="Resources\7za.exe" /> <None Remove="Resources\7za.exe" />
<None Remove="Resources\PatchClient.exe" /> <None Remove="Resources\PatchClient.exe" />
<None Remove="Resources\xdelta3.exe" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Resources\7za.exe" /> <EmbeddedResource Include="Resources\7za.exe" />
<EmbeddedResource Include="Resources\PatchClient.exe" /> <EmbeddedResource Include="Resources\PatchClient.exe" />
<EmbeddedResource Include="Resources\xdelta3.exe" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Binary file not shown.