Updates: Avoid Application.Exit usage, attempt to kill existing processes before upgrade

This commit is contained in:
Sour 2019-10-14 15:04:26 -04:00
parent 094cb1857b
commit 201020b6f9
18 changed files with 49 additions and 33 deletions

View file

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

View file

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

View file

@ -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<uint> keyCodes = InputApi.GetPressedKeys();
uint keyCode = keyCodes.Count > 0 ? keyCodes[0] : 0;
if(keyCode > 0) {

View file

@ -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;
}

View file

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

View file

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

View file

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

View file

@ -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)(() => {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -75,7 +75,6 @@ namespace Mesen.GUI
Application.Run(new frmConfigWizard());
if(ConfigManager.GetConfigFile() == null) {
Application.Exit();
return;
}
}

View file

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

View file

@ -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;
}
}
}

View file

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

View file

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