Example #1
0
        public static LiteralExpressionSyntax Literal(string typeSignature, string val)
        {
            Debug.Assert(typeSignature != null, "Type signature cannot be null.");
            var type = MyVisualScriptingProxy.GetType(typeSignature);

            if (type != null)
            {
                if (type == typeof(float))
                {
                    var floatVal = string.IsNullOrEmpty(val) ? 0 : float.Parse(val);
                    return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(floatVal)));
                }

                if (type == typeof(int) || type == typeof(long))
                {
                    return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(val)));
                }

                if (type == typeof(bool))
                {
                    var value = MySyntaxFactory.NormalizeBool(val);
                    if (value == "true")
                    {
                        return(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression));
                    }

                    return(SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
                }
            }

            return(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(val)));
        }
Example #2
0
        public static ArgumentSyntax ConstantArgument(string typeSignature, string value)
        {
            var type = MyVisualScriptingProxy.GetType(typeSignature);

            // First handle special cases as Color and Enums
            if (type == typeof(Color) || type.IsEnum)
            {
                return(SyntaxFactory.Argument(
                           SyntaxFactory.MemberAccessExpression(
                               SyntaxKind.SimpleMemberAccessExpression,
                               SyntaxFactory.IdentifierName(typeSignature),
                               SyntaxFactory.IdentifierName(value)
                               )));
            }

            if (type == typeof(Vector3D))
            {
                return(SyntaxFactory.Argument(
                           MySyntaxFactory.NewVector3D(value)
                           ));
            }

            return(SyntaxFactory.Argument(
                       Literal(typeSignature, value)
                       ));
        }
        public MyVisualSyntaxFunctionNode(MyObjectBuilder_ScriptNode ob, Type scriptBaseType) : base(ob)
        {
            Debug.Assert(ob is MyObjectBuilder_FunctionScriptNode);
            m_objectBuilder  = (MyObjectBuilder_FunctionScriptNode)ob;
            m_methodInfo     = MyVisualScriptingProxy.GetMethod(ObjectBuilder.Type);
            m_scriptBaseType = scriptBaseType;
            // Probably interface member
            if (m_methodInfo == null)
            {
                var methodName = ObjectBuilder.Type.Remove(0, ObjectBuilder.Type.LastIndexOf('.') + 1);
                methodName   = methodName.Remove(methodName.IndexOf('('));
                m_methodInfo = scriptBaseType.GetMethod(methodName);
            }

            // Check instance methods
            if (m_methodInfo == null && !string.IsNullOrEmpty(ObjectBuilder.DeclaringType))
            {
                var declaringType = MyVisualScriptingProxy.GetType(ObjectBuilder.DeclaringType);
                Debug.Assert(declaringType != null, "Function Node: Declaring type parsing failed.");

                if (declaringType != null)
                {
                    m_methodInfo = MyVisualScriptingProxy.GetMethod(declaringType, ObjectBuilder.Type);
                }
            }

            Debug.Assert(m_methodInfo != null,
                         "For Designers: The Signature: " + ObjectBuilder.Type +
                         " is out of date please consider updating the script in the Editor.");

            if (m_methodInfo != null)
            {
                InitUsing();
            }
        }
        internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            base.CollectInputExpressions(expressions);

            var entryType = MyVisualScriptingProxy.GetType(ObjectBuilder.Type);
            var listType  = typeof(List <>).MakeGenericType(entryType);

            var separatedList = new List <SyntaxNodeOrToken>();

            // Create source of arguments for array creation syntax
            for (var index = 0; index < ObjectBuilder.DefaultEntries.Count; index++)
            {
                var entry   = ObjectBuilder.DefaultEntries[index];
                var literal = MySyntaxFactory.Literal(ObjectBuilder.Type, entry);

                separatedList.Add(literal);

                if (index < ObjectBuilder.DefaultEntries.Count - 1)
                {
                    separatedList.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                }
            }

            // Syntax of "new Type[]{arg0, arg1, ...}"
            ArrayCreationExpressionSyntax arrayCreationSyntax = null;

            if (separatedList.Count > 0)
            {
                arrayCreationSyntax = SyntaxFactory.ArrayCreationExpression(
                    SyntaxFactory.ArrayType(
                        SyntaxFactory.IdentifierName(ObjectBuilder.Type),
                        SyntaxFactory.SingletonList(
                            SyntaxFactory.ArrayRankSpecifier(
                                SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(
                                    SyntaxFactory.OmittedArraySizeExpression()
                                    )
                                )
                            )
                        ),
                    SyntaxFactory.InitializerExpression(
                        SyntaxKind.ArrayInitializerExpression,
                        SyntaxFactory.SeparatedList <ExpressionSyntax>(
                            separatedList
                            )
                        )
                    );
            }

            // Syntax of new List<Type>(arrayCreationSyntax);
            var listCreationSyntax = MySyntaxFactory.GenericObjectCreation(listType, arrayCreationSyntax == null ? null : new[] { arrayCreationSyntax });

            var localVariableSyntax = MySyntaxFactory.LocalVariable(listType, VariableSyntaxName(), listCreationSyntax);

            expressions.Add(localVariableSyntax);
        }
        private StatementSyntax GetCorrectAssignmentsExpression()
        {
            // Determine Type and create correct assingment
            var boundVariableNode = Navigator.GetVariable(ObjectBuilder.VariableName);

            Debug.Assert(boundVariableNode != null, "Missing variable: " + ObjectBuilder.VariableName);

            var type = MyVisualScriptingProxy.GetType(boundVariableNode.ObjectBuilder.VariableType);

            if (type == typeof(string))
            {
                if (ObjectBuilder.ValueInputID.NodeID == -1)
                {
                    return(MySyntaxFactory.VariableAssignmentExpression(ObjectBuilder.VariableName, ObjectBuilder.VariableValue, SyntaxKind.StringLiteralExpression));
                }
            }
            else if (type == typeof(Vector3D))
            {
                if (ObjectBuilder.ValueInputID.NodeID == -1)
                {
                    return(MySyntaxFactory.SimpleAssignment(ObjectBuilder.VariableName, MySyntaxFactory.NewVector3D(ObjectBuilder.VariableValue)));
                }
            }
            else if (type == typeof(bool))
            {
                if (ObjectBuilder.ValueInputID.NodeID == -1)
                {
                    var normalizedValue = MySyntaxFactory.NormalizeBool(ObjectBuilder.VariableValue);
                    var syntaxKind      = normalizedValue == "true" ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression;
                    return(MySyntaxFactory.VariableAssignmentExpression(
                               ObjectBuilder.VariableName, ObjectBuilder.VariableValue, syntaxKind));
                }
            }
            else
            {
                if (ObjectBuilder.ValueInputID.NodeID == -1)
                {
                    return(MySyntaxFactory.VariableAssignmentExpression(ObjectBuilder.VariableName, ObjectBuilder.VariableValue, SyntaxKind.NumericLiteralExpression));
                }
            }

            return(MySyntaxFactory.SimpleAssignment(ObjectBuilder.VariableName, SyntaxFactory.IdentifierName(m_inputVariableName)));
        }
