Skip to content

Commit

Permalink
生成类型预载文件
Browse files Browse the repository at this point in the history
  • Loading branch information
zentia committed Dec 19, 2024
1 parent b2bb178 commit c42c08c
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 80 deletions.
5 changes: 5 additions & 0 deletions Assets/StreamingAssets/TypePreload.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local t

t = CS.ClassLuaCallCS
t = CS.UnityEngine.Vector3
t = CS.System.Collections.Generic.List(CS.System.String)
7 changes: 7 additions & 0 deletions Assets/StreamingAssets/TypePreload.lua.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/StreamingAssets/init_il2cpp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function createTypeProxy(namespace)
return
end
local fullName = namespace and (namespace .. '.' .. name) or name
local cls, csType = csTypeToClass(fullName)
local cls = csTypeToClass(fullName)
if cls then
rawset(cls, '.fqn', fullName)
tbl[name] = cls
Expand Down
8 changes: 4 additions & 4 deletions Assets/XLua/link.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<linker>
<assembly fullname="Assembly-CSharp">
<type fullname="ClassLuaCallCS" preserve="all"/>

</assembly>
<assembly fullname="mscorlib">
<type fullname="System.Collections.Generic.List`1" preserve="all"/>

</assembly>
<assembly fullname="Assembly-CSharp">
<type fullname="ClassLuaCallCS" preserve="all"/>

</assembly>
<assembly fullname="UnityEngine.CoreModule">
<type fullname="UnityEngine.Vector3" preserve="all"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('tte')

function PreLoadInfoTemplate(TypeRegisterInfo)
local typeRegisterInfo = listToLuaArray(TypeRegisterInfo)
local arg = FOR(typeRegisterInfo, function(item)
local t = CS.XLua.TypeExtensions.GetLuaFriendlyName(item.Type)
if t == '' then
return t
end
return string.format('\nt = %s', t)
end)
return string.format([[
local t
%s
]], arg)
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,3 @@ namespace XLuaStaticWrap
#endif
]], arg1, arg2)
end

function referWrapperMember(wrapperName, constructorName, methodName, propertyGetterName, propertySetterName)
local ret = ''
if constructorName then
ret = ', ' .. string.format('Constructor = %s.%s', wrapperName, constructorName)
end
if methodName then
ret = ret .. ', '
string.format('Method = %s.%s', wrapperName, methodName)
end
if propertyGetterName then
ret = ret .. ', '
string.format('PropertyGetter = %s.%s', wrapperName, propertyGetterName)
end
if propertySetterName then
ret = ret .. ', '
string.format('PropertySetter = %s.%s', wrapperName, propertySetterName)
end
return ret
end
30 changes: 29 additions & 1 deletion com.tencent.xlua/Editor/Src/Generator/IL2Cpp/FileExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public static void GenMarcoHeader(string outDir)
}
}

public static void GenRegisterInfo(string outDir, Dictionary<Type, XLuaIl2cpp.Editor.Generator.FileExporter.Script> types)
public static void GenRegisterInfo(string outDir, Dictionary<Type, Script> types)
{
#if XLUA_FULL
var registerInfos = RegisterInfoGenerator.GetRegisterInfos(types, true);
Expand All @@ -771,6 +771,34 @@ public static void GenRegisterInfo(string outDir, Dictionary<Type, XLuaIl2cpp.Ed
textWriter.Flush();
}
}

GenPreLoadInfo(registerInfos);
}

