wabbajack/Wabbajack.Common/Patches.cs

183 lines
6.6 KiB
C#
Raw Normal View History

2020-06-02 03:41:34 +00:00
using System;
2021-01-05 22:09:32 +00:00
using System.Data.SQLite;
2020-06-02 03:41:34 +00:00
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Wabbajack.Common
{
public static class PatchCache
2020-06-02 03:41:34 +00:00
{
2021-01-05 22:09:32 +00:00
private static AbsolutePath DBLocation = Consts.LocalAppDataPath.Combine("GlobalPatchCache.sqlite");
private static string _connectionString;
private static SQLiteConnection _conn;
static PatchCache()
2020-06-02 03:41:34 +00:00
{
2021-01-05 22:09:32 +00:00
_connectionString = String.Intern($"URI=file:{DBLocation};Pooling=True;Max Pool Size=100;");
_conn = new SQLiteConnection(_connectionString);
_conn.Open();
2021-01-05 22:09:32 +00:00
using var cmd = new SQLiteCommand(_conn);
2021-01-05 22:09:32 +00:00
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS PatchCache (
FromHash BIGINT,
ToHash BIGINT,
PatchSize BLOB,
Patch BLOB,
PRIMARY KEY (FromHash, ToHash))";
cmd.ExecuteNonQuery();
2020-06-02 03:41:34 +00:00
}
2020-06-02 03:41:34 +00:00
public static async Task CreatePatchCached(byte[] a, byte[] b, Stream output)
{
await using var cmd = new SQLiteCommand(_conn);
2021-01-05 22:09:32 +00:00
cmd.CommandText = @"INSERT INTO PatchCache (FromHash, ToHash, PatchSize, Patch)
VALUES (@fromHash, @toHash, @patchSize, @patch)";
2020-06-02 03:41:34 +00:00
var dataA = a.xxHash();
var dataB = b.xxHash();
2021-01-05 22:09:32 +00:00
cmd.Parameters.AddWithValue("@fromHash", (long)dataA);
cmd.Parameters.AddWithValue("@toHash", (long)dataB);
2020-06-02 03:41:34 +00:00
await using var patch = new MemoryStream();
Utils.Status("Creating Patch");
2020-06-02 03:41:34 +00:00
OctoDiff.Create(a, b, patch);
patch.Position = 0;
2021-01-05 22:09:32 +00:00
cmd.Parameters.AddWithValue("@patchSize", patch.Length);
cmd.Parameters.AddWithValue("@patch", patch.ToArray());
2021-01-05 22:53:26 +00:00
try
{
await cmd.ExecuteNonQueryAsync();
}
catch (SQLiteException ex)
{
if (!ex.Message.StartsWith("constraint exception"))
throw;
}
2020-06-02 03:41:34 +00:00
await patch.CopyToAsync(output);
}
public static async Task<long> CreatePatchCached(Stream srcStream, Hash srcHash, Stream destStream, Hash destHash,
Stream? patchOutStream = null)
2020-06-02 03:41:34 +00:00
{
if (patchOutStream == null)
{
await using var rcmd = new SQLiteCommand(_conn);
rcmd.CommandText = "SELECT PatchSize FROM PatchCache WHERE FromHash = @fromHash AND ToHash = @toHash";
rcmd.Parameters.AddWithValue("@fromHash", (long)srcHash);
rcmd.Parameters.AddWithValue("@toHash", (long)destHash);
await using var rdr = await rcmd.ExecuteReaderAsync();
while (await rdr.ReadAsync())
{
return rdr.GetInt64(0);
}
}
else
{
if (TryGetPatch(srcHash, destHash, out var array))
{
await patchOutStream!.WriteAsync(array);
return array.Length;
}
}
await using var cmd = new SQLiteCommand(_conn);
2021-01-05 22:09:32 +00:00
cmd.CommandText = @"INSERT INTO PatchCache (FromHash, ToHash, PatchSize, Patch)
VALUES (@fromHash, @toHash, @patchSize, @patch)";
cmd.Parameters.AddWithValue("@fromHash", (long)srcHash);
cmd.Parameters.AddWithValue("@toHash", (long)destHash);
Utils.Status("Creating Patch");
2020-06-02 03:41:34 +00:00
await using var sigStream = new MemoryStream();
await using var patchStream = new MemoryStream();
OctoDiff.Create(srcStream, destStream, sigStream, patchStream);
2021-01-05 22:09:32 +00:00
cmd.Parameters.AddWithValue("@patchSize", patchStream.Length);
cmd.Parameters.AddWithValue("@patch", patchStream.ToArray());
2021-01-05 22:53:26 +00:00
try
{
await cmd.ExecuteNonQueryAsync();
}
catch (SQLiteException ex)
{
if (!ex.Message.StartsWith("constraint exception"))
throw;
}
2020-06-02 03:41:34 +00:00
if (patchOutStream == null) return patchStream.Position;
2020-06-02 03:41:34 +00:00
patchStream.Position = 0;
await patchStream.CopyToAsync(patchOutStream);
return patchStream.Position;
2020-06-02 03:41:34 +00:00
}
2021-01-05 22:09:32 +00:00
public static bool TryGetPatch(Hash fromHash, Hash toHash, [MaybeNullWhen(false)] out byte[] array)
2020-06-02 03:41:34 +00:00
{
using var cmd = new SQLiteCommand(_conn);
2021-01-05 22:09:32 +00:00
cmd.CommandText = @"SELECT PatchSize, Patch FROM PatchCache WHERE FromHash = @fromHash AND ToHash = @toHash";
cmd.Parameters.AddWithValue("@fromHash", (long)fromHash);
cmd.Parameters.AddWithValue("@toHash", (long)toHash);
2020-06-02 03:41:34 +00:00
2021-01-05 22:09:32 +00:00
using var rdr = cmd.ExecuteReader();
while (rdr.Read())
2020-06-02 03:41:34 +00:00
{
2021-01-05 22:09:32 +00:00
array = new byte[rdr.GetInt64(0)];
rdr.GetBytes(1, 0, array, 0, array.Length);
2020-06-02 03:41:34 +00:00
return true;
}
2021-01-05 22:09:32 +00:00
array = Array.Empty<byte>();
2020-06-02 03:41:34 +00:00
return false;
2021-01-05 22:09:32 +00:00
}
2020-06-02 03:41:34 +00:00
public static void ApplyPatch(Stream input, Func<Stream> openPatchStream, Stream output)
{
using var ps = openPatchStream();
using var br = new BinaryReader(ps);
var bytes = br.ReadBytes(8);
var str = Encoding.ASCII.GetString(bytes);
switch (str)
{
case "BSDIFF40":
BSDiff.Apply(input, openPatchStream, output);
return;
case "OCTODELT":
OctoDiff.Apply(input, openPatchStream, output);
return;
default:
throw new Exception($"No diff dispatch for: {str}");
}
}
}
// Convenience hook ins to offer the API from Utils, without having the init fire until they're actually called
public static partial class Utils
{
public static void ApplyPatch(Stream input, Func<Stream> openPatchStream, Stream output) =>
PatchCache.ApplyPatch(input, openPatchStream, output);
public static Task CreatePatchCached(byte[] a, byte[] b, Stream output) =>
PatchCache.CreatePatchCached(a, b, output);
public static Task<long> CreatePatchCached(Stream srcStream, Hash srcHash, Stream destStream, Hash destHash, Stream? patchOutStream = null) =>
PatchCache.CreatePatchCached(srcStream, srcHash, destStream, destHash, patchOutStream);
public static bool TryGetPatch(Hash foundHash, Hash fileHash, [MaybeNullWhen(false)] out byte[] ePatch) =>
PatchCache.TryGetPatch(foundHash, fileHash, out ePatch);
}
2020-06-02 03:41:34 +00:00
}