Esempio n. 1
0
        // runtime helpers
        public static Runtime.Node CreateRuntimeNode(Node node, Component parent)
        {
            System.Type type = node.RuntimeType;
            if (type == null)
            {
                EB.Debug.LogError("No type for class " + node.runtimeTypeName);
                return(null);
            }

            Runtime.Node runtimeNode = (Runtime.Node)System.Activator.CreateInstance(type);
            runtimeNode.Parent = parent;
            runtimeNode.Id     = node.id;

            foreach (var property in node.properties)
            {
                var field = type.GetField(property.name);
                if (field == null || field.FieldType.IsArray)
                {
                    //EB.Debug.LogWarning("Failed to find property:" + property.name);
                }
                else
                {
                    field.SetValue(runtimeNode, property.Value);
                }
            }

            foreach (var array in node.propertyArrays)
            {
                var field = type.GetField(array.name);
                if (field == null || field.FieldType.IsArray == false)
                {
                    EB.Debug.LogWarning("Failed to find property:" + array.name);
                }
                else
                {
                    System.Array items = System.Array.CreateInstance(field.FieldType.GetElementType(), array.items.Count);
                    for (int i = 0; i < array.items.Count; ++i)
                    {
                        items.SetValue(array.items[i].Value, i);
                    }
                    field.SetValue(runtimeNode, items);
                }
            }

            //EB.Debug.LogIf( parent.name == "sequence_questlevel1fnetting_master", "Creating node " +node.id );

            runtimeNode.Init();

            return(runtimeNode);
        }
Esempio n. 2
0
        protected void LinkNodes()
        {
            // connect everything
            foreach (Serialization.Link link in Links)
            {
                // determine the link type
                Runtime.Node inNode  = GetNode(link.inId);
                Runtime.Node outNode = GetNode(link.outId);

                if (inNode == null)
                {
                    EB.Debug.LogError("Invalid Link: inNode is null " + link.inId + " " + this.name);
                    continue;
                }

                if (outNode == null)
                {
                    EB.Debug.LogError("Invalid Link: outNode is null " + link.outId + " " + this.name);
                    continue;
                }

                var inFieldInfo  = Utils.ParseField(link.inName);
                var outFieldInfo = Utils.ParseField(link.outName);

                if (outNode is Runtime.Variable && (link.outName == Runtime.Variable.ValueLinkName || link.outName == string.Empty))
                {
                    // variable link
                    FieldInfo field = inNode.GetType().GetField(inFieldInfo.field);
                    if (field != null)
                    {
                        if (field.FieldType.IsArray)
                        {
                            // add to the array
                            List <Runtime.Variable> items   = new List <Runtime.Variable>();
                            Runtime.Variable[]      current = (Runtime.Variable[])field.GetValue(inNode);
                            if (current != null)
                            {
                                foreach (var c in current)
                                {
                                    items.Add(c);
                                }
                            }

                            if (inFieldInfo.index == -1)
                            {
                                items.Add((Runtime.Variable)outNode);
                            }
                            else
                            {
                                // resize
                                while (items.Count <= inFieldInfo.index)
                                {
                                    items.Add(Runtime.Variable.Null);
                                }
                                items[inFieldInfo.index] = (Runtime.Variable)outNode;
                            }

                            field.SetValue(inNode, items.ToArray());
                        }
                        else
                        {
                            field.SetValue(inNode, outNode);
                        }
                    }
                    else
                    {
                        EB.Debug.LogWarning("Failed to find variable link: " + link.inName);
                    }
                }
                else
                {
                    // regular link
                    FieldInfo field = outNode.GetType().GetField(outFieldInfo.field);
                    if (field != null)
                    {
                        Runtime.Trigger trigger = null;

                        if (field.FieldType.IsArray)
                        {
                            // add to the array
                            List <Runtime.Trigger> items   = new List <Runtime.Trigger>();
                            Runtime.Trigger[]      current = (Runtime.Trigger[])field.GetValue(outNode);
                            if (current != null)
                            {
                                foreach (var c in current)
                                {
                                    items.Add(c);
                                }
                            }

                            if (outFieldInfo.index == -1)
                            {
                                trigger = new Runtime.Trigger();
                                items.Add(trigger);
                            }
                            else
                            {
                                // resize
                                while (items.Count <= outFieldInfo.index)
                                {
                                    items.Add(new Runtime.Trigger());
                                }
                                trigger = items[outFieldInfo.index];
                            }

                            field.SetValue(outNode, items.ToArray());
                        }
                        else
                        {
                            trigger = (Runtime.Trigger)field.GetValue(outNode);
                        }

                        // use create delegate
                        var mi = inNode.GetType().GetMethod(link.inName);
                        if (mi != null)
                        {
                            var action = System.Delegate.CreateDelegate(typeof(System.Action), inNode, mi, false);
                            if (action != null)
                            {
                                trigger.trigger += (System.Action)action;
                            }
                            else
                            {
                                EB.Debug.LogError("Failed to create delegate for function {0} on type {1}", link.inName, inNode.GetType());
                            }
                        }
                        else
                        {
                            EB.Debug.LogError("Failed to get methodInfo for function {0} on type {1}", link.inName, inNode.GetType());
                        }
                    }
                    else
                    {
                        EB.Debug.LogWarning("Failed to find link: " + link.outName);
                    }
                }
            }
        }