Beispiel #1
0
        public void Where_FieldRelationalOperatorField()
        {
            const string expected = "Where F1 = F2";

            var fieldGenerator = new FieldGenerator();
            var field          = fieldGenerator.Field("F2");

            Generator.Where("F1", RelationalOperators.Equal, field);

            Assert.AreEqual(expected, Generator.ToString());
        }
Beispiel #2
0
        protected override TypeGenerator OnGenerateType(ref string output, NamespaceGenerator @namespace)
        {
            var @struct = StructGenerator.Struct(RootAccessModifier.Public, StructModifier.None, Data.title.LegalMemberName());

            if (Data.definedEvent)
            {
                @struct.ImplementInterface(typeof(IDefinedEvent));
            }
            if (Data.inspectable)
            {
                @struct.AddAttribute(AttributeGenerator.Attribute <InspectableAttribute>());
            }
            if (Data.serialized)
            {
                @struct.AddAttribute(AttributeGenerator.Attribute <SerializableAttribute>());
            }
            if (Data.includeInSettings)
            {
                @struct.AddAttribute(AttributeGenerator.Attribute <IncludeInSettingsAttribute>().AddParameter(true));
            }

            for (int i = 0; i < Data.variables.Count; i++)
            {
                if (!string.IsNullOrEmpty(Data.variables[i].name) && Data.variables[i].type != null)
                {
                    var field = FieldGenerator.Field(AccessModifier.Public, FieldModifier.None, Data.variables[i].type, Data.variables[i].name);
                    if (Data.serialized)
                    {
                        if (Data.variables[i].inspectable)
                        {
                            field.AddAttribute(AttributeGenerator.Attribute <InspectableAttribute>());
                        }
                        if (!Data.variables[i].serialized)
                        {
                            field.AddAttribute(AttributeGenerator.Attribute <NonSerializedAttribute>());
                        }
                    }
                    @struct.AddField(field);
                }
            }

            if (@namespace != null)
            {
                @namespace.AddStruct(@struct);
            }

            return(@struct);
        }
        protected override TypeGenerator OnGenerateType(ref string output, NamespaceGenerator @namespace)
        {
            var @class = ClassGenerator.Class(RootAccessModifier.Public, ClassModifier.None, Data.title.LegalMemberName(), Data.scriptableObject ? typeof(ScriptableObject) : typeof(object));

            if (Data.definedEvent)
            {
                @class.ImplementInterface(typeof(IDefinedEvent));
            }
            if (Data.inspectable)
            {
                @class.AddAttribute(AttributeGenerator.Attribute <InspectableAttribute>());
            }
            if (Data.serialized)
            {
                @class.AddAttribute(AttributeGenerator.Attribute <SerializableAttribute>());
            }
            if (Data.includeInSettings)
            {
                @class.AddAttribute(AttributeGenerator.Attribute <IncludeInSettingsAttribute>().AddParameter(true));
            }
            if (Data.scriptableObject)
            {
                @class.AddAttribute(AttributeGenerator.Attribute <CreateAssetMenuAttribute>().AddParameter("menuName", Data.menuName).AddParameter("fileName", Data.fileName).AddParameter("order", Data.order));
            }

            for (int i = 0; i < Data.constructors.Count; i++)
            {
                var constructor = ConstructorGenerator.Constructor(Data.constructors[i].scope, Data.constructors[i].modifier, Data.title.LegalMemberName());
                if (Data.constructors[i].graph.units.Count > 0)
                {
                    var unit = Data.constructors[i].graph.units[0] as FunctionNode;
                    constructor.Body(FunctionNodeGenerator.GetSingleDecorator(unit, unit).GenerateControl(null, new ControlGenerationData(), 0));

                    for (int pIndex = 0; pIndex < Data.constructors[i].parameters.Count; pIndex++)
                    {
                        if (!string.IsNullOrEmpty(Data.constructors[i].parameters[pIndex].name))
                        {
                            constructor.AddParameter(false, ParameterGenerator.Parameter(Data.constructors[i].parameters[pIndex].name, Data.constructors[i].parameters[pIndex].type, ParameterModifier.None));
                        }
                    }
                }

                @class.AddConstructor(constructor);
            }

            for (int i = 0; i < Data.variables.Count; i++)
            {
                if (!string.IsNullOrEmpty(Data.variables[i].name) && Data.variables[i].type != null)
                {
                    var attributes = Data.variables[i].attributes;

                    if (Data.variables[i].isProperty)
                    {
                        var property = PropertyGenerator.Property(Data.variables[i].scope, Data.variables[i].propertyModifier, Data.variables[i].type, Data.variables[i].name, false);

                        for (int attrIndex = 0; attrIndex < attributes.Count; attrIndex++)
                        {
                            AttributeGenerator attrGenerator = AttributeGenerator.Attribute(attributes[attrIndex].GetAttributeType());
                            property.AddAttribute(attrGenerator);
                        }

                        if (Data.variables[i].get)
                        {
                            property.MultiStatementGetter(AccessModifier.Public, NodeGenerator.GetSingleDecorator(Data.variables[i].getter.graph.units[0] as Unit, Data.variables[i].getter.graph.units[0] as Unit)
                                                          .GenerateControl(null, new ControlGenerationData()
                            {
                                returns = Data.variables[i].type
                            }, 0));
                        }

                        if (Data.variables[i].set)
                        {
                            property.MultiStatementSetter(AccessModifier.Public, NodeGenerator.GetSingleDecorator(Data.variables[i].setter.graph.units[0] as Unit, Data.variables[i].setter.graph.units[0] as Unit)
                                                          .GenerateControl(null, new ControlGenerationData(), 0));
                        }

                        @class.AddProperty(property);
                    }
                    else
                    {
                        var field = FieldGenerator.Field(Data.variables[i].scope, Data.variables[i].fieldModifier, Data.variables[i].type, Data.variables[i].name);


                        for (int attrIndex = 0; attrIndex < attributes.Count; attrIndex++)
                        {
                            AttributeGenerator attrGenerator = AttributeGenerator.Attribute(attributes[attrIndex].GetAttributeType());
                            field.AddAttribute(attrGenerator);
                        }

                        @class.AddField(field);
                    }
                }
            }

            for (int i = 0; i < Data.methods.Count; i++)
            {
                if (!string.IsNullOrEmpty(Data.methods[i].name) && Data.methods[i].returnType != null)
                {
                    var method = MethodGenerator.Method(Data.methods[i].scope, Data.methods[i].modifier, Data.methods[i].returnType, Data.methods[i].name);
                    if (Data.methods[i].graph.units.Count > 0)
                    {
                        var unit = Data.methods[i].graph.units[0] as FunctionNode;
                        method.Body(FunctionNodeGenerator.GetSingleDecorator(unit, unit).GenerateControl(null, new ControlGenerationData(), 0));

                        for (int pIndex = 0; pIndex < Data.methods[i].parameters.Count; pIndex++)
                        {
                            if (!string.IsNullOrEmpty(Data.methods[i].parameters[pIndex].name))
                            {
                                method.AddParameter(ParameterGenerator.Parameter(Data.methods[i].parameters[pIndex].name, Data.methods[i].parameters[pIndex].type, ParameterModifier.None));
                            }
                        }
                    }

                    @class.AddMethod(method);
                }
            }

            @namespace.AddClass(@class);

            return(@class);
        }
