Exemplo n.º 1
0
        /// <summary>
        /// Gets the patch for limited visibility nodes for a file with specified path.
        /// </summary>
        /// <param name="path">File path.</param>
        /// <returns>Patch structure.</returns>
        private static Patch GetNodePatch(string path)
        {
            if (!File.Exists(path))
            {
                return(new Patch());
            }
            var t = new Text8(Encoding);

            t.Load(path);
            int i, offset = 0, n = t.Length;

            Text8.Match m;
            var         replacements = new List <Text8.Replacement>();
            bool        isIdentity = false, isSound = false;
            int         soundOffset, endOffset;

            while (offset < n)
            {
                i = t.FindSequence(SeqNode, offset);
                if (i < 0)
                {
                    break;
                }
                offset = i + NodeSequenceLength;
                m      = t.FindSeparator(offset);
                if (m.Offset != offset)
                {
                    break;
                }
                offset += m.Length;
                m       = t.FindNumber(offset);
                if (m.Offset != offset)
                {
                    break;
                }
                isIdentity = new ArraySegment <byte>(t, m.Offset, m.Length).SequenceEqual(SeqNone);
                endOffset  = t.FindSequence(SeqEnd, offset, 255);
                if (endOffset > 0)
                {
                    soundOffset = t.FindSequence(SeqSound, offset, endOffset - offset);
                    isSound     = soundOffset > 0;
                }
                if (!isIdentity && !isSound)
                {
                    replacements.Add(new Text8.Replacement {
                        Offset = m.Offset, Length = m.Length, Data = SeqNone
                    });
                }
                offset += m.Length;
            }
            if (replacements.Count < 1)
            {
                return(new Patch());
            }
            return(new Patch {
                IsNotEmpty = true, Path = path, Replacements = replacements
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Applies a specified patch.
        /// </summary>
        /// <param name="patch"><see cref="Patch"/> structure containing path and a list of replacements to make.</param>
        public static void ApplyPatch(Patch patch)
        {
            var file       = new Text8();
            var backupPath = patch.Path.Replace(TargetPath, BackupPath);
            var backupDir  = Path.GetDirectoryName(backupPath);

            if (!Directory.Exists(backupDir))
            {
                Directory.CreateDirectory(backupDir);
            }
            file.Load(patch.Path);
            file.Save(backupPath);
            file.Replace(patch.Replacements);
            file.Save(patch.Path);
        }