Browse Source

Added begginings of disablable mod support

pull/12/head
Anairkoen Schno 5 years ago
parent
commit
9813b62f01
7 changed files with 136 additions and 15 deletions
  1. +2
    -1
      IPA.Injector/Injector.cs
  2. +1
    -1
      IPA.Loader/Config/SelfConfig.cs
  3. +2
    -0
      IPA.Loader/IPA.Loader.csproj
  4. +42
    -0
      IPA.Loader/Loader/DisabledConfig.cs
  5. +18
    -0
      IPA.Loader/Loader/PluginLoader.cs
  6. +42
    -13
      IPA.Loader/Loader/PluginManager.cs
  7. +29
    -0
      IPA.Loader/PluginInterfaces/BeatSaber/IDisablablePlugin.cs

+ 2
- 1
IPA.Injector/Injector.cs View File

@ -50,7 +50,8 @@ namespace IPA.Injector
log.Debug("Initializing logger");
SelfConfig.Set();
SelfConfig.Load();
DisabledConfig.Load();
loader.Debug("Prepping bootstrapper");


+ 1
- 1
IPA.Loader/Config/SelfConfig.cs View File

@ -31,7 +31,7 @@ namespace IPA.Config
public static Ref<SelfConfig> SelfConfigRef;
public static void Set()
public static void Load()
{
LoaderConfig = Config.GetProviderFor(IPAName, "json");
}


+ 2
- 0
IPA.Loader/IPA.Loader.csproj View File

@ -64,6 +64,7 @@
<Compile Include="Config\SelfConfig.cs" />
<Compile Include="JsonConverters\MultilineStringConverter.cs" />
<Compile Include="Loader\Composite\CompositeBSPlugin.cs" />
<Compile Include="Loader\DisabledConfig.cs" />
<Compile Include="Loader\Features\AddInFeature.cs" />
<Compile Include="Loader\Features\ConfigProviderFeature.cs" />
<Compile Include="Loader\Features\DefineFeature.cs" />
@ -80,6 +81,7 @@
<Compile Include="Logging\Printers\PluginSubLogPrinter.cs" />
<Compile Include="Logging\StdoutInterceptor.cs" />
<Compile Include="PluginInterfaces\BeatSaber\IBeatSaberPlugin.cs" />
<Compile Include="PluginInterfaces\BeatSaber\IDisablablePlugin.cs" />
<Compile Include="PluginInterfaces\BeatSaber\IEnhancedBeatSaberPlugin.cs" />
<Compile Include="PluginInterfaces\BeatSaber\ModsaberModInfo.cs" />
<Compile Include="PluginInterfaces\IGenericEnhancedPlugin.cs" />


+ 42
- 0
IPA.Loader/Loader/DisabledConfig.cs View File

@ -0,0 +1,42 @@
using IPA.Config;
using IPA.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IPA.Loader
{
internal class DisabledConfig
{
private static IConfigProvider _provider;
public static IConfigProvider Provider
{
get => _provider;
set
{
_provider?.RemoveLinks();
value.Load();
Ref = value.MakeLink<DisabledConfig>((c, v) =>
{
if (v.Value.Reset)
c.Store(v.Value = new DisabledConfig { Reset = false });
});
_provider = value;
}
}
public static Ref<DisabledConfig> Ref;
public static void Load()
{
Provider = Config.Config.GetProviderFor("Disabled Mods", "json");
}
public bool Reset = true;
public HashSet<string> DisabledModIds = new HashSet<string>();
}
}

+ 18
- 0
IPA.Loader/Loader/PluginLoader.cs View File

