2016-07-16 17:00:50 +00:00
|
|
|
|
using System;
|
2016-07-16 23:13:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-17 08:05:07 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2016-08-11 02:29:21 +00:00
|
|
|
|
using System.Globalization;
|
2016-07-16 23:13:33 +00:00
|
|
|
|
using System.Linq;
|
2016-07-16 17:00:50 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2017-03-03 05:00:41 +00:00
|
|
|
|
public static class ReflectUtil
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2018-05-18 05:43:07 +00:00
|
|
|
|
public static bool IsValueEqual(this PropertyInfo pi, object obj, object value)
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
|
|
|
|
var v = pi.GetValue(obj, null);
|
2016-08-10 18:43:00 +00:00
|
|
|
|
var c = ConvertValue(value, pi.PropertyType);
|
2016-07-16 17:00:50 +00:00
|
|
|
|
return v.Equals(c);
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-05-18 05:43:07 +00:00
|
|
|
|
public static void SetValue(PropertyInfo pi, object obj, object value)
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2018-05-18 05:43:07 +00:00
|
|
|
|
var c = ConvertValue(value, pi.PropertyType);
|
|
|
|
|
pi.SetValue(obj, c, null);
|
2016-07-16 23:13:33 +00:00
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
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);
|
2017-05-12 04:34:18 +00:00
|
|
|
|
public static object GetValue(Type t, string propertyName) => t.GetTypeInfo().GetDeclaredProperty(propertyName).GetValue(null);
|
|
|
|
|
public static void SetValue(Type t, string propertyName, object value) => t.GetTypeInfo().GetDeclaredProperty(propertyName).SetValue(null, value);
|
2017-03-22 04:40:33 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesStartWithPrefix(Type type, string prefix)
|
2016-07-16 23:13:33 +00:00
|
|
|
|
{
|
2018-05-18 05:43:07 +00:00
|
|
|
|
return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties)
|
2017-01-03 06:17:47 +00:00
|
|
|
|
.Where(p => p.Name.StartsWith(prefix, StringComparison.Ordinal))
|
2018-05-20 14:46:59 +00:00
|
|
|
|
.Select(p => p.Name)
|
|
|
|
|
.Distinct()
|
|
|
|
|
;
|
2016-07-16 23:13:33 +00:00
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublic(Type type)
|
2016-07-18 00:06:50 +00:00
|
|
|
|
{
|
2018-05-20 14:46:59 +00:00
|
|
|
|
return GetAllPropertyInfoCanWritePublic(type).Select(p => p.Name)
|
|
|
|
|
.Distinct()
|
|
|
|
|
;
|
2016-07-23 08:04:55 +00:00
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-05-18 05:43:07 +00:00
|
|
|
|
public static IEnumerable<PropertyInfo> GetAllPropertyInfoCanWritePublic(Type type)
|
2016-07-23 08:04:55 +00:00
|
|
|
|
{
|
2018-05-18 05:43:07 +00:00
|
|
|
|
return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties)
|
|
|
|
|
.Where(p => p.CanWrite && p.SetMethod.IsPublic);
|
2016-09-30 22:12:23 +00:00
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-06-14 01:52:09 +00:00
|
|
|
|
public static IEnumerable<PropertyInfo> GetAllPropertyInfoPublic(Type type)
|
|
|
|
|
{
|
|
|
|
|
return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties)
|
|
|
|
|
.Where(p => (p.CanRead && p.GetMethod.IsPublic) || (p.CanWrite && p.SetMethod.IsPublic));
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-06-14 01:52:09 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesPublic(Type type)
|
|
|
|
|
{
|
|
|
|
|
return GetAllPropertyInfoPublic(type).Select(p => p.Name)
|
|
|
|
|
.Distinct()
|
|
|
|
|
;
|
|
|
|
|
}
|
2018-05-18 05:43:07 +00:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublicDeclared(Type type)
|
2016-09-30 22:12:23 +00:00
|
|
|
|
{
|
2018-05-18 05:43:07 +00:00
|
|
|
|
return type.GetTypeInfo().GetAllProperties()
|
|
|
|
|
.Where(p => p.CanWrite && p.SetMethod.IsPublic)
|
2018-05-20 14:46:59 +00:00
|
|
|
|
.Select(p => p.Name)
|
|
|
|
|
.Distinct()
|
|
|
|
|
;
|
2016-07-18 00:06:50 +00:00
|
|
|
|
}
|
2016-08-10 18:43:00 +00:00
|
|
|
|
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
private static object? ConvertValue(object value, Type type)
|
2016-08-10 18:43:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (type == typeof(DateTime?)) // Used for PKM.MetDate and other similar properties
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return DateTime.TryParseExact(value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateValue)
|
|
|
|
|
? new DateTime?(dateValue)
|
2016-08-11 02:29:21 +00:00
|
|
|
|
: null;
|
2016-08-10 18:43:00 +00:00
|
|
|
|
}
|
2016-08-11 02:29:21 +00:00
|
|
|
|
|
|
|
|
|
// Convert.ChangeType is suitable for most things
|
|
|
|
|
return Convert.ChangeType(value, type);
|
2016-08-10 18:43:00 +00:00
|
|
|
|
}
|
2018-05-18 05:43:07 +00:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<ConstructorInfo> GetAllConstructors(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredConstructors);
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<EventInfo> GetAllEvents(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredEvents);
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<FieldInfo> GetAllFields(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredFields);
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<MemberInfo> GetAllMembers(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredMembers);
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<MethodInfo> GetAllMethods(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredMethods);
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<TypeInfo> GetAllNestedTypes(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredNestedTypes);
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<PropertyInfo> GetAllProperties(this TypeInfo typeInfo)
|
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredProperties);
|
|
|
|
|
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
public static IEnumerable<TypeInfo> GetAllTypeInfo(this TypeInfo? typeInfo)
|
2018-05-18 05:43:07 +00:00
|
|
|
|
{
|
|
|
|
|
while (typeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
yield return typeInfo;
|
|
|
|
|
typeInfo = typeInfo.BaseType?.GetTypeInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2020-09-19 05:11:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="obj"/> has the requested property <see cref="name"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="obj">Object to check for property existence.</param>
|
|
|
|
|
/// <param name="name">Name of the property.</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>
|
2021-01-17 08:05:07 +00:00
|
|
|
|
public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null;
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
public static PropertyInfo? GetPropertyInfo(this TypeInfo typeInfo, string name)
|
2018-05-18 05:43:07 +00:00
|
|
|
|
{
|
|
|
|
|
return typeInfo.GetAllTypeInfo().Select(t => t.GetDeclaredProperty(name)).FirstOrDefault(pi => pi != null);
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-05-18 05:43:07 +00:00
|
|
|
|
private static IEnumerable<T> GetAll<T>(this TypeInfo typeInfo, Func<TypeInfo, IEnumerable<T>> accessor)
|
2016-10-23 19:48:49 +00:00
|
|
|
|
{
|
2018-05-27 22:57:28 +00:00
|
|
|
|
return GetAllTypeInfo(typeInfo).SelectMany(_ => accessor(typeInfo));
|
2016-10-23 19:48:49 +00:00
|
|
|
|
}
|
2020-01-23 01:06:23 +00:00
|
|
|
|
|
2020-09-06 18:24:54 +00:00
|
|
|
|
public static Dictionary<T, string> GetAllConstantsOfType<T>(this Type type) where T : struct
|
2020-01-23 01:06:23 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-01-24 04:11:39 +00:00
|
|
|
|
|
2020-09-06 18:24:54 +00:00
|
|
|
|
public static Dictionary<T, string> GetAllPropertiesOfType<T>(this Type type, object obj) where T : class
|
2020-01-24 04:11:39 +00:00
|
|
|
|
{
|
|
|
|
|
var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
|
|
|
|
var ofType = props.Where(fi => typeof(T).IsAssignableFrom(fi.PropertyType));
|
|
|
|
|
return ofType.ToDictionary(x => (T)x.GetValue(obj), z => z.Name);
|
|
|
|
|
}
|
2016-07-16 17:00:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|