Exemple #1
0
        private void InnerEmbed(string prefix, string fullPath, bool compress, bool addChecksum, bool disableCleanup, Checksums checksums)
        {
            if (!disableCleanup)
            {
                // in any case we can remove this from the copy local paths, because either it's already embedded, or it will be embedded.
                // ReferenceCopyLocalPaths.RemoveAll(item => string.Equals(item, fullPath, StringComparison.OrdinalIgnoreCase));
            }

            var resourceName = $"{prefix}{Path.GetFileName(fullPath).ToLowerInvariant()}";

            if (manifest.Resources.Any(x => string.Equals(x.Name, resourceName, StringComparison.OrdinalIgnoreCase)))
            {
                // - an assembly that is already embedded uncompressed, using <EmbeddedResource> in the project file
                // - if compress == false: an assembly that appeared twice in the ReferenceCopyLocalPaths, e.g. the same library from different nuget packages (https://github.com/Fody/Costura/issues/332)
                if (addChecksum && !checksums.ContainsKey(resourceName))
                {
                    checksums.Add(resourceName, Checksums.CalculateChecksum(fullPath));
                }

                return;
            }

            if (compress)
            {
                resourceName += ".compressed";

                if (manifest.Resources.Any(x => string.Equals(x.Name, resourceName, StringComparison.OrdinalIgnoreCase)))
                {
                    // an assembly that appeared twice in the ReferenceCopyLocalPaths, e.g. the same library from different nuget packages (https://github.com/Fody/Costura/issues/332)
                    return;
                }
            }

            var checksum     = Checksums.CalculateChecksum(fullPath);
            var cacheFile    = Path.Combine(cachePath, $"{checksum}.{resourceName}");
            var memoryStream = BuildMemoryStream(fullPath, compress, cacheFile);

            streams.Add(memoryStream);
            ManifestResourceDeclaration resource = new ManifestResourceDeclaration
            {
                Name     = resourceName,
                IsPublic = false
            };

            resource.ContentStreamProvider = () => memoryStream;

            manifest.Resources.Add(resource);

            if (addChecksum)
            {
                checksums.Add(resourceName, checksum);
            }
        }
Exemple #2
0
        /// <summary>
        /// Calculates checksums for costura-processed unmanaged resources of the assembly, and returns true if
        /// any unprocessed resources exist.
        /// </summary>
        public static bool ProcessNativeResources(AssemblyManifestDeclaration manifest, bool compress, Checksums checksums)
        {
            var  unprocessedNameMatch = new Regex(@"^(.*\.)?costura(32|64)\.", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
            var  processedNameMatch   = new Regex(@"^costura(32|64)\.", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
            bool hasUnmanaged         = false;

            foreach (var resource in manifest.Resources)
            {
                var match = unprocessedNameMatch.Match(resource.Name);
                if (match.Success)
                {
                    resource.Name = resource.Name.Substring(match.Groups[1].Length).ToLowerInvariant();
                    hasUnmanaged  = true;
                }

                if (processedNameMatch.IsMatch(resource.Name))
                {
                    using (var stream = resource.ContentStreamProvider())
                    {
                        if (compress && resource.Name.EndsWith(".compressed", StringComparison.OrdinalIgnoreCase))
                        {
                            using (var compressStream = new DeflateStream(stream, CompressionMode.Decompress))
                            {
                                checksums.Add(resource.Name, Checksums.CalculateChecksum(compressStream));
                            }
                        }
                        else
                        {
                            checksums.Add(resource.Name, Checksums.CalculateChecksum(stream));
                        }
                    }
                }
            }

            return(hasUnmanaged);
        }