Ejemplo n.º 1
0
        public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def)
        {
            var module = def as ModuleDef;
            if (module == null) return;

            string asmName = module.Assembly.Name.String;
            if (!string.IsNullOrEmpty(module.Assembly.Culture) &&
                asmName.EndsWith(".resources")) {
                // Satellite assembly
                var satellitePattern = new Regex(string.Format("^(.*)\\.{0}\\.resources$", module.Assembly.Culture));
                string nameAsmName = asmName.Substring(0, asmName.Length - ".resources".Length);
                ModuleDef mainModule = context.Modules.SingleOrDefault(mod => mod.Assembly.Name == nameAsmName);
                if (mainModule == null) {
                    context.Logger.ErrorFormat("Could not find main assembly of satellite assembly '{0}'.", module.Assembly.FullName);
                    throw new ConfuserException(null);
                }

                string format = "{0}." + module.Assembly.Culture + ".resources";
                foreach (Resource res in module.Resources) {
                    Match match = satellitePattern.Match(res.Name);
                    if (!match.Success)
                        continue;
                    string typeName = match.Groups[1].Value;
                    TypeDef type = mainModule.FindReflectionThrow(typeName);
                    if (type == null) {
                        context.Logger.WarnFormat("Could not find resource type '{0}'.", typeName);
                        continue;
                    }
                    service.ReduceRenameMode(type, RenameMode.ASCII);
                    service.AddReference(type, new ResourceReference(res, type, format));
                }
            }
            else {
                string format = "{0}.resources";
                foreach (Resource res in module.Resources) {
                    Match match = ResourceNamePattern.Match(res.Name);
                    if (!match.Success)
                        continue;
                    string typeName = match.Groups[1].Value;

                    if (typeName.EndsWith(".g")) // WPF resources, ignore
                        continue;

                    TypeDef type = module.FindReflection(typeName);
                    if (type == null) {
                        context.Logger.WarnFormat("Could not find resource type '{0}'.", typeName);
                        continue;
                    }
                    service.ReduceRenameMode(type, RenameMode.ASCII);
                    service.AddReference(type, new ResourceReference(res, type, format));
                }
            }
        }
Ejemplo n.º 2
0
        void AnalyzeCAArgument(ConfuserContext context, INameService service, CAArgument arg)
        {
            if (arg.Type.DefinitionAssembly.IsCorLib() && arg.Type.FullName == "System.Type")
            {
                var typeSig = (TypeSig)arg.Value;
                foreach (var typeRef in typeSig.FindTypeRefs())
                {
                    var typeDef = typeRef.ResolveTypeDefThrow();
                    if (context.Modules.Contains((ModuleDefMD)typeDef.Module))
                    {
                        if (typeRef is TypeRef)
                        {
                            service.AddReference(typeDef, new TypeRefReference((TypeRef)typeRef, typeDef));
                        }

                        service.ReduceRenameMode(typeDef, RenameMode.ASCII);
                    }
                }
            }
            else if (arg.Value is CAArgument[])
            {
                foreach (var elem in (CAArgument[])arg.Value)
                {
                    AnalyzeCAArgument(context, service, elem);
                }
            }
        }
Ejemplo n.º 3
0
 private static void AnalyzeCAArgument(ICollection <ModuleDefMD> modules, INameService service, CAArgument arg)
 {
     if (arg.Value == null)
     {
         return;                                // null was passed to the custom attribute. We'll ignore that.
     }
     if (arg.Type.DefinitionAssembly.IsCorLib() && arg.Type.FullName == "System.Type")
     {
         var typeSig = (TypeSig)arg.Value;
         foreach (ITypeDefOrRef typeRef in typeSig.FindTypeRefs())
         {
             TypeDef typeDef = typeRef.ResolveTypeDefThrow();
             if (modules.Contains((ModuleDefMD)typeDef.Module))
             {
                 if (typeRef is TypeRef)
                 {
                     service.AddReference(typeDef, new TypeRefReference((TypeRef)typeRef, typeDef));
                 }
                 service.ReduceRenameMode(typeDef, RenameMode.Reflection);
             }
         }
     }
     else if (arg.Value is CAArgument[])
     {
         foreach (CAArgument elem in (CAArgument[])arg.Value)
         {
             AnalyzeCAArgument(modules, service, elem);
         }
     }
 }
Ejemplo n.º 4
0
 // Token: 0x0600002E RID: 46 RVA: 0x00003FF4 File Offset: 0x000021F4
 private void AnalyzeCAArgument(ConfuserContext context, INameService service, CAArgument arg)
 {
     if (arg.Type.DefinitionAssembly.IsCorLib() && arg.Type.FullName == "System.Type")
     {
         TypeSig typeSig = (TypeSig)arg.Value;
         using (IEnumerator <ITypeDefOrRef> enumerator = typeSig.FindTypeRefs().GetEnumerator())
         {
             while (enumerator.MoveNext())
             {
                 ITypeDefOrRef typeRef = enumerator.Current;
                 TypeDef       typeDef = typeRef.ResolveTypeDefThrow();
                 if (context.Modules.Contains((ModuleDefMD)typeDef.Module))
                 {
                     if (typeRef is TypeRef)
                     {
                         service.AddReference <TypeDef>(typeDef, new TypeRefReference((TypeRef)typeRef, typeDef));
                     }
                     service.ReduceRenameMode(typeDef, RenameMode.ASCII);
                 }
             }
             return;
         }
     }
     if (arg.Value is CAArgument[])
     {
         CAArgument[] array = (CAArgument[])arg.Value;
         for (int i = 0; i < array.Length; i++)
         {
             CAArgument elem = array[i];
             this.AnalyzeCAArgument(context, service, elem);
         }
     }
 }
