change: obfuz will try find managed dll in project which is compatible with current build target.
parent
2f7e2be97a
commit
f265022d0c
|
@ -5,6 +5,7 @@ using Obfuz.EncryptionVM;
|
|||
using Obfuz.ObfusPasses;
|
||||
using Obfuz.ObfusPasses.CleanUp;
|
||||
using Obfuz.ObfusPasses.SymbolObfus;
|
||||
using Obfuz.Unity;
|
||||
using Obfuz.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -13,6 +14,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using IAssemblyResolver = Obfuz.Utils.IAssemblyResolver;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
|
@ -23,7 +25,7 @@ namespace Obfuz
|
|||
|
||||
private readonly List<string> _assembliesToObfuscate;
|
||||
private readonly List<string> _nonObfuscatedButReferencingObfuscatedAssemblies;
|
||||
private readonly List<string> _assemblySearchPaths;
|
||||
private readonly CombinedAssemblyResolver _assemblyResolver;
|
||||
|
||||
private readonly ConfigurablePassPolicy _passPolicy;
|
||||
|
||||
|
@ -56,7 +58,7 @@ namespace Obfuz
|
|||
_assembliesToObfuscate = builder.AssembliesToObfuscate;
|
||||
_nonObfuscatedButReferencingObfuscatedAssemblies = builder.NonObfuscatedButReferencingObfuscatedAssemblies;
|
||||
_obfuscatedAssemblyOutputPath = builder.ObfuscatedAssemblyOutputPath;
|
||||
_assemblySearchPaths = builder.AssemblySearchPaths;
|
||||
_assemblyResolver = new CombinedAssemblyResolver(new PathAssemblyResolver(builder.AssemblySearchPaths.ToArray()), new UnityProjectManagedAssemblyResolver(builder.BuildTarget));
|
||||
|
||||
_passPolicy = new ConfigurablePassPolicy(_assembliesToObfuscate, builder.EnableObfuscationPasses, builder.ObfuscationPassRuleConfigFiles);
|
||||
|
||||
|
@ -80,7 +82,7 @@ namespace Obfuz
|
|||
Debug.Log($"Obfuscator Run. begin");
|
||||
FileUtil.RecreateDir(_obfuscatedAssemblyOutputPath);
|
||||
RunPipeline(_pipeline1);
|
||||
_assemblySearchPaths.Insert(0, _obfuscatedAssemblyOutputPath);
|
||||
_assemblyResolver.InsertFirst(new PathAssemblyResolver(_obfuscatedAssemblyOutputPath));
|
||||
RunPipeline(_pipeline2);
|
||||
Debug.Log($"Obfuscator Run. end");
|
||||
}
|
||||
|
@ -263,7 +265,7 @@ namespace Obfuz
|
|||
|
||||
private void OnPreObfuscation(Pipeline pipeline)
|
||||
{
|
||||
AssemblyCache assemblyCache = new AssemblyCache(new PathAssemblyResolver(_assemblySearchPaths.ToArray()));
|
||||
AssemblyCache assemblyCache = new AssemblyCache(_assemblyResolver);
|
||||
List<ModuleDef> modulesToObfuscate = new List<ModuleDef>();
|
||||
List<ModuleDef> allObfuscationRelativeModules = new List<ModuleDef>();
|
||||
LoadAssemblies(assemblyCache, modulesToObfuscate, allObfuscationRelativeModules);
|
||||
|
|
|
@ -15,6 +15,8 @@ namespace Obfuz
|
|||
{
|
||||
public class ObfuscatorBuilder
|
||||
{
|
||||
private BuildTarget _buildTarget;
|
||||
|
||||
private string _defaultStaticSecretKey;
|
||||
private string _defaultStaticSecretKeyOutputPath;
|
||||
private string _defaultDynamicSecretKey;
|
||||
|
@ -36,6 +38,12 @@ namespace Obfuz
|
|||
private ObfuscationPassType _enabledObfuscationPasses;
|
||||
private List<IObfuscationPass> _obfuscationPasses = new List<IObfuscationPass>();
|
||||
|
||||
public BuildTarget BuildTarget
|
||||
{
|
||||
get => _buildTarget;
|
||||
set => _buildTarget = value;
|
||||
}
|
||||
|
||||
public string DefaultStaticSecretKey
|
||||
{
|
||||
get => _defaultStaticSecretKey;
|
||||
|
@ -173,6 +181,7 @@ namespace Obfuz
|
|||
: settings.assemblySettings.additionalAssemblySearchPaths.ToList();
|
||||
var builder = new ObfuscatorBuilder
|
||||
{
|
||||
_buildTarget = target,
|
||||
_defaultStaticSecretKey = settings.secretSettings.defaultStaticSecretKey,
|
||||
_defaultStaticSecretKeyOutputPath = settings.secretSettings.DefaultStaticSecretKeyOutputPath,
|
||||
_defaultDynamicSecretKey = settings.secretSettings.defaultDynamicSecretKey,
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
using Obfuz.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Obfuz.Unity
|
||||
{
|
||||
public class UnityProjectManagedAssemblyResolver : AssemblyResolverBase
|
||||
{
|
||||
private readonly Dictionary<string, string> _managedAssemblyNameToPaths = new Dictionary<string, string>();
|
||||
|
||||
public UnityProjectManagedAssemblyResolver(BuildTarget target)
|
||||
{
|
||||
string[] dllGuids = AssetDatabase.FindAssets("t:DefaultAsset");
|
||||
var dllPaths = dllGuids.Select(guid => AssetDatabase.GUIDToAssetPath(guid))
|
||||
.Where(f => f.EndsWith(".dll"))
|
||||
.Where(dllPath =>
|
||||
{
|
||||
PluginImporter importer = AssetImporter.GetAtPath(dllPath) as PluginImporter;
|
||||
if (importer == null || importer.isNativePlugin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!importer.GetCompatibleWithAnyPlatform() && !importer.GetCompatibleWithPlatform(target))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}).ToArray();
|
||||
|
||||
foreach (string dllPath in dllPaths)
|
||||
{
|
||||
Debug.Log($"UnityProjectManagedAssemblyResolver find managed dll:{dllPath}");
|
||||
string assName = Path.GetFileNameWithoutExtension(dllPath);
|
||||
if (_managedAssemblyNameToPaths.TryGetValue(assName, out var existAssPath))
|
||||
{
|
||||
Debug.LogWarning($"UnityProjectManagedAssemblyResolver find duplicate assembly1:{existAssPath} assembly2:{dllPath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_managedAssemblyNameToPaths.Add(Path.GetFileNameWithoutExtension(dllPath), dllPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string ResolveAssembly(string assemblyName)
|
||||
{
|
||||
if (_managedAssemblyNameToPaths.TryGetValue(assemblyName, out string assemblyPath))
|
||||
{
|
||||
return assemblyPath;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 20abbffaf6419c448883ffaa21949753
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration.Assemblies;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz.Utils
|
||||
{
|
||||
public class CombinedAssemblyResolver : AssemblyResolverBase
|
||||
{
|
||||
private readonly List<IAssemblyResolver> _resolvers;
|
||||
|
||||
public CombinedAssemblyResolver(params IAssemblyResolver[] resolvers)
|
||||
{
|
||||
_resolvers = resolvers.ToList();
|
||||
}
|
||||
|
||||
public override string ResolveAssembly(string assemblyName)
|
||||
{
|
||||
foreach (var resolver in _resolvers)
|
||||
{
|
||||
var assemblyPath = resolver.ResolveAssembly(assemblyName);
|
||||
if (assemblyPath != null)
|
||||
{
|
||||
return assemblyPath;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void InsertFirst(IAssemblyResolver resolver)
|
||||
{
|
||||
_resolvers.Insert(0, resolver);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1576f3534f31509458104d2a7ebcd9cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue