diff --git a/BepInEx.IL2CPP/Il2CppInteropManager.cs b/BepInEx.IL2CPP/Il2CppInteropManager.cs index 916a511..654b790 100644 --- a/BepInEx.IL2CPP/Il2CppInteropManager.cs +++ b/BepInEx.IL2CPP/Il2CppInteropManager.cs @@ -11,6 +11,7 @@ using BepInEx.Configuration; using BepInEx.IL2CPP.Hook; using BepInEx.IL2CPP.Logging; using BepInEx.Logging; +using BepInEx.Unity.Core; using Cpp2IL.Core; using HarmonyLib; using Il2CppInterop.Common; @@ -148,7 +149,7 @@ internal static class Il2CppInteropManager : null; } - public static void Initialize(Version unityVersion) + public static void Initialize() { if (initialized) throw new InvalidOperationException("Already initialized"); @@ -159,9 +160,11 @@ internal static class Il2CppInteropManager GenerateInteropAssemblies(); var interopLogger = LoggerFactory.CreateLogger("Il2CppInterop"); + + var unityVersion = UnityInfo.Version; Il2CppInteropRuntime.Create(new RuntimeConfiguration { - UnityVersion = unityVersion, + UnityVersion = new Version(unityVersion.Major, unityVersion.Minor, unityVersion.Build), DetourProvider = new Il2CppInteropDetourProvider() }) .AddLogger(interopLogger) @@ -180,8 +183,7 @@ internal static class Il2CppInteropManager Directory.EnumerateFiles(IL2CPPInteropAssemblyPath, "*.dll").Do(File.Delete); AppDomain.CurrentDomain.AddCecilPlatformAssemblies(UnityBaseLibsDirectory); - var unityVersion = Preloader.UnityVersion.ToString(3); - DownloadUnityAssemblies(unityVersion); + DownloadUnityAssemblies(); var dummyAssemblies = RunCpp2Il(); if (DumpDummyAssemblies.Value) @@ -202,10 +204,12 @@ internal static class Il2CppInteropManager } } - private static void DownloadUnityAssemblies(string unityVersion) + private static void DownloadUnityAssemblies() { + var unityVersion = UnityInfo.Version; var source = - UnityBaseLibrariesSource.Value.Replace("{VERSION}", unityVersion); + UnityBaseLibrariesSource.Value.Replace("{VERSION}", + $"{unityVersion.Major}.{unityVersion.Minor}.{unityVersion.Build}"); if (!string.IsNullOrEmpty(source)) { @@ -249,10 +253,13 @@ internal static class Il2CppInteropManager Cpp2IL.Core.Logger.ErrorLog += (message, s) => cpp2IlLogger.LogError($"[{s}] {message.Trim()}"); - var cpp2IlUnityVersion = - Cpp2IlApi.DetermineUnityVersion(Paths.ExecutablePath, - Path.Combine(Paths.GameRootPath, $"{Paths.ProcessName}_Data")); - Cpp2IlApi.InitializeLibCpp2Il(GameAssemblyPath, metadataPath, cpp2IlUnityVersion, false); + var unityVersion = UnityInfo.Version; + Cpp2IlApi.InitializeLibCpp2Il(GameAssemblyPath, metadataPath, new int[] + { + unityVersion.Major, + unityVersion.Minor, + unityVersion.Build, + }, false); sourceAssemblies = Cpp2IlApi.MakeDummyDLLs(); Cpp2IlApi.RunAttributeRestorationForAllAssemblies(null, LibCpp2IlMain.MetadataVersion >= 29 || diff --git a/BepInEx.IL2CPP/Preloader.cs b/BepInEx.IL2CPP/Preloader.cs index 539c9ba..3e283fe 100644 --- a/BepInEx.IL2CPP/Preloader.cs +++ b/BepInEx.IL2CPP/Preloader.cs @@ -1,28 +1,17 @@ using System; -using System.Diagnostics; -using System.Globalization; using System.Reflection; using System.Runtime.InteropServices; -using BepInEx.Configuration; using BepInEx.Logging; using BepInEx.Preloader.Core; using BepInEx.Preloader.Core.Logging; using BepInEx.Preloader.Core.Patching; using BepInEx.Preloader.RuntimeFixes; +using BepInEx.Unity.Core; namespace BepInEx.IL2CPP; public static class Preloader { - #region Config - - private static readonly ConfigEntry ConfigUnityVersion = ConfigFile.CoreConfig.Bind( - "IL2CPP", "UnityVersion", - string.Empty, - "Unity version to report to Il2CppUnhollower. If empty, version is automatically determined from the game process."); - - #endregion - private static PreloaderConsoleListener PreloaderLog { get; set; } internal static ManualLogSource Log => PreloaderLogger.Log; @@ -30,8 +19,6 @@ public static class Preloader // TODO: This is not needed, maybe remove? (Instance is saved in IL2CPPChainloader itself) private static IL2CPPChainloader Chainloader { get; set; } - public static Version UnityVersion { get; private set; } - public static void Run() { try @@ -62,8 +49,7 @@ public static class Preloader NativeLibrary.SetDllImportResolver(typeof(Il2CppInterop.Runtime.IL2CPP).Assembly, DllImportResolver); - InitializeUnityVersion(); - Il2CppInteropManager.Initialize(UnityVersion); + Il2CppInteropManager.Initialize(); using (var assemblyPatcher = new AssemblyPatcher((data, _) => Assembly.Load(data))) { @@ -103,50 +89,4 @@ public static class Preloader return IntPtr.Zero; } - - private static void InitializeUnityVersion() - { - if (TryInitializeUnityVersion(ConfigUnityVersion.Value)) - Log.Log(LogLevel.Warning, "Unity version obtained from the config."); - else if (TryInitializeUnityVersion(Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion)) - Log.Log(LogLevel.Debug, "Unity version obtained from main application module."); - else - Log.Log(LogLevel.Error, "Running under default Unity version. UnityVersionHandler is not initialized."); - } - - private static bool TryInitializeUnityVersion(string version) - { - try - { - if (string.IsNullOrWhiteSpace(version)) - return false; - - var parts = version.Split('.'); - var major = 0; - var minor = 0; - var build = 0; - - // Issue #229 - Don't use Version.Parse("2019.4.16.14703470L&ProductVersion") - var success = int.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out major); - if (success && parts.Length > 1) - success = int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out minor); - if (success && parts.Length > 2) - success = int.TryParse(parts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out build); - - if (!success) - { - Log.LogError($"Failed to parse Unity version: {version}"); - return false; - } - - UnityVersion = new Version(major, minor, build); - Log.LogInfo($"Running under Unity v{UnityVersion}"); - return true; - } - catch (Exception ex) - { - Log.LogError($"Failed to parse Unity version: {ex}"); - return false; - } - } }