少年修仙传客户端基础资源
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
using HybridCLR.Editor.ABI;
using HybridCLR.Editor.Template;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
 
namespace HybridCLR.Editor.ReversePInvokeWrap
{
    public class Generator
    {
        public void Generate(List<ABIReversePInvokeMethodInfo> methods, string outputFile)
        {
            string template = File.ReadAllText(outputFile, Encoding.UTF8);
            var frr = new FileRegionReplace(template);
            var codes = new List<string>();
 
            int methodIndex = 0;
            var stubCodes = new List<string>();
            foreach(var methodInfo in methods)
            {
                MethodDesc method = methodInfo.Method;
                string paramDeclaringListWithoutMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}"));
                string paramNameListWithoutMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"__arg{p.Index}").Concat(new string[] { "method" }));
                string paramTypeListWithMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()}").Concat(new string[] { "const MethodInfo*" }));
                string methodTypeDef = $"typedef {method.ReturnInfo.Type.GetTypeName()} (*Callback)({paramTypeListWithMethodInfoStr})";
                for (int i = 0; i < methodInfo.Count; i++, methodIndex++)
                {
                    codes.Add($@"
    {method.ReturnInfo.Type.GetTypeName()} __ReversePInvokeMethod_{methodIndex}({paramDeclaringListWithoutMethodInfoStr})
    {{
        const MethodInfo* method = MetadataModule::GetMethodInfoByReversePInvokeWrapperIndex({methodIndex});
        {methodTypeDef};
        {(method.ReturnInfo.IsVoid ? "" : "return ")}((Callback)(method->methodPointerCallByInterp))({paramNameListWithoutMethodInfoStr});
    }}
");
                    stubCodes.Add($"\t\t{{\"{method.Sig}\", (Il2CppMethodPointer)__ReversePInvokeMethod_{methodIndex}}},\n");
                }
                Debug.Log($"[ReversePInvokeWrap.Generator] method:{method.MethodDef} wrapperCount:{methodInfo.Count}");
            }
 
            codes.Add(@"
    ReversePInvokeMethodData g_reversePInvokeMethodStub[]
    {
");
            codes.AddRange(stubCodes);
 
            codes.Add(@"
        {nullptr, nullptr},
    };
");
 
            frr.Replace("CODE", string.Join("", codes));
            frr.Commit(outputFile);
        }
    }
}