Пример #1
0
        /// <summary>
        /// Checks whether or not the wildcard matches for inCode, for example, returns true for wildcard rock-* and inCode rock-granite
        /// </summary>
        /// <param name="wildCard"></param>
        /// <param name="inCode"></param>
        /// <param name="allowedVariants"></param>
        /// <returns></returns>
        public static bool Match(AssetLocation wildCard, AssetLocation inCode, string[] allowedVariants)
        {
            // Todo: Adapt some of the fastMatch methods to save some cpu cycles here

            if (wildCard.Equals(inCode))
            {
                return(true);
            }

            int wildCardIndex;

            if (inCode == null || !wildCard.Domain.Equals(inCode.Domain) || ((wildCardIndex = wildCard.Path.IndexOf("*")) == -1 && wildCard.Path.IndexOf("(") == -1))
            {
                return(false);
            }


            // Some faster/pre checks before doing a regex, because regexes are so, sooooo sloooooow
            if (wildCardIndex == wildCard.Path.Length - 1)
            {
                if (!StringUtil.FastStartsWith(inCode.Path, wildCard.Path, wildCardIndex))
                {
                    return(false);
                }
            }
            else
            {
                if (!StringUtil.FastStartsWith(inCode.Path, wildCard.Path, wildCardIndex))
                {
                    return(false);
                }

                string pattern = Regex.Escape(wildCard.Path).Replace(@"\*", @"(.*)");
                if (!Regex.IsMatch(inCode.Path, @"^" + pattern + @"$", RegexOptions.None))
                {
                    return(false);
                }
            }

            if (allowedVariants != null)
            {
                int    wildcardStartLen = wildCard.Path.IndexOf("*");
                int    wildcardEndLen   = wildCard.Path.Length - wildcardStartLen - 1;
                string code             = inCode.Path.Substring(wildcardStartLen);
                string codepart         = code.Substring(0, code.Length - wildcardEndLen);
                if (!allowedVariants.Contains(codepart))
                {
                    return(false);
                }
            }

            return(true);
        }