Startup: Only overwrite existing files on disks if they don't match the embedded file

This commit is contained in:
Souryo 2017-09-12 20:52:12 -04:00
parent 6ef76ecb20
commit c6f77c8a3d
2 changed files with 36 additions and 21 deletions

View file

@ -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());

View file

@ -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)