backup
walon 2025-04-05 19:02:50 +08:00
parent 9c8746728a
commit 907f6a9dfa
13 changed files with 407 additions and 0 deletions

75
Editor/AssemblyCache.cs Normal file
View File

@ -0,0 +1,75 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
public class AssemblyCache
{
private readonly IAssemblyResolver _assemblyPathResolver;
private readonly ModuleContext _modCtx;
private readonly AssemblyResolver _asmResolver;
public ModuleContext ModCtx => _modCtx;
public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
public AssemblyCache(IAssemblyResolver assemblyResolver)
{
_assemblyPathResolver = assemblyResolver;
_modCtx = ModuleDef.CreateModuleContext();
_asmResolver = (AssemblyResolver)_modCtx.AssemblyResolver;
_asmResolver.EnableTypeDefCache = true;
_asmResolver.UseGAC = false;
}
public ModuleDefMD TryLoadModule(string moduleName)
{
string dllPath = _assemblyPathResolver.ResolveAssembly(moduleName);
if (string.IsNullOrEmpty(dllPath))
{
return null;
}
return LoadModule(moduleName);
}
public ModuleDefMD LoadModule(string moduleName)
{
// Debug.Log($"load module:{moduleName}");
if (LoadedModules.TryGetValue(moduleName, out var mod))
{
return mod;
}
string assemblyPath = _assemblyPathResolver.ResolveAssembly(moduleName);
if (string.IsNullOrEmpty(assemblyPath))
{
throw new FileNotFoundException($"Assembly {moduleName} not found");
}
mod = DoLoadModule(assemblyPath);
LoadedModules.Add(moduleName, mod);
foreach (var refAsm in mod.GetAssemblyRefs())
{
LoadModule(refAsm.Name);
}
return mod;
}
private ModuleDefMD DoLoadModule(string dllPath)
{
//Debug.Log($"do load module:{dllPath}");
ModuleDefMD mod = ModuleDefMD.Load(File.ReadAllBytes(dllPath), _modCtx);
mod.EnableTypeDefFindCache = true;
_asmResolver.AddToCache(mod);
return mod;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
public abstract class AssemblyResolverBase : IAssemblyResolver
{
public abstract string ResolveAssembly(string assemblyName);
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
public interface IAssemblyResolver
{
string ResolveAssembly(string assemblyName);
}
}

77
Editor/Obfuscator.cs Normal file
View File

@ -0,0 +1,77 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
public class Obfuscator
{
public class Options
{
public List<string> AssemblySearchDirs;
public List<string> ObfusAssemblyNames;
public string outputDir;
}
private readonly Options _options;
private readonly AssemblyCache _assemblyCache;
private readonly List<ObfuzAssemblyInfo> _obfuzAssemblies = new List<ObfuzAssemblyInfo>();
public Obfuscator(Options options)
{
_options = options;
_assemblyCache = new AssemblyCache(new PathAssemblyResolver(options.AssemblySearchDirs.ToArray()));
}
public void DoIt()
{
LoadAssemblies();
Rename();
}
private void LoadAssemblies()
{
foreach (string assName in _options.ObfusAssemblyNames)
{
ModuleDefMD mod = _assemblyCache.LoadModule(assName);
var obfuzAsm = new ObfuzAssemblyInfo
{
name = assName,
module = mod,
referenceMeAssemblies = new List<ObfuzAssemblyInfo>(),
};
_obfuzAssemblies.Add(obfuzAsm);
}
var assByName = _obfuzAssemblies.ToDictionary(x => x.name);
foreach (var ass in _obfuzAssemblies)
{
foreach (var refAss in ass.module.GetAssemblyRefs())
{
string refAssName = refAss.Name.ToString();
if (assByName.TryGetValue(refAssName, out var refAssembly))
{
UnityEngine.Debug.Log($"assembly:{ass.name} reference to {refAssName}");
refAssembly.referenceMeAssemblies.Add(ass);
}
}
}
}
private void Rename()
{
var ctx = new ObfuscatorContext
{
assemblies = _obfuzAssemblies,
};
var sr = new SymbolRename(ctx);
sr.Process();
}
}
}

View File

@ -0,0 +1,23 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
public class ObfuzAssemblyInfo
{
public string name;
public ModuleDefMD module;
public List<ObfuzAssemblyInfo> referenceMeAssemblies;
}
public class ObfuscatorContext
{
public List<ObfuzAssemblyInfo> assemblies;
}
}

View File

@ -0,0 +1,16 @@
{
"name": "Obfuz.Editor",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

7
Editor/ObfuzPipeline.cs Normal file
View File

@ -0,0 +1,7 @@
namespace Obfuz
{
public class ObfuzPipeline
{
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Obfuz
{
public class PathAssemblyResolver : AssemblyResolverBase
{
private readonly string[] _searchPaths;
public PathAssemblyResolver(params string[] searchPaths)
{
_searchPaths = searchPaths;
}
public override string ResolveAssembly(string assemblyName)
{
foreach(var path in _searchPaths)
{
string assPath = Path.Combine(path, assemblyName + ".dll");
if (File.Exists(assPath))
{
Debug.Log($"resolve {assemblyName} at {assPath}");
return assPath;
}
}
return null;
}
}
}

View File

@ -0,0 +1,102 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Rename
{
public interface IRenamePolicy
{
bool NeedKeepName(ModuleDefMD mod);
bool NeedKeepName(TypeDef typeDef);
bool NeedKeepName(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);
}
public class RenamePolicy : IRenamePolicy
{
public bool NeedKeepName(ModuleDefMD mod)
{
return false;
}
public bool NeedKeepName(TypeDef typeDef)
{
return false;
}
public bool NeedKeepName(MethodDef methodDef)
{
return false;
}
public bool NeedKeepName(FieldDef fieldDef)
{
return false;
}
public bool NeedKeepName(PropertyDef propertyDef)
{
return false;
}
public bool NeedKeepName(EventDef eventDef)
{
return false;
}
public string GetNewName(ModuleDefMD mod, string originalName)
{
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__";
}
}
}

View File

@ -0,0 +1,26 @@
using Obfuz.Rename;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obfuz
{
public class SymbolRename
{
private readonly ObfuscatorContext _ctx;
private readonly IRenamePolicy _renamePolicy;
public SymbolRename(ObfuscatorContext ctx)
{
_ctx = ctx;
_renamePolicy = new RenamePolicy();
}
public void Process()
{
}
}
}

BIN
Plugins/dnlib.dll Normal file

Binary file not shown.

View File

@ -1,2 +1,3 @@
# Obfuz # Obfuz
Obfuz is a powerful code obfuscation tool designed specifically for Unity projects. Obfuz is a powerful code obfuscation tool designed specifically for Unity projects.

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "com.code-philosophy.obfuz",
"version": "7.9.0",
"displayName": "Obfuz",
"description": "Obfuz is a powerful code obfuscation tool designed specifically for Unity projects.",
"category": "Editor",
"documentationUrl": "https://obfuz.doc.code-philosophy.com/#/",
"changelogUrl": "https://obfuz.doc.code-philosophy.com/RELEASELOG.MD",
"licensesUrl": "https://github.com/focus-creative-games/obfuz/blob/main/LICENSE",
"keywords": [
"obfuscation",
"obfuscator",
"code-philosophy"
],
"author": {
"name": "Code Philosophy",
"email": "obfuz@code-philosophy.com",
"url": "https://code-philosophy.com"
}
}