public Property(Type type, bool isStatic, bool canGet, bool canSet, string name, bool obsolete)
 {
     m_type     = WrapTextTools.Type2String(type);
     m_isStatic = isStatic;
     m_canGet   = canGet;
     m_canSet   = canSet;
     m_name     = name;
     m_obsolete = obsolete;
 }
Exemple #2
0
 void UpdateRegisterTypes()
 {
     List<Type> systemType = new List<Type>();
     List<Type> unityType = new List<Type>();
     List<Type> customType = new List<Type>();
     WrapReflectionTools.GetAllTypes(systemType, unityType, customType);
     string[] output = WrapTextTools.RegisterTypeToString(systemType, unityType, customType);
     //TODO
     string text = File.ReadAllText(WrapConfigFolder + "/RegisterTypesTemplate.txt", System.Text.Encoding.UTF8);// (Resources.Load("WrapPartTemplate") as TextAsset).text;
     text = text.Replace("{RegisterSystemTypes}", output[0]);
     text = text.Replace("{RegisterUnityTypes}", output[1]);
     text = text.Replace("{RegisterCustomTypes}", output[2]);
     if(!Directory.Exists(RegisterGenFolder)){
         Directory.CreateDirectory(RegisterGenFolder);
     }
     WrapTextTools.WriteAllText(RegisterGenFolder, "RegisterTypes.cs", text);
     AssetDatabase.Refresh();
 }
    public Method(string methodType, Type returnType, string methodName, System.Reflection.ParameterInfo[] param, int paramLength, bool isGeneric, bool obsolete)
    {
        m_methodType = methodType;
        if (returnType != null)
        {
            m_returnType = WrapTextTools.Type2String(returnType);
        }
        ;
        m_methodName = methodName;
        m_inType     = new string[paramLength];

        for (int i = 0; i < paramLength; i++)
        {
            m_inType[i] = WrapTextTools.Type2String(param[i].ParameterType);
        }
        m_isGeneric = isGeneric;
        m_obsolete  = obsolete;
    }
Exemple #4
0
    void UpdateWrapPart(string assemblyName, string classname, string manifest, List<string> typeDic, string[] propertys, 
        string wrapNew, string wrapSCall, string wrapMCall, string[] wrapIndex, string[] wrapOp)
    {
        string classWrapName = assemblyName.Replace(".","") + classname.Replace(".","");   //类似UnityEngineVector3,不带点

        string text = File.ReadAllText(WrapConfigFolder + "/WrapPartTemplate.txt", System.Text.Encoding.UTF8);// (Resources.Load("WrapPartTemplate") as TextAsset).text;

        string types = "";
        for(int i = 0; i < typeDic.Count; i++) {
            types += "\t\t" + typeDic[i] + "\n";
        }
        text = text.Replace("{TypesArray}", types);

        text = text.Replace("{WrapName}", classWrapName);
        text = text.Replace("{WrapSGet}", propertys[0]);
        text = text.Replace("{WrapSSet}", propertys[1]);
        text = text.Replace("{WrapMGet}", propertys[2]);
        text = text.Replace("{WrapMSet}", propertys[3]);

        text = text.Replace("{WrapNew}", wrapNew);
        text = text.Replace("{WrapSCall}", wrapSCall);
        text = text.Replace("{WrapMCall}", wrapMCall);

        text = text.Replace("{WrapIGet}", wrapIndex[0]);
        text = text.Replace("{WrapISet}", wrapIndex[1]);

        text = text.Replace("{WrapAdd}", wrapOp[0]);
        text = text.Replace("{WrapSub}", wrapOp[1]);
        text = text.Replace("{WrapMul}", wrapOp[2]);
        text = text.Replace("{WrapDiv}", wrapOp[3]);
        text = text.Replace("{WrapMod}", wrapOp[4]);

        //TODO OP 逻辑比较
         //  (op_Addition,op_subtraction,op_Multiply,op_Division,op_Modulus,op_GreaterThan,op_LessThan,op_GreaterThanOrEqual,op_LessThanOrEqual,op_Equality,op_Inequality

        if(string.IsNullOrEmpty(assemblyName)) {
            WrapTextTools.WriteAllText(WrapGenFolder, classname + ".cs", text);
            WrapTextTools.WriteAllText(WrapGenFolder, classname + ".txt", manifest);
        }
        else {
            WrapTextTools.WriteAllText(WrapGenFolder + "/" + assemblyName, classname + ".cs", text);
            WrapTextTools.WriteAllText(WrapGenFolder + "/" + assemblyName, classname + ".txt", manifest);
        }
    }
