uplev xlua

This commit is contained in:
2020-08-05 07:08:47 +08:00
parent 52cc2a59aa
commit b3f98bfda1
298 changed files with 444 additions and 250 deletions

View File

@@ -527,6 +527,14 @@ namespace CSObjectWrapEditor
if (mb is FieldInfo && (mb as FieldInfo).FieldType.IsPointer) return true;
if (mb is PropertyInfo && (mb as PropertyInfo).PropertyType.IsPointer) return true;
foreach(var filter in memberFilters)
{
if (filter(mb))
{
return true;
}
}
foreach (var exclude in BlackList)
{
if (mb.DeclaringType.ToString() == exclude[0] && mb.Name == exclude[1])
@@ -544,7 +552,15 @@ namespace CSObjectWrapEditor
//指针目前不支持,先过滤
if (mb.GetParameters().Any(pInfo => pInfo.ParameterType.IsPointer)) return true;
if (mb is MethodInfo && (mb as MethodInfo).ReturnType.IsPointer) return false;
if (mb is MethodInfo && (mb as MethodInfo).ReturnType.IsPointer) return true;
foreach (var filter in memberFilters)
{
if (filter(mb))
{
return true;
}
}
foreach (var exclude in BlackList)
{
@@ -618,20 +634,28 @@ namespace CSObjectWrapEditor
GenOne(null, (type, type_info) =>
{
var type2fields = luaenv.NewTable();
foreach(var _type in types)
type2fields.Set(_type, _type.GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => !isMemberInBlackList(x)).ToArray());
type_info.Set("type2fields", type2fields);
type_info.Set("types", types.ToList());
}, templateRef.LuaEnumWrap, textWriter);
textWriter.Close();
}
static string NonmalizeName(string name)
{
return name.Replace("+", "_").Replace(".", "_").Replace("`", "_").Replace("&", "_").Replace("[", "_").Replace("]", "_").Replace(",", "_");
}
static void GenInterfaceBridge(IEnumerable<Type> types, string save_path)
{
foreach (var wrap_type in types)
{
if (!wrap_type.IsInterface) continue;
string filePath = save_path + wrap_type.ToString().Replace("+", "").Replace(".", "")
.Replace("`", "").Replace("&", "").Replace("[", "").Replace("]", "").Replace(",", "") + "Bridge.cs";
string filePath = save_path + NonmalizeName(wrap_type.ToString()) + "Bridge.cs";
StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8);
GenOne(wrap_type, (type, type_info) =>
{
@@ -839,7 +863,8 @@ namespace CSObjectWrapEditor
}
}
return true;
var lastPos = xParams.Length - 1;
return lastPos < 0 || xParams[lastPos].IsParamArray == yParams[lastPos].IsParamArray;
}
public int GetHashCode(MethodInfoSimulation obj)
{
@@ -927,6 +952,7 @@ namespace CSObjectWrapEditor
}
var delegates_groups = types.Select(delegate_type => makeMethodInfoSimulation(delegate_type.GetMethod("Invoke")))
.Where(d => d.DeclaringType.FullName != null)
.Concat(hotfxDelegates)
.GroupBy(d => d, comparer).Select((group) => new { Key = group.Key, Value = group.ToList()});
GenOne(typeof(DelegateBridge), (type, type_info) =>
@@ -973,8 +999,7 @@ namespace CSObjectWrapEditor
foreach (var wrap_type in types)
{
string filePath = save_path + wrap_type.ToString().Replace("+", "").Replace(".", "")
.Replace("`", "").Replace("&", "").Replace("[", "").Replace("]", "").Replace(",", "") + "Wrap.cs";
string filePath = save_path + NonmalizeName(wrap_type.ToString()) + "Wrap.cs";
StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8);
if (wrap_type.IsEnum)
{
@@ -1096,7 +1121,7 @@ namespace CSObjectWrapEditor
var extension_methods_from_lcs = (from t in LuaCallCSharp
where isDefined(t, typeof(ExtensionAttribute))
from method in t.GetMethods(BindingFlags.Static | BindingFlags.Public)
where isDefined(method, typeof(ExtensionAttribute))
where isDefined(method, typeof(ExtensionAttribute)) && !isObsolete(method)
where !method.ContainsGenericParameters || isSupportedGenericMethod(method)
select makeGenericMethodIfNeeded(method))
.Where(method => !lookup.ContainsKey(method.GetParameters()[0].ParameterType));
@@ -1104,7 +1129,7 @@ namespace CSObjectWrapEditor
var extension_methods = (from t in ReflectionUse
where isDefined(t, typeof(ExtensionAttribute))
from method in t.GetMethods(BindingFlags.Static | BindingFlags.Public)
where isDefined(method, typeof(ExtensionAttribute))
where isDefined(method, typeof(ExtensionAttribute)) && !isObsolete(method)
where !method.ContainsGenericParameters || isSupportedGenericMethod(method)
select makeGenericMethodIfNeeded(method)).Concat(extension_methods_from_lcs);
GenOne(typeof(DelegateBridgeBase), (type, type_info) =>
@@ -1264,6 +1289,8 @@ namespace CSObjectWrapEditor
public static List<string> assemblyList = null;
public static List<Func<MemberInfo, bool>> memberFilters = null;
static void AddToList(List<Type> list, Func<object> get, object attr)
{
object obj = get();
@@ -1376,6 +1403,10 @@ namespace CSObjectWrapEditor
{
BlackList.AddRange(get_cfg() as List<List<string>>);
}
if (isDefined(test, typeof(BlackListAttribute)) && typeof(Func<MemberInfo, bool>).IsAssignableFrom(cfg_type))
{
memberFilters.Add(get_cfg() as Func<MemberInfo, bool>);
}
if (isDefined(test, typeof(AdditionalPropertiesAttribute))
&& (typeof(Dictionary<Type, List<string>>)).IsAssignableFrom(cfg_type))
@@ -1450,6 +1481,8 @@ namespace CSObjectWrapEditor
#else
assemblyList = new List<string>();
#endif
memberFilters = new List<Func<MemberInfo, bool>>();
foreach (var t in check_types)
{
MergeCfg(t, null, () => t);
@@ -1604,7 +1637,6 @@ namespace CSObjectWrapEditor
#if !XLUA_GENERAL
static void callCustomGen()
{
return;
foreach (var method in (from type in XLua.Utils.GetAllTypes()
from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
where method.IsDefined(typeof(GenCodeMenuAttribute), false) select method))
@@ -1703,27 +1735,6 @@ namespace CSObjectWrapEditor
clear(GeneratorConfig.common_path);
}
#if UNITY_2018
[MenuItem("XLua/Generate Minimize Code", false, 3)]
public static void GenMini()
{
var start = DateTime.Now;
Directory.CreateDirectory(GeneratorConfig.common_path);
GetGenConfig(XLua.Utils.GetAllTypes());
luaenv.DoString("require 'TemplateCommon'");
var gen_push_types_setter = luaenv.Global.Get<LuaFunction>("SetGenPushAndUpdateTypes");
gen_push_types_setter.Call(GCOptimizeList.Where(t => !t.IsPrimitive && SizeOf(t) != -1).Distinct().ToList());
var xlua_classes_setter = luaenv.Global.Get<LuaFunction>("SetXLuaClasses");
xlua_classes_setter.Call(XLua.Utils.GetAllTypes().Where(t => t.Namespace == "XLua").ToList());
GenDelegateBridges(XLua.Utils.GetAllTypes(false));
GenCodeForClass(true);
GenLuaRegister(true);
callCustomGen();
Debug.Log("finished! use " + (DateTime.Now - start).TotalMilliseconds + " ms");
AssetDatabase.Refresh();
}
#endif
public delegate IEnumerable<CustomGenTask> GetTasks(LuaEnv lua_env, UserConfig user_cfg);
public static void CustomGen(string template_src, GetTasks get_tasks)
@@ -1818,4 +1829,4 @@ namespace CSObjectWrapEditor
}
#endif
}
}
}

0
Assets/XLua/Src/Editor/Generator.cs.meta Executable file → Normal file
View File

40
Assets/XLua/Src/Editor/Hotfix.cs Executable file → Normal file
View File

@@ -24,6 +24,10 @@ using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Diagnostics;
#if UNITY_2019
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
#endif
#endif
namespace XLua
@@ -467,9 +471,9 @@ namespace XLua
{
invoke.Parameters.Add(new ParameterDefinition(self));
}
foreach (var argType in argTypes)
for(int i = 0; i < argTypes.Count; i++)
{
invoke.Parameters.Add(new ParameterDefinition(argType));
invoke.Parameters.Add(new ParameterDefinition(method.Parameters[i].Name, (method.Parameters[i].IsOut ? Mono.Cecil.ParameterAttributes.Out : Mono.Cecil.ParameterAttributes.None), argTypes[i]));
}
invoke.ImplAttributes = Mono.Cecil.MethodImplAttributes.Runtime;
delegateDef.Methods.Add(invoke);
@@ -982,6 +986,15 @@ namespace XLua
}
}
}
int offset = 0;
for (int i = 0; i < instructions.Count; i++)
{
var instruction = instructions[i];
instruction.Offset = offset;
offset += instruction.GetSize();
}
for (int i = 0; i < instructions.Count; i++)
{
var instruction = instructions[i];
@@ -1576,6 +1589,18 @@ namespace XLua
namespace XLua
{
#if UNITY_2019
class MyCustomBuildProcessor : IPostBuildPlayerScriptDLLs
{
public int callbackOrder { get { return 0; } }
public void OnPostBuildPlayerScriptDLLs(BuildReport report)
{
var dir = Path.GetDirectoryName(report.files.Single(file => file.path.EndsWith("Assembly-CSharp.dll")).path);
Hotfix.HotfixInject(dir);
}
}
#endif
public static class Hotfix
{
static bool ContainNotAsciiChar(string s)
@@ -1590,9 +1615,16 @@ namespace XLua
return false;
}
#if !UNITY_2019
[PostProcessScene]
#endif
[MenuItem("XLua/Hotfix Inject In Editor", false, 3)]
public static void HotfixInject()
{
HotfixInject("./Library/ScriptAssemblies");
}
public static void HotfixInject(string assemblyDir)
{
if (Application.isPlaying)
{
@@ -1628,7 +1660,7 @@ namespace XLua
return;
}
var assembly_csharp_path = "./Library/ScriptAssemblies/Assembly-CSharp.dll";
var assembly_csharp_path = Path.Combine(assemblyDir, "Assembly-CSharp.dll");
var id_map_file_path = CSObjectWrapEditor.GeneratorConfig.common_path + "Resources/hotfix_id_map.lua.txt";
var hotfix_cfg_in_editor = CSObjectWrapEditor.GeneratorConfig.common_path + "hotfix_cfg_in_editor.data";
@@ -1670,7 +1702,7 @@ namespace XLua
var idMapFileNames = new List<string>();
foreach (var injectAssemblyPath in injectAssemblyPaths)
{
args[0] = injectAssemblyPath.Replace('\\', '/');
args[0] = Path.Combine(assemblyDir, Path.GetFileName(injectAssemblyPath));
if (ContainNotAsciiChar(args[0]))
{
throw new Exception("project path must contain only ascii characters");

0
Assets/XLua/Src/Editor/Hotfix.cs.meta Executable file → Normal file
View File

0
Assets/XLua/Src/Editor/LinkXmlGen.meta Executable file → Normal file
View File

0
Assets/XLua/Src/Editor/LinkXmlGen/LinkXmlGen.cs Executable file → Normal file
View File

0
Assets/XLua/Src/Editor/LinkXmlGen/LinkXmlGen.cs.meta Executable file → Normal file
View File

0
Assets/XLua/Src/Editor/LinkXmlGen/LinkXmlGen.tpl.txt Executable file → Normal file
View File

View File

0
Assets/XLua/Src/Editor/Template.meta Executable file → Normal file
View File

3
Assets/XLua/Src/Editor/Template/LuaClassWrap.tpl.txt Executable file → Normal file
View File

@@ -413,7 +413,8 @@ namespace XLua.CSObjectWrap
local keyType = overload:GetParameters()[0].ParameterType
local valueType = overload:GetParameters()[1].ParameterType%>
<%=GetCasterStatement(keyType, 2, "key", true)%>;
<%=GetCasterStatement(valueType, 3, "gen_to_be_invoked[key]")%>;
<%=GetCasterStatement(valueType, 3, "gen_value", true)%>;
gen_to_be_invoked[key] = gen_value;
<% else
in_pos = 0;
ForEachCsList(parameters, function(parameter, pi)

View File

View File

View File

View File

View File

View File

View File

25
Assets/XLua/Src/Editor/Template/LuaEnumWrap.tpl.txt Executable file → Normal file
View File

@@ -19,7 +19,13 @@ namespace XLua.CSObjectWrap
{
using Utils = XLua.Utils;
<%ForEachCsList(types, function(type)
local fields = type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
local fields_to_gen = {}
ForEachCsList(fields, function(field)
if field.Name ~= "value__" and not IsObsolute(field) then
table.insert(fields_to_gen, field)
end
end)
%>
public class <%=CSVariableName(type)%>Wrap
{
@@ -30,11 +36,15 @@ namespace XLua.CSObjectWrap
Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, translator, null, null, null, null, null);
Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0);
<%if #fields_to_gen <= 20 then%>
<% ForEachCsList(fields, function(field)
if field.Name == "value__" or IsObsolute(field) then return end
%>
Utils.RegisterObject(L, translator, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%>
<%else%>
Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom);
Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, translator);
@@ -49,9 +59,10 @@ namespace XLua.CSObjectWrap
{
translator.Push<%=CSVariableName(type)%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1));
}
<%if fields.Length > 0 then%>
<%if #fields_to_gen > 0 then%>
else if(lua_type == LuaTypes.LUA_TSTRING)
{
<%if #fields_to_gen <= 20 then%>
<%
local is_first = true
ForEachCsList(fields, function(field, i)
@@ -67,6 +78,16 @@ namespace XLua.CSObjectWrap
{
return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!");
}
<%else%>
try
{
translator.TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
}
catch (System.Exception e)
{
return LuaAPI.luaL_error(L, "cast to " + typeof(<%=CsFullTypeName(type)%>) + " exception:" + e);
}
<%end%>
}
<%end%>
else

View File

25
Assets/XLua/Src/Editor/Template/LuaEnumWrapGCM.tpl.txt Executable file → Normal file
View File

@@ -20,7 +20,13 @@ namespace XLua
public partial class ObjectTranslator
{
<%ForEachCsList(types, function(type)
local fields = type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
local fields_to_gen = {}
ForEachCsList(fields, function(field)
if field.Name ~= "value__" and not IsObsolute(field) then
table.insert(fields_to_gen, field)
end
end)
local v_type_name = CSVariableName(type)
%>
public void __Register<%=v_type_name%>(RealStatePtr L)
@@ -29,11 +35,15 @@ namespace XLua
Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, this, null, null, null, null, null);
Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0);
<%if #fields_to_gen <= 20 then%>
<% ForEachCsList(fields, function(field)
if field.Name == "value__" or IsObsolute(field) then return end
%>
Utils.RegisterObject(L, this, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%>
<%else%>
Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom<%=v_type_name%>);
Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, this);
@@ -46,9 +56,10 @@ namespace XLua
{
Push<%=v_type_name%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1));
}
<%if fields.Length > 0 then%>
<%if #fields_to_gen > 0 then%>
else if(lua_type == LuaTypes.LUA_TSTRING)
{
<%if #fields_to_gen <= 20 then%>
<%
local is_first = true
ForEachCsList(fields, function(field, i)
@@ -64,6 +75,16 @@ namespace XLua
{
return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!");
}
<%else%>
try
{
TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
}
catch (System.Exception e)
{
return LuaAPI.luaL_error(L, "cast to " + typeof(<%=CsFullTypeName(type)%>) + " exception:" + e);
}
<%end%>
}
<%end%>
else

View File

View File

View File

0
Assets/XLua/Src/Editor/Template/LuaRegister.tpl.txt Executable file → Normal file
View File

View File

0
Assets/XLua/Src/Editor/Template/LuaRegisterGCM.tpl.txt Executable file → Normal file
View File

View File

1
Assets/XLua/Src/Editor/Template/LuaWrapPusher.tpl.txt Executable file → Normal file
View File

@@ -76,6 +76,7 @@ namespace XLua
end
if type_info.Flag == CS.XLua.OptimizeFlag.PackAsTable then
%>
<%if PushObjectNeedTranslator(type_info) then %> ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);<%end%>
LuaAPI.xlua_pushcstable(L, <%=type_info.FieldInfos.Count%>, <%=type_id_var_name%>);
<%ForEachCsList(type_info.FieldInfos, function(fieldInfo)%>
LuaAPI.xlua_pushasciistring(L, "<%=fieldInfo.Name%>");

View File

0
Assets/XLua/Src/Editor/Template/PackUnpack.tpl.txt Executable file → Normal file
View File

View File

14
Assets/XLua/Src/Editor/Template/TemplateCommon.lua.txt Executable file → Normal file
View File

@@ -210,8 +210,14 @@ local function _CsFullTypeName(t)
end
function CsFullTypeName(t)
local name = _CsFullTypeName(t)
return xLuaClasses[name] and ("global::" .. name) or name
if t.DeclaringType then
local name = _CsFullTypeName(t)
local declaringTypeName = _CsFullTypeName(t.DeclaringType);
return xLuaClasses[declaringTypeName] and ("global::" .. name) or name
else
local name = _CsFullTypeName(t)
return xLuaClasses[name] and ("global::" .. name) or name
end
end
function CSVariableName(t)
@@ -411,6 +417,10 @@ function AccessorNeedTranslator(accessor)
return not accessor.IsStatic or not JustLuaType(accessor.Type)
end
function PushObjectNeedTranslator(type_info)
return IfAny(type_info.FieldInfos, function(field_info) return not JustLuaType(field_info.Type) end)
end
local GenericParameterAttributes = CS.System.Reflection.GenericParameterAttributes
local enum_and_op = debug.getmetatable(CS.System.Reflection.BindingFlags.Public).__band
local has_generic_flag = function(f1, f2)

View File

0
Assets/XLua/Src/Editor/TemplateRef.cs Executable file → Normal file
View File

0
Assets/XLua/Src/Editor/TemplateRef.cs.meta Executable file → Normal file
View File