示例#1
0
    public static bool IsDiscardMemberInfo(Type type, MemberInfo memberInfo)
    {
        if (typeof(
                Delegate).IsAssignableFrom(type))
        {
            return(true);
        }

        if (TypeMemberFilterConfig.ContainsKey(type))
        {
            var filterList = TypeMemberFilterConfig[type];
            if (filterList.Contains(memberInfo.Name))
            {
                return(true);
            }
        }

        return(JsFrameworkUpgrade.IsDiscardMemberInfo(memberInfo));
    }
示例#2
0
    public static int MethodInfoComparison(MethodInfoAndIndex mi1, MethodInfoAndIndex mi2)
    {
        var result = JsFrameworkUpgrade.MethodInfoComparison(mi1, mi2);

        if (result != 0)
        {
            return(result);
        }

        var m1 = mi1.method;
        var m2 = mi2.method;

        if (!m1.IsStatic && m2.IsStatic)
        {
            return(-1);
        }
        if (m1.IsStatic && !m2.IsStatic)
        {
            return(1);
        }
        if (m1.Name != m2.Name)
        {
            return(string.Compare(m1.Name, m2.Name));
        }
        int max1 = 0;
        {
            var ps = m1.GetParameters();
            if (ps.Length > 0)
            {
                max1 = ps.Length;
            }
            for (int i = ps.Length - 1; i >= 0; i--)
            {
                if (!ps[i].IsOptional)
                {
                    break;
                }
                max1--;
            }
        }
        int max2 = 0;

        {
            var ps = m2.GetParameters();
            if (ps.Length > 0)
            {
                max2 = ps.Length;
            }
            for (int i = ps.Length - 1; i >= 0; i--)
            {
                if (!ps[i].IsOptional)
                {
                    break;
                }
                max2--;
            }
        }
        if (max1 > max2)
        {
            return(-1);
        }
        if (max2 > max1)
        {
            return(1);
        }
        return(0);
    }
