예제 #1
0
    private void LoadParentLeafFunctions(
        out Dictionary <string, MethodInfo> leaves,
        out Dictionary <string, MethodInfo> conditions)
    {
        leaves     = new Dictionary <string, MethodInfo>();
        conditions = new Dictionary <string, MethodInfo>();

        Type parentType = parent.GetType();

        MethodInfo[] allMethods = parentType.GetMethods(BindingFlags.Instance | BindingFlags.Public);

        foreach (MethodInfo method in allMethods)
        {
            object[] custAttrs = method.GetCustomAttributes(typeof(BTLeafAttribute), false);
            if (custAttrs == null || custAttrs.Length == 0)
            {
                continue;
            }

            BTLeafAttribute leafAttr = (BTLeafAttribute)custAttrs[0];

            if (method.GetParameters().Length > 0)
            {
                throw new ArgumentException("Method '" + method.Name + "' cannot have parameters if it is marked as a leaf function");
            }

            string key = leafAttr.LeafName.Trim().ToLowerInvariant();

            if (method.ReturnType == typeof(bool)) //condition
            {
                if (conditions.ContainsKey(key))
                {
                    throw new ArgumentException("Duplicate condition name: '" + key + "'");
                }

                conditions[key] = method;
            }
            else if (method.ReturnType == typeof(BTCoroutine)) //leaf
            {
                if (leaves.ContainsKey(key))
                {
                    throw new ArgumentException("Duplicate leaf name: '" + key + "'");
                }

                leaves[key] = method;
            }
            else
            {
                throw new ArgumentException("Method '" + method.Name + "' must return either bool or BTCoroutine");
            }
        }
    }
예제 #2
0
    private void LoadParentLeafFunctions(
        out Dictionary <string, MethodInfo> complex,
        out Dictionary <string, MethodInfo> simple,
        out Dictionary <string, MethodInfo> condition)
    {
        complex   = new Dictionary <string, MethodInfo>();
        simple    = new Dictionary <string, MethodInfo>();
        condition = new Dictionary <string, MethodInfo>();

        Type parentType = parent.GetType();

        MethodInfo[] allMethods = parentType.GetMethods(BindingFlags.Instance | BindingFlags.Public);

        foreach (MethodInfo method in allMethods)
        {
            object[] custAttrs = method.GetCustomAttributes(typeof(BTLeafAttribute), false);
            if (custAttrs == null || custAttrs.Length == 0)
            {
                continue;
            }

            BTLeafAttribute leafAttr = (BTLeafAttribute)custAttrs[0];

            string key = leafAttr.LeafName.Trim().ToLowerInvariant();

            if (method.ReturnType == typeof(bool) &&
                method.GetParameters().Count() != 0 &&
                method.GetParameters()[0].ParameterType == typeof(Scorer)) // condition
            {
                if (complex.ContainsKey(key))
                {
                    throw new ArgumentException("Duplicate condition node name: '" + key + "'");
                }

                condition[key] = method;
            }

            else if (method.ReturnType == typeof(bool)) // simple
            {
                if (simple.ContainsKey(key))
                {
                    throw new ArgumentException("Duplicate simple node name: '" + key + "'");
                }

                if (method.GetParameters().Length > 0)
                {
                    throw new ArgumentException("Method '" + method.Name + "' cannot have parameters if it is marked as a simple node function");
                }

                simple[key] = method;
            }
            else if (method.ReturnType == typeof(BTCoroutine)) // complex
            {
                if (complex.ContainsKey(key))
                {
                    throw new ArgumentException("Duplicate complex node name: '" + key + "'");
                }

                if (method.GetParameters().Length != 1)
                {
                    throw new ArgumentException("Method '" + method.Name + "' must have a single Stopper parameter if it is a complex node function");
                }

                complex[key] = method;
            }

            else
            {
                throw new ArgumentException("Method '" + method.Name + "' must return either bool or BTCoroutine");
            }
        }
    }