Changed properties to init instead of set to allow using object initializer but not changing the values later

This commit is contained in:
David Higueros committed 2026-06-20 00:52:07 -06:00
1 parent f9f7b7e667
commit 007c202058
2 files changed
+15 -12

No files matched your search

+6 -4
View File
@@ -20,14 +20,16 @@ public interface IModdedOptionList
IMiraPlugin? ParentMod { get; set; } IMiraPlugin? ParentMod { get; set; }
/// <summary> /// <summary>
/// Gets or sets the visibility function for the options. /// Gets the visibility function for the options.
/// Leaving it null will leave the options unchanged.
/// </summary> /// </summary>
Func<int, bool> Visible { get; set; } Func<int, bool>? Visible { get; init; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the options should be included with presets. /// Gets a value indicating whether the options should be included with presets.
/// Leaving it null will leave the options unchanged.
/// </summary> /// </summary>
bool IncludeInPreset { get; set; } bool? IncludeInPreset { get; init; }
/// <summary> /// <summary>
/// Saves the options to a preset configuration file. /// Saves the options to a preset configuration file.
@@ -38,12 +38,13 @@ public class ModdedOptionList<T> : IModdedOptionList, IReadOnlyList<T> where T :
public IReadOnlyList<T> Options { get; } public IReadOnlyList<T> Options { get; }
/// <inheritdoc /> /// <inheritdoc />
public Func<int, bool> Visible public Func<int, bool>? Visible
{ {
get; get;
set init
{ {
field = value; field = value;
if (value == null) return;
foreach (var (option, idx) in Options.Select((o, i) => (o, i))) foreach (var (option, idx) in Options.Select((o, i) => (o, i)))
{ {
option.Visible = () => value(idx); option.Visible = () => value(idx);
@@ -52,15 +53,16 @@ public class ModdedOptionList<T> : IModdedOptionList, IReadOnlyList<T> where T :
} }
/// <inheritdoc /> /// <inheritdoc />
public bool IncludeInPreset public bool? IncludeInPreset
{ {
get; get;
set init
{ {
field = value; field = value;
if (value == null) return;
foreach (var option in Options) foreach (var option in Options)
{ {
option.IncludeInPreset = value; option.IncludeInPreset = value.Value;
} }
} }
} }
@@ -71,12 +73,11 @@ public class ModdedOptionList<T> : IModdedOptionList, IReadOnlyList<T> where T :
/// <param name="count">The option list's length.</param> /// <param name="count">The option list's length.</param>
/// <param name="optionFactory">The option factory to instantiate the options from.</param> /// <param name="optionFactory">The option factory to instantiate the options from.</param>
/// <param name="includeInPreset">Whether to include the options in the preset.</param> /// <param name="includeInPreset">Whether to include the options in the preset.</param>
public ModdedOptionList(int count, Func<int, T> optionFactory, bool includeInPreset = true) public ModdedOptionList(int count, Func<int, T> optionFactory, bool? includeInPreset = true)
{ {
Count = count; Count = count;
Visible = _ => true;
IncludeInPreset = includeInPreset;
Options = Enumerable.Range(0, Count).Select(optionFactory).ToArray(); Options = Enumerable.Range(0, Count).Select(optionFactory).ToArray();
IncludeInPreset = includeInPreset;
} }
/// <inheritdoc/> /// <inheritdoc/>