refactor(core): name the advisor namespace Advice; record advisor decisions
The Advisor class shared its namespace's name, forcing an alias in every Titano.Core caller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
10 files changed
+84
-49
No files matched your search
@@ -0,0 +1,137 @@
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.Core.Advice;
|
||||
|
||||
/// <summary>How one advisor parameter reads and writes <see cref="ProjectSettings"/>.</summary>
|
||||
internal sealed class ParameterBinding
|
||||
{
|
||||
public required string Parameter { get; init; }
|
||||
public required AdvisorSection Section { get; init; }
|
||||
|
||||
/// <summary>A module on/off decision: <see cref="Advisor.Optimise"/> returns it to Auto when the advice differs.</summary>
|
||||
public bool IsModuleSwitch { get; init; }
|
||||
|
||||
/// <summary>The value in force (manual, else advised), null when neither exists.</summary>
|
||||
public required Func<ProjectSettings, object?> Effective { get; init; }
|
||||
|
||||
/// <summary>Auto fields: stores the advice and its reason, never touching a manual value.</summary>
|
||||
public Func<ProjectSettings, Recommendation, ProjectSettings>? Advise { get; init; }
|
||||
|
||||
/// <summary>Auto fields: drops the manual value so the advice applies.</summary>
|
||||
public Func<ProjectSettings, ProjectSettings>? ResetToAuto { get; init; }
|
||||
|
||||
/// <summary>Plain fields: the heavier decision <see cref="Advisor.Optimise"/> takes (each guards user choices).</summary>
|
||||
public Func<ProjectSettings, Recommendation, ProjectSettings>? Set { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>The binding of every <see cref="AdvisorParameters"/> id, in section order.</summary>
|
||||
internal static class ParameterBindings
|
||||
{
|
||||
public static IReadOnlyList<ParameterBinding> All { get; } =
|
||||
[
|
||||
new()
|
||||
{
|
||||
Parameter = AdvisorParameters.Hero, Section = AdvisorSection.Frames,
|
||||
Effective = s => s.HeroFrame,
|
||||
Set = (s, r) => s.HeroFrame is null ? s with { HeroFrame = (int)r.Value! } : s,
|
||||
},
|
||||
new()
|
||||
{
|
||||
Parameter = AdvisorParameters.Excluded, Section = AdvisorSection.Frames,
|
||||
Effective = s => s.ExcludedFrames,
|
||||
Set = (s, r) => s with { ExcludedFrames = [.. s.ExcludedFrames.Union((int[])r.Value!).Order()] },
|
||||
},
|
||||
AutoField(AdvisorParameters.TimingMode, AdvisorSection.Timing, true, s => s.Timing.Mode, (s, a) => s with { Timing = s.Timing with { Mode = a } }),
|
||||
AutoField(AdvisorParameters.FrameRate, AdvisorSection.Timing, false, s => s.Timing.FrameRate, (s, a) => s with { Timing = s.Timing with { FrameRate = a } }),
|
||||
new()
|
||||
{
|
||||
Parameter = AdvisorParameters.FitToSeconds, Section = AdvisorSection.Timing,
|
||||
Effective = s => s.Timing.FitToSeconds,
|
||||
Set = (s, r) => s with { Timing = s.Timing with { FitToSeconds = (double)r.Value! } },
|
||||
},
|
||||
AutoField(AdvisorParameters.Deflicker, AdvisorSection.Exposure, true, s => s.Exposure.Deflicker, (s, a) => s with { Exposure = s.Exposure with { Deflicker = a } }),
|
||||
AutoField(AdvisorParameters.DeflickerWindow, AdvisorSection.Exposure, false, s => s.Exposure.DeflickerWindow, (s, a) => s with { Exposure = s.Exposure with { DeflickerWindow = a } }),
|
||||
AutoField(AdvisorParameters.DeflickerStrength, AdvisorSection.Exposure, false, s => s.Exposure.DeflickerStrength, (s, a) => s with { Exposure = s.Exposure with { DeflickerStrength = a } }),
|
||||
AutoField(AdvisorParameters.Regions, AdvisorSection.Exposure, true, s => s.Exposure.Regions, (s, a) => s with { Exposure = s.Exposure with { Regions = a } }),
|
||||
AutoField(AdvisorParameters.HolyGrail, AdvisorSection.Exposure, true, s => s.Exposure.HolyGrail, (s, a) => s with { Exposure = s.Exposure with { HolyGrail = a } }),
|
||||
AutoField(AdvisorParameters.TransitionLength, AdvisorSection.Exposure, false, s => s.Exposure.TransitionLength, (s, a) => s with { Exposure = s.Exposure with { TransitionLength = a } }),
|
||||
AutoField(AdvisorParameters.WhiteBalanceSmoothing, AdvisorSection.Exposure, true, s => s.Exposure.WhiteBalanceSmoothing, (s, a) => s with { Exposure = s.Exposure with { WhiteBalanceSmoothing = a } }),
|
||||
AutoField(AdvisorParameters.MotionBlur, AdvisorSection.Motion, true, s => s.Motion.MotionBlur, (s, a) => s with { Motion = s.Motion with { MotionBlur = a } }),
|
||||
AutoField(AdvisorParameters.TargetShutterAngle, AdvisorSection.Motion, false, s => s.Motion.TargetShutterAngle, (s, a) => s with { Motion = s.Motion with { TargetShutterAngle = a } }),
|
||||
AutoField(AdvisorParameters.FlowQuality, AdvisorSection.Motion, false, s => s.Motion.FlowQuality, (s, a) => s with { Motion = s.Motion with { FlowQuality = a } }),
|
||||
AutoField(AdvisorParameters.Stabilize, AdvisorSection.Motion, true, s => s.Motion.Stabilize, (s, a) => s with { Motion = s.Motion with { Stabilize = a } }),
|
||||
AutoField(AdvisorParameters.StabilizeSmoothing, AdvisorSection.Motion, false, s => s.Motion.StabilizeSmoothing, (s, a) => s with { Motion = s.Motion with { StabilizeSmoothing = a } }),
|
||||
AutoField(AdvisorParameters.Aspect, AdvisorSection.Camera, false, s => s.Camera.Aspect, (s, a) => s with { Camera = s.Camera with { Aspect = a } }),
|
||||
new()
|
||||
{
|
||||
Parameter = AdvisorParameters.OffsetY, Section = AdvisorSection.Camera,
|
||||
Effective = s => s.Camera.OffsetY,
|
||||
Set = (s, r) => s.Camera.OffsetY == 0 ? s with { Camera = s.Camera with { OffsetY = (double)r.Value! } } : s,
|
||||
},
|
||||
AutoField(AdvisorParameters.ExportPreset, AdvisorSection.Export, true, s => s.Export.Preset, (s, a) => s with { Export = s.Export with { Preset = a } }),
|
||||
new()
|
||||
{
|
||||
Parameter = AdvisorParameters.Resolution, Section = AdvisorSection.Export,
|
||||
Effective = s => s.Export.Overrides is { Width: { } w, Height: { } h } ? Size(w, h) : null,
|
||||
Set = CapResolution,
|
||||
},
|
||||
];
|
||||
|
||||
public static ParameterBinding? Find(string parameter) => All.FirstOrDefault(b => b.Parameter == parameter);
|
||||
|
||||
/// <summary>Value equality that compares lists element by element.</summary>
|
||||
public static bool SameValue(object? a, object? b)
|
||||
{
|
||||
if (a is IEnumerable x && b is IEnumerable y && a is not string && b is not string) return x.Cast<object>().SequenceEqual(y.Cast<object>());
|
||||
return Equals(a, b);
|
||||
}
|
||||
|
||||
/// <summary>Recommendation arguments as stored in <see cref="Auto{T}.ReasonArgs"/> (culture-neutral text).</summary>
|
||||
public static string[] Strings(IReadOnlyList<object> args) => [.. args.Select(a => Convert.ToString(a, CultureInfo.InvariantCulture) ?? string.Empty)];
|
||||
|
||||
public static string Size(int width, int height) => string.Create(CultureInfo.InvariantCulture, $"{width}x{height}");
|
||||
|
||||
public static bool HasAdvice<T>(Auto<T> auto) => typeof(T).IsValueType
|
||||
? auto.ReasonKey is not null && !auto.ReasonKey.StartsWith(AdvisorKeys.MissingPrefix, StringComparison.Ordinal)
|
||||
: auto.AutoValue is not null;
|
||||
|
||||
private static ParameterBinding AutoField<T>(string parameter, AdvisorSection section, bool module, Func<ProjectSettings, Auto<T>> get, Func<ProjectSettings, Auto<T>, ProjectSettings> set) => new()
|
||||
{
|
||||
Parameter = parameter,
|
||||
Section = section,
|
||||
IsModuleSwitch = module,
|
||||
Effective = s => EffectiveOf(get(s)),
|
||||
Advise = (s, r) => set(s, Advise(get(s), r)),
|
||||
ResetToAuto = s => set(s, get(s).Reset()),
|
||||
};
|
||||
|
||||
private static object? EffectiveOf<T>(Auto<T> auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : HasAdvice(auto) ? auto.AutoValue : null;
|
||||
|
||||
/// <summary>
|
||||
/// Stores a value with its reason. Missing data changes no value: the missing-data reason is stored only when the
|
||||
/// parameter has no advice yet, so the Auto tooltip can say why it is empty.
|
||||
/// </summary>
|
||||
private static Auto<T> Advise<T>(Auto<T> auto, Recommendation recommendation)
|
||||
{
|
||||
var args = Strings(recommendation.ReasonArgs);
|
||||
if (recommendation.Value is null)
|
||||
return HasAdvice(auto) ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args };
|
||||
if (recommendation.Value is not T value)
|
||||
throw new InvalidOperationException($"{recommendation.Parameter} expects {typeof(T).Name}, got {recommendation.Value.GetType().Name}.");
|
||||
return auto.WithAdvice(value, recommendation.ReasonKey, args);
|
||||
}
|
||||
|
||||
/// <summary>Overrides the output size when the preset in force is larger than the recommended cap.</summary>
|
||||
private static ProjectSettings CapResolution(ProjectSettings settings, Recommendation recommendation)
|
||||
{
|
||||
var parts = ((string)recommendation.Value!).Split('x');
|
||||
var width = int.Parse(parts[0], CultureInfo.InvariantCulture);
|
||||
var height = int.Parse(parts[1], CultureInfo.InvariantCulture);
|
||||
var id = EffectiveOf(settings.Export.Preset) as string;
|
||||
if (id is null || ExportPresets.Find(id) is not { Width: > 0 } preset || (preset.Width <= width && preset.Height <= height)) return settings;
|
||||
return settings with { Export = settings.Export with { Overrides = settings.Export.Overrides with { Width = width, Height = height } } };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user