Example #1
0
        public override string GetNodeName()
        {
            Type instancecType = type.startType;

            if (instancecType != null)
            {
                if (instancecType.IsDefined(typeof(NodeMenu), true))
                {
                    return((instancecType.GetCustomAttributes(typeof(NodeMenu), true)[0] as NodeMenu).name);
                }
            }
            else
            {
                return("Missing Type");
            }
            return(type.DisplayName(false, false));
        }
Example #2
0
        private string GenerateEventCodes()
        {
            if (!body.isAssigned)
            {
                return(null);
            }
            Type targetType = target.type;

            if (targetType == null)
            {
                return(null);
            }
            System.Type[] parameterTypes = null;
            if (targetType.IsCastableTo(typeof(Delegate)))
            {
                parameterTypes = targetType.GetMethod("Invoke").GetParameters().Select(i => i.ParameterType).ToArray();

                if (CodeGenerator.IsGroupedNode(this) && CodeGenerator.CanSimplifyToLambda(body, targetType, parameterTypes))
                {
                    var bodyNode = body.GetTargetNode();
                    if (bodyNode as MultipurposeNode)
                    {
                        string result = bodyNode.GenerateValueCode();
                        if (result.EndsWith(")"))
                        {
                            int deep = 0;
                            for (int i = result.Length - 1; i > 0; i--)
                            {
                                var c = result[i];
                                if (c == '(')
                                {
                                    if (deep == 0)
                                    {
                                        result = result.Remove(i);
                                        break;
                                    }
                                    else
                                    {
                                        deep--;
                                    }
                                }
                                else if (c == ')' && i != result.Length - 1)
                                {
                                    deep++;
                                }
                            }
                        }
                        return(result);
                    }
                }
            }
            else if (targetType.IsCastableTo(typeof(UnityEventBase)))
            {
                var method = targetType.GetMethod("AddListener");
                var param  = method.GetParameters()[0].ParameterType;
                parameterTypes = param.GetGenericArguments();

                if (CodeGenerator.IsGroupedNode(this) && CodeGenerator.CanSimplifyToLambda(body, typeof(void), parameterTypes))
                {
                    var bodyNode = body.GetTargetNode();
                    if (bodyNode as MultipurposeNode)
                    {
                        string result = bodyNode.GenerateValueCode();
                        if (result.EndsWith(")"))
                        {
                            int deep = 0;
                            for (int i = result.Length - 1; i > 0; i--)
                            {
                                var c = result[i];
                                if (c == '(')
                                {
                                    if (deep == 0)
                                    {
                                        result = result.Remove(i);
                                        break;
                                    }
                                    else
                                    {
                                        deep--;
                                    }
                                }
                                else if (c == ')' && i != result.Length - 1)
                                {
                                    deep++;
                                }
                            }
                        }
                        return(result);
                    }
                }
            }
            else
            {
                throw new Exception("Unsupported event to hook:" + target.DisplayName(true));
            }
            //Generate lambda code
            string        contents       = null;
            List <Type>   types          = new List <Type>();
            List <string> parameterNames = new List <string>();

            for (int i = 0; i < parameterTypes.Length; i++)
            {
                var pType = parameterTypes[i];
                var field = this.GetType().GetField("parameters");
                if (pType != null)
                {
                    string varName = null;
                    if (CodeGenerator.NeedInstanceVariable(this, field, i, new Node[] { this }))
                    {
                        varName  = CodeGenerator.GenerateVariableName("tempVar", this);
                        contents = CodeGenerator.AddVariable(this, field, i, pType, true) + " = " + varName + ";" + contents.AddLineInFirst();
                    }
                    else
                    {
                        varName = CodeGenerator.GetVariableName(this, field, i);
                    }
                    types.Add(pType);
                    parameterNames.Add(varName);
                }
            }
            contents += CodeGenerator.GenerateFlowCode(body, this).AddLineInFirst();
            return(CodeGenerator.GenerateAnonymousMethod(types, parameterNames, contents));
        }