Beispiel #1
0
        /// <summary>Preprocess an assembly unless the cache is up to date.</summary>
        /// <param name="assemblyPath">The assembly file path.</param>
        /// <returns>Returns the rewrite metadata for the preprocessed assembly.</returns>
        public RewriteResult ProcessAssemblyUnlessCached(string assemblyPath)
        {
            // read assembly data
            byte[] assemblyBytes = File.ReadAllBytes(assemblyPath);
            string hash          = string.Join("", MD5.Create().ComputeHash(assemblyBytes).Select(p => p.ToString("X2")));

            // get cached result if current
            CachePaths cachePaths = this.GetCachePaths(assemblyPath);

            {
                CacheEntry cacheEntry = File.Exists(cachePaths.Metadata) ? JsonConvert.DeserializeObject <CacheEntry>(File.ReadAllText(cachePaths.Metadata)) : null;
                if (cacheEntry != null && cacheEntry.IsUpToDate(cachePaths, hash, Constants.ApiVersion))
                {
                    return(new RewriteResult(assemblyPath, cachePaths, assemblyBytes, cacheEntry.Hash, cacheEntry.UseCachedAssembly, isNewerThanCache: false)); // no rewrite needed
                }
            }
            this.Monitor.Log($"Preprocessing {Path.GetFileName(assemblyPath)} for compatibility...", LogLevel.Trace);

            // rewrite assembly
            AssemblyDefinition assembly;

            using (Stream readStream = new MemoryStream(assemblyBytes))
                assembly = AssemblyDefinition.ReadAssembly(readStream);
            bool modified = this.AssemblyTypeRewriter.RewriteAssembly(assembly);

            using (MemoryStream outStream = new MemoryStream())
            {
                assembly.Write(outStream);
                byte[] outBytes = outStream.ToArray();
                return(new RewriteResult(assemblyPath, cachePaths, outBytes, hash, useCachedAssembly: modified, isNewerThanCache: true));
            }
        }
Beispiel #2
0
 /*********
 ** Public methods
 *********/
 /// <summary>Construct an instance.</summary>
 /// <param name="originalAssemblyPath"></param>
 /// <param name="cachePaths">The cache paths.</param>
 /// <param name="assemblyBytes">The rewritten assembly bytes.</param>
 /// <param name="hash">The MD5 hash for the original assembly.</param>
 /// <param name="useCachedAssembly">Whether to use the cached assembly instead of the original assembly.</param>
 /// <param name="isNewerThanCache">Whether this data is newer than the cache.</param>
 public RewriteResult(string originalAssemblyPath, CachePaths cachePaths, byte[] assemblyBytes, string hash, bool useCachedAssembly, bool isNewerThanCache)
 {
     this.OriginalAssemblyPath = originalAssemblyPath;
     this.CachePaths           = cachePaths;
     this.Hash              = hash;
     this.AssemblyBytes     = assemblyBytes;
     this.UseCachedAssembly = useCachedAssembly;
     this.IsNewerThanCache  = isNewerThanCache;
 }
Beispiel #3
0
        /// <summary>Load a preprocessed assembly.</summary>
        /// <param name="assemblyPath">The assembly file path.</param>
        public Assembly LoadCachedAssembly(string assemblyPath)
        {
            CachePaths cachePaths = this.GetCacheInfo(assemblyPath);

            if (!File.Exists(cachePaths.Assembly))
            {
                throw new InvalidOperationException($"The assembly {assemblyPath} doesn't exist in the preprocessed cache.");
            }
            return(Assembly.UnsafeLoadFrom(cachePaths.Assembly)); // unsafe load allows DLLs downloaded from the Internet without the user needing to 'unblock' them
        }
Beispiel #4
0
        /// <summary>Preprocess an assembly and cache the modified version.</summary>
        /// <param name="assemblyPath">The assembly file path.</param>
        public void ProcessAssembly(string assemblyPath)
        {
            // read assembly data
            string assemblyFileName = Path.GetFileName(assemblyPath);
            string assemblyDir      = Path.GetDirectoryName(assemblyPath);

            byte[] assemblyBytes = File.ReadAllBytes(assemblyPath);
            string hash          = $"SMAPI {Constants.Version}|" + string.Join("", MD5.Create().ComputeHash(assemblyBytes).Select(p => p.ToString("X2")));

            // check cache
            CachePaths cachePaths  = this.GetCacheInfo(assemblyPath);
            bool       canUseCache = File.Exists(cachePaths.Assembly) && File.Exists(cachePaths.Hash) && hash == File.ReadAllText(cachePaths.Hash);

            // process assembly if not cached
            if (!canUseCache)
            {
                this.Monitor.Log($"Loading {assemblyFileName} for the first time; preprocessing...", LogLevel.Trace);

                // read assembly definition
                AssemblyDefinition assembly;
                using (Stream readStream = new MemoryStream(assemblyBytes))
                    assembly = AssemblyDefinition.ReadAssembly(readStream);

                // rewrite assembly to match platform
                this.AssemblyTypeRewriter.RewriteAssembly(assembly);

                // write cache
                using (MemoryStream outStream = new MemoryStream())
                {
                    // get assembly bytes
                    assembly.Write(outStream);
                    byte[] outBytes = outStream.ToArray();

                    // write assembly data
                    Directory.CreateDirectory(cachePaths.Directory);
                    File.WriteAllBytes(cachePaths.Assembly, outBytes);
                    File.WriteAllText(cachePaths.Hash, hash);

                    // copy any mdb/pdb files
                    foreach (string path in Directory.GetFiles(assemblyDir, "*.mdb").Concat(Directory.GetFiles(assemblyDir, "*.pdb")))
                    {
                        string filename = Path.GetFileName(path);
                        File.Copy(path, Path.Combine(cachePaths.Directory, filename), overwrite: true);
                    }
                }
            }
        }