2018-03-27 19:46:15 -04:00
|
|
|
|
using Mesen.GUI.Config;
|
|
|
|
|
using System;
|
2017-08-17 20:12:42 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-08-19 21:09:44 -04:00
|
|
|
|
using System.Windows.Forms;
|
2017-08-17 20:12:42 -04:00
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Debugger
|
|
|
|
|
{
|
|
|
|
|
public class MesenLabelFile
|
|
|
|
|
{
|
|
|
|
|
public static void Import(string path, bool silent = false)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<AddressType, Dictionary<UInt32, CodeLabel>> labels = new Dictionary<AddressType, Dictionary<UInt32, CodeLabel>>() {
|
|
|
|
|
{ AddressType.InternalRam, new Dictionary<uint, CodeLabel>() },
|
|
|
|
|
{ AddressType.PrgRom, new Dictionary<uint, CodeLabel>() },
|
|
|
|
|
{ AddressType.Register, new Dictionary<uint, CodeLabel>() },
|
|
|
|
|
{ AddressType.SaveRam, new Dictionary<uint, CodeLabel>() },
|
|
|
|
|
{ AddressType.WorkRam, new Dictionary<uint, CodeLabel>() }
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-27 19:46:15 -04:00
|
|
|
|
DebugImportConfig config = ConfigManager.Config.DebugInfo.ImportConfig;
|
|
|
|
|
|
2017-08-17 20:12:42 -04:00
|
|
|
|
char[] separator = new char[1] { ':' };
|
|
|
|
|
foreach(string row in File.ReadAllLines(path, Encoding.UTF8)) {
|
|
|
|
|
string[] rowData = row.Split(separator, 4);
|
2017-08-19 21:09:44 -04:00
|
|
|
|
if(rowData.Length < 3) {
|
|
|
|
|
//Invalid row
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-08-17 20:12:42 -04:00
|
|
|
|
AddressType type;
|
2018-03-27 19:46:15 -04:00
|
|
|
|
bool importLabel = false;
|
2017-08-17 20:12:42 -04:00
|
|
|
|
switch(rowData[0][0]) {
|
2018-03-27 19:46:15 -04:00
|
|
|
|
case 'G': type = AddressType.Register; importLabel = config.MlbImportRegisterLabels; break;
|
|
|
|
|
case 'R': type = AddressType.InternalRam; importLabel = config.MlbImportInternalRamLabels; break;
|
|
|
|
|
case 'P': type = AddressType.PrgRom; importLabel = config.MlbImportPrgRomLabels; break;
|
|
|
|
|
case 'S': type = AddressType.SaveRam; importLabel = config.MlbImportSaveRamLabels; break;
|
|
|
|
|
case 'W': type = AddressType.WorkRam; importLabel = config.MlbImportWorkRamLabels; break;
|
2017-08-17 20:12:42 -04:00
|
|
|
|
default: continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint address;
|
2018-03-27 19:46:15 -04:00
|
|
|
|
if(importLabel && UInt32.TryParse(rowData[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address)) {
|
2017-08-17 20:12:42 -04:00
|
|
|
|
CodeLabel codeLabel;
|
|
|
|
|
if(!labels[type].TryGetValue(address, out codeLabel)) {
|
|
|
|
|
codeLabel = new CodeLabel();
|
|
|
|
|
codeLabel.Address = address;
|
|
|
|
|
codeLabel.AddressType = type;
|
|
|
|
|
codeLabel.Label = "";
|
|
|
|
|
codeLabel.Comment = "";
|
|
|
|
|
labels[type][address] = codeLabel;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 19:46:15 -04:00
|
|
|
|
if(rowData.Length > 3 && config.MlbImportComments) {
|
2017-08-17 20:12:42 -04:00
|
|
|
|
codeLabel.Comment = rowData[3].Replace("\\n", "\n");
|
|
|
|
|
}
|
|
|
|
|
codeLabel.Label = rowData[2].Replace("\\n", "\n").Replace("\n", "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 21:09:44 -04:00
|
|
|
|
int labelCount = 0;
|
2017-08-17 20:12:42 -04:00
|
|
|
|
foreach(KeyValuePair<AddressType, Dictionary<UInt32, CodeLabel>> kvp in labels) {
|
|
|
|
|
LabelManager.SetLabels(kvp.Value.Values);
|
2017-08-19 21:09:44 -04:00
|
|
|
|
labelCount += kvp.Value.Values.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!silent) {
|
|
|
|
|
MessageBox.Show($"Import completed with {labelCount} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2017-08-17 20:12:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Export(string path)
|
|
|
|
|
{
|
|
|
|
|
List<CodeLabel> labels = new List<CodeLabel>(LabelManager.GetLabels());
|
|
|
|
|
labels.Sort((CodeLabel a, CodeLabel b) => {
|
|
|
|
|
int result = a.AddressType.CompareTo(b.AddressType);
|
|
|
|
|
if(result == 0) {
|
|
|
|
|
return a.Address.CompareTo(b.Address);
|
|
|
|
|
} else {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach(CodeLabel label in labels) {
|
|
|
|
|
sb.Append(label.ToString() + "\n");
|
|
|
|
|
}
|
|
|
|
|
File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|