Skip to content

Commit

Permalink
Add support for BepInEx.MultiFolderLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
harbingerofme committed Dec 19, 2020
1 parent 7cc32fe commit 5924575
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
9 changes: 9 additions & 0 deletions LighterPatcher/LighterPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.10.4" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="BepInEx">
<HintPath>..\libs\BepInEx.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="BepInEx.MultiFolderLoader">
<HintPath>libs\BepInEx.MultiFolderLoader.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="BepInEx.Preloader">
<HintPath>libs\BepInEx.Preloader.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\libs\UnityEngine.dll</HintPath>
<Private>false</Private>
Expand Down
66 changes: 51 additions & 15 deletions LighterPatcher/LightestPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Reflection;
using TypeAttributes = Mono.Cecil.TypeAttributes;

namespace LighterPatcher
{
Expand All @@ -21,9 +24,39 @@ static class LightestPatcher
private static long hash;
internal static BepInEx.Logging.ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource("LighterPatcher");

private static List<string> PluginPaths;

public static void Initialize()
{
neededTypes = new List<string>();
PluginPaths = new List<string>
{
Paths.PluginPath
};
if (BepInEx.Preloader.Patching.AssemblyPatcher.PatcherPlugins.Any((x) =>
{
return x.TypeName == "BepInEx.MultiFolderLoader.MultiFolderLoader";
}))
{
Logger.LogMessage("BepInEx is running MultiFolderLoader");
GetPluginListsFromMultiFolder();
}

}


[MethodImpl(MethodImplOptions.NoInlining)]
private static void GetPluginListsFromMultiFolder()
{
var fieldInfo = typeof(BepInEx.MultiFolderLoader.ModManager).GetField("Mods", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
List<BepInEx.MultiFolderLoader.Mod> mods = (List<BepInEx.MultiFolderLoader.Mod>) fieldInfo.GetValue(null);
foreach(BepInEx.MultiFolderLoader.Mod mod in mods)
{
if(mod.PluginsPath != null)
{
PluginPaths.Add(mod.PluginsPath);
}
}
}

public static IEnumerable<string> TargetDLLs => TellBepinAbsolutelyNothingBecauseThePluginsFolderIsntManaged();
Expand All @@ -33,31 +66,34 @@ private static IEnumerable<string> TellBepinAbsolutelyNothingBecauseThePluginsFo
Logger.LogInfo($"Collecting information for new MMHook");
string oldHash = null;
int modsWithRefs = 0;
foreach (var pluginDll in Directory.GetFiles(Paths.PluginPath, "*.dll", SearchOption.AllDirectories))
foreach (string path in PluginPaths)
{
try
foreach (var pluginDll in Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories))
{
using (var ass = AssemblyDefinition.ReadAssembly(pluginDll))
try
{
if (ass.Name.Name == "MMHOOK_Assembly-CSharp")
using (var ass = AssemblyDefinition.ReadAssembly(pluginDll))
{
oldHash = CollectHash(ass);
if (ass.Name.Name == "MMHOOK_Assembly-CSharp")
{
oldHash = CollectHash(ass);

mmhLocation = pluginDll;
continue;
}
foreach (var refer in ass.MainModule.AssemblyReferences)
{
if (refer.Name == "MMHOOK_Assembly-CSharp")
mmhLocation = pluginDll;
continue;
}
foreach (var refer in ass.MainModule.AssemblyReferences)
{
CollectMethodDefinitions(ass);
modsWithRefs++;
break;
if (refer.Name == "MMHOOK_Assembly-CSharp")
{
CollectMethodDefinitions(ass);
modsWithRefs++;
break;
}
}
}
}
catch (Exception e) { Logger.LogError($"Error on: {pluginDll}"); Logger.LogError(e); }
}
catch (Exception e) { Logger.LogError($"Error on: {pluginDll}"); Logger.LogError(e); }
}

if (mmhLocation == null)
Expand Down

0 comments on commit 5924575

Please sign in to comment.