Generalize settings annotations
This commit is contained in:
@@ -1,45 +1,38 @@
|
||||
namespace ALttPRandomizer.Settings {
|
||||
using System;
|
||||
|
||||
namespace ALttPRandomizer.Settings {
|
||||
using ALttPRandomizer.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
public class CommonSettingsProcessor {
|
||||
public KeyValuePair<string, string> GetSettingPair<T>(T value) where T : Enum {
|
||||
var name = this.GetValueName(value);
|
||||
public IList<string> GetSettings(SeedSettings settings) {
|
||||
var args = new List<string>();
|
||||
|
||||
Type type = typeof(T);
|
||||
var props = typeof(SeedSettings).GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
||||
foreach (var prop in props) {
|
||||
var value = prop.GetValue(settings) ?? throw new SettingsLookupException("settings.{} not found", prop.Name);
|
||||
var valueFieldName = value.ToString() ?? throw new SettingsLookupException("settings.{}.ToString() returned null", prop.Name);
|
||||
var fi = prop.PropertyType.GetField(valueFieldName, BindingFlags.Static | BindingFlags.Public)
|
||||
?? throw new SettingsLookupException("Could not get field info for value {}.{}", prop.PropertyType, valueFieldName);
|
||||
|
||||
var settingName = type.GetCustomAttribute<CommonValueAttribute>()?.Name;
|
||||
if (settingName == null) {
|
||||
settingName = type.Name.ToLower();
|
||||
if (prop.GetCustomAttribute<NoSettingNameAttribute>() == null) {
|
||||
var settingName = prop.GetCustomAttribute<SettingNameAttribute>()?.Name ?? prop.Name.ToLower();
|
||||
var valueName = fi.GetCustomAttribute<SettingNameAttribute>()?.Name ?? valueFieldName.ToLower();
|
||||
|
||||
args.Add(string.Format("--{0}={1}", settingName, valueName));
|
||||
}
|
||||
|
||||
foreach (var att in fi.GetCustomAttributes<AdditionalSettingAttribute>()) {
|
||||
args.Add(att.Setting);
|
||||
}
|
||||
}
|
||||
|
||||
return new(settingName, name);
|
||||
}
|
||||
|
||||
public KeyValuePair<string, string> GetSettingPair<T>(string fieldName, T value) where T : Enum {
|
||||
var name = this.GetValueName(value);
|
||||
|
||||
var fi = typeof(SeedSettings).GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public);
|
||||
|
||||
var settingName = fi?.GetCustomAttribute<CommonValueAttribute>()?.Name ?? fieldName.ToLower();
|
||||
|
||||
return new(settingName, name);
|
||||
}
|
||||
|
||||
private string GetValueName<T>(T value) where T : Enum {
|
||||
Type type = typeof(T);
|
||||
|
||||
var fi = type.GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);
|
||||
|
||||
var name = fi?.GetCustomAttribute<CommonValueAttribute>()?.Name;
|
||||
|
||||
if (name != null) {
|
||||
return name;
|
||||
} else {
|
||||
return value.ToString().ToLower();
|
||||
}
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
public class SettingsLookupException : Exception {
|
||||
public SettingsLookupException(string message, params object?[] args) : base(string.Format(message, args)) { }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user