2019-03-12 09:15:57 -04:00
|
|
|
|
using Mesen.GUI.Config;
|
|
|
|
|
using Mesen.GUI.Forms;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-03-31 15:51:13 -04:00
|
|
|
|
using System.IO;
|
2019-03-12 09:15:57 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Emulation
|
|
|
|
|
{
|
|
|
|
|
public static class EmuRunner
|
|
|
|
|
{
|
|
|
|
|
private static Thread _emuThread = null;
|
2019-03-31 15:51:13 -04:00
|
|
|
|
private static ResourcePath? _romPath = null;
|
2019-03-12 09:15:57 -04:00
|
|
|
|
|
|
|
|
|
public static void LoadRom(ResourcePath romPath, ResourcePath? patchPath = null)
|
|
|
|
|
{
|
|
|
|
|
if(!frmSelectRom.SelectRom(ref romPath)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 15:51:13 -04:00
|
|
|
|
if(patchPath == null && ConfigManager.Config.Preferences.AutoLoadPatches) {
|
|
|
|
|
string[] extensions = new string[3] { ".ips", ".ups", ".bps" };
|
|
|
|
|
foreach(string ext in extensions) {
|
|
|
|
|
string file = Path.Combine(romPath.Folder, Path.GetFileNameWithoutExtension(romPath.FileName)) + ext;
|
|
|
|
|
if(File.Exists(file)) {
|
|
|
|
|
patchPath = file;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_romPath = romPath;
|
2019-10-08 21:04:32 -04:00
|
|
|
|
if(EmuApi.LoadRom(romPath, patchPath)) {
|
|
|
|
|
ConfigManager.Config.RecentFiles.AddRecentFile(romPath, patchPath);
|
|
|
|
|
}
|
2019-03-14 18:07:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 15:51:13 -04:00
|
|
|
|
public static void LoadPatchFile(string patchFile)
|
|
|
|
|
{
|
|
|
|
|
string patchFolder = Path.GetDirectoryName(patchFile);
|
|
|
|
|
HashSet<string> romExtensions = new HashSet<string>() { ".sfc", ".smc", ".swc", ".fig" };
|
|
|
|
|
List<string> romsInFolder = new List<string>();
|
|
|
|
|
foreach(string filepath in Directory.EnumerateFiles(patchFolder)) {
|
|
|
|
|
if(romExtensions.Contains(Path.GetExtension(filepath).ToLowerInvariant())) {
|
|
|
|
|
romsInFolder.Add(filepath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(romsInFolder.Count == 1) {
|
|
|
|
|
//There is a single rom in the same folder as the IPS/BPS patch, use it automatically
|
|
|
|
|
LoadRom(romsInFolder[0], patchFile);
|
|
|
|
|
} else {
|
|
|
|
|
if(!IsRunning() || !_romPath.HasValue) {
|
|
|
|
|
//Prompt the user for a rom to load
|
|
|
|
|
if(MesenMsgBox.Show("SelectRomIps", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
|
|
|
|
|
using(OpenFileDialog ofd = new OpenFileDialog()) {
|
|
|
|
|
ofd.SetFilter(ResourceHelper.GetMessage("FilterRom"));
|
|
|
|
|
if(ConfigManager.Config.RecentFiles.Items.Count > 0) {
|
|
|
|
|
ofd.InitialDirectory = ConfigManager.Config.RecentFiles.Items[0].RomFile.Folder;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-14 15:04:26 -04:00
|
|
|
|
if(ofd.ShowDialog(frmMain.Instance) == DialogResult.OK) {
|
2019-03-31 15:51:13 -04:00
|
|
|
|
LoadRom(ofd.FileName, patchFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if(MesenMsgBox.Show("PatchAndReset", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
|
|
|
|
|
//Confirm that the user wants to patch the current rom and reset
|
|
|
|
|
LoadRom(_romPath.Value, patchFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 18:07:25 -04:00
|
|
|
|
public static void LoadRecentGame(string recentGameArchivePath)
|
|
|
|
|
{
|
|
|
|
|
EmuApi.LoadRecentGame(recentGameArchivePath, false /* TODO , ConfigManager.Config.Preferences.GameSelectionScreenResetGame */);
|
|
|
|
|
StartEmulation();
|
|
|
|
|
}
|
2019-03-12 09:15:57 -04:00
|
|
|
|
|
2019-03-31 15:51:13 -04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void LoadFile(string filename)
|
|
|
|
|
{
|
|
|
|
|
if(File.Exists(filename)) {
|
|
|
|
|
if(IsPatchFile(filename)) {
|
|
|
|
|
LoadPatchFile(filename);
|
|
|
|
|
} else if(Path.GetExtension(filename).ToLowerInvariant() == ".mss") {
|
|
|
|
|
EmuApi.LoadStateFile(filename);
|
|
|
|
|
} else {
|
|
|
|
|
LoadRom(filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 20:05:39 -04:00
|
|
|
|
public static void StartEmulation()
|
2019-03-14 18:07:25 -04:00
|
|
|
|
{
|
2019-03-12 09:15:57 -04:00
|
|
|
|
_emuThread = new Thread(() => {
|
|
|
|
|
EmuApi.Run();
|
2019-03-14 18:07:25 -04:00
|
|
|
|
_emuThread = null;
|
2019-03-12 09:15:57 -04:00
|
|
|
|
});
|
|
|
|
|
_emuThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsRunning()
|
|
|
|
|
{
|
|
|
|
|
return _emuThread != null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|