Example #1
0
    public void TestGenerateDelegateEvents()
    {
        var gen         = new CodeUnitGenerator("TestCodeGen");
        var classGen    = new ClassGenerator("TestClass");
        var delegateGen = new DelegateGenerator("MyEventHandler")
                          .AddParameter("TestClass", "myRef")
                          .AddReturnType(typeof(bool));

        var eventGen = new EventGenerator("OnSomeTrigger", delegateGen.delegateType);

        classGen.AddEvent(eventGen);
        var fireEventMethod = new MethodGenerator("FireEvent")
                              .AddStatement(new StatementBuilder()
                                            //.AddSnippetExpression("Debug.Log();DoMoreStuff();"));
                                            .InvokeEvent(eventGen, new ParamBuilder()
                                                         .AddPrimitiveExpression("new TestClass()")));

        classGen.AddMethod(fireEventMethod);

        gen.AddType(delegateGen);
        gen.AddType(classGen);

        var classSubscriber = new ClassGenerator("MySubscribeClass");
        var field           = new FieldGenerator("TestClass", "eventSource");

        classSubscriber.AddField(field);

        var constructor = new ConstructorGenerator(classSubscriber.classType);

        classSubscriber.AddMethod(constructor);

        var eventHandler = new MethodGenerator("OnSomeTrigger", delegateGen)
                           .AddStatement(new StatementBuilder()
                                         .AddSnippet("Debug.Log(\"Expression1\");")
                                         .AddSnippet("Debug.Log(\"Expression2\");"));

        var subscribeMethod = new MethodGenerator("AddListener")
                              .AddStatement(new StatementBuilder()
                                            .AttachEvent(eventHandler, new FieldTarget(field), eventGen));

        classSubscriber.AddMethod(
            new MethodGenerator("Unsubscribe").AddStatement(
                new StatementBuilder()
                .DetachEvent(eventHandler, new FieldTarget(field), eventGen)));
        classSubscriber.AddMethod(eventHandler);
        classSubscriber.AddMethod(subscribeMethod);
        gen.AddType(classSubscriber);

        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        //Debug.Log(output);
        Assert.IsTrue(output.Contains("OnSomeTrigger"));
        Assert.IsTrue(output.Contains("FireEvent"));
        Assert.IsTrue(output.Contains("+="));
        Assert.IsTrue(output.Contains("-="));
        Assert.IsTrue(output.Contains("delegate"));
        Assert.IsTrue(output.Contains("event"));
    }
Example #2
0
        public static void GenerateCode1()
        {
            MethodGenerator method = new MethodGenerator("Test");

            method.SetAuthority(MethodAuthorityType.Internal);
            method.SetDecorate(MethodDecorateType.Virtual);
            method.SetParms(new string[] { "int", "string" });
            method.SetReturn("bool");
            MethodGenerator method1 = new MethodGenerator("Test1");

            method1.SetAuthority(MethodAuthorityType.Internal);
            method1.SetDecorate(MethodDecorateType.Virtual);
            method1.SetParms(new string[] { "int", "string" });
            method1.SetReturn("bool");
            ClassGenerator textClass = new ClassGenerator("TestClass");

            textClass.SetUsingName(new string[] { "xxxx", "aaaa" });
            textClass.SetBaseClass("xxcvsdf");
            textClass.SetDecorate(ClassDecorateType.Abstract);
            textClass.SetAuthority(AuthorityType.Public);
            textClass.SetInterfaces(new string[] { "asdfsadf", "asdfasdf" });
            textClass.SetNamespace("masdjf");
            textClass.AddMethod(method);
            textClass.AddMethod(method1);
            string classValue = textClass.ToString();

            TxtUtility.StringToFile(classValue);
        }
Example #3
0
    public void TestGenerateForLoop()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass")
                       .SetIsSealed(true);
        var method = new MethodGenerator("IterateStuff");

        method.AddStatement(new StatementBuilder()
                            .AddVariable(typeof(string[]), "myStuff", true)
                            .AddVariable(typeof(bool), "myBool", true)
                            .AddVariable(typeof(int), "myInt", 1)
                            .AddSnippet("//Snippet")
                            .AddForLoop("i", 10, new StatementBuilder()
                                        .AddSnippet("Debug.Log()")
                                        .AddAssignement(new VariableTarget("myInt"), new VariableTarget("i"))
                                        .InvokeMethod(new VariableTarget("myStuff"),
                                                      new ClassTarget("UnityEngine"),
                                                      "GetAllStuff",
                                                      new ParamBuilder()
                                                      .AddVariable("myInt"))));
        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
        Assert.IsTrue(output.Contains("for (i = 0"));
        Assert.IsTrue(output.Contains("(i < 10"));
        Assert.IsTrue(output.Contains("myStuff = UnityEngine.GetAllStuff("));
    }
