Skip to content

Commit

Permalink
WIP add autodetection of enabled toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Nov 26, 2024
1 parent 09500f0 commit 803f1b8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
12 changes: 0 additions & 12 deletions MonkeyLoader.GamePacks.ResoniteModLoader/ModConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,4 @@ public ModConfigurationDefinition(ResoniteModBase owner, Version configVersion,
AutoSave = autoSaveConfig;
}
}

/// <summary>
/// Represents an <see cref="Exception"/> encountered while loading a mod's configuration file.
/// </summary>
public class ModConfigurationException : Exception
{
internal ModConfigurationException(string message) : base(message)
{ }

internal ModConfigurationException(string message, Exception innerException) : base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MonkeyLoader.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

Expand All @@ -13,10 +14,14 @@ namespace ResoniteModLoader
/// </summary>
public class ModConfigurationDefinitionBuilder
{
private static readonly Type _modConfigKeyType = typeof(ModConfigurationKey);
private static readonly MethodInfo _addRangeComponentMethod = AccessTools.Method(typeof(ModConfigurationDefinitionBuilder), nameof(AddRangeComponent));
private static readonly string[] _definitiveEnabledToggles = new[] { "enabled", "mod enabled", "mod_enabled", "is_enabled" };
private static readonly string[] _indicativeEnabledToggles = new[] { "enabled" };
private static readonly Type _modConfigKeyType = typeof(ModConfigurationKey);

private readonly HashSet<ModConfigurationKey> _keys = new();
private readonly ResoniteModBase _owner;

private bool _autoSaveConfig = true;
private Version _configVersion = new(1, 0, 0);

Expand Down Expand Up @@ -88,6 +93,43 @@ internal void ProcessAttributes()
.Do(ProcessField);
}

internal bool TryGetEnabledToggle([NotNullWhen(true)] out DefiningConfigKey<bool>? enabledToggleKey, bool remove = true)
{
enabledToggleKey = null;
ModConfigurationKey? enabledToggle = null;

var potentialKeys = _keys.OfType<ModConfigurationKey<bool>>().ToArray();

foreach (var definitiveEnabledToggle in _definitiveEnabledToggles)
{
if (potentialKeys.FirstOrDefault(key => key.Name.Equals(definitiveEnabledToggle, StringComparison.OrdinalIgnoreCase)) is ModConfigurationKey enabledKey)
{
enabledToggle = enabledKey;
break;
}
}

if (enabledToggle is null)
{
potentialKeys = potentialKeys
.Where(key => _indicativeEnabledToggles
.Any(name => key.Name.Contains(name, StringComparison.OrdinalIgnoreCase)))
.ToArray();

if (potentialKeys.Length != 1)
return false;

enabledToggle = potentialKeys[0];
}

enabledToggleKey = (DefiningConfigKey<bool>)enabledToggle.UntypedKey;

if (remove)
_keys.Remove(enabledToggle);

return true;
}

private static void AddRangeComponent<T>(DefiningConfigKey<T> key, T min, T max)
{
var rangeComponent = new ConfigKeyRange<T>(min, max, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace ResoniteModLoader
{
/// <summary>
/// Represents an <see cref="Exception"/> encountered while loading a mod's configuration file.
/// </summary>
public class ModConfigurationException : Exception
{
internal ModConfigurationException(string message) : base(message)
{ }

internal ModConfigurationException(string message, Exception innerException) : base(message, innerException)
{ }
}
}
3 changes: 2 additions & 1 deletion MonkeyLoader.GamePacks.ResoniteModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public sealed class ModLoader : ResoniteMonkey<ModLoader>
private static readonly Lazy<bool> _isHeadless = new(() => AccessTools.AllTypes().Any(type => type.Namespace == "FrooxEngine.Headless"));

/// <summary>
/// Returns <c>true</c> if ResoniteModLoader was loaded by a headless
/// Gets whether this is running on a headless client.
/// </summary>
/// <value><c>true</c> if ResoniteModLoader was loaded by a headless; otherwise, <c>false</c>.</value>
public static bool IsHeadless => _isHeadless.Value;

/// <inheritdoc/>
Expand Down
15 changes: 12 additions & 3 deletions MonkeyLoader.GamePacks.ResoniteModLoader/ResoniteMod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MonkeyLoader;
using MonkeyLoader.Configuration;
using MonkeyLoader.Logging;
using MonkeyLoader.Patching;
using MonkeyLoader.Resonite;
Expand All @@ -18,6 +19,11 @@ public abstract class ResoniteMod : ResoniteModBase
{
private readonly Lazy<ModConfiguration?> _configuration;

/// <summary>
/// Gets the <see cref="MonkeyBase.Enabled">Enabled</see>-Toggle config item for this mod.
/// </summary>
internal DefiningConfigKey<bool>? EnabledToggle { get; private set; }

/// <inheritdoc/>
protected override ModConfiguration? Configuration => _configuration.Value;

Expand All @@ -26,10 +32,10 @@ protected ResoniteMod()
{
_configuration = new(() =>
{
if (BuildConfigurationDefinition() is ModConfigurationDefinition definition)
return Config.LoadSection(new ModConfiguration(definition));
if (BuildConfigurationDefinition() is not ModConfigurationDefinition definition)
return null;

return null;
return Config.LoadSection(new ModConfiguration(definition));
});
}

Expand Down Expand Up @@ -186,6 +192,9 @@ internal static Logger GetLoggerFromStackTrace(StackTrace stackTrace)

DefineConfiguration(builder);

if (builder.TryGetEnabledToggle(out var enabledToggleKey))
EnabledToggle = enabledToggleKey;

return builder.Build();
}

Expand Down
15 changes: 10 additions & 5 deletions MonkeyLoader.GamePacks.ResoniteModLoader/RmlMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace ResoniteModLoader
{
internal sealed class RmlMod : Mod
{
/// <summary>
/// Map of Assembly to ResoniteMod, used for logging purposes
/// </summary>
internal static readonly Dictionary<Assembly, ResoniteMod> AssemblyLookupMap = new();

private static readonly Type _resoniteModType = typeof(ResoniteMod);
private static readonly Uri _rmlIconUrl = new("https://avatars.githubusercontent.com/u/145755526");

///<inheritdoc/>
public override string Description => "RML Mods don't have descriptions.";

/// <summary>
/// Map of Assembly to ResoniteMod, used for logging purposes
/// </summary>
internal static readonly Dictionary<Assembly, ResoniteMod> AssemblyLookupMap = new();

///<inheritdoc/>
public override IFileSystem FileSystem { get; }

Expand Down Expand Up @@ -79,6 +79,11 @@ public RmlMod(MonkeyLoader.MonkeyLoader loader, string? location, bool isGamePac
authors.Add(resoniteMod.Author);
monkeys.Add(resoniteMod);

resoniteMod.GetConfiguration();

if (resoniteMod.EnabledToggle is not null)
MonkeyToggles.A

// Add dependencies after refactoring MKL
//foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
// dependencies.Add(referencedAssembly.Name, new DependencyReference())
Expand Down

0 comments on commit 803f1b8

Please sign in to comment.