Skip to content

Commit

Permalink
UI: Fixed crashes when attempting to open a file that's in use by ano…
Browse files Browse the repository at this point in the history
…ther application
  • Loading branch information
SourMesen committed Aug 22, 2023
1 parent 9538c90 commit 80f4427
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 57 deletions.
5 changes: 4 additions & 1 deletion UI/Controls/PaletteConfig.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ public void SelectPreset(object parameter)

public void LoadPaletteFile(string filename)
{
using FileStream paletteFile = File.OpenRead(filename);
using FileStream? paletteFile = FileHelper.OpenRead(filename);
if(paletteFile == null) {
return;
}

byte[] paletteFileData = new byte[LargePaletteSize * 3 + 1];
int byteCount = paletteFile.Read(paletteFileData, 0, LargePaletteSize * 3 + 1);
Expand Down
26 changes: 14 additions & 12 deletions UI/Controls/StateGridEntry.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ public void Init()
if(Path.GetExtension(game.FileName) == "." + FileDialogHelper.MesenSaveStateExt) {
img = EmuApi.GetSaveStatePreview(game.FileName);
} else {
using FileStream fs = File.Open(game.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
ZipArchive zip = new ZipArchive(fs);
ZipArchiveEntry? entry = zip.GetEntry("Screenshot.png");
if(entry != null) {
using Stream stream = entry.Open();
//Copy to a memory stream (to avoid what looks like a Skia or Avalonia issue?)
using MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);
img = new Bitmap(ms);
using FileStream? fs = FileHelper.OpenRead(game.FileName);
if(fs != null) {
ZipArchive zip = new ZipArchive(fs);
ZipArchiveEntry? entry = zip.GetEntry("Screenshot.png");
if(entry != null) {
using Stream stream = entry.Open();
//Copy to a memory stream (to avoid what looks like a Skia or Avalonia issue?)
using MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);
img = new Bitmap(ms);
}
}
}
} catch { }
Expand Down
6 changes: 4 additions & 2 deletions UI/Debugger/ViewModels/NesHeaderEditViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public NesHeaderEditViewModel()
string romPath = _romInfo.RomPath;
try {
//TODOv2, get header from core (to support for patches, etc.)
using(FileStream fileStream = File.OpenRead(romPath)) {
fileStream.Read(headerBytes, 0, 16);
using(FileStream? fileStream = FileHelper.OpenRead(romPath)) {
if(fileStream != null) {
fileStream.Read(headerBytes, 0, 16);
}
}
} catch { }

Expand Down
18 changes: 16 additions & 2 deletions UI/Utilities/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Avalonia.Threading;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -40,6 +41,13 @@ public static bool WriteAllBytes(string filepath, byte[] data)
});
}

public static FileStream? OpenRead(string filepath)
{
return AttemptOperation(() => {
return File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
});
}

private static T? AttemptOperation<T>(Func<T> action)
{
int retry = 3;
Expand All @@ -49,7 +57,13 @@ public static bool WriteAllBytes(string filepath, byte[] data)
} catch(Exception ex) {
retry--;
if(retry == 0) {
MesenMsgBox.ShowException(ex);
if(Dispatcher.UIThread.CheckAccess()) {
MesenMsgBox.ShowException(ex);
} else {
Dispatcher.UIThread.Post(() => {
MesenMsgBox.ShowException(ex);
});
}
return default;
} else {
System.Threading.Thread.Sleep(50);
Expand Down
16 changes: 9 additions & 7 deletions UI/Utilities/LoadRomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ public static async void LoadPatchFile(string patchFile)

private static bool IsPatchFile(string filename)
{
using(FileStream stream = File.OpenRead(filename)) {
byte[] header = new byte[5];
stream.Read(header, 0, 5);
if(header[0] == 'P' && header[1] == 'A' && header[2] == 'T' && header[3] == 'C' && header[4] == 'H') {
return true;
} else if((header[0] == 'U' || header[0] == 'B') && header[1] == 'P' && header[2] == 'S' && header[3] == '1') {
return true;
using(FileStream? stream = FileHelper.OpenRead(filename)) {
if(stream != null) {
byte[] header = new byte[5];
stream.Read(header, 0, 5);
if(header[0] == 'P' && header[1] == 'A' && header[2] == 'T' && header[3] == 'C' && header[4] == 'H') {
return true;
} else if((header[0] == 'U' || header[0] == 'B') && header[1] == 'P' && header[2] == 'S' && header[3] == '1') {
return true;
}
}
}
return false;
Expand Down
6 changes: 5 additions & 1 deletion UI/ViewModels/MainMenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,11 @@ private async void InstallHdPack(Window wnd)
}

try {
using(FileStream stream = File.Open(filename, FileMode.Open)) {
using(FileStream? stream = FileHelper.OpenRead(filename)) {
if(stream == null) {
return;
}

ZipArchive zip = new ZipArchive(stream);

//Find the hires.txt file
Expand Down
27 changes: 0 additions & 27 deletions UI/ViewModels/NesConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,6 @@ private void listener_OnNotification(NotificationEventArgs e)
});
}
}

public void LoadPaletteFile(string filename)
{
using(FileStream paletteFile = File.OpenRead(filename)) {
byte[] paletteFileData = new byte[512 * 3];
int byteCount = paletteFile.Read(paletteFileData, 0, 512 * 3);
if(byteCount == 64 * 3 || byteCount == 512 * 3) {
UInt32[] paletteData = new UInt32[byteCount / 3];
for(int i = 0; i < byteCount; i += 3) {
paletteData[i / 3] = ((UInt32)0xFF000000 | (UInt32)paletteFileData[i + 2] | (UInt32)(paletteFileData[i + 1] << 8) | (UInt32)(paletteFileData[i] << 16));
}
Config.UserPalette = paletteData;
}
paletteFile.Close();
}
}

public void ExportPalette(string filename)
{
List<byte> bytePalette = new List<byte>();
foreach(UInt32 value in Config.UserPalette) {
bytePalette.Add((byte)(value >> 16 & 0xFF));
bytePalette.Add((byte)(value >> 8 & 0xFF));
bytePalette.Add((byte)(value & 0xFF));
}
FileHelper.WriteAllBytes(filename, bytePalette.ToArray());
}
}

public enum NesConfigTab
Expand Down
11 changes: 6 additions & 5 deletions UI/ViewModels/UpdatePromptViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ public async Task<bool> UpdateMesen()
downloadPath += Path.GetExtension(entry.Name);
entry.ExtractToFile(downloadPath, true);

string hash;
using(SHA256 sha256 = SHA256.Create()) {
using(FileStream fileStream = File.OpenRead(downloadPath)) {
hash = BitConverter.ToString(sha256.ComputeHash(fileStream)).Replace("-", "");
}
string? hash = null;
using SHA256 sha256 = SHA256.Create();
using FileStream? fileStream = FileHelper.OpenRead(downloadPath);
if(fileStream != null) {
hash = BitConverter.ToString(sha256.ComputeHash(fileStream)).Replace("-", "");
}

if(hash != _updateInfo.Hash) {
File.Delete(downloadPath);
}
Expand Down

0 comments on commit 80f4427

Please sign in to comment.