三国卡牌客户端基础资源仓库
hch
2025-06-20 4841e82bd5e399c4fc39313bbc93c6fc1bb12b2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using dnlib.DotNet;
using dnlib.DotNet.Writer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HybridCLR.Editor.AOT
{
    public class AOTAssemblyMetadataStripper
    {
        public static byte[] Strip(byte[] assemblyBytes)
        {
            var context = ModuleDef.CreateModuleContext();
            var readerOption = new ModuleCreationOptions(context)
            {
                Runtime = CLRRuntimeReaderKind.Mono
            };
            var mod = ModuleDefMD.Load(assemblyBytes, readerOption);
            // remove all resources
            mod.Resources.Clear();
            foreach (var type in mod.GetTypes())
            {
                if (type.HasGenericParameters)
                {
                    continue;
                }
                foreach (var method in type.Methods)
                {
                    if (!method.HasBody || method.HasGenericParameters)
                    {
                        continue;
                    }
                    method.Body = null;
                }
            }
            var writer = new System.IO.MemoryStream();
            var options = new ModuleWriterOptions(mod);
            options.MetadataOptions.Flags |= MetadataFlags.PreserveRids;
            mod.Write(writer, options);
            writer.Flush();
            return writer.ToArray();
        }
 
        public static void Strip(string originalAssemblyPath, string strippedAssemblyPath)
        {
            byte[] originDllBytes = System.IO.File.ReadAllBytes(originalAssemblyPath);
            byte[] strippedDllBytes = Strip(originDllBytes);
            UnityEngine.Debug.Log($"aot dll:{originalAssemblyPath}, length: {originDllBytes.Length} -> {strippedDllBytes.Length}, stripping rate:{(originDllBytes.Length - strippedDllBytes.Length)/(double)originDllBytes.Length} ");
            Directory.CreateDirectory(System.IO.Path.GetDirectoryName(strippedAssemblyPath));
            System.IO.File.WriteAllBytes(strippedAssemblyPath, strippedDllBytes);
        }
    }
}