示例#3
0
    public static void FilterTypeInfo(Type type, ATypeInfo ti)
    {
        bool bIsStaticClass   = type.IsClass && type.IsAbstract && type.IsSealed;
        bool bIsAbstractClass = type.IsClass && type.IsAbstract;

        var lstCons        = new List <ConstructorInfoAndIndex>();
        var lstField       = new List <FieldInfoAndIndex>();
        var lstPro         = new List <PropertyInfoAndIndex>();
        var pMethodNameSet = new HashSet <string>();
        var lstMethod      = new List <MethodInfoAndIndex>();

        for (int i = 0; i < ti.constructors.Length; i++)
        {
            if (bIsAbstractClass)
            {
                continue;
            }

            if (ti.constructors[i] == null)
            {
                lstCons.Add(new ConstructorInfoAndIndex(null, i));
                continue;
            }

            // don't generate MonoBehaviour constructor
            if (type == typeof(MonoBehaviour))
            {
                continue;
            }

            if (!IsMemberObsolete(ti.constructors[i]) &&
                !JSBCodeGenSettings.IsDiscardMemberInfo(type, ti.constructors[i]))
            {
                lstCons.Add(new ConstructorInfoAndIndex(ti.constructors[i], i));
            }
        }

        for (int i = 0; i < ti.fields.Length; i++)
        {
            if (typeof(Delegate).IsAssignableFrom(ti.fields[i].FieldType.BaseType))
            {
                //Debug.Log("[field]" + type.ToString() + "." + ti.fields[i].Name + "is delegate!");
            }
            if (ti.fields[i].FieldType.ContainsGenericParameters)
            {
                continue;
            }

            if (!IsMemberObsolete(ti.fields[i]) && !JSBCodeGenSettings.IsDiscardMemberInfo(type, ti.fields[i]))
            {
                lstField.Add(new FieldInfoAndIndex(ti.fields[i], i));
            }
        }


        for (int i = 0; i < ti.properties.Length; i++)
        {
            var pro = ti.properties[i];

            if (typeof(Delegate).IsAssignableFrom(pro.PropertyType.BaseType))
            {
                // Debug.Log("[property]" + type.ToString() + "." + pro.Name + "is delegate!");
            }

            var accessors = pro.GetAccessors();
            foreach (var v in accessors)
            {
                if (!pMethodNameSet.Contains(v.Name))
                {
                    pMethodNameSet.Add(v.Name);
                }
            }


            //            if (pro.GetIndexParameters().Length > 0)
            //                continue;
            //            if (pro.Name == "Item") //[] not support
            //                continue;

            // Skip Obsolete
            if (IsMemberObsolete(pro))
            {
                continue;
            }

            if (JSBCodeGenSettings.IsDiscardMemberInfo(type, pro))
            {
                continue;
            }

            lstPro.Add(new PropertyInfoAndIndex(pro, i));
        }

        lstPro = JsFrameworkUpgrade.SortPropertyInfoList(type, lstPro);

        for (int i = 0; i < ti.methods.Length; i++)
        {
            var method = ti.methods[i];

            // skip non-static method in static class
            if (bIsStaticClass && !method.IsStatic)
            {
                continue;
            }

            // skip property accessor
            if (method.IsSpecialName &&
                pMethodNameSet.Contains(method.Name))
            {
                continue;
            }

            if (method.IsSpecialName)
            {
                if (method.Name == "op_Addition" ||
                    method.Name == "op_Subtraction" ||
                    method.Name == "op_UnaryNegation" ||
                    method.Name == "op_Multiply" ||
                    method.Name == "op_Division" ||
                    method.Name == "op_Equality" ||
                    method.Name == "op_Inequality" ||
                    method.Name == "op_LessThan" ||
                    method.Name == "op_LessThanOrEqual" ||
                    method.Name == "op_GreaterThan" ||
                    method.Name == "op_GreaterThanOrEqual" ||
                    // SharpKit自己都不支持,就不搞支持了,自己封装接口吧
//                    method.Name == "op_Explicit" ||
                    method.Name == "op_Implicit")
                {
                    if (!method.IsStatic)
                    {
                        Debug.Log("IGNORE not-static special-name function: " + type.Name + "." + method.Name);
                        continue;
                    }
                }
                else if (method.Name.StartsWith("add_") || method.Name.StartsWith("remove_"))
                {
                    var eventName = method.Name.Replace("add_", "").Replace("remove_", "");
                    var realEvent = type.GetEvent(eventName, BindingFlagsMethod);
                    if (realEvent != null && IsMemberObsolete(realEvent))
                    {
                        continue;
                    }
                }
                else
                {
                    Debug.Log("IGNORE special-name function:" + type.Name + "." + method.Name);
                    continue;
                }
            }

            // Skip Obsolete
            if (IsMemberObsolete(method))
            {
                continue;
            }

            ParameterInfo[] ps;
            bool            bDiscard = false;

            //
            // ignore static method who contains T coming from class type
            // because there is no way to call it
            // SharpKit doesn't give c# the type of T
            //
            if (method.IsGenericMethodDefinition && /* || method.IsGenericMethod*/
                method.IsStatic)
            {
                ps = method.GetParameters();
                for (int k = 0; k < ps.Length; k++)
                {
                    if (ps[k].ParameterType.ContainsGenericParameters)
                    {
                        var Ts = JSDataExchangeMgr.RecursivelyGetGenericParameters(ps[k].ParameterType);
                        foreach (var t in Ts)
                        {
                            if (t.DeclaringMethod == null)
                            {
                                bDiscard = true;
                                break;
                            }
                        }
                        if (bDiscard)
                        {
                            break;
                        }
                    }
                }
                if (bDiscard)
                {
                    Debug.LogWarning("Ignore static method " + type.Name + "." + method.Name);
                    continue;
                }
            }

            // does it have unsafe parameter?
            bDiscard = false;
            ps       = method.GetParameters();
            for (int k = 0; k < ps.Length; k++)
            {
                var pt = ps[k].ParameterType;
                while (true)
                {
                    if (pt.IsPointer)
                    {
                        bDiscard = true;
                        break;
                    }
                    if (pt.HasElementType)
                    {
                        pt = pt.GetElementType();
                    }
                    else
                    {
                        break;
                    }
                }

                if (bDiscard)
                {
                    break;
                }
            }
            if (bDiscard)
            {
                Debug.Log(type.Name + "." + method.Name + " was discard because it has unsafe parameter.");
                continue;
            }


            if (JSBCodeGenSettings.IsDiscardMemberInfo(type, method))
            {
                continue;
            }

            lstMethod.Add(new MethodInfoAndIndex(method, i));
        }

        if (lstMethod.Count == 0)
        {
            ti.methodsOLInfo = null;
        }
        else
        {
            // sort methods
            lstMethod.Sort(MethodInfoComparison);
            ti.methodsOLInfo = new int[lstMethod.Count];
        }

        int  overloadedIndex = 1;
        bool bOL             = false;

        for (int i = 0; i < lstMethod.Count; i++)
        {
            ti.methodsOLInfo[i] = 0;
            if (bOL)
            {
                ti.methodsOLInfo[i] = overloadedIndex;
            }

            if (i < lstMethod.Count - 1 && lstMethod[i].method.Name == lstMethod[i + 1].method.Name &&
                ((lstMethod[i].method.IsStatic && lstMethod[i + 1].method.IsStatic) ||
                 (!lstMethod[i].method.IsStatic && !lstMethod[i + 1].method.IsStatic)))
            {
                if (!bOL)
                {
                    overloadedIndex     = 1;
                    bOL                 = true;
                    ti.methodsOLInfo[i] = overloadedIndex;
                }
                overloadedIndex++;
            }
            else
            {
                bOL             = false;
                overloadedIndex = 1;
            }
        }

        ti.constructors      = new ConstructorInfo[lstCons.Count];
        ti.constructorsIndex = new int[lstCons.Count];
        for (int k = 0; k < lstCons.Count; k++)
        {
            ti.constructors[k]      = lstCons[k].method;
            ti.constructorsIndex[k] = lstCons[k].index;
        }

        // ti.fields = lstField.ToArray();
        ti.fields      = new FieldInfo[lstField.Count];
        ti.fieldsIndex = new int[lstField.Count];
        for (int k = 0; k < lstField.Count; k++)
        {
            ti.fields[k]      = lstField[k].method;
            ti.fieldsIndex[k] = lstField[k].index;
        }

        // ti.properties = lstPro.ToArray();
        ti.properties      = new PropertyInfo[lstPro.Count];
        ti.propertiesIndex = new int[lstPro.Count];
        for (int k = 0; k < lstPro.Count; k++)
        {
            ti.properties[k]      = lstPro[k].method;
            ti.propertiesIndex[k] = lstPro[k].index;
        }

        ti.methods      = new MethodInfo[lstMethod.Count];
        ti.methodsIndex = new int[lstMethod.Count];

        for (int k = 0; k < lstMethod.Count; k++)
        {
            ti.methods[k]      = lstMethod[k].method;
            ti.methodsIndex[k] = lstMethod[k].index;
        }
    }