Browse Source

Changed Injector logger to be a special injector log

Improved CriticalSection
4.0.0-beta
Anairkoen Schno 4 years ago
parent
commit
6459dcc762
5 changed files with 49 additions and 59 deletions
  1. +22
    -19
      IPA.Injector/Injector.cs
  2. +1
    -0
      IPA.Loader/Logging/Logger.cs
  3. +26
    -40
      IPA.Loader/Utilities/CriticalSection.cs
  4. BIN
      Refs/MainAssembly.dll
  5. BIN
      Refs/UnityEngine.CoreModule.Net4.dll

+ 22
- 19
IPA.Injector/Injector.cs View File

@ -78,14 +78,14 @@ namespace IPA.Injector
if (AntiPiracy.IsInvalid(Environment.CurrentDirectory)) 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; return;
} }
CriticalSection.Configure(); CriticalSection.Configure();
loader.Debug("Prepping bootstrapper");
injector.Debug("Prepping bootstrapper");
// updates backup // updates backup
InstallBootstrapPatch(); InstallBootstrapPatch();
@ -134,13 +134,13 @@ namespace IPA.Injector
var dataDir = new DirectoryInfo(managedPath).Parent.Name; var dataDir = new DirectoryInfo(managedPath).Parent.Name;
var gameName = dataDir.Substring(0, dataDir.Length - 5); 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 backupPath = Path.Combine(Environment.CurrentDirectory, "IPA", "Backups", gameName);
var bkp = BackupManager.FindLatestBackup(backupPath); var bkp = BackupManager.FindLatestBackup(backupPath);
if (bkp == null) 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 #region Insert patch into UnityEngine.CoreModule.dll
@ -149,7 +149,7 @@ namespace IPA.Injector
"UnityEngine.CoreModule.dll"); "UnityEngine.CoreModule.dll");
// this is a critical section because if you exit in here, CoreModule can die // 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 var unityAsmDef = AssemblyDefinition.ReadAssembly(unityPath, new ReaderParameters
{ {
@ -174,6 +174,13 @@ namespace IPA.Injector
var application = unityModDef.GetType("UnityEngine", "Application"); 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; MethodDefinition cctor = null;
foreach (var m in application.Methods) foreach (var m in application.Methods)
if (m.IsRuntimeSpecialName && m.Name == ".cctor") if (m.IsRuntimeSpecialName && m.Name == ".cctor")
@ -230,13 +237,11 @@ namespace IPA.Injector
bkp?.Add(unityPath); bkp?.Add(unityPath);
unityAsmDef.Write(unityPath); unityAsmDef.Write(unityPath);
} }
CriticalSection.ExitExecuteSection();
} }
endPatchCoreModule:
#endregion Insert patch into UnityEngine.CoreModule.dll #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 #region Virtualize game assemblies
bool isFirst = true; bool isFirst = true;
@ -244,26 +249,26 @@ namespace IPA.Injector
{ {
var ascPath = Path.Combine(managedPath, name); var ascPath = Path.Combine(managedPath, name);
CriticalSection.EnterExecuteSection();
using var execSec = CriticalSection.ExecuteSection();
try try
{ {
loader.Debug($"Virtualizing {name}");
injector.Debug($"Virtualizing {name}");
using var ascModule = VirtualizedModule.Load(ascPath); using var ascModule = VirtualizedModule.Load(ascPath);
ascModule.Virtualize(cAsmName, () => bkp?.Add(ascPath)); ascModule.Virtualize(cAsmName, () => bkp?.Add(ascPath));
} }
catch (Exception e) catch (Exception e)
{ {
loader.Error($"Could not virtualize {ascPath}");
injector.Error($"Could not virtualize {ascPath}");
if (SelfConfig.Debug_.ShowHandledErrorStackTraces_) if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
loader.Error(e);
injector.Error(e);
} }
if (isFirst) if (isFirst)
{ {
try try
{ {
loader.Debug("Applying anti-yeet patch");
injector.Debug("Applying anti-yeet patch");
var ascAsmDef = AssemblyDefinition.ReadAssembly(ascPath, new ReaderParameters var ascAsmDef = AssemblyDefinition.ReadAssembly(ascPath, new ReaderParameters
{ {
@ -282,13 +287,11 @@ namespace IPA.Injector
} }
catch (Exception e) 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_) if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
loader.Warn(e);
injector.Warn(e);
} }
} }
CriticalSection.ExitExecuteSection();
} }
#endregion #endregion
} }


+ 1
- 0
IPA.Loader/Logging/Logger.cs View File

@ -35,6 +35,7 @@ namespace IPA.Logging
internal static Logger updater => log.GetChildLogger("Updater"); internal static Logger updater => log.GetChildLogger("Updater");
internal static Logger libLoader => log.GetChildLogger("LibraryLoader"); internal static Logger libLoader => log.GetChildLogger("LibraryLoader");
internal static Logger injector => log.GetChildLogger("Injector");
internal static Logger loader => log.GetChildLogger("Loader"); internal static Logger loader => log.GetChildLogger("Loader");
internal static Logger features => loader.GetChildLogger("Features"); internal static Logger features => loader.GetChildLogger("Features");
internal static Logger config => log.GetChildLogger("Config"); internal static Logger config => log.GetChildLogger("Config");


+ 26
- 40
IPA.Loader/Utilities/CriticalSection.cs View File

@ -106,6 +106,32 @@ namespace IPA.Utilities
private static volatile bool exitRecieved = false; private static volatile bool exitRecieved = false;
/// <summary>
/// A struct that allows <c>using</c> blocks to manage an execute section.
/// </summary>
public struct AutoExecuteSection : IDisposable
{
private readonly bool constructed;
internal AutoExecuteSection(bool val)
{
constructed = val && !isInExecuteSection;
if (constructed)
EnterExecuteSection();
}
void IDisposable.Dispose()
{
if (constructed)
ExitExecuteSection();
}
}
/// <summary>
/// Creates an <see cref="AutoExecuteSection"/> for automated management of an execute section.
/// </summary>
/// <returns>the new <see cref="AutoExecuteSection"/> that manages the section</returns>
public static AutoExecuteSection ExecuteSection() => new AutoExecuteSection(true);
/// <summary> /// <summary>
/// Enters a critical execution section. Does not nest. /// Enters a critical execution section. Does not nest.
/// </summary> /// </summary>
@ -143,45 +169,5 @@ namespace IPA.Utilities
} }
#endregion #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();
/// <summary>
/// Enters a GC critical section. Each call to this must be paired with a call to <see cref="ExitGCSection"/>.
/// </summary>
/// <note>
/// During a GC critical section, no GCs will occur.
///
/// This may throw an <see cref="EntryPointNotFoundException"/> if the build of Mono the game is running on does
/// not have `mono_unity_gc_disable` exported. Use with caution.
/// </note>
public static void EnterGCSection()
{
mono_unity_gc_disable();
}
/// <summary>
/// Exits a GC critical section. Each call to this must have a preceding call to <see cref="EnterGCSection"/>.
/// </summary>
/// <note>
/// During a GC critical section, no GCs will occur.
///
/// This may throw an <see cref="EntryPointNotFoundException"/> if the build of Mono the game is running on does
/// not have `mono_unity_gc_enable` exported. Use with caution.
/// </note>
public static void ExitGCSection()
{
mono_unity_gc_enable();
}
#endregion
} }
} }

BIN
Refs/MainAssembly.dll View File


BIN
Refs/UnityEngine.CoreModule.Net4.dll View File


Loading…
Cancel
Save