예제 #1
0
        protected static void ConstructTypeInformation(Type type, TypeInformation typeInformation)
        {
            // Implicit Conversions
            var conversions = new Dictionary <string, TypeConversion> ();
            var methods     = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

            foreach (var method in methods)
            {
                if (method.Name == "op_implicit")
                {
                    conversions.Add(method.ReturnType.FullName, TypeConversion.Construct(method));
                }
            }
            typeInformation.ImplicitConversions = conversions;

            // Instancing
            object defaultInstance = null;

            try
            {
                defaultInstance = Activator.CreateInstance(type);
            }
            catch
            {
            }

            // Default Value
            if (defaultInstance != null)
            {
                typeInformation.DefaultValue = JToken.FromObject(defaultInstance);
            }

            // Fields
            if (typeInformation.DefaultValue != null &&
                typeInformation.DefaultValue.Type == JTokenType.Object)
            {
                var fieldInfos = new Dictionary <string, FieldInformation> ();
                foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    if (field.FieldType == typeof(OutputSocket))
                    {
                        continue;
                    }

                    if (field.GetCustomAttribute <JsonIgnoreAttribute> () != null)
                    {
                        continue;
                    }

                    if (field.IsPrivate)
                    {
                        continue;
                    }

                    fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, defaultInstance));
                }
                typeInformation.Fields = fieldInfos;
            }
        }
예제 #2
0
        public static FieldInformation Construct(FieldInfo field)
        {
            object[] attributes   = field.GetCustomAttributes(false);
            string[] attributeIds = new string[attributes.Length];
            for (int i = 0; i < attributes.Length; i++)
            {
                attributeIds[i] = attributes.GetType().Name;
            }

            var fieldInformation = new FieldInformation
            {
                Type       = field.FieldType.Name,
                Name       = field.Name,
                Attributes = attributeIds
            };

            return(fieldInformation);
        }
예제 #3
0
        public static FieldInformation Construct(FieldInfo field, object defaultInstance)
        {
            object[] attributes   = field.GetCustomAttributes(false);
            string[] attributeIds = new string[attributes.Length];
            for (int i = 0; i < attributes.Length; i++)
            {
                attributeIds[i] = attributes.GetType().Name;
            }

            FieldInformation fieldInformation;

            if (typeof(InputSocket).IsAssignableFrom(field.FieldType))
            {
                fieldInformation = new FieldInformation()
                {
                    Type         = "InputSocket",
                    Attributes   = attributeIds,
                    DefaultValue = new JValue((object)null)
                };
            }
            else
            {
                object defaultValue = null;

                if (defaultInstance != null)
                {
                    field.GetValue(defaultInstance);
                }

                string typeName = field.FieldType.Name;

                try
                {
                    fieldInformation = new FieldInformation
                    {
                        Type         = typeName,
                        Attributes   = attributeIds,
                        DefaultValue = new JValue(defaultValue)
                    };
                }
                catch
                {
                    fieldInformation = new FieldInformation
                    {
                        Type         = typeName,
                        Attributes   = attributeIds,
                        DefaultValue = JObject.FromObject(defaultValue)
                    };
                }

                if (typeof(IDictionary).IsAssignableFrom(field.FieldType))
                {
                    fieldInformation.Format = FieldFormat.Dictionary;
                    fieldInformation.Type   = field.FieldType.GetGenericArguments()[1].Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = field.FieldType.GetGenericArguments()[1].Name,
                        Format = FieldFormat.Object
                    };
                }
                else if (field.FieldType.IsArray)
                {
                    fieldInformation.Format = FieldFormat.List;
                }
                else
                {
                    fieldInformation.Format = FieldFormat.Object;
                }
            }


            return(fieldInformation);
        }
