Skip to content

Commit

Permalink
Add priorities for config sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Aug 26, 2024
1 parent 7cdb58d commit ecc03e7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions MonkeyLoader/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class Config : INestedIdentifiable<IConfigOwner>,

private readonly JObject _loadedConfig;

private readonly HashSet<ConfigSection> _sections = new();
private readonly HashSet<ConfigSection> _sections = [];

/// <summary>
/// Gets the config keys defined in this configuration.
Expand Down Expand Up @@ -62,7 +62,7 @@ public sealed class Config : INestedIdentifiable<IConfigOwner>,
/// <summary>
/// Gets all loaded sections of this config.
/// </summary>
public IEnumerable<ConfigSection> Sections => _sections.AsSafeEnumerable();
public IEnumerable<ConfigSection> Sections => _sections.OrderByDescending(section => section.Priority).ThenBy(section => section.Name);

/// <summary>
/// Gets or sets a configuration value for the given key,
Expand Down
10 changes: 8 additions & 2 deletions MonkeyLoader/Configuration/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace MonkeyLoader.Configuration
/// <remarks>
/// Use your mod's <see cref="Configuration.Config"/> instance to <see cref="Config.LoadSection{TSection}()">load sections</see>.
/// </remarks>
public abstract class ConfigSection : INestedIdentifiable<Config>, IIdentifiableOwner<ConfigSection, IDefiningConfigKey>
public abstract class ConfigSection : INestedIdentifiable<Config>, IIdentifiableOwner<ConfigSection, IDefiningConfigKey>, IPrioritizable
{
/// <summary>
/// Stores the <see cref="IDefiningConfigKey"/>s tracked by this section.
/// </summary>
protected readonly HashSet<IDefiningConfigKey> keys = new();
protected readonly HashSet<IDefiningConfigKey> keys = [];

private readonly Lazy<string> _fullId;

Expand Down Expand Up @@ -84,6 +84,12 @@ public abstract class ConfigSection : INestedIdentifiable<Config>, IIdentifiable

Config INestedIdentifiable<Config>.Parent => Config;

/// <remarks>
/// Controls in which order the sections of a <see cref="Configuration.Config"/> are listed.
/// </remarks>
/// <inheritdoc/>
public virtual int Priority => 0;

/// <summary>
/// Gets whether this config section is allowed to be saved.<br/>
/// This can be <c>false</c> if something went wrong while loading it.
Expand Down
3 changes: 3 additions & 0 deletions MonkeyLoader/Logging/LoggingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ internal set

public LoggingLevel Level => LevelKey;

/// <inheritdoc/>
public override int Priority => 30;

public bool ShouldCleanLogDirectory => ShouldWriteLogFile && FilesToPreserve > 0;

[MemberNotNullWhen(true, nameof(DirectoryPath), nameof(CurrentLogFilePath))]
Expand Down
3 changes: 3 additions & 0 deletions MonkeyLoader/Meta/LocationConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public string PatchedAssemblies
set => PatchedAssembliesKey.SetValue(value, SetEventLabel);
}

/// <inheritdoc/>
public override int Priority => 20;

/// <inheritdoc/>
public override Version Version { get; } = new Version(1, 0);

Expand Down
9 changes: 6 additions & 3 deletions MonkeyLoader/NuGet/NuGetConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public sealed class NuGetConfigSection : ConfigSection
/// <inheritdoc/>
public override string Description { get; } = "Contains definitions for how to use which NuGet feeds.";

/// <inheritdoc/>
public override string Id { get; } = "NuGet";

/// <summary>
/// Gets whether checking NuGet feeds to load mod's library dependencies is enabled.
/// </summary>
Expand All @@ -37,9 +40,6 @@ public bool LoadingModsEnabled
set => Config.SetValue(EnableLoadingModsKey, value);
}

/// <inheritdoc/>
public override string Id { get; } = "NuGet";

/// <summary>
/// Gets the NuGet feeds to check for game packs.
/// </summary>
Expand Down Expand Up @@ -67,6 +67,9 @@ public List<NuGetSource> NuGetModSources
set => Config.SetValue(NuGetModSourcesKey, value);
}

/// <inheritdoc/>
public override int Priority => 10;

/// <inheritdoc/>
public override Version Version { get; } = new Version(1, 0);
}
Expand Down

0 comments on commit ecc03e7

Please sign in to comment.