少年修仙传客户端基础资源
hch
2024-06-17 d994c578379d81a7dff0683af034f24e3aaa260e
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using dnlib.DotNet;
using HybridCLR.Editor.ABI;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
 
namespace HybridCLR.Editor.ReversePInvokeWrap
{
    public class RawReversePInvokeMethodInfo
    {
        public MethodDef Method { get; set; }
 
        public CustomAttribute GenerationAttribute { get; set; }
    }
 
    public class ABIReversePInvokeMethodInfo
    {
        public MethodDesc Method { get; set; }
 
        public int Count { get; set; }
    }
 
    public class Analyzer
    {
 
        private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
 
        private readonly List<RawReversePInvokeMethodInfo> _reversePInvokeMethods = new List<RawReversePInvokeMethodInfo>();
 
        public Analyzer(AssemblyCache cache, List<string> assemblyNames)
        {
            foreach (var assemblyName in assemblyNames)
            {
                _rootModules.Add(cache.LoadModule(assemblyName));
            }
        }
 
        private void CollectReversePInvokeMethods()
        {
            foreach (var mod in _rootModules)
            {
                Debug.Log($"ass:{mod.FullName} methodcount:{mod.Metadata.TablesStream.MethodTable.Rows}");
                for (uint rid = 1, n = mod.Metadata.TablesStream.MethodTable.Rows; rid <= n; rid++)
                {
                    var method = mod.ResolveMethod(rid);
                    //Debug.Log($"method:{method}");
                    if (!method.IsStatic || !method.HasCustomAttributes)
                    {
                        continue;
                    }
                    CustomAttribute wa = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.Name == "MonoPInvokeCallbackAttribute");
                    if (wa == null)
                    {
                        continue;
                    }
                    //foreach (var ca in method.CustomAttributes)
                    //{
                    //    Debug.Log($"{ca.AttributeType.FullName} {ca.TypeFullName}");
                    //}
                    _reversePInvokeMethods.Add(new RawReversePInvokeMethodInfo()
                    {
                        Method = method,
                        GenerationAttribute = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "HybridCLR.ReversePInvokeWrapperGenerationAttribute"),
                    });
                }
            }
        }
 
        public List<ABIReversePInvokeMethodInfo> BuildABIMethods()
        {
            var methodsBySig = new Dictionary<string, ABIReversePInvokeMethodInfo>();
            var typeCreator = new TypeCreator();
            foreach(var method in _reversePInvokeMethods)
            {
                MethodDesc desc = new MethodDesc
                {
                    MethodDef = method.Method,
                    ReturnInfo = new ReturnInfo { Type = typeCreator.CreateTypeInfo(method.Method.ReturnType)},
                    ParamInfos = method.Method.Parameters.Select(p => new ParamInfo { Type = typeCreator.CreateTypeInfo(p.Type)}).ToList(),
                };
                desc.Init();
                if (!methodsBySig.TryGetValue(desc.Sig, out var arm))
                {
                    arm = new ABIReversePInvokeMethodInfo()
                    {
                        Method = desc,
                        Count = 0,
                    };
                    methodsBySig.Add(desc.Sig, arm);
                }
                int preserveCount = method.GenerationAttribute != null ? (int)method.GenerationAttribute.ConstructorArguments[0].Value : 1;
                arm.Count += preserveCount;
            }
            var methods = methodsBySig.Values.ToList();
            methods.Sort((a, b) => String.CompareOrdinal(a.Method.Sig, b.Method.Sig));
            return methods;
        }
 
        public void Run()
        {
            CollectReversePInvokeMethods();
        }
    }
}