2020-01-20 06:29:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2020-09-07 20:51:13 +00:00
|
|
|
|
public sealed class SCBlockCompare
|
2020-01-20 06:29:50 +00:00
|
|
|
|
{
|
2020-12-22 01:17:56 +00:00
|
|
|
|
private readonly List<string> AddedKeys = new();
|
|
|
|
|
private readonly List<string> RemovedKeys = new();
|
|
|
|
|
private readonly List<string> TypesChanged = new();
|
|
|
|
|
private readonly List<string> ValueChanged = new();
|
2020-01-20 06:29:50 +00:00
|
|
|
|
|
2020-05-26 22:58:48 +00:00
|
|
|
|
private readonly Dictionary<uint, string> KeyNames;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private string GetKeyName(uint key) => KeyNames.TryGetValue(key, out var value) ? value : $"{key:X8}";
|
2020-05-26 22:58:48 +00:00
|
|
|
|
|
2021-04-12 01:09:54 +00:00
|
|
|
|
public SCBlockCompare(SCBlockAccessor s1, SCBlockAccessor s2, IEnumerable<string> extraKeyNames)
|
2020-01-20 06:29:50 +00:00
|
|
|
|
{
|
2020-01-24 07:19:38 +00:00
|
|
|
|
var b1 = s1.BlockInfo;
|
|
|
|
|
var b2 = s2.BlockInfo;
|
2020-05-26 22:58:48 +00:00
|
|
|
|
KeyNames = GetKeyNames(s1, b1, b2);
|
2021-04-12 01:09:54 +00:00
|
|
|
|
SCBlockMetadata.AddExtraKeyNames(KeyNames, extraKeyNames);
|
2020-01-20 06:29:50 +00:00
|
|
|
|
|
|
|
|
|
var hs1 = new HashSet<uint>(b1.Select(z => z.Key));
|
|
|
|
|
var hs2 = new HashSet<uint>(b2.Select(z => z.Key));
|
|
|
|
|
|
2020-05-26 22:58:48 +00:00
|
|
|
|
LoadAddRemove(s1, s2, hs1, hs2);
|
|
|
|
|
hs1.IntersectWith(hs2);
|
|
|
|
|
LoadChanged(s1, s2, hs1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadAddRemove(SCBlockAccessor s1, SCBlockAccessor s2, ICollection<uint> hs1, IEnumerable<uint> hs2)
|
|
|
|
|
{
|
2020-01-20 06:29:50 +00:00
|
|
|
|
var unique = new HashSet<uint>(hs1);
|
|
|
|
|
unique.SymmetricExceptWith(hs2);
|
|
|
|
|
foreach (var k in unique)
|
|
|
|
|
{
|
2020-01-23 01:06:23 +00:00
|
|
|
|
var name = GetKeyName(k);
|
2020-01-20 06:29:50 +00:00
|
|
|
|
if (hs1.Contains(k))
|
|
|
|
|
{
|
2020-01-24 07:19:38 +00:00
|
|
|
|
var b = s1.GetBlock(k);
|
2020-01-23 01:06:23 +00:00
|
|
|
|
RemovedKeys.Add($"{name} - {b.Type}");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-24 07:19:38 +00:00
|
|
|
|
var b = s2.GetBlock(k);
|
2020-01-23 01:06:23 +00:00
|
|
|
|
AddedKeys.Add($"{name} - {b.Type}");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-26 22:58:48 +00:00
|
|
|
|
}
|
2020-01-20 06:29:50 +00:00
|
|
|
|
|
2020-05-26 22:58:48 +00:00
|
|
|
|
private void LoadChanged(SCBlockAccessor s1, SCBlockAccessor s2, IEnumerable<uint> shared)
|
|
|
|
|
{
|
|
|
|
|
foreach (var k in shared)
|
2020-01-20 06:29:50 +00:00
|
|
|
|
{
|
2020-01-24 07:19:38 +00:00
|
|
|
|
var x1 = s1.GetBlock(k);
|
|
|
|
|
var x2 = s2.GetBlock(k);
|
2020-01-23 01:06:23 +00:00
|
|
|
|
var name = GetKeyName(x1.Key);
|
2020-01-20 06:29:50 +00:00
|
|
|
|
if (x1.Type != x2.Type)
|
|
|
|
|
{
|
2020-01-23 01:06:23 +00:00
|
|
|
|
TypesChanged.Add($"{name} - {x1.Type} => {x2.Type}");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (x1.Data.Length != x2.Data.Length)
|
|
|
|
|
{
|
2020-01-23 01:06:23 +00:00
|
|
|
|
ValueChanged.Add($"{name} - Length: {x1.Data.Length} => {x2.Data.Length}");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x1.Data.Length == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-12-25 20:30:26 +00:00
|
|
|
|
if (x1.Type is SCTypeCode.Object or SCTypeCode.Array)
|
2020-01-20 06:29:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (!x1.Data.SequenceEqual(x2.Data))
|
2020-01-23 01:06:23 +00:00
|
|
|
|
ValueChanged.Add($"{name} - Bytes Changed");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var val1 = x1.GetValue();
|
|
|
|
|
var val2 = x2.GetValue();
|
|
|
|
|
if (Equals(val1, val2))
|
|
|
|
|
continue;
|
|
|
|
|
if (val1 is ulong u1 && val2 is ulong u2)
|
2020-01-23 01:06:23 +00:00
|
|
|
|
ValueChanged.Add($"{name} - {u1:X8} => {u2:x8}");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
else
|
2020-01-23 01:06:23 +00:00
|
|
|
|
ValueChanged.Add($"{name} - {val1} => {val2}");
|
2020-01-20 06:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 07:19:38 +00:00
|
|
|
|
private static Dictionary<uint, string> GetKeyNames(SCBlockAccessor s1, IEnumerable<SCBlock> b1, IEnumerable<SCBlock> b2)
|
|
|
|
|
{
|
|
|
|
|
var aType = s1.GetType();
|
|
|
|
|
var b1n = aType.GetAllPropertiesOfType<SaveBlock>(s1);
|
|
|
|
|
var names = aType.GetAllConstantsOfType<uint>();
|
|
|
|
|
Add(b1n, b1);
|
|
|
|
|
Add(b1n, b2);
|
|
|
|
|
|
|
|
|
|
void Add(Dictionary<SaveBlock, string> list, IEnumerable<SCBlock> blocks)
|
|
|
|
|
{
|
|
|
|
|
foreach (var b in blocks)
|
|
|
|
|
{
|
|
|
|
|
var match = list.FirstOrDefault(z => ReferenceEquals(z.Key.Data, b.Data));
|
|
|
|
|
if (match.Value != null && names.ContainsKey(b.Key))
|
|
|
|
|
names[b.Key] = match.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 06:29:50 +00:00
|
|
|
|
public IReadOnlyList<string> Summary()
|
|
|
|
|
{
|
|
|
|
|
var result = new List<string>();
|
|
|
|
|
AddIfPresent(result, AddedKeys, "Blocks Added:");
|
|
|
|
|
AddIfPresent(result, RemovedKeys, "Blocks Removed:");
|
|
|
|
|
AddIfPresent(result, TypesChanged, "BlockType Changed:");
|
|
|
|
|
AddIfPresent(result, ValueChanged, "Value Changed:");
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
2020-05-26 22:58:48 +00:00
|
|
|
|
static void AddIfPresent(List<string> result, ICollection<string> list, string hdr)
|
2020-01-20 06:29:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
result.Add(hdr);
|
|
|
|
|
result.AddRange(list);
|
|
|
|
|
result.Add(string.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|