Exemplo n.º 1
0
        public void GenCS(IDataReadable reader)
        {
            string className = reader.currentDataTypeName;

            CodeGener protobufBaseGener = new CodeGener(ProtobufData.nameSpace, className);

            protobufBaseGener.newClass.AddMemberCostomAttribute("ProtoBuf.ProtoContract");
            protobufBaseGener.AddImport("System", "ProtoBuf");

            List <string> comment = reader.GetComment();

            protobufBaseGener
            .AddBaseType("ProtobufData<" + className + ">")
            .AddMemberField(typeof(string), "fileName", (member) =>
            {
                member.AddFieldMemberInit("\"" + className + "\"");
            }, System.CodeDom.MemberAttributes.Static | System.CodeDom.MemberAttributes.Final | System.CodeDom.MemberAttributes.Public);

            reader.GetTitle().ForEach((i, title) =>
            {
                string[] titleSplit = title.Split('|');
                string varName      = titleSplit[0];
                string typeName     = titleSplit[1];
                protobufBaseGener.AddMemberProperty(typeName.GetTypeByString(), varName, (member) =>
                {
                    member.AddComment(comment[i], true);
                    member.AddMemberCostomAttribute("ProtoBuf.ProtoMember", (i + 1).ToString());
                });
            });
            PathEx.MakeFileDirectoryExist(PathConfig.GetLoaclGameDataClassPath(PathConfig.DataType.Protobuf));
            protobufBaseGener.GenCSharp(PathConfig.GetLoaclGameDataClassPath(PathConfig.DataType.Protobuf));
        }
Exemplo n.º 2
0
        private void GenEnum(Type enumType)
        {
            if (enumType == null)
            {
                return;
            }
            var className = GetClassFileName(enumType);
            var enumNames = enumType.GetEnumNames();

            CodeGener gener = new CodeGener("UniToLua", className);

            List <CodeStatement> registerMethodStatement = new List <CodeStatement>();

            registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.BeginEnum(\"{enumType.Name}\");"));
            foreach (var enumName in enumNames)
            {
                registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.RegVar(\"{enumName}\", get_{enumName}, null);"));
            }
            registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.EndEnum();"));

            gener.AddMemberMethod(typeof(void), "Register", new Dictionary <string, Type>()
            {
                { "L", typeof(ILuaState) }
            },
                                  MemberAttributes.Public | MemberAttributes.Static, registerMethodStatement.ToArray());

            foreach (var enumName in enumNames)
            {
                GenRegEnum(gener, enumType, enumName);
            }

            gener.GenCSharp(outputPath);
        }
Exemplo n.º 3
0
        public void GenScript(BaseUI ui)
        {
            rootGo = ui.gameObject;
            uiType = ui.GetType();
            string    viewClassName = uiType.Name + "View";
            CodeGener gener         = new CodeGener(UIView.uiViewNameSpace, viewClassName);

            gener.AddBaseType("UIView")
            .AddImport("ResetCore.UGUI");

            rootGo.transform.DoToSelfAndAllChildren((tran) =>
            {
                GameObject go = tran.gameObject;
                if (!go.name.StartsWith(UIView.genableSign))
                {
                    return;
                }

                string goName = go.name.Replace(UIView.genableSign, string.Empty);
                var coms      = go.GetComponents <Component>();
                foreach (var com in coms)
                {
                    Type comType = com.GetType();
                    if (!UIView.uiCompTypeList.Contains(comType))
                    {
                        continue;
                    }

                    gener.AddMemberProperty(comType, UIView.comNameDict[comType] + goName);
                }
            });
            gener.GenCSharp(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath));
        }
Exemplo n.º 4
0
    private static void CreateBundleClass()
    {
        CodeGener gener = new CodeGener(nameSpace, bundleClassName);

        gener.AddBaseType("ObjDataBundle<" + className + ">");
        gener.newClass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute))));
        gener.GenCSharp(classPath);
    }
Exemplo n.º 5
0
        private void GenScriptableObject(List <FieldInfo> list, ScriptComponentLoader scrLoader, Component comp)
        {
            CodeGener gener = new CodeGener("ResetCore.ReAssembly.Data", GetScrObjName(comp));

            gener
            .AddImport("UnityEngine")
            .AddBaseType("ScriptableObject");
            foreach (FieldInfo info in list)
            {
                gener.AddMemberField(info.FieldType, info.Name, null, System.CodeDom.MemberAttributes.Public);
            }
            gener.GenCSharp(DllManagerConst.scriptableCSOutputPath);
        }