Beispiel #4
0
        public override string Generate(int indent)
        {
            NamespaceGenerator @namespace = NamespaceGenerator.Namespace("Unity.VisualScripting.Community.Generated");

            if (Data != null)
            {
                var title        = GetCompoundTitle();
                var delegateType = DelegateType;

                Data.title = title;

                if (!string.IsNullOrEmpty(Data.category))
                {
                    @namespace = NamespaceGenerator.Namespace(Data.category);
                }

                var @class          = ClassGenerator.Class(RootAccessModifier.Public, ClassModifier.None, title, typeof(object)).ImplementInterface(delegateType);
                var properties      = string.Empty;
                var method          = Data.type.type.GetMethod("Invoke");
                var parameterUsings = new List <string>();
                var parameters      = method.GetParameters();
                var parameterNames  = new List <string>();
                var something       = new Dictionary <ParameterInfo, string>();

                for (int i = 0; i < parameters.Length; i++)
                {
                    properties += " new".ConstructHighlight() + " TypeParam".TypeHighlight() + "() { name = " + $@"""{parameters[i].Name}""".StringHighlight() + ", type = " + "typeof".ConstructHighlight() + "(" + (parameters[i].ParameterType.IsGenericParameter ? Data.generics[i].type.type.As().CSharpName() : parameters[i].ParameterType.As().CSharpName()) + ") }";
                    if (!parameterUsings.Contains(parameters[i].ParameterType.Namespace))
                    {
                        parameterUsings.Add(parameters[i].ParameterType.Namespace);
                    }
                    if (i < parameters.Length - 1)
                    {
                        properties += ", ";
                    }
                }

                @class.AddUsings(parameterUsings);

                if (string.IsNullOrEmpty(properties))
                {
                    properties += " ";
                }
                else
                {
                    properties = " " + properties + " ";
                }

                var displayName                 = string.IsNullOrEmpty(Data.displayName) ? Data.type.type.As().CSharpName().RemoveHighlights().RemoveMarkdown() : Data.displayName;
                var constructors                = Data.type.type.GetConstructors();
                var constructorParameters       = constructors[constructors.Length - 1 > 0 ? 1 : 0];
                var stringConstructorParameters = new List <string>();
                var constParams                 = parameters.ToListPooled();
                var assignParams                = string.Empty;

                if (constParams.Count <= 0)
                {
                }

                var constParamsLength = constParams.Count;
                var invokeString      = string.Empty;

                for (int i = constParamsLength - 1; i >= 0; i--)
                {
                    if (constParams[i].Name == "object" || constParams[i].Name == "method")
                    {
                        constParams.Remove(constParams[i]);
                    }
                }


                for (int i = 0; i < constParams.Count; i++)
                {
                    assignParams += constParams[i].Name.EnsureNonConstructName().Replace("&", string.Empty);
                    stringConstructorParameters.Add(constParams[i].Name.EnsureNonConstructName().Replace("&", string.Empty));
                    if (i < constParams.Count - 1)
                    {
                        assignParams += ", ";
                    }
                }

                var remappedGenerics = string.Empty;
                var remappedGeneric  = Data.type.type.Name.EnsureNonConstructName();

                for (int i = 0; i < Data.generics.Count; i++)
                {
                    remappedGenerics += Data.generics[i].type.type.As().CSharpName().Replace("&", string.Empty);
                    if (i < Data.generics.Count - 1)
                    {
                        remappedGenerics += ", ";
                    }
                }

                if (Data.generics.Count - 1 >= 0 || IsAction)
                {
                    for (int i = 0; i < (IsAction ? Data.generics.Count : Mathf.Clamp(Data.generics.Count - 1, 0, Data.generics.Count)); i++)
                    {
                        if (i < Data.generics.Count - 1 || IsAction)
                        {
                            invokeString += $"({ Data.generics[i].type.type.As().CSharpName().Replace("&", string.Empty)})parameters[{i.ToString()}]";
                            if (i < Data.generics.Count - (IsAction ? 1 : 2))
                            {
                                invokeString += ", ";
                            }
                        }
                    }
                }

                if (Data.generics.Count > 0)
                {
                    remappedGeneric = remappedGeneric.Contains("`") ? remappedGeneric.Remove(remappedGeneric.IndexOf("`"), remappedGeneric.Length - remappedGeneric.IndexOf("`")) : remappedGeneric;
                    remappedGeneric = remappedGeneric.TypeHighlight() + "<" + remappedGenerics + ">";
                }
                else
                {
                    remappedGeneric.TypeHighlight();
                }

                @class.AddField(FieldGenerator.Field(AccessModifier.Public, FieldModifier.None, remappedGeneric, Data.type.type.Namespace, "callback"));
                @class.AddField(FieldGenerator.Field(AccessModifier.Public, FieldModifier.None, remappedGeneric, Data.type.type.Namespace, "instance"));

                @class.AddField(FieldGenerator.Field(AccessModifier.Private, FieldModifier.None, typeof(bool), "_initialized"));

                @class.AddProperty(PropertyGenerator.Property(AccessModifier.Public, PropertyModifier.None, typeof(TypeParam), "parameters", false).SingleStatementGetter(AccessModifier.Public, "new".ConstructHighlight() + " TypeParam".TypeHighlight() + "[] {" + properties.Replace("&", string.Empty) + "}").AddTypeIndexer(""));

                @class.AddProperty(PropertyGenerator.Property(AccessModifier.Public, PropertyModifier.None, typeof(string), "DisplayName", false).SingleStatementGetter(AccessModifier.Public, $@"""{remappedGeneric.RemoveHighlights().RemoveMarkdown().Replace("<", " (").Replace(">", ")")}""".StringHighlight()));

                @class.AddProperty(PropertyGenerator.Property(AccessModifier.Public, PropertyModifier.None, typeof(bool), "initialized", false).SingleStatementGetter(AccessModifier.Public, "_initialized").SingleStatementSetter(AccessModifier.Public, "_initialized = value"));

                if (IsFunc)
                {
                    @class.AddProperty(PropertyGenerator.Property(AccessModifier.Public, PropertyModifier.None, typeof(Type), "ReturnType", false).SingleStatementGetter(AccessModifier.Public, "typeof".ConstructHighlight() + "(" + Data.generics[Data.generics.Count - 1].type.type.As().CSharpName() + ")"));
                }

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, typeof(Type), "GetDelegateType").Body("return typeof".ConstructHighlight() + "(" + remappedGeneric + ");"));

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, typeof(object), "GetDelegate").Body("return".ConstructHighlight() + " callback;"));

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, IsAction ? typeof(void) : typeof(object), IsAction ? "Invoke" : "DynamicInvoke").AddParameter(ParameterGenerator.Parameter("parameters", typeof(object[]), Libraries.CSharp.ParameterModifier.None, isParameters: true)).Body($"{(IsAction ? string.Empty : "return").ConstructHighlight()} callback({invokeString});"));

                if (!IsAction)
                {
                    @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, Data.generics[Mathf.Clamp(Data.generics.Count - 1, 0, Data.generics.Count - 1)].type.type, "Invoke").AddParameter(ParameterGenerator.Parameter("parameters", typeof(object[]), Libraries.CSharp.ParameterModifier.None, isParameters: true)).Body($"{(IsAction ? string.Empty : "return").ConstructHighlight()} callback({invokeString});"));
                }

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, typeof(void), "Initialize").
                                 AddParameter(ParameterGenerator.Parameter("flow", typeof(Flow), Libraries.CSharp.ParameterModifier.None)).
                                 AddParameter(ParameterGenerator.Parameter("unit", IsAction ? typeof(ActionNode) : typeof(FuncNode), Libraries.CSharp.ParameterModifier.None)).
                                 AddParameter(ParameterGenerator.Parameter(IsAction ? "flowAction" : "flowFunc", IsAction ? typeof(Action) : typeof(Func <object>), Libraries.CSharp.ParameterModifier.None)).
                                 Body("SetInstance(flow, unit, " + (IsAction ? "flowAction" : "flowFunc") + "); \n" + "callback = " + "new ".ConstructHighlight() + remappedGeneric + "(" + LambdaGenerator.SingleLine(stringConstructorParameters, (!IsAction ? "return".ConstructHighlight() + " " : string.Empty) + $"instance({assignParams});").Generate(0) + ");\n" + "initialized = " + "true".ConstructHighlight() + ";"));

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, typeof(void), "SetInstance").
                                 AddParameter(ParameterGenerator.Parameter("flow", typeof(Flow), Libraries.CSharp.ParameterModifier.None)).
                                 AddParameter(ParameterGenerator.Parameter("unit", IsAction ? typeof(ActionNode) : typeof(FuncNode), Libraries.CSharp.ParameterModifier.None)).
                                 AddParameter(ParameterGenerator.Parameter(IsAction ? "flowAction" : "flowFunc", IsAction ? typeof(Action) : typeof(Func <object>), Libraries.CSharp.ParameterModifier.None)).
                                 Body("instance = " + "new ".ConstructHighlight() + remappedGeneric + "(" + LambdaGenerator.SingleLine(stringConstructorParameters, (stringConstructorParameters.Count > 0 ? "unit.AssignParameters(flow, " + assignParams + "); " + (IsAction ? "flowAction();" : "return ".ConstructHighlight() + "(" + Data.generics[Data.generics.Count - 1].type.type.As().CSharpName() + ")" + "flowFunc();") : (IsAction ? "flowAction();" : "return ".ConstructHighlight() + "(" + Data.generics[Data.generics.Count - 1].type.type.As().CSharpName() + ")" + "flowFunc();"))).Generate(0) + ");"));

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, typeof(void), "Bind").
                                 AddParameter(ParameterGenerator.Parameter("other", typeof(IDelegate), Libraries.CSharp.ParameterModifier.None))
                                 .Body(
                                     "callback += (" + remappedGeneric + ")other.GetDelegate();"
                                     ));

                @class.AddMethod(MethodGenerator.Method(AccessModifier.Public, MethodModifier.None, typeof(void), "Unbind").
                                 AddParameter(ParameterGenerator.Parameter("other", typeof(IDelegate), Libraries.CSharp.ParameterModifier.None))
                                 .Body(
                                     "callback -= (" + remappedGeneric + ")other.GetDelegate();"
                                     ));

                @class.AddUsings(new List <string>()
                {
                    Data.type.type.Namespace, "System"
                });

                @class.AddAttribute(AttributeGenerator.Attribute <IncludeInSettingsAttribute>().AddParameter(true));

                if (Data.lastCompiledName != Data.GetFullTypeName())
                {
                    @class.AddAttribute(AttributeGenerator.Attribute <RenamedFromAttribute>().AddParameter(Data.lastCompiledName));
                }

                var genericNamespace = new List <string>();

                for (int i = 0; i < Data.generics.Count; i++)
                {
                    if (!genericNamespace.Contains(Data.generics[i].type.type.Namespace))
                    {
                        genericNamespace.Add(Data.generics[i].type.type.Namespace);
                    }
                }

                @class.AddUsings(genericNamespace);

                @namespace.AddClass(@class);

                return(@namespace.Generate(indent));
            }

            return(string.Empty);
        }