Skip to content

Commit

Permalink
ignore the assemblies(IsILOnly=false)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhongke committed Mar 17, 2022
1 parent 52b5ce0 commit f6b8996
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 65 deletions.
14 changes: 13 additions & 1 deletion Zack.DotNetTrimmerLib/AssemblyTrimmer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using dnlib.DotNet;
using dnlib.DotNet.MD;

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>
Expand All @@ -17,7 +29,6 @@ public static void TrimAssembly(string asmFile, HashSet<string> loadedTypes)
//Assembly contains more than IL code cannot be trimmed as expected.
//https://github.com/Washi1337/AsmResolver/issues/267
if (!module.IsILOnly) return;

List<TypeDef> typesToBeRemoved = new List<TypeDef>();
foreach (var type in module.Types)
{
Expand All @@ -40,6 +51,7 @@ public static void TrimAssembly(string asmFile, HashSet<string> loadedTypes)
}
}
//if module.IsILOnly, NativeWrite should be used instead of Write
//module.Write(memStream);
module.Write(memStream);
}
memStream.Position = 0;
Expand Down
57 changes: 0 additions & 57 deletions Zack.DotNetTrimmerLib/JsonCleaner.cs

This file was deleted.

8 changes: 1 addition & 7 deletions Zack.DotNetTrimmerLib/Trimmer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,11 @@ public void Run()
.Where(asmPath => !IsFileIgnored(asmPath) && IsManagedAssembly(asmPath)).ToArray();
var assembliesNotLoaded = allDllFiles.Where(d => !recordFileInfo.LoadedAssemblies.Contains(Path.GetFileName(d)));
var totalSize = assembliesNotLoaded.Select(f => new FileInfo(f).Length).Sum() * 1.0 / (1024 * 1024);

/*
foreach(var asmFile in recordFileInfo.LoadedAssemblies)
{
if (!File.Exists(asmFile)) return;
AssemblyTrimmer.TrimAssembly(asmFile, recordFileInfo.LoadedTypes);
}*/
foreach (var file in assembliesNotLoaded)
{
File.Delete(file);
}
AssemblyTrimmer.TrimAssemblies(startupDir, recordFileInfo.LoadedTypes);
IOHelpers.RemoveFiles(startupDir, "*.pdb");
IOHelpers.RemoveFiles(startupDir, "*.runtimeconfig.json");
IOHelpers.RemoveFiles(startupDir, "*.deps.json");
Expand Down

0 comments on commit f6b8996

Please sign in to comment.