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.SceneManagement;
|
|
|
|
|
using UnityEngine;
|
2025-04-19 10:13:18 +08:00
|
|
|
|
using UnityEditor.Compilation;
|
|
|
|
|
using System.Reflection;
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
|
|
|
|
namespace Obfuz
|
|
|
|
|
{
|
2025-04-19 10:13:18 +08:00
|
|
|
|
|
2025-04-19 13:00:38 +08:00
|
|
|
|
#if UNITY_2019_1_OR_NEWER
|
2025-04-19 10:13:18 +08:00
|
|
|
|
internal class ObfuzProcess2021 : IPreprocessBuildWithReport, IPostprocessBuildWithReport
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
|
|
|
|
private static bool s_obfuscated = false;
|
|
|
|
|
|
|
|
|
|
public int callbackOrder => 10000;
|
|
|
|
|
|
2025-04-19 11:47:05 +08:00
|
|
|
|
[InitializeOnLoadMethod]
|
|
|
|
|
private static void Init()
|
|
|
|
|
{
|
|
|
|
|
CompilationPipeline.compilationFinished += OnCompilationFinished;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 12:33:48 +08:00
|
|
|
|
public void OnPreprocessBuild(BuildReport report)
|
|
|
|
|
{
|
|
|
|
|
s_obfuscated = false;
|
2025-04-19 12:55:59 +08:00
|
|
|
|
FileUtil.RemoveDir(GetScriptAssembliesPath());
|
2025-04-19 10:13:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 12:55:59 +08:00
|
|
|
|
private static string GetScriptAssembliesPath()
|
2025-04-19 10:13:18 +08:00
|
|
|
|
{
|
2025-04-19 12:13:52 +08:00
|
|
|
|
#if UNITY_2021_1_OR_NEWER
|
|
|
|
|
//object settings = obj.GetType().GetProperty("settings").GetValue(obj);
|
|
|
|
|
//string path = (string)settings.GetType().GetProperty("OutputDirectory").GetValue(settings);
|
|
|
|
|
//return path;
|
|
|
|
|
return "Library/Bee/PlayerScriptAssemblies";
|
2025-04-19 11:47:05 +08:00
|
|
|
|
#else
|
2025-04-19 12:00:40 +08:00
|
|
|
|
return "Library/PlayerScriptAssemblies";
|
2025-04-19 11:47:05 +08:00
|
|
|
|
#endif
|
2025-04-14 12:33:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 11:47:05 +08:00
|
|
|
|
private static void OnCompilationFinished(object obj)
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-04-19 11:47:05 +08:00
|
|
|
|
if (!BuildPipeline.isBuildingPlayer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!s_obfuscated)
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
2025-04-19 12:55:59 +08:00
|
|
|
|
RunObfuscate(GetScriptAssembliesPath());
|
2025-04-14 12:33:48 +08:00
|
|
|
|
s_obfuscated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnPostprocessBuild(BuildReport report)
|
|
|
|
|
{
|
|
|
|
|
s_obfuscated = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 10:13:18 +08:00
|
|
|
|
private static void RunObfuscate(string scriptAssembliesPath)
|
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-04-16 23:03:41 +08:00
|
|
|
|
string backupPlayerScriptAssembliesPath = settings.GetOriginalAssemblyBackupDir(buildTarget);
|
2025-04-19 10:13:18 +08:00
|
|
|
|
FileUtil.CopyDir(scriptAssembliesPath, backupPlayerScriptAssembliesPath);
|
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
|
|
|
|
|
|
|
|
|
var opt = new Obfuscator.Options
|
|
|
|
|
{
|
|
|
|
|
AssemblySearchDirs = new List<string>
|
|
|
|
|
{
|
2025-04-19 11:47:05 +08:00
|
|
|
|
#if UNITY_2021_1_OR_NEWER
|
2025-04-19 10:13:18 +08:00
|
|
|
|
Path.Combine(applicationContentsPath, "UnityReferenceAssemblies/unity-4.8-api/Facades"),
|
|
|
|
|
Path.Combine(applicationContentsPath, "UnityReferenceAssemblies/unity-4.8-api"),
|
2025-04-19 12:00:40 +08:00
|
|
|
|
#elif UNITY_2020 || UNITY_2019
|
2025-04-19 11:47:05 +08:00
|
|
|
|
Path.Combine(applicationContentsPath, "MonoBleedingEdge/lib/mono/4.7.1-api/Facades"),
|
|
|
|
|
Path.Combine(applicationContentsPath, "MonoBleedingEdge/lib/mono/4.7.1-api"),
|
|
|
|
|
#else
|
2025-04-19 12:55:59 +08:00
|
|
|
|
#error "Unsupported Unity version"
|
2025-04-19 11:47:05 +08:00
|
|
|
|
#endif
|
2025-04-19 10:13:18 +08:00
|
|
|
|
Path.Combine(applicationContentsPath, "Managed/UnityEngine"),
|
2025-04-14 12:33:48 +08:00
|
|
|
|
backupPlayerScriptAssembliesPath,
|
|
|
|
|
},
|
2025-04-18 19:01:11 +08:00
|
|
|
|
ObfuscationRuleFiles = settings.ruleFiles.ToList(),
|
2025-04-18 08:58:13 +08:00
|
|
|
|
mappingXmlPath = settings.mappingFile,
|
2025-04-16 23:03:41 +08:00
|
|
|
|
outputDir = ObfuzSettings.Instance.GetObfuscatedAssemblyOutputDir(buildTarget),
|
2025-04-14 12:33:48 +08:00
|
|
|
|
};
|
|
|
|
|
var obfuz = new Obfuscator(opt);
|
2025-04-16 23:03:41 +08:00
|
|
|
|
obfuz.Run();
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
2025-04-17 22:02:48 +08:00
|
|
|
|
foreach (var dllName in obfuz.ObfuscatedAssemblyNames)
|
2025-04-14 12:33:48 +08:00
|
|
|
|
{
|
|
|
|
|
string src = $"{opt.outputDir}/{dllName}.dll";
|
2025-04-19 10:13:18 +08:00
|
|
|
|
string dst = $"{scriptAssembliesPath}/{dllName}.dll";
|
2025-04-14 12:33:48 +08:00
|
|
|
|
File.Copy(src, dst, true);
|
2025-04-16 23:03:41 +08:00
|
|
|
|
Debug.Log($"obfuscate dll:{dst}");
|
2025-04-14 12:33:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log("Obfuscation end.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-19 10:13:18 +08:00
|
|
|
|
#endif
|
2025-04-19 12:55:59 +08:00
|
|
|
|
}
|