Esempio n. 1
0
        /// <summary>Instruments assembly with AsmZ resolver.</summary>
        /// <param name="targetAssembly">The target assembly.</param>
        protected static void InstrumentAsmZ(AssemblyDefinition targetAssembly)
        {
            var helper = new InstrumentHelper(targetAssembly);

            helper.InjectLibZInitializer();
            helper.InjectAsmZResolver();
        }
Esempio n. 2
0
        /// <summary>Executes the task.</summary>
        /// <param name="mainFileName">Name of the main file.</param>
        /// <param name="allLibZResources">
        ///     if set to <c>true</c> loads all LibZ files in resources on startup.
        /// </param>
        /// <param name="libzFiles">The LibZ files to be loaded on startup.</param>
        /// <param name="libzPatterns">The libz file patterns to be loaded on startup.</param>
        /// <param name="keyFileName">Name of the key file.</param>
        /// <param name="keyFilePassword">The key file password.</param>
        public virtual void Execute(
            string mainFileName,
            bool allLibZResources,
            string[] libzFiles,
            string[] libzPatterns,
            string keyFileName, string keyFilePassword)
        {
            if (!File.Exists(mainFileName))
            {
                throw FileNotFound(mainFileName);
            }
            if (libzFiles == null)
            {
                libzFiles = new string[0];
            }
            if (libzPatterns == null)
            {
                libzPatterns = new string[0];
            }

            var targetAssembly = MsilUtilities.LoadAssembly(mainFileName);

            ValidateLibZInstrumentation(targetAssembly);

            var keyPair = MsilUtilities.LoadKeyPair(keyFileName, keyFilePassword);
            var requiresAsmZResolver = false;

            var bootstrapAssembly =
                FindBootstrapAssembly(targetAssembly, mainFileName);

            if (bootstrapAssembly == null)
            {
                var version = MsilUtilities.GetFrameworkVersion(targetAssembly);

                var bootstrapAssemblyImage = InstrumentHelper.GetBootstrapAssemblyImage(version);
                bootstrapAssembly = MsilUtilities.LoadAssembly(bootstrapAssemblyImage);

                if (bootstrapAssembly == null)
                {
                    throw new ArgumentException("LibZ.Bootstrap has not been found");
                }

                Log.Info("Using built in LibZResolver");

                InjectDll(
                    targetAssembly,
                    bootstrapAssembly,
                    bootstrapAssemblyImage,
                    true);
                requiresAsmZResolver = true;
            }

            _instrumentHelper = new InstrumentHelper(
                targetAssembly,
                bootstrapAssembly);

            _instrumentHelper.InjectLibZInitializer();
            if (requiresAsmZResolver)
            {
                _instrumentHelper.InjectAsmZResolver();
            }
            _instrumentHelper.InjectLibZStartup(allLibZResources, libzFiles, libzPatterns);

            MsilUtilities.SaveAssembly(targetAssembly, mainFileName, keyPair);
        }