Skip to content

Commit

Permalink
[unity]il2cpp版本的ObjectPool可以改为在C++实现,fix Tencent/puerts#1960
Browse files Browse the repository at this point in the history
  • Loading branch information
zentia committed Jan 22, 2025
1 parent 94b2de6 commit 160d1b4
Show file tree
Hide file tree
Showing 34 changed files with 8,760 additions and 13,009 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function TypingTemplate(rawInfo)
using System;
using System.Collections.Generic;
using System.Reflection;
namespace XLuaIl2cpp
namespace XLua
{
public static class ExtensionMethodInfos_Gen
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function getArgValue(signature, LuaName, isRef)
PrimitiveSignatureCppTypeMap[signature],
LuaName)
end
elseif (signature == 'Pv' or signature == 'p') and not isRef then
elseif ((signature == 'Pv' or signature == 'p') or signature:sub(1,1) == 'T') and not isRef then
return string.format('DataTransfer::GetPointer<void>(apis, env, %s)', LuaName)
else
if PrimitiveSignatureCppTypeMap[signature] then
Expand All @@ -108,7 +108,7 @@ function getArgValue(signature, LuaName, isRef)
end
return signature == 'b' and 'false' or '0'
end
if (string.startsWith(signature, sigs.StructPrefix) or string.startsWith(signature, sigs.NullableStructPrefix)) and string.endsWith('_') then
if (string.startsWith(signature, sigs.StructPrefix) or string.startsWith(signature, sigs.NullableStructPrefix)) and string.endsWith(signature, '_') then
return '{}'
end
return 'nullptr'
Expand Down Expand Up @@ -161,7 +161,7 @@ function checkLuaArg(signature, index)
ret = ret ..
string.format([[!DataTransfer::IsAssignable(apis, env, _sv%d, %s, false))
return false;]], index, typeInfoVar)
elseif signature == 'O' then
elseif signature == 'O' or signature:sub(1,1) == 'T' then
return ''
elseif (string.startsWith(signature, sigs.StructPrefix) or string.startsWith(signature, sigs.NullableStructPrefix)) and string.endsWith(signature, '_') then
ret = ret ..
Expand All @@ -179,12 +179,12 @@ function needReturnValue(signature)
if first == 'P' and signature ~= 'Pv' then
return true
end
return false
return first == 'T'
end

function refSetback(signature, index)
local first = string.sub(signature, 1, 1)
if first == 'P' and signature ~= 'Pv' then
if (first == 'P' and signature ~= 'Pv') or first == 'T' then
local elementSignature = string.sub(signature, 2)
local val = CSValToLuaVal(elementSignature, string.format('*p%d', index))
if val then
Expand All @@ -202,21 +202,11 @@ function refSetback(signature, index)
ret%d = apis->native_object_to_value(env, TIp%d, p%d, false);
}
]], index, index, index, index, val, index, index, index, index, index)
elseif string.startsWith(elementSignature, sigs.NullableStructPrefix) and string.endsWith(elementSignature, '+') then
return string.format([[if (p%d == &up%d)
{
if (!p%d->hasValue)
apis->update_boxed_value(env, _sv%d, apis->create_null(env));
if (p%d == &up%d)
apis->update_boxed_value(env, _sv%d, %s);
}
]], index, index, index, index, index, index, index, val)
else
return string.format('\n\tint ret%d = %s;', index, val)
end
end
end

return ''
end