Example #6
0
        internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            var value = ObjectBuilder.Value ?? string.Empty;
            var type  = MyVisualScriptingProxy.GetType(ObjectBuilder.Type);

            base.CollectInputExpressions(expressions);

            // First handle special cases as Color and Enums
            if (type == typeof(Color) || type.IsEnum)
            {
                expressions.Add(
                    MySyntaxFactory.LocalVariable(
                        ObjectBuilder.Type,
                        VariableSyntaxName(),
                        SyntaxFactory.MemberAccessExpression(
                            SyntaxKind.SimpleMemberAccessExpression,
                            SyntaxFactory.IdentifierName(ObjectBuilder.Type),
                            SyntaxFactory.IdentifierName(ObjectBuilder.Value)
                            )
                        )
                    );
            }
            else if (type == typeof(Vector3D))
            {
                expressions.Add(
                    MySyntaxFactory.LocalVariable(ObjectBuilder.Type, VariableSyntaxName(),
                                                  MySyntaxFactory.NewVector3D(ObjectBuilder.Value)
                                                  )
                    );
            }
            else
            {
                // Rest is generic
                expressions.Add(
                    MySyntaxFactory.LocalVariable(
                        ObjectBuilder.Type,
                        VariableSyntaxName(),
                        MySyntaxFactory.Literal(ObjectBuilder.Type, value))
                    );
            }
        }
