From 22336ffa91e66d793bf333c9c93638ab4adde661 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Fri, 21 Dec 2018 22:34:30 -0600 Subject: [PATCH] Completely removed old patching method --- IPA.Injector/Injector.cs | 14 -- IPA.Loader/Updating/ModSaber/ApiEndpoint.cs | 3 +- IPA/IPA.csproj | 2 - IPA/Patcher/Patcher.cs | 134 -------------- IPA/Patcher/Virtualizer.cs | 116 ------------ IPA/Program.cs | 171 +++--------------- .../Debug/IPA.csproj.CoreCompileInputs.cache | 2 +- 7 files changed, 30 insertions(+), 412 deletions(-) delete mode 100644 IPA/Patcher/Patcher.cs delete mode 100644 IPA/Patcher/Virtualizer.cs diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index f882f8a1..168fb5e4 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -14,7 +14,6 @@ using MethodAttributes = Mono.Cecil.MethodAttributes; namespace IPA.Injector { - [SuppressMessage("ReSharper", "UnusedMember.Global")] public static class Injector { // ReSharper disable once UnusedParameter.Global @@ -165,19 +164,6 @@ namespace IPA.Injector bootstrapper.Destroyed += Bootstrapper_Destroyed; } - private static bool _injected; - public static void Inject() - { - if (!_injected) - { - _injected = true; - WinConsole.Initialize(); - SetupLibraryLoading(); - var bootstrapper = new GameObject("Bootstrapper").AddComponent(); - bootstrapper.Destroyed += Bootstrapper_Destroyed; - } - } - private static bool _loadingDone; private static void SetupLibraryLoading() diff --git a/IPA.Loader/Updating/ModSaber/ApiEndpoint.cs b/IPA.Loader/Updating/ModSaber/ApiEndpoint.cs index cb3c9535..f941655c 100644 --- a/IPA.Loader/Updating/ModSaber/ApiEndpoint.cs +++ b/IPA.Loader/Updating/ModSaber/ApiEndpoint.cs @@ -120,8 +120,7 @@ namespace IPA.Updating.ModSaber public string Manifest; } - [JsonProperty("gameVersion"), - JsonConverter(typeof(SemverVersionConverter))] + [JsonProperty("gameVersion")] public GameVersionType GameVersion; #pragma warning restore CS0649 diff --git a/IPA/IPA.csproj b/IPA/IPA.csproj index bb828bf3..e1ddab0d 100644 --- a/IPA/IPA.csproj +++ b/IPA/IPA.csproj @@ -100,8 +100,6 @@ - - diff --git a/IPA/Patcher/Patcher.cs b/IPA/Patcher/Patcher.cs deleted file mode 100644 index a6119a61..00000000 --- a/IPA/Patcher/Patcher.cs +++ /dev/null @@ -1,134 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace IPA.Patcher -{ - internal class PatchedModule - { - private static readonly string[] EntryTypes = { "Input", "Display" }; - - private readonly FileInfo _file; - private ModuleDefinition _module; - - internal struct PatchData { - public bool IsPatched; - public Version Version; - } - - public static PatchedModule Load(string engineFile) - { - return new PatchedModule(engineFile); - } - - private PatchedModule(string engineFile) - { - _file = new FileInfo(engineFile); - - LoadModules(); - } - - private void LoadModules() - { - var resolver = new DefaultAssemblyResolver(); - resolver.AddSearchDirectory(_file.DirectoryName); - - var parameters = new ReaderParameters - { - AssemblyResolver = resolver, - }; - - _module = ModuleDefinition.ReadModule(_file.FullName, parameters); - } - - public PatchData Data - { - get - { - var data = new PatchData { IsPatched = false, Version = null }; - foreach (var @ref in _module.AssemblyReferences) { - switch (@ref.Name) - { - case "IllusionInjector": - case "IllusionPlugin": - data = new PatchData { IsPatched = true, Version = new Version(0, 0, 0, 0) }; - break; - case "IPA.Injector": - return new PatchData { IsPatched = true, Version = @ref.Version }; - } - } - return data; - } - } - - public void Patch(Version v) - { - // First, let's add the reference - var nameReference = new AssemblyNameReference("IPA.Injector", v); - var injectorPath = Path.Combine(_file.DirectoryName ?? throw new InvalidOperationException(), "IPA.Injector.dll"); - var injector = ModuleDefinition.ReadModule(injectorPath); - - bool hasIPAInjector = false; - for (int i = 0; i < _module.AssemblyReferences.Count; i++) - { - if (_module.AssemblyReferences[i].Name == "IllusionInjector") - _module.AssemblyReferences.RemoveAt(i--); - if (_module.AssemblyReferences[i].Name == "IllusionPlugin") - _module.AssemblyReferences.RemoveAt(i--); - if (_module.AssemblyReferences[i].Name == "IPA.Injector") - { - hasIPAInjector = true; - _module.AssemblyReferences[i].Version = v; - } - } - - if (!hasIPAInjector) - { - _module.AssemblyReferences.Add(nameReference); - - int patched = 0; - foreach (var type in FindEntryTypes()) - { - if (PatchType(type, injector)) - { - patched++; - } - } - - if (patched > 0) - { - _module.Write(_file.FullName); - } - else - { - throw new Exception("Could not find any entry type!"); - } - } - else - { - _module.Write(_file.FullName); - } - } - - private bool PatchType(TypeDefinition targetType, ModuleDefinition injector) - { - var targetMethod = targetType.Methods.FirstOrDefault(m => m.IsConstructor && m.IsStatic); - if (targetMethod != null) - { - var methodReference = _module.ImportReference(injector.GetType("IPA.Injector.Injector").Methods.First(m => m.Name == "Inject")); - targetMethod.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, methodReference)); - return true; - } - return false; - } - - - private IEnumerable FindEntryTypes() - { - return _module.GetTypes().Where(m => EntryTypes.Contains(m.Name)); - } - } -} diff --git a/IPA/Patcher/Virtualizer.cs b/IPA/Patcher/Virtualizer.cs deleted file mode 100644 index bfc3ce5b..00000000 --- a/IPA/Patcher/Virtualizer.cs +++ /dev/null @@ -1,116 +0,0 @@ -using Mono.Cecil; -using System; -using System.IO; -using System.Linq; - -namespace IPA.Patcher -{ - class VirtualizedModule - { - private readonly FileInfo _file; - private ModuleDefinition _module; - - public static VirtualizedModule Load(string engineFile) - { - return new VirtualizedModule(engineFile); - } - - private VirtualizedModule(string assemblyFile) - { - _file = new FileInfo(assemblyFile); - - LoadModules(); - } - - private void LoadModules() - { - var resolver = new DefaultAssemblyResolver(); - resolver.AddSearchDirectory(_file.DirectoryName); - - var parameters = new ReaderParameters - { - AssemblyResolver = resolver, - }; - - _module = ModuleDefinition.ReadModule(_file.FullName, parameters); - } - - /// - /// - /// - public void Virtualize() - { - - foreach (var type in _module.Types) - { - VirtualizeType(type); - } - Console.WriteLine(); - - _module.Write(_file.FullName); - } - - private void VirtualizeType(TypeDefinition type) - { - if(type.IsSealed) - { - // Unseal - type.IsSealed = false; - } - - if (type.IsInterface) return; - if (type.IsAbstract) return; - - // These two don't seem to work. - if (type.Name == "SceneControl" || type.Name == "ConfigUI") return; - - //Console.CursorTop--; - Console.CursorLeft = 0; - Program.ClearLine(); - Console.Write("Virtualizing {0}", type.Name); - // Take care of sub types - foreach (var subType in type.NestedTypes) - { - VirtualizeType(subType); - } - - foreach (var method in type.Methods) - { - if (method.IsManaged - && method.IsIL - && !method.IsStatic - && !method.IsVirtual - && !method.IsAbstract - && !method.IsAddOn - && !method.IsConstructor - && !method.IsSpecialName - && !method.IsGenericInstance - && !method.HasOverrides) - { - method.IsVirtual = true; - method.IsPublic = true; - method.IsPrivate = false; - method.IsNewSlot = true; - method.IsHideBySig = true; - } - } - - foreach (var field in type.Fields) - { - if (field.IsPrivate) field.IsFamily = true; - } - } - - public bool IsVirtualized - { - get - { - var awakeMethods = _module.GetTypes().SelectMany(t => t.Methods.Where(m => m.Name == "Awake")); - var methodDefinitions = awakeMethods as MethodDefinition[] ?? awakeMethods.ToArray(); - if (!methodDefinitions.Any()) return false; - - return ((float)methodDefinitions.Count(m => m.IsVirtual) / methodDefinitions.Count()) > 0.5f; - } - } - } -} diff --git a/IPA/Program.cs b/IPA/Program.cs index 68d3b376..120ad5f8 100644 --- a/IPA/Program.cs +++ b/IPA/Program.cs @@ -32,12 +32,12 @@ namespace IPA public static readonly ArgumentFlag ArgNoWait = new ArgumentFlag("--nowait", "-n") { DocString = "doesn't wait for user input after the operation" }; public static readonly ArgumentFlag ArgStart = new ArgumentFlag("--start", "-s") { DocString = "uses value_ as arguments to start the game after the patch/unpatch", ValueString = "ARGUMENTS" }; public static readonly ArgumentFlag ArgLaunch = new ArgumentFlag("--launch", "-l") { DocString = "uses positional parameters as arguments to start the game after patch/unpatch" }; - public static readonly ArgumentFlag ArgDestructive = new ArgumentFlag("--destructive", "-d") { DocString = "patches the game using the now outdated destructive methods" }; + //public static readonly ArgumentFlag ArgDestructive = new ArgumentFlag("--destructive", "-d") { DocString = "patches the game using the now outdated destructive methods" }; [STAThread] public static void Main(string[] args) { - Arguments.CmdLine.Flags(ArgHelp, ArgWaitFor, ArgForce, ArgRevert, ArgNoWait, ArgStart, ArgLaunch, ArgDestructive).Process(); + Arguments.CmdLine.Flags(ArgHelp, ArgWaitFor, ArgForce, ArgRevert, ArgNoWait, ArgStart, ArgLaunch/*, ArgDestructive*/).Process(); if (ArgHelp) { @@ -139,132 +139,35 @@ namespace IPA try { var backup = new BackupUnit(context); - - if (ArgDestructive) - { - #region Patch Version Check - - var patchedModule = PatchedModule.Load(context.EngineFile); -#if DEBUG - var isCurrentNewer = Version.CompareTo(patchedModule.Data.Version) >= 0; -#else - var isCurrentNewer = Version.CompareTo(patchedModule.Data.Version) > 0; -#endif - Console.WriteLine($"Current: {Version} Patched: {patchedModule.Data.Version}"); - if (isCurrentNewer) - { - Console.ForegroundColor = ConsoleColor.White; - Console.WriteLine( - $"Preparing for update, {(patchedModule.Data.Version == null ? "UnPatched" : patchedModule.Data.Version.ToString())} => {Version}"); - Console.WriteLine("--- Starting ---"); - Revert(context); - Console.ResetColor(); - - #region File Copying - - Console.ForegroundColor = ConsoleColor.Magenta; - Console.WriteLine("Updating files... "); - var nativePluginFolder = Path.Combine(context.DataPathDst, "Plugins"); - bool isFlat = Directory.Exists(nativePluginFolder) && - Directory.GetFiles(nativePluginFolder).Any(f => f.EndsWith(".dll")); - bool force = !BackupManager.HasBackup(context) || ArgForce; - var architecture = DetectArchitecture(context.Executable); - - Console.WriteLine("Architecture: {0}", architecture); - - CopyAll(new DirectoryInfo(context.DataPathSrc), new DirectoryInfo(context.DataPathDst), force, - backup, - (from, to) => NativePluginInterceptor(from, to, new DirectoryInfo(nativePluginFolder), isFlat, - architecture)); - CopyAll(new DirectoryInfo(context.LibsPathSrc), new DirectoryInfo(context.LibsPathDst), force, - backup, - (from, to) => NativePluginInterceptor(from, to, new DirectoryInfo(nativePluginFolder), isFlat, - architecture)); - - Console.WriteLine("Successfully updated files!"); - - #endregion - } - else - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine($"Files up to date @ Version {Version}!"); - Console.ResetColor(); - } - - #endregion - - #region Patching - - if (!patchedModule.Data.IsPatched || isCurrentNewer) - { - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($"Patching UnityEngine.dll with Version {Application.ProductVersion}... "); - backup.Add(context.EngineFile); - patchedModule.Patch(Version); - Console.WriteLine("Done!"); - Console.ResetColor(); - } - - #endregion - - #region Creating shortcut - if (!File.Exists(context.ShortcutPath)) - { - Console.ForegroundColor = ConsoleColor.DarkGreen; - Console.WriteLine("Creating shortcut to IPA ({0})... ", context.IPA); - try - { - Shortcut.Create( - fileName: context.ShortcutPath, - targetPath: context.IPA, - arguments: Args(context.Executable, "-ln"), - workingDirectory: context.ProjectRoot, - description: "Launches the game and makes sure it's in a patched state", - hotkey: "", - iconPath: context.Executable - ); - } - catch (Exception) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.Error.WriteLine("Failed to create shortcut, but game was patched!"); - } - Console.ResetColor(); - } - #endregion - } - else - { - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine("Restoring old version... "); - if (BackupManager.HasBackup(context)) - BackupManager.Restore(context); - - var nativePluginFolder = Path.Combine(context.DataPathDst, "Plugins"); - bool isFlat = Directory.Exists(nativePluginFolder) && - Directory.GetFiles(nativePluginFolder).Any(f => f.EndsWith(".dll")); - bool force = !BackupManager.HasBackup(context) || ArgForce; - var architecture = DetectArchitecture(context.Executable); - - Console.ForegroundColor = ConsoleColor.DarkCyan; - Console.WriteLine("Installing files... "); - - CopyAll(new DirectoryInfo(context.DataPathSrc), new DirectoryInfo(context.DataPathDst), force, - backup, - (from, to) => NativePluginInterceptor(from, to, new DirectoryInfo(nativePluginFolder), isFlat, - architecture)); - CopyAll(new DirectoryInfo(context.LibsPathSrc), new DirectoryInfo(context.LibsPathDst), force, - backup, - (from, to) => NativePluginInterceptor(from, to, new DirectoryInfo(nativePluginFolder), isFlat, - architecture)); - CopyAll(new DirectoryInfo(context.IPARoot), new DirectoryInfo(context.ProjectRoot), force, - backup, - null, false); + + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("Restoring old version... "); + if (BackupManager.HasBackup(context)) + BackupManager.Restore(context); + + var nativePluginFolder = Path.Combine(context.DataPathDst, "Plugins"); + bool isFlat = Directory.Exists(nativePluginFolder) && + Directory.GetFiles(nativePluginFolder).Any(f => f.EndsWith(".dll")); + bool force = !BackupManager.HasBackup(context) || ArgForce; + var architecture = DetectArchitecture(context.Executable); + + Console.ForegroundColor = ConsoleColor.DarkCyan; + Console.WriteLine("Installing files... "); + + CopyAll(new DirectoryInfo(context.DataPathSrc), new DirectoryInfo(context.DataPathDst), force, + backup, + (from, to) => NativePluginInterceptor(from, to, new DirectoryInfo(nativePluginFolder), isFlat, + architecture)); + CopyAll(new DirectoryInfo(context.LibsPathSrc), new DirectoryInfo(context.LibsPathDst), force, + backup, + (from, to) => NativePluginInterceptor(from, to, new DirectoryInfo(nativePluginFolder), isFlat, + architecture)); + CopyAll(new DirectoryInfo(context.IPARoot), new DirectoryInfo(context.ProjectRoot), force, + backup, + null, false); //backup.Add(context.AssemblyFile); //backup.Add(context.EngineFile); - } #region Create Plugin Folder @@ -278,24 +181,6 @@ namespace IPA #endregion - #region Virtualizing - - if (ArgDestructive && File.Exists(context.AssemblyFile)) - { - var virtualizedModule = VirtualizedModule.Load(context.AssemblyFile); - if (!virtualizedModule.IsVirtualized) - { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("Virtualizing Assembly-Csharp.dll... "); - backup.Add(context.AssemblyFile); - virtualizedModule.Virtualize(); - Console.WriteLine("Done!"); - Console.ResetColor(); - } - } - - #endregion - } catch (Exception e) { diff --git a/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache b/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache index 608c7d4d..b6c8e70d 100644 --- a/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache +++ b/IPA/obj/Debug/IPA.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -27a27b93f9cca058d6b4f09d77cb8a300416a979 +7adf61ed0901354b09d9632d801b1671b9c38345