From c85ece09fe4095030e821d67658fe184351e3302 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Sat, 25 Sep 2021 19:23:09 +0300 Subject: [PATCH] Delete hardpatcher Hardpatcher is not supported anymore and should not be used --- BepInEx.Bootstrap/BepInEx.Bootstrap.csproj | 19 -- BepInEx.Bootstrap/Entrypoint.cs | 37 ---- BepInEx.Bootstrap/Linker.cs | 16 -- BepInEx.Patcher/BepInEx.Patcher.csproj | 34 ---- BepInEx.Patcher/EmbeddedResource.cs | 21 --- BepInEx.Patcher/ILRepack.targets | 26 --- BepInEx.Patcher/Program.cs | 208 --------------------- BepInEx.sln | 24 --- 8 files changed, 385 deletions(-) delete mode 100644 BepInEx.Bootstrap/BepInEx.Bootstrap.csproj delete mode 100644 BepInEx.Bootstrap/Entrypoint.cs delete mode 100644 BepInEx.Bootstrap/Linker.cs delete mode 100644 BepInEx.Patcher/BepInEx.Patcher.csproj delete mode 100644 BepInEx.Patcher/EmbeddedResource.cs delete mode 100644 BepInEx.Patcher/ILRepack.targets delete mode 100644 BepInEx.Patcher/Program.cs diff --git a/BepInEx.Bootstrap/BepInEx.Bootstrap.csproj b/BepInEx.Bootstrap/BepInEx.Bootstrap.csproj deleted file mode 100644 index bb95a55..0000000 --- a/BepInEx.Bootstrap/BepInEx.Bootstrap.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - net35 - BepInEx.Bootstrap - BepInEx.Bootstrap - none - ..\bin\Patcher\ - 9 - false - - - - - - - - - - diff --git a/BepInEx.Bootstrap/Entrypoint.cs b/BepInEx.Bootstrap/Entrypoint.cs deleted file mode 100644 index 2086f61..0000000 --- a/BepInEx.Bootstrap/Entrypoint.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Reflection; - -namespace BepInEx.Bootstrap -{ - public static class Entrypoint - { - private static readonly string LocalDirectory = - Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); - - public static void Init() - { - AppDomain.CurrentDomain.AssemblyResolve += ResolveBepInEx; - - Linker.StartBepInEx(); - } - - private static Assembly ResolveBepInEx(object sender, ResolveEventArgs args) - { - var path = Path.Combine(LocalDirectory, $@"BepInEx\core\{new AssemblyName(args.Name).Name}.dll"); - - if (!File.Exists(path)) - return null; - - try - { - return Assembly.LoadFile(path); - } - catch (Exception) - { - return null; - } - } - } -} diff --git a/BepInEx.Bootstrap/Linker.cs b/BepInEx.Bootstrap/Linker.cs deleted file mode 100644 index cca2f75..0000000 --- a/BepInEx.Bootstrap/Linker.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Diagnostics; -using BepInEx.Unity.Bootstrap; - -namespace BepInEx.Bootstrap -{ - public static class Linker - { - public static void StartBepInEx() - { - var chainloader = new UnityChainloader(); - - chainloader.Initialize(Process.GetCurrentProcess().MainModule.FileName); - chainloader.Execute(); - } - } -} diff --git a/BepInEx.Patcher/BepInEx.Patcher.csproj b/BepInEx.Patcher/BepInEx.Patcher.csproj deleted file mode 100644 index 1e46d8b..0000000 --- a/BepInEx.Patcher/BepInEx.Patcher.csproj +++ /dev/null @@ -1,34 +0,0 @@ - - - Exe - net35 - true - BepInEx.Patcher - BepInEx.Patcher - embedded - true - ..\bin\Patcher\ - 9 - MinimumRecommendedRules.ruleset - false - - - BepInEx.Patcher.Program - - - - - - - - - - - - - - - - - - diff --git a/BepInEx.Patcher/EmbeddedResource.cs b/BepInEx.Patcher/EmbeddedResource.cs deleted file mode 100644 index 2abacf5..0000000 --- a/BepInEx.Patcher/EmbeddedResource.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Reflection; - -namespace BepInEx.Patcher -{ - internal static class EmbeddedResource - { - public static byte[] Get(string resourceName) - { - using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) - { - var length = (int) stream.Length; - - var buffer = new byte[length]; - - stream.Read(buffer, 0, length); - - return buffer; - } - } - } -} diff --git a/BepInEx.Patcher/ILRepack.targets b/BepInEx.Patcher/ILRepack.targets deleted file mode 100644 index 071b3b5..0000000 --- a/BepInEx.Patcher/ILRepack.targets +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/BepInEx.Patcher/Program.cs b/BepInEx.Patcher/Program.cs deleted file mode 100644 index e21bbfc..0000000 --- a/BepInEx.Patcher/Program.cs +++ /dev/null @@ -1,208 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Threading; -using Mono.Cecil; -using Mono.Cecil.Cil; -using MethodAttributes = Mono.Cecil.MethodAttributes; - -namespace BepInEx.Patcher -{ - internal class Program - { - private static void WriteError(string message) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Failed"); - Console.ResetColor(); - Console.WriteLine(message); - Console.WriteLine(); - } - - private static void WriteSuccess() - { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("Success"); - Console.ResetColor(); - } - - private static string GetUnityEngineAssembly(string managedDir) - { - var path = Path.Combine(managedDir, "UnityEngine.CoreModule.dll"); - if (File.Exists(path)) - return path; - return Path.Combine(managedDir, "UnityEngine.dll"); - } - - private static void Main(string[] args) - { - Console.WriteLine($"BepInEx Patcher v{Assembly.GetExecutingAssembly().GetName().Version}"); - - if (args.Length >= 1) //short circuit for specific dll patch - Environment.Exit(PatchUnityExe(Path.GetDirectoryName(args[0]), args[0], out var message) ? 0 : 9999); - - var hasFound = false; - var hasFailure = false; - var patchCount = 0; - - foreach (var exePath in Directory.GetFiles(Directory.GetCurrentDirectory())) - { - var gameName = Path.GetFileNameWithoutExtension(exePath); - - var managedDir = Environment.CurrentDirectory + $@"\{gameName}_Data\Managed"; - - var unityOutputDLL = GetUnityEngineAssembly(managedDir); - - if (!Directory.Exists(managedDir) || !File.Exists(unityOutputDLL)) - continue; - - hasFound = true; - - Console.Write($"Patching {gameName}... "); - - if (PatchUnityExe(managedDir, unityOutputDLL, out var message)) - { - WriteSuccess(); - patchCount++; - } - else - { - WriteError(message); - hasFailure = true; - } - } - - Console.WriteLine(); - - if (!hasFound) - Console.WriteLine("Didn't find any games to patch! Exiting."); - else - Console.WriteLine($"Patched {patchCount} assemblies!"); - - if (hasFailure) - { - Console.WriteLine("Press any key to continue..."); - Console.ReadKey(); - } - else - { - Thread.Sleep(3000); - } - } - - private static bool PatchUnityExe(string managedDir, string unityOutputDLL, out string message) - { - message = null; - - try - { - var injectedDLL = Path.GetFullPath($"{managedDir}\\BepInEx.Bootstrap.dll"); - File.WriteAllBytes(injectedDLL, EmbeddedResource.Get("BepInEx.Patcher.BepInEx.Bootstrap.dll")); - - var defaultResolver = new DefaultAssemblyResolver(); - defaultResolver.AddSearchDirectory(managedDir); - var rp = new ReaderParameters - { - AssemblyResolver = defaultResolver - }; - - var unityBackupDLL = $"{GetUnityEngineAssembly(managedDir)}.bak"; - - //determine which assembly to use as a base - var unity = AssemblyDefinition.ReadAssembly(unityOutputDLL, rp); - - if (!VerifyAssembly(unity, out message)) - { - //try and fall back to .bak if exists - if (File.Exists(unityBackupDLL)) - { - unity.Dispose(); - unity = AssemblyDefinition.ReadAssembly(unityBackupDLL, rp); - - if (!VerifyAssembly(unity, out message)) - { - //can't use anything - unity.Dispose(); - message += "\r\nThe backup is not usable."; - return false; - } - } - else - { - //can't use anything - unity.Dispose(); - message += "\r\nNo backup exists."; - return false; - } - } - else - { - //make a backup of the assembly - File.Copy(unityOutputDLL, unityBackupDLL, true); - unity.Dispose(); - unity = AssemblyDefinition.ReadAssembly(unityBackupDLL, rp); - } - - //patch - using (unity) - using (var injected = AssemblyDefinition.ReadAssembly(injectedDLL, rp)) - { - InjectAssembly(unity, injected); - - unity.Write(unityOutputDLL); - } - } - catch (Exception e) - { - message = e.ToString(); - return false; - } - - return true; - } - - private static void InjectAssembly(AssemblyDefinition unity, AssemblyDefinition injected) - { - //Entry point - var originalInjectMethod = injected.MainModule.Types.First(x => x.Name == "Entrypoint") - .Methods.First(x => x.Name == "Init"); - - var injectMethod = unity.MainModule.ImportReference(originalInjectMethod); - - var sceneManager = unity.MainModule.Types.First(x => x.Name == "Application"); - - var voidType = unity.MainModule.ImportReference(typeof(void)); - var cctor = new MethodDefinition(".cctor", - MethodAttributes.Static - | MethodAttributes.Private - | MethodAttributes.HideBySig - | MethodAttributes.SpecialName - | MethodAttributes.RTSpecialName, - voidType); - - var ilp = cctor.Body.GetILProcessor(); - ilp.Append(ilp.Create(OpCodes.Call, injectMethod)); - ilp.Append(ilp.Create(OpCodes.Ret)); - - sceneManager.Methods.Add(cctor); - } - - private static bool VerifyAssembly(AssemblyDefinition unity, out string message) - { - var canPatch = true; - message = ""; - - //check if already patched - if (unity.MainModule.AssemblyReferences.Any(x => x.Name.Contains("BepInEx"))) - { - canPatch = false; - - message += "This assembly has already been patched by BepInEx.\r\n"; - } - - message = message.Trim(); - return canPatch; - } - } -} diff --git a/BepInEx.sln b/BepInEx.sln index 5f7ed6b..83f6236 100644 --- a/BepInEx.sln +++ b/BepInEx.sln @@ -3,16 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.28922.388 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BepInEx.Patcher", "BepInEx.Patcher\BepInEx.Patcher.csproj", "{DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}" - ProjectSection(ProjectDependencies) = postProject - {4FFBA620-F5ED-47F9-B90C-DAD1316FD9B9} = {4FFBA620-F5ED-47F9-B90C-DAD1316FD9B9} - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB} = {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB} - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BepInEx.Bootstrap", "BepInEx.Bootstrap\BepInEx.Bootstrap.csproj", "{6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Patcher", "Patcher", "{A9071994-3533-4C1B-89DC-D817B676AB41}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BepInExTests", "BepInExTests\BepInExTests.csproj", "{E7CD429A-D057-48E3-8C51-E5C934E8E07B}" ProjectSection(ProjectDependencies) = postProject {4FFBA620-F5ED-47F9-B90C-DAD1316FD9B9} = {4FFBA620-F5ED-47F9-B90C-DAD1316FD9B9} @@ -61,18 +51,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}.Release_NetFramework|Any CPU.ActiveCfg = Release|Any CPU - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}.Release_Unity|Any CPU.ActiveCfg = Release|Any CPU - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7}.Release|Any CPU.Build.0 = Release|Any CPU - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release_NetFramework|Any CPU.ActiveCfg = Release|Any CPU - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release_Unity|Any CPU.ActiveCfg = Release|Any CPU - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release|Any CPU.Build.0 = Release|Any CPU {E7CD429A-D057-48E3-8C51-E5C934E8E07B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E7CD429A-D057-48E3-8C51-E5C934E8E07B}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7CD429A-D057-48E3-8C51-E5C934E8E07B}.Release_NetFramework|Any CPU.ActiveCfg = Release|Any CPU @@ -128,8 +106,6 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {DC89F18B-235B-4C01-AB31-AF40DCE5C4C7} = {A9071994-3533-4C1B-89DC-D817B676AB41} - {6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB} = {A9071994-3533-4C1B-89DC-D817B676AB41} {EAE9FAE6-8011-45A3-8B6E-0C7F14210533} = {6E2DD21E-0854-4F4A-B925-D90E0016D03D} {D404C973-441D-48ED-B266-C21320BA0D87} = {6E2DD21E-0854-4F4A-B925-D90E0016D03D} {490B052B-7F23-4052-AD04-613856618901} = {6EC98884-A3DC-4828-85EF-5F10F7519429}