mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-10 12:47:27 +08:00
[+] Generate example config via attributes
This commit is contained in:
50
AquaMai/ConfigGenerator.cs
Normal file
50
AquaMai/ConfigGenerator.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using AquaMai.Attributes;
|
||||
using Tomlet;
|
||||
using Tomlet.Models;
|
||||
|
||||
namespace AquaMai;
|
||||
|
||||
public static class ConfigGenerator
|
||||
{
|
||||
public static void GenerateConfig()
|
||||
{
|
||||
var defaultConfig = new Config();
|
||||
foreach (var lang in (string[]) ["en", "zh"])
|
||||
{
|
||||
File.WriteAllText($"AquaMai.{lang}.toml", SerializeConfigWithComments(defaultConfig, lang));
|
||||
}
|
||||
}
|
||||
|
||||
public static string SerializeConfigWithComments(Config config, string lang)
|
||||
{
|
||||
var tomlDoc = TomletMain.DocumentFrom(config);
|
||||
MakeComments(tomlDoc, typeof(Config), lang);
|
||||
return tomlDoc.SerializedValue;
|
||||
}
|
||||
|
||||
private static void MakeComments(TomlTable table, Type configType, string lang)
|
||||
{
|
||||
foreach (var property in configType.GetProperties())
|
||||
{
|
||||
var value = table.GetValue(property.Name);
|
||||
var comment = property.GetCustomAttribute<ConfigCommentAttribute>();
|
||||
if (comment != null)
|
||||
{
|
||||
value.Comments.PrecedingComment = lang switch
|
||||
{
|
||||
"en" => comment.CommentEn,
|
||||
"zh" => comment.CommentZh,
|
||||
_ => throw new ArgumentException($"Unsupported language: {lang}")
|
||||
};
|
||||
}
|
||||
|
||||
if (value is TomlTable subTable)
|
||||
{
|
||||
MakeComments(subTable, property.PropertyType, lang);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user