SND\AstrorEnales_cp 74a1ca7417 added cmd compatibility to the converter tool and
now you can convert from anx to xna as well
(only todo is getting the FullName of the XNA assembly
references and extensive testing)
2011-11-10 21:36:49 +00:00

130 lines
3.4 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
namespace XNAToANXConverter
{
public static class ProjectData
{
private const string XnaBaseName = "Microsoft.Xna.Framework";
private const string AnxBaseName = "ANX.Framework";
#region Convert
public static void Convert(string target, string sourceFilepath,
string destinationFilepath)
{
string sourceFolderPath =
sourceFilepath.Replace(Path.GetFileName(sourceFilepath), "");
string destinationFolderPath =
destinationFilepath.Replace(Path.GetFileName(destinationFilepath), "");
if (Directory.Exists(destinationFolderPath) == false)
{
Directory.CreateDirectory(destinationFolderPath);
}
XDocument doc = XDocument.Load(sourceFilepath);
XElement root = doc.Root;
foreach (XElement propertyGroup in root.Elements())
{
if (propertyGroup.Name.LocalName != "ItemGroup")
{
continue;
}
List<XElement> elements = new List<XElement>(propertyGroup.Elements());
foreach (XElement item in elements)
{
if (item.Name.LocalName == "Reference")
{
#region Process Reference
string assemblyPath = item.Attribute("Include").Value;
if (target == "anx")
{
#region ANX
if (assemblyPath.Contains(XnaBaseName))
{
item.Remove();
string anxPath = assemblyPath.Replace(XnaBaseName, AnxBaseName);
if (anxPath.Contains(", Version"))
{
anxPath = anxPath.Substring(0, anxPath.IndexOf(',')) + ".dll";
}
propertyGroup.Add(new XElement("Reference",
new XAttribute("Include", anxPath)));
}
#endregion
}
else
{
#region XNA
if (assemblyPath.Contains(AnxBaseName))
{
item.Remove();
// TODO: FQN of the xna assemby
string xnaPath = assemblyPath.Replace(AnxBaseName, XnaBaseName);
propertyGroup.Add(new XElement("Reference",
new XAttribute("Include", xnaPath)));
}
#endregion
}
#endregion
}
else if (item.Name.LocalName == "Compile")
{
#region Process Compile
string codeFilepath = item.Attribute("Include").Value;
string absolutePath = Path.Combine(sourceFolderPath, codeFilepath);
string text = File.ReadAllText(absolutePath);
if (target == "anx")
{
text = text.Replace(XnaBaseName, AnxBaseName);
}
else
{
text = text.Replace(AnxBaseName, XnaBaseName);
}
string destCodeFolderPath = codeFilepath.Replace(
Path.GetFileName(codeFilepath), "");
destCodeFolderPath = Path.Combine(destinationFolderPath,
destCodeFolderPath);
if (Directory.Exists(destCodeFolderPath) == false)
{
Directory.CreateDirectory(destCodeFolderPath);
}
File.WriteAllText(Path.Combine(destinationFolderPath,
codeFilepath), text);
#endregion
}
else if (item.Name.LocalName == "BootstrapperPackage")
{
if (target == "xna")
{
continue;
}
#region Process BootstrapperPackage
// Remove all bootstrapper tasks for XNA.
string includeName = item.Attribute("Include").Value;
if (includeName.Contains(XnaBaseName))
{
item.Remove();
}
#endregion
}
}
}
doc.Save(destinationFilepath);
}
#endregion
}
}