77using OTAPI . UnifiedServerProcess . Extensions ;
88using System ;
99using System . Collections . Generic ;
10+ using System . Collections . Immutable ;
1011using System . Linq ;
1112
1213namespace OTAPI . UnifiedServerProcess
1314{
15+ public record struct MergeOption ( ImmutableArray < string > IgnoreExistingMethods ) ;
1416 public class ModAssemblyMerger
1517 {
18+ readonly HashSet < string > IgnoreExistingMethods ;
1619 readonly Dictionary < string , ModuleDefinition > modModules = [ ] ;
17- public ModAssemblyMerger ( params System . Reflection . Assembly [ ] mods ) {
20+ public ModAssemblyMerger ( MergeOption option , params System . Reflection . Assembly [ ] mods ) {
21+
22+ IgnoreExistingMethods = [ .. option . IgnoreExistingMethods ] ;
23+
1824 foreach ( System . Reflection . Assembly assembly in mods ) {
1925 var mod = AssemblyDefinition . ReadAssembly ( assembly . Location ) ;
2026 modModules . TryAdd ( mod . FullName , mod . MainModule ) ;
@@ -270,7 +276,7 @@ static MethodReference RedirectElementMethodRef(ModuleDefinition target, ModuleD
270276
271277 return mappedMethod ;
272278 }
273- static void SetModTypePlaceholder ( ModuleDefinition module , Dictionary < string , TypeDefinition > uspTypes , TypeDefinition modType , TypeDefinition ? declaringType ) {
279+ void SetModTypePlaceholder ( ModuleDefinition module , Dictionary < string , TypeDefinition > uspTypes , TypeDefinition modType , TypeDefinition ? declaringType ) {
274280 if ( ! uspTypes . TryGetValue ( modType . FullName , out TypeDefinition ? target ) ) {
275281 target = new TypeDefinition ( modType . Namespace , modType . Name , modType . Attributes , modType . BaseType ) {
276282 Attributes = modType . Attributes ,
@@ -315,7 +321,8 @@ static void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, Ty
315321 SetModTypePlaceholder ( module , uspTypes , nested , target ) ;
316322 }
317323 }
318- static void PrepareMethod ( TypeDefinition targetType , MethodDefinition modMethod , MethodDefinition ? originalMethod ) {
324+ void PrepareMethod ( TypeDefinition targetType , MethodDefinition modMethod , MethodDefinition ? originalMethod ) {
325+ bool ignored = false ;
319326 if ( modMethod . IsConstructor && ! modMethod . IsStatic ) {
320327 int instCount = 0 ;
321328 foreach ( Instruction ? inst in modMethod . Body . Instructions ) {
@@ -324,14 +331,21 @@ static void PrepareMethod(TypeDefinition targetType, MethodDefinition modMethod,
324331 }
325332 }
326333 if ( instCount <= 3 && originalMethod is not null ) {
327- TypeReference attType_ctor = modMethod . Module . ImportReference ( typeof ( MonoMod . MonoModIgnore ) ) ;
328- modMethod . CustomAttributes . Add ( new CustomAttribute ( new MethodReference ( ".ctor" , modMethod . Module . TypeSystem . Void , attType_ctor ) { HasThis = true } ) ) ;
334+ if ( ! ignored ) {
335+ ignored = true ;
336+ TypeReference attType_ctor = modMethod . Module . ImportReference ( typeof ( MonoMod . MonoModIgnore ) ) ;
337+ modMethod . CustomAttributes . Add ( new CustomAttribute ( new MethodReference ( ".ctor" , modMethod . Module . TypeSystem . Void , attType_ctor ) { HasThis = true } ) ) ;
338+ }
329339 }
330340 else {
331341 TypeReference attType_ctor = modMethod . Module . ImportReference ( typeof ( MonoMod . MonoModConstructor ) ) ;
332342 modMethod . CustomAttributes . Add ( new CustomAttribute ( new MethodReference ( ".ctor" , modMethod . Module . TypeSystem . Void , attType_ctor ) { HasThis = true } ) ) ;
333343 }
334344 }
345+ if ( ! ignored && originalMethod is not null && IgnoreExistingMethods . Contains ( modMethod . Name ) ) {
346+ TypeReference attType_ctor = modMethod . Module . ImportReference ( typeof ( MonoMod . MonoModIgnore ) ) ;
347+ modMethod . CustomAttributes . Add ( new CustomAttribute ( new MethodReference ( ".ctor" , modMethod . Module . TypeSystem . Void , attType_ctor ) { HasThis = true } ) ) ;
348+ }
335349 }
336350 static void SetMemberReplace ( ModuleDefinition module , Collection < CustomAttribute > attributes , bool isEnum ) {
337351 Type type = isEnum ? typeof ( MonoMod . MonoModEnumReplace ) : typeof ( MonoMod . MonoModReplace ) ;
0 commit comments