From 201020b6f9a2f7735b984ffcc14e9aaf18c86c22 Mon Sep 17 00:00:00 2001 From: Sour Date: Mon, 14 Oct 2019 15:04:26 -0400 Subject: [PATCH] Updates: Avoid Application.Exit usage, attempt to kill existing processes before upgrade --- UI/Config/Configuration.cs | 2 +- UI/Config/PreferencesConfig.cs | 2 +- UI/Controls/ctrlRecentGames.cs | 2 +- UI/Emulation/CursorManager.cs | 4 ++-- UI/Emulation/EmuRunner.cs | 2 +- UI/Emulation/SaveStateManager.cs | 4 ++-- UI/Emulation/ShortcutHandler.cs | 4 ++-- UI/Forms/MesenMsgBox.cs | 2 +- UI/Forms/Tools/frmCheatList.cs | 2 +- UI/Forms/frmConfigWizard.cs | 2 +- UI/Forms/frmMain.cs | 4 ++++ UI/Forms/frmSelectRom.cs | 2 +- UI/Interop/EmuApi.cs | 2 +- UI/Program.cs | 1 - UI/Updates/UpdateHelper.cs | 6 +++--- UI/Updates/frmUpdatePrompt.cs | 27 ++++++++++++++------------- UI/Utilities/FirmwareHelper.cs | 2 +- UpdateHelper/Program.cs | 12 ++++++++++++ 18 files changed, 49 insertions(+), 33 deletions(-) diff --git a/UI/Config/Configuration.cs b/UI/Config/Configuration.cs index e622bc8..e170634 100644 --- a/UI/Config/Configuration.cs +++ b/UI/Config/Configuration.cs @@ -14,7 +14,7 @@ namespace Mesen.GUI.Config { private bool _needToSave = false; - public string Version = "0.1.0"; + public string Version = "0.2.0"; public VideoConfig Video; public AudioConfig Audio; public InputConfig Input; diff --git a/UI/Config/PreferencesConfig.cs b/UI/Config/PreferencesConfig.cs index 58679d7..34a6ea7 100644 --- a/UI/Config/PreferencesConfig.cs +++ b/UI/Config/PreferencesConfig.cs @@ -138,7 +138,7 @@ namespace Mesen.GUI.Config FileAssociationHelper.UpdateFileAssociation("mss", this.AssociateMssFiles); } - Application.OpenForms[0].TopMost = AlwaysOnTop; + frmMain.Instance.TopMost = AlwaysOnTop; ShortcutKeyInfo[] shortcutKeys = new ShortcutKeyInfo[ShortcutKeys1.Count + ShortcutKeys2.Count]; int i = 0; diff --git a/UI/Controls/ctrlRecentGames.cs b/UI/Controls/ctrlRecentGames.cs index ec69646..4a3faf0 100644 --- a/UI/Controls/ctrlRecentGames.cs +++ b/UI/Controls/ctrlRecentGames.cs @@ -229,7 +229,7 @@ namespace Mesen.GUI.Controls private void tmrInput_Tick(object sender, EventArgs e) { //Use player 1's controls to navigate the recent game selection screen - if(Application.OpenForms.Count > 0 && Application.OpenForms[0].ContainsFocus && !EmuRunner.IsRunning()) { + if(frmMain.Instance?.ContainsFocus == true && !EmuRunner.IsRunning()) { List keyCodes = InputApi.GetPressedKeys(); uint keyCode = keyCodes.Count > 0 ? keyCodes[0] : 0; if(keyCode > 0) { diff --git a/UI/Emulation/CursorManager.cs b/UI/Emulation/CursorManager.cs index 363a5de..6edf277 100644 --- a/UI/Emulation/CursorManager.cs +++ b/UI/Emulation/CursorManager.cs @@ -106,7 +106,7 @@ namespace Mesen.GUI.Emulation if(_mouseCaptured && AllowMouseCapture) { HideMouse(); _tmrHideMouse.Stop(); - Form frm = Application.OpenForms[0]; + Form frm = frmMain.Instance; Point centerPos = frm.PointToScreen(new Point(frm.Width / 2, frm.Height / 2)); Point diff = new Point(Cursor.Position.X - centerPos.X, Cursor.Position.Y - centerPos.Y); if(diff.X != 0 || diff.Y != 0) { @@ -185,7 +185,7 @@ namespace Mesen.GUI.Emulation } _mouseCaptured = true; HideMouse(); - Form frm = Application.OpenForms[0]; + Form frm = frmMain.Instance; Point centerPos = frm.PointToScreen(new Point(frm.Width / 2, frm.Height / 2)); Cursor.Position = centerPos; } diff --git a/UI/Emulation/EmuRunner.cs b/UI/Emulation/EmuRunner.cs index 8468837..7bf70e4 100644 --- a/UI/Emulation/EmuRunner.cs +++ b/UI/Emulation/EmuRunner.cs @@ -64,7 +64,7 @@ namespace Mesen.GUI.Emulation ofd.InitialDirectory = ConfigManager.Config.RecentFiles.Items[0].RomFile.Folder; } - if(ofd.ShowDialog(Application.OpenForms[0]) == DialogResult.OK) { + if(ofd.ShowDialog(frmMain.Instance) == DialogResult.OK) { LoadRom(ofd.FileName, patchFile); } } diff --git a/UI/Emulation/SaveStateManager.cs b/UI/Emulation/SaveStateManager.cs index 8958946..271c457 100644 --- a/UI/Emulation/SaveStateManager.cs +++ b/UI/Emulation/SaveStateManager.cs @@ -91,7 +91,7 @@ namespace Mesen.GUI.Emulation using(OpenFileDialog ofd = new OpenFileDialog()) { ofd.InitialDirectory = ConfigManager.SaveStateFolder; ofd.SetFilter(ResourceHelper.GetMessage("FilterSavestate")); - if(ofd.ShowDialog(Application.OpenForms[0]) == DialogResult.OK) { + if(ofd.ShowDialog(frmMain.Instance) == DialogResult.OK) { EmuApi.LoadStateFile(ofd.FileName); } } @@ -105,7 +105,7 @@ namespace Mesen.GUI.Emulation sfd.InitialDirectory = ConfigManager.SaveStateFolder; sfd.FileName = EmuApi.GetRomInfo().GetRomName() + ".mss"; sfd.SetFilter(ResourceHelper.GetMessage("FilterSavestate")); - if(sfd.ShowDialog(Application.OpenForms[0]) == DialogResult.OK) { + if(sfd.ShowDialog(frmMain.Instance) == DialogResult.OK) { EmuApi.SaveStateFile(sfd.FileName); } } diff --git a/UI/Emulation/ShortcutHandler.cs b/UI/Emulation/ShortcutHandler.cs index be28d0a..aa09e18 100644 --- a/UI/Emulation/ShortcutHandler.cs +++ b/UI/Emulation/ShortcutHandler.cs @@ -75,7 +75,7 @@ namespace Mesen.GUI.Emulation case EmulatorShortcut.Reset: EmuApi.Reset(); break; case EmulatorShortcut.PowerCycle: EmuApi.PowerCycle(); break; case EmulatorShortcut.PowerOff: Task.Run(() => EmuApi.Stop()); restoreFullscreen = false; break; - case EmulatorShortcut.Exit: Application.OpenForms[0].Close(); restoreFullscreen = false; break; + case EmulatorShortcut.Exit: frmMain.Instance.Close(); restoreFullscreen = false; break; case EmulatorShortcut.ToggleAudio: ToggleAudio(); break; case EmulatorShortcut.ToggleFps: ToggleFps(); break; @@ -152,7 +152,7 @@ namespace Mesen.GUI.Emulation ofd.InitialDirectory = ConfigManager.Config.RecentFiles.Items[0].RomFile.Folder; } - if(ofd.ShowDialog(Application.OpenForms[0]) == DialogResult.OK) { + if(ofd.ShowDialog(frmMain.Instance) == DialogResult.OK) { EmuRunner.LoadFile(ofd.FileName); } } diff --git a/UI/Forms/MesenMsgBox.cs b/UI/Forms/MesenMsgBox.cs index b1d5c59..37e4276 100644 --- a/UI/Forms/MesenMsgBox.cs +++ b/UI/Forms/MesenMsgBox.cs @@ -20,7 +20,7 @@ namespace Mesen.GUI.Forms return MessageBox.Show(string.Format("Critical error (" + text + ")"), "Mesen-S", buttons, icon); } } else { - Form mainForm = Application.OpenForms.Count > 0 ? Application.OpenForms[0] : null; + Form mainForm = frmMain.Instance; if(mainForm?.InvokeRequired == true) { DialogResult result = DialogResult.Cancel; mainForm.Invoke((Action)(() => { diff --git a/UI/Forms/Tools/frmCheatList.cs b/UI/Forms/Tools/frmCheatList.cs index 778dab8..5f674fc 100644 --- a/UI/Forms/Tools/frmCheatList.cs +++ b/UI/Forms/Tools/frmCheatList.cs @@ -33,7 +33,7 @@ namespace Mesen.GUI.Forms frmCheatList frm = new frmCheatList(); _instance = frm; frm.Closed += (s, e) => { _instance = null; }; - frm.Show(null, Application.OpenForms[0]); + frm.Show(null, frmMain.Instance); } protected override void OnLoad(EventArgs e) diff --git a/UI/Forms/frmConfigWizard.cs b/UI/Forms/frmConfigWizard.cs index 806a212..e8d6db6 100644 --- a/UI/Forms/frmConfigWizard.cs +++ b/UI/Forms/frmConfigWizard.cs @@ -145,7 +145,7 @@ namespace Mesen.GUI.Forms private void lblCancel_Click(object sender, EventArgs e) { - Application.Exit(); + Close(); } private void radStoragePortable_CheckedChanged(object sender, EventArgs e) diff --git a/UI/Forms/frmMain.cs b/UI/Forms/frmMain.cs index 53cf097..98c8115 100644 --- a/UI/Forms/frmMain.cs +++ b/UI/Forms/frmMain.cs @@ -29,8 +29,12 @@ namespace Mesen.GUI.Forms private DisplayManager _displayManager; private CommandLineHelper _commandLine; + public static frmMain Instance { get; private set; } + public frmMain(string[] args) { + frmMain.Instance = this; + InitializeComponent(); if(DesignMode) { return; diff --git a/UI/Forms/frmSelectRom.cs b/UI/Forms/frmSelectRom.cs index d70dcb4..32159e9 100644 --- a/UI/Forms/frmSelectRom.cs +++ b/UI/Forms/frmSelectRom.cs @@ -91,7 +91,7 @@ namespace Mesen.GUI.Forms if(archiveRomList.Count > 1) { using(frmSelectRom frm = new frmSelectRom(archiveRomList)) { - if(frm.ShowDialog(null, Application.OpenForms[0]) == DialogResult.OK) { + if(frm.ShowDialog(null, frmMain.Instance) == DialogResult.OK) { ArchiveRomEntry entry = frm.lstRoms.SelectedItem as ArchiveRomEntry; resource.InnerFile = entry.Filename; if(!entry.IsUtf8) { diff --git a/UI/Interop/EmuApi.cs b/UI/Interop/EmuApi.cs index 719766c..eaad0ac 100644 --- a/UI/Interop/EmuApi.cs +++ b/UI/Interop/EmuApi.cs @@ -25,7 +25,7 @@ namespace Mesen.GUI UInt32 revision = version & 0xFF; UInt32 minor = (version >> 8) & 0xFF; UInt32 major = (version >> 16) & 0xFFFF; - return new Version((int)major, (int)minor, (int)revision, 0); + return new Version((int)major, (int)minor, (int)revision); } [DllImport(DllPath)] public static extern IntPtr RegisterNotificationCallback(NotificationListener.NotificationCallback callback); diff --git a/UI/Program.cs b/UI/Program.cs index 2d08c90..296317d 100644 --- a/UI/Program.cs +++ b/UI/Program.cs @@ -75,7 +75,6 @@ namespace Mesen.GUI Application.Run(new frmConfigWizard()); if(ConfigManager.GetConfigFile() == null) { - Application.Exit(); return; } } diff --git a/UI/Updates/UpdateHelper.cs b/UI/Updates/UpdateHelper.cs index 774bf99..11a7763 100644 --- a/UI/Updates/UpdateHelper.cs +++ b/UI/Updates/UpdateHelper.cs @@ -47,10 +47,10 @@ namespace Mesen.GUI.Updates string donateText = xmlDoc.SelectSingleNode("VersionInfo/DonateText")?.InnerText; if(latestVersion > currentVersion) { - Application.OpenForms[0].BeginInvoke((MethodInvoker)(() => { + frmMain.Instance.BeginInvoke((MethodInvoker)(() => { using(frmUpdatePrompt frmUpdate = new frmUpdatePrompt(currentVersion, latestVersion, changeLog, fileHash, donateText)) { - if(frmUpdate.ShowDialog(null, Application.OpenForms[0]) == DialogResult.OK) { - Application.Exit(); + if(frmUpdate.ShowDialog(null, frmMain.Instance) == DialogResult.OK) { + frmMain.Instance.Close(); } } })); diff --git a/UI/Updates/frmUpdatePrompt.cs b/UI/Updates/frmUpdatePrompt.cs index 5d2c529..37b7751 100644 --- a/UI/Updates/frmUpdatePrompt.cs +++ b/UI/Updates/frmUpdatePrompt.cs @@ -62,27 +62,28 @@ namespace Mesen.GUI.Updates this.Close(); #else string destFilePath = System.Reflection.Assembly.GetEntryAssembly().Location; - string srcFilePath = Path.Combine(ConfigManager.DownloadFolder, "Mesen." + lblLatestVersionString.Text + ".exe"); - string backupFilePath = Path.Combine(ConfigManager.BackupFolder, "Mesen." + lblCurrentVersionString.Text + ".exe"); + string srcFilePath = Path.Combine(ConfigManager.DownloadFolder, "Mesen-S." + lblLatestVersionString.Text + ".exe"); + string backupFilePath = Path.Combine(ConfigManager.BackupFolder, "Mesen-S." + lblCurrentVersionString.Text + ".exe"); string updateHelper = Path.Combine(ConfigManager.HomeFolder, "Resources", "MesenUpdater.exe"); if(!File.Exists(updateHelper)) { MesenMsgBox.Show("UpdaterNotFound", MessageBoxButtons.OK, MessageBoxIcon.Error); DialogResult = DialogResult.Cancel; } else if(!string.IsNullOrWhiteSpace(srcFilePath)) { - frmDownloadProgress frmDownload = new frmDownloadProgress("https://www.mesen.ca/snes/Services/GetLatestVersion.php?a=download&p=win&v=" + EmuApi.GetMesenVersion().ToString(3), srcFilePath); - if(frmDownload.ShowDialog() == DialogResult.OK) { - FileInfo fileInfo = new FileInfo(srcFilePath); - if(fileInfo.Length > 0 && ResourceExtractor.GetSha1Hash(File.ReadAllBytes(srcFilePath)) == _fileHash) { - if(Program.IsMono) { - Process.Start("mono", string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", updateHelper, srcFilePath, destFilePath, backupFilePath)); + using(frmDownloadProgress frmDownload = new frmDownloadProgress("https://www.mesen.ca/snes/Services/GetLatestVersion.php?a=download&p=win&v=" + EmuApi.GetMesenVersion().ToString(3), srcFilePath)) { + if(frmDownload.ShowDialog() == DialogResult.OK) { + FileInfo fileInfo = new FileInfo(srcFilePath); + if(fileInfo.Length > 0 && ResourceExtractor.GetSha1Hash(File.ReadAllBytes(srcFilePath)) == _fileHash) { + if(Program.IsMono) { + Process.Start("mono", string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", updateHelper, srcFilePath, destFilePath, backupFilePath)); + } else { + Process.Start(updateHelper, string.Format("\"{0}\" \"{1}\" \"{2}\"", srcFilePath, destFilePath, backupFilePath)); + } } else { - Process.Start(updateHelper, string.Format("\"{0}\" \"{1}\" \"{2}\"", srcFilePath, destFilePath, backupFilePath)); + //Download failed, mismatching hashes + MesenMsgBox.Show("UpdateDownloadFailed", MessageBoxButtons.OK, MessageBoxIcon.Error); + DialogResult = DialogResult.Cancel; } - } else { - //Download failed, mismatching hashes - MesenMsgBox.Show("UpdateDownloadFailed", MessageBoxButtons.OK, MessageBoxIcon.Error); - DialogResult = DialogResult.Cancel; } } } diff --git a/UI/Utilities/FirmwareHelper.cs b/UI/Utilities/FirmwareHelper.cs index f49c15e..9eac933 100644 --- a/UI/Utilities/FirmwareHelper.cs +++ b/UI/Utilities/FirmwareHelper.cs @@ -50,7 +50,7 @@ namespace Mesen.GUI.Utilities if(MesenMsgBox.Show("FirmwareNotFound", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, msg.FirmwareType.ToString(), filename, msg.Size.ToString()) == DialogResult.OK) { using(OpenFileDialog ofd = new OpenFileDialog()) { ofd.SetFilter(ResourceHelper.GetMessage("FilterAll")); - if(ofd.ShowDialog(Application.OpenForms[0]) == DialogResult.OK) { + if(ofd.ShowDialog(frmMain.Instance) == DialogResult.OK) { if(GetFileHash(ofd.FileName) != GetExpectedHash(msg.FirmwareType)) { if(MesenMsgBox.Show("FirmwareMismatch", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, msg.FirmwareType.ToString(), GetFileHash(ofd.FileName), GetExpectedHash(msg.FirmwareType)) != DialogResult.OK) { //Files don't match and user cancelled the action diff --git a/UpdateHelper/Program.cs b/UpdateHelper/Program.cs index dbc3274..e49f9f8 100644 --- a/UpdateHelper/Program.cs +++ b/UpdateHelper/Program.cs @@ -19,6 +19,18 @@ namespace MesenUpdater string backupDestFile = args[2]; bool isAdmin = args.Length > 3 && args[3] == "admin"; + //Wait a bit for the application to shut down before trying to kill it + System.Threading.Thread.Sleep(1000); + try { + foreach(Process process in Process.GetProcessesByName("Mesen-S")) { + try { + if(process.MainModule.FileName == destFile) { + process.Kill(); + } + } catch { } + } + } catch { } + int retryCount = 0; while(retryCount < 10) { try {