2025-10-29 20:40:37 +08:00
|
|
|
|
// Copyright 2025 Code Philosophy
|
|
|
|
|
|
//
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-05-23 08:18:21 +08:00
|
|
|
|
using System.Linq;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public class UnityRenamePolicy : ObfuscationPolicyBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private static HashSet<string> s_monoBehaviourEvents = new HashSet<string> {
|
2025-05-21 11:49:20 +08:00
|
|
|
|
|
|
|
|
|
|
// MonoBehaviour events
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"Awake",
|
|
|
|
|
|
"FixedUpdate",
|
|
|
|
|
|
"LateUpdate",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnAnimatorIK",
|
|
|
|
|
|
|
|
|
|
|
|
"OnAnimatorMove",
|
|
|
|
|
|
"OnApplicationFocus",
|
|
|
|
|
|
"OnApplicationPause",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnApplicationQuit",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnAudioFilterRead",
|
|
|
|
|
|
|
|
|
|
|
|
"OnBecameVisible",
|
|
|
|
|
|
"OnBecameInvisible",
|
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnCollisionEnter",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnCollisionEnter2D",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnCollisionExit",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnCollisionExit2D",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnCollisionStay",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnCollisionStay2D",
|
|
|
|
|
|
"OnConnectedToServer",
|
|
|
|
|
|
"OnControllerColliderHit",
|
|
|
|
|
|
|
|
|
|
|
|
"OnDrawGizmos",
|
|
|
|
|
|
"OnDrawGizmosSelected",
|
|
|
|
|
|
"OnDestroy",
|
|
|
|
|
|
"OnDisable",
|
|
|
|
|
|
"OnDisconnectedFromServer",
|
|
|
|
|
|
|
|
|
|
|
|
"OnEnable",
|
|
|
|
|
|
|
|
|
|
|
|
"OnFailedToConnect",
|
|
|
|
|
|
"OnFailedToConnectToMasterServer",
|
|
|
|
|
|
|
|
|
|
|
|
"OnGUI",
|
|
|
|
|
|
|
|
|
|
|
|
"OnJointBreak",
|
|
|
|
|
|
"OnJointBreak2D",
|
|
|
|
|
|
|
|
|
|
|
|
"OnMasterServerEvent",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnMouseDown",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnMouseDrag",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnMouseEnter",
|
|
|
|
|
|
"OnMouseExit",
|
|
|
|
|
|
"OnMouseOver",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnMouseUp",
|
|
|
|
|
|
"OnMouseUpAsButton",
|
|
|
|
|
|
|
|
|
|
|
|
"OnNetworkInstantiate",
|
|
|
|
|
|
|
|
|
|
|
|
"OnParticleSystemStopped",
|
|
|
|
|
|
"OnParticleTrigger",
|
|
|
|
|
|
"OnParticleUpdateJobScheduled",
|
|
|
|
|
|
"OnPlayerConnected",
|
|
|
|
|
|
"OnPlayerDisconnected",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnPostRender",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnPreCull",
|
|
|
|
|
|
"OnPreRender",
|
|
|
|
|
|
"OnRenderImage",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnRenderObject",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
|
|
|
|
|
|
"OnSerializeNetworkView",
|
|
|
|
|
|
"OnServerInitialized",
|
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
"OnTransformChildrenChanged",
|
|
|
|
|
|
"OnTransformParentChanged",
|
2025-05-21 11:39:42 +08:00
|
|
|
|
"OnTriggerEnter",
|
|
|
|
|
|
"OnTriggerEnter2D",
|
|
|
|
|
|
"OnTriggerExit",
|
|
|
|
|
|
"OnTriggerExit2D",
|
|
|
|
|
|
"OnTriggerStay",
|
|
|
|
|
|
"OnTriggerStay2D",
|
|
|
|
|
|
|
|
|
|
|
|
"OnValidate",
|
|
|
|
|
|
"OnWillRenderObject",
|
|
|
|
|
|
"Reset",
|
|
|
|
|
|
"Start",
|
|
|
|
|
|
"Update",
|
2025-05-21 11:49:20 +08:00
|
|
|
|
|
|
|
|
|
|
// Animator/StateMachineBehaviour
|
|
|
|
|
|
"OnStateEnter",
|
|
|
|
|
|
"OnStateExit",
|
|
|
|
|
|
"OnStateMove",
|
|
|
|
|
|
"OnStateUpdate",
|
|
|
|
|
|
"OnStateIK",
|
|
|
|
|
|
"OnStateMachineEnter",
|
|
|
|
|
|
"OnStateMachineExit",
|
|
|
|
|
|
|
|
|
|
|
|
// ParticleSystem
|
|
|
|
|
|
"OnParticleTrigger",
|
|
|
|
|
|
"OnParticleCollision",
|
|
|
|
|
|
"OnParticleSystemStopped",
|
|
|
|
|
|
|
|
|
|
|
|
// UGUI/EventSystems
|
|
|
|
|
|
"OnPointerClick",
|
|
|
|
|
|
"OnPointerDown",
|
|
|
|
|
|
"OnPointerUp",
|
|
|
|
|
|
"OnPointerEnter",
|
|
|
|
|
|
"OnPointerExit",
|
|
|
|
|
|
"OnDrag",
|
|
|
|
|
|
"OnBeginDrag",
|
|
|
|
|
|
"OnEndDrag",
|
|
|
|
|
|
"OnDrop",
|
|
|
|
|
|
"OnScroll",
|
|
|
|
|
|
"OnSelect",
|
|
|
|
|
|
"OnDeselect",
|
|
|
|
|
|
"OnMove",
|
|
|
|
|
|
"OnSubmit",
|
|
|
|
|
|
"OnCancel",
|
2025-05-21 09:23:29 +08:00
|
|
|
|
};
|
2025-05-23 08:31:43 +08:00
|
|
|
|
|
2025-05-28 10:11:48 +08:00
|
|
|
|
private readonly CachedDictionary<TypeDef, bool> _computeDeclaringTypeDisableAllMemberRenamingCache;
|
2025-06-28 10:49:30 +08:00
|
|
|
|
private readonly CachedDictionary<TypeDef, bool> _isSerializableCache;
|
2025-10-08 22:25:01 +08:00
|
|
|
|
private readonly CachedDictionary<TypeDef, bool> _isInheritFromMonoBehaviourOrScriptableObjectCache;
|
2025-05-29 16:16:32 +08:00
|
|
|
|
private readonly CachedDictionary<TypeDef, bool> _isScriptOrSerializableTypeCache;
|
2025-05-27 22:21:24 +08:00
|
|
|
|
|
2025-05-28 10:11:48 +08:00
|
|
|
|
public UnityRenamePolicy()
|
|
|
|
|
|
{
|
|
|
|
|
|
_computeDeclaringTypeDisableAllMemberRenamingCache = new CachedDictionary<TypeDef, bool>(ComputeDeclaringTypeDisableAllMemberRenaming);
|
2025-06-28 10:49:30 +08:00
|
|
|
|
_isSerializableCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsSerializableType);
|
2025-10-08 22:25:01 +08:00
|
|
|
|
_isInheritFromMonoBehaviourOrScriptableObjectCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptType);
|
2025-05-29 16:16:32 +08:00
|
|
|
|
_isScriptOrSerializableTypeCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptOrSerializableType);
|
2025-05-28 10:11:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef)
|
2025-05-23 08:31:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (typeDef.Name.StartsWith("UnitySourceGeneratedAssemblyMonoScriptTypes_"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-05-27 20:06:14 +08:00
|
|
|
|
if (typeDef.FullName == "Unity.Entities.CodeGeneratedRegistry.AssemblyTypeRegistry")
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (typeDef.Name.StartsWith("__JobReflectionRegistrationOutput"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-05-27 22:50:05 +08:00
|
|
|
|
if (MetaUtil.HasDOTSCompilerGeneratedAttribute(typeDef))
|
2025-05-27 20:06:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-05-23 08:31:43 +08:00
|
|
|
|
if (typeDef.DeclaringType != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return IsUnitySourceGeneratedAssemblyType(typeDef.DeclaringType);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-28 10:11:48 +08:00
|
|
|
|
private bool ComputeDeclaringTypeDisableAllMemberRenaming(TypeDef typeDef)
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-05-27 20:20:49 +08:00
|
|
|
|
if (typeDef.IsEnum && MetaUtil.HasBlackboardEnumAttribute(typeDef))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-05-27 20:20:49 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (IsUnitySourceGeneratedAssemblyType(typeDef))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
2025-05-27 20:06:14 +08:00
|
|
|
|
if (MetaUtil.IsInheritFromDOTSTypes(typeDef))
|
|
|
|
|
|
{
|
2025-05-27 20:20:49 +08:00
|
|
|
|
return true;
|
2025-05-27 20:06:14 +08:00
|
|
|
|
}
|
2025-05-27 20:20:49 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool NeedRename(TypeDef typeDef)
|
|
|
|
|
|
{
|
2025-05-29 16:16:32 +08:00
|
|
|
|
if (_isScriptOrSerializableTypeCache.GetValue(typeDef))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-28 10:11:48 +08:00
|
|
|
|
if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef))
|
2025-05-23 08:31:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-11 19:08:25 +08:00
|
|
|
|
if (MetaUtil.HasBurstCompileAttribute(typeDef))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-27 20:20:49 +08:00
|
|
|
|
if (typeDef.Methods.Any(m => MetaUtil.HasRuntimeInitializeOnLoadMethodAttribute(m)))
|
2025-05-23 08:18:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool NeedRename(MethodDef methodDef)
|
|
|
|
|
|
{
|
2025-05-23 08:31:43 +08:00
|
|
|
|
TypeDef typeDef = methodDef.DeclaringType;
|
2025-10-08 22:25:01 +08:00
|
|
|
|
if (s_monoBehaviourEvents.Contains(methodDef.Name) && _isInheritFromMonoBehaviourOrScriptableObjectCache.GetValue(typeDef))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-05-23 09:24:24 +08:00
|
|
|
|
return false;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
2025-05-28 10:11:48 +08:00
|
|
|
|
if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef))
|
2025-05-27 20:06:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-23 08:18:21 +08:00
|
|
|
|
if (MetaUtil.HasRuntimeInitializeOnLoadMethodAttribute(methodDef))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-11 19:08:25 +08:00
|
|
|
|
if (MetaUtil.HasBurstCompileAttribute(methodDef) || MetaUtil.HasBurstCompileAttribute(methodDef.DeclaringType) || MetaUtil.HasDOTSCompilerGeneratedAttribute(methodDef))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-05-27 22:50:05 +08:00
|
|
|
|
return false;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool NeedRename(FieldDef fieldDef)
|
|
|
|
|
|
{
|
|
|
|
|
|
TypeDef typeDef = fieldDef.DeclaringType;
|
2025-05-29 16:16:32 +08:00
|
|
|
|
if (_isScriptOrSerializableTypeCache.GetValue(typeDef))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-06-12 14:25:24 +08:00
|
|
|
|
if (typeDef.IsEnum)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-29 16:16:32 +08:00
|
|
|
|
if (fieldDef.IsPublic && !fieldDef.IsStatic)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!fieldDef.IsStatic && MetaUtil.IsSerializableField(fieldDef))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
2025-05-28 10:11:48 +08:00
|
|
|
|
if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-05-29 16:16:32 +08:00
|
|
|
|
|
|
|
|
|
|
public override bool NeedRename(PropertyDef propertyDef)
|
|
|
|
|
|
{
|
|
|
|
|
|
TypeDef typeDef = propertyDef.DeclaringType;
|
2025-06-28 10:49:30 +08:00
|
|
|
|
if (_isSerializableCache.GetValue(typeDef))
|
2025-05-29 16:16:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
bool isGetterPublic = propertyDef.GetMethod != null && propertyDef.GetMethod.IsPublic && !propertyDef.GetMethod.IsStatic;
|
|
|
|
|
|
bool isSetterPublic = propertyDef.SetMethod != null && propertyDef.SetMethod.IsPublic && !propertyDef.SetMethod.IsStatic;
|
|
|
|
|
|
|
|
|
|
|
|
if (isGetterPublic || isSetterPublic)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|