Zack.DotNetTrimmerLib/AssemblyTrimmer.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using dnlib.DotNet;

namespace Zack.DotNetTrimmerLib
{
    static class AssemblyTrimmer
    {
        public static void TrimAssemblies(string rootDir, HashSet<string> loadedTypes)
        {
            foreach (var asmFile in Directory.GetFiles(rootDir, "*.dll", SearchOption.AllDirectories))
            {
                if (PEHelpers.IsManagedAssembly(asmFile))
                {
                    TrimAssembly(asmFile, loadedTypes);
                }
            }
        }

        /// <summary>
        /// clear the bodies of methods of unused classes in loadedAssemblies
        /// </summary>
        /// <param name="loadedAssemblies"></param>
        /// <param name="loadedTypes"></param>
        public static void TrimAssembly(string asmFile, HashSet<string> loadedTypes)
        {
            using var memStream = new MemoryStream();
            using (var module = ModuleDefMD.Load(asmFile))
            {
                //Assembly contains more than IL code cannot be trimmed as expected.
                //https://github.com/Washi1337/AsmResolver/issues/267
                if (!module.IsILOnly)
                {
                    ConsoleHelpers.WriteInfo($"{asmFile} is not ILOnly, skipped.");
                    return;
                }
                List<TypeDef> typesToBeRemoved = new List<TypeDef>();
                foreach (var type in module.Types)
                {
                    if (type.IsGlobalModuleType) continue;//<Module>                    
                                                          //struct, nested classes are not included in the report by DiagnosticsClient
                    if (type.IsValueType) continue;//UriCreationOptions etc
                    if (type.IsNested) continue;
                    if (type.HasGenericParameters) continue;//generic classes may have different names as in the report by DiagnosticsClient, like A`1 and A<int>
                    if (!loadedTypes.Contains(type.FullName))
                    {
                        typesToBeRemoved.Add(type);
                    }
                }
                foreach (var type in typesToBeRemoved)
                {
                    //these methods may be referenced by others, so they cannot be removed; however, the body can be replace by 'throw null;'
                    foreach (var method in type.Methods)
                    {
                        PEHelpers.ClearMethodBody(method);
                    }
                }
                //if module.IsILOnly, NativeWrite should be used instead of Write
                //module.Write(memStream);
                module.Write(memStream);
            }
            memStream.Position = 0;

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。