コード例 #1
0
        [Help("Tries to issue the command. Returns true if successful.")] //assumptions: methodnames and classnames are intelligently made. If there are collisions in class and method, we assume class
        //also, classnames arent similar. We can't tell the difference between two classes MATH and Math
        //calling execute:
        //!math.sin 30
        //!sin 30
        public bool Execute(string inputstring, AdditionalParameters additionalparms, out object[] refvals)
        {
            refvals = new object[1];
            if (string.IsNullOrWhiteSpace(inputstring))
            {
                return(false);
            }

            string[] words = _customsplit(inputstring);
            words[0] = words[0].ToLower();

            string     methodname;
            Lieutenant lt;
            string     message;

            if (!_dotnotation(words[0], out lt, out methodname, out message))
            {
                refvals[0] = message;
                return(false);
            }

            if (_disallowedcommands.Contains(methodname))
            {
                refvals[0] = message;
                return(false);
            }

            return(lt.Execute(methodname, words.Skip(1).ToArray(), additionalparms, out refvals));
        }
コード例 #2
0
 public static string Log(string text, AdditionalParameters ap)
 {
     return(Log(text, ap.Origin));
 }
コード例 #3
0
            public bool Execute(string method, string[] words, AdditionalParameters additionalparms, out object[] refvals)
            {
                refvals = new object[1] {
                    ""
                };
                if (Methods.ContainsKey(method))
                {
                    foreach (MethodInfo mi in Methods[method])
                    {
                        ParameterInfo[] miparameters      = mi.GetParameters();
                        object[]        parametervalues   = new object[miparameters.Length];
                        AdditionalParametersAttribute apa = mi.GetCustomAttribute <AdditionalParametersAttribute>(); //todo: deprecate
                        string s = "";
                        foreach (ParameterInfo pi in miparameters)
                        {
                            s += pi.ParameterType + ",";
                        }
                        if (miparameters.Length != words.Length + (apa == null ? 0 : 1))
                        {
                            continue;
                        }
                        int  wordsindex        = -1;
                        bool gotgoodparameters = true;
                        for (int i = 0; i < miparameters.Length; i++) //of method parameters //we convert the parameters to the true datatype. "6"=>6, if the types match
                        {
                            Type dereferencedtype;
                            if (miparameters[i].ParameterType.IsByRef || miparameters[i].IsOut)
                            {
                                dereferencedtype = miparameters[i].ParameterType.GetElementType();
                            }
                            else
                            {
                                dereferencedtype = miparameters[i].ParameterType;
                            }
                            if (miparameters[i].ParameterType == typeof(AdditionalParameters))
                            {
                                parametervalues[i] = additionalparms;
                            }
                            else if (++wordsindex < words.Length && CanConvert(words[wordsindex], dereferencedtype))
                            {
                                parametervalues[i] = _convert[dereferencedtype].Invoke(words[wordsindex] as IConvertible, new object[] { null });
                            }
                            else
                            {
                                gotgoodparameters = false;
                                break;
                            }
                        }

                        if (!gotgoodparameters)
                        {
                            continue;
                        }

                        if (mi.GetCustomAttribute <ViewerPermittedAttribute>() == null && additionalparms.Origin != Executor.Owner)
                        {
                            refvals[0] = "Only owner can run this";
                            return(false);
                        }
                        //we have a matching set of parameters, converted to their true datatype. now we invoke the method
                        try
                        {
                            if (mi.ReturnType == typeof(void))
                            {
                                Task.FromResult(mi.Invoke(CSObject, parametervalues));
                                List <object> refvalueslist = new List <object>();
                                refvalueslist.Add(null); //void
                                for (int i = 0; i < miparameters.Length; i++)
                                {
                                    if (miparameters[i].IsOut || miparameters[i].ParameterType.IsByRef)
                                    {
                                        refvalueslist.Add(parametervalues[i]);
                                    }
                                }
                                refvals = refvalueslist.ToArray();

                                return(true);
                            }
                            else// if (mi.ReturnType !=typeof(Task))
                            {
                                //output =Task.Factory.StartNew(()=>{return mi.Invoke(_methods[words[0]].O, parameters)}).ToString();// ();
                                //string output = await Task.FromResult<string>(mi.Invoke(_methods[words[0]].O, parametervalues).ToString());
                                refvals[0] = Task.FromResult(mi.Invoke(CSObject, parametervalues)).Result;
                                List <object> refvalueslist = new List <object>();
                                refvalueslist.Add(refvals[0]);
                                int index = 0;
                                foreach (ParameterInfo pi in miparameters)
                                {
                                    if (pi.IsOut || pi.ParameterType.IsByRef)
                                    {
                                        refvalueslist.Add(parametervalues[index]);
                                    }
                                    index++;
                                }
                                refvals = refvalueslist.ToArray();
                                return(true);
                            }
                        }

                        catch (Exception e)
                        {
                            Log(e.ToString(), "EXECUTE");
                            refvals[0] = "Exception logged at " + _logname;
                            return(false);
                        }
                    }
                }
                else
                {
                    refvals[0] = "No methods for " + method;
                    return(false);
                }
                //we tried to match parameters to each method, but didn't find one. so report that we didnt find matching parameter set
                StringBuilder op = new StringBuilder();

                op.Append("Mismatched parameters.");
                if (words.Length >= 1)
                {
                    op.AppendFormat("{0}", words[0]);
                }
                for (int i = 1; i < words.Length; i++)
                {
                    op.AppendFormat(", {0}", words[i]);
                }
                refvals[0] = op.ToString() + " " + Help(method);
                return(false);
            }