Exemplo n.º 6
0
        //生成界面代码
        public void GenViewScript(BaseUI ui)
        {
            rootGo = ui.gameObject;
            uiType = ui.GetType();
            string viewClassName = uiType.Name + "View";

            if (File.Exists(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath, viewClassName + ".cs")))
            {
                if (!EditorUtility.DisplayDialog("提示", "已存在" + viewClassName + ",是否要覆盖?", "继续", "取消"))
                {
                    return;
                }
            }

            CodeGener gener = new CodeGener(UIView.uiViewNameSpace, viewClassName);

            gener.AddBaseType(typeof(UIView).Name)
            .AddImport("ResetCore.UGUI");

            rootGo.transform.DoToSelfAndAllChildren((tran) =>
            {
                GameObject go = tran.gameObject;
                if (!go.name.StartsWith(UIView.genableSign))
                {
                    return;
                }

                string goName = go.name.Replace(UIView.genableSign, string.Empty);

                if (goName.Contains(splitGoNameAndProperty[0]))
                {
                    goName = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries)[0];
                }

                gener.AddMemberProperty(typeof(GameObject), UIView.goName + goName);
                var coms = go.GetComponents <Component>();
                foreach (var com in coms)
                {
                    Type comType = com.GetType();
                    if (!UIView.uiCompTypeList.Contains(comType))
                    {
                        continue;
                    }

                    gener.AddMemberProperty(comType, UIView.comNameDict[comType] + goName);
                }
            });
            gener.GenCSharp(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 生成序列化
        /// </summary>
        /// <param name="type"></param>
        public static void GenSerializer(Type type)
        {
            if (type == null)
            {
                return;
            }

            var       typeName  = type.FullName;
            var       finalName = GetSerializerName(type);
            CodeGener g         = new CodeGener(serializerNamespace, finalName);

            g.AddBaseType("IXmlSerializer<" + type.FullName + ">");
            g.AddImport("System.Xml");

            //ToXElement方法
            CodeMemberMethod toXElementMethod = new CodeMemberMethod();

            toXElementMethod.Name       = "Write";
            toXElementMethod.Attributes = MemberAttributes.Public;
            toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(XmlElement)), "xElement"));
            toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "name"));
            toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeName), "obj"));
            //函数体
            toXElementMethod.Statements.AddRange(GetToXElementCodeStatements(type));
            g.newClass.Members.Add(toXElementMethod);

            //Parse方法
            CodeMemberMethod parseMethod = new CodeMemberMethod();

            parseMethod.Name       = "Read";
            parseMethod.Attributes = MemberAttributes.Public;
            parseMethod.ReturnType = new CodeTypeReference(type);
            parseMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(XmlElement)), "xElement"));
            parseMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "name"));
            //函数体
            parseMethod.Statements.AddRange(GetParseXElementCodeStatements(type));
            g.newClass.Members.Add(parseMethod);


            //生成代码
            g.GenCSharp(SerializerGenerPath);
        }
