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;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz
|
|
|
|
|
{
|
|
|
|
|
internal class ObfuzProcess : IPreprocessBuildWithReport, IProcessSceneWithReport, IPostprocessBuildWithReport
|
|
|
|
|
{
|
|
|
|
|
private static bool s_inBuild = false;
|
|
|
|
|
private static bool s_obfuscated = false;
|
|
|
|
|
|
|
|
|
|
public int callbackOrder => 10000;
|
|
|
|
|
|
|
|
|
|
public void OnPreprocessBuild(BuildReport report)
|
|
|
|
|
{
|
|
|
|
|
s_inBuild = true;
|
|
|
|
|
s_obfuscated = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnProcessScene(Scene scene, BuildReport report)
|
|
|
|
|
{
|
|
|
|
|
if (s_inBuild && !s_obfuscated)
|
|
|
|
|
{
|
|
|
|
|
RunObfuscate();
|
|
|
|
|
s_obfuscated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnPostprocessBuild(BuildReport report)
|
|
|
|
|
{
|
|
|
|
|
s_inBuild = false;
|
|
|
|
|
s_obfuscated = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RunObfuscate()
|
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
string originalPlayerScriptAssembliesPath = @"Library\Bee\PlayerScriptAssemblies";
|
2025-04-16 23:03:41 +08:00
|
|
|
|
string backupPlayerScriptAssembliesPath = settings.GetOriginalAssemblyBackupDir(buildTarget);
|
2025-04-14 12:33:48 +08:00
|
|
|
|
BashUtil.CopyDir(originalPlayerScriptAssembliesPath, backupPlayerScriptAssembliesPath);
|
|
|
|
|
|
2025-04-16 23:03:41 +08:00
|
|
|
|
|
2025-04-14 12:33:48 +08:00
|
|
|
|
|
|
|
|
|
var opt = new Obfuscator.Options
|
|
|
|
|
{
|
|
|
|
|
AssemblySearchDirs = new List<string>
|
|
|
|
|
{
|
|
|
|
|
@"D:\UnityHubs\2022.3.60f1\Editor\Data\MonoBleedingEdge\lib\mono\unityaot-win32\Facades",
|
|
|
|
|
@"D:\UnityHubs\2022.3.60f1\Editor\Data\MonoBleedingEdge\lib\mono\unityaot-win32",
|
|
|
|
|
@"D:\UnityHubs\2022.3.60f1\Editor\Data\PlaybackEngines\windowsstandalonesupport\Variations\il2cpp\Managed",
|
|
|
|
|
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";
|
|
|
|
|
string dst = $"{originalPlayerScriptAssembliesPath}/{dllName}.dll";
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|