Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
* upstream/master: (24 commits)
  当方法返回指针时过滤掉 (Tencent#660)
  更新iOS cmake toolchain以支持macOS 10.15 (Tencent#657)
  修复生成扩展方法未判断方法是否过时 (Tencent#649)
  删除unity2018下的Generate Minimize Code选项
  * fix LazyReflectionCall with overloaded methods (Tencent#638)
  update: [98989] cmake在osx平台添加一个预编译定义,为了能在Emmy-Lua在ide下调试时能够加载cpath (Tencent#629)
  示例hotfix配置修改为GetTypes,否则照抄这个非public类型不被注入
  直接通过CS.XX访问一个内嵌类时,会在加载外层类的时候顺便加载了内嵌类,但没有建立type到id的映射,导致xlua.private_accessible认为改内嵌类并未加载,fix Tencent#622
  fix error display when StaticLuaCallbacks::ImportType throws
  unity 2018以后版本的兼容性问题的新解决办法。
  支持通过代码来过滤不需生成代码的方法,主要用于解决2018以后unity运行和编译用的mono不一致的问题。
  非Assembly-CSharp程序集注入时,out参数处理有误
  这里有可能是通过反射得到未设置泛型的类型导致lua脚本报错生成代码失败 (Tencent#617)
  unity的.net 4下,object obj = new sbyte[] { 1, 2 };obj is byte[]会判断为true,修改为兼容性更好的写法,防止sbyte[] 在.net 4下push到lua变成字符串
  自动配置CSharpCallLua列表示例里头,过滤掉未实例化的泛型和引用了editor的类型
  解决bug:当采用反射方式注册枚举值时,如果一个枚举有多个相同的值,比如A,B都是1,那么在lua里头访问B将会为空
  最小生成模式枚举生成代码报错
  最小生成模式枚举生成代码报错
  fix dependence alert
  chunkname 应和脚本文件名一致(否则IDE调试不了) (Tencent#600)
  ...

# Conflicts:
#	build/CMakeLists.txt
#	build/cmake/ios.toolchain.cmake
  • Loading branch information
onlyfeng committed Oct 26, 2019
2 parents 9264903 + bdd8939 commit f21f074
Show file tree
Hide file tree
Showing 16 changed files with 499 additions and 446 deletions.
2 changes: 1 addition & 1 deletion Assets/XLua/Doc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ il2cpp默认会对诸如引擎、c#系统api,第三方dll等等进行代码剪

编译链接的是:unity安装目录\Editor\Data\MonoBleedingEdge\lib\mono\4.7.1-api\mscorlib.dll

解决办法:2.1.14支持外部mono环境,安装个Unity2017,然后把Unity2017安装目录下的MonoBleedingEdge目录拷贝到工程目录下的Tools目录下即可
解决办法:用黑名单排除报错方法即可。不过2019年8月6号以前的版本的黑名单配置对泛型不友好,要一个个泛型实例的配置(比如,Dictionary<int, int>和Dictionary<float, int>要分别配置),而目前发现该问题主要出在泛型Dictionary上。可以更新到2019年8月6号之后的版本,该版本支持配置一个过滤器对泛型方法过滤。这里有对unity 2018的Dictionary的[针对性配置](https://github.com/Tencent/xLua/blob/master/Assets/XLua/Editor/ExampleConfig.cs#L277),直接拷贝使用,如果碰到其它泛型也有多出来的方法,参考Dictionary进行配置

## Plugins源码在哪里可以找到,怎么使用?

Expand Down
39 changes: 35 additions & 4 deletions Assets/XLua/Editor/ExampleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static class ExampleConfig
// }
// }
// }
// return delegate_types.Distinct().ToList();
// return delegate_types.Where(t => t.BaseType == typeof(MulticastDelegate) && !hasGenericParameter(t) && !delegateHasEditorRef(t)).Distinct().ToList();
// }
//}
//--------------end 纯lua编程配置参考----------------------------
Expand All @@ -142,9 +142,9 @@ public static class ExampleConfig
//{
// get
// {
// return (from type in Assembly.Load("Assembly-CSharp").GetExportedTypes()
// where type.Namespace == null || !type.Namespace.StartsWith("XLua")
// select type);
// return (from type in Assembly.Load("Assembly-CSharp").GetTypes()
// where type.Namespace == null || !type.Namespace.StartsWith("XLua")
// select type);
// }
//}
//--------------begin 热补丁自动化配置-------------------------
Expand Down Expand Up @@ -273,4 +273,35 @@ public static class ExampleConfig
new List<string>(){"System.IO.DirectoryInfo", "Create", "System.Security.AccessControl.DirectorySecurity"},
new List<string>(){"UnityEngine.MonoBehaviour", "runInEditMode"},
};

#if UNITY_2018_1_OR_NEWER
[BlackList]
public static Func<MemberInfo, bool> MethodFilter = (memberInfo) =>
{
if (memberInfo.DeclaringType.IsGenericType && memberInfo.DeclaringType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
if (memberInfo.MemberType == MemberTypes.Constructor)
{
ConstructorInfo constructorInfo = memberInfo as ConstructorInfo;
var parameterInfos = constructorInfo.GetParameters();
if (parameterInfos.Length > 0)
{
if (typeof(System.Collections.IEnumerable).IsAssignableFrom(parameterInfos[0].ParameterType))
{
return true;
}
}
}
else if (memberInfo.MemberType == MemberTypes.Method)
{
var methodInfo = memberInfo as MethodInfo;
if (methodInfo.Name == "TryAdd" || methodInfo.Name == "Remove" && methodInfo.GetParameters().Length == 2)
{
return true;
}
}
}
return false;
};
#endif
}
2 changes: 1 addition & 1 deletion Assets/XLua/Examples/02_U3DScripting/LuaBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Awake()
scriptEnv.Set(injection.name, injection.value);
}

luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
luaEnv.DoString(luaScript.text, "LuaTestScript", scriptEnv);

Action luaAwake = scriptEnv.Get<Action>("awake");
scriptEnv.Get("start", out luaStart);
Expand Down
54 changes: 29 additions & 25 deletions Assets/XLua/Src/Editor/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,14 @@ static bool isMemberInBlackList(MemberInfo mb)
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])
Expand All @@ -544,7 +552,15 @@ static bool isMethodInBlackList(MethodBase mb)

//指针目前不支持,先过滤
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)
{
Expand Down Expand Up @@ -932,6 +948,7 @@ static void GenDelegateBridge(IEnumerable<Type> types, string save_path, IEnumer
}

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) =>
Expand Down Expand Up @@ -1120,15 +1137,15 @@ public static void GenLuaRegister(bool minimum = false)
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));

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) =>
Expand Down Expand Up @@ -1288,6 +1305,8 @@ public static void GenPackUnpack(IEnumerable<Type> types, string save_path)

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();
Expand Down Expand Up @@ -1400,6 +1419,10 @@ static void MergeCfg(MemberInfo test, Type cfg_type, Func<object> get_cfg)
{
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))
Expand Down Expand Up @@ -1474,6 +1497,8 @@ public static void GetGenConfig(IEnumerable<Type> check_types)
#else
assemblyList = new List<string>();
#endif
memberFilters = new List<Func<MemberInfo, bool>>();