예제 #4
0
        public static FieldInformation ConstructFieldInformation(ModelMember member, object defaultInstance)
        {
            object[] attributes   = member.GetCustomAttributes(false);
            string[] attributeIds = new string[attributes.Length];
            for (int i = 0; i < attributes.Length; i++)
            {
                attributeIds[i] = attributes.GetType().Name;
            }

            FieldInformation fieldInformation;

            if (typeof(InputSocket).IsAssignableFrom(member.ValueType))
            {
                fieldInformation = new FieldInformation()
                {
                    Type         = "InputSocket",
                    Attributes   = attributeIds,
                    DefaultValue = new JValue((object)null),
                    Format       = FieldFormat.Object,
                };
            }
            else
            {
                object defaultValue = null;

                if (defaultInstance != null)
                {
                    defaultValue = member.GetValue(defaultInstance);
                }

                string typeName = member.ValueType.Name;

                try
                {
                    fieldInformation = new FieldInformation
                    {
                        Type         = typeName,
                        Attributes   = attributeIds,
                        DefaultValue = new JValue(defaultValue),
                        Format       = FieldFormat.Object,
                    };
                }
                catch
                {
                    fieldInformation = new FieldInformation
                    {
                        Type         = typeName,
                        Attributes   = attributeIds,
                        DefaultValue = JObject.FromObject(defaultValue),
                        Format       = FieldFormat.Object,
                    };
                }

                if (typeof(IDictionary).IsAssignableFrom(member.ValueType))
                {
                    fieldInformation.Format = FieldFormat.Dictionary;
                    fieldInformation.Type   = member.ValueType.GetGenericArguments()[1].Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = member.ValueType.GetGenericArguments()[1].Name,
                        Format = FieldFormat.Object
                    };
                }
                else if (member.ValueType.IsArray)
                {
                    var elementType = member.ValueType.GetElementType();

                    fieldInformation.Format = FieldFormat.List;
                    fieldInformation.Type   = elementType.Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = elementType.Name,
                        Format = FieldFormat.Object
                    };
                }
                else if (member.ValueType.IsGenericType &&
                         !member.ValueType.IsGenericTypeDefinition &&
                         member.ValueType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    var elementType = member.ValueType.GenericTypeArguments[0];

                    fieldInformation.Format = FieldFormat.List;
                    fieldInformation.Type   = elementType.Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = elementType.Name,
                        Format = FieldFormat.Object
                    };
                }
                else
                {
                    fieldInformation.Format = FieldFormat.Object;
                }
            }

            return(fieldInformation);
        }
예제 #5
0
        public static NodeInformation Construct(Type nodeType)
        {
            var nodeInformation = new NodeInformation
            {
                Name = nodeType.Name
            };

            var typeDefinition  = new Type[] { typeof(IGraphInstance), nodeType.BaseType.GenericTypeArguments[0] };
            var inputsProperty  = nodeType.GetMethod("Inputs", typeDefinition);
            var outputsProperty = nodeType.GetMethod("Outputs", typeDefinition);

            var    nodeTemplate     = (Node)Activator.CreateInstance(nodeType);
            object metadataInstance = Activator.CreateInstance(typeDefinition[1]);
            var    singleNodeGraph  = new ManifestCaptureGraphInstance(nodeTemplate);

            int inputId            = 0;
            int outputId           = 0;
            var inputSocketFields  = new List <FieldInfo> ();
            var outputSocketFields = new List <FieldInfo> ();
            var fieldInfos         = new List <FieldInformation> ();

            foreach (var field in nodeType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType == typeof(InputSocket))
                {
                    field.SetValue(nodeTemplate, new InputSocket(inputId));
                    inputId++;
                    inputSocketFields.Add(field);
                }
                else if (field.FieldType == typeof(OutputSocket))
                {
                    field.SetValue(nodeTemplate, new OutputSocket(outputId));
                    outputId++;
                    outputSocketFields.Add(field);
                }
                else
                {
                    fieldInfos.Add(FieldInformation.Construct(field));
                }
            }
            nodeInformation.Fields = fieldInfos.ToArray();

            object[] connectParameters = { singleNodeGraph, metadataInstance };
            var      inputsArray       = (InputMap[])inputsProperty.Invoke(nodeTemplate, connectParameters);
            var      outputsArray      = (OutputMap[])outputsProperty.Invoke(nodeTemplate, connectParameters);

            if (inputsArray != null)
            {
                nodeInformation.Inputs = new SocketInformation[inputsArray.Length];

                for (int i = 0; i < inputsArray.Length; i++)
                {
                    var map   = inputsArray[i];
                    var field = inputSocketFields[map.Source.TargetId];
                    nodeInformation.Inputs[i] = SocketInformation.Construct(field, map);
                }
            }
            else
            {
                nodeInformation.Inputs = null;
            }

            if (outputsArray != null)
            {
                nodeInformation.Outputs = new SocketInformation[outputsArray.Length];

                for (int i = 0; i < outputsArray.Length; i++)
                {
                    var map   = outputsArray[i];
                    var field = outputSocketFields[map.Source.Id];
                    nodeInformation.Outputs[i] = SocketInformation.Construct(field, map);
                }
            }
            else
            {
                nodeInformation.Outputs = null;
            }

            return(nodeInformation);
        }
