Ejemplo n.º 1
0
        public static VC_PaserResult_Cmd Paser(string command)
        {
            if (string.IsNullOrEmpty(command))
            {
                errors.Add("No command specified");
                return(new VC_PaserResult_Cmd(string.Empty, string.Empty, errors.ToArray()));
            }

            //Paser for parameter
            var token = command.Split(new[] { ' ' }, 2);

            string args = token.Length == 2 ? token[1] : string.Empty;

            if (string.IsNullOrEmpty(args))
            {
                //Sytex Check for Array ...............
            }



            var result = new VC_PaserResult_Cmd(token[0], args, errors.ToArray());

            errors.Clear();
            return(result);
        }
Ejemplo n.º 2
0
        public static VC_ParserResult_Func Paser(VC_PaserResult_Cmd result, ParameterInfo[] paramterInfos)
        {
            string cmd = result.parameters;

            List <string> errors = new List <string>();
            List <object> convertedParameters = new List <object>();

            foreach (ParameterInfo paramInfo in paramterInfos)
            {
                VType vType = VTypeCollection.Get(paramInfo.ParameterType);
                if (vType == null)
                {
                    errors.Add(string.Format("No converter found for type: {0}", paramInfo.ParameterType.FullName));
                    break;
                }

                cmd = cmd.Trim();
                Match ma = Regex.Match(cmd, string.Format("^{0}", vType.Regex));

                if (String.IsNullOrWhiteSpace(ma.Value))
                {
                    if (paramInfo.IsOptional)
                    {
                        convertedParameters.Add(paramInfo.DefaultValue);
                        continue;
                    }
                    errors.Add("Invalid parameters");
                    break;
                }
                else
                {
                    try
                    {
                        convertedParameters.Add(vType.ConvertFrom(ma.Value));
                    }
                    catch (Exception ex)
                    {
                        errors.Add(string.Format("Failed to convert '{0}' to type {1}.{2}{3}", ma.Value, paramInfo.ParameterType.Name, Environment.NewLine, ex.Message));
                        break;
                    }

                    //
                    cmd = cmd.Substring(ma.Length);
                }
            }

            if (cmd.TrimEnd().Length > 0 && errors.Count == 0)
            {
                errors.Add("Provided parameters do not match with the expected parameters.");
            }


            return(new VC_ParserResult_Func(convertedParameters.ToArray(), errors.ToArray()));
        }
Ejemplo n.º 3
0
        public static void Execute(string command)
        {
            if (initialed == false)
            {
                return;
            }

            if (String.IsNullOrWhiteSpace(command))
            {
                return;
            }

            VC_PaserResult_Cmd result_Cmd = VC_Parser_Cmd.Paser(command);

            if (!result_Cmd.Valid)
            {
                Log(new VC_Log(LogTypes.Error, command + string.Join(Environment.NewLine, result_Cmd.errors)));
                return;
            }

            VC_FunctionInfo funcInfo;

            if (!VC_FunctionInfo.functionInfos.TryGetValue(result_Cmd.functionAlia, out funcInfo))
            {
                Log(new VC_Log(LogTypes.Error, command + string.Format("{0} is not a valid command.", result_Cmd.functionAlia)));
                return;
            }

            ParameterInfo[] methodParams = funcInfo.Method.GetParameters();
            var             result_Func  = VC_Parser_Func.Paser(result_Cmd, methodParams);

            if (!result_Func.Valid)
            {
                string error = string.Join(Environment.NewLine, result_Func.errors);
                Log(LogTypes.Error, command, error);
                return;
            }

            int minParamsCount = methodParams.Count(p => p.IsOptional);
            var parserRes      = result_Func.parameters;

            if (parserRes.Length < minParamsCount)
            {
                Log(LogTypes.Error, command, "Minimum parameters required: {0}{1}Got: {2}", minParamsCount, Environment.NewLine, parserRes.Length);
                return;
            }

            //Invoke Static Function
            funcInfo.Method.Invoke(null, result_Func.parameters);

            Log(LogTypes.Log, command);
        }