Exemplo n.º 8
0
        public void GenBinder(List <Type> targetTypeList)
        {
            CodeGener gener       = new CodeGener("UniToLua", "LuaBinder");
            Hashtable GlobalTable = CreateGlobalTable(targetTypeList);

            List <CodeStatement> bindStatements = new List <CodeStatement>();

            bindStatements.Add(new CodeSnippetStatement("\t\t\tL.BeginModule(null);"));
            GenBindWithTable(bindStatements, GlobalTable);
            bindStatements.Add(new CodeSnippetStatement("\t\t\tL.EndModule();"));

            gener.AddMemberMethod(typeof(void), "Bind",
                                  new Dictionary <string, Type>()
            {
                { "L", typeof(LuaState) }
            },
                                  MemberAttributes.Public | MemberAttributes.Static, bindStatements.ToArray());

            gener.GenCSharp(outputPath);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 生成Bundle列表类
        /// </summary>
        private void GenBundleList()
        {
            CodeGener gener = new CodeGener("ResetCore.NAsset", "Bundles");

            foreach (var kvp in assetBundleDict)
            {
                string name = kvp.Key;
                if (name == "none")
                {
                    continue;
                }
                string defName = name.Replace(" ", "_").Replace("/", "_");
                gener.AddMemberField(typeof(string), defName, (field) =>
                {
                    CodeVariableReferenceExpression fieldExpression = new CodeVariableReferenceExpression("\"" + name + "\"");
                    field.InitExpression = fieldExpression;
                }, MemberAttributes.Static | MemberAttributes.Public);
            }
            gener.GenCSharp(Path.Combine(Application.dataPath, "AssetBundle/Loader/temp"));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 生成资源列表类
        /// </summary>
        private void GenRList()
        {
            CodeGener gener = new CodeGener("ResetCore.NAsset", "R");

            foreach (var kvp in assetBundleDict)
            {
                if (kvp.Key == "none")
                {
                    continue;
                }
                foreach (var path in kvp.Value)
                {
                    string name    = kvp.Key + "_" + FileEx.GetFileNameWithoutExtention(path);
                    string defName = name.Replace(" ", "_").Replace("/", "_");
                    string content = kvp.Key + "###" + FileEx.GetFileNameWithoutExtention(path);
                    gener.AddMemberField(typeof(string), defName, (field) =>
                    {
                        CodeVariableReferenceExpression fieldExpression = new CodeVariableReferenceExpression("\"" + content + "\"");
                        field.InitExpression = fieldExpression;
                    }, MemberAttributes.Static | MemberAttributes.Public);
                }
            }
            gener.GenCSharp(Path.Combine(Application.dataPath, "AssetBundle/Loader/temp"));
        }
Exemplo n.º 11
0
        private void GenClass(Type classType)
        {
            if (classType == null)
            {
                return;
            }
            var       className = GetClassFileName(classType);
            CodeGener gener     = new CodeGener("UniToLua", className);

            var baseClassName = classType.BaseType == typeof(System.Object) || classType.BaseType == null
                ? "null"
                : classType.BaseType.FullName;
            var fields       = classType.GetFields();
            var propertys    = classType.GetProperties();
            var methodGroups = classType.GetMethods().Where((method) =>
            {
                if (propertys.Count(prop => prop.GetMethod == method || prop.SetMethod == method) != 0)
                {
                    return(false);
                }
                if (IsObsolete(method))
                {
                    return(false);
                }
                return(true);
            }).GroupBy(mi => mi.Name).ToArray();

            List <CodeStatement> registerMethodStatement = new List <CodeStatement>();

            registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.BeginClass(typeof({classType.FullName}), {baseClassName});"));

            registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.RegFunction(\"New\", _Create{classType.Name});"));

            foreach (var fieldInfo in fields)
            {
                registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.RegVar(\"{fieldInfo.Name}\", get_{fieldInfo.Name}, set_{fieldInfo.Name});"));
            }

            foreach (var propertyInfo in propertys)
            {
                StringBuilder builder = new StringBuilder($"\t\t\tL.RegVar(\"{propertyInfo.Name}\", ");
                if (propertyInfo.GetMethod != null && propertyInfo.GetMethod.IsPublic)
                {
                    builder.Append($"get_{propertyInfo.Name}, ");
                }
                else
                {
                    builder.Append("null, ");
                }
                if (propertyInfo.SetMethod != null && propertyInfo.SetMethod.IsPublic)
                {
                    builder.Append($"set_{propertyInfo.Name}");
                }
                else
                {
                    builder.Append("null");
                }
                builder.Append(");");
                registerMethodStatement.Add(new CodeSnippetStatement(builder.ToString()));
            }

            foreach (var methodGroup in methodGroups)
            {
                registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.RegFunction(\"{methodGroup.Key}\", {methodGroup.Key});"));
            }

            registerMethodStatement.Add(new CodeSnippetStatement("\t\t\tL.EndClass();"));

            gener.AddMemberMethod(typeof(void), "Register", new Dictionary <string, Type>()
            {
                { "L", typeof(ILuaState) }
            },
                                  MemberAttributes.Public | MemberAttributes.Static, registerMethodStatement.ToArray());

            GenConstructor(gener, classType);

            foreach (var fieldInfo in fields)
            {
                if (fieldInfo.IsStatic)
                {
                    GenRegStaticField(gener, classType, fieldInfo);
                }
                else
                {
                    GenRegMemberField(gener, classType, fieldInfo);
                }
            }

            foreach (var propertyInfo in propertys)
            {
                if ((propertyInfo.GetMethod != null && propertyInfo.GetMethod.IsStatic) ||
                    (propertyInfo.SetMethod != null && propertyInfo.SetMethod.IsStatic))
                {
                    GenRegStaticProperty(gener, classType, propertyInfo);
                }
                else
                {
                    GenRegMemberProperty(gener, classType, propertyInfo);
                }
            }

            foreach (var methodGroup in methodGroups)
            {
                if (!methodGroup.Any())
                {
                    continue;
                }
                if (methodGroup.First().IsStatic)
                {
                    GenRegStaticFunction(gener, classType, methodGroup.ToArray());
                }
                else
                {
                    GenRegMemberFunction(gener, classType, methodGroup.ToArray());
                }
            }

            gener.GenCSharp(outputPath);
        }
