예제 #1
0
        private bool TryExecuteSingleCommand(string commandLine, bool manualMode = false)
        {
            IList <string> tokensList = CCommandTokenizer.Tokenize(commandLine);

            if (tokensList.Count > 0)
            {
                string commandName = tokensList[0];

                CCommand command = CRegistery.FindCommand(commandName);
                if (command != null)
                {
                    command.Delegate      = m_delegate;
                    command.IsManualMode  = manualMode;
                    command.CommandString = commandLine;
                    bool succeed = command.ExecuteTokens(tokensList, commandLine);
                    command.Clear();

                    return(succeed);
                }

                if (manualMode)
                {
                    m_delegate.LogTerminal(CStringUtils.C(commandName + ": command not found", CColorCode.ErrorUnknownCommand));
                }
            }

            return(false);
        }
예제 #2
0
 internal static string C(string str, Color color, int startIndex, int endIndex)
 {
     return(CStringUtils.TryFormat("{0}<color=#{1}>{2}</color>{3}", str.Substring(0, startIndex),
                                   ToHexValue(ref color),
                                   str.Substring(startIndex, endIndex - startIndex),
                                   str.Substring(endIndex)
                                   ));
 }
예제 #3
0
        public static IList <CBinding> List(string prefix = null) // TODO: unit tests
        {
            if (prefix != null)
            {
                return(List(delegate(CBinding binding) {
                    return CStringUtils.StartsWithIgnoreCase(binding.shortCut.ToString(), prefix);
                }));
            }

            return(m_bindings);
        }
예제 #4
0
        public static string NextArg(CIterator <string> iter)
        {
            if (iter.HasNext())
            {
                string arg = CStringUtils.UnArg(iter.Next());
                if (!IsValidArg(arg))
                {
                    throw new CCommandException("Invalid arg: " + arg);
                }

                return(arg);
            }

            throw new CCommandException("Unexpected end of args");
        }
예제 #5
0
        internal static string Arg(string value)
        {
            if (value != null && value.Length > 0)
            {
                value = value.Replace(Quote, EscapedQuote);
                value = value.Replace(SingleQuote, EscapedSingleQuote);

                if (value.IndexOf(' ') != -1)
                {
                    value = CStringUtils.TryFormat("\"{0}\"", value);
                }

                return(value);
            }

            return("\"\"");
        }
예제 #6
0
        public IDictionary <string, object> ListPreferences(string token = null)
        {
            if (string.IsNullOrEmpty(token))
            {
                return(m_data);
            }

            IDictionary <string, object> data = new Dictionary <string, object>();

            foreach (KeyValuePair <string, object> e in m_data)
            {
                if (CStringUtils.StartsWithIgnoreCase(e.Key, token))
                {
                    data[e.Key] = e.Value;
                }
            }

            return(data);
        }
예제 #7
0
        public override void PrintUsage(bool showDescription = false)
        {
            StringBuilder buffer = new StringBuilder();

            // description
            if (showDescription && this.Description != null)
            {
                buffer.AppendFormat("  {0}\n", this.Description);
            }

            string argsUsage = GetArgsUsage();

            string name = CStringUtils.C(this.Name, CColorCode.TableCommand);

            buffer.AppendFormat("  usage: {0}", name);
            buffer.Append(argsUsage);

            Print(buffer.ToString());
        }
예제 #8
0
        internal static bool ShouldListCommand(CCommand cmd, string prefix, CCommandListOptions options = CCommandListOptions.None)
        {
            if (cmd.IsDebug && (options & CCommandListOptions.Debug) == 0)
            {
                return(false);
            }

            if (cmd.IsSystem && (options & CCommandListOptions.System) == 0)
            {
                return(false);
            }

            if (cmd.IsHidden && (options & CCommandListOptions.Hidden) == 0)
            {
                return(false);
            }

            return(prefix == null || CStringUtils.StartsWithIgnoreCase(cmd.Name, prefix));
        }
