2025-05-21 09:23:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.Emit
|
|
|
|
|
{
|
|
|
|
|
public interface IGroupByModuleEntity
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
GroupByModuleEntityManager Manager { get; set; }
|
|
|
|
|
|
|
|
|
|
ModuleDef Module { get; set; }
|
|
|
|
|
|
|
|
|
|
public EncryptionScopeProvider EncryptionScopeProvider { get; }
|
|
|
|
|
|
|
|
|
|
EncryptionScopeInfo EncryptionScope { get; set; }
|
|
|
|
|
|
|
|
|
|
void Init();
|
2025-07-01 18:46:09 +08:00
|
|
|
|
|
|
|
|
|
void Done();
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class GroupByModuleEntityBase : IGroupByModuleEntity
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
public GroupByModuleEntityManager Manager { get; set; }
|
|
|
|
|
|
|
|
|
|
public ModuleDef Module { get; set; }
|
|
|
|
|
|
|
|
|
|
public EncryptionScopeInfo EncryptionScope { get; set; }
|
|
|
|
|
|
|
|
|
|
public EncryptionScopeProvider EncryptionScopeProvider => Manager.EncryptionScopeProvider;
|
|
|
|
|
|
|
|
|
|
public T GetEntity<T>() where T : IGroupByModuleEntity, new()
|
|
|
|
|
{
|
|
|
|
|
return Manager.GetEntity<T>(Module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void Init();
|
2025-07-01 18:46:09 +08:00
|
|
|
|
|
|
|
|
|
public abstract void Done();
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GroupByModuleEntityManager
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<(ModuleDef, Type), IGroupByModuleEntity> _moduleEntityManagers = new Dictionary<(ModuleDef, Type), IGroupByModuleEntity>();
|
|
|
|
|
|
2025-07-02 18:57:53 +08:00
|
|
|
|
public EncryptionScopeProvider EncryptionScopeProvider { get; set; }
|
|
|
|
|
|
|
|
|
|
public T GetEntity<T>(ModuleDef mod) where T : IGroupByModuleEntity, new()
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
var key = (mod, typeof(T));
|
|
|
|
|
if (_moduleEntityManagers.TryGetValue(key, out var emitManager))
|
|
|
|
|
{
|
|
|
|
|
return (T)emitManager;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
T newEmitManager = new T();
|
|
|
|
|
newEmitManager.Manager = this;
|
|
|
|
|
newEmitManager.Module = mod;
|
|
|
|
|
newEmitManager.EncryptionScope = EncryptionScopeProvider.GetScope(mod);
|
|
|
|
|
newEmitManager.Init();
|
2025-05-21 09:23:29 +08:00
|
|
|
|
_moduleEntityManagers[key] = newEmitManager;
|
|
|
|
|
return newEmitManager;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 18:57:53 +08:00
|
|
|
|
public List<T> GetEntities<T>() where T : IGroupByModuleEntity, new()
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
var managers = new List<T>();
|
|
|
|
|
foreach (var kv in _moduleEntityManagers)
|
|
|
|
|
{
|
|
|
|
|
if (kv.Key.Item2 == typeof(T))
|
|
|
|
|
{
|
|
|
|
|
managers.Add((T)kv.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return managers;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 18:57:53 +08:00
|
|
|
|
public void Done<T>() where T : IGroupByModuleEntity, new()
|
2025-07-01 18:46:09 +08:00
|
|
|
|
{
|
|
|
|
|
var managers = GetEntities<T>();
|
|
|
|
|
foreach (var manager in managers)
|
|
|
|
|
{
|
|
|
|
|
manager.Done();
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|