三国卡牌客户端基础资源仓库
hch
2025-09-11 9e1075c83ce5dace7adce242083788bdffdf5d0c
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
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
 
namespace HybridCLR.Editor.MethodBridge
{
 
    public class PInvokeAnalyzer
    {
        private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
 
        private readonly List<CallNativeMethodSignatureInfo> _pinvokeMethodSignatures = new List<CallNativeMethodSignatureInfo>();
 
        public List<CallNativeMethodSignatureInfo> PInvokeMethodSignatures => _pinvokeMethodSignatures;
 
        public PInvokeAnalyzer(AssemblyCache cache, List<string> assemblyNames)
        {
            foreach (var assemblyName in assemblyNames)
            {
                _rootModules.Add(cache.LoadModule(assemblyName));
            }
        }
 
        private CallingConvention GetCallingConvention(MethodDef method)
        {
            switch (method.ImplMap.CallConv)
            {
                case PInvokeAttributes.CallConvWinapi: return CallingConvention.Default;
                case PInvokeAttributes.CallConvCdecl: return CallingConvention.C;
                case PInvokeAttributes.CallConvStdCall: return CallingConvention.StdCall;
                case PInvokeAttributes.CallConvThiscall: return CallingConvention.ThisCall;
                case PInvokeAttributes.CallConvFastcall: return CallingConvention.FastCall;
                default: return CallingConvention.Default;
            }
        }
 
        public void Run()
        {
            foreach (var mod in _rootModules)
            {
                foreach (TypeDef type in mod.GetTypes())
                {
                    foreach (MethodDef method in type.Methods)
                    {
                        if (method.IsPinvokeImpl)
                        {
                            if (!MetaUtil.IsSupportedPInvokeMethodSignature(method.MethodSig))
                            {
                                Debug.LogError($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
                            }
                            _pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo
                            {
                                MethodSig = method.MethodSig,
                                Callvention = method.HasImplMap? GetCallingConvention(method) : (CallingConvention?)null,
                            });
                        }
                    }
                }
            }
        }
    }
}