2025-05-07 19:39:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.Emit
|
|
|
|
|
{
|
2025-05-11 08:46:01 +08:00
|
|
|
|
public interface IGroupByModuleEntity
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
|
|
|
|
void Init(ModuleDef mod);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 08:46:01 +08:00
|
|
|
|
public abstract class GroupByModuleEntityBase : IGroupByModuleEntity
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
|
|
|
|
public abstract void Init(ModuleDef mod);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 08:53:48 +08:00
|
|
|
|
public class GroupByModuleEntityManager
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
2025-05-13 08:49:57 +08:00
|
|
|
|
private readonly Dictionary<(ModuleDef, Type), IGroupByModuleEntity> _moduleEntityManagers = new Dictionary<(ModuleDef, Type), IGroupByModuleEntity>();
|
2025-05-07 19:39:09 +08:00
|
|
|
|
|
2025-05-11 08:46:01 +08:00
|
|
|
|
public T GetEntity<T>(ModuleDef mod, Func<T> creator = null) where T : IGroupByModuleEntity
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
|
|
|
|
var key = (mod, typeof(T));
|
2025-05-13 08:49:57 +08:00
|
|
|
|
if (_moduleEntityManagers.TryGetValue(key, out var emitManager))
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)emitManager;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
T newEmitManager;
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
2025-05-09 20:18:24 +08:00
|
|
|
|
newEmitManager = creator();
|
2025-05-07 19:39:09 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newEmitManager = (T)Activator.CreateInstance(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
newEmitManager.Init(mod);
|
2025-05-13 08:49:57 +08:00
|
|
|
|
_moduleEntityManagers[key] = newEmitManager;
|
2025-05-07 19:39:09 +08:00
|
|
|
|
return newEmitManager;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 08:46:01 +08:00
|
|
|
|
public List<T> GetEntities<T>() where T: IGroupByModuleEntity
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
|
|
|
|
var managers = new List<T>();
|
2025-05-13 08:49:57 +08:00
|
|
|
|
foreach (var kv in _moduleEntityManagers)
|
2025-05-07 19:39:09 +08:00
|
|
|
|
{
|
|
|
|
|
if (kv.Key.Item2 == typeof(T))
|
|
|
|
|
{
|
|
|
|
|
managers.Add((T)kv.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return managers;
|
|
|
|
|
}
|
2025-05-11 08:46:01 +08:00
|
|
|
|
|
2025-05-16 12:33:37 +08:00
|
|
|
|
public DefaultMetadataImporter GetDefaultModuleMetadataImporter(ModuleDef module, EncryptionScopeProvider encryptionScopeProvider)
|
2025-05-11 08:46:01 +08:00
|
|
|
|
{
|
2025-05-16 12:33:37 +08:00
|
|
|
|
return GetEntity<DefaultMetadataImporter>(module, () => new DefaultMetadataImporter(encryptionScopeProvider));
|
2025-05-11 08:46:01 +08:00
|
|
|
|
}
|
2025-05-07 19:39:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|