예제 #6
0
        public static NodeInformation Construct(Type nodeType)
        {
            var nodeInformation = new NodeInformation();

            var nodeTemplate     = (NodeTemplate)Activator.CreateInstance(nodeType);
            var metadataInstance = (NodeInstanceBase)nodeTemplate.CreateInstance();

            var instanceType = metadataInstance.GetType();

            var singleNodeGraph = new ManifestCaptureGraphInstance(nodeTemplate);

            int inputId            = 0;
            int outputId           = 0;
            var inputSocketFields  = new List <FieldInfo>();
            var outputSocketFields = new List <FieldInfo>();
            var fieldInfos         = new Dictionary <string, FieldInformation>();

            foreach (var field in nodeType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType == typeof(OutputSocket))
                {
                    field.SetValue(nodeTemplate, new OutputSocket(outputId));
                    outputId++;
                    outputSocketFields.Add(field);
                }
                else
                {
                    if (field.FieldType == typeof(InputSocket))
                    {
                        field.SetValue(nodeTemplate, new InputSocket(inputId));
                        inputId++;
                        inputSocketFields.Add(field);
                    }
                    fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, nodeTemplate));
                }
            }
            nodeInformation.Fields = fieldInfos;

            var nodeProperty = instanceType.GetProperty("Template");

            nodeProperty.SetValue(metadataInstance, nodeTemplate);

            var connectionMapper = new ConnectionMapper(metadataInstance, singleNodeGraph);

            var inputsArray  = metadataInstance.Inputs(connectionMapper);
            var outputsArray = metadataInstance.Outputs(connectionMapper);

            if (inputsArray != null)
            {
                nodeInformation.Inputs = new Dictionary <string, SocketInformation>(inputsArray.Length);

                for (int i = 0; i < inputsArray.Length; i++)
                {
                    var map   = inputsArray[i];
                    var field = inputSocketFields[map.ConnectionId];
                    nodeInformation.Inputs.Add(field.Name, SocketInformation.Construct(field, map));
                }
            }
            else
            {
                nodeInformation.Inputs = null;
            }

            if (outputsArray != null)
            {
                nodeInformation.Outputs = new Dictionary <string, SocketInformation>(outputsArray.Length);

                for (int i = 0; i < outputsArray.Length; i++)
                {
                    var map   = outputsArray[i];
                    var field = outputSocketFields[map.ConnectionId];
                    nodeInformation.Outputs.Add(field.Name, SocketInformation.Construct(field, map));
                }
            }
            else
            {
                nodeInformation.Outputs = null;
            }

            return(nodeInformation);
        }
예제 #7
0
        public static NodeInformation ConstructNodeInformation(Type nodeType)
        {
            var nodeInformation = new NodeInformation();

            var    typeDefinition   = new Type[] { typeof(IGraphConnections) };
            var    nodeTemplate     = (Node)Activator.CreateInstance(nodeType);
            object metadataInstance = nodeTemplate.CreateInstance();

            var instanceType = metadataInstance.GetType();

            var inputsProperty  = instanceType.GetMethod("Inputs", typeDefinition);
            var outputsProperty = instanceType.GetMethod("Outputs", typeDefinition);

            var singleNodeGraph = new ManifestCaptureGraphInstance(nodeTemplate);

            int inputId            = 0;
            int outputId           = 0;
            var inputSocketFields  = new List <FieldInfo> ();
            var outputSocketFields = new List <FieldInfo> ();
            var fieldInfos         = new Dictionary <string, FieldInformation> ();

            foreach (var field in nodeType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType == typeof(OutputSocket))
                {
                    field.SetValue(nodeTemplate, new OutputSocket(outputId));
                    outputId++;
                    outputSocketFields.Add(field);
                }
                else
                {
                    if (field.FieldType == typeof(InputSocket))
                    {
                        field.SetValue(nodeTemplate, new InputSocket(inputId));
                        inputId++;
                        inputSocketFields.Add(field);
                    }
                    fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, nodeTemplate));
                }
            }
            nodeInformation.Fields = fieldInfos;

            var nodeProperty = instanceType.GetProperty("Node");

            nodeProperty.SetValue(metadataInstance, nodeTemplate);

            object[] connectParameters = { singleNodeGraph };
            var      inputsArray       = (InputMap[])inputsProperty.Invoke(metadataInstance, connectParameters);
            var      outputsArray      = (OutputMap[])outputsProperty.Invoke(metadataInstance, connectParameters);

            if (inputsArray != null)
            {
                nodeInformation.Inputs = new Dictionary <string, SocketInformation> (inputsArray.Length);

                for (int i = 0; i < inputsArray.Length; i++)
                {
                    var map   = inputsArray[i];
                    var field = inputSocketFields[map.ConnectionId];
                    nodeInformation.Inputs.Add(field.Name, SocketInformation.Construct(field, map));
                }
            }
            else
            {
                nodeInformation.Inputs = null;
            }

            if (outputsArray != null)
            {
                nodeInformation.Outputs = new Dictionary <string, SocketInformation> (outputsArray.Length);

                for (int i = 0; i < outputsArray.Length; i++)
                {
                    var map   = outputsArray[i];
                    var field = outputSocketFields[map.ConnectionId];
                    nodeInformation.Outputs.Add(field.Name, SocketInformation.Construct(field, map));
                }
            }
            else
            {
                nodeInformation.Outputs = null;
            }

            return(nodeInformation);
        }