Esempio n. 1
0
        /// <summary>Handle the 'patch parse' command.</summary>
        /// <returns>Returns whether the command was handled.</returns>
        private bool HandleParse(string[] args)
        {
            // get token string
            if (args.Length < 1 || args.Length > 2)
            {
                this.Monitor.Log("The 'patch parse' command expects one to two arguments. See 'patch help parse' for more info.", LogLevel.Error);
                return(true);
            }
            string raw   = args[0];
            string modID = args.Length >= 2 ? args[1] : null;

            // get context
            IContext context;

            try
            {
                context = this.GetContext(modID);
            }
            catch (KeyNotFoundException ex)
            {
                this.Monitor.Log(ex.Message, LogLevel.Error);
                return(true);
            }
            catch (Exception ex)
            {
                this.Monitor.Log(ex.ToString(), LogLevel.Error);
                return(true);
            }

            // parse value
            TokenString tokenStr;

            try
            {
                tokenStr = new TokenString(raw, context, new LogPathBuilder("console command"));
            }
            catch (LexFormatException ex)
            {
                this.Monitor.Log($"Can't parse that token value: {ex.Message}", LogLevel.Error);
                return(true);
            }
            catch (InvalidOperationException outerEx) when(outerEx.InnerException is LexFormatException ex)
            {
                this.Monitor.Log($"Can't parse that token value: {ex.Message}", LogLevel.Error);
                return(true);
            }
            catch (Exception ex)
            {
                this.Monitor.Log($"Can't parse that token value: {ex}", LogLevel.Error);
                return(true);
            }
            IContextualState state = tokenStr.GetDiagnosticState();

            // show result
            StringBuilder output = new StringBuilder();

            output.AppendLine();
            output.AppendLine("Metadata");
            output.AppendLine("----------------");
            output.AppendLine($"   raw value:   {raw}");
            output.AppendLine($"   ready:       {tokenStr.IsReady}");
            output.AppendLine($"   mutable:     {tokenStr.IsMutable}");
            output.AppendLine($"   has tokens:  {tokenStr.HasAnyTokens}");
            if (tokenStr.HasAnyTokens)
            {
                output.AppendLine($"   tokens used: {string.Join(", ", tokenStr.GetTokensUsed().Distinct().OrderBy(p => p, StringComparer.OrdinalIgnoreCase))}");
            }
            output.AppendLine();

            output.AppendLine("Diagnostic state");
            output.AppendLine("----------------");
            output.AppendLine($"   valid:    {state.IsValid}");
            output.AppendLine($"   in scope: {state.IsValid}");
            output.AppendLine($"   ready:    {state.IsReady}");
            if (state.Errors.Any())
            {
                output.AppendLine($"   errors:         {string.Join(", ", state.Errors)}");
            }
            if (state.InvalidTokens.Any())
            {
                output.AppendLine($"   invalid tokens: {string.Join(", ", state.InvalidTokens)}");
            }
            if (state.UnreadyTokens.Any())
            {
                output.AppendLine($"   unready tokens: {string.Join(", ", state.UnreadyTokens)}");
            }
            if (state.UnavailableModTokens.Any())
            {
                output.AppendLine($"   unavailable mod tokens: {string.Join(", ", state.UnavailableModTokens)}");
            }
            output.AppendLine();

            output.AppendLine("Result");
            output.AppendLine("----------------");
            output.AppendLine(!tokenStr.IsReady
                ? "The token string is invalid or unready."
                : $"   The token string is valid and ready. Parsed value: \"{tokenStr}\""
                              );

            this.Monitor.Log(output.ToString(), LogLevel.Debug);
            return(true);
        }