Exemple #1
0
        public static Builder CreateBuilder(this WeaverBase weaver)
        {
            if (weaver == null)
                throw new ArgumentNullException(nameof(weaver), $"Argument '{nameof(weaver)}' cannot be null");

            return new Builder(weaver);
        }
Exemple #2
0
        internal CecilatorBase(WeaverBase weaver)
        {
            this.logError         = weaver.LogError;
            this.logInfo          = weaver.LogInfo;
            this.logWarning       = weaver.LogWarning;
            this.moduleDefinition = weaver.ModuleDefinition;

            var assemblies = this.GetAllAssemblyDefinitions(this.moduleDefinition.AssemblyReferences)
                             .Concat(new AssemblyDefinition[] { this.moduleDefinition.Assembly });

            this.UnusedReference = weaver.ReferenceCopyLocalPaths
                                   .Where(x => x.EndsWith(".dll"))
                                   .Select(x =>
            {
                try
                {
                    return(new AssemblyDefinitionEx(AssemblyDefinition.ReadAssembly(x), x));
                }
                catch (BadImageFormatException e)
                {
                    this.LogInfo(e.Message + " " + x);
                    return(null);
                }
                catch (Exception e)
                {
                    this.LogWarning(e.Message);
                    return(null);
                }
            })
                                   .Where(x => x != null && !assemblies.Any(y => y.FullName.GetHashCode() == x.AssemblyDefinition.FullName.GetHashCode() && y.FullName == x.AssemblyDefinition.FullName))
                                   .ToArray();

            this.allAssemblies = assemblies.Concat(this.UnusedReference.Select(x => x.AssemblyDefinition)).ToArray();

            this.logInfo("-----------------------------------------------------------------------------");

            foreach (var item in allAssemblies)
            {
                this.logInfo("<<Assembly>> " + item.Name);
            }

            this.allTypes = this.allAssemblies.SelectMany(x => x.Modules).Where(x => x != null).SelectMany(x => x.Types).Where(x => x != null).Concat(this.moduleDefinition.Types).ToArray();
            this.logInfo("-----------------------------------------------------------------------------");
            WeaverBase.AllTypes = this.allTypes;

            this.Identification = GenerateName();
        }
        internal CecilatorBase(WeaverBase weaver)
        {
            this.logError         = weaver.LogError;
            this.logInfo          = weaver.LogInfo;
            this.logWarning       = weaver.LogWarning;
            this.moduleDefinition = weaver.ModuleDefinition;

            var assemblies = this.GetAllAssemblyDefinitions(this.moduleDefinition.AssemblyReferences)
                             .Concat(new AssemblyDefinition[] { this.moduleDefinition.Assembly });

            this.UnusedReference = weaver.ReferenceCopyLocalPaths
                                   .Where(x => x.EndsWith(".dll"))
                                   .Select(x =>
            {
                try
                {
                    return(new AssemblyDefinitionEx(AssemblyDefinition.ReadAssembly(x), x));
                }
                catch (BadImageFormatException e)
                {
                    this.LogInfo(e.Message + " " + x);
                    return(null);
                }
                catch (Exception e)
                {
                    this.LogWarning(e.Message);
                    return(null);
                }
            })
                                   .Where(x => x != null && !assemblies.Any(y => y.FullName.GetHashCode() == x.AssemblyDefinition.FullName.GetHashCode() && y.FullName == x.AssemblyDefinition.FullName))
                                   .ToArray();

            this.allAssemblies = assemblies.Concat(this.UnusedReference.Select(x => x.AssemblyDefinition)).ToArray();

            this.logInfo("-----------------------------------------------------------------------------");

            foreach (var item in allAssemblies)
            {
                this.logInfo("<<Assembly>> " + item.Name);
            }

            foreach (var item in this.moduleDefinition.Resources)
            {
                this.LogInfo("<<Resource>> " + item.Name + " " + item.ResourceType);
                if (item.ResourceType == ResourceType.Embedded)
                {
                    var embeddedResource = item as EmbeddedResource;
                    using (var stream = embeddedResource.GetResourceStream())
                    {
                        var resourceNames = new List <string>();
                        var bytes         = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);
                        if (bytes[0] == 0xce && bytes[1] == 0xca && bytes[2] == 0xef && bytes[3] == 0xbe)
                        {
                            var resourceCount = BitConverter.ToInt16(bytes.GetBytes(160, 2).Reverse().ToArray(), 0);

                            if (resourceCount > 0)
                            {
                                var startPoint = resourceCount * 8 + 180;

                                for (int i = 0; i < resourceCount; i++)
                                {
                                    var length = (int)bytes[startPoint];
                                    var data   = Encoding.Unicode.GetString(bytes, startPoint + 1, length).Trim();
                                    startPoint += length + 5;
                                    resourceNames.Add(data);
                                    this.LogInfo("             " + data);
                                }
                            }
                        }
                        this.ResourceNames.AddRange(resourceNames);
                    }
                }
            }
            this.allTypes = this.allAssemblies.SelectMany(x => x.Modules).Where(x => x != null).SelectMany(x => x.Types).Where(x => x != null).Concat(this.moduleDefinition.Types).ToArray();
            this.logInfo("-----------------------------------------------------------------------------");
            WeaverBase.AllTypes = this.allTypes;

            this.Identification = GenerateName();
        }
