Browse Source

Add NoEnableDisableAttribute to indicate that a plugin does not need an OnEnable and OnDisable method

pull/73/head
Anairkoen Schno 3 years ago
parent
commit
b3994aeb2d
Signed by: DaNike GPG Key ID: BEFB74D5F3FC4387
2 changed files with 22 additions and 2 deletions
  1. +6
    -2
      IPA.Loader/Loader/PluginExecutor.cs
  2. +16
    -0
      IPA.Loader/PluginInterfaces/Attributes/LifecycleAttributes.cs

+ 6
- 2
IPA.Loader/Loader/PluginExecutor.cs View File

@ -122,6 +122,7 @@ namespace IPA.Loader
// TODO: make enable and disable able to take a bool indicating which it is // TODO: make enable and disable able to take a bool indicating which it is
private static Action<object> MakeLifecycleEnableFunc(Type type, string name) private static Action<object> MakeLifecycleEnableFunc(Type type, string name)
{ {
var noEnableDisable = type.GetCustomAttribute<NoEnableDisableAttribute>() is not null;
var enableMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance) var enableMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Select(m => (m, attrs: m.GetCustomAttributes(typeof(IEdgeLifecycleAttribute), false))) .Select(m => (m, attrs: m.GetCustomAttributes(typeof(IEdgeLifecycleAttribute), false)))
.Select(t => (t.m, attrs: t.attrs.Cast<IEdgeLifecycleAttribute>())) .Select(t => (t.m, attrs: t.attrs.Cast<IEdgeLifecycleAttribute>()))
@ -129,7 +130,8 @@ namespace IPA.Loader
.Select(t => t.m).ToArray(); .Select(t => t.m).ToArray();
if (enableMethods.Length == 0) if (enableMethods.Length == 0)
{ {
Logger.loader.Notice($"Plugin {name} has no methods marked [OnStart] or [OnEnable]. Is this intentional?");
if (!noEnableDisable)
Logger.loader.Notice($"Plugin {name} has no methods marked [OnStart] or [OnEnable]. Is this intentional?");
return o => { }; return o => { };
} }
@ -153,6 +155,7 @@ namespace IPA.Loader
} }
private static Func<object, Task> MakeLifecycleDisableFunc(Type type, string name) private static Func<object, Task> MakeLifecycleDisableFunc(Type type, string name)
{ {
var noEnableDisable = type.GetCustomAttribute<NoEnableDisableAttribute>() is not null;
var disableMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance) var disableMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Select(m => (m, attrs: m.GetCustomAttributes(typeof(IEdgeLifecycleAttribute), false))) .Select(m => (m, attrs: m.GetCustomAttributes(typeof(IEdgeLifecycleAttribute), false)))
.Select(t => (t.m, attrs: t.attrs.Cast<IEdgeLifecycleAttribute>())) .Select(t => (t.m, attrs: t.attrs.Cast<IEdgeLifecycleAttribute>()))
@ -160,7 +163,8 @@ namespace IPA.Loader
.Select(t => t.m).ToArray(); .Select(t => t.m).ToArray();
if (disableMethods.Length == 0) if (disableMethods.Length == 0)
{ {
Logger.loader.Notice($"Plugin {name} has no methods marked [OnExit] or [OnDisable]. Is this intentional?");
if (!noEnableDisable)
Logger.loader.Notice($"Plugin {name} has no methods marked [OnExit] or [OnDisable]. Is this intentional?");
return o => TaskEx.WhenAll(); return o => TaskEx.WhenAll();
} }


+ 16
- 0
IPA.Loader/PluginInterfaces/Attributes/LifecycleAttributes.cs View File

@ -109,4 +109,20 @@ namespace IPA
{ {
EdgeLifecycleType IEdgeLifecycleAttribute.Type => EdgeLifecycleType.Disable; EdgeLifecycleType IEdgeLifecycleAttribute.Type => EdgeLifecycleType.Disable;
} }
/// <summary>
/// Indicates that the applied plugin class does not need <see cref="OnEnableAttribute"/> or
/// <see cref="OnDisableAttribute"/> methods.
/// </summary>
/// <remarks>
/// This is typically only the ccase when some other utility mod handles their lifecycle for
/// them, such as with SiraUtil and Zenject.
/// </remarks>
/// <seealso cref="OnEnableAttribute"/>
/// <seealso cref="OnDisableAttribute"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class NoEnableDisableAttribute : Attribute
{
}
} }

Loading…
Cancel
Save