Exemplo n.º 12
0
        private void GenStaticLib(Type libType)
        {
            if (libType == null)
            {
                return;
            }
            var       className = GetClassFileName(libType);
            CodeGener gener     = new CodeGener("UniToLua", className);

            var fields       = libType.GetFields(BindingFlags.Public | BindingFlags.Static);
            var propertys    = libType.GetProperties(BindingFlags.Public | BindingFlags.Static);
            var methodGroups = libType.GetMethods(BindingFlags.Public | BindingFlags.Static).Where((method) =>
            {
                if (propertys.Count(prop => prop.GetMethod == method || prop.SetMethod == method) != 0)
                {
                    return(false);
                }
                if (IsObsolete(method))
                {
                    return(false);
                }
                return(true);
            }).GroupBy(mi => mi.Name).ToArray();

            List <CodeStatement> registerMethodStatement = new List <CodeStatement>();

            registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.BeginStaticLib(\"{libType.Name}\");"));

            foreach (var fieldInfo in fields)
            {
                registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.RegVar(\"{fieldInfo.Name}\", get_{fieldInfo.Name}, set_{fieldInfo.Name});"));
            }

            foreach (var propertyInfo in propertys)
            {
                StringBuilder builder = new StringBuilder($"\t\t\tL.RegVar(\"{propertyInfo.Name}\", ");
                if (propertyInfo.GetMethod != null && propertyInfo.GetMethod.IsPublic)
                {
                    builder.Append($"get_{propertyInfo.Name}, ");
                }
                else
                {
                    builder.Append("null, ");
                }
                if (propertyInfo.SetMethod != null && propertyInfo.SetMethod.IsPublic)
                {
                    builder.Append($"set_{propertyInfo.Name}");
                }
                else
                {
                    builder.Append("null");
                }
                builder.Append(");");
                registerMethodStatement.Add(new CodeSnippetStatement(builder.ToString()));
            }

            foreach (var methodGroup in methodGroups)
            {
                registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.RegFunction(\"{methodGroup.Key}\", {methodGroup.Key});"));
            }

            registerMethodStatement.Add(new CodeSnippetStatement($"\t\t\tL.EndStaticLib();"));

            gener.AddMemberMethod(typeof(void), "Register", new Dictionary <string, Type>()
            {
                { "L", typeof(ILuaState) }
            },
                                  MemberAttributes.Public | MemberAttributes.Static, registerMethodStatement.ToArray());

            foreach (var fieldInfo in fields)
            {
                GenRegStaticField(gener, libType, fieldInfo);
            }

            foreach (var propertyInfo in propertys)
            {
                GenRegStaticProperty(gener, libType, propertyInfo);
            }

            foreach (var methodGroup in methodGroups)
            {
                GenRegStaticFunction(gener, libType, methodGroup.ToArray());
            }

            gener.GenCSharp(outputPath);
        }
Exemplo n.º 13
0
        //生成数据代码
        public void GenModelScript(BaseUI ui)
        {
            rootGo = ui.gameObject;
            uiType = ui.GetType();
            string modelClassName = uiType.Name + "Model";

            if (File.Exists(PathEx.Combine(Application.dataPath, UIModel.uiModelScriptPath, modelClassName + ".cs")))
            {
                if (!EditorUtility.DisplayDialog("提示", "已存在" + modelClassName + ",是否要覆盖?", "继续", "取消"))
                {
                    return;
                }
            }

            CodeGener gener = new CodeGener(UIModel.uiModelNameSpace, modelClassName);

            gener.AddBaseType(typeof(UIModel).Name)
            .AddImport("ResetCore.UGUI")
            .AddImport("ResetCore.Event");

            List <string> hasAddedProperty = new List <string>();

            CodeConstructor constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;

            rootGo.transform.DoToSelfAndAllChildren((tran) =>
            {
                GameObject go = tran.gameObject;
                if (!go.name.StartsWith(UIModel.genableSign))
                {
                    return;
                }

                string goName = go.name.Replace(UIModel.genableSign, string.Empty);
                string propertyName;
                string typeName;

                if (!goName.Contains(splitGoNameAndProperty[0]))
                {
                    return;
                }

                string[] tempstrs = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries);
                goName            = tempstrs[0];

                if (!tempstrs[1].Contains(splitComponentAndProperty[0]))
                {
                    EditorUtility.WarnPrefab(go, "命名错误", go.name + "的格式不正确,标准格式为“g-View变量名@组件名->Model属性名:变量类型”", "好的");
                    return;
                }

                propertyName = tempstrs[1].Split(splitComponentAndProperty, StringSplitOptions.RemoveEmptyEntries)[1];

                if (!propertyName.Contains(splitPropertyAndType.ToString()))
                {
                    typeName = "EventProperty<string>";
                }

                tempstrs     = propertyName.Split(splitPropertyAndType, StringSplitOptions.RemoveEmptyEntries);
                propertyName = tempstrs[0];
                typeName     = "EventProperty<" + tempstrs[1] + ">";

                if (!hasAddedProperty.Contains(propertyName))
                {
                    gener.AddMemberProperty(typeName, propertyName);
                    hasAddedProperty.Add(propertyName);

                    constructor.Statements.AddRange(new CodeStatement[]
                    {
                        new CodeCommentStatement("From " + go.name),
                        new CodeSnippetStatement(propertyName + " = new " + typeName + "();")
                    });
                }
            });
            gener.newClass.Members.Add(constructor);
            gener.GenCSharp(PathEx.Combine(Application.dataPath, UIModel.uiModelScriptPath));
        }
