From ba5cc26e83277e9bf31cd37ecaa370302c8d1a5a Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 22 Jan 2020 17:06:23 -0800 Subject: [PATCH] Add indication to which blocks changed Use reflection to grab all const declarations (private and public), and grab the name of that const. Whenever that block is mentioned in our diff, use the name instead of the key. Helps identify what changed / remove noise if you don't care about that block. --- .../Saves/Access/SaveBlockAccessorSWSH.cs | 8 ++++---- PKHeX.Core/Saves/MemeCrypto/SCBlockCompare.cs | 18 +++++++++++------- PKHeX.Core/Util/ReflectUtil.cs | 7 +++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/PKHeX.Core/Saves/Access/SaveBlockAccessorSWSH.cs b/PKHeX.Core/Saves/Access/SaveBlockAccessorSWSH.cs index c2dd56973..2d5970609 100644 --- a/PKHeX.Core/Saves/Access/SaveBlockAccessorSWSH.cs +++ b/PKHeX.Core/Saves/Access/SaveBlockAccessorSWSH.cs @@ -69,10 +69,10 @@ public SaveBlockAccessorSWSH(SAV8SWSH sav) public const uint KGameLanguage = 0x0BFDEBA1; // U32 Game Language public const uint KRepel = 0x9ec079da; // U16 Repel Steps remaining public const uint KRotoRally = 0x38548020; // U32 Roto Rally Score (99,999 cap) - public const int KBattleTowerSinglesVictory = 0x436CAF2B; // U32 Singles victories (9,999,999 cap) - public const int KBattleTowerDoublesVictory = 0x0D477836; // U32 Doubles victories (9,999,999 cap) - public const int KBattleTowerSinglesStreak = 0x6226F5AD; // U16 Singles Streak (255 cap) - public const int KBattleTowerDoublesStreak = 0x5F74FCEE; // U16 Doubles Streak (255 cap) + public const uint KBattleTowerSinglesVictory = 0x436CAF2B; // U32 Singles victories (9,999,999 cap) + public const uint KBattleTowerDoublesVictory = 0x0D477836; // U32 Doubles victories (9,999,999 cap) + public const uint KBattleTowerSinglesStreak = 0x6226F5AD; // U16 Singles Streak (255 cap) + public const uint KBattleTowerDoublesStreak = 0x5F74FCEE; // U16 Doubles Streak (255 cap) public object GetBlockValue(uint key) => GetBlock(key).GetValue(); public void SetBlockValue(uint key, object value) => GetBlock(key).SetValue(value); diff --git a/PKHeX.Core/Saves/MemeCrypto/SCBlockCompare.cs b/PKHeX.Core/Saves/MemeCrypto/SCBlockCompare.cs index bba337a33..025df00d3 100644 --- a/PKHeX.Core/Saves/MemeCrypto/SCBlockCompare.cs +++ b/PKHeX.Core/Saves/MemeCrypto/SCBlockCompare.cs @@ -14,6 +14,8 @@ public SCBlockCompare(SAV8SWSH s1, SAV8SWSH s2) { var b1 = s1.Blocks.BlockInfo; var b2 = s2.Blocks.BlockInfo; + var names = s1.Blocks.GetType().GetAllConstantsOfType(); + string GetKeyName(uint key) => names.TryGetValue(key, out var val) ? val : $"{key:X8}"; var hs1 = new HashSet(b1.Select(z => z.Key)); var hs2 = new HashSet(b2.Select(z => z.Key)); @@ -22,15 +24,16 @@ public SCBlockCompare(SAV8SWSH s1, SAV8SWSH s2) unique.SymmetricExceptWith(hs2); foreach (var k in unique) { + var name = GetKeyName(k); if (hs1.Contains(k)) { var b = s1.Blocks.GetBlock(k); - RemovedKeys.Add($"{b.Key:X8} - {b.Type}"); + RemovedKeys.Add($"{name} - {b.Type}"); } else { var b = s2.Blocks.GetBlock(k); - AddedKeys.Add($"{b.Key:X8} - {b.Type}"); + AddedKeys.Add($"{name} - {b.Type}"); } } @@ -39,14 +42,15 @@ public SCBlockCompare(SAV8SWSH s1, SAV8SWSH s2) { var x1 = s1.Blocks.GetBlock(k); var x2 = s2.Blocks.GetBlock(k); + var name = GetKeyName(x1.Key); if (x1.Type != x2.Type) { - TypesChanged.Add($"{x1.Key:X8} - {x1.Type} => {x2.Type}"); + TypesChanged.Add($"{name} - {x1.Type} => {x2.Type}"); continue; } if (x1.Data.Length != x2.Data.Length) { - ValueChanged.Add($"{x1.Key:X8} - Length: {x1.Data.Length} => {x2.Data.Length}"); + ValueChanged.Add($"{name} - Length: {x1.Data.Length} => {x2.Data.Length}"); continue; } @@ -56,7 +60,7 @@ public SCBlockCompare(SAV8SWSH s1, SAV8SWSH s2) if (x1.Type == SCTypeCode.Object || x1.Type == SCTypeCode.Array) { if (!x1.Data.SequenceEqual(x2.Data)) - ValueChanged.Add($"{x1.Key:X8} - Bytes Changed"); + ValueChanged.Add($"{name} - Bytes Changed"); continue; } @@ -65,9 +69,9 @@ public SCBlockCompare(SAV8SWSH s1, SAV8SWSH s2) if (Equals(val1, val2)) continue; if (val1 is ulong u1 && val2 is ulong u2) - ValueChanged.Add($"{x1.Key:X8} - {u1:X8} => {u2:x8}"); + ValueChanged.Add($"{name} - {u1:X8} => {u2:x8}"); else - ValueChanged.Add($"{x1.Key:X8} - {val1} => {val2}"); + ValueChanged.Add($"{name} - {val1} => {val2}"); } } diff --git a/PKHeX.Core/Util/ReflectUtil.cs b/PKHeX.Core/Util/ReflectUtil.cs index c89849ccc..71ae12307 100644 --- a/PKHeX.Core/Util/ReflectUtil.cs +++ b/PKHeX.Core/Util/ReflectUtil.cs @@ -124,5 +124,12 @@ private static IEnumerable GetAll(this TypeInfo typeInfo, Func accessor(typeInfo)); } + + public static Dictionary GetAllConstantsOfType(this Type type) + { + var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy); + var consts = fields.Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(T)); + return consts.ToDictionary(x => (T)x.GetRawConstantValue(), z => z.Name); + } } }