Skip to content

Commit

Permalink
Add logging options to MonkeyBase, add more mod extensions, improve s…
Browse files Browse the repository at this point in the history
…hutdown process
  • Loading branch information
Banane9 committed Feb 28, 2024
1 parent 74bed17 commit 60e0573
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 59 deletions.
8 changes: 2 additions & 6 deletions MonkeyLoader/Meta/IMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace MonkeyLoader.Meta
/// <summary>
/// Contains all metadata and references for a mod.
/// </summary>
public interface IMod : IConfigOwner, ILoadedNuGetPackage, IShutdown, IComparable<IMod>
public interface IMod : IConfigOwner, ILoadedNuGetPackage, IComparable<IMod>
{
/// <summary>
/// Gets the names of the authors of this mod.
Expand Down Expand Up @@ -65,10 +65,6 @@ public interface IMod : IConfigOwner, ILoadedNuGetPackage, IShutdown, IComparabl
/// </summary>
public abstract Uri? IconUrl { get; }

/// <summary>
/// Gets the unique identifier of this mod.
/// </summary>
//public string Id { get; }
/// <summary>
/// Gets whether this mod is a game pack.
/// </summary>
Expand Down Expand Up @@ -150,7 +146,7 @@ public interface IMod : IConfigOwner, ILoadedNuGetPackage, IShutdown, IComparabl
public bool HasTag(string tag);
}

