forked from Cookies_Github_mirror/AquaDX
[RF] AquaMai configuration refactor (#82)
更新了配置文件格式,原有的配置文件将被自动无缝迁移,详情请见新的配置文件中的注释(例外:`SlideJudgeTweak` 不再默认启用) 旧配置文件将被重命名备份,如果更新到此版本遇到 Bug 请联系我们 Updated configuration file schema. The old config file will be migrated automatically and seamlessly. See the comments in the new configuration file for details. (Except for `SlideJudgeTweak` is no longer enabled by default) Your old configuration file will be renamed as a backup. If you encounter any bug with this version, please contact us.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace AquaMai.Core.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
public class EnableGameVersionAttribute(uint minVersion = 0, uint maxVersion = 0, bool noWarn = false) : Attribute
|
||||
{
|
||||
public uint MinVersion { get; } = minVersion;
|
||||
public uint MaxVersion { get; } = maxVersion;
|
||||
public bool NoWarn { get; } = noWarn;
|
||||
|
||||
public bool ShouldEnable(uint gameVersion)
|
||||
{
|
||||
if (MinVersion > 0 && MinVersion > gameVersion) return false;
|
||||
if (MaxVersion > 0 && MaxVersion < gameVersion) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
79
AquaMai/AquaMai.Core/Attributes/EnableIfAttribute.cs
Normal file
79
AquaMai/AquaMai.Core/Attributes/EnableIfAttribute.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
|
||||
namespace AquaMai.Core.Attributes;
|
||||
|
||||
public enum EnableConditionOperator
|
||||
{
|
||||
Equal,
|
||||
NotEqual,
|
||||
GreaterThan,
|
||||
LessThan,
|
||||
GreaterThanOrEqual,
|
||||
LessThanOrEqual
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
public class EnableIfAttribute(
|
||||
Type referenceType,
|
||||
string referenceMember,
|
||||
EnableConditionOperator @operator,
|
||||
object rightSideValue) : Attribute
|
||||
{
|
||||
public Type ReferenceType { get; } = referenceType;
|
||||
public string ReferenceMember { get; } = referenceMember;
|
||||
public EnableConditionOperator Operator { get; } = @operator;
|
||||
public object RightSideValue { get; } = rightSideValue;
|
||||
|
||||
// Referencing a field in another class and checking if it's true.
|
||||
public EnableIfAttribute(Type referenceType, string referenceMember)
|
||||
: this(referenceType, referenceMember, EnableConditionOperator.Equal, true)
|
||||
{ }
|
||||
|
||||
// Referencing a field in the same class and comparing it with a value.
|
||||
public EnableIfAttribute(string referenceMember, EnableConditionOperator condition, object value)
|
||||
: this(null, referenceMember, condition, value)
|
||||
{ }
|
||||
|
||||
// Referencing a field in the same class and checking if it's true.
|
||||
public EnableIfAttribute(string referenceMember)
|
||||
: this(referenceMember, EnableConditionOperator.Equal, true)
|
||||
{ }
|
||||
|
||||
public bool ShouldEnable(Type selfType)
|
||||
{
|
||||
var referenceType = ReferenceType ?? selfType;
|
||||
var referenceField = referenceType.GetField(
|
||||
ReferenceMember,
|
||||
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
|
||||
var referenceProperty = referenceType.GetProperty(
|
||||
ReferenceMember,
|
||||
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
|
||||
if (referenceField == null && referenceProperty == null)
|
||||
{
|
||||
throw new ArgumentException($"Field or property {ReferenceMember} not found in {referenceType.FullName}");
|
||||
}
|
||||
var referenceMemberValue = referenceField != null ? referenceField.GetValue(null) : referenceProperty.GetValue(null);
|
||||
switch (Operator)
|
||||
{
|
||||
case EnableConditionOperator.Equal:
|
||||
return referenceMemberValue.Equals(RightSideValue);
|
||||
case EnableConditionOperator.NotEqual:
|
||||
return !referenceMemberValue.Equals(RightSideValue);
|
||||
case EnableConditionOperator.GreaterThan:
|
||||
case EnableConditionOperator.LessThan:
|
||||
case EnableConditionOperator.GreaterThanOrEqual:
|
||||
case EnableConditionOperator.LessThanOrEqual:
|
||||
var comparison = (IComparable)referenceMemberValue;
|
||||
return Operator switch
|
||||
{
|
||||
EnableConditionOperator.GreaterThan => comparison.CompareTo(RightSideValue) > 0,
|
||||
EnableConditionOperator.LessThan => comparison.CompareTo(RightSideValue) < 0,
|
||||
EnableConditionOperator.GreaterThanOrEqual => comparison.CompareTo(RightSideValue) >= 0,
|
||||
EnableConditionOperator.LessThanOrEqual => comparison.CompareTo(RightSideValue) <= 0,
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace AquaMai.Core.Attributes;
|
||||
|
||||
// If the field or property with this name is true, the patch will be implicitly enabled, regardless of the config state.
|
||||
// This is handled outside the config module, while The config state won't be actually set to enabled by it.
|
||||
// Won't bypass the restriction of [EnableIf()] and [EnableGameVersion()].
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class EnableImplicitlyIf(string memberName) : Attribute
|
||||
{
|
||||
public string MemberName { get; } = memberName;
|
||||
}
|
||||
Reference in New Issue
Block a user