From e1c8f52a07a3af95def48cd95ff47adfc34e2ee9 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 2 Sep 2023 15:53:51 -0700 Subject: [PATCH] Misc tweaks Set met dates for gen9 static encounters (oops) Check interface implementations for HasProperty --- .../Templates/Gen9/EncounterDist9.cs | 1 + .../Templates/Gen9/EncounterFixed9.cs | 1 + .../Templates/Gen9/EncounterMight9.cs | 1 + .../Templates/Gen9/EncounterStatic9.cs | 1 + .../Templates/Gen9/EncounterTera9.cs | 1 + PKHeX.Core/Util/ReflectUtil.cs | 42 ++++++++++++++++--- 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterDist9.cs b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterDist9.cs index d9deb54c4..c7d1a9b67 100644 --- a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterDist9.cs +++ b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterDist9.cs @@ -251,6 +251,7 @@ public sealed record EncounterDist9 OT_Friendship = pi.BaseFriendship, Met_Location = Location, Met_Level = LevelMin, + MetDate = EncounterDate.GetDateSwitch(), Version = (int)version, Ball = (byte)Ball.Poke, diff --git a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterFixed9.cs b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterFixed9.cs index dbeee1295..b74e54e87 100644 --- a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterFixed9.cs +++ b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterFixed9.cs @@ -89,6 +89,7 @@ public sealed record EncounterFixed9 OT_Friendship = pi.BaseFriendship, Met_Location = Location, Met_Level = LevelMin, + MetDate = EncounterDate.GetDateSwitch(), Version = (int)version, Ball = (byte)Ball.Poke, diff --git a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterMight9.cs b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterMight9.cs index 79b15d68a..c4b73cb33 100644 --- a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterMight9.cs +++ b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterMight9.cs @@ -266,6 +266,7 @@ public sealed record EncounterMight9 OT_Friendship = pi.BaseFriendship, Met_Location = Location, Met_Level = LevelMin, + MetDate = EncounterDate.GetDateSwitch(), Version = (int)version, Ball = (byte)Ball.Poke, diff --git a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterStatic9.cs b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterStatic9.cs index d8b964f01..123e48eac 100644 --- a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterStatic9.cs +++ b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterStatic9.cs @@ -62,6 +62,7 @@ public sealed record EncounterStatic9(GameVersion Version) OT_Friendship = pi.BaseFriendship, Met_Location = Location, Met_Level = LevelMin, + MetDate = EncounterDate.GetDateSwitch(), Version = (int)version, Ball = (byte)Ball.Poke, diff --git a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterTera9.cs b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterTera9.cs index b3a6e84db..c957f0051 100644 --- a/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterTera9.cs +++ b/PKHeX.Core/Legality/Encounters/Templates/Gen9/EncounterTera9.cs @@ -132,6 +132,7 @@ public sealed record EncounterTera9 OT_Friendship = pi.BaseFriendship, Met_Location = Location, Met_Level = LevelMin, + MetDate = EncounterDate.GetDateSwitch(), Version = (int)version, Ball = (byte)Ball.Poke, diff --git a/PKHeX.Core/Util/ReflectUtil.cs b/PKHeX.Core/Util/ReflectUtil.cs index 6cb32d4ba..77e310922 100644 --- a/PKHeX.Core/Util/ReflectUtil.cs +++ b/PKHeX.Core/Util/ReflectUtil.cs @@ -36,8 +36,21 @@ public static class ReflectUtil pi.SetValue(obj, c, null); } - public static object? GetValue(object obj, string name) => GetPropertyInfo(obj.GetType().GetTypeInfo(), name)?.GetValue(obj); - public static void SetValue(object obj, string name, object value) => GetPropertyInfo(obj.GetType().GetTypeInfo(), name)?.SetValue(obj, value, null); + public static object? GetValue(object obj, string name) + { + if (TryGetPropertyInfo(obj.GetType().GetTypeInfo(), name, out var pi)) + return pi.GetValue(obj, null); + return default; + } + public static bool SetValue(object obj, string name, object value) + { + if (!TryGetPropertyInfo(obj.GetType().GetTypeInfo(), name, out var pi)) + return false; + if (!pi.CanWrite) + return false; + pi.SetValue(obj, value); + return true; + } public static IEnumerable GetPropertiesStartWithPrefix(Type type, string prefix) { @@ -143,11 +156,28 @@ public static class ReflectUtil /// Name of the property. /// Reference to the property info for the object, if it exists. /// True if has property, and false if does not have property. is null when returning false. - public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null; - - public static PropertyInfo? GetPropertyInfo(this TypeInfo typeInfo, string name) + public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) { - return typeInfo.GetAllTypeInfo().Select(t => t.GetDeclaredProperty(name)).FirstOrDefault(pi => pi != null); + var type = obj.GetType(); + return TryGetPropertyInfo(type.GetTypeInfo(), name, out pi); + } + + public static bool TryGetPropertyInfo(this TypeInfo typeInfo, string name, [NotNullWhen(true)] out PropertyInfo? pi) + { + foreach (var t in typeInfo.GetAllTypeInfo()) + { + pi = t.GetDeclaredProperty(name); + if (pi != null) + return true; + foreach (var i in t.ImplementedInterfaces) + { + pi = i.GetTypeInfo().GetDeclaredProperty(name); + if (pi != null) + return true; + } + } + pi = null; + return false; } private static IEnumerable GetAll(this TypeInfo typeInfo, Func> accessor)