Skip to content

Commit

Permalink
Use the new Patcher API so that the mod can be fully disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bures committed Jan 12, 2024
1 parent 5703a98 commit 5a16591
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
11 changes: 0 additions & 11 deletions src/PatchManager.PreloadPatcher/Directory.Build.props

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<PackageReference Include="BepInEx.BaseLib" Version="5.4.21"/>
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All"/>
<PackageReference Include="Mono.Cecil" Version="0.11.4"/>
<PackageReference Include="SpaceWarp" Version="1.8.0"/>
</ItemGroup>
</Project>
17 changes: 8 additions & 9 deletions src/PatchManager.PreloadPatcher/Patcher.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using BepInEx;
using BepInEx.Logging;
using JetBrains.Annotations;
using Mono.Cecil;
using Mono.Cecil.Cil;
using SpaceWarp.Preload.API;

namespace PatchManager.PreloadPatcher;

/// <summary>
/// Preload patcher for the game's AssetProvider.
/// </summary>
public static class Patcher
[UsedImplicitly]
internal class Patcher : BasePatcher
{
[UsedImplicitly]
public static IEnumerable<string> TargetDLLs { get; } = new[]
public override IEnumerable<string> DLLsToPatch { get; } = new[]
{
"Assembly-CSharp.dll"
};

private static MethodReference MakeGeneric(this MethodReference method, params GenericParameter[] args)
private static MethodReference MakeGeneric(MethodReference method, params GenericParameter[] args)
{
if (args.Length == 0)
{
Expand All @@ -43,8 +43,7 @@ private static MethodReference MakeGeneric(this MethodReference method, params G
/// </summary>
/// <param name="assemblyDefinition">Game assembly containing the AssetProvider class.</param>
/// <exception cref="Exception">Thrown if the assembly with the replacement method cannot be found.</exception>
[UsedImplicitly]
public static void Patch(ref AssemblyDefinition assemblyDefinition)
public override void ApplyPatch(ref AssemblyDefinition assemblyDefinition)
{
// Now we need to get the assembly with the replacement method
AssemblyDefinition coreAssembly = null;
Expand All @@ -67,14 +66,14 @@ public static void Patch(ref AssemblyDefinition assemblyDefinition)
// Remove every single instruction from the body of the methods
// Emit call to our extracted method
var methodInModule = targetMethod.Module.ImportReference(extractedMethod);
var generic = methodInModule.MakeGeneric(targetMethod.GenericParameters.ToArray());
var generic = MakeGeneric(methodInModule, targetMethod.GenericParameters.ToArray());
targetMethod.Body.Instructions.Clear();
targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_3));
targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, generic));
targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));

Logger.CreateLogSource("Patch Manager Preload").LogInfo("Pre-patching complete!");
Logger.LogInfo("Pre-patching complete!");
}
}

0 comments on commit 5a16591

Please sign in to comment.