[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:
Menci
2024-11-25 01:25:19 +08:00
committed by GitHub
parent e9ee31b22a
commit 37044dae01
217 changed files with 6051 additions and 3040 deletions

View File

@@ -0,0 +1,9 @@
using System;
namespace AquaMai.Config.Attributes;
// When The most inner namespace is the same name of the class, it should be collapsed.
// The class must be the only class in the namespace with a [ConfigSection] attribute.
[AttributeUsage(AttributeTargets.Class)]
public class ConfigCollapseNamespaceAttribute : Attribute
{}

View File

@@ -0,0 +1,13 @@
using System;
namespace AquaMai.Config.Attributes;
public record ConfigComment(string CommentEn, string CommentZh)
{
public string GetLocalized(string lang) => lang switch
{
"en" => CommentEn ?? "",
"zh" => CommentZh ?? "",
_ => throw new ArgumentException($"Unsupported language: {lang}")
};
}

View File

@@ -0,0 +1,24 @@
using System;
namespace AquaMai.Config.Attributes;
public enum SpecialConfigEntry
{
None,
Locale
}
[AttributeUsage(AttributeTargets.Field)]
public class ConfigEntryAttribute(
string en = null,
string zh = null,
// NOTE: Don't use this argument to hide any useful options.
// Only use it to hide options that really won't be used.
bool hideWhenDefault = false,
// NOTE: Use this argument to mark special config entries that need special handling.
SpecialConfigEntry specialConfigEntry = SpecialConfigEntry.None) : Attribute
{
public ConfigComment Comment { get; } = new ConfigComment(en, zh);
public bool HideWhenDefault { get; } = hideWhenDefault;
public SpecialConfigEntry SpecialConfigEntry { get; } = specialConfigEntry;
}

View File

@@ -0,0 +1,21 @@
using System;
namespace AquaMai.Config.Attributes;
[AttributeUsage(AttributeTargets.Class)]
public class ConfigSectionAttribute(
string en = null,
string zh = null,
// It will be hidden if the default value is preserved.
bool exampleHidden = false,
// A "Disabled = true" entry is required to disable the section.
bool defaultOn = false,
// NOTE: You probably shouldn't use this. Only the "General" section is using this.
// Implies defaultOn = true.
bool alwaysEnabled = false) : Attribute
{
public ConfigComment Comment { get; } = new ConfigComment(en, zh);
public bool ExampleHidden { get; } = exampleHidden;
public bool DefaultOn { get; } = defaultOn || alwaysEnabled;
public bool AlwaysEnabled { get; } = alwaysEnabled;
}

View File

@@ -0,0 +1,78 @@
using System;
namespace AquaMai.Config.Attributes;
public enum EnableConditionOperator
{
Equal,
NotEqual,
GreaterThan,
LessThan,
GreaterThanOrEqual,
LessThanOrEqual
}
public class EnableCondition(
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 EnableCondition(Type referenceType, string referenceMember)
: this(referenceType, referenceMember, EnableConditionOperator.Equal, true)
{ }
// Referencing a field in the same class and comparing it with a value.
public EnableCondition(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 EnableCondition(string referenceMember)
: this(referenceMember, EnableConditionOperator.Equal, true)
{ }
public bool Evaluate(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();
}
}
}