[+] Add some interfaces for attributes

pull/91/head
Clansty 2024-11-26 00:03:35 +08:00
parent 0ec048ceba
commit 792dce6843
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
9 changed files with 38 additions and 9 deletions

View File

@ -0,0 +1,8 @@
namespace AquaMai.Config.Interfaces;
public interface IConfigComment
{
string CommentEn { get; init; }
string CommentZh { get; init; }
public string GetLocalized(string lang);
}

View File

@ -0,0 +1,7 @@
namespace AquaMai.Config.Interfaces;
public interface IConfigEntryAttribute
{
IConfigComment Comment { get; }
bool HideWhenDefault { get; }
}

View File

@ -0,0 +1,9 @@
namespace AquaMai.Config.Interfaces;
public interface IConfigSectionAttribute
{
IConfigComment Comment { get; }
bool ExampleHidden { get; }
bool DefaultOn { get; }
bool AlwaysEnabled { get; }
}

View File

@ -10,6 +10,7 @@ public interface IReflectionManager
public string Path { get; } public string Path { get; }
public string Name { get; } public string Name { get; }
public IReflectionField Field { get; } public IReflectionField Field { get; }
public IConfigEntryAttribute Attribute { get; init; }
} }
public interface ISection public interface ISection
@ -17,6 +18,7 @@ public interface IReflectionManager
public string Path { get; } public string Path { get; }
public IReflectionType Type { get; } public IReflectionType Type { get; }
public List<IEntry> Entries { get; } public List<IEntry> Entries { get; }
public IConfigSectionAttribute Attribute { get; init; }
} }
public IEnumerable<ISection> Sections { get; } public IEnumerable<ISection> Sections { get; }

View File

@ -1,8 +1,9 @@
using System; using System;
using AquaMai.Config.Interfaces;
namespace AquaMai.Config.Attributes; namespace AquaMai.Config.Attributes;
public record ConfigComment(string CommentEn, string CommentZh) public record ConfigComment(string CommentEn, string CommentZh) : IConfigComment
{ {
public string GetLocalized(string lang) => lang switch public string GetLocalized(string lang) => lang switch
{ {

View File

@ -1,4 +1,5 @@
using System; using System;
using AquaMai.Config.Interfaces;
namespace AquaMai.Config.Attributes; namespace AquaMai.Config.Attributes;
@ -16,9 +17,9 @@ public class ConfigEntryAttribute(
// Only use it to hide options that really won't be used. // Only use it to hide options that really won't be used.
bool hideWhenDefault = false, bool hideWhenDefault = false,
// NOTE: Use this argument to mark special config entries that need special handling. // NOTE: Use this argument to mark special config entries that need special handling.
SpecialConfigEntry specialConfigEntry = SpecialConfigEntry.None) : Attribute SpecialConfigEntry specialConfigEntry = SpecialConfigEntry.None) : Attribute, IConfigEntryAttribute
{ {
public ConfigComment Comment { get; } = new ConfigComment(en, zh); public IConfigComment Comment { get; } = new ConfigComment(en, zh);
public bool HideWhenDefault { get; } = hideWhenDefault; public bool HideWhenDefault { get; } = hideWhenDefault;
public SpecialConfigEntry SpecialConfigEntry { get; } = specialConfigEntry; public SpecialConfigEntry SpecialConfigEntry { get; } = specialConfigEntry;
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using AquaMai.Config.Interfaces;
namespace AquaMai.Config.Attributes; namespace AquaMai.Config.Attributes;
@ -12,9 +13,9 @@ public class ConfigSectionAttribute(
bool defaultOn = false, bool defaultOn = false,
// NOTE: You probably shouldn't use this. Only the "General" section is using this. // NOTE: You probably shouldn't use this. Only the "General" section is using this.
// Implies defaultOn = true. // Implies defaultOn = true.
bool alwaysEnabled = false) : Attribute bool alwaysEnabled = false) : Attribute, IConfigSectionAttribute
{ {
public ConfigComment Comment { get; } = new ConfigComment(en, zh); public IConfigComment Comment { get; } = new ConfigComment(en, zh);
public bool ExampleHidden { get; } = exampleHidden; public bool ExampleHidden { get; } = exampleHidden;
public bool DefaultOn { get; } = defaultOn || alwaysEnabled; public bool DefaultOn { get; } = defaultOn || alwaysEnabled;
public bool AlwaysEnabled { get; } = alwaysEnabled; public bool AlwaysEnabled { get; } = alwaysEnabled;

View File

@ -107,7 +107,7 @@ public class ConfigSerializer(IConfigSerializer.Options Options) : IConfigSerial
{ {
var entryState = config.GetEntryState(entry); var entryState = config.GetEntryState(entry);
AppendComment(sb, entry.Attribute.Comment); AppendComment(sb, entry.Attribute.Comment);
if (entry.Attribute.SpecialConfigEntry == SpecialConfigEntry.Locale && Options.OverrideLocaleValue) if (((ConfigEntryAttribute)entry.Attribute).SpecialConfigEntry == SpecialConfigEntry.Locale && Options.OverrideLocaleValue)
{ {
AppendEntry(sb, section.Path, entry.Name, Options.Lang); AppendEntry(sb, section.Path, entry.Name, Options.Lang);
} }
@ -151,7 +151,7 @@ public class ConfigSerializer(IConfigSerializer.Options Options) : IConfigSerial
} }
} }
private void AppendComment(StringBuilder sb, ConfigComment comment) private void AppendComment(StringBuilder sb, IConfigComment comment)
{ {
if (comment != null) if (comment != null)
{ {

View File

@ -14,14 +14,14 @@ public class ReflectionManager : IReflectionManager
public string Path { get; init; } public string Path { get; init; }
public string Name { get; init; } public string Name { get; init; }
public IReflectionField Field { get; init; } public IReflectionField Field { get; init; }
public ConfigEntryAttribute Attribute { get; init; } public IConfigEntryAttribute Attribute { get; init; }
} }
public record Section : IReflectionManager.ISection public record Section : IReflectionManager.ISection
{ {
public string Path { get; init; } public string Path { get; init; }
public IReflectionType Type { get; init; } public IReflectionType Type { get; init; }
public ConfigSectionAttribute Attribute { get; init; } public IConfigSectionAttribute Attribute { get; init; }
public List<Entry> entries; public List<Entry> entries;
public List<IReflectionManager.IEntry> Entries => entries.Cast<IReflectionManager.IEntry>().ToList(); public List<IReflectionManager.IEntry> Entries => entries.Cast<IReflectionManager.IEntry>().ToList();
} }