Ejemplo n.º 5
0
 static void PropagateRenamingRestrictions(INameService service, IList <object> objects)
 {
     if (!objects.All(service.CanRename))
     {
         foreach (var o in objects)
         {
             service.SetCanRename(o, false);
         }
     }
     else
     {
         var minimalRenamingLevel = objects.Max(service.GetRenameMode);
         foreach (var o in objects)
         {
             service.ReduceRenameMode(o, minimalRenamingLevel);
         }
     }
 }
Ejemplo n.º 6
0
        public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def)
        {
            var module = def as ModuleDef;

            if (module == null)
            {
                return;
            }

            string asmName = module.Assembly.Name.String;

            if (!string.IsNullOrEmpty(module.Assembly.Culture) &&
                asmName.EndsWith(".resources"))
            {
                // Satellite assembly
                var       satellitePattern = new Regex(string.Format("^(.*)\\.{0}\\.resources$", module.Assembly.Culture));
                string    nameAsmName      = asmName.Substring(0, asmName.Length - ".resources".Length);
                ModuleDef mainModule       = context.Modules.SingleOrDefault(mod => mod.Assembly.Name == nameAsmName);
                if (mainModule == null)
                {
                    context.Logger.ErrorFormat("Could not find main assembly of satellite assembly '{0}'.", module.Assembly.FullName);
                    throw new ConfuserException(null);
                }

                string format = "{0}." + module.Assembly.Culture + ".resources";
                foreach (Resource res in module.Resources)
                {
                    Match match = satellitePattern.Match(res.Name);
                    if (!match.Success)
                    {
                        continue;
                    }
                    string  typeName = match.Groups[1].Value;
                    TypeDef type     = mainModule.FindReflectionThrow(typeName);
                    if (type == null)
                    {
                        context.Logger.WarnFormat("Could not find resource type '{0}'.", typeName);
                        continue;
                    }
                    service.ReduceRenameMode(type, RenameMode.ASCII);
                    service.AddReference(type, new ResourceReference(res, type, format));
                }
            }
            else
            {
                string format = "{0}.resources";
                foreach (Resource res in module.Resources)
                {
                    Match match = ResourceNamePattern.Match(res.Name);
                    if (!match.Success)
                    {
                        continue;
                    }
                    string typeName = match.Groups[1].Value;

                    if (typeName.EndsWith(".g"))                     // WPF resources, ignore
                    {
                        continue;
                    }

                    TypeDef type = module.FindReflection(typeName);
                    if (type == null)
                    {
                        context.Logger.WarnFormat("Could not find resource type '{0}'.", typeName);
                        continue;
                    }
                    service.ReduceRenameMode(type, RenameMode.ASCII);
                    service.AddReference(type, new ResourceReference(res, type, format));
                }
            }
        }
