2018-07-29 11:57:49 -04:00
using Mesen.GUI.Config ;
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace Mesen.GUI.Debugger
{
public class NesasmFnsImporter
{
public static void Import ( string path , bool silent = false )
{
//This only works reliably for NROM games with 32kb PRG
DebugState state = new DebugState ( ) ;
InteropEmu . DebugGetState ( ref state ) ;
2019-01-17 23:02:14 -05:00
int errorCount = 0 ;
2018-07-29 11:57:49 -04:00
bool hasLargePrg = state . Cartridge . PrgRomSize ! = 0x8000 ;
if ( ! silent & & hasLargePrg ) {
if ( MessageBox . Show ( $"Warning: Due to .fns file format limitations, imported labels are not reliable for games that have more than 32kb of PRG ROM.\n\nAre you sure you want to import these labels?" , "Mesen" , MessageBoxButtons . YesNo , MessageBoxIcon . Warning ) ! = DialogResult . Yes ) {
return ;
}
}
Dictionary < UInt32 , CodeLabel > labels = new Dictionary < uint , CodeLabel > ( ) ;
char [ ] separator = new char [ 1 ] { '=' } ;
foreach ( string row in File . ReadAllLines ( path , Encoding . UTF8 ) ) {
string [ ] rowData = row . Split ( separator ) ;
if ( rowData . Length < 2 ) {
//Invalid row
continue ;
}
uint address ;
if ( UInt32 . TryParse ( rowData [ 1 ] . Trim ( ) . Substring ( 1 ) , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out address ) ) {
2019-01-17 23:02:14 -05:00
string labelName = rowData [ 0 ] . Trim ( ) ;
if ( ! LabelManager . LabelRegex . IsMatch ( labelName ) ) {
//Reject labels that don't respect the label naming restrictions
errorCount + + ;
continue ;
}
2018-07-29 11:57:49 -04:00
CodeLabel codeLabel ;
if ( ! labels . TryGetValue ( address , out codeLabel ) ) {
codeLabel = new CodeLabel ( ) ;
codeLabel . Address = hasLargePrg ? address : ( address - 0x8000 ) ;
codeLabel . AddressType = hasLargePrg ? AddressType . Register : AddressType . PrgRom ;
codeLabel . Label = "" ;
codeLabel . Comment = "" ;
labels [ address ] = codeLabel ;
}
2019-01-17 23:02:14 -05:00
codeLabel . Label = labelName ;
} else {
errorCount + + ;
2018-07-29 11:57:49 -04:00
}
}
LabelManager . SetLabels ( labels . Values ) ;
if ( ! silent ) {
2019-01-17 23:02:14 -05:00
string message = $"Import completed with {labels.Values.Count} labels imported" ;
if ( errorCount > 0 ) {
message + = $" and {errorCount} error(s)" ;
}
MessageBox . Show ( message , "Mesen" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2018-07-29 11:57:49 -04:00
}
}
}
}