wabbajack/Wabbajack.Compiler/PatchCache/OctoDiff.cs

64 lines
2.3 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System.IO;
2022-05-27 05:41:11 +00:00
using System.Threading;
2021-09-27 12:42:46 +00:00
using Octodiff.Core;
using Octodiff.Diagnostics;
2022-05-27 05:41:11 +00:00
using Wabbajack.RateLimiter;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Compiler.PatchCache;
public class OctoDiff
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static void Create(byte[] oldData, byte[] newData, Stream output)
{
using var signature = CreateSignature(oldData);
using var oldStream = new MemoryStream(oldData);
using var newStream = new MemoryStream(newData);
var db = new DeltaBuilder();
db.BuildDelta(newStream, new SignatureReader(signature, new NullProgressReporter()),
new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(output)));
}
private static Stream CreateSignature(byte[] oldData)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
using var oldDataStream = new MemoryStream(oldData);
var sigStream = new MemoryStream();
var signatureBuilder = new SignatureBuilder();
signatureBuilder.Build(oldDataStream, new SignatureWriter(sigStream));
sigStream.Position = 0;
return sigStream;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
private static void CreateSignature(Stream oldData, Stream sigStream)
{
var signatureBuilder = new SignatureBuilder();
signatureBuilder.Build(oldData, new SignatureWriter(sigStream));
sigStream.Position = 0;
}
2021-09-27 12:42:46 +00:00
2022-05-27 05:41:11 +00:00
public static void Create(Stream oldData, Stream newData, Stream signature, Stream output, IJob? job)
2021-10-23 16:51:17 +00:00
{
CreateSignature(oldData, signature);
2022-05-27 05:41:11 +00:00
var db = new DeltaBuilder {ProgressReporter = new JobProgressReporter(job, 0)};
db.BuildDelta(newData, new SignatureReader(signature, new JobProgressReporter(job, 100)),
2021-10-23 16:51:17 +00:00
new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(output)));
2021-09-27 12:42:46 +00:00
}
2022-05-27 05:41:11 +00:00
private class JobProgressReporter : IProgressReporter
{
2022-05-29 04:19:25 +00:00
private readonly IJob? _job;
2022-05-27 05:41:11 +00:00
private readonly int _offset;
2022-05-29 04:19:25 +00:00
public JobProgressReporter(IJob? job, int offset)
2022-05-27 05:41:11 +00:00
{
_offset = offset;
_job = job;
}
public void ReportProgress(string operation, long currentPosition, long total)
{
2022-05-29 04:19:25 +00:00
if (_job == default) return;
2022-05-27 05:41:11 +00:00
var percent = Percent.FactoryPutInRange(currentPosition, total);
var toReport = (long) (percent.Value * 100) - (_job.Current - _offset);
_job.ReportNoWait((int) toReport);
}
}
2021-09-27 12:42:46 +00:00
}