mirror of
https://github.com/BepInEx/BepInEx.git
synced 2026-10-20 01:46:13 +00:00
address code review round 3
This commit is contained in:
1 parent
b729aee545
commit
e2396d202b
10 files changed
+97
-18
No files matched your search
@@ -116,18 +116,6 @@ 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>
|
|
||||||
/// <param name="foundPluginInfo">When this method returns, contains the <see cref="PluginInfo"/>
|
|
||||||
/// associated with the specified GUID, if the plugin was loaded; otherwise, null. This parameter is passed uninitialized.</param>
|
|
||||||
/// <returns>true if the plugin with the GUID was loaded by the chainloader; otherwise, false.</returns>
|
|
||||||
public bool TryGetPluginInfoFromGuid(string pluginGuid, out PluginInfo foundPluginInfo)
|
|
||||||
{
|
|
||||||
return Plugins.TryGetValue(pluginGuid, out foundPluginInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Contract
|
#region Contract
|
||||||
|
|
||||||
protected virtual string ConsoleTitle => $"BepInEx {Paths.BepInExVersion} - {Paths.ProcessName}";
|
protected virtual string ConsoleTitle => $"BepInEx {Paths.BepInExVersion} - {Paths.ProcessName}";
|
||||||
@@ -409,7 +397,9 @@ public abstract class BaseChainloader<TPlugin>
|
|||||||
}
|
}
|
||||||
else if (!provider && !loadedAssemblies.TryGetValue(pluginInfo.LoadContext.AssemblyIdentifier, out ass))
|
else if (!provider && !loadedAssemblies.TryGetValue(pluginInfo.LoadContext.AssemblyIdentifier, out ass))
|
||||||
{
|
{
|
||||||
ass = Assembly.Load(pluginInfo.LoadContext.GetAssemblyData());
|
var symbols = pluginInfo.LoadContext.GetAssemblySymbolsData();
|
||||||
|
ass = symbols != null ? Assembly.Load(pluginInfo.LoadContext.GetAssemblyData(), symbols)
|
||||||
|
: Assembly.Load(pluginInfo.LoadContext.GetAssemblyData());
|
||||||
loadedAssemblies[pluginInfo.LoadContext.AssemblyIdentifier] = ass;
|
loadedAssemblies[pluginInfo.LoadContext.AssemblyIdentifier] = ass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,12 @@ public interface IPluginLoadContext : IDisposable
|
|||||||
/// <returns>The assembly's raw data in bytes</returns>
|
/// <returns>The assembly's raw data in bytes</returns>
|
||||||
public byte[] GetAssemblyData();
|
public byte[] GetAssemblyData();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Obtains the assembly's symbols data which will be loaded by the chainloader alongside the assembly
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The assembly's symbols data in bytes (either in portable pdb or mdb format). Null if no symbols exists for this assembly</returns>
|
||||||
|
public byte[] GetAssemblySymbolsData();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Obtains a file's raw data using a relative path from the plugin assembly
|
/// Obtains a file's raw data using a relative path from the plugin assembly
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -221,6 +221,55 @@ public static class Utility
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Try to resolve the symbols data from an assembly DLL.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dllPath">File path of the DLL assembly.</param>
|
||||||
|
/// <param name="symbolsData">The symbols of the DLL assembly.</param>
|
||||||
|
/// <returns>True, if the symbols were found. Otherwise, false.</returns>
|
||||||
|
public static bool TryResolveDllSymbols(string dllPath, out byte[] symbolsData)
|
||||||
|
{
|
||||||
|
symbolsData = null;
|
||||||
|
|
||||||
|
var subDirectory = Path.GetDirectoryName(dllPath);
|
||||||
|
var filename = Path.GetFileNameWithoutExtension(dllPath);
|
||||||
|
|
||||||
|
var potentialSymbolsFileNames = new[]
|
||||||
|
{
|
||||||
|
$"{filename}.mdb",
|
||||||
|
$"{filename}.pdb"
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var symbolFileName in potentialSymbolsFileNames)
|
||||||
|
{
|
||||||
|
var symbolPath = Path.Combine(subDirectory, symbolFileName);
|
||||||
|
if (!File.Exists(symbolPath))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
symbolsData = File.ReadAllBytes(symbolPath);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Assembly LoadAssemblyWithSymbols(string assemblyFilePath)
|
||||||
|
{
|
||||||
|
var assemblyData = File.ReadAllBytes(assemblyFilePath);
|
||||||
|
|
||||||
|
return TryResolveDllSymbols(assemblyFilePath, out byte[] symbolsData)
|
||||||
|
? Assembly.Load(assemblyData, symbolsData)
|
||||||
|
: Assembly.Load(assemblyData);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks whether a given cecil type definition is a subtype of a provided type.
|
/// Checks whether a given cecil type definition is a subtype of a provided type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -259,6 +308,16 @@ public static class Utility
|
|||||||
TryResolveDllAssembly(assemblyName, directory,
|
TryResolveDllAssembly(assemblyName, directory,
|
||||||
s => AssemblyDefinition.ReadAssembly(s, readerParameters), out assembly);
|
s => AssemblyDefinition.ReadAssembly(s, readerParameters), out assembly);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Try to resolve and load the given assembly DLL including their symbols file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assemblyName">Name of the assembly, of the type <see cref="AssemblyName" />.</param>
|
||||||
|
/// <param name="directory">Directory to search the assembly from.</param>
|
||||||
|
/// <param name="assembly">The loaded assembly.</param>
|
||||||
|
/// <returns>True, if the assembly was found and loaded. Otherwise, false.</returns>
|
||||||
|
public static bool TryResolveDllAssemblyWithSymbols(AssemblyName assemblyName, string directory, out Assembly assembly) =>
|
||||||
|
TryResolveDllAssembly(assemblyName, directory, LoadAssemblyWithSymbols, out assembly);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to create a file with the given name
|
/// Tries to create a file with the given name
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ public class BepInExPatcherLoadContext : IPluginLoadContext
|
|||||||
public string AssemblyIdentifier { get; internal set; }
|
public string AssemblyIdentifier { get; internal set; }
|
||||||
public string AssemblyHash { get; internal set; }
|
public string AssemblyHash { get; internal set; }
|
||||||
private byte[] assemblyData;
|
private byte[] assemblyData;
|
||||||
|
private byte[] assemblySymbolsData;
|
||||||
public byte[] GetAssemblyData()
|
public byte[] GetAssemblyData()
|
||||||
{
|
{
|
||||||
if (assemblyData == null)
|
if (assemblyData == null)
|
||||||
@@ -17,6 +18,17 @@ public class BepInExPatcherLoadContext : IPluginLoadContext
|
|||||||
|
|
||||||
return assemblyData;
|
return assemblyData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] GetAssemblySymbolsData()
|
||||||
|
{
|
||||||
|
if (assemblySymbolsData == null)
|
||||||
|
{
|
||||||
|
if (!Utility.TryResolveDllSymbols(AssemblyIdentifier, out assemblySymbolsData))
|
||||||
|
assemblySymbolsData = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return assemblySymbolsData;
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] GetFile(string relativePath)
|
public byte[] GetFile(string relativePath)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class BepInExPatcherProvider : BasePatcherProvider
|
|||||||
if (!AssemblyLocationsByFilename.TryGetValue(name, out var location))
|
if (!AssemblyLocationsByFilename.TryGetValue(name, out var location))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!Utility.TryResolveDllAssembly(new(name), location, out var ass))
|
if (!Utility.TryResolveDllAssemblyWithSymbols(new(name), location, out var ass))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return ass;
|
return ass;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ public class BepInExPluginLoadContext : IPluginLoadContext
|
|||||||
public string AssemblyIdentifier { get; internal set; }
|
public string AssemblyIdentifier { get; internal set; }
|
||||||
public string AssemblyHash { get; internal set; }
|
public string AssemblyHash { get; internal set; }
|
||||||
private byte[] assemblyData;
|
private byte[] assemblyData;
|
||||||
|
private byte[] assemblySymbolsData;
|
||||||
public byte[] GetAssemblyData()
|
public byte[] GetAssemblyData()
|
||||||
{
|
{
|
||||||
if (assemblyData == null)
|
if (assemblyData == null)
|
||||||
@@ -18,6 +19,17 @@ public class BepInExPluginLoadContext : IPluginLoadContext
|
|||||||
return assemblyData;
|
return assemblyData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] GetAssemblySymbolsData()
|
||||||
|
{
|
||||||
|
if (assemblySymbolsData == null)
|
||||||
|
{
|
||||||
|
if (!Utility.TryResolveDllSymbols(AssemblyIdentifier, out assemblySymbolsData))
|
||||||
|
assemblySymbolsData = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return assemblySymbolsData;
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] GetFile(string relativePath)
|
public byte[] GetFile(string relativePath)
|
||||||
{
|
{
|
||||||
if (relativePath == null)
|
if (relativePath == null)
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class BepInExPluginProvider : BasePluginProvider
|
|||||||
if (!AssemblyLocationsByFilename.TryGetValue(name, out var location))
|
if (!AssemblyLocationsByFilename.TryGetValue(name, out var location))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!Utility.TryResolveDllAssembly(new(name), location, out var ass))
|
if (!Utility.TryResolveDllAssemblyWithSymbols(new(name), location, out var ass))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return ass;
|
return ass;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace BepInEx.NET.Common
|
|||||||
|
|
||||||
HarmonyInstance = new Harmony("BepInEx.Plugin." + metadata.GUID);
|
HarmonyInstance = new Harmony("BepInEx.Plugin." + metadata.GUID);
|
||||||
|
|
||||||
if (BaseChainloader<BasePlugin>.Instance.TryGetPluginInfoFromGuid(metadata.GUID, out var pluginInfo))
|
if (BaseChainloader<BasePlugin>.Instance.Plugins.TryGetValue(metadata.GUID, out var pluginInfo))
|
||||||
{
|
{
|
||||||
Info = pluginInfo;
|
Info = pluginInfo;
|
||||||
Info.Instance = this;
|
Info.Instance = this;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public abstract class BasePlugin : IPlugin
|
|||||||
{
|
{
|
||||||
var metadata = MetadataHelper.GetPluginMetadata(this);
|
var metadata = MetadataHelper.GetPluginMetadata(this);
|
||||||
|
|
||||||
if (BaseChainloader<BasePlugin>.Instance.TryGetPluginInfoFromGuid(metadata.GUID, out var pluginInfo))
|
if (BaseChainloader<BasePlugin>.Instance.Plugins.TryGetValue(metadata.GUID, out var pluginInfo))
|
||||||
{
|
{
|
||||||
Info = pluginInfo;
|
Info = pluginInfo;
|
||||||
Info.Instance = this;
|
Info.Instance = this;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public abstract class BaseUnityPlugin : MonoBehaviour, IPlugin
|
|||||||
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.");
|
||||||
|
|
||||||
if (BaseChainloader<BaseUnityPlugin>.Instance.TryGetPluginInfoFromGuid(metadata.GUID, out var pluginInfo))
|
if (BaseChainloader<BaseUnityPlugin>.Instance.Plugins.TryGetValue(metadata.GUID, out var pluginInfo))
|
||||||
{
|
{
|
||||||
Info = pluginInfo;
|
Info = pluginInfo;
|
||||||
Info.Instance = this;
|
Info.Instance = this;
|
||||||
|
|||||||
Reference in new issue
Block a user