using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace SabreTools.Helper
{
///
/// Contains all remappings of known import classes
///
public class Mappings
{
// Local paths
private const string _remappersPath = "Mappings";
// Remapping classes represented by a dictionary of dictionaries (name, (from, to))
private static Dictionary> _datMaps = new Dictionary>();
public static Dictionary> DatMaps
{
get
{
if (_datMaps.Count == 0)
{
CreateRemappings();
}
return _datMaps;
}
}
#region DAT Name Remappings
///
/// Create all remappings to be used by the program
///
private static void CreateRemappings()
{
// Create array of dictionary names
string[] remappings =
{
"Good", "MAME", "MaybeIntro", "NoIntro", "NonGood", "Redump", "TOSEC", "TruRip",
};
// Loop through and add all remappings
foreach (string remapping in remappings)
{
_datMaps.Add(remapping, new Dictionary());
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(Path.Combine(_remappersPath, 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;
}
// If the node is empty, just return so it doesn't crash
if (!node.HasChildNodes)
{
return;
}
// 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")
{
_datMaps[mapping].Add(node.Attributes["from"].Value, node.Attributes["to"].Value);
// 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;
}
}
}
#endregion
}
}