using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace SabreTools.Helper
{
///
/// Contains all remappings of known import classes
///
public class Remapping
{
// Remapping classes represented by dictionaries
public static Dictionary MAME = new Dictionary();
public static Dictionary NoIntro = new Dictionary();
public static Dictionary Redump = new Dictionary();
public static Dictionary TOSEC = new Dictionary();
public static Dictionary TruRip = new Dictionary();
///
/// Create all remappings to be used by the program
///
public static void CreateRemappings()
{
// Create array of dictionary names
string[] remappings =
{
"MAME", "NoIntro", "Redump", "TOSEC", "TruRip",
};
// Loop through and add all remappings
foreach (string remapping in remappings)
{
RemappingHelper(remapping);
}
}
///
/// Create a remapping from XML
///
/// Name of the mapping to be populated
private static void RemappingHelper(string mapping)
{
// Read in remapping from file
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(File.ReadAllText("Mappings/" + mapping + ".xml"));
}
catch (XmlException ex)
{
Console.WriteLine(mapping + " remappings could not be loaded! " + ex.ToString());
return;
}
// Get the mappings parent node
XmlNode node = doc.FirstChild;
while (node.Name != "mappings")
{
node = node.NextSibling;
}
// Get the first mapping node
node = node.FirstChild;
while (node.NodeType != XmlNodeType.Element && node.Name != "mapping")
{
node = node.NextSibling;
}
// Now read in the mappings
while (node != null && node.Name == "mapping")
{
switch (mapping)
{
case "MAME":
MAME.Add(node.Attributes["from"].Value, node.Attributes["to"].Value);
break;
case "NoIntro":
NoIntro.Add(node.Attributes["from"].Value, node.Attributes["to"].Value);
break;
case "Redump":
Redump.Add(node.Attributes["from"].Value, node.Attributes["to"].Value);
break;
case "TOSEC":
TOSEC.Add(node.Attributes["from"].Value, node.Attributes["to"].Value);
break;
case "TruRip":
TruRip.Add(node.Attributes["from"].Value, node.Attributes["to"].Value);
break;
}
// Get the next node and skip over anything that's not an element
node = node.NextSibling;
if (node == null)
{
break;
}
while (node.NodeType != XmlNodeType.Element && node.Name != "mapping")
{
node = node.NextSibling;
}
}
}
}
}