2022-10-17 21:38:39 +08:00
|
|
|
|
using HybridCLR.Editor.ABI;
|
|
|
|
|
using HybridCLR.Editor.Template;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-11-10 13:35:53 +08:00
|
|
|
|
using System.IO;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using System.Linq;
|
2023-11-10 13:35:53 +08:00
|
|
|
|
using System.Text;
|
2022-10-17 21:38:39 +08:00
|
|
|
|
using UnityEngine;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
|
|
|
|
namespace HybridCLR.Editor.ReversePInvokeWrap
|
|
|
|
|
{
|
2022-09-23 09:40:06 +08:00
|
|
|
|
public class Generator
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2023-11-10 13:35:53 +08:00
|
|
|
|
public void Generate(List<ABIReversePInvokeMethodInfo> methods, string outputFile)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2023-11-10 13:35:53 +08:00
|
|
|
|
string template = File.ReadAllText(outputFile, Encoding.UTF8);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
var frr = new FileRegionReplace(template);
|
|
|
|
|
var codes = new List<string>();
|
|
|
|
|
|
2022-10-17 21:38:39 +08:00
|
|
|
|
int methodIndex = 0;
|
|
|
|
|
var stubCodes = new List<string>();
|
|
|
|
|
foreach(var methodInfo in methods)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-10-17 21:38:39 +08:00
|
|
|
|
MethodDesc method = methodInfo.Method;
|
|
|
|
|
string paramDeclaringListWithoutMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}"));
|
2022-10-21 17:42:31 +08:00
|
|
|
|
string paramNameListWithoutMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"__arg{p.Index}").Concat(new string[] { "method" }));
|
2022-10-17 21:38:39 +08:00
|
|
|
|
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($@"
|
2022-10-21 17:42:31 +08:00
|
|
|
|
{method.ReturnInfo.Type.GetTypeName()} __ReversePInvokeMethod_{methodIndex}({paramDeclaringListWithoutMethodInfoStr})
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{{
|
2022-12-12 21:36:41 +08:00
|
|
|
|
const MethodInfo* method = MetadataModule::GetMethodInfoByReversePInvokeWrapperIndex({methodIndex});
|
2022-10-17 21:38:39 +08:00
|
|
|
|
{methodTypeDef};
|
2022-10-21 17:42:31 +08:00
|
|
|
|
{(method.ReturnInfo.IsVoid ? "" : "return ")}((Callback)(method->methodPointerCallByInterp))({paramNameListWithoutMethodInfoStr});
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}}
|
|
|
|
|
");
|
2022-10-17 21:38:39 +08:00
|
|
|
|
stubCodes.Add($"\t\t{{\"{method.Sig}\", (Il2CppMethodPointer)__ReversePInvokeMethod_{methodIndex}}},\n");
|
|
|
|
|
}
|
|
|
|
|
Debug.Log($"[ReversePInvokeWrap.Generator] method:{method.MethodDef} wrapperCount:{methodInfo.Count}");
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
codes.Add(@"
|
2022-10-17 21:38:39 +08:00
|
|
|
|
ReversePInvokeMethodData g_reversePInvokeMethodStub[]
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
");
|
2022-10-17 21:38:39 +08:00
|
|
|
|
codes.AddRange(stubCodes);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
|
|
|
|
codes.Add(@"
|
2022-10-17 21:38:39 +08:00
|
|
|
|
{nullptr, nullptr},
|
2022-09-22 08:56:07 +08:00
|
|
|
|
};
|
|
|
|
|
");
|
|
|
|
|
|
2022-10-17 21:38:39 +08:00
|
|
|
|
frr.Replace("CODE", string.Join("", codes));
|
2022-09-22 08:56:07 +08:00
|
|
|
|
frr.Commit(outputFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|