Skip to content

Commit

Permalink
Merge pull request #1 from cheese3660/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
cheese3660 committed Jan 15, 2024
2 parents 230c4dd + 74dbe39 commit ecde7ff
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
22 changes: 19 additions & 3 deletions Premonition.BepInEx/BepInExPremonitionManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
using Mono.Cecil;
using System.Reflection;
using BepInEx;
using Mono.Cecil;
using Premonition.Core;
using Premonition.Core.Utility;

namespace Premonition.BepInEx;

internal class BepInExPremonitionManager
{
private static IAssemblyResolver GetResolver()
{
var resolver = new DefaultAssemblyResolver();
HashSet<string> searchDirectories = [];
foreach (var dll in Directory.EnumerateFiles(Paths.GameRootPath, "*.dll", SearchOption.AllDirectories))
{
var dllPath = new FileInfo(dll).Directory!.FullName;
searchDirectories.Add(dllPath);
}
foreach (var directory in searchDirectories)
{
resolver.AddSearchDirectory(directory);
}
return resolver;
}
private PremonitionManager? _manager;
private PremonitionManager Manager => _manager ??= new PremonitionManager();
private PremonitionManager Manager => _manager ??= new PremonitionManager(GetResolver());

// ReSharper disable once InconsistentNaming
internal HashSet<string>? TargetDLLs;
Expand All @@ -29,7 +46,6 @@ internal void RegisterRuntimePremonition()
TargetDLLs.Add(patcher);
}
}

