diff --git a/ProjectNLD/Assets/Code/Scripts/HotUpdate/Editor/HotUpadeEditorHelper.cs b/ProjectNLD/Assets/Code/Scripts/HotUpdate/Editor/HotUpadeEditorHelper.cs index 9122deb45e0..c15e577554b 100644 --- a/ProjectNLD/Assets/Code/Scripts/HotUpdate/Editor/HotUpadeEditorHelper.cs +++ b/ProjectNLD/Assets/Code/Scripts/HotUpdate/Editor/HotUpadeEditorHelper.cs @@ -92,6 +92,62 @@ public static class HotUpadeEditorHelper Debug.Log("转移补充元数据dll成功!"); } + private static void _HandleConvertDllToPolymorphic(string dllPath) + { + // 把路径下的所有dll转换成多态dll形势 + // 1. 读取所有*.bytes文件转存到临时目录,去掉.bytes后缀 + // 2. 调用Obfuz4HybridCLR的转换接口进行转换,输出到另外一个临时目录 + // 3. 把转换后的dll覆盖回原来的.bytes文件 + // 临时目录设置在 Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls1" 和 Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls2" + var tempDir1 = Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls1/"; + var tempDir2 = Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls2/"; + if (Directory.Exists(tempDir1)) + { + Directory.Delete(tempDir1, true); + } + if (Directory.Exists(tempDir2)) + { + Directory.Delete(tempDir2, true); + } + Directory.CreateDirectory(tempDir1); + Directory.CreateDirectory(tempDir2); + var dir = new DirectoryInfo(dllPath); + var files = dir.GetFiles(); + // 步骤1: 复制.bytes文件到临时目录1,去掉.bytes后缀 + foreach (var file in files) + { + var dllFileName = Path.GetFileNameWithoutExtension(file.Name); // 去掉.bytes + var destPath = Path.Combine(tempDir1, dllFileName); + File.Copy(file.FullName, destPath, true); + Debug.Log($"[ConvertDll] Copy {file.Name} to {destPath}"); + } + + // 步骤2: 调用Obfuz4HybridCLR转换接口 + ObfuscateUtil.GeneratePolymorphicDll(tempDir1, tempDir2); + + // 步骤3: 把转换后的dll覆盖回原来的.bytes文件 + var convertedFiles = new DirectoryInfo(tempDir2).GetFiles("*.dll"); + foreach (var file in convertedFiles) + { + var originalBytesPath = Path.Combine(dllPath, file.Name + ".bytes"); + if (File.Exists(originalBytesPath)) + { + File.Copy(file.FullName, originalBytesPath, true); + Debug.Log($"[ConvertDll] Override {originalBytesPath} with converted dll"); + } + } + } + + public static void ConvertOriginalDllToPolymorphicDll() + { + // Assets/HotUpdateDlls/HotUpdateDll + var hotUpdateDestinationPath = Application.dataPath + HOT_UPDATE_DESTINATION_PATH; + _HandleConvertDllToPolymorphic(hotUpdateDestinationPath); + // Assets/HotUpdateDlls/MetaDataDll + var metaDataDestinationPath = Application.dataPath + META_DATA_DESTINATION_PATH; + _HandleConvertDllToPolymorphic(metaDataDestinationPath); + } + //之所以采用读取C#文件的方式是因为如果直接读取代码中的列表会出现打包时更改了AOTGenericReferences.cs但Unity没编译导致 //AOTGenericReferences中PatchedAOTAssemblyList还是代码修改前的数据的问题,是因为Unity没有reload domain //https://docs.unity.cn/2023.2/Documentation/Manual/DomainReloading.html diff --git a/ProjectNLD/Assets/Editor/Common/BuildLauncherInEditor.cs b/ProjectNLD/Assets/Editor/Common/BuildLauncherInEditor.cs index a1768c9315c..867e61f3da2 100644 --- a/ProjectNLD/Assets/Editor/Common/BuildLauncherInEditor.cs +++ b/ProjectNLD/Assets/Editor/Common/BuildLauncherInEditor.cs @@ -91,6 +91,12 @@ class BuildLauncher } HotUpadeEditorHelper.CopyMetaDataDll(); + + if (ObfuzSettings.Instance.polymorphicDllSettings.enable) + { + // 使用多态dll + HotUpadeEditorHelper.ConvertOriginalDllToPolymorphicDll(); + } } /// diff --git a/ProjectNLD/ProjectSettings/Obfuz.asset b/ProjectNLD/ProjectSettings/Obfuz.asset index 1fe09056d85..41c62250ee1 100644 --- a/ProjectNLD/ProjectSettings/Obfuz.asset +++ b/ProjectNLD/ProjectSettings/Obfuz.asset @@ -91,5 +91,5 @@ MonoBehaviour: signatureLength: 256 polymorphicDllSettings: enable: 1 - codeGenerationSecretKey: obfuz-polymorphic-key - disableLoadStandardDll: 0 + codeGenerationSecretKey: obfuz-phxh-polymorphic-check-dll + disableLoadStandardDll: 1