Expand All @@ -233,6 +223,8 @@ function refSetReturn(signature, index)
return string.format('\n\tapis->add_return(info, ret%d);', index)
end
end
elseif first == 'T' then
return string.format('\n\tapis->add_return(info, ret%d);', index)
end
return ''
end
Expand All @@ -253,7 +245,7 @@ function LuaValToCSVal(signature, LuaName, CSName, index)
return string.format([[
// LuaValToCSVal s
Il2CppString* %s = converter::Converter<Il2CppString*>::toCpp(apis, env, %s);]], CSName, LuaName)
elseif signature == 'Ps' then
elseif signature == 'Ps' or signature == 'Ts' then
return string.format([[ // LuaValToCSVal Ps
Il2CppString* u%s = converter::Converter<std::reference_wrapper<Il2CppString*>>::toCpp(apis, env, %s); // string ref
Il2CppString** %s = &u%s;
Expand All @@ -264,7 +256,7 @@ function LuaValToCSVal(signature, LuaName, CSName, index)
// LuaValToCSVal o/O
Il2CppObject* %s = LuaValueToCSRef(apis, TI%s, env, %s);
]], CSName, CSName, LuaName)
elseif signature == 'Po' or signature == 'PO' or signature == 'Pa' then
elseif signature == 'Po' or signature == 'PO' or signature == 'Pa' or signature == 'To' or signature == 'TO' or signature == 'Ta' then
return string.format([[ // LuaValToCSVal Po/PO
Il2CppObject* u%s = DataTransfer::GetPointer<Il2CppObject>(apis, env, apis->unboxing(env, %s)); // object ret
Il2CppObject** %s = &u%s;
Expand All @@ -274,7 +266,7 @@ function LuaValToCSVal(signature, LuaName, CSName, index)
%s* p%s = DataTransfer::GetPointer<%s>(apis, env, %s);
%s %s = p%s ? *p%s : %s {};
]], signature, CSName, signature, LuaName, signature, CSName, CSName, CSName, signature)
elseif (string.startsWith(signature, 'P' .. sigs.StructPrefix) or string.startsWith(signature, 'P' .. sigs.NullableStructPrefix)) and string.endsWith(signature, '_') then -- valuetype ref
elseif (string.startsWith(signature, 'P' .. sigs.StructPrefix) or string.startsWith(signature, 'T' .. sigs.StructPrefix)) and string.endsWith(signature, '_') then -- valuetype ref
local s = string.sub(signature, 2)
return string.format([[
// LuaValToCSVal Pstruct
Expand All @@ -285,7 +277,7 @@ function LuaValToCSVal(signature, LuaName, CSName, index)
%s = &u%s;
}
]], s, CSName, s, LuaName, s, CSName, CSName, CSName, CSName, CSName, CSName, CSName, CSName)
elseif signature:sub(1, 1) == 'P' and signature ~= 'Pv' then
elseif (signature:sub(1, 1) == 'P' and signature ~= 'Pv') or (signature:sub(1, 1) == 'T' and signature ~= 'Tv') then
local s = string.sub(signature, 2)
if PrimitiveSignatureCppTypeMap[s] then
if index then
Expand Down Expand Up @@ -394,7 +386,7 @@ function CSValToLuaVal(signature, CSName)
return string.format('apis->create_binary(env, %s, 0)', CSName)
elseif string.startsWith(signature, sigs.StructPrefix) and string.endsWith(signature, '_') then
return string.format('DataTransfer::CopyValueType(apis, env, %s, %s)', CSName, TIName)
elseif signature:sub(1, 1) == 'P' and signature ~= 'Pv' then
elseif (signature:sub(1, 1) == 'P' and signature ~= 'Pv') or signature:sub(1,1) == 'T' then
local elemSignature = string.sub(signature, 2)
if PrimitiveSignatureCppTypeMap[elemSignature] then
return string.format('converter::Converter<std::reference_wrapper<%s>>::toScript(apis, env, *%s)',
Expand All @@ -408,9 +400,13 @@ end

function genArgsLenCheck(parameterSignatures)
local requireNum = 0
while requireNum < #parameterSignatures and parameterSignatures[requireNum + 1]:sub(1, 1) ~= 'V' and parameterSignatures[requireNum + 1]:sub(1, 1) ~= 'D' do
local parameterCount = #parameterSignatures
while requireNum < parameterCount and parameterSignatures[requireNum + 1]:sub(1, 1) ~= 'V' and parameterSignatures[requireNum + 1]:sub(1, 1) ~= 'D' do
requireNum = requireNum + 1
end
if parameterCount > 0 and parameterSignatures[parameterCount]:sub(1,1) == 'T' and requireNum > 0 then
requireNum = requireNum - 1
end
return requireNum ~= #parameterSignatures and string.format('lua_args_len < %d || lua_args_len > %d', requireNum, #parameterSignatures) or string.format('lua_args_len != %d', #parameterSignatures)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--package.cpath = package.cpath .. ';C:/Users/Administrator/AppData/Roaming/JetBrains/Rider2024.2/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
--local dbg = require('emmy_core')
--dbg.tcpConnect('localhost', 9966)
package.cpath = package.cpath .. ';C:/Users/Administrator/AppData/Roaming/JetBrains/Rider2024.2/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

require("tte")
require("il2cpp_snippets")
Expand All @@ -14,6 +14,9 @@ function genFuncWrapper(wrapperInfo)
break
end
end
if wrapperInfo.Signature == 'vtssDO' then
a = 1
end
return TaggedTemplateEngine([[
// ]], wrapperInfo.CsName, [[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
require('tte')

function PreLoadInfoTemplate(normalTypes, evalTypes)
function PreLoadInfoTemplate(normalTypes)
local normal = listToLuaArray(normalTypes)
local arg1 = FOR(normal, function(item)
local t = CS.XLua.TypeExtensions.GetLuaFriendlyName(item, nil, false)
if t == '' then
return t
end
return string.format('\n t = %s', t)
end)
local eval = listToLuaArray(evalTypes)
local arg2 = FOR(eval, function(item)
local t = CS.XLua.TypeExtensions.GetLuaFriendlyName(item, nil, false)
if t == '' then
return t
end
return string.format('\n t = %s', t)
return string.format('\nt = %s', t)
end)
--local eval = listToLuaArray(evalTypes)
--local arg2 = FOR(eval, function(item)
-- local t = CS.XLua.TypeExtensions.GetLuaFriendlyName(item, nil, false)
-- if t == '' then
-- return t
-- end
-- return string.format('\n t = %s', t)
--end)
return string.format([[
function TypePreLoad1()
local t
%s
end
function TypePreLoad2()
local t
local t
%s
end
]], arg1, arg2)
]], arg1)
end
30 changes: 14 additions & 16 deletions com.tencent.xlua/Editor/Src/Generator/IL2Cpp/FileExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using XLua.TypeMapping;
using XLua;
using Utils = XLua.Editor.Generator.Utils;
using XLua.Editor.Generator;
using Object = System.Object;

using Mono.Reflection;
using UnityEngine;
using XLua.LuaDLL;

namespace XLua.Editor.Generator
{
Expand Down Expand Up @@ -143,12 +136,9 @@ private static bool IterateAllValueType(Type type, List<ValueTypeInfo> list)
{
"UnityEditor",
"Mono",
"CriWare",
"XLua",
"DG",
"PixPuerts",
"System.AsyncCallback",
"System.Action",
"System.Reflection",
"System.DateTimeParse",
"System.UnhandledExceptionEventHandler",
Expand Down Expand Up @@ -534,16 +524,16 @@ public static Dictionary<Type, Script> GenCPPWrap(string saveTo)
.GroupBy(s => s.Signature)
.Select(s => s.FirstOrDefault())
.ToList();

var bridgeInfos = delegateInvokes
var delegateInvokeSignature = delegateInvokes
.Select(m => new SignatureInfo
{
Signature = TypeUtils.GetMethodSignature(m, true),
CsName = m + " declare in " + (m.DeclaringType != null ? m.DeclaringType : "unknown class"),
ReturnSignature = TypeUtils.GetTypeSignature(m.ReturnType),
ThisSignature = null,
ParameterSignatures = m.GetParameters().Select(p => TypeUtils.GetParameterSignature(p)).ToList()
})
}).ToList();
var bridgeInfos = delegateInvokeSignature
.GroupBy(s => s.Signature)
.Select(s => s.FirstOrDefault())
.ToList();
Expand Down Expand Up @@ -701,7 +691,7 @@ public static Dictionary<Type, Script> GenCPPWrap(string saveTo)
return types;
}

