Added support for drag and dropping rom/ips files

This commit is contained in:
Souryo 2016-01-28 23:01:01 -05:00
parent a69a0487b4
commit 9bfa62129a
4 changed files with 94 additions and 57 deletions

View File

@ -15,15 +15,15 @@ class RomLoader
string _filename;
string _ipsFilename;
bool LoadFromZip(stringstream &zipFile);
bool LoadFromStream(stringstream &romFile);
uint32_t GetFileSize(stringstream &file);
bool LoadFromZip(istream &zipFile);
bool LoadFromStream(istream &romFile);
uint32_t GetFileSize(istream &file);
uint8_t* ReadFile(stringstream &file, uint32_t &fileSize);
uint8_t* ReadFile(istream &file, uint32_t &fileSize);
bool LoadFromMemory(uint8_t* buffer, size_t length);
public:
bool LoadFile(string filename, stringstream *filestream = nullptr, string ipsFilename = "");
bool LoadFile(string filename, istream *filestream = nullptr, string ipsFilename = "");
RomData GetRomData();
static uint32_t GetCRC32(string filename);
static string FindMatchingRomInFolder(string folder, string romFilename, uint32_t crc32Hash);

View File

@ -4,7 +4,7 @@
#include "iNesLoader.h"
#include "FdsLoader.h"
bool RomLoader::LoadFromZip(stringstream &zipFile)
bool RomLoader::LoadFromZip(istream &zipFile)
{
bool result = false;
@ -36,7 +36,7 @@ bool RomLoader::LoadFromZip(stringstream &zipFile)
return result;
}
bool RomLoader::LoadFromStream(stringstream &romFile)
bool RomLoader::LoadFromStream(istream &romFile)
{
uint32_t fileSize;
uint8_t* buffer = ReadFile(romFile, fileSize);
@ -46,7 +46,7 @@ bool RomLoader::LoadFromStream(stringstream &romFile)
return result;
}
uint32_t RomLoader::GetFileSize(stringstream &file)
uint32_t RomLoader::GetFileSize(istream &file)
{
file.seekg(0, ios::end);
uint32_t fileSize = (uint32_t)file.tellg();
@ -55,7 +55,7 @@ uint32_t RomLoader::GetFileSize(stringstream &file)
return fileSize;
}
uint8_t* RomLoader::ReadFile(stringstream &file, uint32_t &fileSize)
uint8_t* RomLoader::ReadFile(istream &file, uint32_t &fileSize)
{
fileSize = GetFileSize(file);
@ -79,6 +79,8 @@ bool RomLoader::LoadFromMemory(uint8_t* buffer, size_t length)
} else if(memcmp(buffer, "FDS\x1a", 4) == 0 || memcmp(buffer, "\x1*NINTENDO-HVC*", 15) == 0) {
FdsLoader loader;
_romData = loader.LoadRom(fileData, _filename);
} else {
_romData.Error = true;
}
_romData.RawData = fileData;
@ -87,31 +89,34 @@ bool RomLoader::LoadFromMemory(uint8_t* buffer, size_t length)
return !_romData.Error;
}
bool RomLoader::LoadFile(string filename, stringstream *filestream, string ipsFilename)
bool RomLoader::LoadFile(string filename, istream *filestream, string ipsFilename)
{
_filename = filename;
_ipsFilename = ipsFilename;
stringstream ss;
ifstream file;
istream* input = nullptr;
if(!filestream) {
ifstream file(filename, ios::in | ios::binary);
file.open(filename, ios::in | ios::binary);
if(file) {
ss << file.rdbuf();
file.close();
filestream = &ss;
input = &file;
}
} else {
input = filestream;
}
char header[2];
filestream->seekg(0, ios::beg);
filestream->read(header, 2);
filestream->seekg(0, ios::beg);
char header[15];
input->seekg(0, ios::beg);
input->read(header, 15);
input->seekg(0, ios::beg);
if(memcmp(header, "PK", 2) == 0) {
return LoadFromZip(*filestream);
} else {
return LoadFromStream(*filestream);
return LoadFromZip(*input);
} else if(memcmp(header, "NES\x1a", 4) == 0 || memcmp(header, "FDS\x1a", 4) == 0 || memcmp(header, "\x1*NINTENDO-HVC*", 15) == 0) {
return LoadFromStream(*input);
}
return false;
}
RomData RomLoader::GetRomData()

View File

