Misc tweaks

Set met dates for gen9 static encounters (oops)
Check interface implementations for HasProperty
This commit is contained in:
Kurt 2023-09-02 15:53:51 -07:00
parent 0f7e623842
commit e1c8f52a07
6 changed files with 41 additions and 6 deletions

View file

@ -251,6 +251,7 @@ public sealed record EncounterDist9
OT_Friendship = pi.BaseFriendship, OT_Friendship = pi.BaseFriendship,
Met_Location = Location, Met_Location = Location,
Met_Level = LevelMin, Met_Level = LevelMin,
MetDate = EncounterDate.GetDateSwitch(),
Version = (int)version, Version = (int)version,
Ball = (byte)Ball.Poke, Ball = (byte)Ball.Poke,

View file

@ -89,6 +89,7 @@ public sealed record EncounterFixed9
OT_Friendship = pi.BaseFriendship, OT_Friendship = pi.BaseFriendship,
Met_Location = Location, Met_Location = Location,
Met_Level = LevelMin, Met_Level = LevelMin,
MetDate = EncounterDate.GetDateSwitch(),
Version = (int)version, Version = (int)version,
Ball = (byte)Ball.Poke, Ball = (byte)Ball.Poke,

View file

@ -266,6 +266,7 @@ public sealed record EncounterMight9
OT_Friendship = pi.BaseFriendship, OT_Friendship = pi.BaseFriendship,
Met_Location = Location, Met_Location = Location,
Met_Level = LevelMin, Met_Level = LevelMin,
MetDate = EncounterDate.GetDateSwitch(),
Version = (int)version, Version = (int)version,
Ball = (byte)Ball.Poke, Ball = (byte)Ball.Poke,

View file

@ -62,6 +62,7 @@ public sealed record EncounterStatic9(GameVersion Version)
OT_Friendship = pi.BaseFriendship, OT_Friendship = pi.BaseFriendship,
Met_Location = Location, Met_Location = Location,
Met_Level = LevelMin, Met_Level = LevelMin,
MetDate = EncounterDate.GetDateSwitch(),
Version = (int)version, Version = (int)version,
Ball = (byte)Ball.Poke, Ball = (byte)Ball.Poke,

View file

@ -132,6 +132,7 @@ public sealed record EncounterTera9
OT_Friendship = pi.BaseFriendship, OT_Friendship = pi.BaseFriendship,
Met_Location = Location, Met_Location = Location,
Met_Level = LevelMin, Met_Level = LevelMin,
MetDate = EncounterDate.GetDateSwitch(),
Version = (int)version, Version = (int)version,
Ball = (byte)Ball.Poke, Ball = (byte)Ball.Poke,

View file

@ -36,8 +36,21 @@ public static class ReflectUtil
pi.SetValue(obj, c, null); pi.SetValue(obj, c, null);
} }
public static object? GetValue(object obj, string name) => GetPropertyInfo(obj.GetType().GetTypeInfo(), name)?.GetValue(obj); public static object? GetValue(object obj, string name)
public static void SetValue(object obj, string name, object value) => GetPropertyInfo(obj.GetType().GetTypeInfo(), name)?.SetValue(obj, value, null); {
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<string> GetPropertiesStartWithPrefix(Type type, string prefix) public static IEnumerable<string> GetPropertiesStartWithPrefix(Type type, string prefix)
{ {
@ -143,11 +156,28 @@ public static class ReflectUtil
/// <param name="name">Name of the property.</param> /// <param name="name">Name of the property.</param>
/// <param name="pi">Reference to the property info for the object, if it exists.</param> /// <param name="pi">Reference to the property info for the object, if it exists.</param>
/// <returns>True if has property, and false if does not have property. <see cref="pi"/> is null when returning false.</returns> /// <returns>True if has property, and false if does not have property. <see cref="pi"/> is null when returning false.</returns>
public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null; public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi)
public static PropertyInfo? GetPropertyInfo(this TypeInfo typeInfo, string name)
{ {
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<T> GetAll<T>(this TypeInfo typeInfo, Func<TypeInfo, IEnumerable<T>> accessor) private static IEnumerable<T> GetAll<T>(this TypeInfo typeInfo, Func<TypeInfo, IEnumerable<T>> accessor)