2012-08-21 08:17:17 +00:00
|
|
|
|
#region Using Statements
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Reflection;
|
2012-08-29 18:07:54 +00:00
|
|
|
|
using ANX.Framework.NonXNA.Reflection;
|
2015-04-08 14:50:03 +02:00
|
|
|
|
using ANX.Framework.Content.Pipeline.Helpers;
|
|
|
|
|
using ANX.Framework.NonXNA.Development;
|
2012-08-21 08:17:17 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
|
|
|
|
|
|
|
|
|
namespace ANX.Framework.Content.Pipeline.Tasks
|
|
|
|
|
{
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
2012-08-21 08:17:17 +00:00
|
|
|
|
public class ImporterManager
|
|
|
|
|
{
|
|
|
|
|
private Dictionary<String, Type> importerTypes = new Dictionary<string,Type>();
|
2012-09-28 10:43:38 +00:00
|
|
|
|
private Dictionary<String, String> defaultProcessor = new Dictionary<string, string>();
|
2012-08-21 08:17:17 +00:00
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
2012-08-21 08:17:17 +00:00
|
|
|
|
public ImporterManager()
|
|
|
|
|
{
|
|
|
|
|
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
|
|
|
|
|
{
|
2015-04-08 14:50:03 +02:00
|
|
|
|
if (!AssemblyHelper.IsValidForPipeline(assembly.GetName()))
|
|
|
|
|
continue;
|
2013-06-03 16:18:06 +00:00
|
|
|
|
#if LINUX
|
2013-06-03 20:12:07 +00:00
|
|
|
|
Console.WriteLine("ImporterManager: Checking " + assembly.FullName);
|
2013-06-03 16:18:06 +00:00
|
|
|
|
#endif
|
2015-04-26 19:47:26 +02:00
|
|
|
|
foreach (Type type in ANX.Framework.NonXNA.Reflection.TypeHelper.SafelyExtractTypesFrom(assembly))
|
2012-08-21 08:17:17 +00:00
|
|
|
|
{
|
2012-11-17 18:05:21 +00:00
|
|
|
|
if (type == null)
|
|
|
|
|
continue;
|
2012-08-29 18:07:54 +00:00
|
|
|
|
ContentImporterAttribute[] value = (ContentImporterAttribute[])type.GetCustomAttributes(typeof(ContentImporterAttribute), true);
|
2012-08-21 08:17:17 +00:00
|
|
|
|
if (value.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
importerTypes[type.Name] = type;
|
2012-09-28 10:43:38 +00:00
|
|
|
|
|
|
|
|
|
foreach (ContentImporterAttribute cia in value)
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrEmpty(cia.DefaultProcessor))
|
|
|
|
|
{
|
|
|
|
|
defaultProcessor.Add(type.Name, cia.DefaultProcessor);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-21 08:17:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
2012-08-21 08:17:17 +00:00
|
|
|
|
public IContentImporter GetInstance(string importerName)
|
|
|
|
|
{
|
|
|
|
|
Type type;
|
|
|
|
|
if (!this.importerTypes.TryGetValue(importerName, out type))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(String.Format("can't find importer {0}", importerName));
|
|
|
|
|
}
|
|
|
|
|
return (IContentImporter)Activator.CreateInstance(type);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
|
|
|
|
public string GetImporterDisplayName(string importerName)
|
|
|
|
|
{
|
|
|
|
|
var value = this.AvailableImporters.FirstOrDefault((x) => x.Key == importerName);
|
|
|
|
|
if (value.Value != null)
|
|
|
|
|
{
|
|
|
|
|
var attribute = value.Value.GetCustomAttributes(typeof(ContentImporterAttribute), true).Cast<ContentImporterAttribute>().FirstOrDefault();
|
|
|
|
|
if (attribute != null && !string.IsNullOrEmpty(attribute.DisplayName))
|
|
|
|
|
{
|
|
|
|
|
return attribute.DisplayName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return importerName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetImporterName(string displayName)
|
|
|
|
|
{
|
|
|
|
|
foreach (var value in this.AvailableImporters)
|
|
|
|
|
{
|
|
|
|
|
var attribute = value.Value.GetCustomAttributes(typeof(ContentImporterAttribute), true).Cast<ContentImporterAttribute>().FirstOrDefault();
|
|
|
|
|
if (attribute != null)
|
|
|
|
|
{
|
|
|
|
|
if (attribute.DisplayName == displayName)
|
|
|
|
|
{
|
|
|
|
|
return value.Key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return displayName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-09-28 10:43:38 +00:00
|
|
|
|
public String GetDefaultProcessor(string importerName)
|
|
|
|
|
{
|
|
|
|
|
if (defaultProcessor.ContainsKey(importerName))
|
|
|
|
|
{
|
|
|
|
|
return defaultProcessor[importerName];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
2015-04-26 19:47:26 +02:00
|
|
|
|
public String GuessImporterByFileExtension(string filename)
|
2012-08-21 08:17:17 +00:00
|
|
|
|
{
|
|
|
|
|
String extension = System.IO.Path.GetExtension(filename);
|
|
|
|
|
|
2015-04-26 19:47:26 +02:00
|
|
|
|
foreach (var type in this.importerTypes.Values)
|
2012-08-21 08:17:17 +00:00
|
|
|
|
{
|
2015-04-26 19:47:26 +02:00
|
|
|
|
ContentImporterAttribute[] value = (ContentImporterAttribute[])type.GetCustomAttributes(typeof(ContentImporterAttribute), true);
|
|
|
|
|
foreach (ContentImporterAttribute cia in value)
|
2012-08-21 08:17:17 +00:00
|
|
|
|
{
|
2015-04-26 19:47:26 +02:00
|
|
|
|
foreach (string fe in cia.FileExtensions)
|
2012-08-21 08:17:17 +00:00
|
|
|
|
{
|
2015-04-26 19:47:26 +02:00
|
|
|
|
if (string.Equals(fe, extension, StringComparison.InvariantCultureIgnoreCase))
|
2012-08-21 08:17:17 +00:00
|
|
|
|
{
|
2015-04-26 19:47:26 +02:00
|
|
|
|
return type.Name;
|
2012-08-21 08:17:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
2012-10-05 11:19:08 +00:00
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
2012-10-05 11:19:08 +00:00
|
|
|
|
public IEnumerable<KeyValuePair<string, Type>> AvailableImporters
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return importerTypes;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-21 08:17:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|