Exemple #1
0
        private static async Task UpdateFileAsync(FileUpdateInfo fileUpdate, FactorioVersion versionToUpdate, ZipArchive archive, string packageDirectory)
        {
            await Task.Run(() =>
            {
                var file = new FileInfo(versionToUpdate.ExpandPathVariables(fileUpdate.Path));
                if (!file.Exists)
                {
                    throw new CriticalUpdaterException(UpdaterErrorType.FileNotFound);
                }

                uint oldCrc = file.CalculateCrc();
                if (oldCrc != fileUpdate.OldCrc)
                {
                    throw new CriticalUpdaterException(UpdaterErrorType.ChecksumMismatch);
                }

                string entryPath = fileUpdate.Path;
                if (!string.IsNullOrEmpty(packageDirectory))
                {
                    entryPath = string.Join("/", packageDirectory, entryPath);
                }
                entryPath = ResolveArchivePath(entryPath);
                var entry = archive.GetEntry(entryPath);

                using (var output = new MemoryStream())
                {
                    using (var input = file.OpenRead())
                    {
                        using (var patch = new MemoryStream())
                        {
                            using (var source = entry.Open())
                                source.CopyTo(patch);
                            patch.Position = 0;

                            var decoder = new Decoder(input, patch, output);
                            decoder.Run();
                        }
                    }

                    output.Position = 0;
                    using (var destination = file.Open(FileMode.Truncate, FileAccess.Write))
                        output.CopyTo(destination);
                }

                uint newCrc = file.CalculateCrc();
                if (newCrc != fileUpdate.NewCrc)
                {
                    throw new CriticalUpdaterException(UpdaterErrorType.ChecksumMismatch);
                }
            });
        }
Exemple #2
0
        private bool IsFilesDifferent(string leftFile, string rightFile, bool onlySelect = false)
        {
            BMTools.BmDebug.Debug.InfoAsync("compare type=", Settings.CompareType);

            if (Settings.CompareType != CompareTypes.None)
            {
                try
                {
                    BMTools.BmDebug.Debug.InfoAsync("lf=", leftFile, "rf=", rightFile);
                    var lf = new FileInfo(leftFile);
                    var rf = new FileInfo(rightFile);
                    if (Settings.CompareType == CompareTypes.Size)
                    {
                        BMTools.BmDebug.Debug.InfoAsync("lf.Length=", lf.Length, "rf.Length=", rf.Length);
                        if (lf.Length != rf.Length)
                        {
                            return(true);
                        }
                    }
                    else if (Settings.CompareType == CompareTypes.Date)
                    {
                        BMTools.BmDebug.Debug.InfoAsync("lf.LastWriteTime=", lf.LastWriteTime, "rf.LastWriteTime=",
                                                        rf.LastWriteTime);
                        //BMTools.BmDebug.Info("lf=", lf.CreationTime, "rf=", rf.CreationTime);
                        //BMTools.BmDebug.Info("lf=", lf.LastAccessTime, "rf=", rf.LastAccessTime);
                        if (lf.LastWriteTime.Date.Year != rf.LastWriteTime.Date.Year ||
                            lf.LastWriteTime.Date.Day != rf.LastWriteTime.Date.Day ||
                            lf.LastWriteTime.Date.Hour != rf.LastWriteTime.Date.Hour ||
                            lf.LastWriteTime.Date.Minute != rf.LastWriteTime.Date.Minute ||
                            lf.LastWriteTime.Date.Second != rf.LastWriteTime.Date.Second
                            )
                        {
                            return(true);
                        }
                    }
                    else if (Settings.CompareType == CompareTypes.Crc)
                    {
                        var crcL = lf.CalculateCrc();
                        var crcR = rf.CalculateCrc();

                        BMTools.BmDebug.Debug.InfoAsync("lf.crc=", crcL, "rf.crc=", crcR);

                        if (crcL != crcR)
                        {
                            return(true);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!Settings.OnlyExistingFilesCompare)
                    {
                        return(true);
                    }
                    if (onlySelect)
                    {
                        return(true);
                    }
                    BMTools.BmDebug.Debug.CritAsyc("RunCompare ", e.Message);
                    MessageBox.Show(@"File open error=" + e.Message);
                }
            }
            return(false);
        }