예제 #9
0
        //////////////////////////////////////////////////////////////////////////////

        #region Command buffer

        public bool TryExecute(string commandLine, bool manualMode = false)
        {
            try
            {
                IList <string> commandList = CCommandSplitter.Split(commandLine);
                for (int commandIndex = 0; commandIndex < commandList.Count; ++commandIndex)
                {
                    if (!TryExecuteSingleCommand(commandList[commandIndex], manualMode))
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                m_delegate.LogTerminal(CStringUtils.C(e.Message, CColorCode.ErrorUnknownCommand));
                m_delegate.LogTerminal(CStringUtils.C(e.StackTrace, CColorCode.ErrorUnknownCommand));
                return(false);
            }

            return(true);
        }
예제 #10
0
 public static bool ShouldListMethod(MethodInfo m, string prefix)
 {
     return(CStringUtils.StartsWithIgnoreCase(m.Name, prefix));
 }
예제 #11
0
 public static string I(string str)
 {
     return(CStringUtils.TryFormat("<i>{0}</i>", str));
 }
예제 #12
0
 public static string B(string str)
 {
     return(CStringUtils.TryFormat("<b>{0}</b>", str));
 }
예제 #13
0
 public static string C(string str, Color color)
 {
     return(CStringUtils.TryFormat("<color=#{0}>{1}</color>", ToHexValue(ref color), str));
 }
예제 #14
0
 internal static string C(string str, CColorCode color)
 {
     return(CStringUtils.TryFormat("<color=${0}>{1}</color>", ((int)color).ToString(), str));
 }
예제 #15
0
        bool Execute(string[] args)
        {
            if (args.Length == 0)
            {
                PrintIndent("{0} is:\"{1}\" default:\"{2}\"", CStringUtils.C(cvar.Name, CColorCode.TableVar), cvar.Value, cvar.DefaultValue);
                return(false);
            }

            switch (cvar.Type)
            {
            case CVarType.Boolean:
            {
                if (args.Length != 1)
                {
                    PrintError("Unexpected args count");
                    return(false);
                }

                int value;
                if (int.TryParse(args[0], out value) && (value == 0 || value == 1))
                {
                    SetValue(value != 0);
                    return(true);
                }

                PrintError("Invalid value: only '0' and '1' are permitted", args[0]);
                return(false);
            }

            case CVarType.Integer:
            {
                if (args.Length != 1)
                {
                    PrintError("Unexpected args count");
                    return(false);
                }

                int value;
                if (int.TryParse(args[0], out value))
                {
                    SetValue(value);
                    return(true);
                }

                PrintError("Invalid value");
                return(false);
            }

            case CVarType.Float:
            {
                if (args.Length != 1)
                {
                    PrintError("Unexpected args count");
                    return(false);
                }

                float value;
                if (float.TryParse(args[0], out value))
                {
                    SetValue(value);
                    return(true);
                }

                PrintError("Invalid value");
                return(false);
            }

            case CVarType.String:
            {
                if (args.Length != 1)
                {
                    PrintError("Unexpected args count");
                    return(false);
                }

                SetValue(args[0]);
                return(true);
            }

            case CVarType.Color:
            {
                if (args.Length == 3 || args.Length == 4)
                {
                    float[] values = CStringUtils.ParseFloats(args);
                    if (values == null)
                    {
                        PrintError("Invalid values");
                        return(false);
                    }

                    for (int i = 0; i < values.Length; ++i)
                    {
                        if (values[i] < 0.0f || values[i] > 1.0f)
                        {
                            PrintError("Invalid values");
                            return(false);
                        }
                    }

                    Color color = args.Length == 3 ?
                                  new Color(values[0], values[1], values[2]) :
                                  new Color(values[0], values[1], values[2], values[3]);

                    SetValue(ref color);
                    return(true);
                }

                if (args.Length == 1)
                {
                    if (colorRegex == null)
                    {
                        colorRegex = new Regex("^0[xX]([\\dabcdefABCDEF]{1,8})$");
                    }

                    Match match = colorRegex.Match(args[0]);
                    if (!match.Success)
                    {
                        PrintError("Invalid color value: expected 0xRGB[A]");
                        return(false);
                    }

                    string value      = match.Groups[1].Value;
                    uint   colorValue = Convert.ToUInt32(value, 16);

                    if (value.Length == 8)
                    {
                        Color color = CColorUtils.FromRGBA(colorValue);
                        SetValue(ref color);
                    }
                    else
                    {
                        Color color = CColorUtils.FromRGB(colorValue);
                        SetValue(ref color);
                    }

                    return(true);
                }

                PrintError("Unexpected args count: expected R G B A or R G B or 0xRGB[A]");
                return(false);
            }

            case CVarType.Rect:
            {
                if (args.Length != 4)
                {
                    PrintError("Unexpected args count: expected X Y W H");
                    return(false);
                }

                float[] values = CStringUtils.ParseFloats(args);
                if (values == null)
                {
                    PrintError("Invalid values");
                    return(false);
                }

                Rect rect = new Rect(values[0], values[1], values[2], values[3]);
                SetValue(ref rect);

                return(true);
            }

            case CVarType.Vector2:
            {
                if (args.Length != 2)
                {
                    PrintError("Unexpected args count: expected X Y");
                    return(false);
                }

                float[] values = CStringUtils.ParseFloats(args);
                if (values == null)
                {
                    PrintError("Invalid values");
                    return(false);
                }

                Vector2 vector = new Vector2(values[0], values[1]);
                SetValue(ref vector);

                return(true);
            }

            case CVarType.Vector3:
            {
                if (args.Length != 2 && args.Length != 3)
                {
                    PrintError("Unexpected args count: expected X Y or X Y Z");
                    return(false);
                }

                float[] values = CStringUtils.ParseFloats(args);
                if (values == null)
                {
                    PrintError("Invalid values");
                    return(false);
                }

                if (values.Length == 2)
                {
                    Vector3 vector = new Vector3(values[0], values[1]);
                    SetValue(ref vector);
                }
                else
                {
                    Vector3 vector = new Vector3(values[0], values[1], values[2]);
                    SetValue(ref vector);
                }

                return(true);
            }

            case CVarType.Vector4:
            {
                if (args.Length < 2 || args.Length > 4)
                {
                    PrintError("Unexpected args count: expected X Y or X Y Z or X Y Z W");
                    return(false);
                }

                float[] values = CStringUtils.ParseFloats(args);
                if (values == null)
                {
                    PrintError("Invalid values");
                    return(false);
                }

                if (values.Length == 2)
                {
                    Vector4 vector = new Vector4(values[0], values[1]);
                    SetValue(ref vector);
                }
                else if (values.Length == 3)
                {
                    Vector4 vector = new Vector4(values[0], values[1], values[2]);
                    SetValue(ref vector);
                }
                else
                {
                    Vector4 vector = new Vector4(values[0], values[1], values[2], values[3]);
                    SetValue(ref vector);
                }

                return(true);
            }
            }

            if (args.Length != 1)
            {
                Print("usage ");
                return(false);
            }

            if (cvar.IsFloat)
            {
                float value;
                if (float.TryParse(args[0], out value))
                {
                    SetValue(value);
                    return(true);
                }

                PrintError("Invalid float \"{0}\"", args[0]);
                return(false);
            }

            if (cvar.IsInt)
            {
                int value;
                if (int.TryParse(args[0], out value))
                {
                    if (cvar.IsBool && value != 0 && value != 1)
                    {
                        PrintError("Invalid value \"{0}\" only \"0\" and \"1\" are permitted", args[0]);
                        return(false);
                    }

                    SetValue(value);
                    return(true);
                }

                PrintError("Invalid int \"{0}\"", args[0]);
                return(false);
            }

            string str = args[0];

            SetValue(str);
            return(true);
        }