diff --git a/Editor/FileUtil.cs b/Editor/FileUtil.cs new file mode 100644 index 0000000..781431d --- /dev/null +++ b/Editor/FileUtil.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz +{ + public static class FileUtil + { + public static void RecreateDir(string dir) + { + if (Directory.Exists(dir)) + { + Directory.Delete(dir, true); + } + Directory.CreateDirectory(dir); + } + } +} diff --git a/Editor/MetaUtil.cs b/Editor/MetaUtil.cs new file mode 100644 index 0000000..f5737cf --- /dev/null +++ b/Editor/MetaUtil.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz +{ + public static class MetaUtil + { + public static string GetModuleNameWithoutExt(string moduleName) + { + return Path.GetFileNameWithoutExtension(moduleName); + } + } +} diff --git a/Editor/Obfuscator.cs b/Editor/Obfuscator.cs index 5c245c9..3217be2 100644 --- a/Editor/Obfuscator.cs +++ b/Editor/Obfuscator.cs @@ -1,9 +1,12 @@ using dnlib.DotNet; +using Obfuz.Rename; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using UnityEngine; namespace Obfuz { @@ -14,7 +17,7 @@ namespace Obfuz public class Options { public List AssemblySearchDirs; - public List ObfusAssemblyNames; + public List ObfuscatedAssemblyNames; public string outputDir; } @@ -23,21 +26,27 @@ namespace Obfuz private readonly List _obfuzAssemblies = new List(); + private readonly IRenamePolicy _renamePolicy; + private readonly INameMaker _nameMaker; + public Obfuscator(Options options) { _options = options; _assemblyCache = new AssemblyCache(new PathAssemblyResolver(options.AssemblySearchDirs.ToArray())); + _renamePolicy = new RenamePolicy(); + _nameMaker = new NameMaker(); } public void DoIt() { LoadAssemblies(); Rename(); + Save(); } private void LoadAssemblies() { - foreach (string assName in _options.ObfusAssemblyNames) + foreach (string assName in _options.ObfuscatedAssemblyNames) { ModuleDefMD mod = _assemblyCache.LoadModule(assName); var obfuzAsm = new ObfuzAssemblyInfo @@ -46,6 +55,7 @@ namespace Obfuz module = mod, referenceMeAssemblies = new List(), }; + obfuzAsm.referenceMeAssemblies.Add(obfuzAsm); _obfuzAssemblies.Add(obfuzAsm); } @@ -69,9 +79,23 @@ namespace Obfuz var ctx = new ObfuscatorContext { assemblies = _obfuzAssemblies, + renamePolicy = _renamePolicy, + nameMaker = _nameMaker, }; var sr = new SymbolRename(ctx); sr.Process(); } + + private void Save() + { + string outputDir = _options.outputDir; + FileUtil.RecreateDir(outputDir); + foreach (var ass in _obfuzAssemblies) + { + string outputFile = $"{outputDir}/{ass.module.Name}"; + ass.module.Write(outputFile); + Debug.Log($"save module. oldName:{ass.name} newName:{ass.module.Name} output:{outputFile}"); + } + } } } diff --git a/Editor/ObfuscatorContext.cs b/Editor/ObfuscatorContext.cs index 7541bf0..b62a0a0 100644 --- a/Editor/ObfuscatorContext.cs +++ b/Editor/ObfuscatorContext.cs @@ -1,4 +1,5 @@ using dnlib.DotNet; +using Obfuz.Rename; using System; using System.Collections.Generic; using System.Linq; @@ -19,5 +20,9 @@ namespace Obfuz public class ObfuscatorContext { public List assemblies; + + public IRenamePolicy renamePolicy; + + public INameMaker nameMaker; } } diff --git a/Editor/PathAssemblyResolver.cs b/Editor/PathAssemblyResolver.cs index 2ec6593..4070b73 100644 --- a/Editor/PathAssemblyResolver.cs +++ b/Editor/PathAssemblyResolver.cs @@ -24,7 +24,7 @@ namespace Obfuz string assPath = Path.Combine(path, assemblyName + ".dll"); if (File.Exists(assPath)) { - Debug.Log($"resolve {assemblyName} at {assPath}"); + //Debug.Log($"resolve {assemblyName} at {assPath}"); return assPath; } } diff --git a/Editor/Rename/NameMaker.cs b/Editor/Rename/NameMaker.cs new file mode 100644 index 0000000..32c113d --- /dev/null +++ b/Editor/Rename/NameMaker.cs @@ -0,0 +1,62 @@ +using dnlib.DotNet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.Rename +{ + public interface INameMaker + { + string GetNewName(ModuleDefMD mod, string originalName); + + string GetNewName(TypeDef typeDef, string originalName); + + string GetNewName(MethodDef methodDef, string originalName); + + string GetNewName(FieldDef fieldDef, string originalName); + + string GetNewName(PropertyDef propertyDef, string originalName); + + string GetNewName(EventDef eventDef, string originalName); + } + + public class NameMaker : INameMaker + { + private string GetDefaultNewName(string originName) + { + return originName + "_generated_obfuz"; + } + + public string GetNewName(ModuleDefMD mod, string originalName) + { + return GetDefaultNewName(originalName); + } + + public string GetNewName(TypeDef typeDef, string originalName) + { + return GetDefaultNewName(originalName); + } + + public string GetNewName(MethodDef methodDef, string originalName) + { + return GetDefaultNewName(originalName); + } + + public string GetNewName(FieldDef fieldDef, string originalName) + { + return GetDefaultNewName(originalName); + } + + public string GetNewName(PropertyDef propertyDef, string originalName) + { + return GetDefaultNewName(originalName); + } + + public string GetNewName(EventDef eventDef, string originalName) + { + return GetDefaultNewName(originalName); + } + } +} diff --git a/Editor/Rename/RenamePolicy.cs b/Editor/Rename/RenamePolicy.cs index 105d0fc..8ed60ce 100644 --- a/Editor/Rename/RenamePolicy.cs +++ b/Editor/Rename/RenamePolicy.cs @@ -9,94 +9,49 @@ namespace Obfuz.Rename { public interface IRenamePolicy { - bool NeedKeepName(ModuleDefMD mod); + bool NeedRename(ModuleDefMD mod); - bool NeedKeepName(TypeDef typeDef); + bool NeedRename(TypeDef typeDef); - bool NeedKeepName(MethodDef methodDef); + bool NeedRename(MethodDef methodDef); - bool NeedKeepName(FieldDef fieldDef); - - bool NeedKeepName(PropertyDef propertyDef); - - bool NeedKeepName(EventDef eventDef); - - - string GetNewName(ModuleDefMD mod, string originalName); - - string GetNewName(TypeDef typeDef, string originalName); - - string GetNewName(MethodDef methodDef, string originalName); - - string GetNewName(FieldDef fieldDef, string originalName); - - string GetNewName(PropertyDef propertyDef, string originalName); - - string GetNewName(EventDef eventDef, string originalName); + bool NeedRename(FieldDef fieldDef); + bool NeedRename(PropertyDef propertyDef); + bool NeedRename(EventDef eventDef); } public class RenamePolicy : IRenamePolicy { - public bool NeedKeepName(ModuleDefMD mod) + public bool NeedRename(ModuleDefMD mod) { - return false; - } - public bool NeedKeepName(TypeDef typeDef) - { - return false; + return true; } - public bool NeedKeepName(MethodDef methodDef) + public bool NeedRename(TypeDef typeDef) { - return false; + return true; } - public bool NeedKeepName(FieldDef fieldDef) + public bool NeedRename(MethodDef methodDef) { - return false; + return true; } - public bool NeedKeepName(PropertyDef propertyDef) + public bool NeedRename(FieldDef fieldDef) { - return false; + return true; } - public bool NeedKeepName(EventDef eventDef) + public bool NeedRename(PropertyDef propertyDef) { - return false; + return true; } - - public string GetNewName(ModuleDefMD mod, string originalName) + public bool NeedRename(EventDef eventDef) { - return originalName + "_obfuz_generated__"; - } - - public string GetNewName(TypeDef typeDef, string originalName) - { - return originalName + "_obfuz_generated__"; - } - - public string GetNewName(MethodDef methodDef, string originalName) - { - return originalName + "_obfuz_generated__"; - } - - public string GetNewName(FieldDef fieldDef, string originalName) - { - return originalName + "_obfuz_generated__"; - } - - public string GetNewName(PropertyDef propertyDef, string originalName) - { - return originalName + "_obfuz_generated__"; - } - - public string GetNewName(EventDef eventDef, string originalName) - { - return originalName + "_obfuz_generated__"; + return true; } } } diff --git a/Editor/Rename/SymbolRename.cs b/Editor/Rename/SymbolRename.cs index 0cf0160..83425da 100644 --- a/Editor/Rename/SymbolRename.cs +++ b/Editor/Rename/SymbolRename.cs @@ -1,6 +1,9 @@ +using dnlib.DotNet; using Obfuz.Rename; using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; +using UnityEditor.SceneManagement; using UnityEngine; namespace Obfuz @@ -20,7 +23,102 @@ namespace Obfuz public void Process() { + foreach (ObfuzAssemblyInfo ass in _ctx.assemblies) + { + if (_renamePolicy.NeedRename(ass.module)) + { + Rename(ass.module); + } + foreach (TypeDef type in ass.module.GetTypes()) + { + if (type.FullName != "" && _renamePolicy.NeedRename(type)) + { + Rename(type); + } + foreach (FieldDef field in type.Fields) + { + if (_renamePolicy.NeedRename(field)) + { + Rename(field); + } + } + foreach (MethodDef method in type.Methods) + { + if (_renamePolicy.NeedRename(method)) + { + Rename(method); + foreach (Parameter param in method.Parameters) + { + Rename(param.ParamDef); + } + } + } + foreach (EventDef eventDef in type.Events) + { + if (_renamePolicy.NeedRename(eventDef)) + { + Rename(eventDef); + } + } + foreach (PropertyDef property in type.Properties) + { + if (_renamePolicy.NeedRename(property)) + { + Rename(property); + } + } + } + } + } + private List GetReferenceMeAssemblies(ModuleDefMD mod) + { + return _ctx.assemblies.Find(ass => ass.module == mod).referenceMeAssemblies; + } + + private void Rename(ModuleDefMD mod) + { + string oldName = MetaUtil.GetModuleNameWithoutExt(mod.Name); + string newName = _ctx.nameMaker.GetNewName(mod, oldName); + mod.Name = $"{newName}.dll"; + Debug.Log($"rename module. oldName:{oldName} newName:{newName}"); + foreach (ObfuzAssemblyInfo ass in GetReferenceMeAssemblies(mod)) + { + foreach (AssemblyRef assRef in ass.module.GetAssemblyRefs()) + { + if (assRef.Name == oldName) + { + assRef.Name = newName; + Debug.Log($"rename assembly:{ass.name} ref oldName:{oldName} newName:{newName}"); + } + } + } + } + + private void Rename(TypeDef type) + { + } + + private void Rename(FieldDef field) + { + } + + private void Rename(MethodDef method) + { + } + + private void Rename(ParamDef param) + { + + } + + private void Rename(EventDef eventDef) + { + + } + + private void Rename(PropertyDef property) + { } } }