Example #4
0
    public void TestGenerateClassImplementation()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass")
                       .SetIsPartial(true)
                       .SetIsAbstract(true)
                       .AddBaseType("IComponent");
        var field       = new FieldGenerator(typeof(int), "MyField");
        var property    = new AutoPropertyGenerator("TestClass", "MyProp");
        var constructor = new ConstructorGenerator()
                          .AddBaseCall("BaseArg")
                          .AddParameter(field.FieldType, field.Name)
                          .AddParameter(property.Name, property.PropertyType)
                          .AddStatement(new StatementBuilder()
                                        .AddConstructorFieldAssignement(field.Name, field.Name)
                                        .AddConstructorPropertyAssignement(property.Name, property.Name));

        classGen.AddAutoProperty(property);
        classGen.AddField(field);
        classGen.AddMethod(constructor);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
        Assert.IsTrue(output.Contains("base("));
        Assert.IsTrue(output.Contains("BaseArg"));
    }
Example #5
0
    public void TestGenerateClassWithMethod()
    {
        var gen = new CodeUnitGenerator("TestCodeGen");

        gen.NamespaceImports.Add("System");
        ClassGenerator classGen = new ClassGenerator("MyCodeDOMTestClass")
                                  .SetIsSealed(true)
                                  .SetIsSealed(false);
        MethodGenerator method = new MethodGenerator("DoStuff");

        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Assert.IsFalse(output.Contains("sealed"));
        Assert.IsTrue(output.Contains("DoStuff"));
    }
Example #6
0
    public void TestWhileLoop()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass")
                       .SetIsSealed(true);
        var method = new MethodGenerator("IterateStuff");

        method.AddStatement(new StatementBuilder()
                            .AddVariable(typeof(string[]), "myStuff", true)
                            .AddVariable(typeof(bool), "myBool", true)
                            .AddWhile(new CodeSnippetExpression("iterator.MoveNext(myBool)"), new StatementBuilder()
                                      .AddSnippet("Debug.Log(myBool)")));

        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
    }
Example #7
0
    public void TestGenerateClassWithFieldPropertyMethod()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass");
        var field    = new FieldGenerator(typeof(int), "MyField");
        var property = new AutoPropertyGenerator("TestClass", "MyProp");
        var method   = new MethodReturnField(field);

        method.AddStatement("Debug.Log(\"This is a string\"");
        classGen.AddAutoProperty(property);
        classGen.AddField(field);
        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        //Debug.Log(output);
        Assert.IsTrue(output.Contains("MyProp"));
        Assert.IsTrue(output.Contains("MyField"));
        Assert.IsTrue(output.Contains("GetMyField"));
    }
Example #8
0
        public static void ReadCodeGenerator(List<FiledPropObject> list, string realName, string pre, string TemplateVOType)
        {
            string filename = EditorConfig.Instance.CodeGenarateReaderPath + pre + "/" + realName;
            ClassGenerator generator = new ClassGenerator("Game.Template", realName);
            generator.AddBaseType("ITemplateReader");
            //generator.AddImport("System.Collections.Generic");
            List<CodeStatement> CodeStatementList = new List<CodeStatement>();

            CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression(typeof(byte[]), "bytes");
            List<CodeParameterDeclarationExpression> pList = new List<CodeParameterDeclarationExpression>();
            pList.Add(par);

            CodeStatementList.Add(
                new CodeVariableDeclarationStatement(typeof(ByteArray), "data",
                    new CodeObjectCreateExpression(typeof(ByteArray),
                        new CodeVariableReferenceExpression("bytes"))));

            CodeStatementList.Add(
                new CodeVariableDeclarationStatement(typeof(List<object>), "list",
                    new CodeObjectCreateExpression(typeof(List<object>))));

            //length
            CodeStatementList.Add(
                new CodeVariableDeclarationStatement(typeof(int), "length",
                    new CodeVariableReferenceExpression("data.ReadInt()")));

            //for
            CodeIterationStatement forExp = new CodeIterationStatement();
            // 初始化  
            forExp.InitStatement = new CodeVariableDeclarationStatement(typeof(int), "i",
                new CodePrimitiveExpression(0));
            // 递增条件  
            forExp.IncrementStatement = new CodeAssignStatement(
                new CodeVariableReferenceExpression("i"),
                new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression("i"),
                    CodeBinaryOperatorType.Add,
                    new CodePrimitiveExpression(1)));
            // 测试表达式  
            forExp.TestExpression = new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("i"),
                CodeBinaryOperatorType.LessThan,
                new CodeVariableReferenceExpression("length"));
            forExp.Statements.Add(
                     new CodeVariableDeclarationStatement(TemplateVOType, "vo",
                         new CodeObjectCreateExpression(TemplateVOType)));

            for (int i = 0; i < list.Count; i++)
            {
                forExp.Statements.Add(
                    new CodeAssignStatement(
                        new CodeVariableReferenceExpression(
                            "vo." + list[i].name),
                            new CodeMethodInvokeExpression(
                                new CodeTypeReferenceExpression("data"),
                                GetByteArrayMethodName(list[i].type, pre))));
            }

            forExp.Statements.Add(new CodeVariableReferenceExpression("list.Add(vo)"));


            CodeStatementList.Add(forExp);

            //return
            CodeStatementList.Add(
                new CodeMethodReturnStatement(
                    new CodeVariableReferenceExpression("list")));

            generator.AddMethod("GenerateByteArray", pList, CodeStatementList, typeof(List<object>), MemberAttributes.Public | MemberAttributes.Final);
            generator.Generate(filename + ".cs");
        }