internal interface IModInternal : IMod
internal interface IModInternal : IMod, IShutdown
{
public bool LoadEarlyMonkeys();

Expand Down
22 changes: 22 additions & 0 deletions MonkeyLoader/Meta/IShutdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@

namespace MonkeyLoader.Meta
{
/// <summary>
/// Contains extension methods for collections of <see cref="IShutdown"/> instances.
/// </summary>
public static class ShutdownEnumerableExtensions
{
/// <summary>
/// Calls the <see cref="IShutdown.Shutdown"/> method on all elements of the collection,
/// aggregating their success state as an 'all'.
/// </summary>
/// <param name="shutdowns">The <see cref="IShutdown"/> instances to process.</param>
/// <returns><c>true</c> if all instances successfully shut down, <c>false</c> otherwise.</returns>
public static bool ShutdownAll(this IEnumerable<IShutdown> shutdowns)
{
var success = true;

foreach (var shutdown in shutdowns)
success &= shutdown.Shutdown();

return success;
}
}

/// <summary>
/// Interface for everything that can be shut down.
/// </summary>
Expand Down
88 changes: 83 additions & 5 deletions MonkeyLoader/ModEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public static IEarlyMonkey[] GetEarlyMonkeys(this IEnumerable<IMod> mods)
public static IEarlyMonkey[] GetEarlyMonkeysAscending(this IEnumerable<IMod> mods)
=> mods.GetSortedEarlyMonkeys(Monkey.AscendingComparer);

/// <summary>
/// Gets the <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in topological order.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IEarlyMonkey"/>s of.</param>
/// <returns>The <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in topological order.</returns>
public static IEarlyMonkey[] GetEarlyMonkeysAscending(this IMod mod)
=> mod.GetSortedEarlyMonkeys(Monkey.AscendingComparer);

/// <summary>
/// Gets the <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in reverse-topological order.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IEarlyMonkey"/>s of.</param>
/// <returns>The <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in reverse-topological order.</returns>
public static IEarlyMonkey[] GetEarlyMonkeysDescending(this IMod mod)
=> mod.GetSortedEarlyMonkeys(Monkey.DescendingComparer);

/// <summary>
/// Gets the <see cref="IEarlyMonkey"/>s of all given <paramref name="mods"/> in reverse-topological order.
/// </summary>
Expand All @@ -54,12 +70,42 @@ public static IMonkey[] GetMonkeysAscending(this IEnumerable<IMod> mods)
=> mods.GetSortedMonkeys(Monkey.AscendingComparer);

/// <summary>
/// Gets the <see cref="IMonkey"/>s of all given <paramref name="mods"/> in reverse-topological order.
/// Gets the <see cref="IMonkey"/>s of the given <paramref name="mod"/> in topological order.
/// </summary>
/// <param name="mods">The mods to get the <see cref="IMonkey"/>s of.</param>
/// <returns>All <see cref="IMonkey"/>s of the given <paramref name="mods"/> in reverse-topological order.</returns>
public static IMonkey[] GetMonkeysDescending(this IEnumerable<IMod> mods)
=> mods.GetSortedMonkeys(Monkey.DescendingComparer);
/// <param name="mod">The mod to get the <see cref="IMonkey"/>s of.</param>
/// <returns>The <see cref="IMonkey"/>s of the given <paramref name="mod"/> in topological order.</returns>
public static IMonkey[] GetMonkeysAscending(this IMod mod)
=> mod.GetSortedMonkeys(Monkey.AscendingComparer);

/// <summary>
/// Gets the <see cref="IMonkey"/>s of all given <paramref name="mod"/> in reverse-topological order.
/// </summary>
/// <param name="mod">The mods to get the <see cref="IMonkey"/>s of.</param>
/// <returns>All <see cref="IMonkey"/>s of the given <paramref name="mod"/> in reverse-topological order.</returns>
public static IMonkey[] GetMonkeysDescending(this IEnumerable<IMod> mod)
=> mod.GetSortedMonkeys(Monkey.DescendingComparer);

/// <summary>
/// Gets the <see cref="IMonkey"/>s of the given <paramref name="mod"/> in reverse-topological order.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IMonkey"/>s of.</param>
/// <returns>The <see cref="IMonkey"/>s of the given <paramref name="mod"/> in reverse-topological order.</returns>
public static IMonkey[] GetMonkeysDescending(this IMod mod)
=> mod.GetSortedMonkeys(Monkey.DescendingComparer);

/// <summary>
/// Gets the <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in the order defined by the given <see cref="Comparison{T}"/>.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IEarlyMonkey"/>s of.</param>
/// <param name="comparison">The <see cref="Comparison{T}"/> defining the order.</param>
/// <returns>The <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in the defined order.</returns>
public static IEarlyMonkey[] GetSortedEarlyMonkeys(this IMod mod, Comparison<IEarlyMonkey> comparison)
{
var earlyMonkeys = mod.EarlyMonkeys.ToArray();
Array.Sort(earlyMonkeys, comparison);

return earlyMonkeys;
}

/// <summary>
/// Gets the <see cref="IEarlyMonkey"/>s of all given <paramref name="mods"/> in the order defined by the given <see cref="Comparison{T}"/>.
Expand All @@ -84,6 +130,29 @@ public static IEarlyMonkey[] GetSortedEarlyMonkeys(this IEnumerable<IMod> mods,
public static IEarlyMonkey[] GetSortedEarlyMonkeys(this IEnumerable<IMod> mods, IComparer<IEarlyMonkey> comparer)
=> mods.GetSortedEarlyMonkeys(comparer.Compare);

/// <summary>
/// Gets the <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in the order defined by the given <see cref="IComparer{T}"/>.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IEarlyMonkey"/>s of.</param>
/// <param name="comparer">The <see cref="IComparer{T}"/> defining the order.</param>
/// <returns>The <see cref="IEarlyMonkey"/>s of the given <paramref name="mod"/> in the defined order.</returns>
public static IEarlyMonkey[] GetSortedEarlyMonkeys(this IMod mod, IComparer<IEarlyMonkey> comparer)
=> mod.GetSortedEarlyMonkeys(comparer.Compare);

/// <summary>
/// Gets the <see cref="IMonkey"/>s of the given <paramref name="mod"/> in the order defined by the given <see cref="Comparison{T}"/>.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IMonkey"/>s of.</param>
/// <param name="comparison">The <see cref="Comparison{T}"/> defining the order.</param>
/// <returns>The <see cref="IMonkey"/>s of the given <paramref name="mod"/> in the defined order.</returns>
public static IMonkey[] GetSortedMonkeys(this IMod mod, Comparison<IMonkey> comparison)
{
var monkeys = mod.Monkeys.ToArray();
Array.Sort(monkeys, comparison);

return monkeys;
}

/// <summary>
/// Gets the <see cref="IMonkey"/>s of all given <paramref name="mods"/> in the order defined by the given <see cref="IComparer{T}"/>.
/// </summary>
Expand All @@ -93,6 +162,15 @@ public static IEarlyMonkey[] GetSortedEarlyMonkeys(this IEnumerable<IMod> mods,
public static IMonkey[] GetSortedMonkeys(this IEnumerable<IMod> mods, IComparer<IMonkey> comparer)
=> mods.GetSortedMonkeys(comparer.Compare);

/// <summary>
/// Gets the <see cref="IMonkey"/>s of the given <paramref name="mod"/> in the order defined by the given <see cref="IComparer{T}"/>.
/// </summary>
/// <param name="mod">The mod to get the <see cref="IMonkey"/>s of.</param>
/// <param name="comparer">The <see cref="IComparer{T}"/> defining the order.</param>
/// <returns>All <see cref="IMonkey"/>s of the given <paramref name="mod"/> in the defined order.</returns>
public static IMonkey[] GetSortedMonkeys(this IMod mod, IComparer<IMonkey> comparer)
=> mod.GetSortedMonkeys(comparer.Compare);

/// <summary>
/// Gets the <see cref="IMonkey"/>s of all given <paramref name="mods"/> in the order defined by the given <see cref="Comparison{T}"/>.
/// </summary>
Expand Down
Loading

0 comments on commit 60e0573

Please sign in to comment.