internal void Patch(AssemblyDefinition def)
{
Manager.Patch(def);
Expand Down
12 changes: 10 additions & 2 deletions Premonition.Core/PremonitionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Premonition.Core;
/// <summary>
/// This is the main manager class for premonition patching
/// </summary>
public class PremonitionManager
public class PremonitionManager(IAssemblyResolver? resolver = null)
{

/// <summary>
Expand All @@ -22,7 +22,10 @@ public class PremonitionManager
/// <param name="dllPath">Said dll file</param>
public void ReadAssembly(string dllPath)
{
var assemblyDefinition = AssemblyDefinition.ReadAssembly(dllPath);
var assemblyDefinition = AssemblyDefinition.ReadAssembly(dllPath, new ReaderParameters
{
AssemblyResolver = resolver
});
RegisterAssembly(assemblyDefinition);
}

Expand Down Expand Up @@ -100,11 +103,16 @@ private void RegisterMethod(MethodDefinition method, string? assemblyName, strin
var argumentTypes = GetArgumentTypes(method);


// ReSharper disable once ConvertIfStatementToSwitchStatement
if (hadPremonitionAttribute && missingAttributes.Count > 0)
{
LogError(
$"Patch method {method.FullName} is missing the following necessary attributes: {string.Join(", ", missingAttributes)}, this method will not be used");
return;
}
if (!hadPremonitionAttribute)
{
return;
}

PremonitionPatchers.Add(new PremonitionPatcher(assemblyName,typeName,methodName!,argumentTypes,patchType!.Value,method));
Expand Down
12 changes: 6 additions & 6 deletions Premonition.Core/PremonitionPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ private static void PrefixPatch(MethodDefinition methodBeingPatched, MethodRefer
foreach (var argument in patchMethod.Parameters)
{
var argumentInModule = patchMethodInModule.Parameters[argIndex];
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (argument.Name == "__instance")
{
argumentIndices.Add(0);
}

if (argument.Name == "__retVal")
else if (argument.Name == "__retVal")
{
if (!argument.IsOut)
{
Expand Down Expand Up @@ -234,7 +234,7 @@ private static void PrefixPatch(MethodDefinition methodBeingPatched, MethodRefer
{
if (methodBeingPatched.Parameters[i].Name != argument.Name) continue;
found = true;
argumentIndices.Add(i);
argumentIndices.Add(methodBeingPatched.IsStatic ? i : i+1);
break;
}
if (found) continue;
Expand Down Expand Up @@ -587,7 +587,7 @@ private static void PostfixPatchTailCall(MethodDefinition methodBeingPatched, Me
{
if (methodBeingPatched.Parameters[i].Name != argument.Name) continue;
found = true;
argumentIndices.Add(i);
argumentIndices.Add(methodBeingPatched.IsStatic ? i : i+1);
break;
}
if (found) continue;
Expand Down Expand Up @@ -680,7 +680,7 @@ private static void PostfixPatchVoid(MethodDefinition methodBeingPatched, Method
{
if (methodBeingPatched.Parameters[i].Name != argument.Name) continue;
found = true;
argumentIndices.Add(i);
argumentIndices.Add(methodBeingPatched.IsStatic ? i : i+1);
break;
}
if (found) continue;
Expand Down Expand Up @@ -762,7 +762,7 @@ private static void TrampolinePatch(MethodDefinition methodBeingPatched, MethodR
{
if (methodBeingPatched.Parameters[i].Name != argument.Name) continue;
found = true;
argumentIndices.Add(i);
argumentIndices.Add(methodBeingPatched.IsStatic ? i : i+1);
break;
}
if (found) continue;
Expand Down
4 changes: 3 additions & 1 deletion Premonition.SpaceWarp/PremonitionEntrypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public PremonitionEntrypoint()

_manager = new SpaceWarpPremonitionManager();


var disabledPluginGuids = File.ReadAllLines(CommonPaths.DisabledPluginsFilepath);

var swinfoPaths = Directory
Expand Down Expand Up @@ -56,8 +57,9 @@ public PremonitionEntrypoint()
_manager.Read(dll);
}
}
catch (Exception)
catch (Exception e)
{
_manager.LogSource.LogError(e);
// ignore
}
}
Expand Down
34 changes: 28 additions & 6 deletions Premonition.SpaceWarp/SpaceWarpPremonitionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BepInEx.Logging;
using System.Reflection;
using BepInEx;
using BepInEx.Logging;
using Mono.Cecil;
using Premonition.Core;
using Premonition.Core.Utility;
Expand All @@ -8,22 +10,42 @@ namespace Premonition.SpaceWarp;

internal class SpaceWarpPremonitionManager
{
private PremonitionManager _premonitionManager = new();
private static IAssemblyResolver GetResolver()
{
var resolver = new DefaultAssemblyResolver();
HashSet<string> searchDirectories = [];
foreach (var dll in Directory.EnumerateFiles(Paths.GameRootPath, "*.dll", SearchOption.AllDirectories))
{
var dllPath = new FileInfo(dll).Directory!.FullName;
searchDirectories.Add(dllPath);
}
foreach (var directory in searchDirectories)
{
resolver.AddSearchDirectory(directory);
}
return resolver;
}

private readonly PremonitionManager _premonitionManager = new(GetResolver());

internal IEnumerable<string> ToPatch =>
_premonitionManager.PremonitionPatchers.Select(x => x.Assembly + ".dll");

internal ManualLogSource LogSource;


internal SpaceWarpPremonitionManager()
{
var logSource = Logger.CreateLogSource("Premonition.SpaceWarp");
Logging.Listeners.Add(ILogListener.CreateListener(logSource.LogDebug, logSource.LogInfo, logSource.LogWarning,
logSource.LogError));
LogSource = Logger.CreateLogSource("Premonition.SpaceWarp");
Logging.Listeners.Add(ILogListener.CreateListener(LogSource.LogDebug, LogSource.LogInfo, LogSource.LogWarning,
LogSource.LogError));
}

internal void Read(string dll)
{
_premonitionManager.ReadAssembly(dll);
}

internal void Apply(AssemblyDefinition definition)
{
_premonitionManager.Patch(definition);
Expand Down

0 comments on commit ecde7ff

Please sign in to comment.