示例#1
0
            public Pattern(string glob, PatternFlags flags)
            {
                Debug.Assert(glob != null);

                Glob  = glob;
                Flags = flags;
            }
示例#2
0
        /// <summary>
        /// Create an individual ignore rule for the specified pattern
        /// </summary>
        /// <param name="pattern">A glob pattern specifying file(s) this rule should ignore</param>
        /// <param name="flags">Optional flags determining pattern matching behaviour</param>
        public IgnoreRule(string pattern, MatchFlags flags = MatchFlags.PATHNAME)
        {
            if (Utils.IsNullOrWhiteSpace(pattern))
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            // Keep track of the original pattern before modifications (for display purposes)
            OriginalPattern = pattern;
            Pattern         = pattern;
            MatchFlags      = flags;

            // First, let's figure out some things about the pattern and set flags to pass to our match function
            PatternFlags = PatternFlags.NONE;

            // If the pattern starts with an exclamation mark, it's a negation pattern
            // Once we know that, we can remove the exclamation mark (so the pattern behaves just like any other),
            // then just negate the match result when we return it
            if (Pattern.StartsWith("!", sc))
            {
                PatternFlags |= PatternFlags.NEGATION;
                Pattern       = Pattern.Substring(1);
            }

            // If the pattern starts with a forward slash, it should only match an absolute path
            if (Pattern.StartsWith("/", sc))
            {
                PatternFlags |= PatternFlags.ABSOLUTE_PATH;
                Pattern       = Pattern.Substring(1);
            }

            // If the pattern ends with a forward slash, it should only match a directory
            // Again though, once we know that we can remove the slash to normalise the pattern
            if (Pattern.EndsWith("/", sc))
            {
                PatternFlags |= PatternFlags.DIRECTORY;
                Pattern       = Pattern.Substring(0, Pattern.Length - 1);
            }

            _wildcardIndex = Pattern.IndexOfAny(new char[] { '*', '[', '?' });

            // If CASEFOLD is set, string comparisons should ignore case too
            if (MatchFlags.HasFlag(MatchFlags.CASEFOLD))
            {
                sc = StringComparison.OrdinalIgnoreCase;
            }

            // If PATHNAME is set, single asterisks should not match slashes
            if (!MatchFlags.HasFlag(MatchFlags.PATHNAME))
            {
                MatchFlags |= MatchFlags.PATHNAME;
            }

            // TODO: Currently, we are just setting PATHNAME for every rule, because it seems to match the original behaviour
            // See here for a clue: https://github.com/git/git/blob/c2c5f6b1e479f2c38e0e01345350620944e3527f/dir.c#L99
        }
示例#3
0
    public static Mesh GetPatternMesh(WarriorPattern pattern, PatternFlags flags, Vector2 position, bool inverted = false)
    {
        List <Vector2> locations = pattern.GetLocationsForFlags(flags, inverted);
        int            sizeX     = GameData.CurrentBattle.Board.Width;

        Vector3[] vertices  = new Vector3[locations.Count * 4];
        int[]     triangles = new int[locations.Count * 6];
        Vector2[] uv        = new Vector2[vertices.Length];

        position = BoardUtils.BoardToWorldPosition(position);

        int count = 0;

        foreach (Vector2 location in locations)
        {
            int x = ((int)position.x + (int)location.x); //TODO
            int y = ((int)position.y - (int)location.y); //TODO

            if (!BoardUtils.IsInsideBoard(new Vector2(x, y)))
            {
                continue;
            }

            int vX = count * 4;
            vertices[vX]     = new Vector3(x, y);
            vertices[vX + 1] = new Vector3(x + 1, y);
            vertices[vX + 2] = new Vector3(x + 1, y + 1);
            vertices[vX + 3] = new Vector3(x, y + 1);

            int tX = count * 6;
            triangles[tX]     = vX;
            triangles[tX + 1] = vX + 1;
            triangles[tX + 2] = vX + 2;
            triangles[tX + 3] = vX;
            triangles[tX + 4] = vX + 2;
            triangles[tX + 5] = vX + 3;

            //TODO varies depending on the X
            float xOffset = (flags & PatternFlags.Attack) == PatternFlags.Attack ? 0.5f : 0.75f;
            uv[vX]     = new Vector2(xOffset, 0);
            uv[vX + 1] = new Vector2(xOffset + 0.25f, 0);
            uv[vX + 2] = new Vector2(xOffset + 0.25f, 1);
            uv[vX + 3] = new Vector2(xOffset, 1);

            count++;
        }

        Mesh mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.uv        = uv;
        mesh.RecalculateNormals();

        return(mesh);
    }
示例#4
0
    public List <Vector2> GetLocationsForFlags(PatternFlags flags, bool xInverted = false)
    {
        List <Vector2> locations = new List <Vector2>();

        for (int y = 0; y < PATTERN_HEIGHT; y++)
        {
            for (int x = 0; x < PATTERN_WIDTH; x++)
            {
                if ((grid[y * PATTERN_WIDTH + x] & flags) == flags)
                {
                    locations.Add(GetRelativePosition(x, y, xInverted));
                }
            }
        }
        return(locations);
    }
