From 28559fe708a056abc180f5c386b8d1b0612fd062 Mon Sep 17 00:00:00 2001 From: Howard Lam Date: Sat, 11 Jan 2025 21:51:47 +0000 Subject: [PATCH] Checks if target Unity binaries archive exists before attempting download in case game's network conenction is blocked (#1024) --- .../Il2CppInteropManager.cs | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs b/Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs index 4212195..0ddba45 100644 --- a/Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs +++ b/Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; +using System.Linq; using System.Net.Http; using System.Reflection; using System.Security.Cryptography; @@ -282,25 +283,29 @@ internal static partial class Il2CppInteropManager } } - private static void DownloadUnityAssemblies() - { + private static void DownloadUnityAssemblies() { var unityVersion = UnityInfo.Version; - var source = - UnityBaseLibrariesSource.Value.Replace("{VERSION}", - $"{unityVersion.Major}.{unityVersion.Minor}.{unityVersion.Build}"); + var version = $"{unityVersion.Major}.{unityVersion.Minor}.{unityVersion.Build}"; + var source = UnityBaseLibrariesSource.Value.Replace("{VERSION}", version); + if (string.IsNullOrEmpty(source)) return; - if (!string.IsNullOrEmpty(source)) - { - Logger.LogMessage("Downloading unity base libraries"); - - Directory.CreateDirectory(UnityBaseLibsDirectory); - Directory.EnumerateFiles(UnityBaseLibsDirectory, "*.dll").Do(File.Delete); + var uri = new Uri(source); + string file = Path.GetFileName(uri.AbsolutePath); + var baseFolder = Directory.CreateDirectory(UnityBaseLibsDirectory); + baseFolder.EnumerateFiles("*.dll").Do(a=>a.Delete()); + var target = baseFolder.GetFiles(file).FirstOrDefault(); + if (target != null) { + Logger.LogMessage($"Reading unity base libraries from file {source}"); + using var fStream = target.OpenRead(); + using var zipArchive = new ZipArchive(fStream, ZipArchiveMode.Read); + zipArchive.ExtractToDirectory(UnityBaseLibsDirectory); + } else { + Logger.LogMessage($"Downloading unity base libraries {source}"); using var httpClient = new HttpClient(); using var zipStream = httpClient.GetStreamAsync(source).GetAwaiter().GetResult(); - using var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read); - Logger.LogMessage("Extracting downloaded unity base libraries"); + using var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read); zipArchive.ExtractToDirectory(UnityBaseLibsDirectory); } }