-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom classes derived from BepInEx.Configuration.AcceptableValueBase will throw ArgumentNullException #88
Comments
Why not just use |
- Added `supportedTypes` Type array (containing the types of `AcceptableValueList` & `AcceptableValueRange`). - Added method `isTypeSupported` to check if a type is a subclass or an instance of a type in `supportedTypes` via `IsSubclassOfRawGeneric`. - Added `IsSubclassOfRawGeneric(Type generic, Type toCheck)` method which checks if `toCheck` is a subclass or an instance of `generic`.
That's an option that I also thought about, but the drawback is that I would have to copy (and translate) PS: I implemented the latter behaviour in fix-for-issue88 branch (in case needed). Please let me know if you want me to make a PR with these changes :) |
The fix doesn't work since it checks only for types inside the ConfigurationManager assembly. Most plugins will not reference the assembly and instead have their own copies. A more robust implementation of Edit: Why not rename the min value field in your class? |
Renaming the min value field in my class from Now, it's worth noting that, as you said, if any custom class wants to include a custom range via the Maybe the solution is then to enforce the mod devs via a message in the README.md file to use private void GetAcceptableValues(AcceptableValueBase values)
{
var t = values.GetType();
var listProp = t.GetProperty(nameof(AcceptableValueList<bool>.AcceptableValues), BindingFlags.Instance | BindingFlags.Public);
if (listProp != null)
{
AcceptableValues = ((IEnumerable)listProp.GetValue(values, null)).Cast<object>().ToArray();
}
else
{
var minProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MinValue), BindingFlags.Instance | BindingFlags.Public);
var maxProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MaxValue), BindingFlags.Instance | BindingFlags.Public);
if (minProp != null && maxProp != null)
{
AcceptableValueRange = new KeyValuePair<object, object>(minProp.GetValue(values, null), maxProp.GetValue(values, null));
ShowRangeAsPercent = (AcceptableValueRange.Key.Equals(0) || AcceptableValueRange.Key.Equals(1)) && AcceptableValueRange.Value.Equals(100) ||
AcceptableValueRange.Key.Equals(0f) && AcceptableValueRange.Value.Equals(1f);
}
}
} PS: For a better understanding I'll post my original custom class code here :) public class AcceptableValueMinimum<T> : AcceptableValueBase where T : IComparable
{
public virtual T MinValue { get; }
public AcceptableValueMinimum(T minValue) : base(typeof(T))
{
if (minValue == null)
{
throw new ArgumentNullException("minValue");
}
MinValue = minValue;
}
public override object Clamp(object value)
{
if (MinValue.CompareTo(value) > 0) return MinValue;
return value;
}
public override bool IsValid(object value)
{
return MinValue.CompareTo(value) <= 0;
}
public override string ToDescriptionString()
{
return $"# Acceptable value range: From {MinValue} to infinity and beyond";
}
} |
The throw could be removed as you've suggested. It'd be nice if you could make a PR for that. |
Sure! ^^ Edit: I've made the PR. I'll close this issue as completed. Thanks for your help! Have a nice day! :D |
I created a custom
AcceptedValueMinimum
class derived fromAcceptableValueBase
which has only a minimum value restriction. ConfigurationManager will then treat this custom class asAcceptableValueRange
, because it does not have the propertyAcceptableValues
present inAcceptableValueList
. This will always throw an ArgumentNullException because ConfigurationManager checks for the existence of bothMinValue
andMaxValue
properties in the custom class, which are not present in this case.PS: Thanks in advance for your help!
The text was updated successfully, but these errors were encountered: