Exemple #1
0
        public void TokenStringBuilder_WithTokens()
        {
            // arrange
            const string configKey   = "configKey";
            const string configValue = "A";
            const string tokenKey    = "season";
            const string raw         = "  assets/{{configKey}}_{{season}}_{{ invalid  }}.png  ";
            var          context     = new GenericTokenContext(modId => false);

            context.Save(new ImmutableToken(configKey, new InvariantHashSet {
                configValue
            }));
            context.Save(new ImmutableToken(tokenKey, new InvariantHashSet {
                "A"
            }));

            // act
            TokenString      tokenStr        = new TokenString(raw, context);
            IContextualState diagnosticState = tokenStr.GetDiagnosticState();

            // assert
            tokenStr.Raw.Should().Be("assets/{{configKey}}_{{season}}_{{invalid}}.png");
            tokenStr.GetTokenPlaceholders(recursive: false).Should().HaveCount(3);
            tokenStr.GetTokenPlaceholders(recursive: false).Select(name => name.Text).Should().BeEquivalentTo(new[] { "{{configKey}}", "{{season}}", "{{invalid}}" });
            tokenStr.IsReady.Should().BeFalse();
            tokenStr.HasAnyTokens.Should().BeTrue();
            tokenStr.IsSingleTokenOnly.Should().BeFalse();
            diagnosticState.IsReady.Should().BeFalse();
            diagnosticState.UnreadyTokens.Should().BeEmpty();
            diagnosticState.InvalidTokens.Should().HaveCount(1).And.BeEquivalentTo("invalid");
            diagnosticState.Errors.Should().BeEmpty();
        }
Exemple #2
0
        public void TokenStringBuilder_WithSingleToken(string raw, string parsed)
        {
            // arrange
            const string configKey = "tokenKey";
            var          context   = new GenericTokenContext(modId => false);

            context.Save(new ImmutableToken(configKey, new InvariantHashSet {
                "value"
            }));

            // act
            TokenString      tokenStr        = new TokenString(raw, context);
            IContextualState diagnosticState = tokenStr.GetDiagnosticState();

            // assert
            tokenStr.Raw.Should().Be(parsed);
            tokenStr.GetTokenPlaceholders(recursive: false).Should().HaveCount(1);
            tokenStr.GetTokenPlaceholders(recursive: false).Select(p => p.Text).Should().BeEquivalentTo("{{" + configKey + "}}");
            tokenStr.HasAnyTokens.Should().BeTrue();
            tokenStr.IsSingleTokenOnly.Should().BeTrue();
            diagnosticState.IsReady.Should().BeTrue();
            diagnosticState.UnreadyTokens.Should().BeEmpty();
            diagnosticState.InvalidTokens.Should().BeEmpty();
            diagnosticState.Errors.Should().BeEmpty();
        }
Exemple #3
0
        /// <summary>Parse a string which can contain tokens, and validate that it's valid.</summary>
        /// <param name="rawValue">The raw string which may contain tokens.</param>
        /// <param name="tokenContext">The tokens available for this content pack.</param>
        /// <param name="migrator">The migrator which validates and migrates content pack data.</param>
        /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param>
        /// <param name="parsed">The parsed value.</param>
        private bool TryParseStringTokens(string rawValue, IContext tokenContext, IMigration migrator, out string error, out ITokenString parsed)
        {
            // parse
            parsed = new TokenString(rawValue, tokenContext);
            if (!migrator.TryMigrate(parsed, out error))
            {
                return(false);
            }

            // validate unknown tokens
            IContextualState state = parsed.GetDiagnosticState();

            if (state.InvalidTokens.Any())
            {
                error  = $"found unknown tokens ({string.Join(", ", state.InvalidTokens.OrderBy(p => p))})";
                parsed = null;
                return(false);
            }

            // validate tokens
            foreach (LexTokenToken lexToken in parsed.GetTokenPlaceholders(recursive: false))
            {
                IToken token = tokenContext.GetToken(lexToken.Name, enforceContext: false);
                if (token == null)
                {
                    error  = $"'{lexToken}' can't be used as a token because that token could not be found."; // should never happen
                    parsed = null;
                    return(false);
                }
            }

            // looks OK
            error = null;
            return(true);
        }
Exemple #4
0
        public void TokenStringBuilder_PlainString(string raw)
        {
            // act
            TokenString      tokenStr        = new TokenString(raw, new GenericTokenContext(modId => false));
            IContextualState diagnosticState = tokenStr.GetDiagnosticState();

            // assert
            tokenStr.Raw.Should().Be(raw.Trim());
            tokenStr.GetTokenPlaceholders(recursive: false).Should().HaveCount(0);
            tokenStr.HasAnyTokens.Should().BeFalse();
            diagnosticState.IsReady.Should().BeTrue();
            diagnosticState.UnreadyTokens.Should().BeEmpty();
            diagnosticState.InvalidTokens.Should().BeEmpty();
            diagnosticState.Errors.Should().BeEmpty();
        }
Exemple #5
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);
        }