diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 235b722..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "dnlib"] - path = dnlib -url=../dnlib.git \ No newline at end of file diff --git a/Confuser.Core/Confuser.Core.csproj b/Confuser.Core/Confuser.Core.csproj index a804950..b45038d 100644 --- a/Confuser.Core/Confuser.Core.csproj +++ b/Confuser.Core/Confuser.Core.csproj @@ -15,15 +15,12 @@ - - + + + + - - - - - Designer diff --git a/Confuser.Core/Helpers/InjectHelper.cs b/Confuser.Core/Helpers/InjectHelper.cs index cc86ad7..ff0dc29 100644 --- a/Confuser.Core/Helpers/InjectHelper.cs +++ b/Confuser.Core/Helpers/InjectHelper.cs @@ -58,23 +58,20 @@ namespace Confuser.Core.Helpers { /// The injection context. /// The new TypeDef. static TypeDef PopulateContext(TypeDef typeDef, InjectContext ctx) { - TypeDef ret; - IDnlibDef existing; - if (!ctx.Map.TryGetValue(typeDef, out existing)) { + var ret = ctx.Map(typeDef)?.ResolveTypeDef(); + if (ret is null) { ret = Clone(typeDef); - ctx.Map[typeDef] = ret; + ctx.DefMap[typeDef] = ret; } - else - ret = (TypeDef)existing; foreach (TypeDef nestedType in typeDef.NestedTypes) ret.NestedTypes.Add(PopulateContext(nestedType, ctx)); foreach (MethodDef method in typeDef.Methods) - ret.Methods.Add((MethodDef)(ctx.Map[method] = Clone(method))); + ret.Methods.Add((MethodDef)(ctx.DefMap[method] = Clone(method))); foreach (FieldDef field in typeDef.Fields) - ret.Fields.Add((FieldDef)(ctx.Map[field] = Clone(field))); + ret.Fields.Add((FieldDef)(ctx.DefMap[field] = Clone(field))); return ret; } @@ -85,12 +82,11 @@ namespace Confuser.Core.Helpers { /// The origin TypeDef. /// The injection context. static void CopyTypeDef(TypeDef typeDef, InjectContext ctx) { - var newTypeDef = (TypeDef)ctx.Map[typeDef]; - - newTypeDef.BaseType = (ITypeDefOrRef)ctx.Importer.Import(typeDef.BaseType); + var newTypeDef = ctx.Map(typeDef)?.ResolveTypeDefThrow(); + newTypeDef.BaseType = ctx.Importer.Import(typeDef.BaseType); foreach (InterfaceImpl iface in typeDef.Interfaces) - newTypeDef.Interfaces.Add(new InterfaceImplUser((ITypeDefOrRef)ctx.Importer.Import(iface.Interface))); + newTypeDef.Interfaces.Add(new InterfaceImplUser(ctx.Importer.Import(iface.Interface))); } /// @@ -99,7 +95,7 @@ namespace Confuser.Core.Helpers { /// The origin MethodDef. /// The injection context. static void CopyMethodDef(MethodDef methodDef, InjectContext ctx) { - var newMethodDef = (MethodDef)ctx.Map[methodDef]; + var newMethodDef = ctx.Map(methodDef)?.ResolveMethodDefThrow(); newMethodDef.Signature = ctx.Importer.Import(methodDef.Signature); newMethodDef.Parameters.UpdateParameterTypes(); @@ -169,7 +165,7 @@ namespace Confuser.Core.Helpers { /// The origin FieldDef. /// The injection context. static void CopyFieldDef(FieldDef fieldDef, InjectContext ctx) { - var newFieldDef = (FieldDef)ctx.Map[fieldDef]; + var newFieldDef = ctx.Map(fieldDef).ResolveFieldDefThrow(); newFieldDef.Signature = ctx.Importer.Import(fieldDef.Signature); } @@ -202,9 +198,9 @@ namespace Confuser.Core.Helpers { /// The injected TypeDef. public static TypeDef Inject(TypeDef typeDef, ModuleDef target) { var ctx = new InjectContext(typeDef.Module, target); - PopulateContext(typeDef, ctx); + var result = PopulateContext(typeDef, ctx); Copy(typeDef, ctx, true); - return (TypeDef)ctx.Map[typeDef]; + return result; } /// @@ -215,9 +211,10 @@ namespace Confuser.Core.Helpers { /// The injected MethodDef. public static MethodDef Inject(MethodDef methodDef, ModuleDef target) { var ctx = new InjectContext(methodDef.Module, target); - ctx.Map[methodDef] = Clone(methodDef); + MethodDef result; + ctx.DefMap[methodDef] = result = Clone(methodDef); CopyMethodDef(methodDef, ctx); - return (MethodDef)ctx.Map[methodDef]; + return result; } /// @@ -229,20 +226,20 @@ namespace Confuser.Core.Helpers { /// Injected members. public static IEnumerable Inject(TypeDef typeDef, TypeDef newType, ModuleDef target) { var ctx = new InjectContext(typeDef.Module, target); - ctx.Map[typeDef] = newType; + ctx.DefMap[typeDef] = newType; PopulateContext(typeDef, ctx); Copy(typeDef, ctx, false); - return ctx.Map.Values.Except(new[] { newType }); + return ctx.DefMap.Values.Except(new[] { newType }).OfType(); } /// /// Context of the injection process. /// - class InjectContext : ImportResolver { + class InjectContext : ImportMapper { /// /// The mapping of origin definitions to injected definitions. /// - public readonly Dictionary Map = new Dictionary(); + public readonly Dictionary DefMap = new Dictionary(); /// /// The module which source type originated from. @@ -254,11 +251,6 @@ namespace Confuser.Core.Helpers { /// public readonly ModuleDef TargetModule; - /// - /// The importer. - /// - readonly Importer importer; - /// /// Initializes a new instance of the class. /// @@ -267,36 +259,39 @@ namespace Confuser.Core.Helpers { public InjectContext(ModuleDef module, ModuleDef target) { OriginModule = module; TargetModule = target; - importer = new Importer(target, ImporterOptions.TryToUseTypeDefs); - importer.Resolver = this; + Importer = new Importer(target, ImporterOptions.TryToUseTypeDefs, new GenericParamContext(), this); } /// /// Gets the importer. /// /// The importer. - public Importer Importer { - get { return importer; } - } + public Importer Importer { get; } /// - public override TypeDef Resolve(TypeDef typeDef) { - if (Map.ContainsKey(typeDef)) - return (TypeDef)Map[typeDef]; + public override ITypeDefOrRef Map(ITypeDefOrRef source) { + if (DefMap.TryGetValue(source, out var mappedRef)) + return mappedRef as ITypeDefOrRef; return null; } /// - public override MethodDef Resolve(MethodDef methodDef) { - if (Map.ContainsKey(methodDef)) - return (MethodDef)Map[methodDef]; + public override IMethod Map(MethodDef source) { + if (DefMap.TryGetValue(source, out var mappedRef)) + return mappedRef as IMethod; return null; } /// - public override FieldDef Resolve(FieldDef fieldDef) { - if (Map.ContainsKey(fieldDef)) - return (FieldDef)Map[fieldDef]; + public override IField Map(FieldDef source) { + if (DefMap.TryGetValue(source, out var mappedRef)) + return mappedRef as IField; + return null; + } + + public override MemberRef Map(MemberRef source) { + if (DefMap.TryGetValue(source, out var mappedRef)) + return mappedRef as MemberRef; return null; } } diff --git a/Confuser.Protections/Compress/Compressor.cs b/Confuser.Protections/Compress/Compressor.cs index c08e140..33c0ea6 100644 --- a/Confuser.Protections/Compress/Compressor.cs +++ b/Confuser.Protections/Compress/Compressor.cs @@ -301,7 +301,7 @@ namespace Confuser.Protections { } } - class KeyInjector : IModuleWriterListener { + class KeyInjector { readonly CompressorContext ctx; public KeyInjector(CompressorContext ctx) { @@ -312,7 +312,7 @@ namespace Confuser.Protections { OnWriterEvent(args.Writer, args.Event); } - public void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt) { + private void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt) { if (evt == ModuleWriterEvent.MDBeginCreateTables) { // Add key signature uint sigBlob = writer.Metadata.BlobHeap.Add(ctx.KeySig); diff --git a/Confuser2.sln b/Confuser2.sln index d0d74f5..e546dba 100644 --- a/Confuser2.sln +++ b/Confuser2.sln @@ -52,8 +52,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsRenaming", "Tests\W EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsRenaming.Test", "Tests\WinFormsRenaming.Test\WinFormsRenaming.Test.csproj", "{6C8ECB51-EECE-49C3-89EC-CB0AAECCFF7E}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dnlib", "dnlib\src\dnlib.csproj", "{18A3AD8D-DBF6-4489-A407-2972272CC6CB}" -EndProject Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VisualBasicRenamingResx", "Tests\VisualBasicRenamingResx\VisualBasicRenamingResx.vbproj", "{4B2CE997-8157-40B4-B42F-51CE33954AAC}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VisualBasicRenamingResx.Test", "Tests\VisualBasicRenamingResx.Test\VisualBasicRenamingResx.Test.csproj", "{40C6A1BB-69AA-4869-81EE-41917D0B009A}" @@ -78,17 +76,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "161_DynamicTypeRename", "Te EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "161_DynamicTypeRename.Test", "Tests\161_DynamicTypeRename.Test\161_DynamicTypeRename.Test.csproj", "{2B914EE7-F206-4A83-B435-460D054315BB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "193_ConstantsInlining", "Tests\193_ConstantsInlining\193_ConstantsInlining.csproj", "{AB2E1440-7EC2-45A2-8CF3-2975DE8A57AD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "193_ConstantsInlining", "Tests\193_ConstantsInlining\193_ConstantsInlining.csproj", "{AB2E1440-7EC2-45A2-8CF3-2975DE8A57AD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "193_ConstantsInlining.Lib", "Tests\193_ConstantsInlining.Lib\193_ConstantsInlining.Lib.csproj", "{630BF262-768C-4085-89B1-9FEF7375F442}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "193_ConstantsInlining.Lib", "Tests\193_ConstantsInlining.Lib\193_ConstantsInlining.Lib.csproj", "{630BF262-768C-4085-89B1-9FEF7375F442}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "193_ConstantsInlining.Test", "Tests\193_ConstantsInlining.Test\193_ConstantsInlining.Test.csproj", "{E9D90B2A-F563-4A5E-9EFB-B1D6B1E7F8CB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "193_ConstantsInlining.Test", "Tests\193_ConstantsInlining.Test\193_ConstantsInlining.Test.csproj", "{E9D90B2A-F563-4A5E-9EFB-B1D6B1E7F8CB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignatureMismatch2", "Tests\SignatureMismatch2\SignatureMismatch2.csproj", "{3504F678-95FA-4DB2-8437-31A927CABC16}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignatureMismatch2", "Tests\SignatureMismatch2\SignatureMismatch2.csproj", "{3504F678-95FA-4DB2-8437-31A927CABC16}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignatureMismatch2Helper", "Tests\SignatureMismatch2Helper\SignatureMismatch2Helper.csproj", "{02948DD6-47BD-4C82-9B4B-78931DB23B8A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignatureMismatch2Helper", "Tests\SignatureMismatch2Helper\SignatureMismatch2Helper.csproj", "{02948DD6-47BD-4C82-9B4B-78931DB23B8A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignatureMismatch2.Test", "Tests\SignatureMismatch2.Test\SignatureMismatch2.Test.csproj", "{87BEF4D7-813E-48BA-96FE-E3A24BF2DC34}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignatureMismatch2.Test", "Tests\SignatureMismatch2.Test\SignatureMismatch2.Test.csproj", "{87BEF4D7-813E-48BA-96FE-E3A24BF2DC34}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -164,10 +162,6 @@ Global {6C8ECB51-EECE-49C3-89EC-CB0AAECCFF7E}.Debug|Any CPU.Build.0 = Debug|Any CPU {6C8ECB51-EECE-49C3-89EC-CB0AAECCFF7E}.Release|Any CPU.ActiveCfg = Release|Any CPU {6C8ECB51-EECE-49C3-89EC-CB0AAECCFF7E}.Release|Any CPU.Build.0 = Release|Any CPU - {18A3AD8D-DBF6-4489-A407-2972272CC6CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {18A3AD8D-DBF6-4489-A407-2972272CC6CB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {18A3AD8D-DBF6-4489-A407-2972272CC6CB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {18A3AD8D-DBF6-4489-A407-2972272CC6CB}.Release|Any CPU.Build.0 = Release|Any CPU {4B2CE997-8157-40B4-B42F-51CE33954AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4B2CE997-8157-40B4-B42F-51CE33954AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU {4B2CE997-8157-40B4-B42F-51CE33954AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Tests/Confuser.Core.Test/Confuser.Core.Test.csproj b/Tests/Confuser.Core.Test/Confuser.Core.Test.csproj index 6e87890..8ae3e2a 100644 --- a/Tests/Confuser.Core.Test/Confuser.Core.Test.csproj +++ b/Tests/Confuser.Core.Test/Confuser.Core.Test.csproj @@ -10,7 +10,6 @@ - diff --git a/dnlib b/dnlib deleted file mode 160000 index a42ef1e..0000000 --- a/dnlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a42ef1eda187cae5920e59856009510b66b97a97