foreach (var t in check_types)
{
MergeCfg(t, null, () => t);
Expand Down Expand Up @@ -1726,27 +1751,6 @@ public static void ClearAll()
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)
Expand Down Expand Up @@ -1841,4 +1845,4 @@ public static void CheckGenrate(BuildTarget target, string pathToBuiltProject)
}
#endif
}
}
}
6 changes: 3 additions & 3 deletions Assets/XLua/Src/Editor/Hotfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,9 @@ MethodDefinition createDelegateFor(MethodDefinition method, AssemblyDefinition a
{
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);
Expand Down Expand Up @@ -1621,7 +1621,7 @@ public static void HotfixInject()
var mono_path = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName),
"Data/MonoBleedingEdge/bin/mono.exe");
#endif
var inject_tool_path = Path.Combine(Application.dataPath, "Tools/XLuaHotfixInject.exe");
var inject_tool_path = "./Tools/XLuaHotfixInject.exe";
if (!File.Exists(inject_tool_path))
{
UnityEngine.Debug.LogError("please install the Tools");
Expand Down
5 changes: 1 addition & 4 deletions Assets/XLua/Src/Editor/Template/LuaEnumWrap.tpl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ namespace XLua.CSObjectWrap
Utils.RegisterObject(L, translator, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%>
<%else%>
foreach(var e in System.Enum.GetValues(typeof(<%=CsFullTypeName(type)%>)))
{
Utils.RegisterObject(L, translator, Utils.CLS_IDX, e.ToString(), e);
}
Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom);

Expand Down
7 changes: 2 additions & 5 deletions Assets/XLua/Src/Editor/Template/LuaEnumWrapGCM.tpl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ namespace XLua
Utils.RegisterObject(L, this, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%>
<%else%>
foreach(var e in System.Enum.GetValues(typeof(<%=CsFullTypeName(type)%>)))
{
Utils.RegisterObject(L, translator, Utils.CLS_IDX, e.ToString(), e);
}
Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom<%=v_type_name%>);

Expand Down Expand Up @@ -81,7 +78,7 @@ namespace XLua
<%else%>
try
{
translator.TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
}
catch (System.Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/XLua/Src/LuaDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static int lua_error(IntPtr L)
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaopen_i64lib(IntPtr L);//[,,m]

#if !UNITY_SWITCH || UNITY_EDITOR
#if (!UNITY_SWITCH && !UNITY_WEBGL) || UNITY_EDITOR
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaopen_socket_core(IntPtr L);//[,,m]
#endif
Expand Down
4 changes: 2 additions & 2 deletions Assets/XLua/Src/ObjectTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public bool TryDelayWrapLoader(RealStatePtr L, Type type)
{
continue;
}
TryDelayWrapLoader(L, nested_type);
GetTypeId(L, nested_type);
}

return true;
Expand Down Expand Up @@ -1148,7 +1148,7 @@ public void PushAny(RealStatePtr L, object o)
{
LuaAPI.lua_pushstring(L, o as string);
}
else if (o is byte[])
else if (type == typeof(byte[]))
{
LuaAPI.lua_pushstring(L, o as byte[]);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/XLua/Src/StaticLuaCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ internal static int Print(RealStatePtr L)
}
#endif

#if !UNITY_SWITCH || UNITY_EDITOR
#if (!UNITY_SWITCH && !UNITY_WEBGL) || UNITY_EDITOR
[MonoPInvokeCallback(typeof(LuaCSFunction))]
internal static int LoadSocketCore(RealStatePtr L)
{
Expand Down
29 changes: 20 additions & 9 deletions Assets/XLua/Src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,15 @@ static void makeReflectionWrap(RealStatePtr L, Type type, int cls_field, int cls
}
}

public static void loadUpvalue(RealStatePtr L, Type type, string metafunc, int num)
public static void loadUpvalue(RealStatePtr L, Type type, string metafunc, int index)
{
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
LuaAPI.xlua_pushasciistring(L, metafunc);
LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
translator.Push(L, type);
LuaAPI.lua_rawget(L, -2);
for (int i = 1; i <= num; i++)
LuaAPI.lua_remove(L, -2);
for (int i = 1; i <= index; i++)
{
LuaAPI.lua_getupvalue(L, -i, i);
if (LuaAPI.lua_isnil(L, -1))
Expand All @@ -598,13 +599,23 @@ public static void loadUpvalue(RealStatePtr L, Type type, string metafunc, int n
LuaAPI.lua_setupvalue(L, -i - 2, i);
}
}
for (int i = 0; i < num; i++)
for (int i = 0; i < index; i++)
{
LuaAPI.lua_remove(L, -num - 1);
LuaAPI.lua_remove(L, -2);
}
}

public static void MakePrivateAccessible(RealStatePtr L, Type type)
public static void RegisterEnumType(RealStatePtr L, Type type)
{
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
foreach (var name in Enum.GetNames(type))
{
RegisterObject(L, translator, Utils.CLS_IDX, name, Enum.Parse(type, name));
}
}


public static void MakePrivateAccessible(RealStatePtr L, Type type)
{
LuaAPI.lua_checkstack(L, 20);

Expand All @@ -628,7 +639,8 @@ public static void MakePrivateAccessible(RealStatePtr L, Type type)

loadUpvalue(L, type, LuaIndexsFieldName, 2);
int obj_getter = LuaAPI.lua_gettop(L);
int obj_field = obj_getter - 1;
loadUpvalue(L, type, LuaIndexsFieldName, 1);
int obj_field = LuaAPI.lua_gettop(L);

loadUpvalue(L, type, LuaNewIndexsFieldName, 1);
int obj_setter = LuaAPI.lua_gettop(L);
Expand Down Expand Up @@ -725,7 +737,6 @@ internal static int LazyReflectionCall(RealStatePtr L)
if (memberType == LazyMemberTypes.FieldGet)
{
loadUpvalue(L, type, LuaIndexsFieldName, 2);
LuaAPI.lua_remove(L, -2);
}
else
{
Expand Down Expand Up @@ -759,7 +770,6 @@ internal static int LazyReflectionCall(RealStatePtr L)
if (memberType == LazyMemberTypes.PropertyGet)
{
loadUpvalue(L, type, LuaIndexsFieldName, 2);
LuaAPI.lua_remove(L, -2);
}
else
{
Expand Down Expand Up @@ -1358,8 +1368,9 @@ public static void SetCSTable(RealStatePtr L, Type type, int cls_table)
LuaAPI.xlua_pushasciistring(L, path[i]);
if (0 != LuaAPI.xlua_pgettable(L, -2))
{
var err = LuaAPI.lua_tostring(L, -1);
LuaAPI.lua_settop(L, oldTop);
throw new Exception("SetCSTable for [" + type + "] error: " + LuaAPI.lua_tostring(L, -1));
throw new Exception("SetCSTable for [" + type + "] error: " + err);
}
if (LuaAPI.lua_isnil(L, -1))
{
Expand Down
Binary file modified Tools/XLuaHotfixInject.exe
Binary file not shown.
Binary file modified Tools/XLuaHotfixInject.pdb
Binary file not shown.
Loading

0 comments on commit f21f074

Please sign in to comment.