Exemple #4
0
 internal Builder(WeaverBase weaver) : base(weaver)
 {
 }
        internal CecilatorBase(WeaverBase weaver)
        {
            this.Initialize(weaver.LogInfo, weaver.LogWarning, weaver.LogWarningPoint, weaver.LogError, weaver.LogErrorPoint);

            this.moduleDefinition = weaver.ModuleDefinition;

            this.ReferenceCopyLocal = weaver.ReferenceCopyLocalPaths
                                      .Where(x => x.EndsWith(".dll"))
                                      .Select(x => LoadAssembly(x))
                                      .Where(x => x != null)
                                      .ToArray();

            var referencedAssemblies = weaver
                                       .With(x =>
            {
                if (weaver.Config.Attribute("ReferenceRecursive").With(y => y == null ? true : bool.Parse(y.Value)))
                {
                    return(x.GetAllReferencedAssemblies(weaver.Resolve(this.moduleDefinition.AssemblyReferences)));
                }

                return(weaver.Resolve(this.moduleDefinition.AssemblyReferences));
            })
                                       .Concat(weaver.References.Split(';').Select(x => LoadAssembly(x)))
                                       .Where(x => x != null).ToArray() as IEnumerable <AssemblyDefinition>;

            if (weaver.Config.Attribute("ReferenceCopyLocal").With(x => x == null ? true : bool.Parse(x.Value)))
            {
                referencedAssemblies = referencedAssemblies.Concat(this.ReferenceCopyLocal);
            }

            this.ReferencedAssemblies = referencedAssemblies.Distinct(new AssemblyDefinitionEqualityComparer()).ToArray();

            this.Log("-----------------------------------------------------------------------------");

            foreach (var item in this.ReferencedAssemblies)
            {
                this.Log(LogTypes.Info, "<<Assembly>> " + item.Name);
            }

            var resourceNames = new List <string>();

            foreach (var item in this.moduleDefinition.Resources)
            {
                this.Log(LogTypes.Info, "<<Resource>> " + item.Name + " " + item.ResourceType);
                if (item.ResourceType == ResourceType.Embedded)
                {
                    var embeddedResource = item as EmbeddedResource;
                    using (var stream = embeddedResource.GetResourceStream())
                    {
                        var bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);
                        if (bytes[0] == 0xce && bytes[1] == 0xca && bytes[2] == 0xef && bytes[3] == 0xbe)
                        {
                            var resourceCount = BitConverter.ToInt16(bytes.GetBytes(160, 2).Reverse().ToArray(), 0);

                            if (resourceCount > 0)
                            {
                                var startPoint = resourceCount * 8 + 180;

                                for (int i = 0; i < resourceCount; i++)
                                {
                                    var length = (int)bytes[startPoint];
                                    var data   = Encoding.Unicode.GetString(bytes, startPoint + 1, length).Trim();
                                    startPoint += length + 5;
                                    resourceNames.Add(data);
                                    this.Log("             " + data);
                                }
                            }
                        }
                    }
                }
            }

            this.ResourceNames = resourceNames.ToArray();

            this.allTypes = this.ReferencedAssemblies
                            .SelectMany(x => x.Modules)
                            .Where(x => x != null)
                            .SelectMany(x => x.Types)
                            .Where(x => x != null)
                            .Concat(this.moduleDefinition.Types)
                            .Where(x => x.Module != null && x.Module.Assembly != null)
                            .Select(x => new { Prio = GetAssemblyPrio(x), Type = x })
                            .OrderBy(x => x.Prio)
                            .ThenBy(x => x.Type.FullName)
                            .Select(x => x.Type)
                            .Distinct(new TypeDefinitionEqualityComparer())
                            .ToList();
            this.Log("-----------------------------------------------------------------------------");
            WeaverBase.AllTypes = this.allTypes;

            this.Identification = CodeBlocks.GenerateName();
        }