2025-04-14 12:33:48 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEditor.Build;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEditor.Build.Reporting;
|
|
|
|
|
using UnityEngine;
|
2025-04-19 10:13:18 +08:00
|
|
|
|
using UnityEditor.Compilation;
|
2025-05-04 19:24:14 +08:00
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using FileUtil = Obfuz.Utils.FileUtil;
|
2025-05-04 19:55:10 +08:00
|
|
|
|
using Obfuz.Settings;
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
2025-05-04 19:55:10 +08:00
|
|
|
|
namespace Obfuz.Unity
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-04-19 10:13:18 +08:00
|
|
|
|
|
2025-04-19 13:00:38 +08:00
|
|
|
|
#if UNITY_2019_1_OR_NEWER
|
2025-05-15 09:14:48 +08:00
|
|
|
|
public class ObfuscationProcess : IPostBuildPlayerScriptDLLs
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
|
|
|
|
public int callbackOrder => 10000;
|
|
|
|
|
|
2025-05-03 20:41:04 +08:00
|
|
|
|
public static event Action<ObfuscationBeginEventArgs> OnObfuscationBegin;
|
|
|
|
|
|
|
|
|
|
public static event Action<ObfuscationEndEventArgs> OnObfuscationEnd;
|
|
|
|
|
|
2025-05-15 09:14:48 +08:00
|
|
|
|
public void OnPostBuildPlayerScriptDLLs(BuildReport report)
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-05-16 20:02:21 +08:00
|
|
|
|
#if !UNITY_2022_1_OR_NEWER
|
2025-05-16 17:44:28 +08:00
|
|
|
|
RunObfuscate(report.files);
|
|
|
|
|
#else
|
2025-05-15 09:14:48 +08:00
|
|
|
|
RunObfuscate(report.GetFiles());
|
2025-05-16 17:44:28 +08:00
|
|
|
|
#endif
|
2025-04-14 12:33:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 09:14:48 +08:00
|
|
|
|
private static void BackupOriginalDlls(string srcDir, string dstDir, HashSet<string> dllNames)
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-05-15 09:14:48 +08:00
|
|
|
|
FileUtil.RecreateDir(dstDir);
|
|
|
|
|
foreach (string dllName in dllNames)
|
2025-04-19 11:47:05 +08:00
|
|
|
|
{
|
2025-05-15 09:14:48 +08:00
|
|
|
|
string srcFile = Path.Combine(srcDir, dllName);
|
|
|
|
|
string dstFile = Path.Combine(dstDir, dllName);
|
|
|
|
|
if (File.Exists(srcFile))
|
|
|
|
|
{
|
|
|
|
|
File.Copy(srcFile, dstFile, true);
|
|
|
|
|
Debug.Log($"BackupOriginalDll {srcFile} -> {dstFile}");
|
|
|
|
|
}
|
2025-04-14 12:33:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 09:14:48 +08:00
|
|
|
|
private static void RunObfuscate(BuildFile[] files)
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-04-16 23:03:41 +08:00
|
|
|
|
ObfuzSettings settings = ObfuzSettings.Instance;
|
|
|
|
|
if (!settings.enable)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Obfuscation is disabled.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 12:33:48 +08:00
|
|
|
|
Debug.Log("Obfuscation begin...");
|
2025-04-16 23:03:41 +08:00
|
|
|
|
var buildTarget = EditorUserBuildSettings.activeBuildTarget;
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var obfuscationRelativeAssemblyNames = new HashSet<string>(settings.assemblySettings.GetObfuscationRelativeAssemblyNames());
|
2025-05-15 09:14:48 +08:00
|
|
|
|
string stagingAreaTempManagedDllDir = Path.GetDirectoryName(files.First(file => file.path.EndsWith(".dll")).path);
|
2025-04-16 23:03:41 +08:00
|
|
|
|
string backupPlayerScriptAssembliesPath = settings.GetOriginalAssemblyBackupDir(buildTarget);
|
2025-05-15 09:14:48 +08:00
|
|
|
|
BackupOriginalDlls(stagingAreaTempManagedDllDir, backupPlayerScriptAssembliesPath, obfuscationRelativeAssemblyNames);
|
2025-04-16 23:03:41 +08:00
|
|
|
|
|
2025-04-19 10:13:18 +08:00
|
|
|
|
string applicationContentsPath = EditorApplication.applicationContentsPath;
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
2025-05-15 09:14:48 +08:00
|
|
|
|
var obfuscatorBuilder = ObfuscatorBuilder.FromObfuzSettings(settings, buildTarget, false);
|
2025-05-03 21:43:50 +08:00
|
|
|
|
|
|
|
|
|
var assemblySearchDirs = new List<string>
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-05-15 09:14:48 +08:00
|
|
|
|
stagingAreaTempManagedDllDir,
|
2025-05-03 21:43:50 +08:00
|
|
|
|
};
|
|
|
|
|
obfuscatorBuilder.InsertTopPriorityAssemblySearchDirs(assemblySearchDirs);
|
|
|
|
|
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
2025-05-03 22:32:18 +08:00
|
|
|
|
OnObfuscationBegin?.Invoke(new ObfuscationBeginEventArgs
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-05-15 09:14:48 +08:00
|
|
|
|
scriptAssembliesPath = stagingAreaTempManagedDllDir,
|
2025-05-03 22:32:18 +08:00
|
|
|
|
obfuscatedScriptAssembliesPath = obfuscatorBuilder.ObfuscatedAssemblyOutputDir,
|
|
|
|
|
});
|
|
|
|
|
bool succ = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Obfuscator obfuz = obfuscatorBuilder.Build();
|
|
|
|
|
obfuz.Run();
|
2025-04-21 09:57:34 +08:00
|
|
|
|
|
2025-05-15 09:14:48 +08:00
|
|
|
|
foreach (var dllName in obfuscationRelativeAssemblyNames)
|
2025-04-21 09:57:34 +08:00
|
|
|
|
{
|
2025-05-03 22:32:18 +08:00
|
|
|
|
string src = $"{obfuscatorBuilder.ObfuscatedAssemblyOutputDir}/{dllName}.dll";
|
2025-05-15 09:14:48 +08:00
|
|
|
|
string dst = $"{stagingAreaTempManagedDllDir}/{dllName}.dll";
|
2025-05-03 22:32:18 +08:00
|
|
|
|
|
|
|
|
|
if (!File.Exists(src))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"obfuscation assembly not found! skip copy. path:{src}");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
File.Copy(src, dst, true);
|
|
|
|
|
Debug.Log($"obfuscate dll:{dst}");
|
2025-04-21 09:57:34 +08:00
|
|
|
|
}
|
2025-05-03 22:32:18 +08:00
|
|
|
|
succ = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
succ = false;
|
|
|
|
|
Debug.LogException(e);
|
|
|
|
|
Debug.LogError($"Obfuscation failed.");
|
2025-04-14 12:33:48 +08:00
|
|
|
|
}
|
2025-05-03 20:41:04 +08:00
|
|
|
|
OnObfuscationEnd?.Invoke(new ObfuscationEndEventArgs
|
|
|
|
|
{
|
2025-05-03 22:32:18 +08:00
|
|
|
|
success = succ,
|
2025-05-03 20:41:04 +08:00
|
|
|
|
originalScriptAssembliesPath = backupPlayerScriptAssembliesPath,
|
2025-05-15 09:14:48 +08:00
|
|
|
|
obfuscatedScriptAssembliesPath = stagingAreaTempManagedDllDir,
|
2025-05-03 20:41:04 +08:00
|
|
|
|
});
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
|
|
|
|
Debug.Log("Obfuscation end.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-19 10:13:18 +08:00
|
|
|
|
#endif
|
2025-05-16 17:44:28 +08:00
|
|
|
|
}
|