obfuz/Editor/ObfusPasses/CallObfus/DefaultCallProxyObfuscator.cs

54 lines
2.5 KiB
C#
Raw Normal View History

2025-04-28 11:37:48 +08:00
using dnlib.DotNet.Emit;
using dnlib.DotNet;
using System.Collections.Generic;
using Obfuz.Utils;
using Obfuz.Emit;
2025-05-10 11:25:07 +08:00
using Obfuz.Data;
using UnityEngine;
2025-04-28 11:37:48 +08:00
2025-05-04 19:55:10 +08:00
namespace Obfuz.ObfusPasses.CallObfus
2025-04-28 11:37:48 +08:00
{
2025-05-10 09:41:45 +08:00
public class DefaultCallProxyObfuscator : ObfuscatorBase
2025-04-28 11:37:48 +08:00
{
2025-05-16 11:33:03 +08:00
private readonly EncryptionScopeProvider _encryptionScopeProvider;
2025-05-10 11:25:07 +08:00
private readonly ConstFieldAllocator _constFieldAllocator;
2025-05-09 20:18:24 +08:00
private readonly CallProxyAllocator _proxyCallAllocator;
private readonly GroupByModuleEntityManager _moduleEntityManager;
2025-04-28 11:37:48 +08:00
2025-05-16 11:33:03 +08:00
public DefaultCallProxyObfuscator(EncryptionScopeProvider encryptionScopeProvider, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, int encryptionLevel)
2025-04-28 11:37:48 +08:00
{
2025-05-16 11:33:03 +08:00
_encryptionScopeProvider = encryptionScopeProvider;
2025-05-10 11:25:07 +08:00
_constFieldAllocator = constFieldAllocator;
_moduleEntityManager = moduleEntityManager;
2025-05-16 11:33:03 +08:00
_proxyCallAllocator = new CallProxyAllocator(encryptionScopeProvider, moduleEntityManager, encryptionLevel);
2025-04-28 11:37:48 +08:00
}
public override void Done()
{
_proxyCallAllocator.Done();
}
2025-05-10 11:25:07 +08:00
public override void Obfuscate(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool needCacheCall, List<Instruction> obfuscatedInstructions)
2025-04-28 11:37:48 +08:00
{
2025-05-10 11:25:07 +08:00
2025-04-28 11:37:48 +08:00
MethodSig sharedMethodSig = MetaUtil.ToSharedMethodSig(calledMethod.Module.CorLibTypes, MetaUtil.GetInflatedMethodSig(calledMethod));
2025-05-10 11:25:07 +08:00
ProxyCallMethodData proxyCallMethodData = _proxyCallAllocator.Allocate(callerMethod.Module, calledMethod, callVir);
DefaultMetadataImporter importer = _moduleEntityManager.GetDefaultModuleMetadataImporter(callerMethod.Module, _encryptionScopeProvider);
2025-05-10 11:25:07 +08:00
if (needCacheCall)
{
FieldDef cacheField = _constFieldAllocator.Allocate(callerMethod.Module, proxyCallMethodData.index);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
}
else
{
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.encryptedIndex));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.encryptOps));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.salt));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt));
}
2025-04-28 11:37:48 +08:00
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, proxyCallMethodData.proxyMethod));
}
}
}