【加密】测试打包

main
刘涛 2025-10-17 13:12:28 +08:00
parent d66931f20a
commit fb02f5db4c
5 changed files with 72 additions and 18 deletions

View File

@ -5,6 +5,9 @@ using UnityEditor;
using HybridCLR.Editor;
using System.IO;
using System.Linq;
using HybridCLR.Editor.Commands;
using Obfuz.Settings;
using Obfuz4HybridCLR;
using UnityEditor.SceneManagement;
public static class HotUpadeEditorHelper
@ -18,6 +21,33 @@ public static class HotUpadeEditorHelper
const string HOT_UPDATE_DLLS_DIR = "Assets/HotUpdateDlls";
const string AOT_GENERIC_REFERENCES_PATH = "/HybridCLRGenerate/AOTGenericReferences.cs";
[MenuItem("Build/CompileAndObfuscateAndCopyToStreamingAssets")]
public static void CompileAndObfuscateAndCopyDllToTarget()
{
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
string obfuscatedHotUpdateDllPath = PrebuildCommandExt.GetObfuscatedHotUpdateAssemblyOutputPath(target);
ObfuscateUtil.ObfuscateHotUpdateAssemblies(target, obfuscatedHotUpdateDllPath);
Directory.CreateDirectory(Application.streamingAssetsPath);
string hotUpdateDllPath = $"{SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target)}";
List<string> obfuscationRelativeAssemblyNames = ObfuzSettings.Instance.assemblySettings.GetObfuscationRelativeAssemblyNames();
foreach (string assName in SettingsUtil.HotUpdateAssemblyNamesIncludePreserved)
{
string srcDir = obfuscationRelativeAssemblyNames.Contains(assName) ? obfuscatedHotUpdateDllPath : hotUpdateDllPath;
string srcFile = $"{srcDir}/{assName}.dll";
string dstFile = $"{Application.dataPath + HOT_UPDATE_DESTINATION_PATH}{assName}.dll.bytes";
if (File.Exists(srcFile))
{
File.Copy(srcFile, dstFile, true);
Debug.Log($"[CompileAndObfuscate] Copy {srcFile} to {dstFile}");
}
}
}
// [MenuItem("Tools/热更新/转移热更dll")]
public static void CopyHotUpdateDll()

View File

@ -24,24 +24,36 @@ using PhxhSDK.AOT.Bilibili;
public class GameLauncher : MonoBehaviour
{
private static bool _isSetUpStaticSecret = false;
/// <summary>
/// 初始化EncryptionService后被混淆的代码才能正常运行
/// 因此尽可能地早地初始化它。
/// </summary>
[ObfuzIgnore]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void SetUpStaticSecretKey()
private static void _SetUpStaticSecretKey()
{
Debug.Log("SetUpStaticSecret begin");
EncryptionService<DefaultStaticEncryptionScope>.Encryptor = new GeneratedEncryptionVirtualMachine(Resources.Load<TextAsset>("us5af47s54fsa1z90sf").bytes);
Debug.Log("SetUpStaticSecret end");
if (_isSetUpStaticSecret)
{
return;
}
_isSetUpStaticSecret = true;
Debug.Log("[Obfz] SetUpStaticSecret begin");
var loadedAsset = Resources.Load<TextAsset>("us5af47s54fsa1z90sf");
if (loadedAsset == null)
{
Debug.LogError("[Obfz] SetUpStaticSecret failed: could not load static secret asset");
return;
}
EncryptionService<DefaultStaticEncryptionScope>.Encryptor = new GeneratedEncryptionVirtualMachine(loadedAsset.bytes);
Debug.Log("[Obfz] SetUpStaticSecret end");
}
private static void SetUpDynamicSecret(byte[] bytes)
private static void _SetUpDynamicSecret(byte[] bytes)
{
Debug.Log("SetUpDynamicSecret begin");
Debug.Log("[Obfz] SetUpDynamicSecret begin");
EncryptionService<DefaultDynamicEncryptionScope>.Encryptor = new GeneratedEncryptionVirtualMachine(bytes);
Debug.Log("SetUpDynamicSecret end");
Debug.Log("[Obfz] SetUpDynamicSecret end");
}
private const string START_SCENE_NAME = "Assets/Scenes/StartScene.unity";
@ -62,6 +74,7 @@ public class GameLauncher : MonoBehaviour
private void Awake()
{
_SetUpStaticSecretKey();
#if SDK_BILIBILI
GameObject goBiliBiliSDK = new()
{
@ -211,7 +224,7 @@ public class GameLauncher : MonoBehaviour
{
#if USE_HCLR
byte[] dynamicSecret = await _simpleLoader.LoadDllBytes(DYNAMIC_SECRET_PATH);
SetUpDynamicSecret(dynamicSecret);
_SetUpDynamicSecret(dynamicSecret);
await _simpleLoader.UnloadDll(DYNAMIC_SECRET_PATH);
Debug.Log("需要热更新,开始加载程序集");

View File

@ -10,6 +10,7 @@ using HybridCLR.Editor.Link;
using HybridCLR.Editor.Meta;
using Obfuz;
using Obfuz.Settings;
using Obfuz.Unity;
using Obfuz4HybridCLR;
using PhxhSDK.AOT.VersionUpdate;
using UnityEditor;
@ -70,16 +71,22 @@ class BuildLauncher
BuildUtils.SetFileReadable(Application.dataPath + HybirdCLRGenerateDir);
#region 未接入Obfuz
PrebuildCommand.GenerateAll();
#endregion
// #region 未接入Obfuz
// PrebuildCommand.GenerateAll();
// #endregion
#region 接入Obfuz
/*GenerateObfuzLinkXml();
PrebuildCommandExt.GenerateAll();*/
// GenerateObfuzLinkXml();
ObfuzMenu.GenerateEncryptionVM();
ObfuzMenu.SaveSecretFile();
PrebuildCommandExt.GenerateAll();
#endregion
HotUpadeEditorHelper.CopyHotUpdateDll();
// Obfuz 需要重新混淆热更后的dll而不是直接拷贝
HotUpadeEditorHelper.CompileAndObfuscateAndCopyDllToTarget();
// HotUpadeEditorHelper.CopyHotUpdateDll();
HotUpadeEditorHelper.CopyMetaDataDll();
}

@ -1 +1 @@
Subproject commit 34fd5dde2a6bfcccb8a6494c5837782c174d3fdc
Subproject commit 7906e516bf5c354dabd3ce1273acd3c25d42f89a

View File

@ -16,12 +16,16 @@ MonoBehaviour:
enable: 1
linkXmlProcessCallbackOrder: 10000
obfuscationProcessCallbackOrder: 10000
compatibilitySettings:
targetRuntime: 0
assemblySettings:
assembliesToObfuscate: []
nonObfuscatedButReferencingObfuscatedAssemblies:
assembliesToObfuscate:
- Assembly-CSharp
- Framework
- GamePlay
- GameWrapper
- FrameWork
- Phxh
nonObfuscatedButReferencingObfuscatedAssemblies: []
additionalAssemblySearchPaths: []
obfuscateObfuzRuntime: 1
obfuscationPassSettings: