obfuz/Editor/ObfusPasses/CallObfus/DefaultCallProxyObfuscator.cs

54 lines
2.3 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
{
private readonly IRandom _random;
2025-05-09 20:18:24 +08:00
private readonly IEncryptor _encryptor;
2025-05-10 11:25:07 +08:00
private readonly ConstFieldAllocator _constFieldAllocator;
2025-05-09 20:18:24 +08:00
private readonly CallProxyAllocator _proxyCallAllocator;
2025-04-28 11:37:48 +08:00
2025-05-10 11:25:07 +08:00
public DefaultCallProxyObfuscator(IRandom random, IEncryptor encryptor, ConstFieldAllocator constFieldAllocator)
2025-04-28 11:37:48 +08:00
{
_random = random;
2025-05-09 20:18:24 +08:00
_encryptor = encryptor;
2025-05-10 11:25:07 +08:00
_constFieldAllocator = constFieldAllocator;
2025-05-09 20:18:24 +08:00
_proxyCallAllocator = new CallProxyAllocator(random, _encryptor);
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);
DefaultModuleMetadataImporter importer = MetadataImporter.Instance.GetDefaultModuleMetadataImporter(callerMethod.Module);
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));
}
}
}