Skip to content

Commit

Permalink
Implement InitAfter on GlobalStateAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Fayti1703 committed Jan 1, 2023
1 parent 968a90e commit 7c0d019
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Ktisis/GlobalStateAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ namespace Ktisis {
* <summary>Indicates that this class has global state and may contain static methods annotated with <see cref="GlobalInitAttribute"/> and/or <see cref="GlobalDisposeAttribute"/>.</summary>
*/
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class GlobalStateAttribute : Attribute {}
public class GlobalStateAttribute : Attribute {
public Type[] InitAfter { get; set; } = Array.Empty<Type>();
}
}
26 changes: 24 additions & 2 deletions Ktisis/Ktisis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Ktisis.History;
using Ktisis.Events;
using Ktisis.Overlay;
using System.Runtime.CompilerServices;

namespace Ktisis {
public sealed class Ktisis : IDalamudPlugin {
Expand Down Expand Up @@ -135,13 +136,34 @@ private static void GlobalInit() {
}

private static IEnumerable<Type> GetGlobalInitTypes() =>
typeof(Ktisis).Assembly.GetTypes()
.Where(x => x.CustomAttributes.Any(x => x.AttributeType == typeof(GlobalStateAttribute)));
typeof(Ktisis).Assembly.GetTypes().Select(type => (type, globalStateAttr: type.GetCustomAttribute<GlobalStateAttribute>()))
.Where(x => x.globalStateAttr != null)
.OrderBy(x => (x.type, initAfter: new HashSet<Type>(x.globalStateAttr!.InitAfter)), new DelegateComparer<(Type type, HashSet<Type> initAfter)>((a, b) => {
if(a.initAfter.Contains(b.type))
return 1;
if(b.initAfter.Contains(a.type))
return -1;
return 0;
})).Select(x => x.type);

private static void GlobalDispose() {
while (ToGloballyDispose.TryPop(out MethodInfo? toDispose)) {
toDispose.Invoke(null, Array.Empty<object>());
}
}
}

public struct DelegateComparer<T> : IComparer<T> {

public delegate int Comparer(T? x, T? y);

public Comparer comparer;

public DelegateComparer(Comparer comparer) {
this.comparer = comparer;
}

public int Compare(T? x, T? y) => this.comparer(x, y);
}

}

0 comments on commit 7c0d019

Please sign in to comment.