From 304e04bdda9a18d2513d308486fa0d8bdd588e6b Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sat, 11 Jan 2020 16:27:03 -0600 Subject: [PATCH] Changed Injector logger to be a special injector log Improved CriticalSection --- IPA.Injector/Injector.cs | 41 ++++++++------- IPA.Loader/Logging/Logger.cs | 1 + IPA.Loader/Utilities/CriticalSection.cs | 66 ++++++++++-------------- Refs/UnityEngine.CoreModule.Net4.dll | Bin 614400 -> 614400 bytes 4 files changed, 49 insertions(+), 59 deletions(-) diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index f453492e..4ef3b09d 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -78,14 +78,14 @@ namespace IPA.Injector if (AntiPiracy.IsInvalid(Environment.CurrentDirectory)) { - loader.Error("Invalid installation; please buy the game to run BSIPA."); + log.Error("Invalid installation; please buy the game to run BSIPA."); return; } CriticalSection.Configure(); - loader.Debug("Prepping bootstrapper"); + injector.Debug("Prepping bootstrapper"); // updates backup InstallBootstrapPatch(); @@ -134,13 +134,13 @@ namespace IPA.Injector var dataDir = new DirectoryInfo(managedPath).Parent.Name; var gameName = dataDir.Substring(0, dataDir.Length - 5); - loader.Debug("Finding backup"); + injector.Debug("Finding backup"); var backupPath = Path.Combine(Environment.CurrentDirectory, "IPA", "Backups", gameName); var bkp = BackupManager.FindLatestBackup(backupPath); if (bkp == null) - loader.Warn("No backup found! Was BSIPA installed using the installer?"); + injector.Warn("No backup found! Was BSIPA installed using the installer?"); - loader.Debug("Ensuring patch on UnityEngine.CoreModule exists"); + injector.Debug("Ensuring patch on UnityEngine.CoreModule exists"); #region Insert patch into UnityEngine.CoreModule.dll @@ -149,7 +149,7 @@ namespace IPA.Injector "UnityEngine.CoreModule.dll"); // this is a critical section because if you exit in here, CoreModule can die - CriticalSection.EnterExecuteSection(); + using var critSec = CriticalSection.ExecuteSection(); var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath, new ReaderParameters { @@ -174,6 +174,13 @@ namespace IPA.Injector var application = unityModDef.GetType("UnityEngine", "Application"); + if (application == null) + { + injector.Critical("UnityEngine.CoreModule doesn't have a definition for UnityEngine.Application!" + + "Nothing to patch to get ourselves into the Unity run cycle!"); + goto endPatchCoreModule; + } + MethodDefinition cctor = null; foreach (var m in application.Methods) if (m.IsRuntimeSpecialName && m.Name == ".cctor") @@ -230,13 +237,11 @@ namespace IPA.Injector bkp?.Add(unityPath); unityAsmDef.Write(unityPath); } - - CriticalSection.ExitExecuteSection(); } - + endPatchCoreModule: #endregion Insert patch into UnityEngine.CoreModule.dll - loader.Debug("Ensuring game assemblies are virtualized"); + injector.Debug("Ensuring game assemblies are virtualized"); #region Virtualize game assemblies bool isFirst = true; @@ -244,26 +249,26 @@ namespace IPA.Injector { var ascPath = Path.Combine(managedPath, name); - CriticalSection.EnterExecuteSection(); + using var execSec = CriticalSection.ExecuteSection(); try { - loader.Debug($"Virtualizing {name}"); + injector.Debug($"Virtualizing {name}"); using var ascModule = VirtualizedModule.Load(ascPath); ascModule.Virtualize(cAsmName, () => bkp?.Add(ascPath)); } catch (Exception e) { - loader.Error($"Could not virtualize {ascPath}"); + injector.Error($"Could not virtualize {ascPath}"); if (SelfConfig.Debug_.ShowHandledErrorStackTraces_) - loader.Error(e); + injector.Error(e); } if (isFirst) { try { - loader.Debug("Applying anti-yeet patch"); + injector.Debug("Applying anti-yeet patch"); var ascAsmDef = AssemblyDefinition.ReadAssembly(ascPath, new ReaderParameters { @@ -282,13 +287,11 @@ namespace IPA.Injector } catch (Exception e) { - loader.Warn($"Could not apply anti-yeet patch to {ascPath}"); + injector.Warn($"Could not apply anti-yeet patch to {ascPath}"); if (SelfConfig.Debug_.ShowHandledErrorStackTraces_) - loader.Warn(e); + injector.Warn(e); } } - - CriticalSection.ExitExecuteSection(); } #endregion } diff --git a/IPA.Loader/Logging/Logger.cs b/IPA.Loader/Logging/Logger.cs index 10e94d9a..0fe53704 100644 --- a/IPA.Loader/Logging/Logger.cs +++ b/IPA.Loader/Logging/Logger.cs @@ -35,6 +35,7 @@ namespace IPA.Logging internal static Logger updater => log.GetChildLogger("Updater"); internal static Logger libLoader => log.GetChildLogger("LibraryLoader"); + internal static Logger injector => log.GetChildLogger("Injector"); internal static Logger loader => log.GetChildLogger("Loader"); internal static Logger features => loader.GetChildLogger("Features"); internal static Logger config => log.GetChildLogger("Config"); diff --git a/IPA.Loader/Utilities/CriticalSection.cs b/IPA.Loader/Utilities/CriticalSection.cs index 3ef57d29..026692cc 100644 --- a/IPA.Loader/Utilities/CriticalSection.cs +++ b/IPA.Loader/Utilities/CriticalSection.cs @@ -106,6 +106,32 @@ namespace IPA.Utilities private static volatile bool exitRecieved = false; + /// + /// A struct that allows using blocks to manage an execute section. + /// + public struct AutoExecuteSection : IDisposable + { + private readonly bool constructed; + internal AutoExecuteSection(bool val) + { + constructed = val && !isInExecuteSection; + if (constructed) + EnterExecuteSection(); + } + + void IDisposable.Dispose() + { + if (constructed) + ExitExecuteSection(); + } + } + + /// + /// Creates an for automated management of an execute section. + /// + /// the new that manages the section + public static AutoExecuteSection ExecuteSection() => new AutoExecuteSection(true); + /// /// Enters a critical execution section. Does not nest. /// @@ -143,45 +169,5 @@ namespace IPA.Utilities } #endregion - - #region GC section - - // i wish i could reference GC_enable and GC_disable directly - [DllImport("mono-2.0-bdwgc")] - private static extern void mono_unity_gc_enable(); - [DllImport("mono-2.0-bdwgc")] - private static extern void mono_unity_gc_disable(); - - /// - /// Enters a GC critical section. Each call to this must be paired with a call to . - /// - /// - /// During a GC critical section, no GCs will occur. - /// - /// This may throw an if the build of Mono the game is running on does - /// not have `mono_unity_gc_disable` exported. Use with caution. - /// - public static void EnterGCSection() - { - mono_unity_gc_disable(); - } - - - /// - /// Exits a GC critical section. Each call to this must have a preceding call to . - /// - /// - /// During a GC critical section, no GCs will occur. - /// - /// This may throw an if the build of Mono the game is running on does - /// not have `mono_unity_gc_enable` exported. Use with caution. - /// - public static void ExitGCSection() - { - mono_unity_gc_enable(); - } - - #endregion - } } diff --git a/Refs/UnityEngine.CoreModule.Net4.dll b/Refs/UnityEngine.CoreModule.Net4.dll index 757916691b0ac800352185a2ec4bec86ebbdf746..2fbda56981a71810cc93045912466881cb7d90e9 100644 GIT binary patch delta 47 wcmZoTpxOXLEsQNpEzB(}TUa~7S(6!(8JOF9!&!lt4T#x+m}7fyIA<{j0ELtfumAu6 delta 47 wcmZoTpxOXLEsQNpEzB(}TUa~7S$P>)85r7o!&!lt4T#x+m}7fyIA<{j0CNux`2YX_