示例#5
0
        /// <summary>
        /// Check if a path matches the rule pattern.
        /// </summary>
        /// <param name="path">String representing the path to check.</param>
        /// <param name="pathIsDirectory">Should be set True if the path represents
        /// a directory, False if it represents a file.</param>
        /// <returns>True if the file or directory path matches the rule pattern.</returns>
        public bool IsMatch(string path, bool pathIsDirectory)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException("Path cannot be null or empty", nameof(path));
            }

            // .gitignore files use Unix paths (with a forward slash separator),
            // so make sure our input also uses forward slashes
            path = path.NormalisePath().TrimStart('/');

            // Shortcut return if the pattern is directory-only and the path isn't a directory
            // This has to be determined by the OS (at least that's the only reliable way),
            // so we pass that information in as a boolean so the consuming code can provide it
            if (PatternFlags.HasFlag(PatternFlags.DIRECTORY) && pathIsDirectory == false)
            {
                return(false);
            }

            // If the pattern is an absolute path pattern, the path must start with the part of the pattern
            // before any wildcards occur. If it doesn't, we can just return a negative match
            var patternBeforeFirstWildcard = _wildcardIndex != -1
                ? Pattern.Substring(0, _wildcardIndex)
                : Pattern;

            if (PatternFlags.HasFlag(PatternFlags.ABSOLUTE_PATH) &&
                !path.StartsWith(patternBeforeFirstWildcard, _sc))
            {
                return(false);
            }

            // If we got this far, we can't figure out the match with simple
            // string matching, so use our regex match function

            // If the *pattern* does not contain any slashes, it should match *any*
            // occurence, *anywhere* within the path (e.g. '*.jpg' should match
            // 'a.jpg', 'a/b.jpg', 'a/b/c.jpg'), so try matching before each slash
            if (!Pattern.Contains("/") && path.Contains("/"))
            {
                return(path.Split('/').Any(segment => Matcher.TryMatch(_rx, segment)));
            }

            // If the *path* doesn't contain any slashes, we should skip over the conditional above
            return(Matcher.TryMatch(_rx, path));
        }
示例#6
0
    public static GameObject GetVisualObject(this WarriorPattern pattern, PatternFlags flags, Vector2 referencePosition, bool inverted = false)
    {
        GameObject patternObject = new GameObject("_PATTERN_" + flags.ToString());

        patternObject.transform.position = new Vector3(patternObject.transform.position.x, patternObject.transform.position.y, -5); //TODO

        MeshFilter meshFilter = (MeshFilter)patternObject.AddComponent(typeof(MeshFilter));

        meshFilter.mesh = MeshGenerator.GetPatternMesh(pattern, flags, referencePosition, inverted);

        MeshRenderer renderer = patternObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

        renderer.material.shader = Shader.Find("Sprites/Default");

        Texture2D texture = (Texture2D)Resources.Load("Sprites/board");

        renderer.material.mainTexture = texture;

        return(patternObject);
    }
示例#7
0
 public void SetFlagsAt(int x, int y, PatternFlags flags)
 {
     grid [GetIndexFromRelativePosition(x, y)] = flags;
 }
示例#8
0
 public Pattern(string glob, PatternFlags flags)
 {
     Glob  = glob;
     Flags = flags;
 }
示例#9
0
        internal static bool TryParsePattern(string line, StringBuilder reusableBuffer, [NotNullWhen(true)] out string?glob, out PatternFlags flags)
        {
            glob  = null;
            flags = PatternFlags.None;

            // Trailing spaces are ignored unless '\'-escaped.
            // Leading spaces are significant.
            // Other whitespace (\t, \v, \f) is significant.
            int e = line.Length - 1;

            while (e >= 0 && line[e] == ' ')
            {
                e--;
            }

            e++;

            // Skip blank line.
            if (e == 0)
            {
                return(false);
            }

            // put trailing space back if escaped:
            if (e < line.Length && line[e] == ' ' && line[e - 1] == '\\')
            {
                e++;
            }

            int s = 0;

            // Skip comment.
            if (line[s] == '#')
            {
                return(false);
            }

            // Pattern negation.
            if (line[s] == '!')
            {
                flags |= PatternFlags.Negative;
                s++;
            }

            if (s == e)
            {
                return(false);
            }

            if (line[e - 1] == '/')
            {
                flags |= PatternFlags.DirectoryPattern;
                e--;
            }

            if (s == e)
            {
                return(false);
            }

            if (line.IndexOf('/', s, e - s) >= 0)
            {
                flags |= PatternFlags.FullPath;
            }

            if (line[s] == '/')
            {
                s++;
            }

            if (s == e)
            {
                return(false);
            }

            int escape = line.IndexOf('\\', s, e - s);

            if (escape < 0)
            {
                glob = line.Substring(s, e - s);
                return(true);
            }

            reusableBuffer.Clear();
            reusableBuffer.Append(line, s, escape - s);

            int i = escape;

            while (i < e)
            {
                var c = line[i++];
                if (c == '\\' && i < e)
                {
                    c = line[i++];
                }

                reusableBuffer.Append(c);
            }

            glob = reusableBuffer.ToString();
            return(true);
        }
示例#10
0
 public GitIgnorePattern(string pattern, PatternFlags flags)
 {
     Pattern = pattern;
     Flags = flags;
 }