修复PrebuildCommandExt.GenerateAll中生成桥接函数时如果混淆程序集是预编译的dll,并不在热更新dll输出目录中,会错误地从搜索目录加载原始插件dll的bug
parent
10e6c0d914
commit
76cb8fbcbb
|
@ -3,14 +3,8 @@ using Obfuz.Settings;
|
|||
using Obfuz;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEditor;
|
||||
using HybridCLR.Editor.Commands;
|
||||
using HybridCLR.Editor.Installer;
|
||||
using System.IO;
|
||||
using HybridCLR.Editor.ABI;
|
||||
using UnityEngine;
|
||||
using Obfuz.Unity;
|
||||
|
||||
|
@ -34,11 +28,9 @@ namespace Obfuz4HybridCLR
|
|||
}
|
||||
}
|
||||
|
||||
public static void CompileAndObfuscateHotUpdateAssemblies(BuildTarget target)
|
||||
public static void ObfuscateHotUpdateAssemblies(BuildTarget target, string outputDir)
|
||||
{
|
||||
string hotUpdateDllPath = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
|
||||
BashUtil.RemoveDir(hotUpdateDllPath);
|
||||
CompileDllCommand.CompileDll(target);
|
||||
|
||||
AssemblySettings assemblySettings = ObfuzSettings.Instance.assemblySettings;
|
||||
ObfuscationProcess.ValidateReferences(hotUpdateDllPath, new HashSet<string>(assemblySettings.GetAssembliesToObfuscate()), new HashSet<string>(assemblySettings.GetObfuscationRelativeAssemblyNames()));
|
||||
|
@ -46,10 +38,31 @@ namespace Obfuz4HybridCLR
|
|||
{
|
||||
hotUpdateDllPath,
|
||||
};
|
||||
Obfuscate(target, assemblySearchPaths, hotUpdateDllPath);
|
||||
if (AreSameDirectory(hotUpdateDllPath, outputDir))
|
||||
{
|
||||
throw new Exception($"hotUpdateDllPath:{hotUpdateDllPath} can't be same to outputDir:{outputDir}");
|
||||
}
|
||||
Obfuscate(target, assemblySearchPaths, outputDir);
|
||||
foreach (string hotUpdateAssemblyName in SettingsUtil.HotUpdateAssemblyNamesExcludePreserved)
|
||||
{
|
||||
string srcFile = $"{hotUpdateDllPath}/{hotUpdateAssemblyName}.dll";
|
||||
string dstFile = $"{outputDir}/{hotUpdateAssemblyName}.dll";
|
||||
// only copy non obfuscated assemblies
|
||||
if (File.Exists(srcFile) && !File.Exists(dstFile))
|
||||
{
|
||||
File.Copy(srcFile, dstFile, true);
|
||||
Debug.Log($"[CompileAndObfuscateDll] Copy nonObfuscated assembly {srcFile} to {dstFile}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Obfuscate(BuildTarget target, List<string> assemblySearchPaths, string outputPath)
|
||||
/// <summary>
|
||||
/// output obfuscated assemblies to ObfuzSettings.Instance.GetObfuscatedAssemblyOutputPath(target)
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="assemblySearchPaths"></param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static void Obfuscate(BuildTarget target, List<string> assemblySearchPaths, string obfuscatedAssemblyOutputPath)
|
||||
{
|
||||
var obfuzSettings = ObfuzSettings.Instance;
|
||||
|
||||
|
@ -57,11 +70,6 @@ namespace Obfuz4HybridCLR
|
|||
ObfuscatorBuilder builder = ObfuscatorBuilder.FromObfuzSettings(obfuzSettings, target, true);
|
||||
builder.InsertTopPriorityAssemblySearchPaths(assemblySearchDirs);
|
||||
|
||||
string obfuscatedAssemblyOutputPath = obfuzSettings.GetObfuscatedAssemblyOutputPath(target);
|
||||
if (AreSameDirectory(outputPath, obfuscatedAssemblyOutputPath))
|
||||
{
|
||||
throw new Exception($"outputPath:{outputPath} can't be same to ObfuscatedAssemblyOutputPath:{obfuscatedAssemblyOutputPath}");
|
||||
}
|
||||
foreach (var assemblySearchDir in builder.CoreSettingsFacade.assemblySearchPaths)
|
||||
{
|
||||
if (AreSameDirectory(assemblySearchDir, obfuscatedAssemblyOutputPath))
|
||||
|
@ -72,15 +80,6 @@ namespace Obfuz4HybridCLR
|
|||
|
||||
Obfuscator obfuz = builder.Build();
|
||||
obfuz.Run();
|
||||
|
||||
Directory.CreateDirectory(outputPath);
|
||||
foreach (string srcFile in Directory.GetFiles(obfuscatedAssemblyOutputPath, "*.dll"))
|
||||
{
|
||||
string fileName = Path.GetFileName(srcFile);
|
||||
string destFile = $"{outputPath}/{fileName}";
|
||||
File.Copy(srcFile, destFile, true);
|
||||
Debug.Log($"Copy {srcFile} to {destFile}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,27 @@ using HybridCLR.Editor.Link;
|
|||
using HybridCLR.Editor.Meta;
|
||||
using UnityEditor.Build;
|
||||
using HybridCLR.Editor.Installer;
|
||||
using HybridCLR.Editor.MethodBridge;
|
||||
using System.Linq;
|
||||
using Analyzer = HybridCLR.Editor.MethodBridge.Analyzer;
|
||||
using HybridCLR.Editor.Settings;
|
||||
using Obfuz.Utils;
|
||||
using FileUtil = Obfuz.Utils.FileUtil;
|
||||
using IAssemblyResolver = HybridCLR.Editor.Meta.IAssemblyResolver;
|
||||
using CombinedAssemblyResolver = HybridCLR.Editor.Meta.CombinedAssemblyResolver;
|
||||
using MetaUtil = HybridCLR.Editor.Meta.MetaUtil;
|
||||
using AssemblyCache = HybridCLR.Editor.Meta.AssemblyCache;
|
||||
|
||||
namespace Obfuz4HybridCLR
|
||||
{
|
||||
public static class PrebuildCommandExt
|
||||
{
|
||||
public static string GetObfuscatedHotUpdateAssemblyOutputPath(BuildTarget target)
|
||||
{
|
||||
return $"{ObfuzSettings.Instance.ObfuzRootDir}/{target}/ObfuscatedHotUpdateAssemblies";
|
||||
}
|
||||
|
||||
|
||||
[MenuItem("HybridCLR/ObfuzExtension/GenerateAll")]
|
||||
public static void GenerateAll()
|
||||
{
|
||||
|
@ -27,18 +43,82 @@ namespace Obfuz4HybridCLR
|
|||
throw new BuildFailedException($"You have not initialized HybridCLR, please install it via menu 'HybridCLR/Installer'");
|
||||
}
|
||||
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
|
||||
ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
|
||||
CompileDllCommand.CompileDll(target);
|
||||
Il2CppDefGeneratorCommand.GenerateIl2CppDef();
|
||||
LinkGeneratorCommand.GenerateLinkXml(target);
|
||||
StripAOTDllCommand.GenerateStripedAOTDlls(target);
|
||||
MethodBridgeGeneratorCommand.GenerateMethodBridgeAndReversePInvokeWrapper(target);
|
||||
AOTReferenceGeneratorCommand.GenerateAOTGenericReference(target);
|
||||
|
||||
string obfuscatedHotUpdateDllPath = GetObfuscatedHotUpdateAssemblyOutputPath(target);
|
||||
ObfuscateUtil.ObfuscateHotUpdateAssemblies(target, obfuscatedHotUpdateDllPath);
|
||||
GenerateMethodBridgeAndReversePInvokeWrapper(target, obfuscatedHotUpdateDllPath);
|
||||
}
|
||||
|
||||
[MenuItem("HybridCLR/ObfuzExtension/CompileAndObfuscateDll")]
|
||||
public static void CompileAndObfuscateDll()
|
||||
{
|
||||
ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(EditorUserBuildSettings.activeBuildTarget);
|
||||
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
|
||||
CompileDllCommand.CompileDll(target);
|
||||
|
||||
string obfuscatedHotUpdateDllPath = GetObfuscatedHotUpdateAssemblyOutputPath(target);
|
||||
ObfuscateUtil.ObfuscateHotUpdateAssemblies(target, obfuscatedHotUpdateDllPath);
|
||||
}
|
||||
|
||||
public static IAssemblyResolver CreateObfuscatedHotUpdateAssemblyResolver(BuildTarget target, List<string> obfuscatedHotUpdateAssemblies, string obfuscatedHotUpdateDllPath)
|
||||
{
|
||||
return new FixedSetAssemblyResolver(obfuscatedHotUpdateDllPath, obfuscatedHotUpdateAssemblies);
|
||||
}
|
||||
|
||||
public static IAssemblyResolver CreateObfuscatedHotUpdateAndAOTAssemblyResolver(BuildTarget target, List<string> hotUpdateAssemblies, List<string> assembliesToObfuscate, string obfuscatedHotUpdateDllPath)
|
||||
{
|
||||
return new CombinedAssemblyResolver(
|
||||
CreateObfuscatedHotUpdateAssemblyResolver(target, hotUpdateAssemblies.Intersect(assembliesToObfuscate).ToList(), obfuscatedHotUpdateDllPath),
|
||||
MetaUtil.CreateHotUpdateAssemblyResolver(target, hotUpdateAssemblies.Except(assembliesToObfuscate).ToList()),
|
||||
MetaUtil.CreateAOTAssemblyResolver(target)
|
||||
);
|
||||
}
|
||||
|
||||
public static void GenerateMethodBridgeAndReversePInvokeWrapper(BuildTarget target, string obfuscatedHotUpdateDllPath)
|
||||
{
|
||||
string aotDllDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
|
||||
List<string> aotAssemblyNames = Directory.Exists(aotDllDir) ?
|
||||
Directory.GetFiles(aotDllDir, "*.dll", SearchOption.TopDirectoryOnly).Select(Path.GetFileNameWithoutExtension).ToList()
|
||||
: new List<string>();
|
||||
if (aotAssemblyNames.Count == 0)
|
||||
{
|
||||
throw new Exception($"no aot assembly found. please run `HybridCLR/Generate/All` or `HybridCLR/Generate/AotDlls` to generate aot dlls before runing `HybridCLR/Generate/MethodBridge`");
|
||||
}
|
||||
AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateAOTAssemblyResolver(target), aotAssemblyNames);
|
||||
|
||||
var methodBridgeAnalyzer = new Analyzer(new Analyzer.Options
|
||||
{
|
||||
MaxIterationCount = Math.Min(20, SettingsUtil.HybridCLRSettings.maxMethodBridgeGenericIteration),
|
||||
Collector = collector,
|
||||
});
|
||||
|
||||
methodBridgeAnalyzer.Run();
|
||||
|
||||
List<string> hotUpdateDlls = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved;
|
||||
var cache = new AssemblyCache(CreateObfuscatedHotUpdateAndAOTAssemblyResolver(target, hotUpdateDlls, ObfuzSettings.Instance.assemblySettings.GetAssembliesToObfuscate(), obfuscatedHotUpdateDllPath));
|
||||
|
||||
var reversePInvokeAnalyzer = new MonoPInvokeCallbackAnalyzer(cache, hotUpdateDlls);
|
||||
reversePInvokeAnalyzer.Run();
|
||||
|
||||
var calliAnalyzer = new CalliAnalyzer(cache, hotUpdateDlls);
|
||||
calliAnalyzer.Run();
|
||||
var pinvokeAnalyzer = new PInvokeAnalyzer(cache, hotUpdateDlls);
|
||||
pinvokeAnalyzer.Run();
|
||||
var callPInvokeMethodSignatures = pinvokeAnalyzer.PInvokeMethodSignatures;
|
||||
|
||||
string templateFile = $"{SettingsUtil.TemplatePathInPackage}/MethodBridge.cpp.tpl";
|
||||
string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp";
|
||||
|
||||
var callNativeMethodSignatures = calliAnalyzer.CalliMethodSignatures.Concat(pinvokeAnalyzer.PInvokeMethodSignatures).ToList();
|
||||
|
||||
var generateMethodBridgeMethod = typeof(MethodBridgeGeneratorCommand).GetMethod("GenerateMethodBridgeCppFile", BindingFlags.NonPublic | BindingFlags.Static);
|
||||
generateMethodBridgeMethod.Invoke(null, new object[] { methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, callNativeMethodSignatures, templateFile, outputFile });
|
||||
|
||||
MethodBridgeGeneratorCommand.CleanIl2CppBuildCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue