Beispiel #1
0
        /// <summary>Parse a boolean <see cref="PatchConfig.Enabled"/> value from 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 TryParseEnabled(string rawValue, IContext tokenContext, IMigration migrator, out string error, out bool parsed)
        {
            parsed = false;

            // analyse string
            if (!this.TryParseStringTokens(rawValue, tokenContext, migrator, out error, out ITokenString tokenString))
            {
                return(false);
            }

            // validate & extract tokens
            string text = rawValue;

            if (tokenString.HasAnyTokens)
            {
                // only one token allowed
                if (!tokenString.IsSingleTokenOnly)
                {
                    error = "can't be treated as a true/false value because it contains multiple tokens.";
                    return(false);
                }

                // parse token
                LexTokenToken lexToken = tokenString.GetTokenPlaceholders(recursive: false).Single();
                IToken        token    = tokenContext.GetToken(lexToken.Name, enforceContext: false);
                ITokenString  input    = new TokenString(lexToken.InputArg, tokenContext);

                // check token options
                InvariantHashSet allowedValues = token?.GetAllowedValues(input);
                if (token == null || token.IsMutable || !token.IsReady)
                {
                    error = $"can only use static tokens in this field, consider using a {nameof(PatchConfig.When)} condition instead.";
                    return(false);
                }
                if (allowedValues == null || !allowedValues.All(p => bool.TryParse(p, out _)))
                {
                    error = "that token isn't restricted to 'true' or 'false'.";
                    return(false);
                }
                if (token.CanHaveMultipleValues(input))
                {
                    error = "can't be treated as a true/false value because that token can have multiple values.";
                    return(false);
                }

                text = token.GetValues(input).First();
            }

            // parse text
            if (!bool.TryParse(text, out parsed))
            {
                error = $"can't parse {tokenString.Raw} as a true/false value.";
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <summary>Parse a boolean <see cref="PatchConfig.Enabled"/> value from 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="error">An error phrase indicating why parsing failed (if applicable).</param>
        /// <param name="parsed">The parsed value.</param>
        private bool TryParseEnabled(string rawValue, IContext tokenContext, out string error, out bool parsed)
        {
            parsed = false;

            // analyse string
            if (!this.TryParseTokenString(rawValue, tokenContext, out error, out TokenString tokenString))
            {
                return(false);
            }

            // validate & extract tokens
            string text = rawValue;

            if (tokenString.HasAnyTokens)
            {
                // only one token allowed
                if (!tokenString.IsSingleTokenOnly)
                {
                    error = "can't be treated as a true/false value because it contains multiple tokens.";
                    return(false);
                }

                // check token options
                TokenName        tokenName     = tokenString.Tokens.First();
                IToken           token         = tokenContext.GetToken(tokenName, enforceContext: false);
                InvariantHashSet allowedValues = token?.GetAllowedValues(tokenName);
                if (token == null || token.IsMutable || !token.IsValidInContext)
                {
                    error = $"can only use static tokens in this field, consider using a {nameof(PatchConfig.When)} condition instead.";
                    return(false);
                }
                if (allowedValues == null || !allowedValues.All(p => p == true.ToString() || p == false.ToString()))
                {
                    error = "that token isn't restricted to 'true' or 'false'.";
                    return(false);
                }
                if (token.CanHaveMultipleValues(tokenName))
                {
                    error = "can't be treated as a true/false value because that token can have multiple values.";
                    return(false);
                }

                text = token.GetValues(tokenName).First();
            }

            // parse text
            if (!bool.TryParse(text, out parsed))
            {
                error = $"can't parse {tokenString.Raw} as a true/false value.";
                return(false);
            }
            return(true);
        }