public static void GenExtensionMethodInfos(string outDir, Dictionary<Type, FileExporter.Script> types)
public static void GenExtensionMethodInfos(string outDir, Dictionary<Type, Script> types)
{
var genTypes = types.Keys
.Where(t => !t.IsGenericTypeDefinition && !t.Name.StartsWith("<"))
Expand Down Expand Up @@ -823,9 +813,17 @@ public static void GenRegisterInfo(string outDir, Dictionary<Type, Script> types
GenPreLoadInfo(registerInfos);
}

private static Type[] PreLoadTypeBlackList =
{
// typeof(System.Action),
// typeof(XLua.ObjectCast),
// typeof(XLua.LuaDLL.lua_CSFunction),
};

public static void GenPreLoadInfo(List<RegisterInfoForGenerate> registerInfos)
{
var types = registerInfos.Select(item => item.Type).ToList();
var types = registerInfos.Select(item => item.Type).Where(item => !PreLoadTypeBlackList.Contains(item)).ToList();
// var delegateTypes = delegateInvokes.Select(item => item.DeclaringType).Where(item => !PreLoadTypeBlackList.Contains(item)).ToList();
using (var luaEnv = new LuaEnv())
{
var assetPath = Path.GetFullPath("Packages/com.tencent.xlua/");
Expand All @@ -836,7 +834,7 @@ public static void GenPreLoadInfo(List<RegisterInfoForGenerate> registerInfos)
var bytes = File.ReadAllBytes(path);
luaEnv.DoString<LuaFunction>(bytes, name);
var func = luaEnv.Global.Get<LuaFunction>("PreLoadInfoTemplate");
var registerInfoContent = func.Func<List<Type>, List<Type>, string>(types, delegateInvokes.Select(item => item.DeclaringType).ToList());
var registerInfoContent = func.Func<List<Type>, string>(types);
#if OS_GAME
var registerInfoPath = "RawAssets/LuaScripts/TypePreLoad.lua";
#else
Expand Down
Loading

0 comments on commit 160d1b4

Please sign in to comment.