You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.9 KiB

  1. #nullable enable
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. #if NET3
  7. using Net3_Proxy;
  8. #endif
  9. namespace IPA.Loader.Features
  10. {
  11. /// <summary>
  12. /// The root interface for a mod Feature.
  13. /// </summary>
  14. /// <remarks>
  15. /// Avoid storing any data in any subclasses. If you do, it may result in a failure to load the feature.
  16. /// </remarks>
  17. public abstract class Feature
  18. {
  19. /// <summary>
  20. /// Initializes the feature with the data provided in the definition.
  21. /// </summary>
  22. /// <remarks>
  23. /// <para>This gets called AFTER your <c>Init</c> method, but BEFORE the target's <c>Init</c> method. If it is applied to the defining plugin, <c>BeforeInit</c> is not called.</para>
  24. /// <para>Returning <see langword="false" /> does <i>not</i> prevent the plugin from being loaded. It simply prevents the feature from being used.</para>
  25. /// </remarks>
  26. /// <param name="meta">the metadata of the plugin that is being prepared</param>
  27. /// <param name="featureData">the data provided with the feature</param>
  28. /// <returns><see langword="true"/> if the feature is valid for the plugin, <see langword="false"/> otherwise</returns>
  29. protected abstract bool Initialize(PluginMetadata meta, JObject featureData);
  30. /// <summary>
  31. /// The message to be logged when the feature is not valid for a plugin.
  32. /// This should also be set whenever either <see cref="BeforeInit"/> returns false.
  33. /// </summary>
  34. /// <value>the message to show when the feature is marked invalid</value>
  35. public virtual string? InvalidMessage { get; protected set; }
  36. /// <summary>
  37. /// Called before a plugin's `Init` method is called. This will not be called if there is no `Init` method. This should never throw an exception. An exception will abort the loading of the plugin with an error.
  38. /// </summary>
  39. /// <param name="plugin">the plugin to be initialized</param>
  40. /// <returns>whether or not to call the Init method</returns>
  41. public virtual void BeforeInit(PluginMetadata plugin) { }
  42. /// <summary>
  43. /// Called after a plugin has been fully initialized, whether or not there is an `Init` method. This should never throw an exception.
  44. /// </summary>
  45. /// <param name="plugin">the plugin that was just initialized</param>
  46. /// <param name="pluginInstance">the instance of the plugin being initialized</param>
  47. public virtual void AfterInit(PluginMetadata plugin, object pluginInstance) => AfterInit(plugin);
  48. /// <summary>
  49. /// Called after a plugin has been fully initialized, whether or not there is an `Init` method. This should never throw an exception.
  50. /// </summary>
  51. /// <param name="plugin">the plugin that was just initialized</param>
  52. public virtual void AfterInit(PluginMetadata plugin) { }
  53. /// <summary>
  54. /// Called after a plugin with this feature appplied is disabled.
  55. /// </summary>
  56. /// <param name="plugin">the plugin that was disabled</param>
  57. public virtual void AfterDisable(PluginMetadata plugin) { }
  58. // TODO: rework features to take arguments as JSON objects
  59. static Feature()
  60. {
  61. Reset();
  62. }
  63. internal static void Reset()
  64. {
  65. featureTypes = new()
  66. {
  67. { "IPA.DefineFeature", typeof(DefineFeature) }
  68. };
  69. featureDelcarers = new()
  70. {
  71. { "IPA.DefineFeature", null }
  72. };
  73. }
  74. private static Dictionary<string, Type> featureTypes = null!;
  75. private static Dictionary<string, PluginMetadata?> featureDelcarers = null!;
  76. internal static bool HasFeature(string name) => featureTypes.ContainsKey(name);
  77. internal static bool PreregisterFeature(PluginMetadata defining, string name)
  78. {
  79. if (featureDelcarers.ContainsKey(name)) return false;
  80. featureDelcarers.Add(name, defining);
  81. return true;
  82. }
  83. internal static bool RegisterFeature(PluginMetadata definingPlugin, string name, Type type)
  84. {
  85. if (!typeof(Feature).IsAssignableFrom(type))
  86. throw new ArgumentException($"Feature type not subclass of {nameof(Feature)}", nameof(type));
  87. if (featureTypes.ContainsKey(name)) return false;
  88. if (featureDelcarers.TryGetValue(name, out var declarer))
  89. {
  90. if (definingPlugin != declarer)
  91. return false;
  92. }
  93. else
  94. {
  95. featureDelcarers.Add(name, definingPlugin);
  96. }
  97. featureTypes.Add(name, type);
  98. return true;
  99. }
  100. private class EmptyFeature : Feature
  101. {
  102. protected override bool Initialize(PluginMetadata meta, JObject featureData)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. }
  107. internal string FeatureName = null!;
  108. internal class Instance
  109. {
  110. public readonly PluginMetadata AppliedTo;
  111. public readonly string Name;
  112. public readonly JObject Data;
  113. public Instance(PluginMetadata appliedTo, string name, JObject data)
  114. {
  115. AppliedTo = appliedTo;
  116. Name = name;
  117. Data = data;
  118. type = null;
  119. }
  120. private Type? type;
  121. public bool TryGetDefiningPlugin(out PluginMetadata? plugin)
  122. {
  123. return featureDelcarers.TryGetValue(Name, out plugin);
  124. }
  125. // returns whether or not Initialize returned true, feature is always set when the thing exists
  126. public bool TryCreate(out Feature feature)
  127. {
  128. if (type == null)
  129. {
  130. if (!featureTypes.TryGetValue(Name, out type))
  131. {
  132. feature = new EmptyFeature() { InvalidMessage = "No such feature type found", FeatureName = Name };
  133. return false;
  134. }
  135. }
  136. bool result;
  137. try
  138. {
  139. feature = (Feature)Activator.CreateInstance(type);
  140. feature.FeatureName = Name;
  141. result = feature.Initialize(AppliedTo, Data);
  142. }
  143. catch (Exception e)
  144. {
  145. result = false;
  146. feature = new EmptyFeature() { InvalidMessage = e.ToString(), FeatureName = Name };
  147. }
  148. return result;
  149. }
  150. }
  151. }
  152. }