Cheats: Improved export code + fixed import bug

This commit is contained in:
Souryo 2016-08-22 20:25:44 -04:00
parent 44c359bd6f
commit 82884113d7
3 changed files with 26 additions and 20 deletions

View File

@ -13,30 +13,36 @@ namespace Mesen.GUI.Forms.Cheats
public static void Export(string filename, IEnumerable<CheatInfo> cheats) public static void Export(string filename, IEnumerable<CheatInfo> cheats)
{ {
//Exports to an XML format compatible with Nestopia's, but with an extra flag (isPrgOffset) //Exports to an XML format compatible with Nestopia's, but with an extra flag (isPrgOffset)
string xml = "<cheats MesenCheatFile=\"true\" version=\"1.0\">"; XmlWriter writer = XmlWriter.Create(filename, new XmlWriterSettings() { Indent = true });
writer.WriteStartElement("cheats");
writer.WriteAttributeString("MesenCheatFile", "true");
writer.WriteAttributeString("version", "1.0");
foreach(CheatInfo cheat in cheats) { foreach(CheatInfo cheat in cheats) {
string enabled = cheat.Enabled ? "1" : "0"; writer.WriteStartElement("cheat");
string gameCrc = "0x" + cheat.GameCrc; writer.WriteAttributeString("enabled", cheat.Enabled ? "1" : "0");
string address = "0x" + cheat.Address.ToString("X4"); writer.WriteAttributeString("game", "0x" + cheat.GameCrc);
string value = "0x" + cheat.Value.ToString("X2"); writer.WriteAttributeString("gameName", cheat.GameName);
string compare = "0x" + cheat.CompareValue.ToString("X2");
string genie = cheat.GameGenieCode;
string rocky = cheat.ProActionRockyCode.ToString("X8");
string genieTag = cheat.CheatType == CheatType.GameGenie ? $"<genie>{genie}</genie>" : ""; switch(cheat.CheatType) {
string rockyTag = cheat.CheatType == CheatType.ProActionRocky ? $"<rocky>{rocky}</rocky>" : ""; case CheatType.GameGenie: writer.WriteElementString("genie", cheat.GameGenieCode); break;
string customTags = cheat.CheatType == CheatType.Custom ? $"<address>{address}</address><value>{value}</value><compare>{compare}</compare>" : ""; case CheatType.ProActionRocky: writer.WriteElementString("rocky", cheat.ProActionRockyCode.ToString("X8")); break;
case CheatType.Custom:
string prgAddress = !cheat.IsRelativeAddress ? "<isPrgOffset>true</isPrgOffset>" : ""; writer.WriteElementString("address", "0x" + cheat.Address.ToString("X4"));
writer.WriteElementString("value", "0x" + cheat.Value.ToString("X2"));
xml += $"<cheat enabled=\"{enabled}\" game=\"{gameCrc}\" gameName=\"{cheat.GameName}\">{genieTag}{rockyTag}{customTags}{prgAddress}<description>{cheat.CheatName}</description></cheat>"; writer.WriteElementString("compare", "0x" + cheat.CompareValue.ToString("X2"));
if(!cheat.IsRelativeAddress) {
writer.WriteElementString("isPrgOffset", "true");
}
break;
}
writer.WriteElementString("description", cheat.CheatName);
writer.WriteEndElement();
} }
xml += "</cheats>"; writer.WriteEndElement();
XmlDocument xmlDoc = new XmlDocument(); writer.Flush();
xmlDoc.LoadXml(xml); writer.Close();
xmlDoc.Save(filename);
} }
} }
} }

View File

@ -70,7 +70,7 @@ namespace Mesen.GUI.Forms.Cheats
var address = node.SelectSingleNode("address"); var address = node.SelectSingleNode("address");
var value = node.SelectSingleNode("value"); var value = node.SelectSingleNode("value");
var compare = node.SelectSingleNode("compare"); var compare = node.SelectSingleNode("compare");
bool isPrgOffset = node.SelectSingleNode("isPrgOffset")?.Value == "true"; bool isPrgOffset = node.SelectSingleNode("isPrgOffset")?.InnerText == "true";
var cheat = new CheatInfo(); var cheat = new CheatInfo();
cheat.GameCrc = crc; cheat.GameCrc = crc;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 251 B

After

Width:  |  Height:  |  Size: 251 B