From c6f77c8a3dd991ccf788d82336fd403cc63d7b66 Mon Sep 17 00:00:00 2001 From: Souryo Date: Tue, 12 Sep 2017 20:52:12 -0400 Subject: [PATCH] Startup: Only overwrite existing files on disks if they don't match the embedded file --- GUI.NET/Forms/frmUpdatePrompt.cs | 15 +----------- GUI.NET/ResourceManager.cs | 42 ++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/GUI.NET/Forms/frmUpdatePrompt.cs b/GUI.NET/Forms/frmUpdatePrompt.cs index dd129681..41e2b7af 100644 --- a/GUI.NET/Forms/frmUpdatePrompt.cs +++ b/GUI.NET/Forms/frmUpdatePrompt.cs @@ -72,7 +72,7 @@ namespace Mesen.GUI.Forms frmDownloadProgress frmDownload = new frmDownloadProgress("http://www.mesen.ca/Services/GetLatestVersion.php?a=download&p=win&v=" + InteropEmu.GetMesenVersion(), srcFilePath); if(frmDownload.ShowDialog() == DialogResult.OK) { FileInfo fileInfo = new FileInfo(srcFilePath); - if(fileInfo.Length > 0 && GetSha1Hash(srcFilePath) == _fileHash) { + if(fileInfo.Length > 0 && ResourceManager.GetSha1Hash(File.ReadAllBytes(srcFilePath)) == _fileHash) { if(Program.IsMono) { Process.Start("mono", string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", updateHelper, srcFilePath, destFilePath, backupFilePath)); } else { @@ -88,19 +88,6 @@ namespace Mesen.GUI.Forms #endif } - private string GetSha1Hash(string filename) - { - using(SHA1Managed sha1 = new SHA1Managed()) { - byte[] hash = sha1.ComputeHash(File.ReadAllBytes(filename)); - - var sb = new StringBuilder(hash.Length * 2); - foreach(byte b in hash) { - sb.Append(b.ToString("x2")); - } - return sb.ToString(); - } - } - private void picDonate_Click(object sender, EventArgs e) { Process.Start("http://www.mesen.ca/Donate.php?l=" + ResourceHelper.GetLanguageCode()); diff --git a/GUI.NET/ResourceManager.cs b/GUI.NET/ResourceManager.cs index 6dbd80e3..6bc80414 100644 --- a/GUI.NET/ResourceManager.cs +++ b/GUI.NET/ResourceManager.cs @@ -7,24 +7,52 @@ using System.Text; using System.Threading.Tasks; using Mesen.GUI.Config; using System.IO.Compression; +using System.Security.Cryptography; namespace Mesen.GUI { class ResourceManager { + public static string GetSha1Hash(byte[] fileData) + { + using(SHA1Managed sha1 = new SHA1Managed()) { + byte[] hash = sha1.ComputeHash(fileData); + + var sb = new StringBuilder(hash.Length * 2); + foreach(byte b in hash) { + sb.Append(b.ToString("x2")); + } + return sb.ToString(); + } + } + private static void ExtractFile(ZipArchiveEntry entry, string outputFilename) { if(File.Exists(outputFilename)) { + byte[] zipFileData = new byte[entry.Length]; + using(Stream fileStream = entry.Open()) { + fileStream.Read(zipFileData, 0, (int)entry.Length); + } + + string diskFileSha1 = GetSha1Hash(File.ReadAllBytes(outputFilename)); + string zipFileSha1 = GetSha1Hash(zipFileData); + + if(diskFileSha1 != zipFileSha1) { + try { + File.Delete(outputFilename); + } catch { } + try { + File.WriteAllBytes(outputFilename, zipFileData); + } catch { } + } + } else { try { - File.Delete(outputFilename); + //On Mono, using overwrite = true for ExtractToFile crashes/kills any currently running instance that uses the file. + //This is probably a Mono bug? + //Better to attempt a delete & then extract, like now (and like it used to be) + entry.ExtractToFile(outputFilename); } catch { } } - try { - //On Mono, using overwrite = true for ExtractToFile crashes/kills any currently running instance that uses the file. - //This is probably a Mono bug? - //Better to attempt a delete & then extract, like now (and like it used to be) - entry.ExtractToFile(outputFilename); - } catch { } } public static Stream GetZippedResource(string filename)