obfuz/Editor/Emit/GroupByModuleEntityManager.cs

110 lines
3.6 KiB
C#
Raw Normal View History

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.
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; }
EncryptionScopeProvider EncryptionScopeProvider { get; }
2025-07-02 18:57:53 +08:00
EncryptionScopeInfo EncryptionScope { get; set; }
void Init();
2025-07-01 18:46:09 +08:00
void Done();
}
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();
}
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()
{
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();
_moduleEntityManagers[key] = newEmitManager;
return newEmitManager;
}
}
2025-07-02 18:57:53 +08:00
public List<T> GetEntities<T>() where T : IGroupByModuleEntity, new()
{
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();
}
}
}
}