コード例 #1
0
        public static IncrementalPatchInfo DeserializeXMLToIncrementalPatchInfo(string xmlContent)
        {
            var serializer = new XmlSerializer(typeof(IncrementalPatchInfo));

            using (var stream = new StringReader(xmlContent))
            {
                IncrementalPatchInfo result = serializer.Deserialize(stream) as IncrementalPatchInfo;
                if (result != null)
                {
                    // Always use Path.DirectorySeparatorChar
                    for (int i = 0; i < result.RenamedFiles.Count; i++)
                    {
                        PatchRenamedItem renamedItem = result.RenamedFiles[i];
                        renamedItem.BeforePath = renamedItem.BeforePath.Replace(AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
                        renamedItem.AfterPath  = renamedItem.AfterPath.Replace(AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
                    }

                    for (int i = 0; i < result.Files.Count; i++)
                    {
                        PatchItem patchItem = result.Files[i];
                        patchItem.Path = patchItem.Path.Replace(AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
                    }
                }

                return(result);
            }
        }
コード例 #2
0
        private bool ApplyDiffToFile(string filePath, string diffPath, string targetPath, int patchItemIndex)
        {
            PatchItem item = patchInfo.Files[patchItemIndex];

            FileInfo targetFile = new FileInfo(targetPath);

            if (targetFile.Exists && targetFile.MatchesSignature(item.AfterFileSize, item.AfterMd5Hash))
            {
                comms.Log(Localization.Get(StringId.AlreadyUpToDateXthFile, patchItemIndex + 1, patchInfo.Files.Count, item.Path));
                return(true);
            }

            if (item.BeforeFileSize == 0L)
            {
                comms.Log(Localization.Get(StringId.CreatingXthFile, patchItemIndex + 1, patchInfo.Files.Count, item.Path));

                Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                File.Copy(diffPath, targetPath, true);
                File.Delete(diffPath);

                return(true);
            }

            FileInfo localFile = new FileInfo(filePath);

            if (!localFile.Exists || !localFile.MatchesSignature(item.BeforeFileSize, item.BeforeMd5Hash))
            {
                return(false);
            }

            comms.Log(Localization.Get(StringId.UpdatingXthFile, patchItemIndex + 1, patchInfo.Files.Count, item.Path));

            FilePatchProgress progress       = null;
            string            tempOutputPath = diffPath + "_.tmp";

            if (comms.LogProgress)
            {
                progress = new FilePatchProgress(Path.GetFileName(filePath));
                comms.SetProgress(progress);
            }

            OctoUtils.ApplyDelta(filePath, tempOutputPath, diffPath, progress);

            FileInfo updatedFile = new FileInfo(tempOutputPath);

            if (!updatedFile.Exists || !updatedFile.MatchesSignature(item.AfterFileSize, item.AfterMd5Hash))
            {
                return(false);
            }

            Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
            File.Copy(tempOutputPath, targetPath, true);
            File.Delete(tempOutputPath);
            File.Delete(diffPath);

            return(true);
        }