Example #7
0
        public MyVisualScriptNavigator(MyObjectBuilder_VisualScript scriptOb)
        {
            var scriptBase = string.IsNullOrEmpty(scriptOb.Interface) ? null : MyVisualScriptingProxy.GetType(scriptOb.Interface);

            foreach (var scriptNodeOb in scriptOb.Nodes)
            {
                Debug.Assert(!m_idToNode.ContainsKey(scriptNodeOb.ID));

                MyVisualSyntaxNode node;
                if (scriptNodeOb is MyObjectBuilder_NewListScriptNode)
                {
                    node = new MyVisualSyntaxNewListNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_SwitchScriptNode)
                {
                    node = new MyVisualSyntaxSwitchNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_LocalizationScriptNode)
                {
                    node = new MyVisualSyntaxLocalizationNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_LogicGateScriptNode)
                {
                    node = new MyVisualSyntaxLogicGateNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_ForLoopScriptNode)
                {
                    node = new MyVisualSyntaxForLoopNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_SequenceScriptNode)
                {
                    node = new MyVisualSyntaxSequenceNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_ArithmeticScriptNode)
                {
                    node = new MyVisualSyntaxArithmeticNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_InterfaceMethodNode)
                {
                    node = new MyVisualSyntaxInterfaceMethodNode(scriptNodeOb, scriptBase);
                }
                else if (scriptNodeOb is MyObjectBuilder_KeyEventScriptNode)
                {
                    node = new MyVisualSyntaxKeyEventNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_BranchingScriptNode)
                {
                    node = new MyVisualSyntaxBranchingNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_InputScriptNode)
                {
                    node = new MyVisualSyntaxInputNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_CastScriptNode)
                {
                    node = new MyVisualSyntaxCastNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_EventScriptNode)
                {
                    node = new MyVisualSyntaxEventNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_FunctionScriptNode)
                {
                    node = new MyVisualSyntaxFunctionNode(scriptNodeOb, scriptBase);
                }
                else if (scriptNodeOb is MyObjectBuilder_VariableSetterScriptNode)
                {
                    node = new MyVisualSyntaxSetterNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_TriggerScriptNode)
                {
                    node = new MyVisualSyntaxTriggerNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_VariableScriptNode)
                {
                    node = new MyVisualSyntaxVariableNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_ConstantScriptNode)
                {
                    node = new MyVisualSyntaxConstantNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_GetterScriptNode)
                {
                    node = new MyVisualSyntaxGetterNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_OutputScriptNode)
                {
                    node = new MyVisualSyntaxOutputNode(scriptNodeOb);
                }
                else if (scriptNodeOb is MyObjectBuilder_ScriptScriptNode)
                {
                    node = new MyVisualSyntaxScriptNode(scriptNodeOb);
                }
                else
                {
                    continue;
                }

                node.Navigator = this;

                m_idToNode.Add(scriptNodeOb.ID, node);

                var type = node.GetType();
                if (!m_nodesByType.ContainsKey(type))
                {
                    m_nodesByType.Add(type, new List <MyVisualSyntaxNode>());
                }

                m_nodesByType[type].Add(node);

                if (type == typeof(MyVisualSyntaxVariableNode))
                {
                    m_variablesByName.Add(((MyObjectBuilder_VariableScriptNode)scriptNodeOb).VariableName, (MyVisualSyntaxVariableNode)node);
                }
            }
        }
Example #8
0
 public MyVisualSyntaxVariableNode(MyObjectBuilder_ScriptNode ob) : base(ob)
 {
     Debug.Assert(ob is MyObjectBuilder_VariableScriptNode);
     m_variableType = MyVisualScriptingProxy.GetType(ObjectBuilder.VariableType);
     Using          = MySyntaxFactory.UsingStatementSyntax(m_variableType.Namespace);
 }