示例#1
0
        public static Stream CheckIfCompressed(string resourceName, Stream sourceStream, AssemblyDefinition sourceAssembly)
        {
            bool compressed = false;

            var metaResource = sourceAssembly.MainModule.Resources.FirstOrDefault(r => r.Name == (resourceName + "_meta"));

            if (metaResource != null && metaResource is EmbeddedResource)
            {
                var embeddedHash = (EmbeddedResource)metaResource;
                using (var metaStream = embeddedHash.GetResourceStream())
                {
                    var meta = ResourceMetaData.FromStream(metaStream);
                    compressed = meta.compressed;
                }
            }
            if (compressed)
            {
                MemoryStream finalStream = new MemoryStream();
                sourceStream.Decompress(finalStream);
                return(finalStream);
            }
            else
            {
                return(sourceStream);
            }
        }
示例#2
0
        public static void EmbedResource(string assemblyToEmbedTo, string resourceName, Stream streamToEmbed, string hash = null, CompressionMethod compression = CompressionMethod.Auto)
        {
            if (hash == null)
            {
                hash = streamToEmbed.GetHash();
            }
            double previousTime = GetTime();

            while (GetTime() - previousTime <= 10.0)
            {
                try
                {
                    using (var resolver = new MainResolver())
                    {
                        using (var definition = AssemblyDefinition.ReadAssembly(assemblyToEmbedTo, new ReaderParameters {
                            ReadWrite = true, AssemblyResolver = resolver
                        }))
                        {
                            var finding = definition.MainModule.Resources.FirstOrDefault(r => r.Name == resourceName);
                            if (finding != null)
                            {
                                var metaResource = definition.MainModule.Resources.FirstOrDefault(r => r.Name == (resourceName + "_meta"));
                                if (metaResource != null && metaResource is EmbeddedResource)
                                {
                                    var embeddedHash = (EmbeddedResource)metaResource;
                                    using (var metaStream = embeddedHash.GetResourceStream())
                                    {
                                        var meta = ResourceMetaData.FromStream(metaStream);
                                        if (meta.hash == hash)
                                        {
                                            return;
                                        }
                                    }
                                }
                                definition.MainModule.Resources.Remove(finding);
                                if (metaResource != null)
                                {
                                    definition.MainModule.Resources.Remove(metaResource);
                                }
                            }
                            if (compression == CompressionMethod.Auto || compression == CompressionMethod.UseCompression)
                            {
                                using (var compressedStream = new MemoryStream())
                                {
                                    //Console.WriteLine("E");
                                    using (var compressionStream = new GZipStream(compressedStream, CompressionMode.Compress))
                                    {
                                        streamToEmbed.CopyToStream(compressionStream);
                                        compressedStream.Position = 0;

                                        Stream smallestStream     = compressedStream;
                                        bool   actuallyCompressed = true;
                                        if (streamToEmbed.Length < compressedStream.Length && compression == CompressionMethod.Auto)
                                        {
                                            smallestStream     = streamToEmbed;
                                            actuallyCompressed = false;
                                        }

                                        var er = new EmbeddedResource(resourceName, ManifestResourceAttributes.Public, smallestStream);
                                        definition.MainModule.Resources.Add(er);

                                        using (var metaStream = new ResourceMetaData(actuallyCompressed, hash).ToStream())
                                        {
                                            var hashResource = new EmbeddedResource(resourceName + "_meta", ManifestResourceAttributes.Public, metaStream);
                                            definition.MainModule.Resources.Add(hashResource);
                                            definition.MainModule.Write();
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var er = new EmbeddedResource(resourceName, ManifestResourceAttributes.Public, streamToEmbed);
                                definition.MainModule.Resources.Add(er);
                                using (var metaStream = new ResourceMetaData(false, hash).ToStream())
                                {
                                    var hashResource = new EmbeddedResource(resourceName + "_meta", ManifestResourceAttributes.Public, metaStream);
                                    definition.MainModule.Resources.Add(hashResource);
                                    definition.MainModule.Write();
                                }
                            }
                        }
                    }
                    break;
                }
                catch (Exception e)
                {
                    if (e.Message.Contains("because it is being used by another process"))
                    {
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            if (GetTime() - previousTime > 10.0)
            {
                throw new Exception("Embedding Timeout");
            }
        }