@ -23,6 +23,7 @@ namespace IPA.Loader
{
LoadMetadata();
Resolve();
FilterDisabled();
ComputeLoadOrder();
});
@ -287,6 +288,7 @@ namespace IPA.Loader
}
// keep track of these for the updater; it should still be able to update mods not loaded
// TODO: add ignore reason
internal static HashSet<PluginMetadata> ignoredPlugins = new HashSet<PluginMetadata>();
internal static void Resolve()
@ -353,6 +355,22 @@ namespace IPA.Loader
PluginsMetadata = resolved;
}
private static void FilterDisabled()
{ // TODO: move disabled to a seperate list from ignored
var enabled = new List<PluginMetadata>(PluginsMetadata.Count);
var disabled = DisabledConfig.Ref.Value.DisabledModIds;
foreach (var meta in PluginsMetadata)
{
if (disabled.Contains(meta.Id ?? meta.Name))
ignoredPlugins.Add(meta);
else
enabled.Add(meta);
}
PluginsMetadata = enabled;
}
internal static void ComputeLoadOrder()
{
#if DEBUG


+ 42
- 13
IPA.Loader/Loader/PluginManager.cs View File

@ -26,18 +26,7 @@ namespace IPA.Loader
/// <summary>
/// An <see cref="IEnumerable"/> of new Beat Saber plugins
/// </summary>
internal static IEnumerable<IBeatSaberPlugin> BSPlugins
{
get
{
if(_bsPlugins == null)
{
LoadPlugins();
}
return (_bsPlugins ?? throw new InvalidOperationException()).Select(p => p.Plugin);
}
}
internal static IEnumerable<IBeatSaberPlugin> BSPlugins => (_bsPlugins ?? throw new InvalidOperationException()).Select(p => p.Plugin);
private static List<PluginInfo> _bsPlugins;
internal static IEnumerable<PluginInfo> BSMetas => _bsPlugins;
@ -56,11 +45,51 @@ namespace IPA.Loader
/// </summary>
/// <param name="name">the ModSaber name of the plugin to get (must be an exact match)</param>
/// <returns>the plugin info for the requested plugin or null</returns>
public static PluginInfo GetPluginFromModSaberName(string name)
[Obsolete("Old name. Use GetPluginFromId instead.")]
public static PluginInfo GetPluginFromModSaberName(string name) => GetPluginFromId(name);
/// <summary>
/// Gets info about the plugin with the specified ID.
/// </summary>
/// <param name="name">the ModSaber name of the plugin to get (must be an exact match)</param>
/// <returns>the plugin info for the requested plugin or null</returns>
public static PluginInfo GetPluginFromId(string name)
{
return BSMetas.FirstOrDefault(p => p.Metadata.Id == name);
}
/// <summary>
/// Disables a plugin for the next time the game starts.
/// </summary>
/// <param name="plugin">the plugin to disable</param>
public static void DisablePlugin(PluginInfo plugin) =>
DisablePlugin(plugin.Metadata.Id ?? plugin.Metadata.Name);
/// <summary>
/// Disables a plugin for the next time the game starts.
/// </summary>
/// <param name="pluginId">the ID, or name if the ID is null, of the plugin to disable</param>
public static void DisablePlugin(string pluginId)
{
DisabledConfig.Ref.Value.DisabledModIds.Add(pluginId);
}
/// <summary>
/// Enables a plugin that had been previously disabled.
/// </summary>
/// <param name="plugin">the plugin to enable</param>
public static void EnablePlugin(PluginMetadata plugin) =>
EnablePlugin(plugin.Id ?? plugin.Name);
/// <summary>
/// Enables a plugin that had been previously disabled.
/// </summary>
/// <param name="pluginId">the ID, or name if the ID is null, of the plugin to enable</param>
public static void EnablePlugin(string pluginId)
{
DisabledConfig.Ref.Value.DisabledModIds.Remove(pluginId);
}
/// <summary>
/// Gets a list of all BSIPA plugins.
/// </summary>


+ 29
- 0
IPA.Loader/PluginInterfaces/BeatSaber/IDisablablePlugin.cs View File

@ -0,0 +1,29 @@
namespace IPA
{
/// <summary>
/// Provides methods to allow runtime enabling and disabling of a plugin.
/// </summary>
public interface IDisablablePlugin
{
/// <summary>
/// Called when a plugin is enabled. This is where you shhould set up Harmony patches and the like.
/// </summary>
/// <remarks>
/// This will be called after Init, and will be called when the plugin loads normally too.
/// When a plugin is disabled at startup, neither this nor Init will be called until it is enabled.
///
/// Init will only ever be called once.
/// </remarks>
void OnEnable();
/// <summary>
/// Called when a plugin is disabled at runtime. This should disable things like Harmony patches and unsubscribe
/// from events. After this is called there should be no lingering effects of the mod.
/// </summary>
/// <remarks>
/// This will get called at shutdown, after <see cref="IBeatSaberPlugin.OnApplicationQuit"/>, as well as when the
/// plugin is disabled at runtime.
/// </remarks>
void OnDisable();
}
}

Loading…
Cancel
Save