Exemplo n.º 14
0
        //生成自动绑定代码
        public void GenBindScript(BaseUI ui)
        {
            rootGo = ui.gameObject;
            uiType = ui.GetType();
            string binderClassName = uiType.Name + "Binder";

            if (File.Exists(PathEx.Combine(Application.dataPath, UIBinder.uiBinderScriptPath, binderClassName + ".cs")))
            {
                if (!EditorUtility.DisplayDialog("提示", "已存在" + binderClassName + ",是否要覆盖?", "继续", "取消"))
                {
                    return;
                }
            }

            CodeGener gener = new CodeGener(UIBinder.uiBinderNameSpace, binderClassName);

            gener.AddBaseType(typeof(UIBinder).Name)
            .AddImport("ResetCore.UGUI")
            .AddImport("ResetCore.Event")
            .AddImport(UIView.uiViewNameSpace)
            .AddImport(UIModel.uiModelNameSpace);


            CodeMemberMethod initMethod = new CodeMemberMethod();

            initMethod.AddComment("Used to bind view and model", true);
            initMethod.Name = "Bind";
            string viewClassName  = uiType.Name + "View";
            string modelClassName = uiType.Name + "Model";

            initMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] {
                new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(UIView).Name), "view"),
                new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(UIModel).Name), "model"),
            });
            initMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override;

            List <string> hasAddedProperty = new List <string>();

            rootGo.transform.DoToSelfAndAllChildren((tran) =>
            {
                GameObject go = tran.gameObject;
                if (!go.name.StartsWith(UIBinder.genableSign))
                {
                    return;
                }

                string goName = go.name.Replace(UIBinder.genableSign, string.Empty);
                string comName;
                string propertyName;
                string typeName;

                if (!goName.Contains(splitGoNameAndProperty[0]))
                {
                    return;
                }

                string[] tempstrs = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries);
                goName            = tempstrs[0];

                if (!tempstrs[1].Contains(splitComponentAndProperty[0]))
                {
                    EditorUtility.WarnPrefab(go, "命名错误", go.name + "的格式不正确,标准格式为“g-View变量名@组件名->Model属性名:变量类型”", "好的");
                    return;
                }

                tempstrs     = tempstrs[1].Split(splitComponentAndProperty, StringSplitOptions.RemoveEmptyEntries);
                comName      = tempstrs[0];
                propertyName = tempstrs[1];

                if (!propertyName.Contains(splitPropertyAndType.ToString()))
                {
                    typeName = "EventProperty<string>";
                }

                tempstrs     = propertyName.Split(splitPropertyAndType, StringSplitOptions.RemoveEmptyEntries);
                propertyName = tempstrs[0];
                typeName     = "EventProperty<" + tempstrs[1] + ">";

                initMethod.Statements.AddRange(new CodeStatement[] {
                    new CodeCommentStatement("From " + go.name),
                    new CodeSnippetStatement("((" + viewClassName + ")view)." +
                                             comName + goName + ".Bind(((" + modelClassName + ")model)." + propertyName + ");"),
                });
            });

            gener.newClass.Members.Add(initMethod);

            gener.GenCSharp(PathEx.Combine(Application.dataPath, UIBinder.uiBinderScriptPath));
        }