Exemple #5
0
    void OnlyAddClass(Type type)
    {
        List<string> _typeDic = new List<string>();
        string classFullName = WrapReflectionTools.GetTrueName(type);
        if(!WrapTextTools.IsSupported(classFullName))
            return;
        if(_classBlackList.Contains(classFullName))
            return;

        string assemblyName = WrapReflectionTools.GetWrapFolderName(type);
        string classname = WrapReflectionTools.GetWrapFileName(type);

        string manifest = "";//一个记事本,用来保存哪些内容做了Wrap

        //变量或属性
        List<Property> savePropertys = WrapReflectionTools.GetPropertys(type, ref manifest, _memberBlackList);
        string[] propertyPartStr = WrapTextTools.Propertys2PartStr(classFullName, savePropertys);

        //构造函数
        List<Method> constructMethods = WrapReflectionTools.GetConstructor(type, ref manifest);
        string wrapNew = WrapTextTools.Constructor2PartStr(classFullName, constructMethods);
        WrapTextTools.CallTypes2TypeStr(classFullName, constructMethods, _typeDic);

        //静态方法(最后的参数是忽略属性,因为属性也是一种方法)
        List<Method> staticMethods = WrapReflectionTools.GetStaticMethods(type, ref manifest, savePropertys, _memberBlackList);
        string wrapSCall = WrapTextTools.SCall2PartStr(classFullName, staticMethods);
        WrapTextTools.CallTypes2TypeStr(classFullName, staticMethods, _typeDic);

        //成员方法
        List<Method> memberMethods = WrapReflectionTools.GetInstanceMethods(type, ref manifest, savePropertys, _memberBlackList);
        string wrapMCall = WrapTextTools.MCall2PartStr(classFullName, memberMethods);
        WrapTextTools.CallTypes2TypeStr(classFullName, memberMethods, _typeDic);

        //索引
        List<Method> indexMethods = WrapReflectionTools.GetIndex(type, ref manifest);
        string[] wrapIndex = WrapTextTools.Index2PartStr(classFullName, indexMethods);

        //运算符(数学运算和逻辑运算)
        List<Method> opMethods = WrapReflectionTools.GetOp(type, ref manifest);
        string[] wrapOp = WrapTextTools.Op2PartStr(opMethods);

        UpdateWrapPart(assemblyName, classname, manifest, _typeDic, propertyPartStr,
            wrapNew, wrapSCall, wrapMCall, wrapIndex, wrapOp);
    }
    public static List <Method> GetInstanceMethods(Type type, ref string manifest, List <Property> ignoreProperty, List <string> filter)
    {
        List <Method> saveMethods = new List <Method> ();
        string        trueName    = GetTrueName(type);

        manifest += "\n成员方法\n";

        if (IsStaticClass(type))                //是否是静态类,静态类没有构造函数
        {
            manifest += "静态类没有成员方法";
            return(saveMethods);                                //静态类依然会反射出成员方法(比如ToString,GetType),但没法调用,我们不保存
        }

        System.Reflection.MethodInfo[] imis = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);         //这里没有获取私有方法,因为即使获取了西瓜也没有办法调用
        //基类的方法一起导出,这样可以自动调用基类

        PropertyInfo[] pis      = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        bool           hasIndex = false;

        for (int i = 0; i < pis.Length; i++)
        {
            if (pis[i].Name == "Item")
            {
                hasIndex = true;
                break;
            }
        }
        for (int i = 0; i < imis.Length; i++)
        {
            if (filter.Contains(trueName + "." + imis[i].Name))           //成员黑名单
            {
                continue;
            }

            //属性上面已经获取过了 这里做个判断,是否包含这个属性,包含的话continue,否则表示有方法名是get_,set_开头
            if (ContainsProperty(ignoreProperty, imis[i].Name))
            {
                continue;
            }

            if (imis[i].Name == "get_Item" || imis[i].Name == "set_Item")
            {
                if (hasIndex)
                {
                    continue;
                }
            }

            string retType = WrapTextTools.Type2String(imis[i].ReturnType);

            bool   isObsolete = IsObsolete(imis[i]);
            string s          = "";
            if (isObsolete)
            {
                s += "[Obsolete]";
            }
            s += "public " + retType + " ";
            s += imis [i].Name;
            bool isGeneric = (imis[i].ContainsGenericParameters && imis[i].IsGenericMethod);
            if (isGeneric)
            {
                s += "<T>";
            }

            s += "(";
            System.Reflection.ParameterInfo[] param = imis [i].GetParameters();
            if (param.Length == 0)
            {
                saveMethods.Add(new Method("MCall", imis [i].ReturnType, imis [i].Name, param, 0, isGeneric, isObsolete));
            }
            else
            {
                if (param [0].IsOptional)
                {
                    saveMethods.Add(new Method("MCall", imis [i].ReturnType, imis [i].Name, param, 0, isGeneric, isObsolete));
                }
                for (int j = 0; j < param.Length; j++)
                {
                    string paramType = WrapTextTools.Type2String(param[j].ParameterType);
                    bool   finish    = true;
                    if (paramType.EndsWith("&"))                       //ref,out
                    {
                        s     += "ref " + paramType.Substring(0, paramType.Length - 1) + " " + param [j].Name;
                        finish = false;
                    }
                    else
                    {
                        s += paramType + " " + param [j].Name;
                        if (param [j].IsOptional)
                        {
                            s += " = " + param [j].DefaultValue;
                        }
                    }
                    if (j != param.Length - 1)
                    {
                        s += ", ";
                    }

                    if (finish)
                    {
                        if (j == param.Length - 1 || param [j + 1].IsOptional)
                        {
                            saveMethods.Add(new Method("MCall", imis [i].ReturnType, imis [i].Name, param, j + 1, isGeneric, isObsolete));
                        }
                    }
                }
            }
            s        += ")";
            manifest += s + "\n";
        }
        return(saveMethods);
    }
    public static List <Method> GetStaticMethods(Type type, ref string manifest, List <Property> ignoreProperty, List <string> filter)
    {
        List <Method> saveMethods = new List <Method> ();
        string        trueName    = GetTrueName(type);

        manifest += "\n静态方法\n";

        //TODO 不包含op,不包含index,暂时不包含ref,in, out, 不包含IEnumrator
        System.Reflection.MethodInfo[] smis = type.GetMethods(BindingFlags.Public | BindingFlags.Static);         //这里没有获取私有方法,因为即使获取了西瓜也没有办法调用
        //基类的方法一起导出,这样可以自动调用基类
        for (int i = 0; i < smis.Length; i++)
        {
            if (filter.Contains(trueName + "." + smis[i].Name))           //成员黑名单
            {
                continue;
            }

            //属性上面已经获取过了 这里做个判断,是否包含这个属性,包含的话continue,否则表示有方法名是get_,set_开头
            if (ContainsProperty(ignoreProperty, smis [i].Name))
            {
                continue;
            }

            if (IsOp(smis [i]))
            {
                continue;
            }

            string retType = WrapTextTools.Type2String(smis[i].ReturnType);

            bool   isObsolete = IsObsolete(smis[i]);
            string s          = "";
            if (isObsolete)
            {
                s += "[Obsolete]";
            }
            s += "public static " + retType + " ";
            s += smis [i].Name;
            bool isGeneric = (smis[i].ContainsGenericParameters && smis[i].IsGenericMethod);
            if (isGeneric)
            {
                s += "<T>";
            }
            s += "(";
            System.Reflection.ParameterInfo[] param = smis [i].GetParameters();
            if (param.Length == 0)
            {
                saveMethods.Add(new Method("SCall", smis [i].ReturnType, smis [i].Name, param, 0, isGeneric, isObsolete));
            }
            else
            {
                if (param [0].IsOptional)
                {
                    saveMethods.Add(new Method("SCall", smis [i].ReturnType, smis [i].Name, param, 0, isGeneric, isObsolete));
                }
                for (int j = 0; j < param.Length; j++)
                {
                    string paramType = WrapTextTools.Type2String(param[j].ParameterType);
                    bool   finish    = true;
                    if (paramType.EndsWith("&"))                       //ref,out
                    {
                        s     += "ref " + paramType.Substring(0, paramType.Length - 1) + " " + param [j].Name;
                        finish = false;
                    }
                    else
                    {
                        s += paramType + " " + param [j].Name;
                        if (param [j].IsOptional)
                        {
                            s += " = " + param [j].DefaultValue;
                        }
                    }
                    if (j != param.Length - 1)
                    {
                        s += ", ";
                    }

                    if (finish)
                    {
                        if (j == param.Length - 1 || param [j + 1].IsOptional)
                        {
                            saveMethods.Add(new Method("SCall", smis [i].ReturnType, smis [i].Name, param, j + 1, isGeneric, isObsolete));
                        }
                    }
                }
            }
            s        += ")";
            manifest += s + "\n";
        }
        return(saveMethods);
    }
    public static List <Property> GetPropertys(Type type, ref string manifest, List <string> filter)
    {
        List <Property> savePropertys = new List <Property>();
        string          trueName      = GetTrueName(type);

        manifest += "\n字段\n";
        FieldInfo[] fis = type.GetFields();
        for (int i = 0; i < fis.Length; i++)
        {
            if (filter.Contains(trueName + "." + fis[i].Name))           //成员黑名单
            {
                continue;
            }

            bool isObsolete = IsObsolete(fis[i]);
            if (isObsolete)
            {
                manifest += "[Obsolete]";
            }

            manifest += "public ";
            string attributes = fis[i].Attributes.ToString();
            bool   isStatic   = attributes.Contains("Static");
            if (isStatic)
            {
                manifest += "static ";
            }
            bool isReadonly = attributes.Contains("Literal") && attributes.Contains("HasDefault"); //const
            isReadonly |= attributes.Contains("InitOnly");                                         //readonly
            if (isReadonly)
            {
                manifest += "readonly ";
            }
            manifest += WrapTextTools.Type2String(fis[i].FieldType) + " " + fis[i].Name + ";\n";
            savePropertys.Add(new Property(fis[i].FieldType, isStatic, true, !isReadonly, fis[i].Name, isObsolete));
        }

        manifest += "\n静态属性\n";
        PropertyInfo[] spis = type.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.SetProperty);
        for (int i = 0; i < spis.Length; i++)
        {
            if (filter.Contains(trueName + "." + spis[i].Name))           //成员黑名单
            {
                continue;
            }

            bool isObsolete = IsObsolete(spis[i]);
            if (isObsolete)
            {
                manifest += "[Obsolete]";
            }

            manifest += "public static ";
            manifest += WrapTextTools.Type2String(spis[i].PropertyType) + " " + spis[i].Name + "{";
            //这么判断的原因是可能是public Instance{get; private set;}
            if (spis[i].CanRead && spis[i].GetGetMethod(true).IsPublic)
            {
                manifest += "get;";
                savePropertys.Add(new Property(spis[i].PropertyType, true, true, false, spis[i].Name, isObsolete));
            }
            if (spis[i].CanWrite && spis[i].GetSetMethod(true).IsPublic)
            {
                manifest += "set;";
                savePropertys.Add(new Property(spis[i].PropertyType, true, false, true, spis[i].Name, isObsolete));
            }
            manifest += "}\n";
        }

        manifest += "\n实例属性\n";
        PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        //检查是否有Index
        MethodInfo[] mis      = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
        bool         hasIndex = false;

        for (int j = 0; j < mis.Length; j++)
        {
            if (mis[j].Name == "get_Item" || mis[j].Name == "set_Item")
            {
                hasIndex = true;
                break;
            }
        }

        for (int i = 0; i < pis.Length; i++)
        {
            bool isObsolete = IsObsolete(pis[i]);

            if (filter.Contains(trueName + "." + pis[i].Name))           //成员黑名单
            {
                continue;
            }

            if (pis[i].Name == "Item" && hasIndex)
            {
                continue;                //如果有get_Item或者set_Item,表示是[Index],否则表示一个属性
            }
            if (isObsolete)
            {
                manifest += "[Obsolete]";
            }
            manifest += "public ";
            manifest += WrapTextTools.Type2String(pis[i].PropertyType) + " " + pis[i].Name + "{";
            //这么判断的原因是可能是public Instance{get; private set;}
            if (pis[i].CanRead && pis[i].GetGetMethod(true).IsPublic)
            {
                manifest += "get;";
                savePropertys.Add(new Property(pis[i].PropertyType, false, true, false, pis[i].Name, isObsolete));
            }
            if (pis[i].CanWrite && pis[i].GetSetMethod(true).IsPublic)
            {
                manifest += "set;";
                savePropertys.Add(new Property(pis[i].PropertyType, false, false, true, pis[i].Name, isObsolete));
            }
            manifest += "}\n";
        }

        return(savePropertys);
    }