private static void GenPreLoadInfo(List<RegisterInfoForGenerate> registerInfos)
{
using (var luaEnv = new LuaEnv())
{
var assetPath = Path.GetFullPath("Packages/com.tencent.xlua/");
assetPath = assetPath.Replace("\\", "/");
luaEnv.DoString($"package.path = package.path..';{assetPath + "Editor/Resources/xlua/templates"}/?.lua'");
const string name = "preloadinfo.tpl.lua";
var path = Path.Combine(assetPath, $"Editor/Resources/xlua/templates/{name}");
var bytes = File.ReadAllBytes(path);
luaEnv.DoString<LuaFunction>(bytes, name);
var func = luaEnv.Global.Get<LuaFunction>("PreLoadInfoTemplate");
var registerInfoContent = func.Func<List<RegisterInfoForGenerate>, string>(registerInfos);
#if OS_GAME
var registerInfoPath = "RawAssets/LuaScripts/TypePreLoad.lua";
#else
var registerInfoPath = Application.streamingAssetsPath + "/TypePreLoad.lua";
#endif
using (var textWriter = new StreamWriter(registerInfoPath, false, Encoding.UTF8))
{
textWriter.Write(registerInfoContent);
textWriter.Flush();
}
}
}

public static void Gen(string csPath)
Expand Down
15 changes: 4 additions & 11 deletions com.tencent.xlua/Editor/Src/Generator/RegisterInfo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may be subject to their corresponding license terms.
* This file is subject to the terms and conditions defined in file 'LICENSE', which is part of this source code package.
*/
using XLua.TypeMapping;
using System.Reflection;
using System.Collections.Generic;
Expand All @@ -16,18 +10,17 @@ namespace Generator
{
internal class MemberRegisterInfoForGenerate : MemberRegisterInfo
{
// use string to work well in js
new public string MemberType;

new public string UseBindingMode;

new public string Constructor;
public string Constructor;

new public string Method;
public string Method;

new public string PropertyGetter;
public string PropertyGetter;

new public string PropertySetter;
public string PropertySetter;

}
internal class RegisterInfoForGenerate : RegisterInfo
Expand Down
126 changes: 83 additions & 43 deletions com.tencent.xlua/Runtime/Src/Default/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may be subject to their corresponding license terms.
* This file is subject to the terms and conditions defined in file 'LICENSE', which is part of this source code package.
*/

#if !XLUA_IL2CPP || !ENABLE_IL2CPP

using System;
Expand All @@ -14,7 +7,6 @@ namespace XLua
{
public static partial class TypeExtensions
{

internal static bool IsStruct(this Type type)
{
return type.IsValueType() && !type.IsEnum() && !type.IsPrimitive();
Expand All @@ -28,10 +20,67 @@ static string GetNameWithoutNamespace(Type type)
.Select(x => GetFriendlyName(x)).ToArray();
return type.Name.Split('`')[0] + "<" + string.Join(", ", genericArgumentNames) + ">";
}
else
{
return type.Name;
}

public static string GetLuaFriendlyName(Type type, Type[] genericArguments = null)
{
if (type == typeof(void))
return "";// error type?
if (type == typeof(int))
return "CS.System.Int32";
if (type == typeof(uint))
return "CS.System.UInt32";
if (type == typeof(short))
return "CS.System.Int16";
if (type == typeof(byte))
return "CS.System.Byte";
if (type == typeof(sbyte))
return "CS.System.SByte";
if (type == typeof(ushort))
return "CS.System.UInt16";
if (type == typeof(bool))
return "CS.System.Boolean";
if (type == typeof(long))
return "CS.System.Int64";
if (type == typeof(ulong))
return "CS.System.UInt64";
if (type == typeof(float))
return "CS.System.Single";
if (type == typeof(double))
return "CS.System.Double";
if (type == typeof(string))
return "CS.System.String";
if (type.IsArray)
return "";
if (type.IsGenericParameter)
return type.Name;
if (type.IsNested)
{
if (type.DeclaringType.IsNested)
{
if (type.DeclaringType.IsGenericTypeDefinition)
return GetLuaFriendlyName(type.DeclaringType, type.GetGenericArguments()) + '.' + type.Name;
return GetLuaFriendlyName(type.DeclaringType) + '.' + type.Name;
}

if (type.DeclaringType.IsGenericTypeDefinition)
{
var genericArgumentNames =
(genericArguments == null ? type.GetGenericArguments() : genericArguments)
.Select(x => GetLuaFriendlyName(x)).ToArray();
return type.DeclaringType.FullName.Split('`')[0] + "(" + string.Join(", ", genericArgumentNames) +
")" + "." + type.Name;
}
}

if (type.IsGenericType)
{
var genericArgumentNames = type.GetGenericArguments().Select(x => GetLuaFriendlyName(x)).ToArray();
return "CS." + (type.FullName == null ? type.Namespace + "." + type.Name : type.FullName).Split('`')[0] + "(" +
string.Join(", ", genericArgumentNames) + ")";
}
return $"CS.{type.FullName}";
}

public static string GetFriendlyName(this Type type, Type[] genericArguments = null)
Expand All @@ -40,72 +89,63 @@ public static string GetFriendlyName(this Type type, Type[] genericArguments = n
return "int";
if (type == typeof(uint))
return "uint";
else if (type == typeof(short))
if (type == typeof(short))
return "short";
else if (type == typeof(byte))
if (type == typeof(byte))
return "byte";
else if (type == typeof(sbyte))
if (type == typeof(sbyte))
return "sbyte";
else if (type == typeof(ushort))
if (type == typeof(ushort))
return "ushort";
else if (type == typeof(bool))
if (type == typeof(bool))
return "bool";
else if (type == typeof(long))
if (type == typeof(long))
return "long";
else if (type == typeof(ulong))
if (type == typeof(ulong))
return "ulong";
else if (type == typeof(float))
if (type == typeof(float))
return "float";
else if (type == typeof(double))
if (type == typeof(double))
return "double";
else if (type == typeof(decimal))
return "decimal";
else if (type == typeof(string))
if (type == typeof(string))
return "string";
else if (type == typeof(void))
if (type == typeof(void))
return "void";
else if (type.IsArray)
if (type.IsArray)
{
if (type.GetArrayRank() > 1)
{
return GetFriendlyName(type.GetElementType()) + "[" + new String(',', type.GetArrayRank() - 1) + "]";
}
else
{
return GetFriendlyName(type.GetElementType()) + "[]";
}
return GetFriendlyName(type.GetElementType()) + "[]";
}
else if (type.IsGenericParameter)
if (type.IsGenericParameter)
{
return type.Name;
}
else if (type.IsNested)
if (type.IsNested)
{
if (type.DeclaringType.IsNested) {
if (type.DeclaringType.IsNested)
{
if (type.DeclaringType.IsGenericTypeDefinition)
return GetFriendlyName(type.DeclaringType, type.GetGenericArguments())+ '.' + type.Name;
else
return GetFriendlyName(type.DeclaringType)+ '.' + type.Name;
return GetFriendlyName(type.DeclaringType) + '.' + type.Name;

}
else if (type.DeclaringType.IsGenericTypeDefinition)
if (type.DeclaringType.IsGenericTypeDefinition)
{
var genericArgumentNames = (genericArguments == null ? type.GetGenericArguments() : genericArguments)
.Select(x => GetFriendlyName(x)).ToArray();
var genericArgumentNames = (genericArguments == null ? type.GetGenericArguments() : genericArguments).Select(x => GetFriendlyName(x)).ToArray();
return type.DeclaringType.FullName.Split('`')[0] + "<" + string.Join(", ", genericArgumentNames) + ">" + '.' + type.Name;
}
else
{
return GetFriendlyName(type.DeclaringType) + '.' + GetNameWithoutNamespace(type);
}
return GetFriendlyName(type.DeclaringType) + '.' + GetNameWithoutNamespace(type);
}
else if (type.IsGenericType)
if (type.IsGenericType)
{
var genericArgumentNames = type.GetGenericArguments()
.Select(x => GetFriendlyName(x)).ToArray();
return (type.FullName == null ? type.Namespace + "." + type.Name : type.FullName).Split('`')[0] + "<" + string.Join(", ", genericArgumentNames) + ">";
}
else
return type.FullName;
return type.FullName;
}
}
}
Expand Down

0 comments on commit c42c08c

Please sign in to comment.