From 620d695880dacb46b81ca1538e021fad839625ee Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 28 May 2025 09:25:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81custom=20rename=20policy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ObfusPasses/SymbolObfus/SymbolRename.cs | 25 +++++++++++++- .../Editor/Obfuscator.cs | 5 +-- .../Settings/SymbolObfuscationSettings.cs | 9 ++++- .../Editor/Utils/ReflectionUtil.cs | 33 +++++++++++++++++++ .../Editor/Utils/ReflectionUtil.cs.meta | 11 +++++++ 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs create mode 100644 com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs.meta diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs index d2f2b6b..045a2ea 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs @@ -33,6 +33,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus private readonly Dictionary> _customAttributeArgumentsWithTypeByMods = new Dictionary>(); private readonly RenameRecordMap _renameRecordMap; private readonly VirtualMethodGroupCalculator _virtualMethodGroupCalculator; + private readonly List _customPolicies = new List(); class CustomAttributeInfo { @@ -50,6 +51,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus _renameRecordMap = new RenameRecordMap(settings.debug ? null : settings.symbolMappingFile); _virtualMethodGroupCalculator = new VirtualMethodGroupCalculator(); _nameMaker = settings.debug ? NameMakerFactory.CreateDebugNameMaker() : NameMakerFactory.CreateNameMakerBaseASCIICharSet(settings.obfuscatedNamePrefix); + + foreach (var customPolicyType in settings.customRenamePolicyTypes) + { + if (Activator.CreateInstance(customPolicyType, new object[] { this }) is IObfuscationPolicy customPolicy) + { + _customPolicies.Add(customPolicy); + } + else + { + Debug.LogWarning($"Custom rename policy type {customPolicyType} is not a valid IObfuscationPolicy"); + } + } } public void Init() @@ -59,8 +72,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus _toObfuscatedModules = ctx.modulesToObfuscate; _obfuscatedAndNotObfuscatedModules = ctx.allObfuscationRelativeModules; _toObfuscatedModuleSet = new HashSet(ctx.modulesToObfuscate); + var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.coreSettings.assembliesToObfuscate, ctx.modulesToObfuscate, _obfuscationRuleFiles); - _renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(new SupportPassPolicy(ctx.passPolicy), new SystemRenamePolicy(), new UnityRenamePolicy(), obfuscateRuleConfig)); + var totalRenamePolicies = new List + { + new SupportPassPolicy(ctx.passPolicy), + new SystemRenamePolicy(), + new UnityRenamePolicy(), + obfuscateRuleConfig, + }; + totalRenamePolicies.AddRange(_customPolicies); + + _renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(totalRenamePolicies.ToArray())); BuildCustomAttributeArguments(); } diff --git a/com.code-philosophy.obfuz/Editor/Obfuscator.cs b/com.code-philosophy.obfuz/Editor/Obfuscator.cs index b142f42..d756e38 100644 --- a/com.code-philosophy.obfuz/Editor/Obfuscator.cs +++ b/com.code-philosophy.obfuz/Editor/Obfuscator.cs @@ -128,10 +128,7 @@ namespace Obfuz } var vms = new VirtualMachineSimulator(vm, secretKey); - var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies() - .Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine")) - .Where(type => type != null) - .ToList(); + var generatedVmTypes = ReflectionUtil.FindTypesInCurrentAppDomain("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine"); if (generatedVmTypes.Count == 0) { throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine not found in any assembly! Please run `Obfuz/GenerateVm` to generate it!"); diff --git a/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs b/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs index a5f27e5..e7ca08f 100644 --- a/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs +++ b/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs @@ -1,4 +1,6 @@ -using System; +using Obfuz.ObfusPasses.SymbolObfus; +using Obfuz.Utils; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -14,6 +16,7 @@ namespace Obfuz.Settings public bool useConsistentNamespaceObfuscation; public string symbolMappingFile; public List ruleFiles; + public List customRenamePolicyTypes; } [Serializable] @@ -33,6 +36,9 @@ namespace Obfuz.Settings [Tooltip("rule files")] public string[] ruleFiles; + [Tooltip("custom rename policy types")] + public string[] customRenamePolicyTypes; + public SymbolObfuscationSettingsFacade ToFacade() { return new SymbolObfuscationSettingsFacade @@ -42,6 +48,7 @@ namespace Obfuz.Settings useConsistentNamespaceObfuscation = useConsistentNamespaceObfuscation, symbolMappingFile = symbolMappingFile, ruleFiles = ruleFiles.ToList(), + customRenamePolicyTypes = customRenamePolicyTypes.Select(typeName => ReflectionUtil.FindUniqueTypeInCurrentAppDomain(typeName)).ToList(), }; } } diff --git a/com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs b/com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs new file mode 100644 index 0000000..677c498 --- /dev/null +++ b/com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.Utils +{ + public static class ReflectionUtil + { + public static List FindTypesInCurrentAppDomain(string fullName) + { + return AppDomain.CurrentDomain.GetAssemblies() + .Select(assembly => assembly.GetType(fullName)) + .Where(type => type != null) + .ToList(); + } + + public static Type FindUniqueTypeInCurrentAppDomain(string fullName) + { + var foundTypes = FindTypesInCurrentAppDomain(fullName); + if (foundTypes.Count == 0) + { + throw new Exception($"class {fullName} not found in any assembly!"); + } + if (foundTypes.Count > 1) + { + throw new Exception($"class {fullName} found in multiple assemblies! Please retain only one!"); + } + return foundTypes[0]; + } + } +} diff --git a/com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs.meta b/com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs.meta new file mode 100644 index 0000000..05d723b --- /dev/null +++ b/com.code-philosophy.obfuz/Editor/Utils/ReflectionUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6fac8216afeffb746b1b67d1f16883b8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: