Exemplo n.º 1
0
        public override void OnBegin(AssemblyDef assembly)
        {
            var resources = assembly.Resources;

            if (resources.Count == 0)
            {
                return;
            }

            var entryPoints = new List <string>()
            {
                "System.Reflection.Assembly::GetManifestResourceStream(System.String):System.IO.Stream"
            };

            // Find all entry points
            var methods = entryPoints.ConvertAll(name => assembly.Find <MethodRef>(name)).Where(method => method != null).ToList();

            if (methods.Count == 0)
            {
                return;
            }

            // Get all resource entry points callers
            var query = from method in methods
                        from caller in assembly.CallersOf(method)
                        from resource in assembly.Resources
                        where caller.UserStrings.Contains(resource.Name)
                        group new { Caller = caller, Method = method } by resource into g
                select new { Resource = g.Key, Groups = g };

            var resourceLoaders = query.ToList();

            if (resourceLoaders.Count == 0)
            {
                return;
            }

            // Merge decompress code
            MergeDecompress(assembly);

            foreach (var item in query)
            {
                var res = item.Resource;

                foreach (var group in item.Groups)
                {
                    MethodDef method = GetDecompressMethod(group.Method);
                    if (method != null)
                    {
                        group.Caller.ReplaceCall(group.Method, method);
                    }
                }

                var compressed = Compress(res);
                resources.Remove(res);
                resources.Add(compressed);

                double ratio = (((double)compressed.Data.Length) / res.Data.Length);
                Logger.Write("Resource {0} compress ratio {1:P}", res.Name, ratio);
            }
        }