Example #9
0
        public static void DictionaryGenerator(string realName, Type type, Type subType, Type dataType)
        {
            string filename = EditorConfig.Instance.CodeGenarateDataDicPath + realName;
            ClassGenerator generator = new ClassGenerator("Game.Template", realName + "Dictionary");
            generator.AddBaseType("ITemplateDictionary");
            generator.AddImport("UnityEngine");
            //generator.AddImport("System.Collections.Generic");

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

            generator.AddProperty("Item List", type, null);
            generator.AddProperty("Item Dic", dataType, null);

            CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression(typeof(List<object>), "list");
            List<CodeParameterDeclarationExpression> pList = new List<CodeParameterDeclarationExpression>();
            pList.Add(par);
            CodeStatementList.Add(new CodeAssignStatement(
                new CodeVariableReferenceExpression("_itemlist"),
                new CodeObjectCreateExpression(type)));

            CodeStatementList.Add(
                new CodeVariableDeclarationStatement(typeof(int), "length",
                    new CodeVariableReferenceExpression("list.Count")));

            //for
            CodeIterationStatement forExp = new CodeIterationStatement();
            // 初始化  
            forExp.InitStatement = new CodeVariableDeclarationStatement(typeof(int), "i",
                new CodePrimitiveExpression(0));
            // 递增条件  
            forExp.IncrementStatement = new CodeAssignStatement(
                new CodeVariableReferenceExpression("i"),
                new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression("i"),
                    CodeBinaryOperatorType.Add,
                    new CodePrimitiveExpression(1)));
            // 测试表达式  
            forExp.TestExpression = new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("i"),
                CodeBinaryOperatorType.LessThan,
                new CodeVariableReferenceExpression("length"));
            forExp.Statements.Add(
                     new CodeExpressionStatement(
                        new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression("_itemlist"), "Add",
                            new CodeVariableReferenceExpression("list[i] as " + subType))));

            CodeStatementList.Add(forExp);

            CodeStatementList.Add(
                new CodeExpressionStatement(
                        new CodeMethodInvokeExpression(
                            new CodeThisReferenceExpression(), "InitDictionary")));

            generator.AddMethod("Init", pList, CodeStatementList, null, MemberAttributes.Public | MemberAttributes.Final);

            CodeStatementList.Clear();

            //for
            forExp = new CodeIterationStatement();
            // 初始化  
            forExp.InitStatement = new CodeVariableDeclarationStatement(typeof(int), "i",
                new CodePrimitiveExpression(0));
            // 递增条件  
            forExp.IncrementStatement = new CodeAssignStatement(
                new CodeVariableReferenceExpression("i"),
                new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression("i"),
                    CodeBinaryOperatorType.Add,
                    new CodePrimitiveExpression(1)));
            // 测试表达式  
            forExp.TestExpression = new CodeBinaryOperatorExpression(
                new CodeVariableReferenceExpression("i"),
                CodeBinaryOperatorType.LessThan,
                new CodeVariableReferenceExpression("length"));

            CodeTryCatchFinallyStatement myTrycafly = new CodeTryCatchFinallyStatement();
            // try  
            myTrycafly.TryStatements.Add(new CodeExpressionStatement(
                        new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression("_itemdic"), "Add",
                            new CodeVariableReferenceExpression("_itemlist[i].id"),
                            new CodeVariableReferenceExpression("_itemlist[i]"))));
            // catch  
            myTrycafly.CatchClauses.Add(new CodeCatchClause(
                "ex", new CodeTypeReference(typeof(Exception)),
                new CodeExpressionStatement(
                    new CodeMethodInvokeExpression(
                        new CodeTypeReferenceExpression("Debug"),
                        "LogWarning",
                        new CodeVariableReferenceExpression("ex.Message")))));
            forExp.Statements.Add(myTrycafly);

            CodeStatementList.Add(
                new CodeVariableDeclarationStatement(typeof(int), "length",
                    new CodeVariableReferenceExpression("_itemlist.Count")));

            par = new CodeParameterDeclarationExpression(dataType, "_itemdic");
            pList = new List<CodeParameterDeclarationExpression>();
            pList.Add(par);
            CodeStatementList.Add(new CodeAssignStatement(
                new CodeVariableReferenceExpression("_itemdic"),
                new CodeObjectCreateExpression(dataType)));

            CodeStatementList.Add(forExp);

            generator.AddMethod("InitDictionary", null, CodeStatementList, null, MemberAttributes.Private);
            generator.Generate(filename + "Dictionary.cs");
            Debug.Log("Code generator completed : " + realName + "Dictionary.cs");
        }