Add Apr2025 Randomizer

This commit is contained in:
2025-03-06 10:29:23 -06:00
parent 83d860ef79
commit 7604c78db8
15 changed files with 422 additions and 76 deletions

View File

@@ -1,38 +1,61 @@
using System;
namespace ALttPRandomizer.Settings {
namespace ALttPRandomizer.Settings {
using ALttPRandomizer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public class CommonSettingsProcessor {
public IList<string> GetSettings(SeedSettings settings) {
var args = new List<string>();
public IEnumerable<string> GetSettings(RandomizerInstance randomizer, SeedSettings settings) {
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 value = prop.GetValue(settings) ?? throw new SettingsLookupException("settings.{0} not found", prop.Name);
var valueFieldName = value.ToString() ?? throw new SettingsLookupException("settings.{0}.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);
?? throw new SettingsLookupException("Could not get field info for value {0}.{1}", prop.PropertyType, valueFieldName);
if (prop.GetCustomAttribute<NoSettingNameAttribute>() == null) {
var settingName = prop.GetCustomAttribute<SettingNameAttribute>()?.Name ?? prop.Name.ToLower();
var valueName = fi.GetCustomAttribute<SettingNameAttribute>()?.Name ?? valueFieldName.ToLower();
if (!prop.GetCustomAttributes<NoSettingNameAttribute>().Any(att => att.HasRandomizer(randomizer))) {
var settingName =
prop.GetCustomAttributes<SettingNameAttribute>()
.FirstOrDefault(att => att.HasRandomizer(randomizer))?.Name ?? prop.Name.ToLower();
var valueName =
fi.GetCustomAttributes<SettingNameAttribute>()
.FirstOrDefault(att => att.HasRandomizer(randomizer))?.Name ?? valueFieldName.ToLower();
args.Add(string.Format("--{0}={1}", settingName, valueName));
yield return string.Format("--{0}={1}", settingName, valueName);
}
foreach (var att in fi.GetCustomAttributes<AdditionalSettingAttribute>()) {
args.Add(att.Setting);
foreach (var att in fi.GetCustomAttributes<AdditionalSettingAttribute>().Where(att => att.HasRandomizer(randomizer))) {
yield return att.Setting;
}
}
}
return args;
public void ValidateSettings(RandomizerInstance randomizer, SeedSettings settings) {
var props = typeof(SeedSettings).GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var prop in props) {
var value = prop.GetValue(settings) ?? throw new SettingsLookupException("settings.{0} not found", prop.Name);
foreach (var att in prop.GetCustomAttributes<RequiredSettingAttribute>().Where(att => att.HasRandomizer(randomizer))) {
if (!att.Values.Contains(value)) {
throw new InvalidSettingsException("{0} contains value {1} not in required set [{2}]", prop.Name, value, string.Join(", ", att.Values));
}
}
foreach (var att in prop.GetCustomAttributes<ForbiddenSettingAttribute>().Where(att => att.HasRandomizer(randomizer))) {
if (att.Values.Contains(value)) {
throw new InvalidSettingsException("{0} contains forbidden value {1}", prop.Name, value);
}
}
}
}
}
public class SettingsLookupException : Exception {
public SettingsLookupException(string message, params object?[] args) : base(string.Format(message, args)) { }
}
public class InvalidSettingsException : Exception {
public InvalidSettingsException(string message, params object?[] args) : base(string.Format(message, args)) { }
}
}