Beispiel #1
0
    private static void AddCommand(string command, string description, MethodInfo method, object instance = null)
    {
        // Fetch the parameters of the class
        ParameterInfo[] parameters = method.GetParameters();
        if (parameters == null)
        {
            parameters = new ParameterInfo[0];
        }

        bool isMethodValid = true;

        // Store the parameter types in an array
        Type[] parameterTypes = new Type[parameters.Length];
        for (int k = 0; k < parameters.Length; k++)
        {
            Type parameterType = parameters[k].ParameterType;
            if (parseFunctions.ContainsKey(parameterType))
            {
                parameterTypes[k] = parameterType;
            }
            else
            {
                isMethodValid = false;
                break;
            }
        }

        // If method is valid, associate it with the entered command
        if (isMethodValid)
        {
            StringBuilder methodSignature = new StringBuilder(256);
            methodSignature.Append(command).Append(": ");

            if (!string.IsNullOrEmpty(description))
            {
                methodSignature.Append(description).Append(" -> ");
            }

            methodSignature.Append(method.DeclaringType.ToString()).Append(".").Append(method.Name).Append("(");
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                Type   type = parameterTypes[i];
                string typeName;
                if (!typeReadableNames.TryGetValue(type, out typeName))
                {
                    typeName = type.Name;
                }

                methodSignature.Append(typeName);

                if (i < parameterTypes.Length - 1)
                {
                    methodSignature.Append(", ");
                }
            }

            methodSignature.Append(")");

            Type returnType = method.ReturnType;
            if (returnType != typeof(void))
            {
                string returnTypeName;
                if (!typeReadableNames.TryGetValue(returnType, out returnTypeName))
                {
                    returnTypeName = returnType.Name;
                }

                methodSignature.Append(" : ").Append(returnTypeName);
            }

            methods[command] = new ConsoleMethodInfo(method, parameterTypes, instance, methodSignature.ToString());
        }
    }
Beispiel #2
0
    // Create a new command and set its properties
    private static void AddCommand(string command, string methodName, System.Type ownerType, object instance = null)
    {
        // Get the method from the class
        MethodInfo method = ownerType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

        if (method == null)
        {
            Debug.LogError(methodName + " does not exist in " + ownerType);
            return;
        }

        // Fetch the parameters of the class
        ParameterInfo[] parameters = method.GetParameters();
        if (parameters == null || parameters.Length == 0)
        {
            // Method takes no parameters

            // Associate the method with the entered command
            methods[command] = new ConsoleMethodInfo(method, new System.Type[] { }, instance);

            string methodSignature = method.ToString();
            methodSignature = methodSignature.Replace("Void ", "");
            methodSignatures.Add(command + ": " + ownerType.ToString() + "." + methodSignature);
        }
        else
        {
            // Method takes parameter(s), check their types
            // to see if this method is valid (can be called by a command)

            bool isMethodValid = true;

            // Store the parameter types in an array
            System.Type[] parameterTypes = new System.Type[parameters.Length];
            for (int k = 0; k < parameters.Length; k++)
            {
                System.Type parameterType = parameters[k].ParameterType;
                if (parameterType == typeof(int) || parameterType == typeof(float) || parameterType == typeof(bool) || parameterType == typeof(string) ||
                    parameterType == typeof(Vector2) || parameterType == typeof(Vector3) || parameterType == typeof(Vector4))
                {
                    parameterTypes[k] = parameterType;
                }
                else
                {
                    isMethodValid = false;
                    break;
                }
            }

            // If method is valid, associate it with the entered command
            if (isMethodValid)
            {
                methods[command] = new ConsoleMethodInfo(method, parameterTypes, instance);

                string methodSignature = method.ToString();
                methodSignature = methodSignature.Replace("Int32", "Integer");
                methodSignature = methodSignature.Replace("Single", "Float");
                methodSignature = methodSignature.Replace("System.", "");
                methodSignature = methodSignature.Replace("Void ", "");
                methodSignatures.Add(command + ": " + ownerType.ToString() + "." + methodSignature);
            }
        }
    }