@ -52,6 +52,7 @@
this.mnuReset = new System.Windows.Forms.ToolStripMenuItem();
this.mnuStop = new System.Windows.Forms.ToolStripMenuItem();
this.sepFdsDisk = new System.Windows.Forms.ToolStripSeparator();
this.mnuSwitchDiskSide = new System.Windows.Forms.ToolStripMenuItem();
this.mnuSelectDisk = new System.Windows.Forms.ToolStripMenuItem();
this.mnuEjectDisk = new System.Windows.Forms.ToolStripMenuItem();
this.mnuOptions = new System.Windows.Forms.ToolStripMenuItem();
@ -119,7 +120,6 @@
this.mnuAbout = new System.Windows.Forms.ToolStripMenuItem();
this.menuTimer = new System.Windows.Forms.Timer(this.components);
this.dxViewer = new Mesen.GUI.Controls.DXViewer();
this.mnuSwitchDiskSide = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
@ -246,6 +246,14 @@
this.sepFdsDisk.Name = "sepFdsDisk";
this.sepFdsDisk.Size = new System.Drawing.Size(197, 6);
//
// mnuSwitchDiskSide
//
this.mnuSwitchDiskSide.Name = "mnuSwitchDiskSide";
this.mnuSwitchDiskSide.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.B)));
this.mnuSwitchDiskSide.Size = new System.Drawing.Size(200, 22);
this.mnuSwitchDiskSide.Text = "Switch Disk Side";
this.mnuSwitchDiskSide.Click += new System.EventHandler(this.mnuSwitchDiskSide_Click);
//
// mnuSelectDisk
//
this.mnuSelectDisk.Name = "mnuSelectDisk";
@ -772,16 +780,9 @@
this.dxViewer.Size = new System.Drawing.Size(263, 176);
this.dxViewer.TabIndex = 1;
//
// mnuSwitchDiskSide
//
this.mnuSwitchDiskSide.Name = "mnuSwitchDiskSide";
this.mnuSwitchDiskSide.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.B)));
this.mnuSwitchDiskSide.Size = new System.Drawing.Size(200, 22);
this.mnuSwitchDiskSide.Text = "Switch Disk Side";
this.mnuSwitchDiskSide.Click += new System.EventHandler(this.mnuSwitchDiskSide_Click);
//
// frmMain
//
this.AllowDrop = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
@ -793,6 +794,8 @@
this.MainMenuStrip = this.menuStrip;
this.Name = "frmMain";
this.Text = "Mesen";
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.frmMain_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.frmMain_DragEnter);
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);

View File

@ -225,35 +225,49 @@ namespace Mesen.GUI.Forms
ofd.InitialDirectory = Path.GetDirectoryName(ConfigManager.Config.RecentFiles[0]);
}
if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
if(Path.GetExtension(ofd.FileName).ToLowerInvariant() == ".ips") {
string ipsFile = ofd.FileName;
string romFile = Path.Combine(Path.GetDirectoryName(ofd.FileName), Path.GetFileNameWithoutExtension(ofd.FileName));
LoadFile(ofd.FileName);
}
}
if(File.Exists(romFile+".nes") || File.Exists(romFile+".zip") || File.Exists(romFile+".fds")) {
string ext = string.Empty;
if(File.Exists(romFile+".nes")) ext = ".nes";
if(File.Exists(romFile+".zip")) ext = ".zip";
if(File.Exists(romFile+".fds")) ext = ".fds";
LoadROM(romFile + ext);
InteropEmu.ApplyIpsPatch(ipsFile);
} else {
if(_emuThread == null) {
if(MessageBox.Show("Please select a ROM matching the IPS patch file.", string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) {
ofd.Filter = "All supported formats (*.nes, *.zip, *.fds)|*.NES;*.ZIP;*.FDS|NES Roms (*.nes)|*.NES|Famicom Disk System Roms (*.fds)|*.FDS|ZIP Archives (*.zip)|*.ZIP|All (*.*)|*.*";
if(ConfigManager.Config.RecentFiles.Count > 0) {
ofd.InitialDirectory = Path.GetDirectoryName(ConfigManager.Config.RecentFiles[0]);
}
if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
LoadROM(ofd.FileName);
}
InteropEmu.ApplyIpsPatch(ipsFile);
}
} else if(MessageBox.Show("Patch and reset the current game?", string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) {
InteropEmu.ApplyIpsPatch(ipsFile);
private void LoadFile(string filename)
{
if(Path.GetExtension(filename).ToLowerInvariant() == ".ips") {
LoadIpsFile(filename);
} else {
LoadROM(filename, ConfigManager.Config.PreferenceInfo.AutoLoadIpsPatches);
}
}
private void LoadIpsFile(string filename)
{
string ipsFile = filename;
string romFile = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename));
if(File.Exists(romFile+".nes") || File.Exists(romFile+".zip") || File.Exists(romFile+".fds")) {
string ext = string.Empty;
if(File.Exists(romFile+".nes"))
ext = ".nes";
if(File.Exists(romFile+".zip"))
ext = ".zip";
if(File.Exists(romFile+".fds"))
ext = ".fds";
LoadROM(romFile + ext);
InteropEmu.ApplyIpsPatch(ipsFile);
} else {
if(_emuThread == null) {
if(MessageBox.Show("Please select a ROM matching the IPS patch file.", string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) {
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All supported formats (*.nes, *.zip, *.fds)|*.NES;*.ZIP;*.FDS|NES Roms (*.nes)|*.NES|Famicom Disk System Roms (*.fds)|*.FDS|ZIP Archives (*.zip)|*.ZIP|All (*.*)|*.*";
if(ConfigManager.Config.RecentFiles.Count > 0) {
ofd.InitialDirectory = Path.GetDirectoryName(ConfigManager.Config.RecentFiles[0]);
}
if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
LoadROM(ofd.FileName);
}
InteropEmu.ApplyIpsPatch(ipsFile);
}
} else {
LoadROM(ofd.FileName, ConfigManager.Config.PreferenceInfo.AutoLoadIpsPatches);
} else if(MessageBox.Show("Patch and reset the current game?", string.Empty, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) {
InteropEmu.ApplyIpsPatch(ipsFile);
}
}
}
@ -813,5 +827,20 @@ namespace Mesen.GUI.Forms
}
}
}
private void frmMain_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if(File.Exists(files[0])) {
LoadFile(files[0]);
}
}
private void frmMain_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy;
}
}
}
}