Ejemplo n.º 7
0
        public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def)
        {
            var module = def as ModuleDef;

            if (module == null)
            {
                return;
            }

            string asmName = module.Assembly.Name.String;

            if (!string.IsNullOrEmpty(module.Assembly.Culture) &&
                asmName.EndsWith(".resources"))
            {
                // Satellite assembly
                var       satellitePattern = new Regex(string.Format("^(.*)\\.{0}\\.resources$", module.Assembly.Culture));
                string    nameAsmName      = asmName.Substring(0, asmName.Length - ".resources".Length);
                ModuleDef mainModule       = context.Modules.SingleOrDefault(mod => mod.Assembly.Name == nameAsmName);
                if (mainModule == null)
                {
                    context.Logger.ErrorFormat("Could not find main assembly of satellite assembly '{0}'.", module.Assembly.FullName);
                    throw new ConfuserException(null);
                }

                string format = "{0}." + module.Assembly.Culture + ".resources";
                foreach (Resource res in module.Resources)
                {
                    Match match = satellitePattern.Match(res.Name);
                    if (!match.Success)
                    {
                        continue;
                    }
                    string  typeName = match.Groups[1].Value;
                    TypeDef type     = mainModule.FindReflection(typeName);
                    if (type == null)
                    {
                        context.Logger.WarnFormat(Resources.ResourceAnalyzer_Analyze_CouldNotFindResourceType, typeName);
                        continue;
                    }
                    service.ReduceRenameMode(type, RenameMode.Reflection);
                    service.AddReference(type, new ResourceReference(res, type, format));
                }
            }
            else
            {
                string format = "{0}.resources";
                foreach (Resource res in module.Resources)
                {
                    Match match = ResourceNamePattern.Match(res.Name);
                    if (!match.Success || res.ResourceType != ResourceType.Embedded)
                    {
                        continue;
                    }
                    string typeName = match.Groups[1].Value;

                    if (typeName.EndsWith(".g"))                     // WPF resources, ignore
                    {
                        continue;
                    }

                    // This variable is set true in case the name of the resource doesn't match the name of the class.
                    // That happens for the resources in Visual Basic.
                    var     mismatchingName = false;
                    TypeDef type            = module.FindReflection(typeName);
                    if (type == null)
                    {
                        if (typeName.EndsWith(".Resources"))
                        {
                            typeName        = typeName.Substring(0, typeName.Length - 10) + ".My.Resources.Resources";
                            type            = module.FindReflection(typeName);
                            mismatchingName = type != null;
                        }
                    }

                    if (type == null)
                    {
                        context.Logger.WarnFormat(Resources.ResourceAnalyzer_Analyze_CouldNotFindResourceType, typeName);
                        continue;
                    }
                    service.ReduceRenameMode(type, RenameMode.Reflection);
                    service.AddReference(type, new ResourceReference(res, type, format));

                    if (mismatchingName)
                    {
                        // Add string type references in case the name didn't match. This will cause the resource to get
                        // the same name as the class, despite that not being the case before. But that doesn't really matter.
                        FindLdTokenResourceReferences(type, match.Groups[1].Value, service);
                    }
                }
            }
        }
        // Token: 0x06000026 RID: 38 RVA: 0x000037A4 File Offset: 0x000019A4
        public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def)
        {
            ModuleDef module = def as ModuleDef;

            if (module == null)
            {
                return;
            }
            string asmName = module.Assembly.Name.String;

            if (!string.IsNullOrEmpty(module.Assembly.Culture) && asmName.EndsWith(".resources"))
            {
                Regex     satellitePattern = new Regex(string.Format("^(.*)\\.{0}\\.resources$", module.Assembly.Culture));
                string    nameAsmName      = asmName.Substring(0, asmName.Length - ".resources".Length);
                ModuleDef mainModule       = context.Modules.SingleOrDefault((ModuleDefMD mod) => mod.Assembly.Name == nameAsmName);
                if (mainModule == null)
                {
                    context.Logger.ErrorFormat("Could not find main assembly of satellite assembly '{0}'.", new object[]
                    {
                        module.Assembly.FullName
                    });
                    throw new ConfuserException(null);
                }
                string format = "{0}." + module.Assembly.Culture + ".resources";
                using (IEnumerator <Resource> enumerator = module.Resources.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        Resource res   = enumerator.Current;
                        Match    match = satellitePattern.Match(res.Name);
                        if (match.Success)
                        {
                            string  typeName = match.Groups[1].Value;
                            TypeDef type     = mainModule.FindReflectionThrow(typeName);
                            if (type == null)
                            {
                                context.Logger.WarnFormat("Could not find resource type '{0}'.", new object[]
                                {
                                    typeName
                                });
                            }
                            else
                            {
                                service.ReduceRenameMode(type, RenameMode.ASCII);
                                service.AddReference <TypeDef>(type, new ResourceReference(res, type, format));
                            }
                        }
                    }
                    return;
                }
            }
            string format2 = "{0}.resources";

            foreach (Resource res2 in module.Resources)
            {
                Match match2 = ResourceAnalyzer.ResourceNamePattern.Match(res2.Name);
                if (match2.Success && res2.ResourceType == ResourceType.Embedded)
                {
                    string typeName2 = match2.Groups[1].Value;
                    if (!typeName2.EndsWith(".g"))
                    {
                        TypeDef type2 = module.FindReflection(typeName2);
                        if (type2 == null)
                        {
                            context.Logger.WarnFormat("Could not find resource type '{0}'.", new object[]
                            {
                                typeName2
                            });
                        }
                        else
                        {
                            service.ReduceRenameMode(type2, RenameMode.ASCII);
                            service.AddReference <TypeDef>(type2, new ResourceReference(res2, type2, format2));
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
 void AnalyzeCAArgument(ConfuserContext context, INameService service, CAArgument arg)
 {
     if (arg.Type.DefinitionAssembly.IsCorLib() && arg.Type.FullName == "System.Type") {
         var typeSig = (TypeSig)arg.Value;
         foreach (ITypeDefOrRef typeRef in typeSig.FindTypeRefs()) {
             TypeDef typeDef = typeRef.ResolveTypeDefThrow();
             if (context.Modules.Contains((ModuleDefMD)typeDef.Module)) {
                 if (typeRef is TypeRef)
                     service.AddReference(typeDef, new TypeRefReference((TypeRef)typeRef, typeDef));
                 service.ReduceRenameMode(typeDef, RenameMode.ASCII);
             }
         }
     }
     else if (arg.Value is CAArgument[]) {
         foreach (CAArgument elem in (CAArgument[])arg.Value)
             AnalyzeCAArgument(context, service, elem);
     }
 }