Add a Source property to PluginInfo

Also abstract an interface among the base plugins classes to make this easier
This commit is contained in:
aldelaro5 committed 2024-08-23 23:06:54 -04:00
1 parent d8dd404e52
commit b688bfa978
9 files changed
+75 -47

No files matched your search

-3
View File
@@ -12,9 +12,6 @@
<PackageReference Include="SemanticVersioning" Version="2.0.2"/> <PackageReference Include="SemanticVersioning" Version="2.0.2"/>
<PackageReference Include="MonoMod.Utils" Version="22.5.1.1"/> <PackageReference Include="MonoMod.Utils" Version="22.5.1.1"/>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Remove="Contract\IPlugin.cs"/>
</ItemGroup>
<!-- CopyLocalLockFileAssemblies causes to also output shared assemblies: https://github.com/NuGet/Home/issues/4837#issuecomment-354536302 --> <!-- CopyLocalLockFileAssemblies causes to also output shared assemblies: https://github.com/NuGet/Home/issues/4837#issuecomment-354536302 -->
<!-- Since all core assemblies usually follow naming of System.*, we just delete them for now --> <!-- Since all core assemblies usually follow naming of System.*, we just delete them for now -->
+19 -4
View File
@@ -116,6 +116,16 @@ public abstract class BaseChainloader<TPlugin>
return pluginTarget.Build > CurrentAssemblyVersion.Build; return pluginTarget.Build > CurrentAssemblyVersion.Build;
} }
/// <summary>
/// Obtains the <see cref="PluginInfo"/> corresponding to its GUID
/// </summary>
/// <param name="pluginGuid">The GUID of the plugin to get the info from</param>
/// <returns>The info of the plugin, null if the plugin wasn't loaded by the chainloader</returns>
public static PluginInfo GetPluginInfoFromGuid(string pluginGuid)
{
return Plugins.TryGetValue(pluginGuid, out var pluginInfo) ? pluginInfo : null;
}
#region Contract #region Contract
protected virtual string ConsoleTitle => $"BepInEx {Paths.BepInExVersion} - {Paths.ProcessName}"; protected virtual string ConsoleTitle => $"BepInEx {Paths.BepInExVersion} - {Paths.ProcessName}";
@@ -127,7 +137,7 @@ public abstract class BaseChainloader<TPlugin>
/// If a plugin fails to load, it is removed. This list only contains the plugins that were actually /// If a plugin fails to load, it is removed. This list only contains the plugins that were actually
/// loaded and have their dependencies satisfied. /// loaded and have their dependencies satisfied.
/// </summary> /// </summary>
public Dictionary<string, PluginInfo> Plugins { get; } = new(); public static Dictionary<string, PluginInfo> Plugins { get; } = new();
/// <summary> /// <summary>
/// List of all <see cref="PluginInfo" /> instances of all providers loaded via the chainloader. /// List of all <see cref="PluginInfo" /> instances of all providers loaded via the chainloader.
@@ -138,8 +148,9 @@ public abstract class BaseChainloader<TPlugin>
/// <summary> /// <summary>
/// List of all <see cref="IPluginLoadContext" /> discovered from all the providers /// List of all <see cref="IPluginLoadContext" /> discovered from all the providers
/// with each context associated with its provider info
/// </summary> /// </summary>
public List<IPluginLoadContext> LoadContexts { get; } = new(); public Dictionary<IPluginLoadContext, PluginInfo> LoadContexts { get; } = new();
/// <summary> /// <summary>
/// Collection of error chainloader messages that occured during plugin loading. /// Collection of error chainloader messages that occured during plugin loading.
@@ -344,7 +355,7 @@ public abstract class BaseChainloader<TPlugin>
LoadPlugins(providers, provider: true); LoadPlugins(providers, provider: true);
FinishedProviders?.Invoke(); FinishedProviders?.Invoke();
var plugins = TypeLoader.GetPluginsFromLoadContexts(LoadContexts, ToPluginInfo<TPlugin>, HasBepinPluginType<BepInPlugin>, "chainloader"); var plugins = TypeLoader.GetPluginsFromLoadContexts(LoadContexts.Keys, ToPluginInfo<TPlugin>, HasBepinPluginType<BepInPlugin>, "chainloader");
Logger.Log(LogLevel.Info, $"{plugins.Count} plugin{(plugins.Count == 1 ? "" : "s")} to load"); Logger.Log(LogLevel.Info, $"{plugins.Count} plugin{(plugins.Count == 1 ? "" : "s")} to load");
LoadPlugins(plugins, provider: false); LoadPlugins(plugins, provider: false);
Finished?.Invoke(); Finished?.Invoke();
@@ -404,11 +415,15 @@ public abstract class BaseChainloader<TPlugin>
plugin.Instance = Activator.CreateInstance(type); plugin.Instance = Activator.CreateInstance(type);
AppDomain.CurrentDomain.AssemblyResolve += (_, args) => ((BasePluginProvider)plugin.Instance).ResolveAssembly(args.Name); AppDomain.CurrentDomain.AssemblyResolve += (_, args) => ((BasePluginProvider)plugin.Instance).ResolveAssembly(args.Name);
var pluginLoadContexts = ((BasePluginProvider)plugin.Instance).GetPlugins(); var pluginLoadContexts = ((BasePluginProvider)plugin.Instance).GetPlugins();
LoadContexts.AddRange(pluginLoadContexts); foreach (IPluginLoadContext context in pluginLoadContexts)
{
LoadContexts[context] = plugin;
}
ProviderLoaded?.Invoke(plugin); ProviderLoaded?.Invoke(plugin);
} }
else else
{ {
plugin.Source = LoadContexts[plugin.LoadContext];
plugin.Instance = LoadPlugin(plugin, ass); plugin.Instance = LoadPlugin(plugin, ass);
PluginLoaded?.Invoke(plugin); PluginLoaded?.Invoke(plugin);
plugin.LoadContext.Dispose(); plugin.LoadContext.Dispose();
+1 -1
View File
@@ -184,7 +184,7 @@ public static class TypeLoader
/// A dictionary of all assemblies in the directory and the list of type metadatas of types that match the /// A dictionary of all assemblies in the directory and the list of type metadatas of types that match the
/// selector. /// selector.
/// </returns> /// </returns>
public static List<T> GetPluginsFromLoadContexts<T>(IList<IPluginLoadContext> loadContexts, public static List<T> GetPluginsFromLoadContexts<T>(IEnumerable<IPluginLoadContext> loadContexts,
Func<TypeDefinition, IPluginLoadContext, string, T> typeSelector, Func<TypeDefinition, IPluginLoadContext, string, T> typeSelector,
Func<AssemblyDefinition, bool> assemblyFilter = null, Func<AssemblyDefinition, bool> assemblyFilter = null,
string cacheName = null) string cacheName = null)
+17 -22
View File
@@ -1,28 +1,23 @@
using System; using BepInEx.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BepInEx.Configuration;
using BepInEx.Logging; using BepInEx.Logging;
namespace BepInEx.Contract namespace BepInEx.Contract;
internal interface IPlugin
{ {
public interface IPlugin /// <summary>
{ /// Information about this plugin as it was loaded.
/// <summary> /// </summary>
/// Information about this plugin as it was loaded. PluginInfo Info { get; }
/// </summary>
PluginInfo Info { get; }
/// <summary> /// <summary>
/// Logger instance tied to this plugin. /// Logger instance tied to this plugin.
/// </summary> /// </summary>
ManualLogSource Logger { get; } ManualLogSource Logger { get; }
/// <summary> /// <summary>
/// Default config file tied to this plugin. The config file will not be created until /// Default config file tied to this plugin. The config file will not be created until
/// any settings are added and changed, or <see cref="ConfigFile.Save"/> is called. /// any settings are added and changed, or <see cref="ConfigFile.Save"/> is called.
/// </summary> /// </summary>
ConfigFile Config { get; } ConfigFile Config { get; }
}
} }
+6
View File
@@ -17,6 +17,12 @@ public class PluginInfo : ICacheable
/// </summary> /// </summary>
public BepInPlugin Metadata { get; internal set; } public BepInPlugin Metadata { get; internal set; }
/// <summary>
/// The <see cref="PluginInfo"/> of the plugin provider that loaded this plugin.
/// This is null if this plugin is a provider
/// </summary>
public PluginInfo Source { get; internal set; }
/// <summary> /// <summary>
/// The load context used to load this plugin, null if it is a provider /// The load context used to load this plugin, null if it is a provider
/// </summary> /// </summary>
+1
View File
@@ -2,6 +2,7 @@ using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("BepInEx.Preloader.Core")] [assembly: InternalsVisibleTo("BepInEx.Preloader.Core")]
[assembly: InternalsVisibleTo("BepInEx.Unity.Mono")] [assembly: InternalsVisibleTo("BepInEx.Unity.Mono")]
[assembly: InternalsVisibleTo("BepInEx.NET.Common")]
[assembly: InternalsVisibleTo("BepInEx.NET.Framework.Launcher")] [assembly: InternalsVisibleTo("BepInEx.NET.Framework.Launcher")]
[assembly: InternalsVisibleTo("BepInEx.NET.CoreCLR")] [assembly: InternalsVisibleTo("BepInEx.NET.CoreCLR")]
[assembly: InternalsVisibleTo("BepInEx.Unity.IL2CPP")] [assembly: InternalsVisibleTo("BepInEx.Unity.IL2CPP")]
+14 -4
View File
@@ -1,10 +1,12 @@
using BepInEx.Bootstrap;
using BepInEx.Configuration; using BepInEx.Configuration;
using BepInEx.Contract;
using BepInEx.Logging; using BepInEx.Logging;
using HarmonyLib; using HarmonyLib;
namespace BepInEx.NET.Common namespace BepInEx.NET.Common
{ {
public abstract class BasePlugin public abstract class BasePlugin : IPlugin
{ {
protected BasePlugin() protected BasePlugin()
{ {
@@ -12,13 +14,21 @@ namespace BepInEx.NET.Common
HarmonyInstance = new Harmony("BepInEx.Plugin." + metadata.GUID); HarmonyInstance = new Harmony("BepInEx.Plugin." + metadata.GUID);
Log = Logger.CreateLogSource(metadata.Name); Info = BaseChainloader<BasePlugin>.GetPluginInfoFromGuid(metadata.GUID);
Info.Instance = this;
Logger = BepInEx.Logging.Logger.CreateLogSource(metadata.Name);
Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata); Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata);
} }
/// <inheritdoc />
public PluginInfo Info { get; }
/// <inheritdoc />
public ManualLogSource Logger { get; }
public ManualLogSource Log { get; } /// <inheritdoc />
public ConfigFile Config { get; } public ConfigFile Config { get; }
public Harmony HarmonyInstance { get; set; } public Harmony HarmonyInstance { get; set; }
@@ -1,21 +1,28 @@
using BepInEx.Configuration; using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Contract;
using BepInEx.Logging; using BepInEx.Logging;
using Il2CppInterop.Runtime.InteropTypes; using Il2CppInterop.Runtime.InteropTypes;
namespace BepInEx.Unity.IL2CPP; namespace BepInEx.Unity.IL2CPP;
public abstract class BasePlugin public abstract class BasePlugin : IPlugin
{ {
protected BasePlugin() protected BasePlugin()
{ {
var metadata = MetadataHelper.GetPluginMetadata(this); var metadata = MetadataHelper.GetPluginMetadata(this);
Log = Logger.CreateLogSource(metadata.Name); Info = BaseChainloader<BasePlugin>.GetPluginInfoFromGuid(metadata.GUID);
Info.Instance = this;
Logger = BepInEx.Logging.Logger.CreateLogSource(metadata.Name);
Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata); Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata);
} }
public ManualLogSource Log { get; } public PluginInfo Info { get; }
public ManualLogSource Logger { get; }
public ConfigFile Config { get; } public ConfigFile Config { get; }
@@ -1,5 +1,7 @@
using System; using System;
using BepInEx.Bootstrap;
using BepInEx.Configuration; using BepInEx.Configuration;
using BepInEx.Contract;
using BepInEx.Logging; using BepInEx.Logging;
using UnityEngine; using UnityEngine;
@@ -8,7 +10,7 @@ namespace BepInEx.Unity.Mono;
/// <summary> /// <summary>
/// The base plugin type that is used by the BepInEx plugin loader. /// The base plugin type that is used by the BepInEx plugin loader.
/// </summary> /// </summary>
public abstract class BaseUnityPlugin : MonoBehaviour public abstract class BaseUnityPlugin : MonoBehaviour, IPlugin
{ {
/// <summary> /// <summary>
/// Create a new instance of a plugin and all of its tied in objects. /// Create a new instance of a plugin and all of its tied in objects.
@@ -21,13 +23,8 @@ public abstract class BaseUnityPlugin : MonoBehaviour
throw new InvalidOperationException("Can't create an instance of " + GetType().FullName + throw new InvalidOperationException("Can't create an instance of " + GetType().FullName +
" because it inherits from BaseUnityPlugin and the BepInPlugin attribute is missing."); " because it inherits from BaseUnityPlugin and the BepInPlugin attribute is missing.");
Info = new PluginInfo Info = BaseChainloader<BaseUnityPlugin>.GetPluginInfoFromGuid(metadata.GUID);
{ Info.Instance = this;
Metadata = metadata,
Instance = this,
Dependencies = MetadataHelper.GetDependencies(GetType()),
Processes = MetadataHelper.GetAttributes<BepInProcess>(GetType()),
};
Logger = BepInEx.Logging.Logger.CreateLogSource(metadata.Name); Logger = BepInEx.Logging.Logger.CreateLogSource(metadata.Name);
@@ -42,7 +39,7 @@ public abstract class BaseUnityPlugin : MonoBehaviour
/// <summary> /// <summary>
/// Logger instance tied to this plugin. /// Logger instance tied to this plugin.
/// </summary> /// </summary>
protected ManualLogSource Logger { get; } public ManualLogSource Logger { get; }
/// <summary> /// <summary>
/// Default config file tied to this plugin. The config file will not be created until /// Default config file tied to this plugin. The config file will not be created until